OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "net/base/sdch_manager.h" | 5 #include "net/base/sdch_manager.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "crypto/sha2.h" | 12 #include "crypto/sha2.h" |
13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 14 #include "net/base/sdch_observer.h" |
14 #include "net/url_request/url_request_http_job.h" | 15 #include "net/url_request/url_request_http_job.h" |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 void StripTrailingDot(GURL* gurl) { | 19 void StripTrailingDot(GURL* gurl) { |
19 std::string host(gurl->host()); | 20 std::string host(gurl->host()); |
20 | 21 |
21 if (host.empty()) | 22 if (host.empty()) |
22 return; | 23 return; |
23 | 24 |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 DCHECK(CalledOnValidThread()); | 251 DCHECK(CalledOnValidThread()); |
251 while (!dictionaries_.empty()) { | 252 while (!dictionaries_.empty()) { |
252 DictionaryMap::iterator it = dictionaries_.begin(); | 253 DictionaryMap::iterator it = dictionaries_.begin(); |
253 dictionaries_.erase(it->first); | 254 dictionaries_.erase(it->first); |
254 } | 255 } |
255 } | 256 } |
256 | 257 |
257 void SdchManager::ClearData() { | 258 void SdchManager::ClearData() { |
258 blacklisted_domains_.clear(); | 259 blacklisted_domains_.clear(); |
259 allow_latency_experiment_.clear(); | 260 allow_latency_experiment_.clear(); |
260 if (fetcher_.get()) | |
261 fetcher_->Cancel(); | |
262 | 261 |
263 // Note that this may result in not having dictionaries we've advertised | 262 // Note that this may result in not having dictionaries we've advertised |
264 // for incoming responses. The window is relatively small (as ClearData() | 263 // for incoming responses. The window is relatively small (as ClearData() |
265 // is not expected to be called frequently), so we rely on meta-refresh | 264 // is not expected to be called frequently), so we rely on meta-refresh |
266 // to handle this case. | 265 // to handle this case. |
267 dictionaries_.clear(); | 266 dictionaries_.clear(); |
| 267 |
| 268 FOR_EACH_OBSERVER(SdchObserver, observers_, OnClearDictionaries(this)); |
268 } | 269 } |
269 | 270 |
270 // static | 271 // static |
271 void SdchManager::SdchErrorRecovery(ProblemCodes problem) { | 272 void SdchManager::SdchErrorRecovery(ProblemCodes problem) { |
272 UMA_HISTOGRAM_ENUMERATION("Sdch3.ProblemCodes_4", problem, MAX_PROBLEM_CODE); | 273 UMA_HISTOGRAM_ENUMERATION("Sdch3.ProblemCodes_4", problem, MAX_PROBLEM_CODE); |
273 } | 274 } |
274 | 275 |
275 void SdchManager::set_sdch_fetcher(scoped_ptr<SdchFetcher> fetcher) { | |
276 DCHECK(CalledOnValidThread()); | |
277 fetcher_ = fetcher.Pass(); | |
278 } | |
279 | |
280 // static | 276 // static |
281 void SdchManager::EnableSdchSupport(bool enabled) { | 277 void SdchManager::EnableSdchSupport(bool enabled) { |
282 g_sdch_enabled_ = enabled; | 278 g_sdch_enabled_ = enabled; |
283 } | 279 } |
284 | 280 |
285 // static | 281 // static |
286 void SdchManager::EnableSecureSchemeSupport(bool enabled) { | 282 void SdchManager::EnableSecureSchemeSupport(bool enabled) { |
287 g_secure_scheme_supported_ = enabled; | 283 g_secure_scheme_supported_ = enabled; |
288 } | 284 } |
289 | 285 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 if (count > 0) { | 366 if (count > 0) { |
371 it->second.count = count; | 367 it->second.count = count; |
372 } else { | 368 } else { |
373 it->second.count = 0; | 369 it->second.count = 0; |
374 it->second.reason = MIN_PROBLEM_CODE; | 370 it->second.reason = MIN_PROBLEM_CODE; |
375 } | 371 } |
376 | 372 |
377 return false; | 373 return false; |
378 } | 374 } |
379 | 375 |
380 void SdchManager::FetchDictionary(const GURL& request_url, | 376 void SdchManager::OnGetDictionary(const GURL& request_url, |
381 const GURL& dictionary_url) { | 377 const GURL& dictionary_url) { |
382 DCHECK(CalledOnValidThread()); | 378 if (!CanFetchDictionary(request_url, dictionary_url)) |
383 if (CanFetchDictionary(request_url, dictionary_url) && fetcher_.get()) { | 379 return; |
384 ++fetches_count_for_testing_; | 380 |
385 fetcher_->Schedule(dictionary_url); | 381 FOR_EACH_OBSERVER(SdchObserver, observers_, |
386 } | 382 OnGetDictionary(this, request_url, dictionary_url)); |
387 } | 383 } |
388 | 384 |
389 bool SdchManager::CanFetchDictionary(const GURL& referring_url, | 385 bool SdchManager::CanFetchDictionary(const GURL& referring_url, |
390 const GURL& dictionary_url) const { | 386 const GURL& dictionary_url) const { |
391 DCHECK(CalledOnValidThread()); | 387 DCHECK(CalledOnValidThread()); |
392 /* The user agent may retrieve a dictionary from the dictionary URL if all of | 388 /* The user agent may retrieve a dictionary from the dictionary URL if all of |
393 the following are true: | 389 the following are true: |
394 1 The dictionary URL host name matches the referrer URL host name and | 390 1 The dictionary URL host name matches the referrer URL host name and |
395 scheme. | 391 scheme. |
396 2 The dictionary URL host name domain matches the parent domain of the | 392 2 The dictionary URL host name domain matches the parent domain of the |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 allow_latency_experiment_.insert(url.host()); | 488 allow_latency_experiment_.insert(url.host()); |
493 return; | 489 return; |
494 } | 490 } |
495 ExperimentSet::iterator it = allow_latency_experiment_.find(url.host()); | 491 ExperimentSet::iterator it = allow_latency_experiment_.find(url.host()); |
496 if (allow_latency_experiment_.end() == it) | 492 if (allow_latency_experiment_.end() == it) |
497 return; // It was already erased, or never allowed. | 493 return; // It was already erased, or never allowed. |
498 SdchErrorRecovery(LATENCY_TEST_DISALLOWED); | 494 SdchErrorRecovery(LATENCY_TEST_DISALLOWED); |
499 allow_latency_experiment_.erase(it); | 495 allow_latency_experiment_.erase(it); |
500 } | 496 } |
501 | 497 |
| 498 void SdchManager::AddObserver(SdchObserver* observer) { |
| 499 observers_.AddObserver(observer); |
| 500 observer->AddRef(); |
| 501 } |
| 502 |
| 503 void SdchManager::RemoveObserver(SdchObserver* observer) { |
| 504 observers_.RemoveObserver(observer); |
| 505 observer->RemoveRef(); |
| 506 } |
| 507 |
502 void SdchManager::AddSdchDictionary(const std::string& dictionary_text, | 508 void SdchManager::AddSdchDictionary(const std::string& dictionary_text, |
503 const GURL& dictionary_url) { | 509 const GURL& dictionary_url) { |
504 DCHECK(CalledOnValidThread()); | 510 DCHECK(CalledOnValidThread()); |
505 std::string client_hash; | 511 std::string client_hash; |
506 std::string server_hash; | 512 std::string server_hash; |
507 GenerateHash(dictionary_text, &client_hash, &server_hash); | 513 GenerateHash(dictionary_text, &client_hash, &server_hash); |
508 if (dictionaries_.find(server_hash) != dictionaries_.end()) { | 514 if (dictionaries_.find(server_hash) != dictionaries_.end()) { |
509 SdchErrorRecovery(DICTIONARY_ALREADY_LOADED); | 515 SdchErrorRecovery(DICTIONARY_ALREADY_LOADED); |
510 return; // Already loaded. | 516 return; // Already loaded. |
511 } | 517 } |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 void SdchManager::UrlSafeBase64Encode(const std::string& input, | 615 void SdchManager::UrlSafeBase64Encode(const std::string& input, |
610 std::string* output) { | 616 std::string* output) { |
611 // Since this is only done during a dictionary load, and hashes are only 8 | 617 // Since this is only done during a dictionary load, and hashes are only 8 |
612 // characters, we just do the simple fixup, rather than rewriting the encoder. | 618 // characters, we just do the simple fixup, rather than rewriting the encoder. |
613 base::Base64Encode(input, output); | 619 base::Base64Encode(input, output); |
614 std::replace(output->begin(), output->end(), '+', '-'); | 620 std::replace(output->begin(), output->end(), '+', '-'); |
615 std::replace(output->begin(), output->end(), '/', '_'); | 621 std::replace(output->begin(), output->end(), '/', '_'); |
616 } | 622 } |
617 | 623 |
618 } // namespace net | 624 } // namespace net |
OLD | NEW |