OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/search/search.h" | 5 #include "chrome/browser/search/search.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/rand_util.h" | |
11 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
13 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
14 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/google/google_util.h" | 14 #include "chrome/browser/google/google_util.h" |
16 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
18 #include "chrome/browser/search/instant_service.h" | 17 #include "chrome/browser/search/instant_service.h" |
19 #include "chrome/browser/search/instant_service_factory.h" | 18 #include "chrome/browser/search/instant_service_factory.h" |
20 #include "chrome/browser/search_engines/template_url_service.h" | 19 #include "chrome/browser/search_engines/template_url_service.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 // Field trial groups should be named things like "Group7 espv:2 instant:1". | 51 // Field trial groups should be named things like "Group7 espv:2 instant:1". |
53 // The first token is always GroupN for some integer N, followed by a | 52 // The first token is always GroupN for some integer N, followed by a |
54 // space-delimited list of key:value pairs which correspond to these flags: | 53 // space-delimited list of key:value pairs which correspond to these flags: |
55 const char kEmbeddedPageVersionFlagName[] = "espv"; | 54 const char kEmbeddedPageVersionFlagName[] = "espv"; |
56 #if defined(OS_IOS) || defined(OS_ANDROID) | 55 #if defined(OS_IOS) || defined(OS_ANDROID) |
57 const uint64 kEmbeddedPageVersionDefault = 1; | 56 const uint64 kEmbeddedPageVersionDefault = 1; |
58 #else | 57 #else |
59 const uint64 kEmbeddedPageVersionDefault = 2; | 58 const uint64 kEmbeddedPageVersionDefault = 2; |
60 #endif | 59 #endif |
61 | 60 |
62 // The staleness timeout can be set (in seconds) via this config. | |
63 const char kStalePageTimeoutFlagName[] = "stale"; | |
64 const int kStalePageTimeoutDefault = 3 * 3600; // 3 hours. | |
65 | |
66 const char kHideVerbatimFlagName[] = "hide_verbatim"; | 61 const char kHideVerbatimFlagName[] = "hide_verbatim"; |
67 const char kShowNtpFlagName[] = "show_ntp"; | |
68 const char kUseCacheableNTP[] = "use_cacheable_ntp"; | |
69 const char kPrefetchSearchResultsFlagName[] = "prefetch_results"; | 62 const char kPrefetchSearchResultsFlagName[] = "prefetch_results"; |
70 const char kPrefetchSearchResultsOnSRP[] = "prefetch_results_srp"; | 63 const char kPrefetchSearchResultsOnSRP[] = "prefetch_results_srp"; |
71 const char kDisplaySearchButtonFlagName[] = "display_search_button"; | 64 const char kDisplaySearchButtonFlagName[] = "display_search_button"; |
72 const char kEnableOriginChipFlagName[] = "origin_chip"; | 65 const char kEnableOriginChipFlagName[] = "origin_chip"; |
73 #if !defined(OS_IOS) && !defined(OS_ANDROID) | 66 #if !defined(OS_IOS) && !defined(OS_ANDROID) |
74 const char kEnableQueryExtractionFlagName[] = "query_extraction"; | 67 const char kEnableQueryExtractionFlagName[] = "query_extraction"; |
75 #endif | 68 #endif |
76 | 69 |
77 // Constants for the field trial name and group prefix. | 70 // Constants for the field trial name and group prefix. |
78 // Note in M30 and below this field trial was named "InstantExtended" and in | 71 // Note in M30 and below this field trial was named "InstantExtended" and in |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
352 return url.is_valid() && | 345 return url.is_valid() && |
353 profile && | 346 profile && |
354 IsInstantExtendedAPIEnabled() && | 347 IsInstantExtendedAPIEnabled() && |
355 (url.SchemeIs(chrome::kChromeSearchScheme) || | 348 (url.SchemeIs(chrome::kChromeSearchScheme) || |
356 IsInstantURL(url, profile)); | 349 IsInstantURL(url, profile)); |
357 } | 350 } |
358 | 351 |
359 bool ShouldUseProcessPerSiteForInstantURL(const GURL& url, Profile* profile) { | 352 bool ShouldUseProcessPerSiteForInstantURL(const GURL& url, Profile* profile) { |
360 return ShouldAssignURLToInstantRenderer(url, profile) && | 353 return ShouldAssignURLToInstantRenderer(url, profile) && |
361 (url.host() == chrome::kChromeSearchLocalNtpHost || | 354 (url.host() == chrome::kChromeSearchLocalNtpHost || |
362 url.host() == chrome::kChromeSearchOnlineNtpHost); | 355 url.host() == chrome::kChromeSearchOnlineNtpHost); |
Jered
2014/01/10 16:29:41
I think we can get rid of this constant now.
samarth
2014/01/10 20:50:21
Actually this brings to mind a bug with the cachea
| |
363 } | 356 } |
364 | 357 |
365 bool IsNTPURL(const GURL& url, Profile* profile) { | 358 bool IsNTPURL(const GURL& url, Profile* profile) { |
366 if (!url.is_valid()) | 359 if (!url.is_valid()) |
367 return false; | 360 return false; |
368 | 361 |
369 if (!IsInstantExtendedAPIEnabled()) | 362 if (!IsInstantExtendedAPIEnabled()) |
370 return url == GURL(chrome::kChromeUINewTabURL); | 363 return url == GURL(chrome::kChromeUINewTabURL); |
371 | 364 |
372 return profile && | 365 return profile && |
(...skipping 15 matching lines...) Expand all Loading... | |
388 if (!contents || !entry || !IsInstantExtendedAPIEnabled()) | 381 if (!contents || !entry || !IsInstantExtendedAPIEnabled()) |
389 return false; | 382 return false; |
390 | 383 |
391 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 384 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
392 if (!IsRenderedInInstantProcess(contents, profile)) | 385 if (!IsRenderedInInstantProcess(contents, profile)) |
393 return false; | 386 return false; |
394 | 387 |
395 if (entry->GetURL() == GetLocalInstantURL(profile)) | 388 if (entry->GetURL() == GetLocalInstantURL(profile)) |
396 return true; | 389 return true; |
397 | 390 |
398 if (ShouldUseCacheableNTP()) { | 391 GURL new_tab_url(GetNewTabPageURL(profile)); |
399 GURL new_tab_url(GetNewTabPageURL(profile)); | 392 return new_tab_url.is_valid() && |
400 return new_tab_url.is_valid() && | 393 search::MatchesOriginAndPath(entry->GetURL(), new_tab_url); |
401 search::MatchesOriginAndPath(entry->GetURL(), new_tab_url); | |
402 } | |
403 | |
404 return IsInstantURL(entry->GetVirtualURL(), profile) && | |
405 GetSearchTermsImpl(contents, entry).empty(); | |
406 } | 394 } |
407 | 395 |
408 bool IsSuggestPrefEnabled(Profile* profile) { | 396 bool IsSuggestPrefEnabled(Profile* profile) { |
409 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && | 397 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && |
410 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled); | 398 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled); |
411 } | 399 } |
412 | 400 |
413 GURL GetInstantURL(Profile* profile, int start_margin, | 401 GURL GetInstantURL(Profile* profile, int start_margin, |
414 bool force_instant_results) { | 402 bool force_instant_results) { |
415 if (!IsInstantExtendedAPIEnabled() || !IsSuggestPrefEnabled(profile)) | 403 if (!IsInstantExtendedAPIEnabled() || !IsSuggestPrefEnabled(profile)) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
451 return result; | 439 return result; |
452 for (size_t i = 0; i < template_url->URLCount(); ++i) { | 440 for (size_t i = 0; i < template_url->URLCount(); ++i) { |
453 TemplateURLRef ref(template_url, i); | 441 TemplateURLRef ref(template_url, i); |
454 result.push_back(TemplateURLRefToGURL(ref, kDisableStartMargin, false, | 442 result.push_back(TemplateURLRefToGURL(ref, kDisableStartMargin, false, |
455 false)); | 443 false)); |
456 } | 444 } |
457 return result; | 445 return result; |
458 } | 446 } |
459 | 447 |
460 GURL GetNewTabPageURL(Profile* profile) { | 448 GURL GetNewTabPageURL(Profile* profile) { |
461 if (!ShouldUseCacheableNTP()) | |
462 return GURL(); | |
463 | |
464 if (!profile || profile->IsOffTheRecord()) | 449 if (!profile || profile->IsOffTheRecord()) |
465 return GURL(); | 450 return GURL(); |
466 | 451 |
467 if (!IsSuggestPrefEnabled(profile)) | 452 if (!IsSuggestPrefEnabled(profile)) |
468 return GURL(chrome::kChromeSearchLocalNtpUrl); | 453 return GURL(chrome::kChromeSearchLocalNtpUrl); |
469 | 454 |
470 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 455 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
471 if (!template_url) | 456 if (!template_url) |
472 return GURL(chrome::kChromeSearchLocalNtpUrl); | 457 return GURL(chrome::kChromeSearchLocalNtpUrl); |
473 | 458 |
474 GURL url(TemplateURLRefToGURL(template_url->new_tab_url_ref(), | 459 GURL url(TemplateURLRefToGURL(template_url->new_tab_url_ref(), |
475 kDisableStartMargin, false, false)); | 460 kDisableStartMargin, false, false)); |
476 if (!url.is_valid() || !url.SchemeIsSecure()) | 461 if (!url.is_valid() || !url.SchemeIsSecure()) |
477 return GURL(chrome::kChromeSearchLocalNtpUrl); | 462 return GURL(chrome::kChromeSearchLocalNtpUrl); |
478 | 463 |
479 if (!IsURLAllowedForSupervisedUser(url, profile)) | 464 if (!IsURLAllowedForSupervisedUser(url, profile)) |
480 return GURL(chrome::kChromeSearchLocalNtpUrl); | 465 return GURL(chrome::kChromeSearchLocalNtpUrl); |
481 | 466 |
482 return url; | 467 return url; |
483 } | 468 } |
484 | 469 |
485 GURL GetSearchResultPrefetchBaseURL(Profile* profile) { | 470 GURL GetSearchResultPrefetchBaseURL(Profile* profile) { |
486 return ShouldPrefetchSearchResults() ? | 471 return ShouldPrefetchSearchResults() ? |
487 GetInstantURL(profile, kDisableStartMargin, true) : GURL(); | 472 GetInstantURL(profile, kDisableStartMargin, true) : GURL(); |
488 } | 473 } |
489 | 474 |
490 bool ShouldPrefetchSearchResults() { | 475 bool ShouldPrefetchSearchResults() { |
491 if (!ShouldUseCacheableNTP()) | |
492 return false; | |
493 | |
494 FieldTrialFlags flags; | 476 FieldTrialFlags flags; |
495 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( | 477 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( |
496 kPrefetchSearchResultsFlagName, false, flags); | 478 kPrefetchSearchResultsFlagName, false, flags); |
497 } | 479 } |
498 | 480 |
499 GURL GetLocalInstantURL(Profile* profile) { | 481 GURL GetLocalInstantURL(Profile* profile) { |
500 return GURL(chrome::kChromeSearchLocalNtpUrl); | 482 return GURL(chrome::kChromeSearchLocalNtpUrl); |
501 } | 483 } |
502 | 484 |
503 bool ShouldHideTopVerbatimMatch() { | 485 bool ShouldHideTopVerbatimMatch() { |
504 FieldTrialFlags flags; | 486 FieldTrialFlags flags; |
505 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( | 487 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( |
506 kHideVerbatimFlagName, false, flags); | 488 kHideVerbatimFlagName, false, flags); |
507 } | 489 } |
508 | 490 |
509 bool ShouldUseCacheableNTP() { | |
510 FieldTrialFlags flags; | |
511 return !GetFieldTrialInfo(&flags) || GetBoolValueForFlagWithDefault( | |
512 kUseCacheableNTP, true, flags); | |
513 } | |
514 | |
515 bool ShouldShowInstantNTP() { | |
516 // If using the cacheable NTP, load the NTP directly instead of preloading its | |
517 // contents using InstantNTP. | |
518 if (ShouldUseCacheableNTP()) | |
519 return false; | |
520 | |
521 FieldTrialFlags flags; | |
522 return !GetFieldTrialInfo(&flags) || | |
523 GetBoolValueForFlagWithDefault(kShowNtpFlagName, true, flags); | |
524 } | |
525 | |
526 DisplaySearchButtonConditions GetDisplaySearchButtonConditions() { | 491 DisplaySearchButtonConditions GetDisplaySearchButtonConditions() { |
527 const CommandLine* cl = CommandLine::ForCurrentProcess(); | 492 const CommandLine* cl = CommandLine::ForCurrentProcess(); |
528 if (cl->HasSwitch(switches::kDisableSearchButtonInOmnibox)) { | 493 if (cl->HasSwitch(switches::kDisableSearchButtonInOmnibox)) { |
529 return DISPLAY_SEARCH_BUTTON_NEVER; | 494 return DISPLAY_SEARCH_BUTTON_NEVER; |
530 } else if (cl->HasSwitch(switches::kEnableSearchButtonInOmniboxForStr)) { | 495 } else if (cl->HasSwitch(switches::kEnableSearchButtonInOmniboxForStr)) { |
531 return DISPLAY_SEARCH_BUTTON_FOR_STR; | 496 return DISPLAY_SEARCH_BUTTON_FOR_STR; |
532 } else if (cl->HasSwitch(switches::kEnableSearchButtonInOmniboxForStrOrIip)) { | 497 } else if (cl->HasSwitch(switches::kEnableSearchButtonInOmniboxForStrOrIip)) { |
533 return DISPLAY_SEARCH_BUTTON_FOR_STR_OR_IIP; | 498 return DISPLAY_SEARCH_BUTTON_FOR_STR_OR_IIP; |
534 } else if (cl->HasSwitch(switches::kEnableSearchButtonInOmniboxAlways)) { | 499 } else if (cl->HasSwitch(switches::kEnableSearchButtonInOmniboxAlways)) { |
535 return DISPLAY_SEARCH_BUTTON_ALWAYS; | 500 return DISPLAY_SEARCH_BUTTON_ALWAYS; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
568 GURL effective_url(url); | 533 GURL effective_url(url); |
569 | 534 |
570 // Replace the scheme with "chrome-search:". | 535 // Replace the scheme with "chrome-search:". |
571 url_canon::Replacements<char> replacements; | 536 url_canon::Replacements<char> replacements; |
572 std::string search_scheme(chrome::kChromeSearchScheme); | 537 std::string search_scheme(chrome::kChromeSearchScheme); |
573 replacements.SetScheme(search_scheme.data(), | 538 replacements.SetScheme(search_scheme.data(), |
574 url_parse::Component(0, search_scheme.length())); | 539 url_parse::Component(0, search_scheme.length())); |
575 | 540 |
576 // If the URL corresponds to an online NTP, replace the host with | 541 // If the URL corresponds to an online NTP, replace the host with |
577 // "online-ntp". | 542 // "online-ntp". |
578 std::string online_ntp_host(chrome::kChromeSearchOnlineNtpHost); | 543 std::string online_ntp_host(chrome::kChromeSearchOnlineNtpHost); |
Jered
2014/01/10 16:29:41
Here, too.
samarth
2014/01/10 20:50:21
Same.
| |
579 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 544 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
580 if (template_url) { | 545 if (template_url) { |
581 const GURL instant_url = TemplateURLRefToGURL( | 546 const GURL instant_url = TemplateURLRefToGURL( |
582 template_url->instant_url_ref(), kDisableStartMargin, false, false); | 547 template_url->instant_url_ref(), kDisableStartMargin, false, false); |
583 if (instant_url.is_valid() && | 548 if (instant_url.is_valid() && |
584 search::MatchesOriginAndPath(url, instant_url)) { | 549 search::MatchesOriginAndPath(url, instant_url)) { |
585 replacements.SetHost(online_ntp_host.c_str(), | 550 replacements.SetHost(online_ntp_host.c_str(), |
586 url_parse::Component(0, online_ntp_host.length())); | 551 url_parse::Component(0, online_ntp_host.length())); |
587 } | 552 } |
588 } | 553 } |
589 | 554 |
590 effective_url = effective_url.ReplaceComponents(replacements); | 555 effective_url = effective_url.ReplaceComponents(replacements); |
591 return effective_url; | 556 return effective_url; |
592 } | 557 } |
593 | 558 |
594 int GetInstantLoaderStalenessTimeoutSec() { | |
595 int timeout_sec = kStalePageTimeoutDefault; | |
596 FieldTrialFlags flags; | |
597 if (GetFieldTrialInfo(&flags)) { | |
598 timeout_sec = GetUInt64ValueForFlagWithDefault(kStalePageTimeoutFlagName, | |
599 kStalePageTimeoutDefault, | |
600 flags); | |
601 } | |
602 | |
603 // Require a minimum 5 minute timeout. | |
604 if (timeout_sec < 0 || (timeout_sec > 0 && timeout_sec < 300)) | |
605 timeout_sec = kStalePageTimeoutDefault; | |
606 | |
607 // Randomize by upto 15% either side. | |
608 timeout_sec = base::RandInt(timeout_sec * 0.85, timeout_sec * 1.15); | |
609 | |
610 return timeout_sec; | |
611 } | |
612 | |
613 bool IsPreloadedInstantExtendedNTP(const content::WebContents* contents) { | |
614 if (!IsInstantExtendedAPIEnabled()) | |
615 return false; | |
616 | |
617 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
618 if (!profile_manager) | |
619 return false; // The profile manager can be NULL while testing. | |
620 | |
621 const std::vector<Profile*>& profiles = profile_manager->GetLoadedProfiles(); | |
622 for (size_t i = 0; i < profiles.size(); ++i) { | |
623 const InstantService* instant_service = | |
624 InstantServiceFactory::GetForProfile(profiles[i]); | |
625 if (instant_service && instant_service->GetNTPContents() == contents) | |
626 return true; | |
627 } | |
628 return false; | |
629 } | |
630 | |
631 bool HandleNewTabURLRewrite(GURL* url, | 559 bool HandleNewTabURLRewrite(GURL* url, |
632 content::BrowserContext* browser_context) { | 560 content::BrowserContext* browser_context) { |
633 if (!IsInstantExtendedAPIEnabled()) | 561 if (!IsInstantExtendedAPIEnabled()) |
634 return false; | 562 return false; |
635 | 563 |
636 if (!url->SchemeIs(chrome::kChromeUIScheme) || | 564 if (!url->SchemeIs(chrome::kChromeUIScheme) || |
637 url->host() != chrome::kChromeUINewTabHost) | 565 url->host() != chrome::kChromeUINewTabHost) |
638 return false; | 566 return false; |
639 | 567 |
640 Profile* profile = Profile::FromBrowserContext(browser_context); | 568 Profile* profile = Profile::FromBrowserContext(browser_context); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
747 | 675 |
748 // Given a FieldTrialFlags object, returns the boolean value of the provided | 676 // Given a FieldTrialFlags object, returns the boolean value of the provided |
749 // flag. | 677 // flag. |
750 bool GetBoolValueForFlagWithDefault(const std::string& flag, | 678 bool GetBoolValueForFlagWithDefault(const std::string& flag, |
751 bool default_value, | 679 bool default_value, |
752 const FieldTrialFlags& flags) { | 680 const FieldTrialFlags& flags) { |
753 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); | 681 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
754 } | 682 } |
755 | 683 |
756 } // namespace chrome | 684 } // namespace chrome |
OLD | NEW |