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( | |
273 new base::FundamentalValue(kPredictorStartupFormatVersion)); | |
274 for (FirstNavigations::iterator it = first_navigations_.begin(); | |
275 it != first_navigations_.end(); | |
276 ++it) { | |
277 DCHECK(it->first == Predictor::CanonicalizeUrl(it->first)); | |
278 startup_list->Append(new StringValue(it->first.spec())); | |
279 } | |
280 } | |
281 | |
282 void InitialObserver::GetFirstResolutionsHtml(std::string* output) { | |
283 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
284 | |
285 UrlInfo::UrlInfoTable resolution_list; | |
286 { | |
287 for (FirstNavigations::iterator it(first_navigations_.begin()); | |
288 it != first_navigations_.end(); | |
289 it++) { | |
290 UrlInfo info; | |
291 info.SetUrl(it->first); | |
292 info.set_time(it->second); | |
293 resolution_list.push_back(info); | |
294 } | |
295 } | |
296 UrlInfo::GetHtmlTable(resolution_list, | |
297 "Future startups will prefetch DNS records for ", false, output); | |
298 } | |
299 | |
300 //------------------------------------------------------------------------------ | |
301 // Support observer to detect opening and closing of OffTheRecord windows. | |
302 // This object lives on the UI thread. | |
303 | |
304 class OffTheRecordObserver : public NotificationObserver { | |
305 public: | |
306 void Register() { | |
307 // TODO(pkasting): This test should not be necessary. See crbug.com/12475. | |
308 if (registrar_.IsEmpty()) { | |
309 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED, | |
310 NotificationService::AllSources()); | |
311 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED, | |
312 NotificationService::AllSources()); | |
313 } | |
314 } | |
315 | |
316 void Observe(int type, const NotificationSource& source, | |
317 const NotificationDetails& details) { | |
318 switch (type) { | |
319 case chrome::NOTIFICATION_BROWSER_OPENED: | |
320 if (!Source<Browser>(source)->profile()->IsOffTheRecord()) | |
321 break; | |
322 ++count_off_the_record_windows_; | |
323 OnTheRecord(false); | |
324 break; | |
325 | |
326 case chrome::NOTIFICATION_BROWSER_CLOSED: | |
327 if (!Source<Browser>(source)->profile()->IsOffTheRecord()) | |
328 break; // Ignore ordinary windows. | |
329 DCHECK_LT(0, count_off_the_record_windows_); | |
330 if (0 >= count_off_the_record_windows_) // Defensive coding. | |
331 break; | |
332 if (--count_off_the_record_windows_) | |
333 break; // Still some windows are incognito. | |
334 OnTheRecord(true); | |
335 break; | |
336 | |
337 default: | |
338 break; | |
339 } | |
340 } | |
341 | |
342 private: | |
343 friend struct base::DefaultLazyInstanceTraits<OffTheRecordObserver>; | |
344 | |
345 OffTheRecordObserver() : count_off_the_record_windows_(0) {} | |
346 ~OffTheRecordObserver() {} | |
347 | |
348 NotificationRegistrar registrar_; | |
349 int count_off_the_record_windows_; | |
350 | |
351 DISALLOW_COPY_AND_ASSIGN(OffTheRecordObserver); | |
352 }; | |
353 | |
354 static base::LazyInstance<OffTheRecordObserver> g_off_the_record_observer( | |
355 base::LINKER_INITIALIZED); | |
356 | |
357 //------------------------------------------------------------------------------ | |
358 // This section supports the about:dns page. | |
359 //------------------------------------------------------------------------------ | |
360 | |
361 // Provide global support for the about:dns page. | |
362 void PredictorGetHtmlInfo(std::string* output) { | |
363 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
364 | |
365 if (!predictor_enabled || NULL == g_predictor) { | |
366 output->append("DNS pre-resolution and TCP pre-connection is disabled."); | |
367 } else { | |
368 if (!on_the_record_switch) { | |
369 output->append("Incognito mode is active in a window."); | |
370 } else { | |
371 // List items fetched at startup. | |
372 if (g_initial_observer) | |
373 g_initial_observer->GetFirstResolutionsHtml(output); | |
374 // Show list of subresource predictions and stats. | |
375 g_predictor->GetHtmlReferrerLists(output); | |
376 // Show list of prediction results. | |
377 g_predictor->GetHtmlInfo(output); | |
378 } | |
379 } | |
380 } | |
381 | |
382 void ClearPredictorCache() { | |
383 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
384 if (!predictor_enabled || NULL == g_predictor) | |
385 return; | |
386 g_predictor->DiscardAllResults(); | |
387 } | |
388 | |
389 //------------------------------------------------------------------------------ | |
390 // This section intializes global DNS prefetch services. | |
391 //------------------------------------------------------------------------------ | |
392 | |
393 static void InitNetworkPredictor(TimeDelta max_dns_queue_delay, | |
394 size_t max_parallel_resolves, | |
395 PrefService* user_prefs, | |
396 PrefService* local_state, | |
397 bool preconnect_enabled) { | |
398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
399 | |
400 bool prefetching_enabled = | |
401 user_prefs->GetBoolean(prefs::kNetworkPredictionEnabled); | |
402 | |
403 // Gather the list of hostnames to prefetch on startup. | |
404 UrlList urls = | |
405 GetPredictedUrlListAtStartup(user_prefs, local_state); | |
406 | |
407 ListValue* referral_list = | |
408 static_cast<ListValue*>(user_prefs->GetList( | |
409 prefs::kDnsPrefetchingHostReferralList)->DeepCopy()); | |
410 | |
411 // Remove obsolete preferences from local state if necessary. | |
412 int current_version = | |
413 local_state->GetInteger(prefs::kMultipleProfilePrefMigration); | |
414 if ((current_version & browser::DNS_PREFS) == 0) { | |
415 local_state->RegisterListPref(prefs::kDnsStartupPrefetchList, | |
416 PrefService::UNSYNCABLE_PREF); | |
417 local_state->RegisterListPref(prefs::kDnsHostReferralList, | |
418 PrefService::UNSYNCABLE_PREF); | |
419 local_state->ClearPref(prefs::kDnsStartupPrefetchList); | |
420 local_state->ClearPref(prefs::kDnsHostReferralList); | |
421 local_state->SetInteger(prefs::kMultipleProfilePrefMigration, | |
422 current_version | browser::DNS_PREFS); | |
423 } | |
424 | |
425 g_browser_process->io_thread()->InitNetworkPredictor( | |
426 prefetching_enabled, max_dns_queue_delay, max_parallel_resolves, urls, | |
427 referral_list, preconnect_enabled); | |
428 } | |
429 | |
430 void FinalizePredictorInitialization( | |
431 Predictor* global_predictor, | |
432 const UrlList& startup_urls, | |
433 ListValue* referral_list) { | |
434 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
435 g_predictor = global_predictor; | |
436 g_initial_observer = new InitialObserver(); | |
437 | |
438 // Prefetch these hostnames on startup. | |
439 DnsPrefetchMotivatedList(startup_urls, | |
440 UrlInfo::STARTUP_LIST_MOTIVATED); | |
441 g_predictor->DeserializeReferrersThenDelete(referral_list); | |
442 } | |
443 | |
444 void FreePredictorResources() { | |
445 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
446 g_predictor = NULL; // Owned and released by io_thread.cc. | |
447 delete g_initial_observer; | |
448 g_initial_observer = NULL; | |
449 } | |
450 | |
451 //------------------------------------------------------------------------------ | |
452 // Functions to handle saving of hostnames from one session to the next, to | |
453 // expedite startup times. | |
454 | |
455 static void SaveDnsPrefetchStateForNextStartupAndTrimOnIOThread( | |
456 ListValue* startup_list, | |
457 ListValue* referral_list, | |
458 base::WaitableEvent* completion) { | |
459 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
460 | |
461 if (NULL == g_predictor) { | |
462 completion->Signal(); | |
463 return; | |
464 } | |
465 | |
466 if (g_initial_observer) | |
467 g_initial_observer->GetInitialDnsResolutionList(startup_list); | |
468 | |
469 // Do at least one trim at shutdown, in case the user wasn't running long | |
470 // enough to do any regular trimming of referrers. | |
471 g_predictor->TrimReferrersNow(); | |
472 g_predictor->SerializeReferrers(referral_list); | |
473 | |
474 completion->Signal(); | |
475 } | |
476 | |
477 void SavePredictorStateForNextStartupAndTrim(PrefService* prefs) { | |
478 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
479 | |
480 if (!predictor_enabled || g_predictor == NULL) | |
481 return; | |
482 | |
483 base::WaitableEvent completion(true, false); | |
484 | |
485 ListPrefUpdate update_startup_list(prefs, prefs::kDnsPrefetchingStartupList); | |
486 ListPrefUpdate update_referral_list(prefs, | |
487 prefs::kDnsPrefetchingHostReferralList); | |
488 bool posted = BrowserThread::PostTask( | |
489 BrowserThread::IO, | |
490 FROM_HERE, | |
491 NewRunnableFunction(SaveDnsPrefetchStateForNextStartupAndTrimOnIOThread, | |
492 update_startup_list.Get(), | |
493 update_referral_list.Get(), | |
494 &completion)); | |
495 | |
496 // TODO(jar): Synchronous waiting for the IO thread is a potential source | |
497 // to deadlocks and should be investigated. See http://crbug.com/78451. | |
498 DCHECK(posted); | |
499 if (posted) | |
500 completion.Wait(); | |
501 } | |
502 | |
503 static UrlList GetPredictedUrlListAtStartup(PrefService* user_prefs, | |
504 PrefService* local_state) { | |
505 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
506 UrlList urls; | |
507 // Recall list of URLs we learned about during last session. | |
508 // This may catch secondary hostnames, pulled in by the homepages. It will | |
509 // also catch more of the "primary" home pages, since that was (presumably) | |
510 // rendered first (and will be rendered first this time too). | |
511 const ListValue* startup_list = | |
512 user_prefs->GetList(prefs::kDnsPrefetchingStartupList); | |
513 | |
514 if (startup_list) { | |
515 ListValue::const_iterator it = startup_list->begin(); | |
516 int format_version = -1; | |
517 if (it != startup_list->end() && | |
518 (*it)->GetAsInteger(&format_version) && | |
519 format_version == kPredictorStartupFormatVersion) { | |
520 ++it; | |
521 for (; it != startup_list->end(); ++it) { | |
522 std::string url_spec; | |
523 if (!(*it)->GetAsString(&url_spec)) { | |
524 LOG(DFATAL); | |
525 break; // Format incompatibility. | |
526 } | |
527 GURL url(url_spec); | |
528 if (!url.has_host() || !url.has_scheme()) { | |
529 LOG(DFATAL); | |
530 break; // Format incompatibility. | |
531 } | |
532 | |
533 urls.push_back(url); | |
534 } | |
535 } | |
536 } | |
537 | |
538 // Prepare for any static home page(s) the user has in prefs. The user may | |
539 // have a LOT of tab's specified, so we may as well try to warm them all. | |
540 SessionStartupPref tab_start_pref = | |
541 SessionStartupPref::GetStartupPref(user_prefs); | |
542 if (SessionStartupPref::URLS == tab_start_pref.type) { | |
543 for (size_t i = 0; i < tab_start_pref.urls.size(); i++) { | |
544 GURL gurl = tab_start_pref.urls[i]; | |
545 if (!gurl.is_valid() || gurl.SchemeIsFile() || gurl.host().empty()) | |
546 continue; | |
547 if (gurl.SchemeIs("http") || gurl.SchemeIs("https")) | |
548 urls.push_back(gurl.GetWithEmptyPath()); | |
549 } | |
550 } | |
551 | |
552 if (urls.empty()) | |
553 urls.push_back(GURL("http://www.google.com:80")); | |
554 | |
555 return urls; | |
556 } | |
557 | |
558 //------------------------------------------------------------------------------ | |
559 // Methods for the helper class that is used to startup and teardown the whole | |
560 // g_predictor system (both DNS pre-resolution and TCP/IP pre-connection). | |
561 | |
562 PredictorInit::PredictorInit(PrefService* user_prefs, | |
563 PrefService* local_state, | |
564 bool preconnect_enabled) { | |
565 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
566 // Set up a field trial to see what disabling DNS pre-resolution does to | |
567 // latency of page loads. | |
568 base::FieldTrial::Probability kDivisor = 1000; | |
569 // For each option (i.e., non-default), we have a fixed probability. | |
570 base::FieldTrial::Probability kProbabilityPerGroup = 1; // 0.1% probability. | |
571 | |
572 // After June 30, 2011 builds, it will always be in default group | |
573 // (default_enabled_prefetch). | |
574 trial_ = new base::FieldTrial("DnsImpact", kDivisor, | |
575 "default_enabled_prefetch", 2011, 10, 30); | |
576 | |
577 // First option is to disable prefetching completely. | |
578 int disabled_prefetch = trial_->AppendGroup("disabled_prefetch", | |
579 kProbabilityPerGroup); | |
580 | |
581 // We're running two experiments at the same time. The first set of trials | |
582 // modulates the delay-time until we declare a congestion event (and purge | |
583 // our queue). The second modulates the number of concurrent resolutions | |
584 // we do at any time. Users are in exactly one trial (or the default) during | |
585 // any one run, and hence only one experiment at a time. | |
586 // Experiment 1: | |
587 // Set congestion detection at 250, 500, or 750ms, rather than the 1 second | |
588 // default. | |
589 int max_250ms_prefetch = trial_->AppendGroup("max_250ms_queue_prefetch", | |
590 kProbabilityPerGroup); | |
591 int max_500ms_prefetch = trial_->AppendGroup("max_500ms_queue_prefetch", | |
592 kProbabilityPerGroup); | |
593 int max_750ms_prefetch = trial_->AppendGroup("max_750ms_queue_prefetch", | |
594 kProbabilityPerGroup); | |
595 // Set congestion detection at 2 seconds instead of the 1 second default. | |
596 int max_2s_prefetch = trial_->AppendGroup("max_2s_queue_prefetch", | |
597 kProbabilityPerGroup); | |
598 // Experiment 2: | |
599 // Set max simultaneous resoultions to 2, 4, or 6, and scale the congestion | |
600 // limit proportionally (so we don't impact average probability of asserting | |
601 // congesion very much). | |
602 int max_2_concurrent_prefetch = trial_->AppendGroup( | |
603 "max_2 concurrent_prefetch", kProbabilityPerGroup); | |
604 int max_4_concurrent_prefetch = trial_->AppendGroup( | |
605 "max_4 concurrent_prefetch", kProbabilityPerGroup); | |
606 int max_6_concurrent_prefetch = trial_->AppendGroup( | |
607 "max_6 concurrent_prefetch", kProbabilityPerGroup); | |
608 | |
609 // We will register the incognito observer regardless of whether prefetching | |
610 // is enabled, as it is also used to clear the host cache. | |
611 g_off_the_record_observer.Get().Register(); | |
612 | |
613 if (trial_->group() != disabled_prefetch) { | |
614 // Initialize the DNS prefetch system. | |
615 size_t max_parallel_resolves = kMaxSpeculativeParallelResolves; | |
616 int max_queueing_delay_ms = kMaxSpeculativeResolveQueueDelayMs; | |
617 | |
618 if (trial_->group() == max_2_concurrent_prefetch) | |
619 max_parallel_resolves = 2; | |
620 else if (trial_->group() == max_4_concurrent_prefetch) | |
621 max_parallel_resolves = 4; | |
622 else if (trial_->group() == max_6_concurrent_prefetch) | |
623 max_parallel_resolves = 6; | |
624 | |
625 if (trial_->group() == max_250ms_prefetch) { | |
626 max_queueing_delay_ms = | |
627 (250 * kTypicalSpeculativeGroupSize) / max_parallel_resolves; | |
628 } else if (trial_->group() == max_500ms_prefetch) { | |
629 max_queueing_delay_ms = | |
630 (500 * kTypicalSpeculativeGroupSize) / max_parallel_resolves; | |
631 } else if (trial_->group() == max_750ms_prefetch) { | |
632 max_queueing_delay_ms = | |
633 (750 * kTypicalSpeculativeGroupSize) / max_parallel_resolves; | |
634 } else if (trial_->group() == max_2s_prefetch) { | |
635 max_queueing_delay_ms = | |
636 (2000 * kTypicalSpeculativeGroupSize) / max_parallel_resolves; | |
637 } | |
638 | |
639 TimeDelta max_queueing_delay( | |
640 TimeDelta::FromMilliseconds(max_queueing_delay_ms)); | |
641 | |
642 DCHECK(!g_predictor); | |
643 InitNetworkPredictor(max_queueing_delay, max_parallel_resolves, user_prefs, | |
644 local_state, preconnect_enabled); | |
645 } | |
646 } | |
647 | |
648 PredictorInit::~PredictorInit() { | |
649 } | |
650 | |
651 } // namespace chrome_browser_net | |
OLD | NEW |