Page Speed Optimization Libraries  1.2.24.1
net/instaweb/rewriter/public/rewrite_context.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 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 
00018 
00019 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_H_
00020 #define NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_H_
00021 
00022 #include <set>
00023 #include <vector>
00024 
00025 #include "net/instaweb/http/public/http_cache.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/server_context.h"
00029 #include "net/instaweb/rewriter/public/resource_slot.h"
00030 #include "net/instaweb/rewriter/public/rewrite_context.h"
00031 #include "net/instaweb/rewriter/public/rewrite_result.h"
00032 #include "net/instaweb/util/public/basictypes.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 #include "net/instaweb/util/public/url_segment_encoder.h"
00037 
00038 namespace net_instaweb {
00039 
00040 class AsyncFetch;
00041 class CachedResult;
00042 class GoogleUrl;
00043 class InputInfo;
00044 class MessageHandler;
00045 class NamedLock;
00046 class OutputPartitions;
00047 class ResourceContext;
00048 class ResponseHeaders;
00049 class RewriteDriver;
00050 class RewriteOptions;
00051 class Writer;
00052 
00093 class RewriteContext {
00094  public:
00097   RewriteContext(RewriteDriver* driver,   
00098                  RewriteContext* parent,  
00099                  ResourceContext* resource_context);
00100   virtual ~RewriteContext();
00101 
00106   int num_slots() const { return slots_.size(); }
00107   ResourceSlotPtr slot(int index) const { return slots_[index]; }
00108 
00111   int num_outputs() const { return outputs_.size(); }
00112   OutputResourcePtr output(int i) const { return outputs_[i]; }
00113 
00116   int num_output_partitions() const;
00117   const CachedResult* output_partition(int i) const;
00118   CachedResult* output_partition(int i);
00119 
00122   bool chained() const { return chained_ != 0; }
00123 
00127   void AddSlot(const ResourceSlotPtr& slot);
00128 
00131   void RemoveLastSlot();
00132 
00143   void Initiate();
00144 
00152   bool Fetch(const OutputResourcePtr& output_resource,
00153              AsyncFetch* fetch,
00154              MessageHandler* message_handler);
00155 
00179   void Propagate(bool render_slots);
00180 
00183   bool slow() const { return slow_; }
00184 
00186   bool has_parent() const { return parent_ != NULL; }
00187 
00189   RewriteContext* parent() { return parent_; }
00190   const RewriteContext* parent() const { return parent_; }
00191 
00194   void AddNestedContext(RewriteContext* context);
00195 
00197   void set_force_rewrite(bool x) { force_rewrite_ = x; }
00198 
00199   const ResourceContext* resource_context() { return resource_context_.get(); }
00200 
00205   void DetachSlots();
00206 
00207  protected:
00208   typedef std::vector<InputInfo*> InputInfoStarVector;
00209   typedef std::vector<GoogleUrl*> GoogleUrlStarVector;
00210 
00212 
00216   ServerContext* FindServerContext() const;
00217   const RewriteOptions* Options() const;
00218   RewriteDriver* Driver() const;
00219 
00222   void AddRecheckDependency();
00223 
00227   void RenderPartitionOnDetach(int partition_index);
00228 
00233   void RewriteDone(RewriteResult result, int partition_index);
00234 
00239   void NestedRewriteDone(const RewriteContext* context);
00240 
00244   void StartNestedTasks();
00245 
00248   bool CreateOutputResourceForCachedOutput(const CachedResult* cached_result,
00249                                            OutputResourcePtr* output_resource);
00250 
00254   virtual bool OptimizationOnly() const { return true; }
00255 
00273   virtual bool Partition(OutputPartitions* partitions,
00274                          OutputResourceVector* outputs);
00275 
00281   virtual void PartitionAsync(OutputPartitions* partitions,
00282                               OutputResourceVector* outputs);
00283 
00287   void PartitionDone(bool result);
00288 
00291   void CrossThreadPartitionDone(bool result);
00292 
00304   virtual void Rewrite(int partition_index,
00305                        CachedResult* partition,
00306                        const OutputResourcePtr& output) = 0;
00307 
00312   virtual void Harvest();
00313 
00325   virtual void Render();
00326 
00329 
00336   virtual const UrlSegmentEncoder* encoder() const;
00337 
00340   virtual GoogleString CacheKeySuffix() const;
00341 
00343   virtual const char* id() const = 0;
00344 
00353   virtual OutputResourceKind kind() const = 0;
00354 
00364   virtual void StartFetchReconstruction();
00365 
00371   void DetachFetch();
00372 
00377   virtual bool DecodeFetchUrls(const OutputResourcePtr& output_resource,
00378                                MessageHandler* message_handler,
00379                                GoogleUrlStarVector* url_vector);
00380 
00384   virtual void FixFetchFallbackHeaders(ResponseHeaders* headers);
00385 
00388   virtual void FetchCallbackDone(bool success);
00389 
00396   virtual bool AbsolutifyIfNeeded(const StringPiece& input_contents,
00397                                   Writer* writer, MessageHandler* handler);
00398 
00403   virtual void FetchTryFallback(const GoogleString& url,
00404                                 const StringPiece& hash);
00405 
00407   void Freshen();
00408 
00410   int num_nested() const { return nested_.size(); }
00411   RewriteContext* nested(int i) const { return nested_[i]; }
00412 
00413   OutputPartitions* partitions() { return partitions_.get(); }
00414 
00415   void set_notify_driver_on_fetch_done(bool value) {
00416     notify_driver_on_fetch_done_ = value;
00417   }
00418 
00420   AsyncFetch* async_fetch();
00421 
00423   bool FetchContextDetached();
00424 
00426   MessageHandler* fetch_message_handler();
00427 
00429   bool stale_rewrite() const { return stale_rewrite_; }
00430 
00431  private:
00432   struct CacheLookupResult;
00433   class OutputCacheCallback;
00434   friend class OutputCacheCallback;
00435   class HTTPCacheCallback;
00436   friend class HTTPCacheCallback;
00437   class ResourceCallbackUtils;
00438   friend class ResourceCallbackUtils;
00439   class ResourceFetchCallback;
00440   class ResourceReconstructCallback;
00441   class ResourceRevalidateCallback;
00442   friend class ResourceRevalidateCallback;
00443   class InvokeRewriteFunction;
00444   friend class InvokeRewriteFunction;
00445   class RewriteFreshenCallback;
00446 
00447   typedef std::set<RewriteContext*> ContextSet;
00448 
00452   enum FallbackCondition {
00453     kFallbackDiscretional,   
00454 
00455     kFallbackEmergency    
00456 
00457   };
00458 
00460   void Start();
00461   void SetPartitionKey();
00462   void StartFetch();
00463   void CancelFetch();
00464   void OutputCacheDone(CacheLookupResult* cache_result);
00465   void OutputCacheHit(bool write_partitions);
00466   void OutputCacheRevalidate(const InputInfoStarVector& to_revalidate);
00467   void OutputCacheMiss();
00468   void ResourceFetchDone(bool success, ResourcePtr resource, int slot_index);
00469   void ResourceRevalidateDone(InputInfo* input_info, bool success);
00470   void LogMetadataCacheInfo(bool cache_ok, bool can_revalidate);
00471 
00476   void RepeatedSuccess(const RewriteContext* primary);
00477   void RepeatedFailure();
00478 
00487   void Finalize();
00488 
00490   NamedLock* Lock();
00491 
00501   void FetchInputs();
00502 
00506   void Activate();
00507 
00514   void StartRewriteForHtml();
00515   void StartRewriteForFetch();
00516 
00523   bool ReadyToRewrite() const;
00524 
00527   void RunSuccessors();
00528 
00531   void WritePartition();
00532 
00542   void FinalizeRewriteForHtml();
00543 
00549   void RetireRewriteForHtml(bool permit_render);
00550 
00553   void MarkSlow();
00554 
00557   void MarkTooBusy();
00558 
00562   void CollectDependentTopLevel(ContextSet* contexts);
00563 
00566   void RewriteDoneImpl(RewriteResult result, int partition_index);
00567 
00570   void StartNestedTasksImpl();
00571 
00573   void FetchCacheDone(CacheLookupResult* cache_result);
00574 
00577   void FetchFallbackCacheDone(HTTPCache::FindResult result,
00578                               HTTPCache::Callback* data);
00579 
00584   bool CanFetchFallbackToOriginal(FallbackCondition circumstance) const;
00585 
00587   ResourceSlotVector slots_;
00588 
00592   std::vector<bool> render_slots_;
00593 
00604 
00605   bool started_;
00606   scoped_ptr<OutputPartitions> partitions_;
00607   OutputResourceVector outputs_;
00608   int outstanding_fetches_;
00609   int outstanding_rewrites_;
00610   scoped_ptr<ResourceContext> resource_context_;
00611   GoogleString partition_key_;
00612 
00613   UrlSegmentEncoder default_encoder_;
00614 
00617   scoped_ptr<NamedLock> lock_;
00618 
00622   class FetchContext;
00623   scoped_ptr<FetchContext> fetch_;
00624 
00627   std::vector<RewriteContext*> successors_;
00628 
00632   std::vector<RewriteContext*> repeated_;
00633 
00637   int num_pending_nested_;
00638   std::vector<RewriteContext*> nested_;
00639 
00641   RewriteContext* parent_;
00642 
00652   RewriteDriver* driver_;
00653 
00655   int num_predecessors_;
00656 
00659   bool chained_;
00660 
00678 
00680   bool rewrite_done_;
00681 
00686   bool ok_to_write_output_partitions_;
00687 
00690   bool was_too_busy_;
00691 
00695   bool slow_;
00696 
00698   bool revalidate_ok_;
00699 
00702   bool notify_driver_on_fetch_done_;
00703 
00706   bool force_rewrite_;
00707 
00710   bool stale_rewrite_;
00711 
00712   DISALLOW_COPY_AND_ASSIGN(RewriteContext);
00713 };
00714 
00715 }  
00716 
00717 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines