Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: net/base/sdch_manager.h

Issue 664263002: Restructure SDCH layering to allow more separation (observer/1->[0,n] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Results of self review. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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) {
Ryan Sleevi 2014/10/20 22:42:04 Any chance we can remove this inheritance as part
Randy Smith (Not in Mondays) 2014/10/21 19:05:36 I'm happy to, but I want to understand the rationa
Ryan Sleevi 2014/10/21 23:09:29 Because generally trait-inheritance requires concr
Randy Smith (Not in Mondays) 2014/10/23 20:57:07 Done.
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
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
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 10 matching lines...) Expand all
338 std::string* client_hash, std::string* server_hash); 296 std::string* client_hash, std::string* server_hash);
339 297
340 // For Latency testing only, we need to know if we've succeeded in doing a 298 // 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 299 // 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 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 // Register for events generated by the SDCH subsystem.
349 return fetches_count_for_testing_; 307 virtual void AddObserver(SdchObserver* observer);
350 } 308 virtual void RemoveObserver(SdchObserver* observer);
351
352 // Implementation of SdchFetcher::Delegate.
353 309
354 // Add an SDCH dictionary to our list of availible 310 // Add an SDCH dictionary to our list of availible
355 // dictionaries. This addition will fail if addition is illegal 311 // dictionaries. This addition will fail if addition is illegal
356 // (data in the dictionary is not acceptable from the 312 // (data in the dictionary is not acceptable from the
357 // dictionary_url; dictionary already added, etc.). 313 // dictionary_url; dictionary already added, etc.).
358 virtual void AddSdchDictionary(const std::string& dictionary_text, 314 virtual void AddSdchDictionary(const std::string& dictionary_text,
359 const GURL& dictionary_url) OVERRIDE; 315 const GURL& dictionary_url);
360 316
361 private: 317 private:
362 struct BlacklistInfo { 318 struct BlacklistInfo {
363 BlacklistInfo() 319 BlacklistInfo()
364 : count(0), 320 : count(0),
365 exponential_count(0), 321 exponential_count(0),
366 reason(MIN_PROBLEM_CODE) {} 322 reason(MIN_PROBLEM_CODE) {}
367 323
368 int count; // # of times to refuse SDCH advertisement. 324 int count; // # of times to refuse SDCH advertisement.
369 int exponential_count; // Current exponential backoff ratchet. 325 int exponential_count; // Current exponential backoff ratchet.
370 ProblemCodes reason; // Why domain was blacklisted. 326 ProblemCodes reason; // Why domain was blacklisted.
371 327
372 }; 328 };
373 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo; 329 typedef std::map<std::string, BlacklistInfo> DomainBlacklistInfo;
374 typedef std::set<std::string> ExperimentSet; 330 typedef std::set<std::string> ExperimentSet;
375 331
332 // Security test function used before initiating a FetchDictionary.
333 // Return true if fetch is legal.
Ryan Sleevi 2014/10/20 22:42:04 Documentation needs more. I have trouble understan
Randy Smith (Not in Mondays) 2014/10/21 19:05:36 Done, but note that the change in this CL is makin
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_;
402 364
403 DISALLOW_COPY_AND_ASSIGN(SdchManager); 365 DISALLOW_COPY_AND_ASSIGN(SdchManager);
404 }; 366 };
405 367
406 } // namespace net 368 } // namespace net
407 369
408 #endif // NET_BASE_SDCH_MANAGER_H_ 370 #endif // NET_BASE_SDCH_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698