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 | |
6 // SDCH filter (processes sdch enconded content). | |
7 | |
8 // Exactly one instance of SdchManager is built, and all references are made | |
9 // into that collection. | |
10 // | |
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 | |
13 // dictionary, and make judgements about what URLs can use, set, etc. a | |
14 // dictionary. | |
15 | |
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 | |
18 // module) to decompress data. | |
19 | 5 |
mmenke
2014/11/06 15:30:12
nit: Remove extra blank line.
Randy Smith (Not in Mondays)
2014/11/06 16:39:04
Done.
| |
20 #ifndef NET_BASE_SDCH_MANAGER_H_ | 6 #ifndef NET_BASE_SDCH_MANAGER_H_ |
21 #define NET_BASE_SDCH_MANAGER_H_ | 7 #define NET_BASE_SDCH_MANAGER_H_ |
22 | 8 |
23 #include <map> | 9 #include <map> |
24 #include <set> | 10 #include <set> |
25 #include <string> | 11 #include <string> |
26 | 12 |
27 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
28 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
29 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
30 #include "base/threading/non_thread_safe.h" | 16 #include "base/observer_list.h" |
17 #include "base/threading/thread_checker.h" | |
31 #include "base/time/time.h" | 18 #include "base/time/time.h" |
32 #include "net/base/net_export.h" | 19 #include "net/base/net_export.h" |
33 #include "url/gurl.h" | 20 #include "url/gurl.h" |
34 | 21 |
35 namespace net { | 22 namespace net { |
36 | 23 |
37 //------------------------------------------------------------------------------ | 24 class SdchObserver; |
38 // Create a public interface to help us load SDCH dictionaries. | |
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 | 25 |
49 // Called whenever the SdchFetcher has successfully retrieved a | 26 // Provides global database of differential decompression dictionaries for the |
50 // dictionary. |dictionary_text| contains the body of the dictionary | 27 // SDCH filter (processes sdch enconded content). |
51 // retrieved from |dictionary_url|. | 28 // |
52 virtual void AddSdchDictionary(const std::string& dictionary_text, | 29 // The SdchManager maintains a collection of memory resident dictionaries. It |
53 const GURL& dictionary_url) = 0; | 30 // can find a dictionary (based on a server specification of a hash), store a |
54 }; | 31 // dictionary, and make judgements about what URLs can use, set, etc. a |
32 // dictionary. | |
55 | 33 |
56 SdchFetcher() {} | 34 // These dictionaries are acquired over the net, and include a header |
57 virtual ~SdchFetcher() {} | 35 // (containing metadata) as well as a VCDIFF dictionary (for use by a VCDIFF |
58 | 36 // module) to decompress data. |
59 // The Schedule() method is called when there is a need to get a dictionary | 37 class NET_EXPORT SdchManager { |
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 | |
72 //------------------------------------------------------------------------------ | |
73 | |
74 class NET_EXPORT SdchManager | |
75 : public SdchFetcher::Delegate, | |
76 public NON_EXPORTED_BASE(base::NonThreadSafe) { | |
77 public: | 38 public: |
78 // A list of errors that appeared and were either resolved, or used to turn | 39 // A list of errors that appeared and were either resolved, or used to turn |
79 // off sdch encoding. | 40 // off sdch encoding. |
80 enum ProblemCodes { | 41 enum ProblemCodes { |
81 MIN_PROBLEM_CODE, | 42 MIN_PROBLEM_CODE, |
82 | 43 |
83 // Content-encoding correction problems. | 44 // Content-encoding correction problems. |
84 ADDED_CONTENT_ENCODING = 1, | 45 ADDED_CONTENT_ENCODING = 1, |
85 FIXED_CONTENT_ENCODING = 2, | 46 FIXED_CONTENT_ENCODING = 2, |
86 FIXED_CONTENT_ENCODINGS = 3, | 47 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. | 206 // of the dictionary. The following are the known headers. |
246 const std::string domain_; | 207 const std::string domain_; |
247 const std::string path_; | 208 const std::string path_; |
248 const base::Time expiration_; // Implied by max-age. | 209 const base::Time expiration_; // Implied by max-age. |
249 const std::set<int> ports_; | 210 const std::set<int> ports_; |
250 | 211 |
251 DISALLOW_COPY_AND_ASSIGN(Dictionary); | 212 DISALLOW_COPY_AND_ASSIGN(Dictionary); |
252 }; | 213 }; |
253 | 214 |
254 SdchManager(); | 215 SdchManager(); |
255 ~SdchManager() override; | 216 virtual ~SdchManager(); |
mmenke
2014/11/06 15:30:12
There aren't any subclasses of this, are there?
Randy Smith (Not in Mondays)
2014/11/06 16:39:04
Not any more. Done.
| |
256 | 217 |
257 // Clear data (for browser data removal). | 218 // Clear data (for browser data removal). |
258 void ClearData(); | 219 void ClearData(); |
259 | 220 |
260 // Record stats on various errors. | 221 // Record stats on various errors. |
261 static void SdchErrorRecovery(ProblemCodes problem); | 222 static void SdchErrorRecovery(ProblemCodes problem); |
262 | 223 |
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. | 224 // Enables or disables SDCH compression. |
267 static void EnableSdchSupport(bool enabled); | 225 static void EnableSdchSupport(bool enabled); |
268 | 226 |
269 static bool sdch_enabled() { return g_sdch_enabled_; } | 227 static bool sdch_enabled() { return g_sdch_enabled_; } |
270 | 228 |
271 // Enables or disables SDCH compression over secure connection. | 229 // Enables or disables SDCH compression over secure connection. |
272 static void EnableSecureSchemeSupport(bool enabled); | 230 static void EnableSecureSchemeSupport(bool enabled); |
273 | 231 |
274 static bool secure_scheme_supported() { return g_secure_scheme_supported_; } | 232 static bool secure_scheme_supported() { return g_secure_scheme_supported_; } |
275 | 233 |
(...skipping 22 matching lines...) Expand all Loading... | |
298 | 256 |
299 // Unit test only: Indicate what current blacklist increment is for a domain. | 257 // Unit test only: Indicate what current blacklist increment is for a domain. |
300 int BlacklistDomainExponential(const std::string& domain); | 258 int BlacklistDomainExponential(const std::string& domain); |
301 | 259 |
302 // Check to see if SDCH is enabled (globally), and the given URL is in a | 260 // 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 | 261 // supported domain (i.e., not blacklisted, and either the specific supported |
304 // domain, or all domains were assumed supported). If it is blacklist, reduce | 262 // 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. | 263 // by 1 the number of times it will be reported as blacklisted. |
306 bool IsInSupportedDomain(const GURL& url); | 264 bool IsInSupportedDomain(const GURL& url); |
307 | 265 |
308 // Schedule the URL fetching to load a dictionary. This will always return | 266 // Send out appropriate events notifying observers that a Get-Dictionary |
309 // before the dictionary is actually loaded and added. | 267 // header has been seen. |
310 // After the implied task does completes, the dictionary will have been | 268 void OnGetDictionary(const GURL& request_url, const GURL& dictionary_url); |
mmenke
2014/11/06 15:30:12
The reason for an observer-type rather than using
Randy Smith (Not in Mondays)
2014/11/06 16:39:04
It's breaking dependencies. That's primarily for
| |
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 | 269 |
319 // Find the vcdiff dictionary (the body of the sdch dictionary that appears | 270 // 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| | 271 // 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 | 272 // 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 | 273 // be sure the returned |dictionary| can be used for decoding content supplied |
323 // in response to a request for |referring_url|. | 274 // in response to a request for |referring_url|. |
324 // Return null in |dictionary| if there is no matching legal dictionary. | 275 // Return null in |dictionary| if there is no matching legal dictionary. |
325 void GetVcdiffDictionary(const std::string& server_hash, | 276 void GetVcdiffDictionary(const std::string& server_hash, |
326 const GURL& referring_url, | 277 const GURL& referring_url, |
327 scoped_refptr<Dictionary>* dictionary); | 278 scoped_refptr<Dictionary>* dictionary); |
(...skipping 10 matching lines...) Expand all Loading... | |
338 std::string* client_hash, std::string* server_hash); | 289 std::string* client_hash, std::string* server_hash); |
339 | 290 |
340 // For Latency testing only, we need to know if we've succeeded in doing a | 291 // 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 | 292 // 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 | 293 // problems with SDCH, we opt-out of the test unless/until we perform a |
343 // complete SDCH decoding. | 294 // complete SDCH decoding. |
344 bool AllowLatencyExperiment(const GURL& url) const; | 295 bool AllowLatencyExperiment(const GURL& url) const; |
345 | 296 |
346 void SetAllowLatencyExperiment(const GURL& url, bool enable); | 297 void SetAllowLatencyExperiment(const GURL& url, bool enable); |
347 | 298 |
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 | 299 // Add an SDCH dictionary to our list of availible |
355 // dictionaries. This addition will fail if addition is illegal | 300 // dictionaries. This addition will fail if addition is illegal |
356 // (data in the dictionary is not acceptable from the | 301 // (data in the dictionary is not acceptable from the |
357 // dictionary_url; dictionary already added, etc.). | 302 // dictionary_url; dictionary already added, etc.). |
358 void AddSdchDictionary(const std::string& dictionary_text, | 303 void AddSdchDictionary(const std::string& dictionary_text, |
359 const GURL& dictionary_url) override; | 304 const GURL& dictionary_url); |
305 | |
306 // Registration for events generated by the SDCH subsystem. | |
307 virtual void AddObserver(SdchObserver* observer); | |
308 virtual void RemoveObserver(SdchObserver* observer); | |
360 | 309 |
361 private: | 310 private: |
362 struct BlacklistInfo { | 311 struct BlacklistInfo { |
363 BlacklistInfo() | 312 BlacklistInfo() |
364 : count(0), | 313 : count(0), |
365 exponential_count(0), | 314 exponential_count(0), |
366 reason(MIN_PROBLEM_CODE) {} | 315 reason(MIN_PROBLEM_CODE) {} |
367 | 316 |
368 int count; // # of times to refuse SDCH advertisement. | 317 int count; // # of times to refuse SDCH advertisement. |
369 int exponential_count; // Current exponential backoff ratchet. | 318 int exponential_count; // Current exponential backoff ratchet. |
370 ProblemCodes reason; // Why domain was blacklisted. | 319 ProblemCodes reason; // Why domain was blacklisted. |
371 | 320 |
372 }; | 321 }; |
373 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo; | 322 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo; |
374 typedef std::set<std::string> ExperimentSet; | 323 typedef std::set<std::string> ExperimentSet; |
375 | 324 |
325 // Determines whether a "Get-Dictionary" header is legal (dictionary | |
326 // url has appropriate relationship to referrer url) in the SDCH | |
327 // protocol. Return true if fetch is legal. | |
328 bool CanFetchDictionary(const GURL& referring_url, | |
329 const GURL& dictionary_url) const; | |
330 | |
376 // A map of dictionaries info indexed by the hash that the server provides. | 331 // A map of dictionaries info indexed by the hash that the server provides. |
377 typedef std::map<std::string, scoped_refptr<Dictionary> > DictionaryMap; | 332 typedef std::map<std::string, scoped_refptr<Dictionary> > DictionaryMap; |
378 | 333 |
379 // Support SDCH compression, by advertising in headers. | 334 // Support SDCH compression, by advertising in headers. |
380 static bool g_sdch_enabled_; | 335 static bool g_sdch_enabled_; |
381 | 336 |
382 // Support SDCH compression for HTTPS requests and responses. When supported, | 337 // Support SDCH compression for HTTPS requests and responses. When supported, |
383 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS. | 338 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS. |
384 static bool g_secure_scheme_supported_; | 339 static bool g_secure_scheme_supported_; |
385 | 340 |
386 // A simple implementation of a RFC 3548 "URL safe" base64 encoder. | 341 // A simple implementation of a RFC 3548 "URL safe" base64 encoder. |
387 static void UrlSafeBase64Encode(const std::string& input, | 342 static void UrlSafeBase64Encode(const std::string& input, |
388 std::string* output); | 343 std::string* output); |
389 DictionaryMap dictionaries_; | 344 DictionaryMap dictionaries_; |
390 | 345 |
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. | 346 // List domains where decode failures have required disabling sdch. |
395 DomainBlacklistInfo blacklisted_domains_; | 347 DomainBlacklistInfo blacklisted_domains_; |
396 | 348 |
397 // List of hostnames for which a latency experiment is allowed (because a | 349 // List of hostnames for which a latency experiment is allowed (because a |
398 // round trip test has recently passed). | 350 // round trip test has recently passed). |
399 ExperimentSet allow_latency_experiment_; | 351 ExperimentSet allow_latency_experiment_; |
400 | 352 |
401 int fetches_count_for_testing_; | 353 // Observers that want to be notified of SDCH events. |
354 // Assert list is empty on destruction since if there is an observer | |
355 // that hasn't removed itself from the list, that observer probably | |
356 // has a reference to the SdchManager. | |
357 ObserverList<SdchObserver, true> observers_; | |
358 | |
359 base::ThreadChecker thread_checker_; | |
402 | 360 |
403 DISALLOW_COPY_AND_ASSIGN(SdchManager); | 361 DISALLOW_COPY_AND_ASSIGN(SdchManager); |
404 }; | 362 }; |
405 | 363 |
406 } // namespace net | 364 } // namespace net |
407 | 365 |
408 #endif // NET_BASE_SDCH_MANAGER_H_ | 366 #endif // NET_BASE_SDCH_MANAGER_H_ |
OLD | NEW |