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

Side by Side Diff: chrome/browser/net/dns_global.cc

Issue 523076: Clear the host cache when closing the last incognito window.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Address jar's comments Created 10 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "chrome/browser/net/dns_global.h" 5 #include "chrome/browser/net/dns_global.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 16 matching lines...) Expand all
27 #include "chrome/common/chrome_switches.h" 27 #include "chrome/common/chrome_switches.h"
28 #include "net/base/fixed_host_resolver.h" 28 #include "net/base/fixed_host_resolver.h"
29 #include "net/base/host_resolver.h" 29 #include "net/base/host_resolver.h"
30 #include "net/base/host_resolver_impl.h" 30 #include "net/base/host_resolver_impl.h"
31 31
32 using base::Time; 32 using base::Time;
33 using base::TimeDelta; 33 using base::TimeDelta;
34 34
35 namespace chrome_browser_net { 35 namespace chrome_browser_net {
36 36
37 static void DiscardAllPrefetchState(); 37 static void ChangedToOnTheRecord();
38 static void DnsMotivatedPrefetch(const std::string& hostname, 38 static void DnsMotivatedPrefetch(const std::string& hostname,
39 DnsHostInfo::ResolutionMotivation motivation); 39 DnsHostInfo::ResolutionMotivation motivation);
40 static void DnsPrefetchMotivatedList( 40 static void DnsPrefetchMotivatedList(
41 const NameList& hostnames, 41 const NameList& hostnames,
42 DnsHostInfo::ResolutionMotivation motivation); 42 DnsHostInfo::ResolutionMotivation motivation);
43 43
44 // static 44 // static
45 const size_t DnsPrefetcherInit::kMaxConcurrentLookups = 8; 45 const size_t DnsGlobalInit::kMaxPrefetchConcurrentLookups = 8;
46 46
47 // static 47 // static
48 const int DnsPrefetcherInit::kMaxQueueingDelayMs = 1000; 48 const int DnsGlobalInit::kMaxPrefetchQueueingDelayMs = 1000;
49 49
50 // Host resolver shared by DNS prefetcher, and the main URLRequestContext. 50 // Host resolver shared by DNS prefetcher, and the main URLRequestContext.
51 static net::HostResolver* global_host_resolver = NULL; 51 static net::HostResolver* global_host_resolver = NULL;
52 52
53 //------------------------------------------------------------------------------ 53 //------------------------------------------------------------------------------
54 // This section contains all the globally accessable API entry points for the 54 // This section contains all the globally accessable API entry points for the
55 // DNS Prefetching feature. 55 // DNS Prefetching feature.
56 //------------------------------------------------------------------------------ 56 //------------------------------------------------------------------------------
57 57
58 // Status of prefetch feature, controlling whether any prefetching is done. 58 // Status of prefetch feature, controlling whether any prefetching is done.
59 static bool dns_prefetch_enabled = true; 59 static bool dns_prefetch_enabled = true;
60 60
61 // Cached inverted copy of the off_the_record pref. 61 // Cached inverted copy of the off_the_record pref.
62 static bool on_the_record_switch = true; 62 static bool on_the_record_switch = true;
63 63
64 // Enable/disable Dns prefetch activity (either via command line, or via pref). 64 // Enable/disable Dns prefetch activity (either via command line, or via pref).
65 void EnableDnsPrefetch(bool enable) { 65 void EnableDnsPrefetch(bool enable) {
66 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 66 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
67 dns_prefetch_enabled = enable; 67 dns_prefetch_enabled = enable;
68 } 68 }
69 69
70 void OnTheRecord(bool enable) { 70 void OnTheRecord(bool enable) {
71 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 71 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
72 if (on_the_record_switch == enable) 72 if (on_the_record_switch == enable)
73 return; 73 return;
74 on_the_record_switch = enable; 74 on_the_record_switch = enable;
75 if (on_the_record_switch) 75 if (on_the_record_switch) {
76 DiscardAllPrefetchState(); // Destroy all evidence of our OTR session. 76 ChromeThread::PostTask(
77 ChromeThread::IO,
78 FROM_HERE,
79 NewRunnableFunction(ChangedToOnTheRecord));
80 }
77 } 81 }
78 82
79 void RegisterPrefs(PrefService* local_state) { 83 void RegisterPrefs(PrefService* local_state) {
80 local_state->RegisterListPref(prefs::kDnsStartupPrefetchList); 84 local_state->RegisterListPref(prefs::kDnsStartupPrefetchList);
81 local_state->RegisterListPref(prefs::kDnsHostReferralList); 85 local_state->RegisterListPref(prefs::kDnsHostReferralList);
82 } 86 }
83 87
84 void RegisterUserPrefs(PrefService* user_prefs) { 88 void RegisterUserPrefs(PrefService* user_prefs) {
85 user_prefs->RegisterBooleanPref(prefs::kDnsPrefetchingEnabled, true); 89 user_prefs->RegisterBooleanPref(prefs::kDnsPrefetchingEnabled, true);
86 } 90 }
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 // then try to shutdown the browser) don't instigate the CHECK about 406 // then try to shutdown the browser) don't instigate the CHECK about
403 // "some slaves have not finished" 407 // "some slaves have not finished"
404 const TimeDelta kAllowableShutdownTime(TimeDelta::FromSeconds(10)); 408 const TimeDelta kAllowableShutdownTime(TimeDelta::FromSeconds(10));
405 DCHECK(NULL == dns_master); 409 DCHECK(NULL == dns_master);
406 if (!dns_master) { 410 if (!dns_master) {
407 // Have the DnsMaster issue resolve requests through a global HostResolver 411 // Have the DnsMaster issue resolve requests through a global HostResolver
408 // that is shared by the main URLRequestContext, and lives on the IO thread. 412 // that is shared by the main URLRequestContext, and lives on the IO thread.
409 dns_master = new DnsMaster(GetGlobalHostResolver(), 413 dns_master = new DnsMaster(GetGlobalHostResolver(),
410 max_queue_delay, max_concurrent); 414 max_queue_delay, max_concurrent);
411 dns_master->AddRef(); 415 dns_master->AddRef();
412 // We did the initialization, so we should prime the pump, and set up
413 // the DNS resolution system to run.
414 Singleton<OffTheRecordObserver>::get()->Register();
415 416
416 if (user_prefs) { 417 if (user_prefs) {
417 bool enabled = user_prefs->GetBoolean(prefs::kDnsPrefetchingEnabled); 418 bool enabled = user_prefs->GetBoolean(prefs::kDnsPrefetchingEnabled);
418 EnableDnsPrefetch(enabled); 419 EnableDnsPrefetch(enabled);
419 } 420 }
420 421
421 DLOG(INFO) << "DNS Prefetch service started"; 422 DLOG(INFO) << "DNS Prefetch service started";
422 423
423 // Start observing real HTTP stack resolutions. 424 // Start observing real HTTP stack resolutions.
424 // TODO(eroman): really this should be called from IO thread (since that is 425 // TODO(eroman): really this should be called from IO thread (since that is
(...skipping 25 matching lines...) Expand all
450 global_host_resolver = NULL; 451 global_host_resolver = NULL;
451 } 452 }
452 } 453 }
453 454
454 void FreeDnsPrefetchResources() { 455 void FreeDnsPrefetchResources() {
455 DCHECK(NULL != dns_master); 456 DCHECK(NULL != dns_master);
456 dns_master->Release(); 457 dns_master->Release();
457 dns_master = NULL; 458 dns_master = NULL;
458 } 459 }
459 460
460 static void DiscardAllPrefetchState() { 461 static void ChangedToOnTheRecord() {
461 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 462 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
462 if (!dns_master)
463 return;
464 463
465 ChromeThread::PostTask( 464 if (dns_master) {
466 ChromeThread::IO, 465 // Destroy all evidence of our OTR session.
467 FROM_HERE, 466 dns_master->DnsMaster::DiscardAllResults();
468 NewRunnableMethod(dns_master, 467 }
469 &DnsMaster::DiscardAllResults)); 468
469 // Clear the host cache to avoid showing entries from the OTR session
470 // in about:net-internals.
471 net::HostCache* host_cache = GetGlobalHostResolver()->GetHostCache();
472 if (host_cache)
473 host_cache->clear();
470 } 474 }
471 475
472 //------------------------------------------------------------------------------ 476 //------------------------------------------------------------------------------
473 477
474 net::HostResolver* GetGlobalHostResolver() { 478 net::HostResolver* GetGlobalHostResolver() {
475 // Called from UI thread. 479 // Called from UI thread.
476 if (!global_host_resolver) { 480 if (!global_host_resolver) {
477 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 481 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
478 482
479 // The FixedHostResolver allows us to send all network requests through 483 // The FixedHostResolver allows us to send all network requests through
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 FROM_HERE, 607 FROM_HERE,
604 NewRunnableMethod(dns_master, 608 NewRunnableMethod(dns_master,
605 &DnsMaster::DeserializeReferrersThenDelete, 609 &DnsMaster::DeserializeReferrersThenDelete,
606 referral_list_copy)); 610 referral_list_copy));
607 } 611 }
608 612
609 //------------------------------------------------------------------------------ 613 //------------------------------------------------------------------------------
610 // Methods for the helper class that is used to startup and teardown the whole 614 // Methods for the helper class that is used to startup and teardown the whole
611 // DNS prefetch system. 615 // DNS prefetch system.
612 616
613 DnsPrefetcherInit::DnsPrefetcherInit(PrefService* user_prefs, 617 DnsGlobalInit::DnsGlobalInit(PrefService* user_prefs,
614 PrefService* local_state) { 618 PrefService* local_state) {
615 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 619 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
616 // Set up a field trial to see what disabling DNS pre-resolution does to 620 // Set up a field trial to see what disabling DNS pre-resolution does to
617 // latency of page loads. 621 // latency of page loads.
618 FieldTrial::Probability kDivisor = 100; 622 FieldTrial::Probability kDivisor = 100;
619 // For each option (i.e., non-default), we have a fixed probability. 623 // For each option (i.e., non-default), we have a fixed probability.
620 FieldTrial::Probability kProbabilityPerGroup = 10; // 10% probability. 624 FieldTrial::Probability kProbabilityPerGroup = 10; // 10% probability.
621 625
622 trial_ = new FieldTrial("DnsImpact", kDivisor); 626 trial_ = new FieldTrial("DnsImpact", kDivisor);
623 627
624 // First option is to disable prefetching completely. 628 // First option is to disable prefetching completely.
625 int disabled_prefetch = trial_->AppendGroup("_disabled_prefetch", 629 int disabled_prefetch = trial_->AppendGroup("_disabled_prefetch",
626 kProbabilityPerGroup); 630 kProbabilityPerGroup);
627 // Set congestion detection at 250, 500, or 750ms, rather than the 1 second 631 // Set congestion detection at 250, 500, or 750ms, rather than the 1 second
628 // default. 632 // default.
629 int max_250ms_prefetch = trial_->AppendGroup("_max_250ms_queue_prefetch", 633 int max_250ms_prefetch = trial_->AppendGroup("_max_250ms_queue_prefetch",
630 kProbabilityPerGroup); 634 kProbabilityPerGroup);
631 int max_500ms_prefetch = trial_->AppendGroup("_max_500ms_queue_prefetch", 635 int max_500ms_prefetch = trial_->AppendGroup("_max_500ms_queue_prefetch",
632 kProbabilityPerGroup); 636 kProbabilityPerGroup);
633 int max_750ms_prefetch = trial_->AppendGroup("_max_750ms_queue_prefetch", 637 int max_750ms_prefetch = trial_->AppendGroup("_max_750ms_queue_prefetch",
634 kProbabilityPerGroup); 638 kProbabilityPerGroup);
635 // Set congestion detection at 2 seconds instead of the 1 second default. 639 // Set congestion detection at 2 seconds instead of the 1 second default.
636 int max_2s_prefetch = trial_->AppendGroup("_max_2s_queue_prefetch", 640 int max_2s_prefetch = trial_->AppendGroup("_max_2s_queue_prefetch",
637 kProbabilityPerGroup); 641 kProbabilityPerGroup);
638 642
639 trial_->AppendGroup("_default_enabled_prefetch", 643 trial_->AppendGroup("_default_enabled_prefetch",
640 FieldTrial::kAllRemainingProbability); 644 FieldTrial::kAllRemainingProbability);
641 645
646 // We will register the incognito observer regardless of whether prefetching
647 // is enabled, as it is also used to clear the host cache.
648 Singleton<OffTheRecordObserver>::get()->Register();
649
642 if (trial_->group() != disabled_prefetch) { 650 if (trial_->group() != disabled_prefetch) {
643 // Initialize the DNS prefetch system. 651 // Initialize the DNS prefetch system.
644 652
645 size_t max_concurrent = kMaxConcurrentLookups; 653 size_t max_concurrent = kMaxPrefetchConcurrentLookups;
646 654
647 int max_queueing_delay_ms = kMaxQueueingDelayMs; 655 int max_queueing_delay_ms = kMaxPrefetchQueueingDelayMs;
648 656
649 if (trial_->group() == max_250ms_prefetch) 657 if (trial_->group() == max_250ms_prefetch)
650 max_queueing_delay_ms = 250; 658 max_queueing_delay_ms = 250;
651 else if (trial_->group() == max_500ms_prefetch) 659 else if (trial_->group() == max_500ms_prefetch)
652 max_queueing_delay_ms = 500; 660 max_queueing_delay_ms = 500;
653 else if (trial_->group() == max_750ms_prefetch) 661 else if (trial_->group() == max_750ms_prefetch)
654 max_queueing_delay_ms = 750; 662 max_queueing_delay_ms = 750;
655 else if (trial_->group() == max_2s_prefetch) 663 else if (trial_->group() == max_2s_prefetch)
656 max_queueing_delay_ms = 2000; 664 max_queueing_delay_ms = 2000;
657 665
658 TimeDelta max_queueing_delay( 666 TimeDelta max_queueing_delay(
659 TimeDelta::FromMilliseconds(max_queueing_delay_ms)); 667 TimeDelta::FromMilliseconds(max_queueing_delay_ms));
660 668
661 DCHECK(!dns_master); 669 DCHECK(!dns_master);
662 InitDnsPrefetch(max_queueing_delay, max_concurrent, user_prefs); 670 InitDnsPrefetch(max_queueing_delay, max_concurrent, user_prefs);
663 DCHECK(dns_master); // Will be checked in destructor. 671 DCHECK(dns_master); // Will be checked in destructor.
664 DnsPrefetchHostNamesAtStartup(user_prefs, local_state); 672 DnsPrefetchHostNamesAtStartup(user_prefs, local_state);
665 RestoreSubresourceReferrers(local_state); 673 RestoreSubresourceReferrers(local_state);
666 } 674 }
667 } 675 }
668 676
669 DnsPrefetcherInit::~DnsPrefetcherInit() { 677 DnsGlobalInit::~DnsGlobalInit() {
670 if (dns_master) 678 if (dns_master)
671 FreeDnsPrefetchResources(); 679 FreeDnsPrefetchResources();
672 } 680 }
673 681
674 } // namespace chrome_browser_net 682 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698