Page Speed Optimization Libraries  1.13.35.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 
24 #include "net/instaweb/rewriter/cached_result.pb.h"
34 
35 namespace net_instaweb {
36 
37 /*
38  * The Local Storage Cache rewriter reduces HTTP requests by inlining resources
39  * and using browser-side javascript to store the inlined resources in local
40  * storage. The javascript also creates a cookie that reflects the resources it
41  * has in local storage. On a repeat view, the server uses the cookie to
42  * determine if it should replace an inlined resource with a script snippet
43  * that loads the resource from local storage. In effect, we get browser
44  * caching of inlined resources, theoretically speeding up first view (by
45  * inlining) and repeat view (by not resending the inlined resource).
46  */
48  public:
49  static const char kLscCookieName[];
50  static const char kLscInitializer[];
51 
53  static const char kCandidatesFound[];
54  static const char kStoredTotal[];
55  static const char kStoredImages[];
56  static const char kStoredCss[];
57  static const char kCandidatesAdded[];
58  static const char kCandidatesRemoved[];
59 
61  class InlineState {
62  public:
63  InlineState() : initialized_(false), enabled_(false) {}
64 
65  private:
66  friend class LocalStorageCacheFilter;
67 
68  bool initialized_;
69  bool enabled_;
70  GoogleString url_;
71  };
72 
73  explicit LocalStorageCacheFilter(RewriteDriver* rewrite_driver);
74  virtual ~LocalStorageCacheFilter();
75 
77  static void InitStats(Statistics* statistics);
78 
79  virtual void StartDocumentImpl();
80  virtual void EndDocument();
81  virtual void StartElementImpl(HtmlElement* element);
82  virtual void EndElementImpl(HtmlElement* element);
83 
84  virtual const char* Name() const { return "LocalStorageCache"; }
85  virtual const char* id() const {
86  return RewriteOptions::kLocalStorageCacheId;
87  }
88 
89  std::set<StringPiece>* mutable_cookie_hashes() { return &cookie_hashes_; }
90 
108  static bool AddStorableResource(const StringPiece& url,
109  RewriteDriver* driver,
110  bool skip_cookie_check,
111  HtmlElement* element,
112  InlineState* state);
113 
122  static bool AddLscAttributes(const StringPiece url,
123  const CachedResult& cached,
124  RewriteDriver* driver,
125  HtmlElement* element);
126 
128  static void RemoveLscAttributes(HtmlElement* element,
129  RewriteDriver* driver);
130 
131  ScriptUsage GetScriptUsage() const override { return kWillInjectScripts; }
132 
133  private:
134  void InsertOurScriptElement(HtmlElement* before);
135  static bool IsHashInCookie(const RewriteDriver* driver,
136  const StringPiece cookie_name,
137  const StringPiece hash,
138  std::set<StringPiece>* hash_set);
139  static GoogleString ExtractOtherImgAttributes(const HtmlElement* element);
140  static GoogleString GenerateHashFromUrlAndElement(const RewriteDriver* driver,
141  const StringPiece& lsc_url,
142  const HtmlElement* element);
143 
145  bool script_inserted_;
147  bool script_needs_inserting_;
150  std::set<StringPiece> cookie_hashes_;
151 
153  Variable* num_local_storage_cache_candidates_found_;
155  Variable* num_local_storage_cache_stored_total_;
157  Variable* num_local_storage_cache_stored_images_;
159  Variable* num_local_storage_cache_stored_css_;
161  Variable* num_local_storage_cache_candidates_added_;
163  Variable* num_local_storage_cache_candidates_removed_;
164 
165 
166 };
167 
168 }
169 
170 #endif
static bool AddLscAttributes(const StringPiece url, const CachedResult &cached, RewriteDriver *driver, HtmlElement *element)
ScriptUsage GetScriptUsage() const override
Definition: local_storage_cache_filter.h:131
static const char kCandidatesFound[]
Statistics' names.
Definition: local_storage_cache_filter.h:53
Definition: statistics.h:43
virtual const char * Name() const
The name of this filter – used for logging and debugging.
Definition: local_storage_cache_filter.h:84
static bool AddStorableResource(const StringPiece &url, RewriteDriver *driver, bool skip_cookie_check, HtmlElement *element, InlineState *state)
Definition: local_storage_cache_filter.h:47
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
Definition: html_element.h:42
static void RemoveLscAttributes(HtmlElement *element, RewriteDriver *driver)
Remove the LSC attributes from the given element.
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
ScriptUsage
Describes a filter's relationship with scripts.
Definition: html_filter.h:38
Definition: rewrite_filter.h:35
Definition: rewrite_driver.h:100
State information for an inline filter using LSC.
Definition: local_storage_cache_filter.h:61
static const char kLscInitializer[]
public for the test harness only.
Definition: local_storage_cache_filter.h:50
static void InitStats(Statistics *statistics)
May be called multiple times, if there are multiple statistics objects.