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 kOriginChipFlagName[] = "origin_chip"; | 65 const char kOriginChipFlagName[] = "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 309 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 search::MatchesOriginAndPath(url, instant_url)) { | 563 search::MatchesOriginAndPath(url, instant_url)) { |
599 replacements.SetHost(online_ntp_host.c_str(), | 564 replacements.SetHost(online_ntp_host.c_str(), |
600 url_parse::Component(0, online_ntp_host.length())); | 565 url_parse::Component(0, online_ntp_host.length())); |
601 } | 566 } |
602 } | 567 } |
603 | 568 |
604 effective_url = effective_url.ReplaceComponents(replacements); | 569 effective_url = effective_url.ReplaceComponents(replacements); |
605 return effective_url; | 570 return effective_url; |
606 } | 571 } |
607 | 572 |
608 int GetInstantLoaderStalenessTimeoutSec() { | |
609 int timeout_sec = kStalePageTimeoutDefault; | |
610 FieldTrialFlags flags; | |
611 if (GetFieldTrialInfo(&flags)) { | |
612 timeout_sec = GetUInt64ValueForFlagWithDefault(kStalePageTimeoutFlagName, | |
613 kStalePageTimeoutDefault, | |
614 flags); | |
615 } | |
616 | |
617 // Require a minimum 5 minute timeout. | |
618 if (timeout_sec < 0 || (timeout_sec > 0 && timeout_sec < 300)) | |
619 timeout_sec = kStalePageTimeoutDefault; | |
620 | |
621 // Randomize by upto 15% either side. | |
622 timeout_sec = base::RandInt(timeout_sec * 0.85, timeout_sec * 1.15); | |
623 | |
624 return timeout_sec; | |
625 } | |
626 | |
627 bool IsPreloadedInstantExtendedNTP(const content::WebContents* contents) { | |
628 if (!IsInstantExtendedAPIEnabled()) | |
629 return false; | |
630 | |
631 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
632 if (!profile_manager) | |
633 return false; // The profile manager can be NULL while testing. | |
634 | |
635 const std::vector<Profile*>& profiles = profile_manager->GetLoadedProfiles(); | |
636 for (size_t i = 0; i < profiles.size(); ++i) { | |
637 const InstantService* instant_service = | |
638 InstantServiceFactory::GetForProfile(profiles[i]); | |
639 if (instant_service && instant_service->GetNTPContents() == contents) | |
640 return true; | |
641 } | |
642 return false; | |
643 } | |
644 | |
645 bool HandleNewTabURLRewrite(GURL* url, | 573 bool HandleNewTabURLRewrite(GURL* url, |
646 content::BrowserContext* browser_context) { | 574 content::BrowserContext* browser_context) { |
647 if (!IsInstantExtendedAPIEnabled()) | 575 if (!IsInstantExtendedAPIEnabled()) |
648 return false; | 576 return false; |
649 | 577 |
650 if (!url->SchemeIs(chrome::kChromeUIScheme) || | 578 if (!url->SchemeIs(chrome::kChromeUIScheme) || |
651 url->host() != chrome::kChromeUINewTabHost) | 579 url->host() != chrome::kChromeUINewTabHost) |
652 return false; | 580 return false; |
653 | 581 |
654 Profile* profile = Profile::FromBrowserContext(browser_context); | 582 Profile* profile = Profile::FromBrowserContext(browser_context); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 | 689 |
762 // Given a FieldTrialFlags object, returns the boolean value of the provided | 690 // Given a FieldTrialFlags object, returns the boolean value of the provided |
763 // flag. | 691 // flag. |
764 bool GetBoolValueForFlagWithDefault(const std::string& flag, | 692 bool GetBoolValueForFlagWithDefault(const std::string& flag, |
765 bool default_value, | 693 bool default_value, |
766 const FieldTrialFlags& flags) { | 694 const FieldTrialFlags& flags) { |
767 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); | 695 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
768 } | 696 } |
769 | 697 |
770 } // namespace chrome | 698 } // namespace chrome |
OLD | NEW |