| 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/observer_list.h" |
| 30 #include "base/threading/non_thread_safe.h" | 31 #include "base/threading/non_thread_safe.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; |
| 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 //------------------------------------------------------------------------------ |
| 73 | 42 |
| 74 class NET_EXPORT SdchManager | 43 class NET_EXPORT SdchManager |
| 75 : public SdchFetcher::Delegate, | 44 : public NON_EXPORTED_BASE(base::NonThreadSafe) { |
| 76 public NON_EXPORTED_BASE(base::NonThreadSafe) { | |
| 77 public: | 45 public: |
| 78 // A list of errors that appeared and were either resolved, or used to turn | 46 // A list of errors that appeared and were either resolved, or used to turn |
| 79 // off sdch encoding. | 47 // off sdch encoding. |
| 80 enum ProblemCodes { | 48 enum ProblemCodes { |
| 81 MIN_PROBLEM_CODE, | 49 MIN_PROBLEM_CODE, |
| 82 | 50 |
| 83 // Content-encoding correction problems. | 51 // Content-encoding correction problems. |
| 84 ADDED_CONTENT_ENCODING = 1, | 52 ADDED_CONTENT_ENCODING = 1, |
| 85 FIXED_CONTENT_ENCODING = 2, | 53 FIXED_CONTENT_ENCODING = 2, |
| 86 FIXED_CONTENT_ENCODINGS = 3, | 54 FIXED_CONTENT_ENCODINGS = 3, |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 | 221 |
| 254 SdchManager(); | 222 SdchManager(); |
| 255 virtual ~SdchManager(); | 223 virtual ~SdchManager(); |
| 256 | 224 |
| 257 // Clear data (for browser data removal). | 225 // Clear data (for browser data removal). |
| 258 void ClearData(); | 226 void ClearData(); |
| 259 | 227 |
| 260 // Record stats on various errors. | 228 // Record stats on various errors. |
| 261 static void SdchErrorRecovery(ProblemCodes problem); | 229 static void SdchErrorRecovery(ProblemCodes problem); |
| 262 | 230 |
| 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. | 231 // Enables or disables SDCH compression. |
| 267 static void EnableSdchSupport(bool enabled); | 232 static void EnableSdchSupport(bool enabled); |
| 268 | 233 |
| 269 static bool sdch_enabled() { return g_sdch_enabled_; } | 234 static bool sdch_enabled() { return g_sdch_enabled_; } |
| 270 | 235 |
| 271 // Enables or disables SDCH compression over secure connection. | 236 // Enables or disables SDCH compression over secure connection. |
| 272 static void EnableSecureSchemeSupport(bool enabled); | 237 static void EnableSecureSchemeSupport(bool enabled); |
| 273 | 238 |
| 274 static bool secure_scheme_supported() { return g_secure_scheme_supported_; } | 239 static bool secure_scheme_supported() { return g_secure_scheme_supported_; } |
| 275 | 240 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 298 | 263 |
| 299 // Unit test only: Indicate what current blacklist increment is for a domain. | 264 // Unit test only: Indicate what current blacklist increment is for a domain. |
| 300 int BlacklistDomainExponential(const std::string& domain); | 265 int BlacklistDomainExponential(const std::string& domain); |
| 301 | 266 |
| 302 // Check to see if SDCH is enabled (globally), and the given URL is in a | 267 // 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 | 268 // supported domain (i.e., not blacklisted, and either the specific supported |
| 304 // domain, or all domains were assumed supported). If it is blacklist, reduce | 269 // 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. | 270 // by 1 the number of times it will be reported as blacklisted. |
| 306 bool IsInSupportedDomain(const GURL& url); | 271 bool IsInSupportedDomain(const GURL& url); |
| 307 | 272 |
| 308 // Schedule the URL fetching to load a dictionary. This will always return | 273 // Send out appropriate events notifying observers that a Get-Dictionary |
| 309 // before the dictionary is actually loaded and added. | 274 // header has been seen. |
| 310 // After the implied task does completes, the dictionary will have been | 275 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 | 276 |
| 319 // Find the vcdiff dictionary (the body of the sdch dictionary that appears | 277 // 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| | 278 // 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 | 279 // 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 | 280 // be sure the returned |dictionary| can be used for decoding content supplied |
| 323 // in response to a request for |referring_url|. | 281 // in response to a request for |referring_url|. |
| 324 // Return null in |dictionary| if there is no matching legal dictionary. | 282 // Return null in |dictionary| if there is no matching legal dictionary. |
| 325 void GetVcdiffDictionary(const std::string& server_hash, | 283 void GetVcdiffDictionary(const std::string& server_hash, |
| 326 const GURL& referring_url, | 284 const GURL& referring_url, |
| 327 scoped_refptr<Dictionary>* dictionary); | 285 scoped_refptr<Dictionary>* dictionary); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 342 // problems with SDCH, we opt-out of the test unless/until we perform a | 300 // problems with SDCH, we opt-out of the test unless/until we perform a |
| 343 // complete SDCH decoding. | 301 // complete SDCH decoding. |
| 344 bool AllowLatencyExperiment(const GURL& url) const; | 302 bool AllowLatencyExperiment(const GURL& url) const; |
| 345 | 303 |
| 346 void SetAllowLatencyExperiment(const GURL& url, bool enable); | 304 void SetAllowLatencyExperiment(const GURL& url, bool enable); |
| 347 | 305 |
| 348 int GetFetchesCountForTesting() const { | 306 int GetFetchesCountForTesting() const { |
| 349 return fetches_count_for_testing_; | 307 return fetches_count_for_testing_; |
| 350 } | 308 } |
| 351 | 309 |
| 352 // Implementation of SdchFetcher::Delegate. | 310 // Register for events generated by the SDCH subsystem. |
| 311 virtual void AddObserver(SdchObserver* observer); |
| 312 virtual void RemoveObserver(SdchObserver* observer); |
| 353 | 313 |
| 354 // Add an SDCH dictionary to our list of availible | 314 // Add an SDCH dictionary to our list of availible |
| 355 // dictionaries. This addition will fail if addition is illegal | 315 // dictionaries. This addition will fail if addition is illegal |
| 356 // (data in the dictionary is not acceptable from the | 316 // (data in the dictionary is not acceptable from the |
| 357 // dictionary_url; dictionary already added, etc.). | 317 // dictionary_url; dictionary already added, etc.). |
| 358 virtual void AddSdchDictionary(const std::string& dictionary_text, | 318 virtual void AddSdchDictionary(const std::string& dictionary_text, |
| 359 const GURL& dictionary_url) OVERRIDE; | 319 const GURL& dictionary_url); |
| 360 | 320 |
| 361 private: | 321 private: |
| 362 struct BlacklistInfo { | 322 struct BlacklistInfo { |
| 363 BlacklistInfo() | 323 BlacklistInfo() |
| 364 : count(0), | 324 : count(0), |
| 365 exponential_count(0), | 325 exponential_count(0), |
| 366 reason(MIN_PROBLEM_CODE) {} | 326 reason(MIN_PROBLEM_CODE) {} |
| 367 | 327 |
| 368 int count; // # of times to refuse SDCH advertisement. | 328 int count; // # of times to refuse SDCH advertisement. |
| 369 int exponential_count; // Current exponential backoff ratchet. | 329 int exponential_count; // Current exponential backoff ratchet. |
| 370 ProblemCodes reason; // Why domain was blacklisted. | 330 ProblemCodes reason; // Why domain was blacklisted. |
| 371 | 331 |
| 372 }; | 332 }; |
| 373 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo; | 333 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo; |
| 374 typedef std::set<std::string> ExperimentSet; | 334 typedef std::set<std::string> ExperimentSet; |
| 375 | 335 |
| 336 // Security test function used before initiating a FetchDictionary. |
| 337 // Return true if fetch is legal. |
| 338 bool CanFetchDictionary(const GURL& referring_url, |
| 339 const GURL& dictionary_url) const; |
| 340 |
| 376 // A map of dictionaries info indexed by the hash that the server provides. | 341 // A map of dictionaries info indexed by the hash that the server provides. |
| 377 typedef std::map<std::string, scoped_refptr<Dictionary> > DictionaryMap; | 342 typedef std::map<std::string, scoped_refptr<Dictionary> > DictionaryMap; |
| 378 | 343 |
| 379 // Support SDCH compression, by advertising in headers. | 344 // Support SDCH compression, by advertising in headers. |
| 380 static bool g_sdch_enabled_; | 345 static bool g_sdch_enabled_; |
| 381 | 346 |
| 382 // Support SDCH compression for HTTPS requests and responses. When supported, | 347 // Support SDCH compression for HTTPS requests and responses. When supported, |
| 383 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS. | 348 // HTTPS applicable dictionaries MUST have been acquired securely via HTTPS. |
| 384 static bool g_secure_scheme_supported_; | 349 static bool g_secure_scheme_supported_; |
| 385 | 350 |
| 386 // A simple implementation of a RFC 3548 "URL safe" base64 encoder. | 351 // A simple implementation of a RFC 3548 "URL safe" base64 encoder. |
| 387 static void UrlSafeBase64Encode(const std::string& input, | 352 static void UrlSafeBase64Encode(const std::string& input, |
| 388 std::string* output); | 353 std::string* output); |
| 389 DictionaryMap dictionaries_; | 354 DictionaryMap dictionaries_; |
| 390 | 355 |
| 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. | 356 // List domains where decode failures have required disabling sdch. |
| 395 DomainBlacklistInfo blacklisted_domains_; | 357 DomainBlacklistInfo blacklisted_domains_; |
| 396 | 358 |
| 397 // List of hostnames for which a latency experiment is allowed (because a | 359 // List of hostnames for which a latency experiment is allowed (because a |
| 398 // round trip test has recently passed). | 360 // round trip test has recently passed). |
| 399 ExperimentSet allow_latency_experiment_; | 361 ExperimentSet allow_latency_experiment_; |
| 400 | 362 |
| 401 int fetches_count_for_testing_; | 363 int fetches_count_for_testing_; |
| 402 | 364 |
| 365 // Observers that want to be notified of SDCH events. |
| 366 // Assert list is empty on destruction since if there is an observer |
| 367 // that hasn't removed itself from the list, that observer probably |
| 368 // has a reference to the SdchManager. |
| 369 ObserverList<SdchObserver, true> observers_; |
| 370 |
| 403 DISALLOW_COPY_AND_ASSIGN(SdchManager); | 371 DISALLOW_COPY_AND_ASSIGN(SdchManager); |
| 404 }; | 372 }; |
| 405 | 373 |
| 406 } // namespace net | 374 } // namespace net |
| 407 | 375 |
| 408 #endif // NET_BASE_SDCH_MANAGER_H_ | 376 #endif // NET_BASE_SDCH_MANAGER_H_ |
| OLD | NEW |