Page Speed Optimization Libraries
1.6.29.3
|
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 00021 00022 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_OUTPUT_RESOURCE_H_ 00023 #define NET_INSTAWEB_REWRITER_PUBLIC_OUTPUT_RESOURCE_H_ 00024 00025 #include "base/logging.h" 00026 #include "net/instaweb/http/public/request_context.h" 00027 #include "net/instaweb/rewriter/public/output_resource_kind.h" 00028 #include "net/instaweb/rewriter/public/resource.h" 00029 #include "net/instaweb/rewriter/public/resource_namer.h" 00030 #include "net/instaweb/util/public/basictypes.h" 00031 #include "net/instaweb/util/public/queued_worker_pool.h" 00032 #include "net/instaweb/util/public/ref_counted_ptr.h" 00033 #include "net/instaweb/util/public/scoped_ptr.h" 00034 #include "net/instaweb/util/public/string.h" 00035 #include "net/instaweb/util/public/string_util.h" 00036 00037 namespace net_instaweb { 00038 00039 class CachedResult; 00040 class Function; 00041 class MessageHandler; 00042 class NamedLock; 00043 class ServerContext; 00044 class RewriteOptions; 00045 class Writer; 00046 struct ContentType; 00047 00048 class OutputResource : public Resource { 00049 public: 00058 OutputResource(ServerContext* server_context, 00059 const StringPiece& resolved_base, 00060 const StringPiece& unmapped_base, 00061 const StringPiece& original_base, 00062 const ResourceNamer& resource_id, 00063 const RewriteOptions* options, 00064 OutputResourceKind kind); 00065 00066 virtual void LoadAndCallback(NotCacheablePolicy not_cacheable_policy, 00067 const RequestContextPtr& request_context, 00068 AsyncCallback* callback); 00075 virtual GoogleString url() const; 00079 GoogleString UrlEvenIfHashNotSet(); 00080 00085 void DumpToDisk(MessageHandler* handler); 00086 00091 NamedLock* CreationLock(); 00092 00095 bool TryLockForCreation(); 00096 00100 void LockForCreation(QueuedWorkerPool::Sequence* worker, Function* callback); 00101 00103 void DropCreationLock(); 00104 00107 void UpdateCachedResultPreservingInputInfo(CachedResult* to_update) const; 00108 00122 virtual GoogleString name_key() const; 00123 00133 GoogleString HttpCacheKey() const; 00134 00136 const GoogleString& resolved_base() const { return resolved_base_; } 00137 const GoogleString& unmapped_base() const { return unmapped_base_; } 00138 const GoogleString& original_base() const { return original_base_; } 00139 const ResourceNamer& full_name() const { return full_name_; } 00140 ResourceNamer* mutable_full_name() { return &full_name_; } 00141 StringPiece name() const { return full_name_.name(); } 00142 StringPiece experiment() const { return full_name_.experiment(); } 00143 StringPiece suffix() const; 00144 StringPiece filter_prefix() const { return full_name_.id(); } 00145 StringPiece hash() const { return full_name_.hash(); } 00146 bool has_hash() const { return !hash().empty(); } 00147 void clear_hash() { 00148 full_name_.ClearHash(); 00149 computed_url_.clear(); 00150 } 00151 00161 GoogleString decoded_base() const; 00162 00170 bool IsWritten() const { return writing_complete_; } 00171 00173 virtual void SetType(const ContentType* type); 00174 00187 const CachedResult* cached_result() const { return cached_result_; } 00188 00196 CachedResult* EnsureCachedResultCreated(); 00197 00198 void clear_cached_result(); 00199 00202 void set_cached_result(CachedResult* cached_result) { 00203 clear_cached_result(); 00204 cached_result_ = cached_result; 00205 } 00206 00208 CachedResult* ReleaseCachedResult() { 00209 CHECK(cached_result_owned_); 00210 CachedResult* ret = cached_result_; 00211 cached_result_ = NULL; 00212 cached_result_owned_ = false; 00213 return ret; 00214 } 00215 00216 OutputResourceKind kind() const { return kind_; } 00217 00218 bool has_lock() const; 00219 00221 void SetWritten(bool written) { writing_complete_ = true; } 00222 00223 virtual const RewriteOptions* rewrite_options() const { 00224 return rewrite_options_; 00225 } 00226 00230 Writer* BeginWrite(MessageHandler* message_handler); 00231 void EndWrite(MessageHandler* message_handler); 00232 00233 virtual bool UseHttpCache() const { return true; } 00234 00235 protected: 00236 virtual ~OutputResource(); 00237 REFCOUNT_FRIEND_DECLARATION(OutputResource); 00238 00239 private: 00240 friend class RewriteDriver; 00241 friend class ServerContext; 00242 friend class ServerContextTest; 00243 00244 void SetHash(const StringPiece& hash); 00245 StringPiece extension() const { return full_name_.ext(); } 00246 00248 GoogleString DumpFileName() const; 00249 00250 bool writing_complete_; 00251 00258 bool cached_result_owned_; 00259 CachedResult* cached_result_; 00260 00279 GoogleString resolved_base_; 00280 GoogleString unmapped_base_; 00281 GoogleString original_base_; 00282 00283 ResourceNamer full_name_; 00284 00286 mutable GoogleString computed_url_; 00287 00290 scoped_ptr<NamedLock> creation_lock_; 00291 00292 const RewriteOptions* rewrite_options_; 00293 00296 OutputResourceKind kind_; 00297 00298 DISALLOW_COPY_AND_ASSIGN(OutputResource); 00299 }; 00300 00301 } 00302 00303 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_OUTPUT_RESOURCE_H_