Page Speed Optimization Libraries  1.6.29.3
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     kFetchStatusOther,
00076   };
00077 
00078   Resource(ServerContext* server_context, const ContentType* type);
00079 
00081   ServerContext* server_context() const { return server_context_; }
00082 
00085   virtual bool IsValidAndCacheable() const;
00086 
00091   bool IsSafeToRewrite(bool rewrite_uncacheable) const;
00092 
00095   bool loaded() const { return response_headers_.status_code() != 0; }
00096   bool HttpStatusOk() const {
00097     return (response_headers_.status_code() == HttpStatus::kOK);
00098   }
00099 
00108   void LoadAsync(NotCacheablePolicy not_cacheable_policy,
00109                  const RequestContextPtr& request_context,
00110                  AsyncCallback* callback);
00111 
00117   virtual void RefreshIfImminentlyExpiring();
00118 
00124   GoogleString ContentsHash() const;
00125 
00128   void AddInputInfoToPartition(HashHint suggest_include_content_hash,
00129                                int index, CachedResult* partition);
00130 
00141   virtual void FillInPartitionInputInfo(HashHint suggest_include_content_hash,
00142                                         InputInfo* input);
00143 
00144   void FillInPartitionInputInfoFromResponseHeaders(
00145       const ResponseHeaders& headers,
00146       InputInfo* input);
00147 
00151   int64 CacheExpirationTimeMs() const;
00152 
00153   StringPiece contents() const {
00154     StringPiece val;
00155     bool got_contents = value_.ExtractContents(&val);
00156     CHECK(got_contents) << "Resource contents read before loading: " << url();
00157     return val;
00158   }
00159   ResponseHeaders* response_headers() { return &response_headers_; }
00160   const ResponseHeaders* response_headers() const { return &response_headers_; }
00161   const ContentType* type() const { return type_; }
00162   virtual void SetType(const ContentType* type);
00163 
00165   StringPiece charset() const { return charset_; }
00166   void set_charset(StringPiece c) { c.CopyToString(&charset_); }
00167 
00169   virtual GoogleString url() const = 0;
00170 
00173   void DetermineContentType();
00174 
00178   class AsyncCallback {
00179    public:
00180     explicit AsyncCallback(const ResourcePtr& resource) : resource_(resource) {}
00181 
00182     virtual ~AsyncCallback();
00183     virtual void Done(bool lock_failure, bool resource_ok) = 0;
00184 
00185     const ResourcePtr& resource() { return resource_; }
00186 
00187    private:
00188     ResourcePtr resource_;
00189     DISALLOW_COPY_AND_ASSIGN(AsyncCallback);
00190   };
00191 
00194   class FreshenCallback : public AsyncCallback {
00195    public:
00196     explicit FreshenCallback(const ResourcePtr& resource)
00197         : AsyncCallback(resource) {}
00198 
00199     virtual ~FreshenCallback();
00202     virtual InputInfo* input_info() { return NULL; }
00203 
00206     virtual void Done(bool lock_failure, bool resource_ok) {
00207       delete this;
00208     }
00209 
00210    private:
00211     DISALLOW_COPY_AND_ASSIGN(FreshenCallback);
00212   };
00213 
00219   bool Link(HTTPValue* source, MessageHandler* handler);
00220 
00224   virtual void Freshen(FreshenCallback* callback, MessageHandler* handler);
00225 
00227   void LinkFallbackValue(HTTPValue* value);
00228 
00229   void set_is_background_fetch(bool x) { is_background_fetch_ = x; }
00230   bool is_background_fetch() const { return is_background_fetch_; }
00231 
00232   FetchResponseStatus fetch_response_status() {
00233     return fetch_response_status_;
00234   }
00235 
00236   void set_fetch_response_status(FetchResponseStatus x) {
00237     fetch_response_status_ = x;
00238   }
00239 
00243   virtual bool UseHttpCache() const = 0;
00244 
00245  protected:
00246   virtual ~Resource();
00247   REFCOUNT_FRIEND_DECLARATION(Resource);
00248   friend class ServerContext;
00249   friend class ReadAsyncHttpCacheCallback; 
00250   friend class RewriteDriver; 
00251   friend class UrlReadAsyncFetchCallback;
00252 
00262   virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy,
00263                                const RequestContextPtr& request_context,
00264                                AsyncCallback* callback) = 0;
00265 
00266   void set_enable_cache_purge(bool x) { enable_cache_purge_ = x; }
00267 
00268   void set_disable_rewrite_on_no_transform(bool x) {
00269     disable_rewrite_on_no_transform_ = x;
00270   }
00271   ServerContext* server_context_;
00272 
00273   const ContentType* type_;
00274   GoogleString charset_;
00275   HTTPValue value_; 
00276   ResponseHeaders response_headers_;
00277 
00281   HTTPValue fallback_value_;
00282 
00283  private:
00285   FetchResponseStatus fetch_response_status_;
00286 
00292   bool is_background_fetch_;
00293   bool enable_cache_purge_;
00294   bool disable_rewrite_on_no_transform_;
00295 
00296   DISALLOW_COPY_AND_ASSIGN(Resource);
00297 };
00298 
00299 }  
00300 
00301 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines