Page Speed Optimization Libraries
1.2.24.1
|
00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00025 00026 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_ 00027 #define NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_ 00028 00029 #include <vector> 00030 00031 #include "base/logging.h" 00032 #include "net/instaweb/http/public/http_value.h" 00033 #include "net/instaweb/http/public/meta_data.h" 00034 #include "net/instaweb/http/public/response_headers.h" 00035 #include "net/instaweb/util/public/basictypes.h" 00036 #include "net/instaweb/util/public/ref_counted_ptr.h" 00037 #include "net/instaweb/util/public/string.h" 00038 #include "net/instaweb/util/public/string_util.h" 00039 00040 namespace net_instaweb { 00041 00042 class CachedResult; 00043 struct ContentType; 00044 class InputInfo; 00045 class MessageHandler; 00046 class Resource; 00047 class ServerContext; 00048 class RewriteOptions; 00049 00050 typedef RefCountedPtr<Resource> ResourcePtr; 00051 typedef std::vector<ResourcePtr> ResourceVector; 00052 00053 class Resource : public RefCounted<Resource> { 00054 public: 00055 enum HashHint { 00056 kOmitInputHash, 00057 kIncludeInputHash 00058 }; 00059 00062 enum NotCacheablePolicy { 00063 kLoadEvenIfNotCacheable, 00064 kReportFailureIfNotCacheable, 00065 }; 00066 00068 enum FetchResponseStatus { 00069 kFetchStatusNotSet, 00070 kFetchStatusOK, 00071 kFetchStatusUncacheable, 00072 kFetchStatus4xxError, 00073 kFetchStatusOther, 00074 }; 00075 00076 Resource(ServerContext* server_context, const ContentType* type); 00077 00079 ServerContext* server_context() const { return server_context_; } 00080 00083 virtual bool IsValidAndCacheable() const; 00084 00087 bool IsSafeToRewrite() const; 00088 00091 bool loaded() const { return response_headers_.status_code() != 0; } 00092 bool HttpStatusOk() const { 00093 return (response_headers_.status_code() == HttpStatus::kOK); 00094 } 00095 00101 GoogleString ContentsHash() const; 00102 00105 void AddInputInfoToPartition(HashHint suggest_include_content_hash, 00106 int index, CachedResult* partition); 00107 00118 virtual void FillInPartitionInputInfo(HashHint suggest_include_content_hash, 00119 InputInfo* input); 00120 00121 void FillInPartitionInputInfoFromResponseHeaders( 00122 const ResponseHeaders& headers, 00123 InputInfo* input); 00124 00128 int64 CacheExpirationTimeMs() const; 00129 00130 StringPiece contents() const { 00131 StringPiece val; 00132 bool got_contents = value_.ExtractContents(&val); 00133 CHECK(got_contents) << "Resource contents read before loading: " << url(); 00134 return val; 00135 } 00136 ResponseHeaders* response_headers() { return &response_headers_; } 00137 const ResponseHeaders* response_headers() const { return &response_headers_; } 00138 const ContentType* type() const { return type_; } 00139 virtual void SetType(const ContentType* type); 00140 00142 StringPiece charset() const { return charset_; } 00143 void set_charset(StringPiece c) { c.CopyToString(&charset_); } 00144 00145 virtual bool IsCacheableTypeOfResource() const { return true; } 00146 00148 virtual GoogleString url() const = 0; 00149 00152 void DetermineContentType(); 00153 00157 virtual const RewriteOptions* rewrite_options() const = 0; 00158 00162 class AsyncCallback { 00163 public: 00164 explicit AsyncCallback(const ResourcePtr& resource) : resource_(resource) {} 00165 00166 virtual ~AsyncCallback(); 00167 virtual void Done(bool lock_ok, bool resource_ok) = 0; 00168 00169 const ResourcePtr& resource() { return resource_; } 00170 00173 virtual bool EnableThreaded() const { return false; } 00174 00175 private: 00176 ResourcePtr resource_; 00177 DISALLOW_COPY_AND_ASSIGN(AsyncCallback); 00178 }; 00179 00182 class FreshenCallback : public AsyncCallback { 00183 public: 00184 explicit FreshenCallback(const ResourcePtr& resource) 00185 : AsyncCallback(resource) {} 00186 00187 virtual ~FreshenCallback(); 00190 virtual InputInfo* input_info() { return NULL; } 00191 00194 virtual void Done(bool lock_failure, bool resource_ok) { 00195 delete this; 00196 } 00197 00198 private: 00199 DISALLOW_COPY_AND_ASSIGN(FreshenCallback); 00200 }; 00201 00207 bool Link(HTTPValue* source, MessageHandler* handler); 00208 00212 virtual void Freshen(FreshenCallback* callback, MessageHandler* handler); 00213 00215 void LinkFallbackValue(HTTPValue* value); 00216 00217 void set_is_background_fetch(bool x) { is_background_fetch_ = x; } 00218 bool is_background_fetch() const { return is_background_fetch_; } 00219 00220 FetchResponseStatus fetch_response_status() { 00221 return fetch_response_status_; 00222 } 00223 00224 void set_fetch_response_status(FetchResponseStatus x) { 00225 fetch_response_status_ = x; 00226 } 00227 00228 protected: 00229 virtual ~Resource(); 00230 REFCOUNT_FRIEND_DECLARATION(Resource); 00231 friend class ServerContext; 00232 friend class RewriteDriver; 00233 friend class UrlReadAsyncFetchCallback; 00234 friend class ResourceManagerHttpCallback; 00235 00239 virtual bool Load(MessageHandler* message_handler) = 0; 00240 00245 virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy, 00246 AsyncCallback* callback, 00247 MessageHandler* message_handler); 00248 00249 ServerContext* server_context_; 00250 00251 const ContentType* type_; 00252 GoogleString charset_; 00253 HTTPValue value_; 00254 ResponseHeaders response_headers_; 00255 00259 HTTPValue fallback_value_; 00260 00261 private: 00263 FetchResponseStatus fetch_response_status_; 00264 00270 bool is_background_fetch_; 00271 DISALLOW_COPY_AND_ASSIGN(Resource); 00272 }; 00273 00274 } 00275 00276 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_