[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 3530cd9 | 2010-06-27 06:22:01 | [diff] [blame] | 5 | #include "chrome/browser/net/predictor_api.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 10 | #include "base/metrics/field_trial.h" |
[email protected] | 94dc228 | 2009-05-26 19:32:32 | [diff] [blame] | 11 | #include "base/singleton.h" |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 12 | #include "base/stl_util-inl.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 13 | #include "base/string_number_conversions.h" |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 14 | #include "base/thread.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "base/values.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 16 | #include "base/waitable_event.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | #include "chrome/browser/browser.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 18 | #include "chrome/browser/browser_process.h" |
[email protected] | 017a7a11 | 2010-10-12 16:38:27 | [diff] [blame] | 19 | #include "chrome/browser/browser_thread.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 20 | #include "chrome/browser/io_thread.h" |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 21 | #include "chrome/browser/net/preconnect.h" |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 22 | #include "chrome/browser/net/referrer.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 23 | #include "chrome/browser/net/url_info.h" |
| 24 | #include "chrome/browser/prefs/pref_service.h" |
| 25 | #include "chrome/browser/prefs/session_startup_pref.h" |
[email protected] | 505323e2 | 2009-01-24 02:47:58 | [diff] [blame] | 26 | #include "chrome/browser/profile.h" |
[email protected] | f1c8a2b | 2009-05-22 01:52:43 | [diff] [blame] | 27 | #include "chrome/common/notification_registrar.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | #include "chrome/common/notification_service.h" |
| 29 | #include "chrome/common/pref_names.h" |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 30 | #include "net/base/host_resolver.h" |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 31 | #include "net/base/host_resolver_impl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 33 | using base::Time; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 34 | using base::TimeDelta; |
| 35 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | namespace chrome_browser_net { |
| 37 | |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 38 | static void DnsPrefetchMotivatedList(const UrlList& urls, |
[email protected] | f4ef861ba | 2010-07-28 22:37:23 | [diff] [blame] | 39 | UrlInfo::ResolutionMotivation motivation); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | |
[email protected] | f4ef861ba | 2010-07-28 22:37:23 | [diff] [blame] | 41 | static UrlList GetPredictedUrlListAtStartup(PrefService* user_prefs, |
| 42 | PrefService* local_state); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 43 | |
[email protected] | e085c30 | 2009-06-01 18:31:36 | [diff] [blame] | 44 | // static |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 45 | const size_t PredictorInit::kMaxPrefetchConcurrentLookups = 8; |
[email protected] | e085c30 | 2009-06-01 18:31:36 | [diff] [blame] | 46 | |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 47 | // static |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 48 | const int PredictorInit::kMaxPrefetchQueueingDelayMs = 500; |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 49 | |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 50 | // A version number for prefs that are saved. This should be incremented when |
| 51 | // we change the format so that we discard old data. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 52 | static const int kPredictorStartupFormatVersion = 1; |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 53 | |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 54 | // There will only be one instance ever created of the following Observer class. |
| 55 | // The InitialObserver lives on the IO thread, and monitors navigations made by |
| 56 | // the network stack. This is only used to identify startup time resolutions |
| 57 | // (for re-resolution during our next process startup). |
| 58 | // TODO(jar): Consider preconnecting at startup, which may be faster than |
| 59 | // waiting for render process to start and request a connection. |
| 60 | class InitialObserver { |
| 61 | public: |
| 62 | // Recording of when we observed each navigation. |
| 63 | typedef std::map<GURL, base::TimeTicks> FirstNavigations; |
| 64 | |
| 65 | // Potentially add a new URL to our startup list. |
| 66 | void Append(const GURL& url); |
| 67 | |
| 68 | // Get an HTML version of our current planned first_navigations_. |
| 69 | void GetFirstResolutionsHtml(std::string* output); |
| 70 | |
| 71 | // Persist the current first_navigations_ for storage in a list. |
| 72 | void GetInitialDnsResolutionList(ListValue* startup_list); |
| 73 | |
| 74 | private: |
| 75 | // List of the first N URL resolutions observed in this run. |
| 76 | FirstNavigations first_navigations_; |
| 77 | |
| 78 | // The number of URLs we'll save for pre-resolving at next startup. |
| 79 | static const size_t kStartupResolutionCount = 10; |
| 80 | }; |
| 81 | |
| 82 | // TODO(willchan): Look at killing this global. |
| 83 | static InitialObserver* g_initial_observer = NULL; |
| 84 | |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 85 | //------------------------------------------------------------------------------ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | // This section contains all the globally accessable API entry points for the |
| 87 | // DNS Prefetching feature. |
| 88 | //------------------------------------------------------------------------------ |
| 89 | |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 90 | // Status of speculative DNS resolution and speculative TCP/IP connection |
| 91 | // feature. |
| 92 | static bool predictor_enabled = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | |
| 94 | // Cached inverted copy of the off_the_record pref. |
| 95 | static bool on_the_record_switch = true; |
| 96 | |
| 97 | // Enable/disable Dns prefetch activity (either via command line, or via pref). |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 98 | void EnablePredictor(bool enable) { |
[email protected] | 847e54f | 2010-01-23 16:37:56 | [diff] [blame] | 99 | // NOTE: this is invoked on the UI thread. |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 100 | predictor_enabled = enable; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void OnTheRecord(bool enable) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 104 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | if (on_the_record_switch == enable) |
| 106 | return; |
| 107 | on_the_record_switch = enable; |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 108 | if (on_the_record_switch) |
| 109 | g_browser_process->io_thread()->ChangedToOnTheRecord(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void RegisterPrefs(PrefService* local_state) { |
| 113 | local_state->RegisterListPref(prefs::kDnsStartupPrefetchList); |
[email protected] | 03c5e86 | 2009-02-17 22:50:14 | [diff] [blame] | 114 | local_state->RegisterListPref(prefs::kDnsHostReferralList); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void RegisterUserPrefs(PrefService* user_prefs) { |
| 118 | user_prefs->RegisterBooleanPref(prefs::kDnsPrefetchingEnabled, true); |
| 119 | } |
| 120 | |
| 121 | // When enabled, we use the following instance to service all requests in the |
| 122 | // browser process. |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 123 | // TODO(willchan): Look at killing this. |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 124 | static Predictor* g_predictor = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | |
| 126 | // This API is only used in the browser process. |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 127 | // It is called from an IPC message originating in the renderer. It currently |
| 128 | // includes both Page-Scan, and Link-Hover prefetching. |
[email protected] | 03c5e86 | 2009-02-17 22:50:14 | [diff] [blame] | 129 | // TODO(jar): Separate out link-hover prefetching, and page-scan results. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 130 | void DnsPrefetchList(const NameList& hostnames) { |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 131 | // TODO(jar): Push GURL transport further back into renderer, but this will |
| 132 | // require a Webkit change in the observer :-/. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 133 | UrlList urls; |
| 134 | for (NameList::const_iterator it = hostnames.begin(); |
| 135 | it < hostnames.end(); |
| 136 | ++it) { |
| 137 | urls.push_back(GURL("http://" + *it + ":80")); |
| 138 | } |
| 139 | |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 140 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 141 | DnsPrefetchMotivatedList(urls, UrlInfo::PAGE_SCAN_MOTIVATED); |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void DnsPrefetchMotivatedList( |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 145 | const UrlList& urls, |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 146 | UrlInfo::ResolutionMotivation motivation) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 147 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 148 | BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 149 | if (!predictor_enabled || NULL == g_predictor) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | return; |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 151 | |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 152 | if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 153 | g_predictor->ResolveList(urls, motivation); |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 154 | } else { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 155 | BrowserThread::PostTask( |
| 156 | BrowserThread::IO, |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 157 | FROM_HERE, |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 158 | NewRunnableMethod(g_predictor, |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 159 | &Predictor::ResolveList, urls, motivation)); |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 160 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | e7a5b787 | 2008-12-10 23:52:43 | [diff] [blame] | 163 | // This API is used by the autocomplete popup box (where URLs are typed). |
[email protected] | e326922d | 2010-09-03 09:08:10 | [diff] [blame] | 164 | void AnticipateOmniboxUrl(const GURL& url, bool preconnectable) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 165 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 166 | if (!predictor_enabled || NULL == g_predictor) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 167 | return; |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 168 | if (!url.is_valid() || !url.has_host()) |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 169 | return; |
| 170 | |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 171 | g_predictor->AnticipateOmniboxUrl(url, preconnectable); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | e326922d | 2010-09-03 09:08:10 | [diff] [blame] | 174 | void PreconnectUrlAndSubresources(const GURL& url) { |
[email protected] | e326922d | 2010-09-03 09:08:10 | [diff] [blame] | 175 | if (!predictor_enabled || NULL == g_predictor) |
| 176 | return; |
| 177 | if (!url.is_valid() || !url.has_host()) |
| 178 | return; |
| 179 | |
| 180 | g_predictor->PreconnectUrlAndSubresources(url); |
| 181 | } |
| 182 | |
| 183 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | //------------------------------------------------------------------------------ |
| 185 | // This section intermingles prefetch results with actual browser HTTP |
| 186 | // network activity. It supports calculating of the benefit of a prefetch, as |
| 187 | // well as recording what prefetched hostname resolutions might be potentially |
| 188 | // helpful during the next chrome-startup. |
| 189 | //------------------------------------------------------------------------------ |
| 190 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 191 | void PredictFrameSubresources(const GURL& url) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 192 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 193 | if (!predictor_enabled || NULL == g_predictor) |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 194 | return; |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 195 | g_predictor->PredictFrameSubresources(url); |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 198 | void LearnAboutInitialNavigation(const GURL& url) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 199 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 200 | if (!predictor_enabled || NULL == g_initial_observer ) |
| 201 | return; |
| 202 | g_initial_observer->Append(url); |
| 203 | } |
| 204 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 205 | void LearnFromNavigation(const GURL& referring_url, const GURL& target_url) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 206 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 207 | if (!predictor_enabled || NULL == g_predictor) |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 208 | return; |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 209 | g_predictor->LearnFromNavigation(referring_url, target_url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // The observer class needs to connect starts and finishes of HTTP network |
| 213 | // resolutions. We use the following type for that map. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 214 | typedef std::map<int, UrlInfo> ObservedResolutionMap; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 215 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | //------------------------------------------------------------------------------ |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 217 | // Member definitions for InitialObserver class. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 219 | void InitialObserver::Append(const GURL& url) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 220 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 221 | |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 222 | if (!on_the_record_switch || NULL == g_predictor) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | return; |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 224 | if (kStartupResolutionCount <= first_navigations_.size()) |
| 225 | return; |
[email protected] | f4ef861ba | 2010-07-28 22:37:23 | [diff] [blame] | 226 | |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 227 | if (url.SchemeIs("http") || url.SchemeIs("https")) { |
| 228 | const GURL url_without_path(url.GetWithEmptyPath()); |
| 229 | if (first_navigations_.find(url_without_path) == first_navigations_.end()) |
| 230 | first_navigations_[url_without_path] = base::TimeTicks::Now(); |
| 231 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 234 | void InitialObserver::GetInitialDnsResolutionList(ListValue* startup_list) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 235 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | DCHECK(startup_list); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | startup_list->Clear(); |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 238 | DCHECK_EQ(0u, startup_list->GetSize()); |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 239 | startup_list->Append(new FundamentalValue(kPredictorStartupFormatVersion)); |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 240 | for (FirstNavigations::iterator it = first_navigations_.begin(); |
| 241 | it != first_navigations_.end(); |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 242 | ++it) { |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 243 | DCHECK(it->first == Predictor::CanonicalizeUrl(it->first)); |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 244 | startup_list->Append(new StringValue(it->first.spec())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 248 | void InitialObserver::GetFirstResolutionsHtml(std::string* output) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 249 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 250 | |
[email protected] | f4ef861ba | 2010-07-28 22:37:23 | [diff] [blame] | 251 | UrlInfo::UrlInfoTable resolution_list; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 252 | { |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 253 | for (FirstNavigations::iterator it(first_navigations_.begin()); |
| 254 | it != first_navigations_.end(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 255 | it++) { |
[email protected] | f4ef861ba | 2010-07-28 22:37:23 | [diff] [blame] | 256 | UrlInfo info; |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 257 | info.SetUrl(it->first); |
| 258 | info.set_time(it->second); |
[email protected] | f4ef861ba | 2010-07-28 22:37:23 | [diff] [blame] | 259 | resolution_list.push_back(info); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 260 | } |
| 261 | } |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 262 | UrlInfo::GetHtmlTable(resolution_list, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 263 | "Future startups will prefetch DNS records for ", false, output); |
| 264 | } |
| 265 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 266 | //------------------------------------------------------------------------------ |
| 267 | // Support observer to detect opening and closing of OffTheRecord windows. |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 268 | // This object lives on the UI thread. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 269 | |
| 270 | class OffTheRecordObserver : public NotificationObserver { |
| 271 | public: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 272 | void Register() { |
[email protected] | eca71da | 2009-05-22 03:06:57 | [diff] [blame] | 273 | // TODO(pkasting): This test should not be necessary. See crbug.com/12475. |
| 274 | if (registrar_.IsEmpty()) { |
| 275 | registrar_.Add(this, NotificationType::BROWSER_CLOSED, |
| 276 | NotificationService::AllSources()); |
| 277 | registrar_.Add(this, NotificationType::BROWSER_OPENED, |
| 278 | NotificationService::AllSources()); |
| 279 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void Observe(NotificationType type, const NotificationSource& source, |
| 283 | const NotificationDetails& details) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 284 | switch (type.value) { |
| 285 | case NotificationType::BROWSER_OPENED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | if (!Source<Browser>(source)->profile()->IsOffTheRecord()) |
| 287 | break; |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 288 | ++count_off_the_record_windows_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | OnTheRecord(false); |
| 290 | break; |
| 291 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 292 | case NotificationType::BROWSER_CLOSED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 293 | if (!Source<Browser>(source)->profile()->IsOffTheRecord()) |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 294 | break; // Ignore ordinary windows. |
| 295 | DCHECK_LT(0, count_off_the_record_windows_); |
| 296 | if (0 >= count_off_the_record_windows_) // Defensive coding. |
| 297 | break; |
| 298 | if (--count_off_the_record_windows_) |
| 299 | break; // Still some windows are incognito. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 300 | OnTheRecord(true); |
| 301 | break; |
| 302 | |
| 303 | default: |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | private: |
[email protected] | 94dc228 | 2009-05-26 19:32:32 | [diff] [blame] | 309 | friend struct DefaultSingletonTraits<OffTheRecordObserver>; |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 310 | OffTheRecordObserver() : count_off_the_record_windows_(0) {} |
| 311 | ~OffTheRecordObserver() {} |
[email protected] | 94dc228 | 2009-05-26 19:32:32 | [diff] [blame] | 312 | |
[email protected] | f1c8a2b | 2009-05-22 01:52:43 | [diff] [blame] | 313 | NotificationRegistrar registrar_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 314 | int count_off_the_record_windows_; |
| 315 | |
[email protected] | e8013b3 | 2008-10-27 18:55:52 | [diff] [blame] | 316 | DISALLOW_COPY_AND_ASSIGN(OffTheRecordObserver); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 317 | }; |
| 318 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 319 | //------------------------------------------------------------------------------ |
| 320 | // This section supports the about:dns page. |
| 321 | //------------------------------------------------------------------------------ |
| 322 | |
| 323 | // Provide global support for the about:dns page. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 324 | void PredictorGetHtmlInfo(std::string* output) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 325 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 326 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 327 | output->append("<html><head><title>About DNS</title>" |
| 328 | // We'd like the following no-cache... but it doesn't work. |
| 329 | // "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">" |
| 330 | "</head><body>"); |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 331 | if (!predictor_enabled || NULL == g_predictor) { |
[email protected] | c4566249 | 2010-10-05 00:45:22 | [diff] [blame] | 332 | output->append("DNS pre-resolution and TCP pre-connection is disabled."); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 333 | } else { |
| 334 | if (!on_the_record_switch) { |
| 335 | output->append("Incognito mode is active in a window."); |
| 336 | } else { |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 337 | // List items fetched at startup. |
| 338 | if (g_initial_observer) |
| 339 | g_initial_observer->GetFirstResolutionsHtml(output); |
| 340 | // Show list of subresource predictions and stats. |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 341 | g_predictor->GetHtmlReferrerLists(output); |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 342 | // Show list of prediction results. |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 343 | g_predictor->GetHtmlInfo(output); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | output->append("</body></html>"); |
| 347 | } |
| 348 | |
| 349 | //------------------------------------------------------------------------------ |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 350 | // This section intializes global DNS prefetch services. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 351 | //------------------------------------------------------------------------------ |
| 352 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 353 | static void InitNetworkPredictor(TimeDelta max_dns_queue_delay, |
| 354 | size_t max_concurrent, PrefService* user_prefs, PrefService* local_state, |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 355 | bool preconnect_enabled) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 356 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 357 | |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 358 | bool prefetching_enabled = |
| 359 | user_prefs->GetBoolean(prefs::kDnsPrefetchingEnabled); |
[email protected] | cf5a15c8 | 2008-09-28 18:01:34 | [diff] [blame] | 360 | |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 361 | // Gather the list of hostnames to prefetch on startup. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 362 | UrlList urls = |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 363 | GetPredictedUrlListAtStartup(user_prefs, local_state); |
[email protected] | cf5a15c8 | 2008-09-28 18:01:34 | [diff] [blame] | 364 | |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 365 | ListValue* referral_list = |
| 366 | static_cast<ListValue*>( |
| 367 | local_state->GetMutableList(prefs::kDnsHostReferralList)->DeepCopy()); |
| 368 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 369 | g_browser_process->io_thread()->InitNetworkPredictor( |
| 370 | prefetching_enabled, max_dns_queue_delay, max_concurrent, urls, |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 371 | referral_list, preconnect_enabled); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | } |
| 373 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 374 | void FinalizePredictorInitialization( |
| 375 | Predictor* global_predictor, |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 376 | const UrlList& startup_urls, |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 377 | ListValue* referral_list) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 378 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 379 | g_predictor = global_predictor; |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 380 | g_initial_observer = new InitialObserver(); |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 381 | |
[email protected] | 847e54f | 2010-01-23 16:37:56 | [diff] [blame] | 382 | // Prefetch these hostnames on startup. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 383 | DnsPrefetchMotivatedList(startup_urls, |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 384 | UrlInfo::STARTUP_LIST_MOTIVATED); |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 385 | g_predictor->DeserializeReferrersThenDelete(referral_list); |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 388 | void FreePredictorResources() { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 389 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 390 | g_predictor = NULL; // Owned and released by io_thread.cc. |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 391 | delete g_initial_observer; |
| 392 | g_initial_observer = NULL; |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 393 | } |
| 394 | |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 395 | //------------------------------------------------------------------------------ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | // Functions to handle saving of hostnames from one session to the next, to |
| 397 | // expedite startup times. |
| 398 | |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 399 | static void SaveDnsPrefetchStateForNextStartupAndTrimOnIOThread( |
| 400 | ListValue* startup_list, |
| 401 | ListValue* referral_list, |
| 402 | base::WaitableEvent* completion) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 403 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 404 | |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 405 | if (NULL == g_predictor) { |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 406 | completion->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | return; |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 410 | if (g_initial_observer) |
| 411 | g_initial_observer->GetInitialDnsResolutionList(startup_list); |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 412 | |
| 413 | // TODO(jar): Trimming should be done more regularly, such as every 48 hours |
| 414 | // of physical time, or perhaps after 48 hours of running (excluding time |
| 415 | // between sessions possibly). |
| 416 | // For now, we'll just trim at shutdown. |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 417 | g_predictor->TrimReferrers(); |
| 418 | g_predictor->SerializeReferrers(referral_list); |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 419 | |
| 420 | completion->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 421 | } |
| 422 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 423 | void SavePredictorStateForNextStartupAndTrim(PrefService* prefs) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 424 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 425 | |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 426 | if (!predictor_enabled || g_predictor == NULL) |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 427 | return; |
| 428 | |
| 429 | base::WaitableEvent completion(true, false); |
| 430 | |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 431 | bool posted = BrowserThread::PostTask( |
| 432 | BrowserThread::IO, |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 433 | FROM_HERE, |
| 434 | NewRunnableFunction(SaveDnsPrefetchStateForNextStartupAndTrimOnIOThread, |
| 435 | prefs->GetMutableList(prefs::kDnsStartupPrefetchList), |
| 436 | prefs->GetMutableList(prefs::kDnsHostReferralList), |
| 437 | &completion)); |
| 438 | |
| 439 | DCHECK(posted); |
| 440 | if (posted) |
| 441 | completion.Wait(); |
| 442 | } |
| 443 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 444 | static UrlList GetPredictedUrlListAtStartup(PrefService* user_prefs, |
| 445 | PrefService* local_state) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 446 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 447 | UrlList urls; |
| 448 | // Recall list of URLs we learned about during last session. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 449 | // This may catch secondary hostnames, pulled in by the homepages. It will |
| 450 | // also catch more of the "primary" home pages, since that was (presumably) |
| 451 | // rendered first (and will be rendered first this time too). |
| 452 | ListValue* startup_list = |
| 453 | local_state->GetMutableList(prefs::kDnsStartupPrefetchList); |
| 454 | if (startup_list) { |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 455 | ListValue::iterator it = startup_list->begin(); |
| 456 | int format_version = -1; |
| 457 | if (it != startup_list->end() && |
| 458 | (*it)->GetAsInteger(&format_version) && |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 459 | format_version == kPredictorStartupFormatVersion) { |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 460 | ++it; |
| 461 | for (; it != startup_list->end(); ++it) { |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 462 | std::string url_spec; |
| 463 | if (!(*it)->GetAsString(&url_spec)) { |
| 464 | LOG(DFATAL); |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 465 | break; // Format incompatibility. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 466 | } |
| 467 | GURL url(url_spec); |
| 468 | if (!url.has_host() || !url.has_scheme()) { |
| 469 | LOG(DFATAL); |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 470 | break; // Format incompatibility. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 471 | } |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 472 | |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 473 | urls.push_back(url); |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 474 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
| 478 | // Prepare for any static home page(s) the user has in prefs. The user may |
| 479 | // have a LOT of tab's specified, so we may as well try to warm them all. |
| 480 | SessionStartupPref tab_start_pref = |
| 481 | SessionStartupPref::GetStartupPref(user_prefs); |
| 482 | if (SessionStartupPref::URLS == tab_start_pref.type) { |
| 483 | for (size_t i = 0; i < tab_start_pref.urls.size(); i++) { |
| 484 | GURL gurl = tab_start_pref.urls[i]; |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 485 | if (!gurl.is_valid() || gurl.SchemeIsFile() || gurl.host().empty()) |
| 486 | continue; |
| 487 | if (gurl.SchemeIs("http") || gurl.SchemeIs("https")) |
| 488 | urls.push_back(gurl.GetWithEmptyPath()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 492 | if (urls.empty()) |
| 493 | urls.push_back(GURL("http://www.google.com:80")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 494 | |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 495 | return urls; |
[email protected] | 03c5e86 | 2009-02-17 22:50:14 | [diff] [blame] | 496 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 497 | |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 498 | //------------------------------------------------------------------------------ |
| 499 | // Methods for the helper class that is used to startup and teardown the whole |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 500 | // g_predictor system (both DNS pre-resolution and TCP/IP connection |
| 501 | // prewarming). |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 502 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 503 | PredictorInit::PredictorInit(PrefService* user_prefs, |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 504 | PrefService* local_state, |
[email protected] | e326922d | 2010-09-03 09:08:10 | [diff] [blame] | 505 | bool preconnect_enabled) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 506 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 507 | // Set up a field trial to see what disabling DNS pre-resolution does to |
| 508 | // latency of page loads. |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 509 | base::FieldTrial::Probability kDivisor = 1000; |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 510 | // For each option (i.e., non-default), we have a fixed probability. |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 511 | base::FieldTrial::Probability kProbabilityPerGroup = 100; // 10% probability. |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 512 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 513 | trial_ = new base::FieldTrial("DnsImpact", kDivisor); |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 514 | |
| 515 | // First option is to disable prefetching completely. |
[email protected] | 58d2d2d | 2010-08-05 22:46:33 | [diff] [blame] | 516 | int disabled_prefetch = trial_->AppendGroup("disabled_prefetch", |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 517 | kProbabilityPerGroup); |
[email protected] | f9f4841b | 2010-03-20 05:41:42 | [diff] [blame] | 518 | |
| 519 | |
| 520 | // We're running two experiments at the same time. The first set of trials |
| 521 | // modulates the delay-time until we declare a congestion event (and purge |
| 522 | // our queue). The second modulates the number of concurrent resolutions |
| 523 | // we do at any time. Users are in exactly one trial (or the default) during |
| 524 | // any one run, and hence only one experiment at a time. |
| 525 | // Experiment 1: |
[email protected] | 85d48aef | 2009-07-18 06:17:19 | [diff] [blame] | 526 | // Set congestion detection at 250, 500, or 750ms, rather than the 1 second |
| 527 | // default. |
[email protected] | 58d2d2d | 2010-08-05 22:46:33 | [diff] [blame] | 528 | int max_250ms_prefetch = trial_->AppendGroup("max_250ms_queue_prefetch", |
[email protected] | 85d48aef | 2009-07-18 06:17:19 | [diff] [blame] | 529 | kProbabilityPerGroup); |
[email protected] | 58d2d2d | 2010-08-05 22:46:33 | [diff] [blame] | 530 | int max_500ms_prefetch = trial_->AppendGroup("max_500ms_queue_prefetch", |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 531 | kProbabilityPerGroup); |
[email protected] | 58d2d2d | 2010-08-05 22:46:33 | [diff] [blame] | 532 | int max_750ms_prefetch = trial_->AppendGroup("max_750ms_queue_prefetch", |
[email protected] | 85d48aef | 2009-07-18 06:17:19 | [diff] [blame] | 533 | kProbabilityPerGroup); |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 534 | // Set congestion detection at 2 seconds instead of the 1 second default. |
[email protected] | 58d2d2d | 2010-08-05 22:46:33 | [diff] [blame] | 535 | int max_2s_prefetch = trial_->AppendGroup("max_2s_queue_prefetch", |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 536 | kProbabilityPerGroup); |
[email protected] | f9f4841b | 2010-03-20 05:41:42 | [diff] [blame] | 537 | // Experiment 2: |
| 538 | // Set max simultaneous resoultions to 2, 4, or 6, and scale the congestion |
| 539 | // limit proportionally (so we don't impact average probability of asserting |
| 540 | // congesion very much). |
| 541 | int max_2_concurrent_prefetch = trial_->AppendGroup( |
[email protected] | 58d2d2d | 2010-08-05 22:46:33 | [diff] [blame] | 542 | "max_2 concurrent_prefetch", kProbabilityPerGroup); |
[email protected] | f9f4841b | 2010-03-20 05:41:42 | [diff] [blame] | 543 | int max_4_concurrent_prefetch = trial_->AppendGroup( |
[email protected] | 58d2d2d | 2010-08-05 22:46:33 | [diff] [blame] | 544 | "max_4 concurrent_prefetch", kProbabilityPerGroup); |
[email protected] | f9f4841b | 2010-03-20 05:41:42 | [diff] [blame] | 545 | int max_6_concurrent_prefetch = trial_->AppendGroup( |
[email protected] | 58d2d2d | 2010-08-05 22:46:33 | [diff] [blame] | 546 | "max_6 concurrent_prefetch", kProbabilityPerGroup); |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 547 | |
[email protected] | 58d2d2d | 2010-08-05 22:46:33 | [diff] [blame] | 548 | trial_->AppendGroup("default_enabled_prefetch", |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 549 | base::FieldTrial::kAllRemainingProbability); |
[email protected] | e695fbd6 | 2009-06-30 16:31:54 | [diff] [blame] | 550 | |
[email protected] | 8d53d50 | 2010-01-11 19:13:08 | [diff] [blame] | 551 | // We will register the incognito observer regardless of whether prefetching |
| 552 | // is enabled, as it is also used to clear the host cache. |
| 553 | Singleton<OffTheRecordObserver>::get()->Register(); |
| 554 | |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 555 | if (trial_->group() != disabled_prefetch) { |
| 556 | // Initialize the DNS prefetch system. |
[email protected] | 8d53d50 | 2010-01-11 19:13:08 | [diff] [blame] | 557 | size_t max_concurrent = kMaxPrefetchConcurrentLookups; |
[email protected] | 8d53d50 | 2010-01-11 19:13:08 | [diff] [blame] | 558 | int max_queueing_delay_ms = kMaxPrefetchQueueingDelayMs; |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 559 | |
[email protected] | 85d48aef | 2009-07-18 06:17:19 | [diff] [blame] | 560 | if (trial_->group() == max_250ms_prefetch) |
| 561 | max_queueing_delay_ms = 250; |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 562 | else if (trial_->group() == max_500ms_prefetch) |
| 563 | max_queueing_delay_ms = 500; |
[email protected] | 85d48aef | 2009-07-18 06:17:19 | [diff] [blame] | 564 | else if (trial_->group() == max_750ms_prefetch) |
| 565 | max_queueing_delay_ms = 750; |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 566 | else if (trial_->group() == max_2s_prefetch) |
| 567 | max_queueing_delay_ms = 2000; |
[email protected] | f9f4841b | 2010-03-20 05:41:42 | [diff] [blame] | 568 | if (trial_->group() == max_2_concurrent_prefetch) |
| 569 | max_concurrent = 2; |
| 570 | else if (trial_->group() == max_4_concurrent_prefetch) |
| 571 | max_concurrent = 4; |
| 572 | else if (trial_->group() == max_6_concurrent_prefetch) |
| 573 | max_concurrent = 6; |
| 574 | // Scale acceptable delay so we don't cause congestion limits to fire as |
| 575 | // we modulate max_concurrent (*if* we are modulating it at all). |
| 576 | max_queueing_delay_ms = (kMaxPrefetchQueueingDelayMs * |
| 577 | kMaxPrefetchConcurrentLookups) / max_concurrent; |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 578 | |
| 579 | TimeDelta max_queueing_delay( |
| 580 | TimeDelta::FromMilliseconds(max_queueing_delay_ms)); |
| 581 | |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 582 | DCHECK(!g_predictor); |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 583 | InitNetworkPredictor(max_queueing_delay, max_concurrent, user_prefs, |
| 584 | local_state, preconnect_enabled); |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 588 | PredictorInit::~PredictorInit() { |
| 589 | } |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 590 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 591 | } // namespace chrome_browser_net |