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