Page Speed Optimization Libraries  1.7.30.1
net/instaweb/rewriter/public/local_storage_cache_filter.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 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_LOCAL_STORAGE_CACHE_FILTER_H_
00020 #define NET_INSTAWEB_REWRITER_PUBLIC_LOCAL_STORAGE_CACHE_FILTER_H_
00021 
00022 #include <set>
00023 
00024 #include "net/instaweb/rewriter/public/rewrite_filter.h"
00025 #include "net/instaweb/util/public/basictypes.h"
00026 #include "net/instaweb/rewriter/public/rewrite_options.h"
00027 #include "net/instaweb/util/public/string.h"
00028 #include "net/instaweb/util/public/string_util.h"
00029 
00030 namespace net_instaweb {
00031 
00032 class CachedResult;
00033 class HtmlElement;
00034 class RewriteDriver;
00035 class Statistics;
00036 class Variable;
00037 
00038 /*
00039  * The Local Storage Cache rewriter reduces HTTP requests by inlining resources
00040  * and using browser-side javascript to store the inlined resources in local
00041  * storage. The javascript also creates a cookie that reflects the resources it
00042  * has in local storage. On a repeat view, the server uses the cookie to
00043  * determine if it should replace an inlined resource with a script snippet
00044  * that loads the resource from local storage. In effect, we get browser
00045  * caching of inlined resources, theoretically speeding up first view (by
00046  * inlining) and repeat view (by not resending the inlined resource).
00047  */
00048 class LocalStorageCacheFilter : public RewriteFilter {
00049  public:
00050   static const char kLscCookieName[];
00051   static const char kLscInitializer[]; 
00052 
00054   static const char kCandidatesFound[];
00055   static const char kStoredTotal[];
00056   static const char kStoredImages[];
00057   static const char kStoredCss[];
00058   static const char kCandidatesAdded[];
00059   static const char kCandidatesRemoved[];
00060 
00062   class InlineState {
00063    public:
00064     InlineState() : initialized_(false), enabled_(false) {}
00065 
00066    private:
00067     friend class LocalStorageCacheFilter;
00068 
00069     bool initialized_;
00070     bool enabled_;
00071     GoogleString url_;
00072   };
00073 
00074   explicit LocalStorageCacheFilter(RewriteDriver* rewrite_driver);
00075   virtual ~LocalStorageCacheFilter();
00076 
00078   static void InitStats(Statistics* statistics);
00079 
00080   virtual void StartDocumentImpl();
00081   virtual void EndDocument();
00082   virtual void StartElementImpl(HtmlElement* element);
00083   virtual void EndElementImpl(HtmlElement* element);
00084 
00085   virtual const char* Name() const { return "LocalStorageCache"; }
00086   virtual const char* id() const {
00087     return RewriteOptions::kLocalStorageCacheId;
00088   }
00089 
00090   std::set<StringPiece>* mutable_cookie_hashes() { return &cookie_hashes_; }
00091 
00109   static bool AddStorableResource(const StringPiece& url,
00110                                   RewriteDriver* driver,
00111                                   bool skip_cookie_check,
00112                                   HtmlElement* element,
00113                                   InlineState* state);
00114 
00123   static bool AddLscAttributes(const StringPiece url,
00124                                const CachedResult& cached,
00125                                RewriteDriver* driver,
00126                                HtmlElement* element);
00127 
00129   static void RemoveLscAttributes(HtmlElement* element,
00130                                   RewriteDriver* driver);
00131 
00132  private:
00133   void InsertOurScriptElement(HtmlElement* before);
00134   static bool IsHashInCookie(const RewriteDriver* driver,
00135                              const StringPiece cookie_name,
00136                              const StringPiece hash,
00137                              std::set<StringPiece>* hash_set);
00138   static GoogleString ExtractOtherImgAttributes(const HtmlElement* element);
00139   static GoogleString GenerateHashFromUrlAndElement(const RewriteDriver* driver,
00140                                                     const StringPiece& lsc_url,
00141                                                     const HtmlElement* element);
00142 
00144   bool script_inserted_;
00146   bool script_needs_inserting_;
00149   std::set<StringPiece> cookie_hashes_;
00150 
00152   Variable* num_local_storage_cache_candidates_found_;
00154   Variable* num_local_storage_cache_stored_total_;
00156   Variable* num_local_storage_cache_stored_images_;
00158   Variable* num_local_storage_cache_stored_css_;
00160   Variable* num_local_storage_cache_candidates_added_;
00162   Variable* num_local_storage_cache_candidates_removed_;
00163 
00164   DISALLOW_COPY_AND_ASSIGN(LocalStorageCacheFilter);
00165 };
00166 
00167 }  
00168 
00169 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_LOCAL_STORAGE_CACHE_FILTER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines