Chromium Code Reviews| 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" | 10 #include "base/rand_util.h" |
|
kmadhusu
2014/01/09 23:31:43
This can be removed.
samarth
2014/01/10 05:13:32
Done.
| |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/google/google_util.h" | 15 #include "chrome/browser/google/google_util.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
| 18 #include "chrome/browser/search/instant_service.h" | 18 #include "chrome/browser/search/instant_service.h" |
| 19 #include "chrome/browser/search/instant_service_factory.h" | 19 #include "chrome/browser/search/instant_service_factory.h" |
| 20 #include "chrome/browser/search_engines/template_url_service.h" | 20 #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". | 52 // 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 | 53 // 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: | 54 // space-delimited list of key:value pairs which correspond to these flags: |
| 55 const char kEmbeddedPageVersionFlagName[] = "espv"; | 55 const char kEmbeddedPageVersionFlagName[] = "espv"; |
| 56 #if defined(OS_IOS) || defined(OS_ANDROID) | 56 #if defined(OS_IOS) || defined(OS_ANDROID) |
| 57 const uint64 kEmbeddedPageVersionDefault = 1; | 57 const uint64 kEmbeddedPageVersionDefault = 1; |
| 58 #else | 58 #else |
| 59 const uint64 kEmbeddedPageVersionDefault = 2; | 59 const uint64 kEmbeddedPageVersionDefault = 2; |
| 60 #endif | 60 #endif |
| 61 | 61 |
| 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"; | 62 const char kHideVerbatimFlagName[] = "hide_verbatim"; |
| 67 const char kShowNtpFlagName[] = "show_ntp"; | |
| 68 const char kUseCacheableNTP[] = "use_cacheable_ntp"; | |
| 69 const char kPrefetchSearchResultsFlagName[] = "prefetch_results"; | 63 const char kPrefetchSearchResultsFlagName[] = "prefetch_results"; |
| 70 const char kPrefetchSearchResultsOnSRP[] = "prefetch_results_srp"; | 64 const char kPrefetchSearchResultsOnSRP[] = "prefetch_results_srp"; |
| 71 const char kDisplaySearchButtonFlagName[] = "display_search_button"; | 65 const char kDisplaySearchButtonFlagName[] = "display_search_button"; |
| 72 const char kEnableOriginChipFlagName[] = "origin_chip"; | 66 const char kEnableOriginChipFlagName[] = "origin_chip"; |
| 73 #if !defined(OS_IOS) && !defined(OS_ANDROID) | 67 #if !defined(OS_IOS) && !defined(OS_ANDROID) |
| 74 const char kEnableQueryExtractionFlagName[] = "query_extraction"; | 68 const char kEnableQueryExtractionFlagName[] = "query_extraction"; |
| 75 #endif | 69 #endif |
| 76 | 70 |
| 77 // Constants for the field trial name and group prefix. | 71 // Constants for the field trial name and group prefix. |
| 78 // Note in M30 and below this field trial was named "InstantExtended" and in | 72 // 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()) | 382 if (!contents || !entry || !IsInstantExtendedAPIEnabled()) |
| 389 return false; | 383 return false; |
| 390 | 384 |
| 391 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 385 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 392 if (!IsRenderedInInstantProcess(contents, profile)) | 386 if (!IsRenderedInInstantProcess(contents, profile)) |
| 393 return false; | 387 return false; |
| 394 | 388 |
| 395 if (entry->GetURL() == GetLocalInstantURL(profile)) | 389 if (entry->GetURL() == GetLocalInstantURL(profile)) |
| 396 return true; | 390 return true; |
| 397 | 391 |
| 398 if (ShouldUseCacheableNTP()) { | 392 GURL new_tab_url(GetNewTabPageURL(profile)); |
| 399 GURL new_tab_url(GetNewTabPageURL(profile)); | 393 return new_tab_url.is_valid() && |
| 400 return new_tab_url.is_valid() && | 394 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 } | 395 } |
| 407 | 396 |
| 408 bool IsSuggestPrefEnabled(Profile* profile) { | 397 bool IsSuggestPrefEnabled(Profile* profile) { |
| 409 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && | 398 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && |
| 410 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled); | 399 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled); |
| 411 } | 400 } |
| 412 | 401 |
| 413 GURL GetInstantURL(Profile* profile, int start_margin, | 402 GURL GetInstantURL(Profile* profile, int start_margin, |
| 414 bool force_instant_results) { | 403 bool force_instant_results) { |
| 415 if (!IsInstantExtendedAPIEnabled() || !IsSuggestPrefEnabled(profile)) | 404 if (!IsInstantExtendedAPIEnabled() || !IsSuggestPrefEnabled(profile)) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 451 return result; | 440 return result; |
| 452 for (size_t i = 0; i < template_url->URLCount(); ++i) { | 441 for (size_t i = 0; i < template_url->URLCount(); ++i) { |
| 453 TemplateURLRef ref(template_url, i); | 442 TemplateURLRef ref(template_url, i); |
| 454 result.push_back(TemplateURLRefToGURL(ref, kDisableStartMargin, false, | 443 result.push_back(TemplateURLRefToGURL(ref, kDisableStartMargin, false, |
| 455 false)); | 444 false)); |
| 456 } | 445 } |
| 457 return result; | 446 return result; |
| 458 } | 447 } |
| 459 | 448 |
| 460 GURL GetNewTabPageURL(Profile* profile) { | 449 GURL GetNewTabPageURL(Profile* profile) { |
| 461 if (!ShouldUseCacheableNTP()) | |
| 462 return GURL(); | |
| 463 | |
| 464 if (!profile || profile->IsOffTheRecord()) | 450 if (!profile || profile->IsOffTheRecord()) |
| 465 return GURL(); | 451 return GURL(); |
| 466 | 452 |
| 467 if (!IsSuggestPrefEnabled(profile)) | 453 if (!IsSuggestPrefEnabled(profile)) |
| 468 return GURL(chrome::kChromeSearchLocalNtpUrl); | 454 return GURL(chrome::kChromeSearchLocalNtpUrl); |
| 469 | 455 |
| 470 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 456 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
| 471 if (!template_url) | 457 if (!template_url) |
| 472 return GURL(chrome::kChromeSearchLocalNtpUrl); | 458 return GURL(chrome::kChromeSearchLocalNtpUrl); |
| 473 | 459 |
| 474 GURL url(TemplateURLRefToGURL(template_url->new_tab_url_ref(), | 460 GURL url(TemplateURLRefToGURL(template_url->new_tab_url_ref(), |
| 475 kDisableStartMargin, false, false)); | 461 kDisableStartMargin, false, false)); |
| 476 if (!url.is_valid() || !url.SchemeIsSecure()) | 462 if (!url.is_valid() || !url.SchemeIsSecure()) |
| 477 return GURL(chrome::kChromeSearchLocalNtpUrl); | 463 return GURL(chrome::kChromeSearchLocalNtpUrl); |
| 478 | 464 |
| 479 if (!IsURLAllowedForSupervisedUser(url, profile)) | 465 if (!IsURLAllowedForSupervisedUser(url, profile)) |
| 480 return GURL(chrome::kChromeSearchLocalNtpUrl); | 466 return GURL(chrome::kChromeSearchLocalNtpUrl); |
| 481 | 467 |
| 482 return url; | 468 return url; |
| 483 } | 469 } |
| 484 | 470 |
| 485 GURL GetSearchResultPrefetchBaseURL(Profile* profile) { | 471 GURL GetSearchResultPrefetchBaseURL(Profile* profile) { |
| 486 return ShouldPrefetchSearchResults() ? | 472 return ShouldPrefetchSearchResults() ? |
| 487 GetInstantURL(profile, kDisableStartMargin, true) : GURL(); | 473 GetInstantURL(profile, kDisableStartMargin, true) : GURL(); |
| 488 } | 474 } |
| 489 | 475 |
| 490 bool ShouldPrefetchSearchResults() { | 476 bool ShouldPrefetchSearchResults() { |
| 491 if (!ShouldUseCacheableNTP()) | |
| 492 return false; | |
| 493 | |
| 494 FieldTrialFlags flags; | 477 FieldTrialFlags flags; |
| 495 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( | 478 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( |
| 496 kPrefetchSearchResultsFlagName, false, flags); | 479 kPrefetchSearchResultsFlagName, false, flags); |
| 497 } | 480 } |
| 498 | 481 |
| 499 GURL GetLocalInstantURL(Profile* profile) { | 482 GURL GetLocalInstantURL(Profile* profile) { |
| 500 return GURL(chrome::kChromeSearchLocalNtpUrl); | 483 return GURL(chrome::kChromeSearchLocalNtpUrl); |
| 501 } | 484 } |
| 502 | 485 |
| 503 bool ShouldHideTopVerbatimMatch() { | 486 bool ShouldHideTopVerbatimMatch() { |
| 504 FieldTrialFlags flags; | 487 FieldTrialFlags flags; |
| 505 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( | 488 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( |
| 506 kHideVerbatimFlagName, false, flags); | 489 kHideVerbatimFlagName, false, flags); |
| 507 } | 490 } |
| 508 | 491 |
| 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() { | 492 DisplaySearchButtonConditions GetDisplaySearchButtonConditions() { |
| 527 const CommandLine* cl = CommandLine::ForCurrentProcess(); | 493 const CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 528 if (cl->HasSwitch(switches::kDisableSearchButtonInOmnibox)) { | 494 if (cl->HasSwitch(switches::kDisableSearchButtonInOmnibox)) { |
| 529 return DISPLAY_SEARCH_BUTTON_NEVER; | 495 return DISPLAY_SEARCH_BUTTON_NEVER; |
| 530 } else if (cl->HasSwitch(switches::kEnableSearchButtonInOmniboxForStr)) { | 496 } else if (cl->HasSwitch(switches::kEnableSearchButtonInOmniboxForStr)) { |
| 531 return DISPLAY_SEARCH_BUTTON_FOR_STR; | 497 return DISPLAY_SEARCH_BUTTON_FOR_STR; |
| 532 } else if (cl->HasSwitch(switches::kEnableSearchButtonInOmniboxForStrOrIip)) { | 498 } else if (cl->HasSwitch(switches::kEnableSearchButtonInOmniboxForStrOrIip)) { |
| 533 return DISPLAY_SEARCH_BUTTON_FOR_STR_OR_IIP; | 499 return DISPLAY_SEARCH_BUTTON_FOR_STR_OR_IIP; |
| 534 } else if (cl->HasSwitch(switches::kEnableSearchButtonInOmniboxAlways)) { | 500 } else if (cl->HasSwitch(switches::kEnableSearchButtonInOmniboxAlways)) { |
| 535 return DISPLAY_SEARCH_BUTTON_ALWAYS; | 501 return DISPLAY_SEARCH_BUTTON_ALWAYS; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 584 search::MatchesOriginAndPath(url, instant_url)) { | 550 search::MatchesOriginAndPath(url, instant_url)) { |
| 585 replacements.SetHost(online_ntp_host.c_str(), | 551 replacements.SetHost(online_ntp_host.c_str(), |
| 586 url_parse::Component(0, online_ntp_host.length())); | 552 url_parse::Component(0, online_ntp_host.length())); |
| 587 } | 553 } |
| 588 } | 554 } |
| 589 | 555 |
| 590 effective_url = effective_url.ReplaceComponents(replacements); | 556 effective_url = effective_url.ReplaceComponents(replacements); |
| 591 return effective_url; | 557 return effective_url; |
| 592 } | 558 } |
| 593 | 559 |
| 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, | 560 bool HandleNewTabURLRewrite(GURL* url, |
| 632 content::BrowserContext* browser_context) { | 561 content::BrowserContext* browser_context) { |
| 633 if (!IsInstantExtendedAPIEnabled()) | 562 if (!IsInstantExtendedAPIEnabled()) |
| 634 return false; | 563 return false; |
| 635 | 564 |
| 636 if (!url->SchemeIs(chrome::kChromeUIScheme) || | 565 if (!url->SchemeIs(chrome::kChromeUIScheme) || |
| 637 url->host() != chrome::kChromeUINewTabHost) | 566 url->host() != chrome::kChromeUINewTabHost) |
| 638 return false; | 567 return false; |
| 639 | 568 |
| 640 Profile* profile = Profile::FromBrowserContext(browser_context); | 569 Profile* profile = Profile::FromBrowserContext(browser_context); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 747 | 676 |
| 748 // Given a FieldTrialFlags object, returns the boolean value of the provided | 677 // Given a FieldTrialFlags object, returns the boolean value of the provided |
| 749 // flag. | 678 // flag. |
| 750 bool GetBoolValueForFlagWithDefault(const std::string& flag, | 679 bool GetBoolValueForFlagWithDefault(const std::string& flag, |
| 751 bool default_value, | 680 bool default_value, |
| 752 const FieldTrialFlags& flags) { | 681 const FieldTrialFlags& flags) { |
| 753 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); | 682 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
| 754 } | 683 } |
| 755 | 684 |
| 756 } // namespace chrome | 685 } // namespace chrome |
| OLD | NEW |