OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Provides global database of differential decompression dictionaries for the | 5 // Provides global database of differential decompression dictionaries for the |
6 // SDCH filter (processes sdch enconded content). | 6 // SDCH filter (processes sdch enconded content). |
7 | 7 |
8 // Exactly one instance of SdchManager is built, and all references are made | 8 // Exactly one instance of SdchManager is built, and all references are made |
9 // into that collection. | 9 // into that collection. |
10 // | 10 // |
11 // The SdchManager maintains a collection of memory resident dictionaries. It | 11 // The SdchManager maintains a collection of memory resident dictionaries. It |
12 // can find a dictionary (based on a server specification of a hash), store a | 12 // can find a dictionary (based on a server specification of a hash), store a |
13 // dictionary, and make judgements about what URLs can use, set, etc. a | 13 // dictionary, and make judgements about what URLs can use, set, etc. a |
14 // dictionary. | 14 // dictionary. |
15 | 15 |
16 // These dictionaries are acquired over the net, and include a header | 16 // These dictionaries are acquired over the net, and include a header |
17 // (containing metadata) as well as a VCDIFF dictionary (for use by a VCDIFF | 17 // (containing metadata) as well as a VCDIFF dictionary (for use by a VCDIFF |
18 // module) to decompress data. | 18 // module) to decompress data. |
19 | 19 |
20 #ifndef NET_BASE_SDCH_MANAGER_H_ | 20 #ifndef NET_BASE_SDCH_MANAGER_H_ |
21 #define NET_BASE_SDCH_MANAGER_H_ | 21 #define NET_BASE_SDCH_MANAGER_H_ |
22 | 22 |
23 #include <map> | 23 #include <map> |
24 #include <set> | 24 #include <set> |
25 #include <string> | 25 #include <string> |
26 | 26 |
27 #include "base/gtest_prod_util.h" | 27 #include "base/gtest_prod_util.h" |
28 #include "base/memory/ref_counted.h" | 28 #include "base/memory/ref_counted.h" |
29 #include "base/memory/scoped_ptr.h" | 29 #include "base/memory/scoped_ptr.h" |
30 #include "base/threading/non_thread_safe.h" | 30 #include "base/threading/non_thread_safe.h" |
31 #include "base/time/time.h" | 31 #include "base/time/time.h" |
32 #include "net/base/net_export.h" | 32 #include "net/base/net_export.h" |
33 #include "url/gurl.h" | 33 #include "url/gurl.h" |
34 | 34 |
35 namespace net { | 35 namespace net { |
36 | 36 |
37 //------------------------------------------------------------------------------ | 37 //------------------------------------------------------------------------------ |
38 // Create a public interface to help us load SDCH dictionaries. | 38 // Create a public interface to help us load SDCH dictionaries. |
39 // The SdchManager class allows registration to support this interface. | 39 // The SdchManager class allows registration to support this interface. |
40 // A browser may register a fetcher that is used by the dictionary managers to | 40 // A browser may register a fetcher that is used by the dictionary managers to |
41 // get data from a specified URL. This allows us to use very high level browser | 41 // get data from a specified URL. This allows us to use very high level browser |
42 // functionality in this base (when the functionaity can be provided). | 42 // functionality in this base (when the functionality can be provided). |
43 class NET_EXPORT SdchFetcher { | 43 class NET_EXPORT SdchFetcher { |
44 public: | 44 public: |
45 class Delegate { | |
46 public: | |
47 virtual ~Delegate() {} | |
48 | |
49 // Deliver the fetched dictionary back to the consumer. | |
Ryan Sleevi
2014/09/08 19:29:54
Documentation:
// Called whenever the SdchFetcher
Randy Smith (Not in Mondays)
2014/09/08 20:03:29
Good point. Done.
| |
50 virtual void AddSdchDictionary(const std::string& dictionary_text, | |
51 const GURL& dictionary_url) = 0; | |
52 }; | |
53 | |
45 SdchFetcher() {} | 54 SdchFetcher() {} |
46 virtual ~SdchFetcher() {} | 55 virtual ~SdchFetcher() {} |
47 | 56 |
48 // The Schedule() method is called when there is a need to get a dictionary | 57 // The Schedule() method is called when there is a need to get a dictionary |
49 // from a server. The callee is responsible for getting that dictionary_text, | 58 // from a server. The callee is responsible for getting that dictionary_text, |
50 // and then calling back to AddSdchDictionary() to the SdchManager instance. | 59 // and then calling back to AddSdchDictionary() in the Delegate instance. |
51 virtual void Schedule(const GURL& dictionary_url) = 0; | 60 virtual void Schedule(const GURL& dictionary_url) = 0; |
52 | 61 |
53 // The Cancel() method is called to cancel all pending dictionary fetches. | 62 // The Cancel() method is called to cancel all pending dictionary fetches. |
54 // This is used for implementation of ClearData() below. | 63 // This is used for implementation of ClearData() below. |
55 virtual void Cancel() = 0; | 64 virtual void Cancel() = 0; |
56 | 65 |
57 private: | 66 private: |
58 DISALLOW_COPY_AND_ASSIGN(SdchFetcher); | 67 DISALLOW_COPY_AND_ASSIGN(SdchFetcher); |
59 }; | 68 }; |
60 | 69 |
61 //------------------------------------------------------------------------------ | 70 //------------------------------------------------------------------------------ |
62 | 71 |
63 class NET_EXPORT SdchManager : public NON_EXPORTED_BASE(base::NonThreadSafe) { | 72 class NET_EXPORT SdchManager |
73 : public SdchFetcher::Delegate, | |
74 public NON_EXPORTED_BASE(base::NonThreadSafe) { | |
64 public: | 75 public: |
65 // A list of errors that appeared and were either resolved, or used to turn | 76 // A list of errors that appeared and were either resolved, or used to turn |
66 // off sdch encoding. | 77 // off sdch encoding. |
67 enum ProblemCodes { | 78 enum ProblemCodes { |
68 MIN_PROBLEM_CODE, | 79 MIN_PROBLEM_CODE, |
69 | 80 |
70 // Content-encoding correction problems. | 81 // Content-encoding correction problems. |
71 ADDED_CONTENT_ENCODING = 1, | 82 ADDED_CONTENT_ENCODING = 1, |
72 FIXED_CONTENT_ENCODING = 2, | 83 FIXED_CONTENT_ENCODING = 2, |
73 FIXED_CONTENT_ENCODINGS = 3, | 84 FIXED_CONTENT_ENCODINGS = 3, |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 public: | 182 public: |
172 // Sdch filters can get our text to use in decoding compressed data. | 183 // Sdch filters can get our text to use in decoding compressed data. |
173 const std::string& text() const { return text_; } | 184 const std::string& text() const { return text_; } |
174 | 185 |
175 private: | 186 private: |
176 friend class base::RefCounted<Dictionary>; | 187 friend class base::RefCounted<Dictionary>; |
177 friend class SdchManager; // Only manager can construct an instance. | 188 friend class SdchManager; // Only manager can construct an instance. |
178 FRIEND_TEST_ALL_PREFIXES(SdchManagerTest, PathMatch); | 189 FRIEND_TEST_ALL_PREFIXES(SdchManagerTest, PathMatch); |
179 | 190 |
180 // Construct a vc-diff usable dictionary from the dictionary_text starting | 191 // Construct a vc-diff usable dictionary from the dictionary_text starting |
181 // at the given offset. The supplied client_hash should be used to | 192 // at the given offset. The supplied client_hash should be used to |
182 // advertise the dictionary's availability relative to the suppplied URL. | 193 // advertise the dictionary's availability relative to the suppplied URL. |
183 Dictionary(const std::string& dictionary_text, | 194 Dictionary(const std::string& dictionary_text, |
184 size_t offset, | 195 size_t offset, |
185 const std::string& client_hash, | 196 const std::string& client_hash, |
186 const GURL& url, | 197 const GURL& url, |
187 const std::string& domain, | 198 const std::string& domain, |
188 const std::string& path, | 199 const std::string& path, |
189 const base::Time& expiration, | 200 const base::Time& expiration, |
190 const std::set<int>& ports); | 201 const std::set<int>& ports); |
191 ~Dictionary(); | 202 virtual ~Dictionary(); |
192 | 203 |
193 const GURL& url() const { return url_; } | 204 const GURL& url() const { return url_; } |
194 const std::string& client_hash() const { return client_hash_; } | 205 const std::string& client_hash() const { return client_hash_; } |
195 | 206 |
196 // Security method to check if we can advertise this dictionary for use | 207 // Security method to check if we can advertise this dictionary for use |
197 // if the |target_url| returns SDCH compressed data. | 208 // if the |target_url| returns SDCH compressed data. |
198 bool CanAdvertise(const GURL& target_url); | 209 bool CanAdvertise(const GURL& target_url); |
199 | 210 |
200 // Security methods to check if we can establish a new dictionary with the | 211 // Security methods to check if we can establish a new dictionary with the |
201 // given data, that arrived in response to get of dictionary_url. | 212 // given data, that arrived in response to get of dictionary_url. |
(...skipping 18 matching lines...) Expand all Loading... | |
220 // Part of the hash of text_ that the client uses to advertise the fact that | 231 // Part of the hash of text_ that the client uses to advertise the fact that |
221 // it has a specific dictionary pre-cached. | 232 // it has a specific dictionary pre-cached. |
222 std::string client_hash_; | 233 std::string client_hash_; |
223 | 234 |
224 // The GURL that arrived with the text_ in a URL request to specify where | 235 // The GURL that arrived with the text_ in a URL request to specify where |
225 // this dictionary may be used. | 236 // this dictionary may be used. |
226 const GURL url_; | 237 const GURL url_; |
227 | 238 |
228 // Metadate "headers" in before dictionary text contained the following: | 239 // Metadate "headers" in before dictionary text contained the following: |
229 // Each dictionary payload consists of several headers, followed by the text | 240 // Each dictionary payload consists of several headers, followed by the text |
230 // of the dictionary. The following are the known headers. | 241 // of the dictionary. The following are the known headers. |
231 const std::string domain_; | 242 const std::string domain_; |
232 const std::string path_; | 243 const std::string path_; |
233 const base::Time expiration_; // Implied by max-age. | 244 const base::Time expiration_; // Implied by max-age. |
234 const std::set<int> ports_; | 245 const std::set<int> ports_; |
235 | 246 |
236 DISALLOW_COPY_AND_ASSIGN(Dictionary); | 247 DISALLOW_COPY_AND_ASSIGN(Dictionary); |
237 }; | 248 }; |
238 | 249 |
239 SdchManager(); | 250 SdchManager(); |
240 ~SdchManager(); | 251 virtual ~SdchManager(); |
241 | 252 |
242 // Clear data (for browser data removal). | 253 // Clear data (for browser data removal). |
243 void ClearData(); | 254 void ClearData(); |
244 | 255 |
245 // Record stats on various errors. | 256 // Record stats on various errors. |
246 static void SdchErrorRecovery(ProblemCodes problem); | 257 static void SdchErrorRecovery(ProblemCodes problem); |
247 | 258 |
248 // Register a fetcher that this class can use to obtain dictionaries. | 259 // Register a fetcher that this class can use to obtain dictionaries. |
249 void set_sdch_fetcher(scoped_ptr<SdchFetcher> fetcher); | 260 void set_sdch_fetcher(scoped_ptr<SdchFetcher> fetcher); |
250 | 261 |
251 // Enables or disables SDCH compression. | 262 // Enables or disables SDCH compression. |
252 static void EnableSdchSupport(bool enabled); | 263 static void EnableSdchSupport(bool enabled); |
253 | 264 |
254 static bool sdch_enabled() { return g_sdch_enabled_; } | 265 static bool sdch_enabled() { return g_sdch_enabled_; } |
255 | 266 |
256 // Enables or disables SDCH compression over secure connection. | 267 // Enables or disables SDCH compression over secure connection. |
257 static void EnableSecureSchemeSupport(bool enabled); | 268 static void EnableSecureSchemeSupport(bool enabled); |
258 | 269 |
259 static bool secure_scheme_supported() { return g_secure_scheme_supported_; } | 270 static bool secure_scheme_supported() { return g_secure_scheme_supported_; } |
260 | 271 |
261 // Briefly prevent further advertising of SDCH on this domain (if SDCH is | 272 // Briefly prevent further advertising of SDCH on this domain (if SDCH is |
262 // enabled). After enough calls to IsInSupportedDomain() the blacklisting | 273 // enabled). After enough calls to IsInSupportedDomain() the blacklisting |
263 // will be removed. Additional blacklists take exponentially more calls | 274 // will be removed. Additional blacklists take exponentially more calls |
264 // to IsInSupportedDomain() before the blacklisting is undone. | 275 // to IsInSupportedDomain() before the blacklisting is undone. |
265 // Used when filter errors are found from a given domain, but it is plausible | 276 // Used when filter errors are found from a given domain, but it is plausible |
266 // that the cause is temporary (such as application startup, where cached | 277 // that the cause is temporary (such as application startup, where cached |
267 // entries are used, but a dictionary is not yet loaded). | 278 // entries are used, but a dictionary is not yet loaded). |
268 void BlacklistDomain(const GURL& url, ProblemCodes blacklist_reason); | 279 void BlacklistDomain(const GURL& url, ProblemCodes blacklist_reason); |
269 | 280 |
270 // Used when SEVERE filter errors are found from a given domain, to prevent | 281 // Used when SEVERE filter errors are found from a given domain, to prevent |
271 // further use of SDCH on that domain. | 282 // further use of SDCH on that domain. |
272 void BlacklistDomainForever(const GURL& url, ProblemCodes blacklist_reason); | 283 void BlacklistDomainForever(const GURL& url, ProblemCodes blacklist_reason); |
273 | 284 |
274 // Unit test only, this function resets enabling of sdch, and clears the | 285 // Unit test only, this function resets enabling of sdch, and clears the |
275 // blacklist. | 286 // blacklist. |
276 void ClearBlacklistings(); | 287 void ClearBlacklistings(); |
277 | 288 |
278 // Unit test only, this function resets the blacklisting count for a domain. | 289 // Unit test only, this function resets the blacklisting count for a domain. |
279 void ClearDomainBlacklisting(const std::string& domain); | 290 void ClearDomainBlacklisting(const std::string& domain); |
280 | 291 |
281 // Unit test only: indicate how many more times a domain will be blacklisted. | 292 // Unit test only: indicate how many more times a domain will be blacklisted. |
282 int BlackListDomainCount(const std::string& domain); | 293 int BlackListDomainCount(const std::string& domain); |
283 | 294 |
284 // Unit test only: Indicate what current blacklist increment is for a domain. | 295 // Unit test only: Indicate what current blacklist increment is for a domain. |
285 int BlacklistDomainExponential(const std::string& domain); | 296 int BlacklistDomainExponential(const std::string& domain); |
286 | 297 |
287 // Check to see if SDCH is enabled (globally), and the given URL is in a | 298 // Check to see if SDCH is enabled (globally), and the given URL is in a |
288 // supported domain (i.e., not blacklisted, and either the specific supported | 299 // supported domain (i.e., not blacklisted, and either the specific supported |
289 // domain, or all domains were assumed supported). If it is blacklist, reduce | 300 // domain, or all domains were assumed supported). If it is blacklist, reduce |
290 // by 1 the number of times it will be reported as blacklisted. | 301 // by 1 the number of times it will be reported as blacklisted. |
291 bool IsInSupportedDomain(const GURL& url); | 302 bool IsInSupportedDomain(const GURL& url); |
292 | 303 |
293 // Schedule the URL fetching to load a dictionary. This will always return | 304 // Schedule the URL fetching to load a dictionary. This will always return |
294 // before the dictionary is actually loaded and added. | 305 // before the dictionary is actually loaded and added. |
295 // After the implied task does completes, the dictionary will have been | 306 // After the implied task does completes, the dictionary will have been |
296 // cached in memory. | 307 // cached in memory. |
297 void FetchDictionary(const GURL& request_url, const GURL& dictionary_url); | 308 void FetchDictionary(const GURL& request_url, const GURL& dictionary_url); |
298 | 309 |
299 // Security test function used before initiating a FetchDictionary. | 310 // Security test function used before initiating a FetchDictionary. |
300 // Return true if fetch is legal. | 311 // Return true if fetch is legal. |
301 bool CanFetchDictionary(const GURL& referring_url, | 312 bool CanFetchDictionary(const GURL& referring_url, |
302 const GURL& dictionary_url) const; | 313 const GURL& dictionary_url) const; |
303 | 314 |
304 // Add an SDCH dictionary to our list of availible dictionaries. This addition | |
305 // will fail (return false) if addition is illegal (data in the dictionary is | |
306 // not acceptable from the dictionary_url; dictionary already added, etc.). | |
307 bool AddSdchDictionary(const std::string& dictionary_text, | |
308 const GURL& dictionary_url); | |
309 | |
310 // Find the vcdiff dictionary (the body of the sdch dictionary that appears | 315 // Find the vcdiff dictionary (the body of the sdch dictionary that appears |
311 // after the meta-data headers like Domain:...) with the given |server_hash| | 316 // after the meta-data headers like Domain:...) with the given |server_hash| |
312 // to use to decompreses data that arrived as SDCH encoded content. Check to | 317 // to use to decompreses data that arrived as SDCH encoded content. Check to |
313 // be sure the returned |dictionary| can be used for decoding content supplied | 318 // be sure the returned |dictionary| can be used for decoding content supplied |
314 // in response to a request for |referring_url|. | 319 // in response to a request for |referring_url|. |
315 // Return null in |dictionary| if there is no matching legal dictionary. | 320 // Return null in |dictionary| if there is no matching legal dictionary. |
316 void GetVcdiffDictionary(const std::string& server_hash, | 321 void GetVcdiffDictionary(const std::string& server_hash, |
317 const GURL& referring_url, | 322 const GURL& referring_url, |
318 scoped_refptr<Dictionary>* dictionary); | 323 scoped_refptr<Dictionary>* dictionary); |
319 | 324 |
320 // Get list of available (pre-cached) dictionaries that we have already loaded | 325 // Get list of available (pre-cached) dictionaries that we have already loaded |
321 // into memory. The list is a comma separated list of (client) hashes per | 326 // into memory. The list is a comma separated list of (client) hashes per |
322 // the SDCH spec. | 327 // the SDCH spec. |
323 void GetAvailDictionaryList(const GURL& target_url, std::string* list); | 328 void GetAvailDictionaryList(const GURL& target_url, std::string* list); |
324 | 329 |
325 // Construct the pair of hashes for client and server to identify an SDCH | 330 // Construct the pair of hashes for client and server to identify an SDCH |
326 // dictionary. This is only made public to facilitate unit testing, but is | 331 // dictionary. This is only made public to facilitate unit testing, but is |
327 // otherwise private | 332 // otherwise private |
328 static void GenerateHash(const std::string& dictionary_text, | 333 static void GenerateHash(const std::string& dictionary_text, |
329 std::string* client_hash, std::string* server_hash); | 334 std::string* client_hash, std::string* server_hash); |
330 | 335 |
331 // For Latency testing only, we need to know if we've succeeded in doing a | 336 // For Latency testing only, we need to know if we've succeeded in doing a |
332 // round trip before starting our comparative tests. If ever we encounter | 337 // round trip before starting our comparative tests. If ever we encounter |
333 // problems with SDCH, we opt-out of the test unless/until we perform a | 338 // problems with SDCH, we opt-out of the test unless/until we perform a |
334 // complete SDCH decoding. | 339 // complete SDCH decoding. |
335 bool AllowLatencyExperiment(const GURL& url) const; | 340 bool AllowLatencyExperiment(const GURL& url) const; |
336 | 341 |
337 void SetAllowLatencyExperiment(const GURL& url, bool enable); | 342 void SetAllowLatencyExperiment(const GURL& url, bool enable); |
338 | 343 |
339 int GetFetchesCountForTesting() const { | 344 int GetFetchesCountForTesting() const { |
340 return fetches_count_for_testing_; | 345 return fetches_count_for_testing_; |
341 } | 346 } |
342 | 347 |
348 // Implementation of SdchFetcher::Delegate. | |
349 | |
350 // Add an SDCH dictionary to our list of availible | |
351 // dictionaries. This addition will fail if addition is illegal | |
352 // (data in the dictionary is not acceptable from the | |
353 // dictionary_url; dictionary already added, etc.). | |
354 virtual void AddSdchDictionary(const std::string& dictionary_text, | |
355 const GURL& dictionary_url) OVERRIDE; | |
356 | |
343 private: | 357 private: |
344 struct BlacklistInfo { | 358 struct BlacklistInfo { |
345 BlacklistInfo() | 359 BlacklistInfo() |
346 : count(0), | 360 : count(0), |
347 exponential_count(0), | 361 exponential_count(0), |
348 reason(MIN_PROBLEM_CODE) {} | 362 reason(MIN_PROBLEM_CODE) {} |
349 | 363 |
350 int count; // # of times to refuse SDCH advertisement. | 364 int count; // # of times to refuse SDCH advertisement. |
351 int exponential_count; // Current exponential backoff ratchet. | 365 int exponential_count; // Current exponential backoff ratchet. |
352 ProblemCodes reason; // Why domain was blacklisted. | 366 ProblemCodes reason; // Why domain was blacklisted. |
(...skipping 28 matching lines...) Expand all Loading... | |
381 ExperimentSet allow_latency_experiment_; | 395 ExperimentSet allow_latency_experiment_; |
382 | 396 |
383 int fetches_count_for_testing_; | 397 int fetches_count_for_testing_; |
384 | 398 |
385 DISALLOW_COPY_AND_ASSIGN(SdchManager); | 399 DISALLOW_COPY_AND_ASSIGN(SdchManager); |
386 }; | 400 }; |
387 | 401 |
388 } // namespace net | 402 } // namespace net |
389 | 403 |
390 #endif // NET_BASE_SDCH_MANAGER_H_ | 404 #endif // NET_BASE_SDCH_MANAGER_H_ |
OLD | NEW |