Page Speed Optimization Libraries
1.7.30.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_CACHEABLE_RESOURCE_BASE_H_ 00027 #define NET_INSTAWEB_REWRITER_PUBLIC_CACHEABLE_RESOURCE_BASE_H_ 00028 00029 #include "net/instaweb/http/public/request_context.h" 00030 #include "net/instaweb/rewriter/public/resource.h" 00031 #include "net/instaweb/rewriter/public/server_context.h" 00032 #include "net/instaweb/util/public/basictypes.h" 00033 #include "net/instaweb/util/public/string_util.h" 00034 #include "pagespeed/kernel/base/string.h" 00035 00036 namespace net_instaweb { 00037 00038 struct ContentType; 00039 class HTTPCache; 00040 class HTTPValue; 00041 class MessageHandler; 00042 class RequestHeaders; 00043 class ResponseHeaders; 00044 class RewriteDriver; 00045 class RewriteOptions; 00046 class Statistics; 00047 class Timer; 00048 class Variable; 00049 00050 class CacheableResourceBase : public Resource { 00051 public: 00053 00055 virtual bool UseHttpCache() const { return true; } 00056 00057 virtual bool IsValidAndCacheable() const; 00058 00060 virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy, 00061 const RequestContextPtr& request_context, 00062 AsyncCallback* callback); 00063 00064 virtual void Freshen(FreshenCallback* callback, MessageHandler* handler); 00065 virtual void RefreshIfImminentlyExpiring(); 00066 virtual GoogleString url() const { return url_; } 00067 virtual GoogleString cache_key() const { return cache_key_; } 00068 00069 protected: 00071 CacheableResourceBase(StringPiece stat_prefix, 00072 StringPiece url, 00073 StringPiece cache_key, 00074 const ContentType* type, 00075 RewriteDriver* rewrite_driver); 00076 virtual ~CacheableResourceBase(); 00077 00078 static void InitStats(StringPiece stat_prefix, Statistics* statistics); 00079 00082 virtual void PrepareRequest(const RequestContextPtr& request_context, 00083 RequestHeaders* headers); 00084 00089 virtual void PrepareResponseHeaders(ResponseHeaders* headers); 00090 00091 HTTPCache* http_cache() const { return server_context()->http_cache(); } 00092 RewriteDriver* rewrite_driver() const { return rewrite_driver_; } 00093 const RewriteOptions* rewrite_options() const; 00094 00097 bool ShouldSkipBackgroundFetch() const; 00098 00099 private: 00100 class FreshenHttpCacheCallback; 00101 class LoadHttpCacheCallback; 00102 class FetchCallbackBase; 00103 class FreshenFetchCallback; 00104 class LoadFetchCallback; 00105 friend class CacheableResourceBaseTest; 00106 00110 bool UpdateInputInfoForFreshen(const ResponseHeaders& headers, 00111 const HTTPValue& value, 00112 Resource::FreshenCallback* callback); 00113 00116 bool IsValidAndCacheableImpl(const ResponseHeaders& headers) const; 00117 00118 Timer* timer() const { return server_context()->timer(); } 00119 MessageHandler* message_handler() const { 00120 return server_context()->message_handler(); 00121 } 00122 00123 GoogleString url_; 00124 GoogleString cache_key_; 00125 00126 RewriteDriver* rewrite_driver_; 00127 Variable* hits_; 00128 Variable* recent_fetch_failures_; 00129 Variable* recent_uncacheables_treated_as_miss_; 00130 Variable* recent_uncacheables_treated_as_failure_; 00131 Variable* misses_; 00132 00133 DISALLOW_COPY_AND_ASSIGN(CacheableResourceBase); 00134 }; 00135 00136 } 00137 00138 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_CACHEABLE_RESOURCE_BASE_H_