Page Speed Optimization Libraries  1.2.24.1
net/instaweb/rewriter/public/output_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 
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/rewriter/public/output_resource_kind.h"
00027 #include "net/instaweb/rewriter/public/resource.h"
00028 #include "net/instaweb/rewriter/public/resource_namer.h"
00029 #include "net/instaweb/util/public/basictypes.h"
00030 #include "net/instaweb/util/public/queued_worker_pool.h"
00031 #include "net/instaweb/util/public/ref_counted_ptr.h"
00032 #include "net/instaweb/util/public/scoped_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 ServerContext;
00043 class RewriteOptions;
00044 class Writer;
00045 struct ContentType;
00046 
00047 class OutputResource : public Resource {
00048  public:
00057   OutputResource(ServerContext* server_context,
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 
00127   GoogleString HttpCacheKey() const;
00128 
00130   const GoogleString& resolved_base() const { return resolved_base_; }
00131   const GoogleString& unmapped_base() const { return unmapped_base_; }
00132   const GoogleString& original_base() const { return original_base_; }
00133   const ResourceNamer& full_name() const { return full_name_; }
00134   StringPiece name() const { return full_name_.name(); }
00135   StringPiece experiment() const { return full_name_.experiment(); }
00136   StringPiece suffix() const;
00137   StringPiece filter_prefix() const { return full_name_.id(); }
00138   StringPiece hash() const { return full_name_.hash(); }
00139   bool has_hash() const { return !hash().empty(); }
00140 
00150   GoogleString decoded_base() const;
00151 
00159   bool IsWritten() const;
00160 
00162   virtual void SetType(const ContentType* type);
00163 
00176   const CachedResult* cached_result() const { return cached_result_; }
00177 
00185   CachedResult* EnsureCachedResultCreated();
00186 
00187   void clear_cached_result();
00188 
00191   void set_cached_result(CachedResult* cached_result) {
00192     clear_cached_result();
00193     cached_result_ = cached_result;
00194   }
00195 
00197   CachedResult* ReleaseCachedResult() {
00198     CHECK(cached_result_owned_);
00199     CachedResult* ret = cached_result_;
00200     cached_result_ = NULL;
00201     cached_result_owned_ = false;
00202     return ret;
00203   }
00204 
00205   OutputResourceKind kind() const { return kind_; }
00206 
00207   bool has_lock() const;
00208 
00210   void set_written(bool written) { writing_complete_ = true; }
00211 
00212   virtual const RewriteOptions* rewrite_options() const {
00213     return rewrite_options_;
00214   }
00215 
00219   Writer* BeginWrite(MessageHandler* message_handler);
00220   void EndWrite(MessageHandler* message_handler);
00221 
00222  protected:
00223   virtual ~OutputResource();
00224   REFCOUNT_FRIEND_DECLARATION(OutputResource);
00225 
00226  private:
00227   friend class ResourceManagerTestingPeer;
00228   friend class RewriteDriver;
00229   friend class ServerContext;
00230   friend class ServerContextTest;
00231 
00232   void SetHash(const StringPiece& hash);
00233   StringPiece extension() const { return full_name_.ext(); }
00234 
00236   GoogleString DumpFileName() const;
00237 
00238   bool writing_complete_;
00239 
00246   bool cached_result_owned_;
00247   CachedResult* cached_result_;
00248 
00267   GoogleString resolved_base_;
00268   GoogleString unmapped_base_;
00269   GoogleString original_base_;
00270 
00271   ResourceNamer full_name_;
00272 
00274   mutable GoogleString computed_url_;
00275 
00278   scoped_ptr<NamedLock> creation_lock_;
00279 
00280   const RewriteOptions* rewrite_options_;
00281 
00284   OutputResourceKind kind_;
00285 
00286   DISALLOW_COPY_AND_ASSIGN(OutputResource);
00287 };
00288 
00289 }  
00290 
00291 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_OUTPUT_RESOURCE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines