Page Speed Optimization Libraries
1.3.25.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 00146 virtual GoogleString url() const = 0; 00147 00150 void DetermineContentType(); 00151 00155 virtual const RewriteOptions* rewrite_options() const = 0; 00156 00160 class AsyncCallback { 00161 public: 00162 explicit AsyncCallback(const ResourcePtr& resource) : resource_(resource) {} 00163 00164 virtual ~AsyncCallback(); 00165 virtual void Done(bool lock_ok, bool resource_ok) = 0; 00166 00167 const ResourcePtr& resource() { return resource_; } 00168 00171 virtual bool EnableThreaded() const { return false; } 00172 00173 private: 00174 ResourcePtr resource_; 00175 DISALLOW_COPY_AND_ASSIGN(AsyncCallback); 00176 }; 00177 00180 class FreshenCallback : public AsyncCallback { 00181 public: 00182 explicit FreshenCallback(const ResourcePtr& resource) 00183 : AsyncCallback(resource) {} 00184 00185 virtual ~FreshenCallback(); 00188 virtual InputInfo* input_info() { return NULL; } 00189 00192 virtual void Done(bool lock_failure, bool resource_ok) { 00193 delete this; 00194 } 00195 00196 private: 00197 DISALLOW_COPY_AND_ASSIGN(FreshenCallback); 00198 }; 00199 00205 bool Link(HTTPValue* source, MessageHandler* handler); 00206 00210 virtual void Freshen(FreshenCallback* callback, MessageHandler* handler); 00211 00213 void LinkFallbackValue(HTTPValue* value); 00214 00215 void set_is_background_fetch(bool x) { is_background_fetch_ = x; } 00216 bool is_background_fetch() const { return is_background_fetch_; } 00217 00218 FetchResponseStatus fetch_response_status() { 00219 return fetch_response_status_; 00220 } 00221 00222 void set_fetch_response_status(FetchResponseStatus x) { 00223 fetch_response_status_ = x; 00224 } 00225 00229 virtual bool UseHttpCache() const = 0; 00230 00231 protected: 00232 virtual ~Resource(); 00233 REFCOUNT_FRIEND_DECLARATION(Resource); 00234 friend class ServerContext; 00235 friend class RewriteDriver; 00236 friend class UrlReadAsyncFetchCallback; 00237 friend class ResourceManagerHttpCallback; 00238 00248 virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy, 00249 AsyncCallback* callback, 00250 MessageHandler* message_handler) = 0; 00251 00252 ServerContext* server_context_; 00253 00254 const ContentType* type_; 00255 GoogleString charset_; 00256 HTTPValue value_; 00257 ResponseHeaders response_headers_; 00258 00262 HTTPValue fallback_value_; 00263 00264 private: 00266 FetchResponseStatus fetch_response_status_; 00267 00273 bool is_background_fetch_; 00274 DISALLOW_COPY_AND_ASSIGN(Resource); 00275 }; 00276 00277 } 00278 00279 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_