OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This file contains the SdchManager class and two nested classes | |
6 // (Dictionary, DictionarySet). SdchManager::Dictionary contains all | |
7 // of the information about an SDCH dictionary. The manager is | |
8 // responsible for storing those dictionaries, and provides access to | |
9 // them through DictionarySet objects. A DictionarySet is an object | |
10 // whose lifetime is under the control of the consumer. It is a | |
11 // reference to a set of dictionaries, and guarantees that none of | |
12 // those dictionaries will be destroyed while the DictionarySet | |
13 // reference is alive. | |
14 | |
15 #ifndef NET_BASE_SDCH_MANAGER_H_ | |
16 #define NET_BASE_SDCH_MANAGER_H_ | |
17 | |
18 #include <map> | |
19 #include <set> | |
20 #include <string> | |
21 #include <vector> | |
22 | |
23 #include "base/gtest_prod_util.h" | |
24 #include "base/memory/ref_counted.h" | |
25 #include "base/memory/scoped_ptr.h" | |
26 #include "base/observer_list.h" | |
27 #include "base/threading/thread_checker.h" | |
28 #include "base/time/time.h" | |
29 #include "net/base/net_export.h" | |
30 #include "net/base/sdch_problem_codes.h" | |
31 #include "url/gurl.h" | |
32 | |
33 namespace base { | |
34 class Clock; | |
35 class Value; | |
36 } | |
37 | |
38 namespace net { | |
39 | |
40 class SdchObserver; | |
41 | |
42 // Provides global database of differential decompression dictionaries for the | |
43 // SDCH filter (processes sdch enconded content). | |
44 // | |
45 // The SdchManager maintains a collection of memory resident dictionaries. It | |
46 // can find a dictionary (based on a server specification of a hash), store a | |
47 // dictionary, and make judgements about what URLs can use, set, etc. a | |
48 // dictionary. | |
49 | |
50 // These dictionaries are acquired over the net, and include a header | |
51 // (containing metadata) as well as a VCDIFF dictionary (for use by a VCDIFF | |
52 // module) to decompress data. | |
53 // | |
54 // A dictionary held by the manager may nonetheless outlive the manager if | |
55 // a DictionarySet object refers to it; see below. | |
56 class NET_EXPORT SdchManager { | |
57 public: | |
58 class Dictionary; | |
59 typedef std::map<std::string, scoped_refptr<base::RefCountedData<Dictionary>>> | |
60 DictionaryMap; | |
61 | |
62 class NET_EXPORT_PRIVATE Dictionary { | |
63 public: | |
64 // Construct a vc-diff usable dictionary from the dictionary_text starting | |
65 // at the given offset. The supplied client_hash should be used to | |
66 // advertise the dictionary's availability relative to the suppplied URL. | |
67 Dictionary(const std::string& dictionary_text, | |
68 size_t offset, | |
69 const std::string& client_hash, | |
70 const std::string& server_hash, | |
71 const GURL& url, | |
72 const std::string& domain, | |
73 const std::string& path, | |
74 const base::Time& expiration, | |
75 const std::set<int>& ports); | |
76 | |
77 ~Dictionary(); | |
78 | |
79 // Sdch filters can get our text to use in decoding compressed data. | |
80 const std::string& text() const { return text_; } | |
81 | |
82 const GURL& url() const { return url_; } | |
83 const std::string& client_hash() const { return client_hash_; } | |
84 const std::string& server_hash() const { return server_hash_; } | |
85 const std::string& domain() const { return domain_; } | |
86 const std::string& path() const { return path_; } | |
87 const base::Time& expiration() const { return expiration_; } | |
88 const std::set<int>& ports() const { return ports_; } | |
89 | |
90 // Security methods to check if we can establish a new dictionary with the | |
91 // given data, that arrived in response to get of dictionary_url. | |
92 static SdchProblemCode CanSet(const std::string& domain, | |
93 const std::string& path, | |
94 const std::set<int>& ports, | |
95 const GURL& dictionary_url); | |
96 | |
97 // Security method to check if we can use a dictionary to decompress a | |
98 // target that arrived with a reference to this dictionary. | |
99 SdchProblemCode CanUse(const GURL& referring_url) const; | |
100 | |
101 // Compare paths to see if they "match" for dictionary use. | |
102 static bool PathMatch(const std::string& path, | |
103 const std::string& restriction); | |
104 | |
105 // Compare domains to see if the "match" for dictionary use. | |
106 static bool DomainMatch(const GURL& url, const std::string& restriction); | |
107 | |
108 // Is this dictionary expired? | |
109 bool Expired() const; | |
110 | |
111 void SetClockForTesting(scoped_ptr<base::Clock> clock); | |
112 | |
113 private: | |
114 friend class base::RefCountedData<Dictionary>; | |
115 | |
116 // Private copy-constructor to support RefCountedData<>, which requires | |
117 // that an object stored in it be either DefaultConstructible or | |
118 // CopyConstructible | |
119 Dictionary(const Dictionary& rhs); | |
120 | |
121 // The actual text of the dictionary. | |
122 std::string text_; | |
123 | |
124 // Part of the hash of text_ that the client uses to advertise the fact that | |
125 // it has a specific dictionary pre-cached. | |
126 std::string client_hash_; | |
127 | |
128 // Part of the hash of text_ that the server uses to identify the | |
129 // dictionary it wants used for decoding. | |
130 std::string server_hash_; | |
131 | |
132 // The GURL that arrived with the text_ in a URL request to specify where | |
133 // this dictionary may be used. | |
134 const GURL url_; | |
135 | |
136 // Metadate "headers" in before dictionary text contained the following: | |
137 // Each dictionary payload consists of several headers, followed by the text | |
138 // of the dictionary. The following are the known headers. | |
139 const std::string domain_; | |
140 const std::string path_; | |
141 const base::Time expiration_; // Implied by max-age. | |
142 const std::set<int> ports_; | |
143 | |
144 scoped_ptr<base::Clock> clock_; | |
145 | |
146 void operator=(const Dictionary&) = delete; | |
147 }; | |
148 | |
149 // A handle for one or more dictionaries which will keep the dictionaries | |
150 // alive and accessible for the handle's lifetime. | |
151 class NET_EXPORT_PRIVATE DictionarySet { | |
152 public: | |
153 ~DictionarySet(); | |
154 | |
155 // Return a comma separated list of client hashes. | |
156 std::string GetDictionaryClientHashList() const; | |
157 | |
158 // Lookup a given dictionary based on server hash. Returned pointer | |
159 // is guaranteed to be valid for the lifetime of the DictionarySet. | |
160 // Returns NULL if hash is not a valid server hash for a dictionary | |
161 // named by DictionarySet. | |
162 const SdchManager::Dictionary* GetDictionary(const std::string& hash) const; | |
163 | |
164 bool Empty() const; | |
165 | |
166 private: | |
167 // A DictionarySet may only be constructed by the SdchManager. | |
168 friend class SdchManager; | |
169 | |
170 DictionarySet(); | |
171 void AddDictionary(const std::string& server_hash, | |
172 const scoped_refptr<base::RefCountedData< | |
173 SdchManager::Dictionary>>& dictionary); | |
174 | |
175 DictionaryMap dictionaries_; | |
176 | |
177 DISALLOW_COPY_AND_ASSIGN(DictionarySet); | |
178 }; | |
179 | |
180 SdchManager(); | |
181 ~SdchManager(); | |
182 | |
183 // Clear data (for browser data removal). | |
184 void ClearData(); | |
185 | |
186 // Record stats on various errors. | |
187 static void SdchErrorRecovery(SdchProblemCode problem); | |
188 | |
189 // Enables or disables SDCH compression. | |
190 static void EnableSdchSupport(bool enabled); | |
191 | |
192 static bool sdch_enabled() { return g_sdch_enabled_; } | |
193 | |
194 // Enables or disables SDCH compression over secure connection. | |
195 static void EnableSecureSchemeSupport(bool enabled); | |
196 | |
197 static bool secure_scheme_supported() { return g_secure_scheme_supported_; } | |
198 | |
199 // Briefly prevent further advertising of SDCH on this domain (if SDCH is | |
200 // enabled). After enough calls to IsInSupportedDomain() the blacklisting | |
201 // will be removed. Additional blacklists take exponentially more calls | |
202 // to IsInSupportedDomain() before the blacklisting is undone. | |
203 // Used when filter errors are found from a given domain, but it is plausible | |
204 // that the cause is temporary (such as application startup, where cached | |
205 // entries are used, but a dictionary is not yet loaded). | |
206 void BlacklistDomain(const GURL& url, SdchProblemCode blacklist_reason); | |
207 | |
208 // Used when SEVERE filter errors are found from a given domain, to prevent | |
209 // further use of SDCH on that domain. | |
210 void BlacklistDomainForever(const GURL& url, | |
211 SdchProblemCode blacklist_reason); | |
212 | |
213 // Unit test only, this function resets enabling of sdch, and clears the | |
214 // blacklist. | |
215 void ClearBlacklistings(); | |
216 | |
217 // Unit test only, this function resets the blacklisting count for a domain. | |
218 void ClearDomainBlacklisting(const std::string& domain); | |
219 | |
220 // Unit test only: indicate how many more times a domain will be blacklisted. | |
221 int BlackListDomainCount(const std::string& domain); | |
222 | |
223 // Unit test only: Indicate what current blacklist increment is for a domain. | |
224 int BlacklistDomainExponential(const std::string& domain); | |
225 | |
226 // Check to see if SDCH is enabled (globally), and the given URL is in a | |
227 // supported domain (i.e., not blacklisted, and either the specific supported | |
228 // domain, or all domains were assumed supported). If it is blacklist, reduce | |
229 // by 1 the number of times it will be reported as blacklisted. | |
230 SdchProblemCode IsInSupportedDomain(const GURL& url); | |
231 | |
232 // Send out appropriate events notifying observers that a Get-Dictionary | |
233 // header has been seen. | |
234 SdchProblemCode OnGetDictionary(const GURL& request_url, | |
235 const GURL& dictionary_url); | |
236 | |
237 // Send out appropriate events notifying observers that a dictionary | |
238 // was successfully used to decode a request. | |
239 void OnDictionaryUsed(const std::string& server_hash); | |
240 | |
241 // Get a handle to the available dictionaries that might be used | |
242 // for encoding responses for the given URL. The return set will not | |
243 // include expired dictionaries. If no dictionaries | |
244 // are appropriate to use with the target_url, NULL is returned. | |
245 scoped_ptr<DictionarySet> GetDictionarySet(const GURL& target_url); | |
246 | |
247 // Get a handle to a specific dictionary, by its server hash, confirming | |
248 // that that specific dictionary is appropriate to use with |target_url|. | |
249 // Expired dictionaries will be returned. If no dictionary with that | |
250 // hash exists that is usable with |target_url|, NULL is returned. | |
251 // If there is a usability problem, |*error_code| is set to the | |
252 // appropriate problem code. | |
253 scoped_ptr<DictionarySet> GetDictionarySetByHash( | |
254 const GURL& target_url, | |
255 const std::string& server_hash, | |
256 SdchProblemCode* problem_code); | |
257 | |
258 // Construct the pair of hashes for client and server to identify an SDCH | |
259 // dictionary. This is only made public to facilitate unit testing, but is | |
260 // otherwise private | |
261 static void GenerateHash(const std::string& dictionary_text, | |
262 std::string* client_hash, std::string* server_hash); | |
263 | |
264 // For Latency testing only, we need to know if we've succeeded in doing a | |
265 // round trip before starting our comparative tests. If ever we encounter | |
266 // problems with SDCH, we opt-out of the test unless/until we perform a | |
267 // complete SDCH decoding. | |
268 bool AllowLatencyExperiment(const GURL& url) const; | |
269 | |
270 void SetAllowLatencyExperiment(const GURL& url, bool enable); | |
271 | |
272 base::Value* SdchInfoToValue() const; | |
273 | |
274 // Add an SDCH dictionary to our list of availible | |
275 // dictionaries. This addition will fail if addition is illegal | |
276 // (data in the dictionary is not acceptable from the | |
277 // dictionary_url; dictionary already added, etc.). | |
278 // If |server_hash| is non-null, returns the server hash that may be | |
279 // used as an argument to GetDictionarySetByHash. | |
280 // Returns SDCH_OK if the addition was successfull, and corresponding error | |
281 // code otherwise. | |
282 SdchProblemCode AddSdchDictionary(const std::string& dictionary_text, | |
283 const GURL& dictionary_url, | |
284 std::string* server_hash_p); | |
285 | |
286 // Remove an SDCH dictionary | |
287 SdchProblemCode RemoveSdchDictionary(const std::string& server_hash); | |
288 | |
289 // Registration for events generated by the SDCH subsystem. | |
290 void AddObserver(SdchObserver* observer); | |
291 void RemoveObserver(SdchObserver* observer); | |
292 | |
293 static scoped_ptr<DictionarySet> CreateEmptyDictionarySetForTesting(); | |
294 | |
295 private: | |
296 struct BlacklistInfo { | |
297 BlacklistInfo() : count(0), exponential_count(0), reason(SDCH_OK) {} | |
298 | |
299 int count; // # of times to refuse SDCH advertisement. | |
300 int exponential_count; // Current exponential backoff ratchet. | |
301 SdchProblemCode reason; // Why domain was blacklisted. | |
302 }; | |
303 | |
304 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo; | |
305 typedef std::set<std::string> ExperimentSet; | |
306 | |
307 // Determines whether a "Get-Dictionary" header is legal (dictionary | |
308 // url has appropriate relationship to referrer url) in the SDCH | |
309 // protocol. Return SDCH_OK if fetch is legal. | |
310 SdchProblemCode CanFetchDictionary(const GURL& referring_url, | |
311 const GURL& dictionary_url) const; | |
312 | |
313 // Support SDCH compression, by advertising in headers. | |
314 static bool g_sdch_enabled_; | |
315 | |
316 // Support SDCH compression for HTTPS requests and responses. When supported, | |
317 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS. | |
318 static bool g_secure_scheme_supported_; | |
319 | |
320 // A simple implementation of a RFC 3548 "URL safe" base64 encoder. | |
321 static void UrlSafeBase64Encode(const std::string& input, | |
322 std::string* output); | |
323 | |
324 DictionaryMap dictionaries_; | |
325 | |
326 // List domains where decode failures have required disabling sdch. | |
327 DomainBlacklistInfo blacklisted_domains_; | |
328 | |
329 // List of hostnames for which a latency experiment is allowed (because a | |
330 // round trip test has recently passed). | |
331 ExperimentSet allow_latency_experiment_; | |
332 | |
333 // Observers that want to be notified of SDCH events. | |
334 // Assert list is empty on destruction since if there is an observer | |
335 // that hasn't removed itself from the list, that observer probably | |
336 // has a reference to the SdchManager. | |
337 ObserverList<SdchObserver, true> observers_; | |
338 | |
339 base::ThreadChecker thread_checker_; | |
340 | |
341 DISALLOW_COPY_AND_ASSIGN(SdchManager); | |
342 }; | |
343 | |
344 } // namespace net | |
345 | |
346 #endif // NET_BASE_SDCH_MANAGER_H_ | |
OLD | NEW |