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/observer_list.h" |
31 #include "base/threading/thread_checker.h" | |
31 #include "base/time/time.h" | 32 #include "base/time/time.h" |
32 #include "net/base/net_export.h" | 33 #include "net/base/net_export.h" |
33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
34 | 35 |
35 namespace net { | 36 namespace net { |
36 | 37 |
37 //------------------------------------------------------------------------------ | 38 class SdchObserver; |
38 // Create a public interface to help us load SDCH dictionaries. | 39 class URLRequest; |
Ryan Sleevi
2014/11/04 21:40:44
Unneeded now, right?
Randy Smith (Not in Mondays)
2014/11/05 20:35:01
Right; thanks.
| |
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 | |
41 // get data from a specified URL. This allows us to use very high level browser | |
42 // functionality in this base (when the functionality can be provided). | |
43 class NET_EXPORT SdchFetcher { | |
44 public: | |
45 class NET_EXPORT Delegate { | |
46 public: | |
47 virtual ~Delegate() {} | |
48 | |
49 // Called whenever the SdchFetcher has successfully retrieved a | |
50 // dictionary. |dictionary_text| contains the body of the dictionary | |
51 // retrieved from |dictionary_url|. | |
52 virtual void AddSdchDictionary(const std::string& dictionary_text, | |
53 const GURL& dictionary_url) = 0; | |
54 }; | |
55 | |
56 SdchFetcher() {} | |
57 virtual ~SdchFetcher() {} | |
58 | |
59 // The Schedule() method is called when there is a need to get a dictionary | |
60 // from a server. The callee is responsible for getting that dictionary_text, | |
61 // and then calling back to AddSdchDictionary() in the Delegate instance. | |
62 virtual void Schedule(const GURL& dictionary_url) = 0; | |
63 | |
64 // The Cancel() method is called to cancel all pending dictionary fetches. | |
65 // This is used for implementation of ClearData() below. | |
66 virtual void Cancel() = 0; | |
67 | |
68 private: | |
69 DISALLOW_COPY_AND_ASSIGN(SdchFetcher); | |
70 }; | |
71 | 40 |
72 //------------------------------------------------------------------------------ | 41 //------------------------------------------------------------------------------ |
Ryan Sleevi
2014/11/04 21:40:44
delete
Randy Smith (Not in Mondays)
2014/11/05 20:35:01
Done.
| |
73 | 42 |
74 class NET_EXPORT SdchManager | 43 class NET_EXPORT SdchManager { |
Ryan Sleevi
2014/11/04 21:40:44
Documentation?
Randy Smith (Not in Mondays)
2014/11/05 20:35:01
What's your mental model for the documentation to
Ryan Sleevi
2014/11/05 22:21:39
My mental model is to try and keep class-specific
| |
75 : public SdchFetcher::Delegate, | |
76 public NON_EXPORTED_BASE(base::NonThreadSafe) { | |
77 public: | 44 public: |
78 // A list of errors that appeared and were either resolved, or used to turn | 45 // A list of errors that appeared and were either resolved, or used to turn |
79 // off sdch encoding. | 46 // off sdch encoding. |
80 enum ProblemCodes { | 47 enum ProblemCodes { |
81 MIN_PROBLEM_CODE, | 48 MIN_PROBLEM_CODE, |
82 | 49 |
83 // Content-encoding correction problems. | 50 // Content-encoding correction problems. |
84 ADDED_CONTENT_ENCODING = 1, | 51 ADDED_CONTENT_ENCODING = 1, |
85 FIXED_CONTENT_ENCODING = 2, | 52 FIXED_CONTENT_ENCODING = 2, |
86 FIXED_CONTENT_ENCODINGS = 3, | 53 FIXED_CONTENT_ENCODINGS = 3, |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
245 // of the dictionary. The following are the known headers. | 212 // of the dictionary. The following are the known headers. |
246 const std::string domain_; | 213 const std::string domain_; |
247 const std::string path_; | 214 const std::string path_; |
248 const base::Time expiration_; // Implied by max-age. | 215 const base::Time expiration_; // Implied by max-age. |
249 const std::set<int> ports_; | 216 const std::set<int> ports_; |
250 | 217 |
251 DISALLOW_COPY_AND_ASSIGN(Dictionary); | 218 DISALLOW_COPY_AND_ASSIGN(Dictionary); |
252 }; | 219 }; |
253 | 220 |
254 SdchManager(); | 221 SdchManager(); |
255 ~SdchManager() override; | 222 virtual ~SdchManager(); |
256 | 223 |
257 // Clear data (for browser data removal). | 224 // Clear data (for browser data removal). |
258 void ClearData(); | 225 void ClearData(); |
259 | 226 |
260 // Record stats on various errors. | 227 // Record stats on various errors. |
261 static void SdchErrorRecovery(ProblemCodes problem); | 228 static void SdchErrorRecovery(ProblemCodes problem); |
262 | 229 |
263 // Register a fetcher that this class can use to obtain dictionaries. | |
264 void set_sdch_fetcher(scoped_ptr<SdchFetcher> fetcher); | |
265 | |
266 // Enables or disables SDCH compression. | 230 // Enables or disables SDCH compression. |
267 static void EnableSdchSupport(bool enabled); | 231 static void EnableSdchSupport(bool enabled); |
268 | 232 |
269 static bool sdch_enabled() { return g_sdch_enabled_; } | 233 static bool sdch_enabled() { return g_sdch_enabled_; } |
270 | 234 |
271 // Enables or disables SDCH compression over secure connection. | 235 // Enables or disables SDCH compression over secure connection. |
272 static void EnableSecureSchemeSupport(bool enabled); | 236 static void EnableSecureSchemeSupport(bool enabled); |
273 | 237 |
274 static bool secure_scheme_supported() { return g_secure_scheme_supported_; } | 238 static bool secure_scheme_supported() { return g_secure_scheme_supported_; } |
275 | 239 |
(...skipping 22 matching lines...) Expand all Loading... | |
298 | 262 |
299 // Unit test only: Indicate what current blacklist increment is for a domain. | 263 // Unit test only: Indicate what current blacklist increment is for a domain. |
300 int BlacklistDomainExponential(const std::string& domain); | 264 int BlacklistDomainExponential(const std::string& domain); |
301 | 265 |
302 // Check to see if SDCH is enabled (globally), and the given URL is in a | 266 // Check to see if SDCH is enabled (globally), and the given URL is in a |
303 // supported domain (i.e., not blacklisted, and either the specific supported | 267 // supported domain (i.e., not blacklisted, and either the specific supported |
304 // domain, or all domains were assumed supported). If it is blacklist, reduce | 268 // domain, or all domains were assumed supported). If it is blacklist, reduce |
305 // by 1 the number of times it will be reported as blacklisted. | 269 // by 1 the number of times it will be reported as blacklisted. |
306 bool IsInSupportedDomain(const GURL& url); | 270 bool IsInSupportedDomain(const GURL& url); |
307 | 271 |
308 // Schedule the URL fetching to load a dictionary. This will always return | 272 // Send out appropriate events notifying observers that a Get-Dictionary |
309 // before the dictionary is actually loaded and added. | 273 // header has been seen. |
310 // After the implied task does completes, the dictionary will have been | 274 void OnGetDictionary(const GURL& request_url, const GURL& dictionary_url); |
311 // cached in memory. | |
312 void FetchDictionary(const GURL& request_url, const GURL& dictionary_url); | |
313 | |
314 // Security test function used before initiating a FetchDictionary. | |
315 // Return true if fetch is legal. | |
316 bool CanFetchDictionary(const GURL& referring_url, | |
317 const GURL& dictionary_url) const; | |
318 | 275 |
319 // Find the vcdiff dictionary (the body of the sdch dictionary that appears | 276 // Find the vcdiff dictionary (the body of the sdch dictionary that appears |
320 // after the meta-data headers like Domain:...) with the given |server_hash| | 277 // after the meta-data headers like Domain:...) with the given |server_hash| |
321 // to use to decompreses data that arrived as SDCH encoded content. Check to | 278 // to use to decompreses data that arrived as SDCH encoded content. Check to |
322 // be sure the returned |dictionary| can be used for decoding content supplied | 279 // be sure the returned |dictionary| can be used for decoding content supplied |
323 // in response to a request for |referring_url|. | 280 // in response to a request for |referring_url|. |
324 // Return null in |dictionary| if there is no matching legal dictionary. | 281 // Return null in |dictionary| if there is no matching legal dictionary. |
325 void GetVcdiffDictionary(const std::string& server_hash, | 282 void GetVcdiffDictionary(const std::string& server_hash, |
326 const GURL& referring_url, | 283 const GURL& referring_url, |
327 scoped_refptr<Dictionary>* dictionary); | 284 scoped_refptr<Dictionary>* dictionary); |
(...skipping 10 matching lines...) Expand all Loading... | |
338 std::string* client_hash, std::string* server_hash); | 295 std::string* client_hash, std::string* server_hash); |
339 | 296 |
340 // For Latency testing only, we need to know if we've succeeded in doing a | 297 // For Latency testing only, we need to know if we've succeeded in doing a |
341 // round trip before starting our comparative tests. If ever we encounter | 298 // round trip before starting our comparative tests. If ever we encounter |
342 // problems with SDCH, we opt-out of the test unless/until we perform a | 299 // problems with SDCH, we opt-out of the test unless/until we perform a |
343 // complete SDCH decoding. | 300 // complete SDCH decoding. |
344 bool AllowLatencyExperiment(const GURL& url) const; | 301 bool AllowLatencyExperiment(const GURL& url) const; |
345 | 302 |
346 void SetAllowLatencyExperiment(const GURL& url, bool enable); | 303 void SetAllowLatencyExperiment(const GURL& url, bool enable); |
347 | 304 |
348 int GetFetchesCountForTesting() const { | |
349 return fetches_count_for_testing_; | |
350 } | |
351 | |
352 // Implementation of SdchFetcher::Delegate. | |
353 | |
354 // Add an SDCH dictionary to our list of availible | 305 // Add an SDCH dictionary to our list of availible |
355 // dictionaries. This addition will fail if addition is illegal | 306 // dictionaries. This addition will fail if addition is illegal |
356 // (data in the dictionary is not acceptable from the | 307 // (data in the dictionary is not acceptable from the |
357 // dictionary_url; dictionary already added, etc.). | 308 // dictionary_url; dictionary already added, etc.). |
358 void AddSdchDictionary(const std::string& dictionary_text, | 309 void AddSdchDictionary(const std::string& dictionary_text, |
359 const GURL& dictionary_url) override; | 310 const GURL& dictionary_url); |
311 | |
312 // Registration for events generated by the SDCH subsystem. | |
313 virtual void AddObserver(SdchObserver* observer); | |
314 virtual void RemoveObserver(SdchObserver* observer); | |
360 | 315 |
361 private: | 316 private: |
362 struct BlacklistInfo { | 317 struct BlacklistInfo { |
363 BlacklistInfo() | 318 BlacklistInfo() |
364 : count(0), | 319 : count(0), |
365 exponential_count(0), | 320 exponential_count(0), |
366 reason(MIN_PROBLEM_CODE) {} | 321 reason(MIN_PROBLEM_CODE) {} |
367 | 322 |
368 int count; // # of times to refuse SDCH advertisement. | 323 int count; // # of times to refuse SDCH advertisement. |
369 int exponential_count; // Current exponential backoff ratchet. | 324 int exponential_count; // Current exponential backoff ratchet. |
370 ProblemCodes reason; // Why domain was blacklisted. | 325 ProblemCodes reason; // Why domain was blacklisted. |
371 | 326 |
372 }; | 327 }; |
373 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo; | 328 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo; |
374 typedef std::set<std::string> ExperimentSet; | 329 typedef std::set<std::string> ExperimentSet; |
375 | 330 |
331 // Determines whether a "Get-Dictionary" header is legal (dictionary | |
332 // url has appropriate relationship to referrer url) in the SDCH | |
333 // protocol. Return true if fetch is legal. | |
334 bool CanFetchDictionary(const GURL& referring_url, | |
335 const GURL& dictionary_url) const; | |
336 | |
376 // A map of dictionaries info indexed by the hash that the server provides. | 337 // A map of dictionaries info indexed by the hash that the server provides. |
377 typedef std::map<std::string, scoped_refptr<Dictionary> > DictionaryMap; | 338 typedef std::map<std::string, scoped_refptr<Dictionary> > DictionaryMap; |
378 | 339 |
379 // Support SDCH compression, by advertising in headers. | 340 // Support SDCH compression, by advertising in headers. |
380 static bool g_sdch_enabled_; | 341 static bool g_sdch_enabled_; |
381 | 342 |
382 // Support SDCH compression for HTTPS requests and responses. When supported, | 343 // Support SDCH compression for HTTPS requests and responses. When supported, |
383 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS. | 344 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS. |
384 static bool g_secure_scheme_supported_; | 345 static bool g_secure_scheme_supported_; |
385 | 346 |
386 // A simple implementation of a RFC 3548 "URL safe" base64 encoder. | 347 // A simple implementation of a RFC 3548 "URL safe" base64 encoder. |
387 static void UrlSafeBase64Encode(const std::string& input, | 348 static void UrlSafeBase64Encode(const std::string& input, |
388 std::string* output); | 349 std::string* output); |
389 DictionaryMap dictionaries_; | 350 DictionaryMap dictionaries_; |
390 | 351 |
391 // An instance that can fetch a dictionary given a URL. | |
392 scoped_ptr<SdchFetcher> fetcher_; | |
393 | |
394 // List domains where decode failures have required disabling sdch. | 352 // List domains where decode failures have required disabling sdch. |
395 DomainBlacklistInfo blacklisted_domains_; | 353 DomainBlacklistInfo blacklisted_domains_; |
396 | 354 |
397 // List of hostnames for which a latency experiment is allowed (because a | 355 // List of hostnames for which a latency experiment is allowed (because a |
398 // round trip test has recently passed). | 356 // round trip test has recently passed). |
399 ExperimentSet allow_latency_experiment_; | 357 ExperimentSet allow_latency_experiment_; |
400 | 358 |
401 int fetches_count_for_testing_; | 359 // Observers that want to be notified of SDCH events. |
360 // Assert list is empty on destruction since if there is an observer | |
361 // that hasn't removed itself from the list, that observer probably | |
362 // has a reference to the SdchManager. | |
363 ObserverList<SdchObserver, true> observers_; | |
364 | |
365 base::ThreadChecker thread_checker_; | |
402 | 366 |
403 DISALLOW_COPY_AND_ASSIGN(SdchManager); | 367 DISALLOW_COPY_AND_ASSIGN(SdchManager); |
404 }; | 368 }; |
405 | 369 |
406 } // namespace net | 370 } // namespace net |
407 | 371 |
408 #endif // NET_BASE_SDCH_MANAGER_H_ | 372 #endif // NET_BASE_SDCH_MANAGER_H_ |
OLD | NEW |