Page Speed Optimization Libraries  1.5.27.2
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 void LoadAndCallback(NotCacheablePolicy not_cacheable_policy,
00066                                AsyncCallback* callback,
00067                                MessageHandler* handler);
00074   virtual GoogleString url() const;
00078   GoogleString UrlEvenIfHashNotSet();
00079 
00084   void DumpToDisk(MessageHandler* handler);
00085 
00090   NamedLock* CreationLock();
00091 
00094   bool TryLockForCreation();
00095 
00099   void LockForCreation(QueuedWorkerPool::Sequence* worker, Function* callback);
00100 
00102   void DropCreationLock();
00103 
00106   void UpdateCachedResultPreservingInputInfo(CachedResult* to_update) const;
00107 
00121   virtual GoogleString name_key() const;
00122 
00132   GoogleString HttpCacheKey() const;
00133 
00135   const GoogleString& resolved_base() const { return resolved_base_; }
00136   const GoogleString& unmapped_base() const { return unmapped_base_; }
00137   const GoogleString& original_base() const { return original_base_; }
00138   const ResourceNamer& full_name() const { return full_name_; }
00139   ResourceNamer* mutable_full_name() { return &full_name_; }
00140   StringPiece name() const { return full_name_.name(); }
00141   StringPiece experiment() const { return full_name_.experiment(); }
00142   StringPiece suffix() const;
00143   StringPiece filter_prefix() const { return full_name_.id(); }
00144   StringPiece hash() const { return full_name_.hash(); }
00145   bool has_hash() const { return !hash().empty(); }
00146 
00156   GoogleString decoded_base() const;
00157 
00165   bool IsWritten() const { return writing_complete_; }
00166 
00168   virtual void SetType(const ContentType* type);
00169 
00182   const CachedResult* cached_result() const { return cached_result_; }
00183 
00191   CachedResult* EnsureCachedResultCreated();
00192 
00193   void clear_cached_result();
00194 
00197   void set_cached_result(CachedResult* cached_result) {
00198     clear_cached_result();
00199     cached_result_ = cached_result;
00200   }
00201 
00203   CachedResult* ReleaseCachedResult() {
00204     CHECK(cached_result_owned_);
00205     CachedResult* ret = cached_result_;
00206     cached_result_ = NULL;
00207     cached_result_owned_ = false;
00208     return ret;
00209   }
00210 
00211   OutputResourceKind kind() const { return kind_; }
00212 
00213   bool has_lock() const;
00214 
00216   void SetWritten(bool written) { writing_complete_ = true; }
00217 
00218   virtual const RewriteOptions* rewrite_options() const {
00219     return rewrite_options_;
00220   }
00221 
00225   Writer* BeginWrite(MessageHandler* message_handler);
00226   void EndWrite(MessageHandler* message_handler);
00227 
00228   virtual bool UseHttpCache() const { return true; }
00229 
00230  protected:
00231   virtual ~OutputResource();
00232   REFCOUNT_FRIEND_DECLARATION(OutputResource);
00233 
00234  private:
00235   friend class RewriteDriver;
00236   friend class ServerContext;
00237   friend class ServerContextTest;
00238 
00239   void SetHash(const StringPiece& hash);
00240   StringPiece extension() const { return full_name_.ext(); }
00241 
00243   GoogleString DumpFileName() const;
00244 
00245   bool writing_complete_;
00246 
00253   bool cached_result_owned_;
00254   CachedResult* cached_result_;
00255 
00274   GoogleString resolved_base_;
00275   GoogleString unmapped_base_;
00276   GoogleString original_base_;
00277 
00278   ResourceNamer full_name_;
00279 
00281   mutable GoogleString computed_url_;
00282 
00285   scoped_ptr<NamedLock> creation_lock_;
00286 
00287   const RewriteOptions* rewrite_options_;
00288 
00291   OutputResourceKind kind_;
00292 
00293   DISALLOW_COPY_AND_ASSIGN(OutputResource);
00294 };
00295 
00296 }  
00297 
00298 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_OUTPUT_RESOURCE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines