OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/net/predictor_api.h" | |
6 | |
7 #include <map> | |
8 #include <string> | |
9 | |
10 #include "base/lazy_instance.h" | |
11 #include "base/metrics/field_trial.h" | |
12 #include "base/stl_util.h" | |
13 #include "base/string_number_conversions.h" | |
14 #include "base/synchronization/waitable_event.h" | |
15 #include "base/threading/thread.h" | |
16 #include "base/values.h" | |
17 #include "chrome/browser/browser_process.h" | |
18 #include "chrome/browser/io_thread.h" | |
19 #include "chrome/browser/net/preconnect.h" | |
20 #include "chrome/browser/net/referrer.h" | |
21 #include "chrome/browser/net/url_info.h" | |
22 #include "chrome/browser/prefs/browser_prefs.h" | |
23 #include "chrome/browser/prefs/pref_service.h" | |
24 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
25 #include "chrome/browser/prefs/session_startup_pref.h" | |
26 #include "chrome/browser/profiles/profile.h" | |
27 #include "chrome/browser/ui/browser.h" | |
28 #include "chrome/common/chrome_notification_types.h" | |
29 #include "chrome/common/pref_names.h" | |
30 #include "content/browser/browser_thread.h" | |
31 #include "content/common/notification_registrar.h" | |
32 #include "content/common/notification_service.h" | |
33 #include "net/base/host_resolver.h" | |
34 #include "net/base/host_resolver_impl.h" | |
35 | |
36 using base::Time; | |
37 using base::TimeDelta; | |
38 | |
39 namespace chrome_browser_net { | |
40 | |
41 static void DnsPrefetchMotivatedList(const UrlList& urls, | |
42 UrlInfo::ResolutionMotivation motivation); | |
43 | |
44 static UrlList GetPredictedUrlListAtStartup(PrefService* user_prefs, | |
45 PrefService* local_state); | |
46 | |
47 // Given that the underlying Chromium resolver defaults to a total maximum of | |
48 // 8 paralell resolutions, we will avoid any chance of starving navigational | |
49 // resolutions by limiting the number of paralell speculative resolutions. | |
50 // TODO(jar): Move this limitation into the resolver. | |
51 // static | |
52 const size_t PredictorInit::kMaxSpeculativeParallelResolves = 3; | |
53 | |
54 // To control our congestion avoidance system, which discards a queue when | |
55 // resolutions are "taking too long," we need an expected resolution time. | |
56 // Common average is in the range of 300-500ms. | |
57 static const int kExpectedResolutionTimeMs = 500; | |
58 | |
59 // To control the congestion avoidance system, we need an estimate of how many | |
60 // speculative requests may arrive at once. Since we currently only keep 8 | |
61 // subresource names for each frame, we'll use that as our basis. Note that | |
62 // when scanning search results lists, we might actually get 10 at a time, and | |
63 // wikipedia can often supply (during a page scan) upwards of 50. In those odd | |
64 // cases, we may discard some of the later speculative requests mistakenly | |
65 // assuming that the resolutions took too long. | |
66 static const int kTypicalSpeculativeGroupSize = 8; | |
67 | |
68 // The next constant specifies an amount of queueing delay that is "too large," | |
69 // and indicative of problems with resolutions (perhaps due to an overloaded | |
70 // router, or such). When we exceed this delay, congestion avoidance will kick | |
71 // in and all speculations in the queue will be discarded. | |
72 // static | |
73 const int PredictorInit::kMaxSpeculativeResolveQueueDelayMs = | |
74 (kExpectedResolutionTimeMs * kTypicalSpeculativeGroupSize) / | |
75 kMaxSpeculativeParallelResolves; | |
76 | |
77 // A version number for prefs that are saved. This should be incremented when | |
78 // we change the format so that we discard old data. | |
79 static const int kPredictorStartupFormatVersion = 1; | |
80 | |
81 // There will only be one instance ever created of the following Observer class. | |
82 // The InitialObserver lives on the IO thread, and monitors navigations made by | |
83 // the network stack. This is only used to identify startup time resolutions | |
84 // (for re-resolution during our next process startup). | |
85 // TODO(jar): Consider preconnecting at startup, which may be faster than | |
86 // waiting for render process to start and request a connection. | |
87 class InitialObserver { | |
88 public: | |
89 // Recording of when we observed each navigation. | |
90 typedef std::map<GURL, base::TimeTicks> FirstNavigations; | |
91 | |
92 // Potentially add a new URL to our startup list. | |
93 void Append(const GURL& url); | |
94 | |
95 // Get an HTML version of our current planned first_navigations_. | |
96 void GetFirstResolutionsHtml(std::string* output); | |
97 | |
98 // Persist the current first_navigations_ for storage in a list. | |
99 void GetInitialDnsResolutionList(ListValue* startup_list); | |
100 | |
101 // Discards all initial loading history. | |
102 void DiscardInitialNavigationHistory() { first_navigations_.clear(); } | |
103 | |
104 private: | |
105 // List of the first N URL resolutions observed in this run. | |
106 FirstNavigations first_navigations_; | |
107 | |
108 // The number of URLs we'll save for pre-resolving at next startup. | |
109 static const size_t kStartupResolutionCount = 10; | |
110 }; | |
111 | |
112 // TODO(willchan): Look at killing this global. | |
113 static InitialObserver* g_initial_observer = NULL; | |
114 | |
115 //------------------------------------------------------------------------------ | |
116 // This section contains all the globally accessable API entry points for the | |
117 // DNS Prefetching feature. | |
118 //------------------------------------------------------------------------------ | |
119 | |
120 // Status of speculative DNS resolution and speculative TCP/IP connection | |
121 // feature. | |
122 static bool predictor_enabled = true; | |
123 | |
124 // Cached inverted copy of the off_the_record pref. | |
125 static bool on_the_record_switch = true; | |
126 | |
127 // Enable/disable Dns prefetch activity (either via command line, or via pref). | |
128 void EnablePredictor(bool enable) { | |
129 // NOTE: this is invoked on the UI thread. | |
130 predictor_enabled = enable; | |
131 } | |
132 | |
133 void OnTheRecord(bool enable) { | |
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
135 if (on_the_record_switch == enable) | |
136 return; | |
137 on_the_record_switch = enable; | |
138 if (on_the_record_switch) | |
139 g_browser_process->io_thread()->ChangedToOnTheRecord(); | |
140 } | |
141 | |
142 void DiscardInitialNavigationHistory() { | |
143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
144 if (g_initial_observer) | |
145 g_initial_observer->DiscardInitialNavigationHistory(); | |
146 } | |
147 | |
148 void RegisterUserPrefs(PrefService* user_prefs) { | |
149 user_prefs->RegisterListPref(prefs::kDnsPrefetchingStartupList, | |
150 PrefService::UNSYNCABLE_PREF); | |
151 user_prefs->RegisterListPref(prefs::kDnsPrefetchingHostReferralList, | |
152 PrefService::UNSYNCABLE_PREF); | |
153 } | |
154 | |
155 // When enabled, we use the following instance to service all requests in the | |
156 // browser process. | |
157 // TODO(willchan): Look at killing this. | |
158 static Predictor* g_predictor = NULL; | |
159 | |
160 // This API is only used in the browser process. | |
161 // It is called from an IPC message originating in the renderer. It currently | |
162 // includes both Page-Scan, and Link-Hover prefetching. | |
163 // TODO(jar): Separate out link-hover prefetching, and page-scan results. | |
164 void DnsPrefetchList(const NameList& hostnames) { | |
165 // TODO(jar): Push GURL transport further back into renderer, but this will | |
166 // require a Webkit change in the observer :-/. | |
167 UrlList urls; | |
168 for (NameList::const_iterator it = hostnames.begin(); | |
169 it < hostnames.end(); | |
170 ++it) { | |
171 urls.push_back(GURL("http://" + *it + ":80")); | |
172 } | |
173 | |
174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
175 DnsPrefetchMotivatedList(urls, UrlInfo::PAGE_SCAN_MOTIVATED); | |
176 } | |
177 | |
178 static void DnsPrefetchMotivatedList( | |
179 const UrlList& urls, | |
180 UrlInfo::ResolutionMotivation motivation) { | |
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | |
182 BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
183 if (!predictor_enabled || NULL == g_predictor) | |
184 return; | |
185 | |
186 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
187 g_predictor->ResolveList(urls, motivation); | |
188 } else { | |
189 BrowserThread::PostTask( | |
190 BrowserThread::IO, | |
191 FROM_HERE, | |
192 NewRunnableMethod(g_predictor, | |
193 &Predictor::ResolveList, urls, motivation)); | |
194 } | |
195 } | |
196 | |
197 // This API is used by the autocomplete popup box (where URLs are typed). | |
198 void AnticipateOmniboxUrl(const GURL& url, bool preconnectable) { | |
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
200 if (!predictor_enabled || NULL == g_predictor) | |
201 return; | |
202 if (!url.is_valid() || !url.has_host()) | |
203 return; | |
204 | |
205 g_predictor->AnticipateOmniboxUrl(url, preconnectable); | |
206 } | |
207 | |
208 void PreconnectUrlAndSubresources(const GURL& url) { | |
209 if (!predictor_enabled || NULL == g_predictor) | |
210 return; | |
211 if (!url.is_valid() || !url.has_host()) | |
212 return; | |
213 | |
214 g_predictor->PreconnectUrlAndSubresources(url); | |
215 } | |
216 | |
217 | |
218 //------------------------------------------------------------------------------ | |
219 // This section intermingles prefetch results with actual browser HTTP | |
220 // network activity. It supports calculating of the benefit of a prefetch, as | |
221 // well as recording what prefetched hostname resolutions might be potentially | |
222 // helpful during the next chrome-startup. | |
223 //------------------------------------------------------------------------------ | |
224 | |
225 void PredictFrameSubresources(const GURL& url) { | |
226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
227 if (!predictor_enabled || NULL == g_predictor) | |
228 return; | |
229 g_predictor->PredictFrameSubresources(url); | |
230 } | |
231 | |
232 void LearnAboutInitialNavigation(const GURL& url) { | |
233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
234 if (!predictor_enabled || NULL == g_initial_observer ) | |
235 return; | |
236 g_initial_observer->Append(url); | |
237 } | |
238 | |
239 void LearnFromNavigation(const GURL& referring_url, const GURL& target_url) { | |
240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
241 if (!predictor_enabled || NULL == g_predictor) | |
242 return; | |
243 g_predictor->LearnFromNavigation(referring_url, target_url); | |
244 } | |
245 | |
246 // The observer class needs to connect starts and finishes of HTTP network | |
247 // resolutions. We use the following type for that map. | |
248 typedef std::map<int, UrlInfo> ObservedResolutionMap; | |
249 | |
250 //------------------------------------------------------------------------------ | |
251 // Member definitions for InitialObserver class. | |
252 | |
253 void InitialObserver::Append(const GURL& url) { | |
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
255 | |
256 if (!on_the_record_switch || NULL == g_predictor) | |
257 return; | |
258 if (kStartupResolutionCount <= first_navigations_.size()) | |
259 return; | |
260 | |
261 DCHECK(url.SchemeIs("http") || url.SchemeIs("https")); | |
262 DCHECK_EQ(url, Predictor::CanonicalizeUrl(url)); | |
263 if (first_navigations_.find(url) == first_navigations_.end()) | |
264 first_navigations_[url] = base::TimeTicks::Now(); | |
265 } | |
266 | |
267 void InitialObserver::GetInitialDnsResolutionList(ListValue* startup_list) { | |
268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
269 DCHECK(startup_list); | |
270 startup_list->Clear(); | |
271 DCHECK_EQ(0u, startup_list->GetSize()); | |
272 startup_list->Append(new FundamentalValue(kPredictorStartupFormatVersion)); | |
273 for (FirstNavigations::iterator it = first_navigations_.begin(); | |
274 it != first_navigations_.end(); | |
275 ++it) { | |
276 DCHECK(it->first == Predictor::CanonicalizeUrl(it->first)); | |
277 startup_list->Append(new StringValue(it->first.spec())); | |
278 } | |
279 } | |
280 | |
281 void InitialObserver::GetFirstResolutionsHtml(std::string* output) { | |
282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
283 | |
284 UrlInfo::UrlInfoTable resolution_list; | |
285 { | |
286 for (FirstNavigations::iterator it(first_navigations_.begin()); | |
287 it != first_navigations_.end(); | |
288 it++) { | |
289 UrlInfo info; | |
290 info.SetUrl(it->first); | |
291 info.set_time(it->second); | |
292 resolution_list.push_back(info); | |
293 } | |
294 } | |
295 UrlInfo::GetHtmlTable(resolution_list, | |
296 "Future startups will prefetch DNS records for ", false, output); | |
297 } | |
298 | |
299 //------------------------------------------------------------------------------ | |
300 // Support observer to detect opening and closing of OffTheRecord windows. | |
301 // This object lives on the UI thread. | |
302 | |
303 class OffTheRecordObserver : public NotificationObserver { | |
304 public: | |
305 void Register() { | |
306 // TODO(pkasting): This test should not be necessary. See crbug.com/12475. | |
307 if (registrar_.IsEmpty()) { | |
308 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED, | |
309 NotificationService::AllSources()); | |
310 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED, | |
311 NotificationService::AllSources()); | |
312 } | |
313 } | |
314 | |
315 void Observe(int type, const NotificationSource& source, | |
316 const NotificationDetails& details) { | |
317 switch (type) { | |
318 case chrome::NOTIFICATION_BROWSER_OPENED: | |
319 if (!Source<Browser>(source)->profile()->IsOffTheRecord()) | |
320 break; | |
321 ++count_off_the_record_windows_; | |
322 OnTheRecord(false); | |
323 break; | |
324 | |
325 case chrome::NOTIFICATION_BROWSER_CLOSED: | |
326 if (!Source<Browser>(source)->profile()->IsOffTheRecord()) | |
327 break; // Ignore ordinary windows. | |
328 DCHECK_LT(0, count_off_the_record_windows_); | |
329 if (0 >= count_off_the_record_windows_) // Defensive coding. | |
330 break; | |
331 if (--count_off_the_record_windows_) | |
332 break; // Still some windows are incognito. | |
333 OnTheRecord(true); | |
334 break; | |
335 | |
336 default: | |
337 break; | |
338 } | |
339 } | |
340 | |
341 private: | |
342 friend struct base::DefaultLazyInstanceTraits<OffTheRecordObserver>; | |
343 | |
344 OffTheRecordObserver() : count_off_the_record_windows_(0) {} | |
345 ~OffTheRecordObserver() {} | |
346 | |
347 NotificationRegistrar registrar_; | |
348 int count_off_the_record_windows_; | |
349 | |
350 DISALLOW_COPY_AND_ASSIGN(OffTheRecordObserver); | |
351 }; | |
352 | |
353 static base::LazyInstance<OffTheRecordObserver> g_off_the_record_observer( | |
354 base::LINKER_INITIALIZED); | |
355 | |
356 //------------------------------------------------------------------------------ | |
357 // This section supports the about:dns page. | |
358 //------------------------------------------------------------------------------ | |
359 | |
360 // Provide global support for the about:dns page. | |
361 void PredictorGetHtmlInfo(std::string* output) { | |
362 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
363 | |
364 if (!predictor_enabled || NULL == g_predictor) { | |
365 output->append("DNS pre-resolution and TCP pre-connection is disabled."); | |
366 } else { | |
367 if (!on_the_record_switch) { | |
368 output->append("Incognito mode is active in a window."); | |
369 } else { | |
370 // List items fetched at startup. | |
371 if (g_initial_observer) | |
372 g_initial_observer->GetFirstResolutionsHtml(output); | |
373 // Show list of subresource predictions and stats. | |
374 g_predictor->GetHtmlReferrerLists(output); | |
375 // Show list of prediction results. | |
376 g_predictor->GetHtmlInfo(output); | |
377 } | |
378 } | |
379 } | |
380 | |
381 void ClearPredictorCache() { | |
382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
383 if (!predictor_enabled || NULL == g_predictor) | |
384 return; | |
385 g_predictor->DiscardAllResults(); | |
386 } | |
387 | |
388 //------------------------------------------------------------------------------ | |
389 // This section intializes global DNS prefetch services. | |
390 //------------------------------------------------------------------------------ | |
391 | |
392 static void InitNetworkPredictor(TimeDelta max_dns_queue_delay, | |
393 size_t max_parallel_resolves, | |
394 PrefService* user_prefs, | |
395 PrefService* local_state, | |
396 bool preconnect_enabled) { | |
397 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
398 | |
399 bool prefetching_enabled = | |
400 user_prefs->GetBoolean(prefs::kNetworkPredictionEnabled); | |
401 | |
402 // Gather the list of hostnames to prefetch on startup. | |
403 UrlList urls = | |
404 GetPredictedUrlListAtStartup(user_prefs, local_state); | |
405 | |
406 ListValue* referral_list = | |
407 static_cast<ListValue*>(user_prefs->GetList( | |
408 prefs::kDnsPrefetchingHostReferralList)->DeepCopy()); | |
409 | |
410 // Remove obsolete preferences from local state if necessary. | |
411 int current_version = | |
412 local_state->GetInteger(prefs::kMultipleProfilePrefMigration); | |
413 if ((current_version & browser::DNS_PREFS) == 0) { | |
414 local_state->RegisterListPref(prefs::kDnsStartupPrefetchList, | |
415 PrefService::UNSYNCABLE_PREF); | |
416 local_state->RegisterListPref(prefs::kDnsHostReferralList, | |
417 PrefService::UNSYNCABLE_PREF); | |
418 local_state->ClearPref(prefs::kDnsStartupPrefetchList); | |
419 local_state->ClearPref(prefs::kDnsHostReferralList); | |
420 local_state->SetInteger(prefs::kMultipleProfilePrefMigration, | |
421 current_version | browser::DNS_PREFS); | |
422 } | |
423 | |
424 g_browser_process->io_thread()->InitNetworkPredictor( | |
425 prefetching_enabled, max_dns_queue_delay, max_parallel_resolves, urls, | |
426 referral_list, preconnect_enabled); | |
427 } | |
428 | |
429 void FinalizePredictorInitialization( | |
430 Predictor* global_predictor, | |
431 const UrlList& startup_urls, | |
432 ListValue* referral_list) { | |
433 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
434 g_predictor = global_predictor; | |
435 g_initial_observer = new InitialObserver(); | |
436 | |
437 // Prefetch these hostnames on startup. | |
438 DnsPrefetchMotivatedList(startup_urls, | |
439 UrlInfo::STARTUP_LIST_MOTIVATED); | |
440 g_predictor->DeserializeReferrersThenDelete(referral_list); | |
441 } | |
442 | |
443 void FreePredictorResources() { | |
444 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
445 g_predictor = NULL; // Owned and released by io_thread.cc. | |
446 delete g_initial_observer; | |
447 g_initial_observer = NULL; | |
448 } | |
449 | |
450 //------------------------------------------------------------------------------ | |
451 // Functions to handle saving of hostnames from one session to the next, to | |
452 // expedite startup times. | |
453 | |
454 static void SaveDnsPrefetchStateForNextStartupAndTrimOnIOThread( | |
455 ListValue* startup_list, | |
456 ListValue* referral_list, | |
457 base::WaitableEvent* completion) { | |
458 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
459 | |
460 if (NULL == g_predictor) { | |
461 completion->Signal(); | |
462 return; | |
463 } | |
464 | |
465 if (g_initial_observer) | |
466 g_initial_observer->GetInitialDnsResolutionList(startup_list); | |
467 | |
468 // Do at least one trim at shutdown, in case the user wasn't running long | |
469 // enough to do any regular trimming of referrers. | |
470 g_predictor->TrimReferrersNow(); | |
471 g_predictor->SerializeReferrers(referral_list); | |
472 | |
473 completion->Signal(); | |
474 } | |
475 | |
476 void SavePredictorStateForNextStartupAndTrim(PrefService* prefs) { | |
477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
478 | |
479 if (!predictor_enabled || g_predictor == NULL) | |
480 return; | |
481 | |
482 base::WaitableEvent completion(true, false); | |
483 | |
484 ListPrefUpdate update_startup_list(prefs, prefs::kDnsPrefetchingStartupList); | |
485 ListPrefUpdate update_referral_list(prefs, | |
486 prefs::kDnsPrefetchingHostReferralList); | |
487 bool posted = BrowserThread::PostTask( | |
488 BrowserThread::IO, | |
489 FROM_HERE, | |
490 NewRunnableFunction(SaveDnsPrefetchStateForNextStartupAndTrimOnIOThread, | |
491 update_startup_list.Get(), | |
492 update_referral_list.Get(), | |
493 &completion)); | |
494 | |
495 // TODO(jar): Synchronous waiting for the IO thread is a potential source | |
496 // to deadlocks and should be investigated. See http://crbug.com/78451. | |
497 DCHECK(posted); | |
498 if (posted) | |
499 completion.Wait(); | |
500 } | |
501 | |
502 static UrlList GetPredictedUrlListAtStartup(PrefService* user_prefs, | |
503 PrefService* local_state) { | |
504 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
505 UrlList urls; | |
506 // Recall list of URLs we learned about during last session. | |
507 // This may catch secondary hostnames, pulled in by the homepages. It will | |
508 // also catch more of the "primary" home pages, since that was (presumably) | |
509 // rendered first (and will be rendered first this time too). | |
510 const ListValue* startup_list = | |
511 user_prefs->GetList(prefs::kDnsPrefetchingStartupList); | |
512 | |
513 if (startup_list) { | |
514 ListValue::const_iterator it = startup_list->begin(); | |
515 int format_version = -1; | |
516 if (it != startup_list->end() && | |
517 (*it)->GetAsInteger(&format_version) && | |
518 format_version == kPredictorStartupFormatVersion) { | |
519 ++it; | |
520 for (; it != startup_list->end(); ++it) { | |
521 std::string url_spec; | |
522 if (!(*it)->GetAsString(&url_spec)) { | |
523 LOG(DFATAL); | |
524 break; // Format incompatibility. | |
525 } | |
526 GURL url(url_spec); | |
527 if (!url.has_host() || !url.has_scheme()) { | |
528 LOG(DFATAL); | |
529 break; // Format incompatibility. | |
530 } | |
531 | |
532 urls.push_back(url); | |
533 } | |
534 } | |
535 } | |
536 | |
537 // Prepare for any static home page(s) the user has in prefs. The user may | |
538 // have a LOT of tab's specified, so we may as well try to warm them all. | |
539 SessionStartupPref tab_start_pref = | |
540 SessionStartupPref::GetStartupPref(user_prefs); | |
541 if (SessionStartupPref::URLS == tab_start_pref.type) { | |
542 for (size_t i = 0; i < tab_start_pref.urls.size(); i++) { | |
543 GURL gurl = tab_start_pref.urls[i]; | |
544 if (!gurl.is_valid() || gurl.SchemeIsFile() || gurl.host().empty()) | |
545 continue; | |
546 if (gurl.SchemeIs("http") || gurl.SchemeIs("https")) | |
547 urls.push_back(gurl.GetWithEmptyPath()); | |
548 } | |
549 } | |
550 | |
551 if (urls.empty()) | |
552 urls.push_back(GURL("http://www.google.com:80")); | |
553 | |
554 return urls; | |
555 } | |
556 | |
557 //------------------------------------------------------------------------------ | |
558 // Methods for the helper class that is used to startup and teardown the whole | |
559 // g_predictor system (both DNS pre-resolution and TCP/IP pre-connection). | |
560 | |
561 PredictorInit::PredictorInit(PrefService* user_prefs, | |
562 PrefService* local_state, | |
563 bool preconnect_enabled) { | |
564 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
565 // Set up a field trial to see what disabling DNS pre-resolution does to | |
566 // latency of page loads. | |
567 base::FieldTrial::Probability kDivisor = 1000; | |
568 // For each option (i.e., non-default), we have a fixed probability. | |
569 base::FieldTrial::Probability kProbabilityPerGroup = 1; // 0.1% probability. | |
570 | |
571 // After June 30, 2011 builds, it will always be in default group | |
572 // (default_enabled_prefetch). | |
573 trial_ = new base::FieldTrial("DnsImpact", kDivisor, | |
574 "default_enabled_prefetch", 2011, 10, 30); | |
575 | |
576 // First option is to disable prefetching completely. | |
577 int disabled_prefetch = trial_->AppendGroup("disabled_prefetch", | |
578 kProbabilityPerGroup); | |
579 | |
580 // We're running two experiments at the same time. The first set of trials | |
581 // modulates the delay-time until we declare a congestion event (and purge | |
582 // our queue). The second modulates the number of concurrent resolutions | |
583 // we do at any time. Users are in exactly one trial (or the default) during | |
584 // any one run, and hence only one experiment at a time. | |
585 // Experiment 1: | |
586 // Set congestion detection at 250, 500, or 750ms, rather than the 1 second | |
587 // default. | |
588 int max_250ms_prefetch = trial_->AppendGroup("max_250ms_queue_prefetch", | |
589 kProbabilityPerGroup); | |
590 int max_500ms_prefetch = trial_->AppendGroup("max_500ms_queue_prefetch", | |
591 kProbabilityPerGroup); | |
592 int max_750ms_prefetch = trial_->AppendGroup("max_750ms_queue_prefetch", | |
593 kProbabilityPerGroup); | |
594 // Set congestion detection at 2 seconds instead of the 1 second default. | |
595 int max_2s_prefetch = trial_->AppendGroup("max_2s_queue_prefetch", | |
596 kProbabilityPerGroup); | |
597 // Experiment 2: | |
598 // Set max simultaneous resoultions to 2, 4, or 6, and scale the congestion | |
599 // limit proportionally (so we don't impact average probability of asserting | |
600 // congesion very much). | |
601 int max_2_concurrent_prefetch = trial_->AppendGroup( | |
602 "max_2 concurrent_prefetch", kProbabilityPerGroup); | |
603 int max_4_concurrent_prefetch = trial_->AppendGroup( | |
604 "max_4 concurrent_prefetch", kProbabilityPerGroup); | |
605 int max_6_concurrent_prefetch = trial_->AppendGroup( | |
606 "max_6 concurrent_prefetch", kProbabilityPerGroup); | |
607 | |
608 // We will register the incognito observer regardless of whether prefetching | |
609 // is enabled, as it is also used to clear the host cache. | |
610 g_off_the_record_observer.Get().Register(); | |
611 | |
612 if (trial_->group() != disabled_prefetch) { | |
613 // Initialize the DNS prefetch system. | |
614 size_t max_parallel_resolves = kMaxSpeculativeParallelResolves; | |
615 int max_queueing_delay_ms = kMaxSpeculativeResolveQueueDelayMs; | |
616 | |
617 if (trial_->group() == max_2_concurrent_prefetch) | |
618 max_parallel_resolves = 2; | |
619 else if (trial_->group() == max_4_concurrent_prefetch) | |
620 max_parallel_resolves = 4; | |
621 else if (trial_->group() == max_6_concurrent_prefetch) | |
622 max_parallel_resolves = 6; | |
623 | |
624 if (trial_->group() == max_250ms_prefetch) { | |
625 max_queueing_delay_ms = | |
626 (250 * kTypicalSpeculativeGroupSize) / max_parallel_resolves; | |
627 } else if (trial_->group() == max_500ms_prefetch) { | |
628 max_queueing_delay_ms = | |
629 (500 * kTypicalSpeculativeGroupSize) / max_parallel_resolves; | |
630 } else if (trial_->group() == max_750ms_prefetch) { | |
631 max_queueing_delay_ms = | |
632 (750 * kTypicalSpeculativeGroupSize) / max_parallel_resolves; | |
633 } else if (trial_->group() == max_2s_prefetch) { | |
634 max_queueing_delay_ms = | |
635 (2000 * kTypicalSpeculativeGroupSize) / max_parallel_resolves; | |
636 } | |
637 | |
638 TimeDelta max_queueing_delay( | |
639 TimeDelta::FromMilliseconds(max_queueing_delay_ms)); | |
640 | |
641 DCHECK(!g_predictor); | |
642 InitNetworkPredictor(max_queueing_delay, max_parallel_resolves, user_prefs, | |
643 local_state, preconnect_enabled); | |
644 } | |
645 } | |
646 | |
647 PredictorInit::~PredictorInit() { | |
648 } | |
649 | |
650 } // namespace chrome_browser_net | |
OLD | NEW |