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 "base/scoped_ptr.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/string.h" 00034 #include "net/instaweb/util/public/string_util.h" 00035 00036 namespace net_instaweb { 00037 00038 class CachedResult; 00039 class Function; 00040 class MessageHandler; 00041 class NamedLock; 00042 class ResourceManager; 00043 class RewriteOptions; 00044 class Writer; 00045 struct ContentType; 00046 00047 class OutputResource : public Resource { 00048 public: 00057 OutputResource(ResourceManager* resource_manager, 00058 const StringPiece& resolved_base, 00059 const StringPiece& unmapped_base, 00060 const StringPiece& original_base, 00061 const ResourceNamer& resource_id, 00062 const RewriteOptions* options, 00063 OutputResourceKind kind); 00064 00065 virtual bool Load(MessageHandler* message_handler); 00069 virtual GoogleString url() const; 00073 GoogleString UrlEvenIfHashNotSet(); 00074 00079 void DumpToDisk(MessageHandler* handler); 00080 00085 NamedLock* CreationLock(); 00086 00089 bool TryLockForCreation(); 00090 00094 void LockForCreation(QueuedWorkerPool::Sequence* worker, Function* callback); 00095 00097 void DropCreationLock(); 00098 00101 void UpdateCachedResultPreservingInputInfo(CachedResult* to_update) const; 00102 00116 virtual GoogleString name_key() const; 00117 00119 const GoogleString& resolved_base() const { return resolved_base_; } 00120 const GoogleString& unmapped_base() const { return unmapped_base_; } 00121 const GoogleString& original_base() const { return original_base_; } 00122 const ResourceNamer& full_name() const { return full_name_; } 00123 StringPiece name() const { return full_name_.name(); } 00124 StringPiece suffix() const; 00125 StringPiece filter_prefix() const { return full_name_.id(); } 00126 StringPiece hash() const { return full_name_.hash(); } 00127 bool has_hash() const { return !hash().empty(); } 00128 00138 GoogleString decoded_base() const; 00139 00147 bool IsWritten() const; 00148 00150 virtual void SetType(const ContentType* type); 00151 00164 const CachedResult* cached_result() const { return cached_result_; } 00165 00173 CachedResult* EnsureCachedResultCreated(); 00174 00175 void clear_cached_result(); 00176 00179 void set_cached_result(CachedResult* cached_result) { 00180 clear_cached_result(); 00181 cached_result_ = cached_result; 00182 } 00183 00185 CachedResult* ReleaseCachedResult() { 00186 CHECK(cached_result_owned_); 00187 CachedResult* ret = cached_result_; 00188 cached_result_ = NULL; 00189 cached_result_owned_ = false; 00190 return ret; 00191 } 00192 00193 OutputResourceKind kind() const { return kind_; } 00194 00195 bool has_lock() const; 00196 00198 void set_written(bool written) { writing_complete_ = true; } 00199 00200 virtual const RewriteOptions* rewrite_options() const { 00201 return rewrite_options_; 00202 } 00203 00207 Writer* BeginWrite(MessageHandler* message_handler); 00208 void EndWrite(MessageHandler* message_handler); 00209 00210 protected: 00211 virtual ~OutputResource(); 00212 REFCOUNT_FRIEND_DECLARATION(OutputResource); 00213 00214 private: 00215 friend class ResourceManager; 00216 friend class ResourceManagerTest; 00217 friend class ResourceManagerTestingPeer; 00218 friend class RewriteDriver; 00219 00220 void SetHash(const StringPiece& hash); 00221 StringPiece extension() const { return full_name_.ext(); } 00222 00224 GoogleString DumpFileName() const; 00225 00226 bool writing_complete_; 00227 00234 bool cached_result_owned_; 00235 CachedResult* cached_result_; 00236 00255 GoogleString resolved_base_; 00256 GoogleString unmapped_base_; 00257 GoogleString original_base_; 00258 00259 ResourceNamer full_name_; 00260 00262 mutable GoogleString computed_url_; 00263 00266 scoped_ptr<NamedLock> creation_lock_; 00267 00273 const RewriteOptions* rewrite_options_; 00274 00277 OutputResourceKind kind_; 00278 00279 DISALLOW_COPY_AND_ASSIGN(OutputResource); 00280 }; 00281 00282 } 00283 00284 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_OUTPUT_RESOURCE_H_