00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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 ResourceManager;
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
00067 Resource(ResourceManager* resource_manager, const ContentType* type);
00068
00070 ResourceManager* resource_manager() const { return resource_manager_; }
00071
00076 virtual bool IsValidAndCacheable() const;
00077
00080 bool loaded() const { return response_headers_.status_code() != 0; }
00081 bool HttpStatusOk() const {
00082 return (response_headers_.status_code() == HttpStatus::kOK);
00083 }
00084
00090 GoogleString ContentsHash() const;
00091
00094 void AddInputInfoToPartition(HashHint suggest_include_content_hash,
00095 int index, CachedResult* partition);
00096
00107 virtual void FillInPartitionInputInfo(HashHint suggest_include_content_hash,
00108 InputInfo* input);
00109
00110 void FillInPartitionInputInfoFromResponseHeaders(
00111 const ResponseHeaders& headers,
00112 InputInfo* input);
00113
00117 int64 CacheExpirationTimeMs() const;
00118
00119 StringPiece contents() const {
00120 StringPiece val;
00121 bool got_contents = value_.ExtractContents(&val);
00122 CHECK(got_contents) << "Resource contents read before loading";
00123 return val;
00124 }
00125 ResponseHeaders* response_headers() { return &response_headers_; }
00126 const ResponseHeaders* response_headers() const { return &response_headers_; }
00127 const ContentType* type() const { return type_; }
00128 virtual void SetType(const ContentType* type);
00129
00131 StringPiece charset() const { return charset_; }
00132 void set_charset(StringPiece c) { c.CopyToString(&charset_); }
00133
00134 virtual bool IsCacheableTypeOfResource() const { return true; }
00135
00137 virtual GoogleString url() const = 0;
00138
00141 void DetermineContentType();
00142
00146 virtual const RewriteOptions* rewrite_options() const = 0;
00147
00151 class AsyncCallback {
00152 public:
00153 explicit AsyncCallback(const ResourcePtr& resource) : resource_(resource) {}
00154
00155 virtual ~AsyncCallback();
00156 virtual void Done(bool success) = 0;
00157
00158 const ResourcePtr& resource() { return resource_; }
00159
00162 virtual bool EnableThreaded() const { return false; }
00163
00164 private:
00165 ResourcePtr resource_;
00166 DISALLOW_COPY_AND_ASSIGN(AsyncCallback);
00167 };
00168
00171 class FreshenCallback : public AsyncCallback {
00172 public:
00173 explicit FreshenCallback(const ResourcePtr& resource)
00174 : AsyncCallback(resource) {}
00175
00176 virtual ~FreshenCallback();
00179 virtual InputInfo* input_info() { return NULL; }
00180
00183 virtual void Done(bool success) {
00184 delete this;
00185 }
00186
00187 private:
00188 DISALLOW_COPY_AND_ASSIGN(FreshenCallback);
00189 };
00190
00196 bool Link(HTTPValue* source, MessageHandler* handler);
00197
00201 virtual void Freshen(FreshenCallback* callback, MessageHandler* handler);
00202
00204 void LinkFallbackValue(HTTPValue* value);
00205
00206 void set_is_background_fetch(bool x) { is_background_fetch_ = x; }
00207 bool is_background_fetch() const { return is_background_fetch_; }
00208
00209 protected:
00210 virtual ~Resource();
00211 REFCOUNT_FRIEND_DECLARATION(Resource);
00212 friend class ResourceManager;
00213 friend class RewriteDriver;
00214 friend class UrlReadAsyncFetchCallback;
00215 friend class ResourceManagerHttpCallback;
00216
00220 virtual bool Load(MessageHandler* message_handler) = 0;
00221
00226 virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy,
00227 AsyncCallback* callback,
00228 MessageHandler* message_handler);
00229
00230 ResourceManager* resource_manager_;
00231
00232 const ContentType* type_;
00233 GoogleString charset_;
00234 HTTPValue value_;
00235 ResponseHeaders response_headers_;
00236
00240 HTTPValue fallback_value_;
00241
00242 private:
00248 bool is_background_fetch_;
00249 DISALLOW_COPY_AND_ASSIGN(Resource);
00250 };
00251
00252 }
00253
00254 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_H_