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/threading/non_thread_safe.h" |
31 #include "base/time/time.h" | 31 #include "base/time/time.h" |
32 #include "net/base/net_export.h" | 32 #include "net/base/net_export.h" |
33 #include "url/gurl.h" | 33 #include "url/gurl.h" |
34 | 34 |
35 namespace net { | 35 namespace net { |
36 | 36 |
37 //------------------------------------------------------------------------------ | 37 //------------------------------------------------------------------------------ |
38 // Create a public interface to help us load SDCH dictionaries. | 38 // Create a public interface to help us load SDCH dictionaries. |
39 // The SdchManager class allows registration to support this interface. | 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 | 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 | 41 // get data from a specified URL. This allows us to use very high level browser |
42 // functionality in this base (when the functionaity can be provided). | 42 // functionality in this base (when the functionality can be provided). |
43 class NET_EXPORT SdchFetcher { | 43 class NET_EXPORT SdchFetcher { |
44 public: | 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 |
45 SdchFetcher() {} | 56 SdchFetcher() {} |
46 virtual ~SdchFetcher() {} | 57 virtual ~SdchFetcher() {} |
47 | 58 |
48 // The Schedule() method is called when there is a need to get a dictionary | 59 // The Schedule() method is called when there is a need to get a dictionary |
49 // from a server. The callee is responsible for getting that dictionary_text, | 60 // from a server. The callee is responsible for getting that dictionary_text, |
50 // and then calling back to AddSdchDictionary() to the SdchManager instance. | 61 // and then calling back to AddSdchDictionary() in the Delegate instance. |
51 virtual void Schedule(const GURL& dictionary_url) = 0; | 62 virtual void Schedule(const GURL& dictionary_url) = 0; |
52 | 63 |
53 // The Cancel() method is called to cancel all pending dictionary fetches. | 64 // The Cancel() method is called to cancel all pending dictionary fetches. |
54 // This is used for implementation of ClearData() below. | 65 // This is used for implementation of ClearData() below. |
55 virtual void Cancel() = 0; | 66 virtual void Cancel() = 0; |
56 | 67 |
57 private: | 68 private: |
58 DISALLOW_COPY_AND_ASSIGN(SdchFetcher); | 69 DISALLOW_COPY_AND_ASSIGN(SdchFetcher); |
59 }; | 70 }; |
60 | 71 |
61 //------------------------------------------------------------------------------ | 72 //------------------------------------------------------------------------------ |
62 | 73 |
63 class NET_EXPORT SdchManager : public NON_EXPORTED_BASE(base::NonThreadSafe) { | 74 class NET_EXPORT SdchManager |
| 75 : public SdchFetcher::Delegate, |
| 76 public NON_EXPORTED_BASE(base::NonThreadSafe) { |
64 public: | 77 public: |
65 // A list of errors that appeared and were either resolved, or used to turn | 78 // A list of errors that appeared and were either resolved, or used to turn |
66 // off sdch encoding. | 79 // off sdch encoding. |
67 enum ProblemCodes { | 80 enum ProblemCodes { |
68 MIN_PROBLEM_CODE, | 81 MIN_PROBLEM_CODE, |
69 | 82 |
70 // Content-encoding correction problems. | 83 // Content-encoding correction problems. |
71 ADDED_CONTENT_ENCODING = 1, | 84 ADDED_CONTENT_ENCODING = 1, |
72 FIXED_CONTENT_ENCODING = 2, | 85 FIXED_CONTENT_ENCODING = 2, |
73 FIXED_CONTENT_ENCODINGS = 3, | 86 FIXED_CONTENT_ENCODINGS = 3, |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 public: | 184 public: |
172 // Sdch filters can get our text to use in decoding compressed data. | 185 // Sdch filters can get our text to use in decoding compressed data. |
173 const std::string& text() const { return text_; } | 186 const std::string& text() const { return text_; } |
174 | 187 |
175 private: | 188 private: |
176 friend class base::RefCounted<Dictionary>; | 189 friend class base::RefCounted<Dictionary>; |
177 friend class SdchManager; // Only manager can construct an instance. | 190 friend class SdchManager; // Only manager can construct an instance. |
178 FRIEND_TEST_ALL_PREFIXES(SdchManagerTest, PathMatch); | 191 FRIEND_TEST_ALL_PREFIXES(SdchManagerTest, PathMatch); |
179 | 192 |
180 // Construct a vc-diff usable dictionary from the dictionary_text starting | 193 // Construct a vc-diff usable dictionary from the dictionary_text starting |
181 // at the given offset. The supplied client_hash should be used to | 194 // at the given offset. The supplied client_hash should be used to |
182 // advertise the dictionary's availability relative to the suppplied URL. | 195 // advertise the dictionary's availability relative to the suppplied URL. |
183 Dictionary(const std::string& dictionary_text, | 196 Dictionary(const std::string& dictionary_text, |
184 size_t offset, | 197 size_t offset, |
185 const std::string& client_hash, | 198 const std::string& client_hash, |
186 const GURL& url, | 199 const GURL& url, |
187 const std::string& domain, | 200 const std::string& domain, |
188 const std::string& path, | 201 const std::string& path, |
189 const base::Time& expiration, | 202 const base::Time& expiration, |
190 const std::set<int>& ports); | 203 const std::set<int>& ports); |
191 ~Dictionary(); | 204 virtual ~Dictionary(); |
192 | 205 |
193 const GURL& url() const { return url_; } | 206 const GURL& url() const { return url_; } |
194 const std::string& client_hash() const { return client_hash_; } | 207 const std::string& client_hash() const { return client_hash_; } |
195 | 208 |
196 // Security method to check if we can advertise this dictionary for use | 209 // Security method to check if we can advertise this dictionary for use |
197 // if the |target_url| returns SDCH compressed data. | 210 // if the |target_url| returns SDCH compressed data. |
198 bool CanAdvertise(const GURL& target_url); | 211 bool CanAdvertise(const GURL& target_url); |
199 | 212 |
200 // Security methods to check if we can establish a new dictionary with the | 213 // Security methods to check if we can establish a new dictionary with the |
201 // given data, that arrived in response to get of dictionary_url. | 214 // given data, that arrived in response to get of dictionary_url. |
(...skipping 18 matching lines...) Expand all Loading... |
220 // Part of the hash of text_ that the client uses to advertise the fact that | 233 // Part of the hash of text_ that the client uses to advertise the fact that |
221 // it has a specific dictionary pre-cached. | 234 // it has a specific dictionary pre-cached. |
222 std::string client_hash_; | 235 std::string client_hash_; |
223 | 236 |
224 // The GURL that arrived with the text_ in a URL request to specify where | 237 // The GURL that arrived with the text_ in a URL request to specify where |
225 // this dictionary may be used. | 238 // this dictionary may be used. |
226 const GURL url_; | 239 const GURL url_; |
227 | 240 |
228 // Metadate "headers" in before dictionary text contained the following: | 241 // Metadate "headers" in before dictionary text contained the following: |
229 // Each dictionary payload consists of several headers, followed by the text | 242 // Each dictionary payload consists of several headers, followed by the text |
230 // of the dictionary. The following are the known headers. | 243 // of the dictionary. The following are the known headers. |
231 const std::string domain_; | 244 const std::string domain_; |
232 const std::string path_; | 245 const std::string path_; |
233 const base::Time expiration_; // Implied by max-age. | 246 const base::Time expiration_; // Implied by max-age. |
234 const std::set<int> ports_; | 247 const std::set<int> ports_; |
235 | 248 |
236 DISALLOW_COPY_AND_ASSIGN(Dictionary); | 249 DISALLOW_COPY_AND_ASSIGN(Dictionary); |
237 }; | 250 }; |
238 | 251 |
239 SdchManager(); | 252 SdchManager(); |
240 ~SdchManager(); | 253 virtual ~SdchManager(); |
241 | 254 |
242 // Clear data (for browser data removal). | 255 // Clear data (for browser data removal). |
243 void ClearData(); | 256 void ClearData(); |
244 | 257 |
245 // Record stats on various errors. | 258 // Record stats on various errors. |
246 static void SdchErrorRecovery(ProblemCodes problem); | 259 static void SdchErrorRecovery(ProblemCodes problem); |
247 | 260 |
248 // Register a fetcher that this class can use to obtain dictionaries. | 261 // Register a fetcher that this class can use to obtain dictionaries. |
249 void set_sdch_fetcher(scoped_ptr<SdchFetcher> fetcher); | 262 void set_sdch_fetcher(scoped_ptr<SdchFetcher> fetcher); |
250 | 263 |
251 // Enables or disables SDCH compression. | 264 // Enables or disables SDCH compression. |
252 static void EnableSdchSupport(bool enabled); | 265 static void EnableSdchSupport(bool enabled); |
253 | 266 |
254 static bool sdch_enabled() { return g_sdch_enabled_; } | 267 static bool sdch_enabled() { return g_sdch_enabled_; } |
255 | 268 |
256 // Enables or disables SDCH compression over secure connection. | 269 // Enables or disables SDCH compression over secure connection. |
257 static void EnableSecureSchemeSupport(bool enabled); | 270 static void EnableSecureSchemeSupport(bool enabled); |
258 | 271 |
259 static bool secure_scheme_supported() { return g_secure_scheme_supported_; } | 272 static bool secure_scheme_supported() { return g_secure_scheme_supported_; } |
260 | 273 |
261 // Briefly prevent further advertising of SDCH on this domain (if SDCH is | 274 // Briefly prevent further advertising of SDCH on this domain (if SDCH is |
262 // enabled). After enough calls to IsInSupportedDomain() the blacklisting | 275 // enabled). After enough calls to IsInSupportedDomain() the blacklisting |
263 // will be removed. Additional blacklists take exponentially more calls | 276 // will be removed. Additional blacklists take exponentially more calls |
264 // to IsInSupportedDomain() before the blacklisting is undone. | 277 // to IsInSupportedDomain() before the blacklisting is undone. |
265 // Used when filter errors are found from a given domain, but it is plausible | 278 // Used when filter errors are found from a given domain, but it is plausible |
266 // that the cause is temporary (such as application startup, where cached | 279 // that the cause is temporary (such as application startup, where cached |
267 // entries are used, but a dictionary is not yet loaded). | 280 // entries are used, but a dictionary is not yet loaded). |
268 void BlacklistDomain(const GURL& url, ProblemCodes blacklist_reason); | 281 void BlacklistDomain(const GURL& url, ProblemCodes blacklist_reason); |
269 | 282 |
270 // Used when SEVERE filter errors are found from a given domain, to prevent | 283 // Used when SEVERE filter errors are found from a given domain, to prevent |
271 // further use of SDCH on that domain. | 284 // further use of SDCH on that domain. |
272 void BlacklistDomainForever(const GURL& url, ProblemCodes blacklist_reason); | 285 void BlacklistDomainForever(const GURL& url, ProblemCodes blacklist_reason); |
273 | 286 |
274 // Unit test only, this function resets enabling of sdch, and clears the | 287 // Unit test only, this function resets enabling of sdch, and clears the |
275 // blacklist. | 288 // blacklist. |
276 void ClearBlacklistings(); | 289 void ClearBlacklistings(); |
277 | 290 |
278 // Unit test only, this function resets the blacklisting count for a domain. | 291 // Unit test only, this function resets the blacklisting count for a domain. |
279 void ClearDomainBlacklisting(const std::string& domain); | 292 void ClearDomainBlacklisting(const std::string& domain); |
280 | 293 |
281 // Unit test only: indicate how many more times a domain will be blacklisted. | 294 // Unit test only: indicate how many more times a domain will be blacklisted. |
282 int BlackListDomainCount(const std::string& domain); | 295 int BlackListDomainCount(const std::string& domain); |
283 | 296 |
284 // Unit test only: Indicate what current blacklist increment is for a domain. | 297 // Unit test only: Indicate what current blacklist increment is for a domain. |
285 int BlacklistDomainExponential(const std::string& domain); | 298 int BlacklistDomainExponential(const std::string& domain); |
286 | 299 |
287 // Check to see if SDCH is enabled (globally), and the given URL is in a | 300 // Check to see if SDCH is enabled (globally), and the given URL is in a |
288 // supported domain (i.e., not blacklisted, and either the specific supported | 301 // supported domain (i.e., not blacklisted, and either the specific supported |
289 // domain, or all domains were assumed supported). If it is blacklist, reduce | 302 // domain, or all domains were assumed supported). If it is blacklist, reduce |
290 // by 1 the number of times it will be reported as blacklisted. | 303 // by 1 the number of times it will be reported as blacklisted. |
291 bool IsInSupportedDomain(const GURL& url); | 304 bool IsInSupportedDomain(const GURL& url); |
292 | 305 |
293 // Schedule the URL fetching to load a dictionary. This will always return | 306 // Schedule the URL fetching to load a dictionary. This will always return |
294 // before the dictionary is actually loaded and added. | 307 // before the dictionary is actually loaded and added. |
295 // After the implied task does completes, the dictionary will have been | 308 // After the implied task does completes, the dictionary will have been |
296 // cached in memory. | 309 // cached in memory. |
297 void FetchDictionary(const GURL& request_url, const GURL& dictionary_url); | 310 void FetchDictionary(const GURL& request_url, const GURL& dictionary_url); |
298 | 311 |
299 // Security test function used before initiating a FetchDictionary. | 312 // Security test function used before initiating a FetchDictionary. |
300 // Return true if fetch is legal. | 313 // Return true if fetch is legal. |
301 bool CanFetchDictionary(const GURL& referring_url, | 314 bool CanFetchDictionary(const GURL& referring_url, |
302 const GURL& dictionary_url) const; | 315 const GURL& dictionary_url) const; |
303 | 316 |
304 // Add an SDCH dictionary to our list of availible dictionaries. This addition | |
305 // will fail (return false) if addition is illegal (data in the dictionary is | |
306 // not acceptable from the dictionary_url; dictionary already added, etc.). | |
307 bool AddSdchDictionary(const std::string& dictionary_text, | |
308 const GURL& dictionary_url); | |
309 | |
310 // Find the vcdiff dictionary (the body of the sdch dictionary that appears | 317 // Find the vcdiff dictionary (the body of the sdch dictionary that appears |
311 // after the meta-data headers like Domain:...) with the given |server_hash| | 318 // after the meta-data headers like Domain:...) with the given |server_hash| |
312 // to use to decompreses data that arrived as SDCH encoded content. Check to | 319 // to use to decompreses data that arrived as SDCH encoded content. Check to |
313 // be sure the returned |dictionary| can be used for decoding content supplied | 320 // be sure the returned |dictionary| can be used for decoding content supplied |
314 // in response to a request for |referring_url|. | 321 // in response to a request for |referring_url|. |
315 // Return null in |dictionary| if there is no matching legal dictionary. | 322 // Return null in |dictionary| if there is no matching legal dictionary. |
316 void GetVcdiffDictionary(const std::string& server_hash, | 323 void GetVcdiffDictionary(const std::string& server_hash, |
317 const GURL& referring_url, | 324 const GURL& referring_url, |
318 scoped_refptr<Dictionary>* dictionary); | 325 scoped_refptr<Dictionary>* dictionary); |
319 | 326 |
320 // Get list of available (pre-cached) dictionaries that we have already loaded | 327 // Get list of available (pre-cached) dictionaries that we have already loaded |
321 // into memory. The list is a comma separated list of (client) hashes per | 328 // into memory. The list is a comma separated list of (client) hashes per |
322 // the SDCH spec. | 329 // the SDCH spec. |
323 void GetAvailDictionaryList(const GURL& target_url, std::string* list); | 330 void GetAvailDictionaryList(const GURL& target_url, std::string* list); |
324 | 331 |
325 // Construct the pair of hashes for client and server to identify an SDCH | 332 // Construct the pair of hashes for client and server to identify an SDCH |
326 // dictionary. This is only made public to facilitate unit testing, but is | 333 // dictionary. This is only made public to facilitate unit testing, but is |
327 // otherwise private | 334 // otherwise private |
328 static void GenerateHash(const std::string& dictionary_text, | 335 static void GenerateHash(const std::string& dictionary_text, |
329 std::string* client_hash, std::string* server_hash); | 336 std::string* client_hash, std::string* server_hash); |
330 | 337 |
331 // For Latency testing only, we need to know if we've succeeded in doing a | 338 // For Latency testing only, we need to know if we've succeeded in doing a |
332 // round trip before starting our comparative tests. If ever we encounter | 339 // round trip before starting our comparative tests. If ever we encounter |
333 // problems with SDCH, we opt-out of the test unless/until we perform a | 340 // problems with SDCH, we opt-out of the test unless/until we perform a |
334 // complete SDCH decoding. | 341 // complete SDCH decoding. |
335 bool AllowLatencyExperiment(const GURL& url) const; | 342 bool AllowLatencyExperiment(const GURL& url) const; |
336 | 343 |
337 void SetAllowLatencyExperiment(const GURL& url, bool enable); | 344 void SetAllowLatencyExperiment(const GURL& url, bool enable); |
338 | 345 |
339 int GetFetchesCountForTesting() const { | 346 int GetFetchesCountForTesting() const { |
340 return fetches_count_for_testing_; | 347 return fetches_count_for_testing_; |
341 } | 348 } |
342 | 349 |
| 350 // Implementation of SdchFetcher::Delegate. |
| 351 |
| 352 // Add an SDCH dictionary to our list of availible |
| 353 // dictionaries. This addition will fail if addition is illegal |
| 354 // (data in the dictionary is not acceptable from the |
| 355 // dictionary_url; dictionary already added, etc.). |
| 356 virtual void AddSdchDictionary(const std::string& dictionary_text, |
| 357 const GURL& dictionary_url) OVERRIDE; |
| 358 |
343 private: | 359 private: |
344 struct BlacklistInfo { | 360 struct BlacklistInfo { |
345 BlacklistInfo() | 361 BlacklistInfo() |
346 : count(0), | 362 : count(0), |
347 exponential_count(0), | 363 exponential_count(0), |
348 reason(MIN_PROBLEM_CODE) {} | 364 reason(MIN_PROBLEM_CODE) {} |
349 | 365 |
350 int count; // # of times to refuse SDCH advertisement. | 366 int count; // # of times to refuse SDCH advertisement. |
351 int exponential_count; // Current exponential backoff ratchet. | 367 int exponential_count; // Current exponential backoff ratchet. |
352 ProblemCodes reason; // Why domain was blacklisted. | 368 ProblemCodes reason; // Why domain was blacklisted. |
(...skipping 28 matching lines...) Expand all Loading... |
381 ExperimentSet allow_latency_experiment_; | 397 ExperimentSet allow_latency_experiment_; |
382 | 398 |
383 int fetches_count_for_testing_; | 399 int fetches_count_for_testing_; |
384 | 400 |
385 DISALLOW_COPY_AND_ASSIGN(SdchManager); | 401 DISALLOW_COPY_AND_ASSIGN(SdchManager); |
386 }; | 402 }; |
387 | 403 |
388 } // namespace net | 404 } // namespace net |
389 | 405 |
390 #endif // NET_BASE_SDCH_MANAGER_H_ | 406 #endif // NET_BASE_SDCH_MANAGER_H_ |
OLD | NEW |