Page Speed Optimization Libraries  1.9.32.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
local_storage_cache_filter.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_LOCAL_STORAGE_CACHE_FILTER_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_LOCAL_STORAGE_CACHE_FILTER_H_
21 
22 #include <set>
23 
29 
30 namespace net_instaweb {
31 
32 class CachedResult;
33 class HtmlElement;
34 class RewriteDriver;
35 class Statistics;
36 class Variable;
37 
38 /*
39  * The Local Storage Cache rewriter reduces HTTP requests by inlining resources
40  * and using browser-side javascript to store the inlined resources in local
41  * storage. The javascript also creates a cookie that reflects the resources it
42  * has in local storage. On a repeat view, the server uses the cookie to
43  * determine if it should replace an inlined resource with a script snippet
44  * that loads the resource from local storage. In effect, we get browser
45  * caching of inlined resources, theoretically speeding up first view (by
46  * inlining) and repeat view (by not resending the inlined resource).
47  */
49  public:
50  static const char kLscCookieName[];
51  static const char kLscInitializer[];
52 
54  static const char kCandidatesFound[];
55  static const char kStoredTotal[];
56  static const char kStoredImages[];
57  static const char kStoredCss[];
58  static const char kCandidatesAdded[];
59  static const char kCandidatesRemoved[];
60 
62  class InlineState {
63  public:
64  InlineState() : initialized_(false), enabled_(false) {}
65 
66  private:
67  friend class LocalStorageCacheFilter;
68 
69  bool initialized_;
70  bool enabled_;
71  GoogleString url_;
72  };
73 
74  explicit LocalStorageCacheFilter(RewriteDriver* rewrite_driver);
75  virtual ~LocalStorageCacheFilter();
76 
78  static void InitStats(Statistics* statistics);
79 
80  virtual void StartDocumentImpl();
81  virtual void EndDocument();
82  virtual void StartElementImpl(HtmlElement* element);
83  virtual void EndElementImpl(HtmlElement* element);
84 
85  virtual const char* Name() const { return "LocalStorageCache"; }
86  virtual const char* id() const {
87  return RewriteOptions::kLocalStorageCacheId;
88  }
89 
90  std::set<StringPiece>* mutable_cookie_hashes() { return &cookie_hashes_; }
91 
109  static bool AddStorableResource(const StringPiece& url,
110  RewriteDriver* driver,
111  bool skip_cookie_check,
112  HtmlElement* element,
113  InlineState* state);
114 
123  static bool AddLscAttributes(const StringPiece url,
124  const CachedResult& cached,
125  RewriteDriver* driver,
126  HtmlElement* element);
127 
129  static void RemoveLscAttributes(HtmlElement* element,
130  RewriteDriver* driver);
131 
132  private:
133  void InsertOurScriptElement(HtmlElement* before);
134  static bool IsHashInCookie(const RewriteDriver* driver,
135  const StringPiece cookie_name,
136  const StringPiece hash,
137  std::set<StringPiece>* hash_set);
138  static GoogleString ExtractOtherImgAttributes(const HtmlElement* element);
139  static GoogleString GenerateHashFromUrlAndElement(const RewriteDriver* driver,
140  const StringPiece& lsc_url,
141  const HtmlElement* element);
142 
144  bool script_inserted_;
146  bool script_needs_inserting_;
149  std::set<StringPiece> cookie_hashes_;
150 
152  Variable* num_local_storage_cache_candidates_found_;
154  Variable* num_local_storage_cache_stored_total_;
156  Variable* num_local_storage_cache_stored_images_;
158  Variable* num_local_storage_cache_stored_css_;
160  Variable* num_local_storage_cache_candidates_added_;
162  Variable* num_local_storage_cache_candidates_removed_;
163 
164  DISALLOW_COPY_AND_ASSIGN(LocalStorageCacheFilter);
165 };
166 
167 }
168 
169 #endif
static bool AddLscAttributes(const StringPiece url, const CachedResult &cached, RewriteDriver *driver, HtmlElement *element)
static const char kCandidatesFound[]
Statistics' names.
Definition: local_storage_cache_filter.h:54
static bool AddStorableResource(const StringPiece &url, RewriteDriver *driver, bool skip_cookie_check, HtmlElement *element, InlineState *state)
Definition: local_storage_cache_filter.h:48
static void RemoveLscAttributes(HtmlElement *element, RewriteDriver *driver)
Remove the LSC attributes from the given element.
Definition: rewrite_filter.h:37
Definition: rewrite_driver.h:98
State information for an inline filter using LSC.
Definition: local_storage_cache_filter.h:62
static const char kLscInitializer[]
public for the test harness only.
Definition: local_storage_cache_filter.h:51
static void InitStats(Statistics *statistics)
May be called multiple times, if there are multiple statistics objects.