Page Speed Optimization Libraries  1.7.30.1
net/instaweb/rewriter/public/resource.h
Go to the documentation of this file.
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/request_context.h"
00035 #include "net/instaweb/http/public/response_headers.h"
00036 #include "net/instaweb/util/public/basictypes.h"
00037 #include "net/instaweb/util/public/ref_counted_ptr.h"
00038 #include "net/instaweb/util/public/string.h"
00039 #include "net/instaweb/util/public/string_util.h"
00040 
00041 namespace net_instaweb {
00042 
00043 class CachedResult;
00044 struct ContentType;
00045 class InputInfo;
00046 class MessageHandler;
00047 class Resource;
00048 class ServerContext;
00049 
00050 typedef RefCountedPtr<Resource> ResourcePtr;
00051 typedef std::vector<ResourcePtr> ResourceVector;
00052 
00053 class Resource : public RefCounted<Resource> {
00054  public:
00055   class AsyncCallback;
00056 
00057   enum HashHint {
00058     kOmitInputHash,
00059     kIncludeInputHash
00060   };
00061 
00064   enum NotCacheablePolicy {
00065     kLoadEvenIfNotCacheable,
00066     kReportFailureIfNotCacheable,
00067   };
00068 
00070   enum FetchResponseStatus {
00071     kFetchStatusNotSet,
00072     kFetchStatusOK,
00073     kFetchStatusUncacheable,
00074     kFetchStatus4xxError,
00075     kFetchStatusDropped,
00076     kFetchStatusOther,
00077   };
00078 
00079   Resource(ServerContext* server_context, const ContentType* type);
00080 
00082   ServerContext* server_context() const { return server_context_; }
00083 
00086   virtual bool IsValidAndCacheable() const;
00087 
00092   bool IsSafeToRewrite(bool rewrite_uncacheable) const;
00093 
00096   bool loaded() const { return response_headers_.status_code() != 0; }
00097   bool HttpStatusOk() const {
00098     return (response_headers_.status_code() == HttpStatus::kOK);
00099   }
00100 
00109   void LoadAsync(NotCacheablePolicy not_cacheable_policy,
00110                  const RequestContextPtr& request_context,
00111                  AsyncCallback* callback);
00112 
00118   virtual void RefreshIfImminentlyExpiring();
00119 
00125   GoogleString ContentsHash() const;
00126 
00129   void AddInputInfoToPartition(HashHint suggest_include_content_hash,
00130                                int index, CachedResult* partition);
00131 
00142   virtual void FillInPartitionInputInfo(HashHint suggest_include_content_hash,
00143                                         InputInfo* input);
00144 
00145   void FillInPartitionInputInfoFromResponseHeaders(
00146       const ResponseHeaders& headers,
00147       InputInfo* input);
00148 
00152   int64 CacheExpirationTimeMs() const;
00153 
00154   StringPiece contents() const {
00155     StringPiece val;
00156     bool got_contents = value_.ExtractContents(&val);
00157     CHECK(got_contents) << "Resource contents read before loading: " << url();
00158     return val;
00159   }
00160   ResponseHeaders* response_headers() { return &response_headers_; }
00161   const ResponseHeaders* response_headers() const { return &response_headers_; }
00162   const ContentType* type() const { return type_; }
00163   virtual void SetType(const ContentType* type);
00164 
00166   StringPiece charset() const { return charset_; }
00167   void set_charset(StringPiece c) { c.CopyToString(&charset_); }
00168 
00170   virtual GoogleString url() const = 0;
00171 
00174   virtual GoogleString cache_key() const {
00175     return url();
00176   }
00177 
00180   void DetermineContentType();
00181 
00185   class AsyncCallback {
00186    public:
00187     explicit AsyncCallback(const ResourcePtr& resource) : resource_(resource) {}
00188 
00189     virtual ~AsyncCallback();
00190     virtual void Done(bool lock_failure, bool resource_ok) = 0;
00191 
00192     const ResourcePtr& resource() { return resource_; }
00193 
00194    private:
00195     ResourcePtr resource_;
00196     DISALLOW_COPY_AND_ASSIGN(AsyncCallback);
00197   };
00198 
00201   class FreshenCallback : public AsyncCallback {
00202    public:
00203     explicit FreshenCallback(const ResourcePtr& resource)
00204         : AsyncCallback(resource) {}
00205 
00206     virtual ~FreshenCallback();
00209     virtual InputInfo* input_info() { return NULL; }
00210 
00213     virtual void Done(bool lock_failure, bool resource_ok) {
00214       delete this;
00215     }
00216 
00217    private:
00218     DISALLOW_COPY_AND_ASSIGN(FreshenCallback);
00219   };
00220 
00226   bool Link(HTTPValue* source, MessageHandler* handler);
00227 
00231   virtual void Freshen(FreshenCallback* callback, MessageHandler* handler);
00232 
00234   void LinkFallbackValue(HTTPValue* value);
00235 
00236   void set_is_background_fetch(bool x) { is_background_fetch_ = x; }
00237   bool is_background_fetch() const { return is_background_fetch_; }
00238 
00239   FetchResponseStatus fetch_response_status() {
00240     return fetch_response_status_;
00241   }
00242 
00243   void set_fetch_response_status(FetchResponseStatus x) {
00244     fetch_response_status_ = x;
00245   }
00246 
00250   virtual bool UseHttpCache() const = 0;
00251 
00252  protected:
00253   virtual ~Resource();
00254   REFCOUNT_FRIEND_DECLARATION(Resource);
00255   friend class ServerContext;
00256   friend class ReadAsyncHttpCacheCallback; 
00257   friend class RewriteDriver; 
00258   friend class UrlReadAsyncFetchCallback;
00259 
00269   virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy,
00270                                const RequestContextPtr& request_context,
00271                                AsyncCallback* callback) = 0;
00272 
00273   void set_enable_cache_purge(bool x) { enable_cache_purge_ = x; }
00274   void set_proactive_resource_freshening(bool x) {
00275     proactive_resource_freshening_ = x;
00276   }
00277 
00278   void set_disable_rewrite_on_no_transform(bool x) {
00279     disable_rewrite_on_no_transform_ = x;
00280   }
00281   ServerContext* server_context_;
00282 
00283   const ContentType* type_;
00284   GoogleString charset_;
00285   HTTPValue value_; 
00286   ResponseHeaders response_headers_;
00287 
00291   HTTPValue fallback_value_;
00292 
00293  private:
00295   FetchResponseStatus fetch_response_status_;
00296 
00302   bool is_background_fetch_;
00303   bool enable_cache_purge_;
00304   bool proactive_resource_freshening_;
00305   bool disable_rewrite_on_no_transform_;
00306 
00307   DISALLOW_COPY_AND_ASSIGN(Resource);
00308 };
00309 
00310 }  
00311 
00312 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines