Page Speed Optimization Libraries
1.2.24.1
|
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 00036 /* 00037 * The Local Storage Cache rewriter reduces HTTP requests by inlining resources 00038 * and using browser-side javascript to store the inlined resources in local 00039 * storage. The javascript also creates a cookie that reflects the resources it 00040 * has in local storage. On a repeat view, the server uses the cookie to 00041 * determine if it should replace an inlined resource with a script snippet 00042 * that loads the resource from local storage. In effect, we get browser 00043 * caching of inlined resources, theoretically speeding up first view (by 00044 * inlining) and repeat view (by not resending the inlined resource). 00045 */ 00046 class LocalStorageCacheFilter : public RewriteFilter { 00047 public: 00048 static const char kLscCookieName[]; 00049 static const char kLscInitializer[]; 00050 00052 class InlineState { 00053 public: 00054 InlineState() : initialized_(false), enabled_(false) {} 00055 00056 private: 00057 friend class LocalStorageCacheFilter; 00058 00059 bool initialized_; 00060 bool enabled_; 00061 GoogleString url_; 00062 }; 00063 00064 explicit LocalStorageCacheFilter(RewriteDriver* rewrite_driver); 00065 virtual ~LocalStorageCacheFilter(); 00066 00067 virtual void StartDocumentImpl(); 00068 virtual void EndDocument(); 00069 virtual void StartElementImpl(HtmlElement* element); 00070 virtual void EndElementImpl(HtmlElement* element); 00071 00072 virtual const char* Name() const { return "LocalStorageCache"; } 00073 virtual const char* id() const { 00074 return RewriteOptions::kLocalStorageCacheId; 00075 } 00076 00077 std::set<StringPiece>* mutable_cookie_hashes() { return &cookie_hashes_; } 00078 00096 static bool AddStorableResource(const StringPiece& url, 00097 RewriteDriver* driver, 00098 bool skip_cookie_check, 00099 HtmlElement* element, 00100 InlineState* state); 00101 00112 static bool AddLscAttributes(const StringPiece url, 00113 const CachedResult& cached, 00114 bool has_url, 00115 RewriteDriver* driver, 00116 HtmlElement* element); 00117 00119 static void RemoveLscAttributes(HtmlElement* element); 00120 00121 private: 00122 void InsertOurScriptElement(HtmlElement* before); 00123 static bool IsHashInCookie(const RewriteDriver* driver, 00124 const StringPiece cookie_name, 00125 const StringPiece hash, 00126 std::set<StringPiece>* hash_set); 00127 static GoogleString ExtractOtherImgAttributes(const HtmlElement* element); 00128 00130 bool script_inserted_; 00132 bool script_needs_inserting_; 00135 std::set<StringPiece> cookie_hashes_; 00136 00137 DISALLOW_COPY_AND_ASSIGN(LocalStorageCacheFilter); 00138 }; 00139 00140 } 00141 00142 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_LOCAL_STORAGE_CACHE_FILTER_H_