| 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/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/google/google_util.h" | 16 #include "chrome/browser/google/google_util.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/profiles/profile_manager.h" | 18 #include "chrome/browser/profiles/profile_manager.h" |
| 19 #include "chrome/browser/search/instant_service.h" | 19 #include "chrome/browser/search/instant_service.h" |
| 20 #include "chrome/browser/search/instant_service_factory.h" | 20 #include "chrome/browser/search/instant_service_factory.h" |
| 21 #include "chrome/browser/search_engines/template_url_service.h" | 21 #include "chrome/browser/search_engines/template_url_service.h" |
| 22 #include "chrome/browser/search_engines/template_url_service_factory.h" | 22 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 23 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
| 24 #include "chrome/browser/ui/browser_instant_controller.h" | 24 #include "chrome/browser/ui/browser_instant_controller.h" |
| 25 #include "chrome/browser/ui/browser_iterator.h" | 25 #include "chrome/browser/ui/browser_iterator.h" |
| 26 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
| 27 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
| 28 #include "chrome/common/search_urls.h" |
| 28 #include "chrome/common/url_constants.h" | 29 #include "chrome/common/url_constants.h" |
| 29 #include "components/sessions/serialized_navigation_entry.h" | 30 #include "components/sessions/serialized_navigation_entry.h" |
| 30 #include "components/user_prefs/pref_registry_syncable.h" | 31 #include "components/user_prefs/pref_registry_syncable.h" |
| 31 #include "content/public/browser/navigation_entry.h" | 32 #include "content/public/browser/navigation_entry.h" |
| 32 #include "content/public/browser/render_process_host.h" | 33 #include "content/public/browser/render_process_host.h" |
| 33 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
| 34 #include "grit/generated_resources.h" | 35 #include "grit/generated_resources.h" |
| 35 #include "ui/base/l10n/l10n_util.h" | 36 #include "ui/base/l10n/l10n_util.h" |
| 36 | 37 |
| 37 namespace chrome { | 38 namespace chrome { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 GURL TemplateURLRefToGURL(const TemplateURLRef& ref, | 124 GURL TemplateURLRefToGURL(const TemplateURLRef& ref, |
| 124 int start_margin, | 125 int start_margin, |
| 125 bool append_extra_query_params) { | 126 bool append_extra_query_params) { |
| 126 TemplateURLRef::SearchTermsArgs search_terms_args = | 127 TemplateURLRef::SearchTermsArgs search_terms_args = |
| 127 TemplateURLRef::SearchTermsArgs(string16()); | 128 TemplateURLRef::SearchTermsArgs(string16()); |
| 128 search_terms_args.omnibox_start_margin = start_margin; | 129 search_terms_args.omnibox_start_margin = start_margin; |
| 129 search_terms_args.append_extra_query_params = append_extra_query_params; | 130 search_terms_args.append_extra_query_params = append_extra_query_params; |
| 130 return GURL(ref.ReplaceSearchTerms(search_terms_args)); | 131 return GURL(ref.ReplaceSearchTerms(search_terms_args)); |
| 131 } | 132 } |
| 132 | 133 |
| 133 bool MatchesOrigin(const GURL& my_url, const GURL& other_url) { | |
| 134 return my_url.host() == other_url.host() && | |
| 135 my_url.port() == other_url.port() && | |
| 136 (my_url.scheme() == other_url.scheme() || | |
| 137 (my_url.SchemeIs(content::kHttpsScheme) && | |
| 138 other_url.SchemeIs(content::kHttpScheme))); | |
| 139 } | |
| 140 | |
| 141 bool MatchesAnySearchURL(const GURL& url, TemplateURL* template_url) { | 134 bool MatchesAnySearchURL(const GURL& url, TemplateURL* template_url) { |
| 142 GURL search_url = | 135 GURL search_url = |
| 143 TemplateURLRefToGURL(template_url->url_ref(), kDisableStartMargin, false); | 136 TemplateURLRefToGURL(template_url->url_ref(), kDisableStartMargin, false); |
| 144 if (search_url.is_valid() && MatchesOriginAndPath(url, search_url)) | 137 if (search_url.is_valid() && |
| 138 search::MatchesOriginAndPath(url, search_url)) |
| 145 return true; | 139 return true; |
| 146 | 140 |
| 147 // "URLCount() - 1" because we already tested url_ref above. | 141 // "URLCount() - 1" because we already tested url_ref above. |
| 148 for (size_t i = 0; i < template_url->URLCount() - 1; ++i) { | 142 for (size_t i = 0; i < template_url->URLCount() - 1; ++i) { |
| 149 TemplateURLRef ref(template_url, i); | 143 TemplateURLRef ref(template_url, i); |
| 150 search_url = TemplateURLRefToGURL(ref, kDisableStartMargin, false); | 144 search_url = TemplateURLRefToGURL(ref, kDisableStartMargin, false); |
| 151 if (search_url.is_valid() && MatchesOriginAndPath(url, search_url)) | 145 if (search_url.is_valid() && |
| 146 search::MatchesOriginAndPath(url, search_url)) |
| 152 return true; | 147 return true; |
| 153 } | 148 } |
| 154 | 149 |
| 155 return false; | 150 return false; |
| 156 } | 151 } |
| 157 | 152 |
| 158 void RecordInstantExtendedOptInState() { | 153 void RecordInstantExtendedOptInState() { |
| 159 if (instant_extended_opt_in_state_gate) | 154 if (instant_extended_opt_in_state_gate) |
| 160 return; | 155 return; |
| 161 | 156 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 | 198 |
| 204 // Returns true if |url| can be used as an Instant URL for |profile|. | 199 // Returns true if |url| can be used as an Instant URL for |profile|. |
| 205 bool IsInstantURL(const GURL& url, Profile* profile) { | 200 bool IsInstantURL(const GURL& url, Profile* profile) { |
| 206 if (!IsInstantExtendedAPIEnabled()) | 201 if (!IsInstantExtendedAPIEnabled()) |
| 207 return false; | 202 return false; |
| 208 | 203 |
| 209 if (!url.is_valid()) | 204 if (!url.is_valid()) |
| 210 return false; | 205 return false; |
| 211 | 206 |
| 212 const GURL new_tab_url(GetNewTabPageURL(profile)); | 207 const GURL new_tab_url(GetNewTabPageURL(profile)); |
| 213 if (new_tab_url.is_valid() && MatchesOriginAndPath(url, new_tab_url)) | 208 if (new_tab_url.is_valid() && |
| 209 search::MatchesOriginAndPath(url, new_tab_url)) |
| 214 return true; | 210 return true; |
| 215 | 211 |
| 216 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 212 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
| 217 if (!template_url) | 213 if (!template_url) |
| 218 return false; | 214 return false; |
| 219 | 215 |
| 220 if (!IsSuitableURLForInstant(url, template_url)) | 216 if (!IsSuitableURLForInstant(url, template_url)) |
| 221 return false; | 217 return false; |
| 222 | 218 |
| 223 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); | 219 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); |
| 224 const GURL instant_url = | 220 const GURL instant_url = |
| 225 TemplateURLRefToGURL(instant_url_ref, kDisableStartMargin, false); | 221 TemplateURLRefToGURL(instant_url_ref, kDisableStartMargin, false); |
| 226 if (!instant_url.is_valid()) | 222 if (!instant_url.is_valid()) |
| 227 return false; | 223 return false; |
| 228 | 224 |
| 229 if (MatchesOriginAndPath(url, instant_url)) | 225 if (search::MatchesOriginAndPath(url, instant_url)) |
| 230 return true; | 226 return true; |
| 231 | 227 |
| 232 return !ShouldSuppressInstantExtendedOnSRP() && | 228 return !ShouldSuppressInstantExtendedOnSRP() && |
| 233 MatchesAnySearchURL(url, template_url); | 229 MatchesAnySearchURL(url, template_url); |
| 234 } | 230 } |
| 235 | 231 |
| 236 string16 GetSearchTermsImpl(const content::WebContents* contents, | 232 string16 GetSearchTermsImpl(const content::WebContents* contents, |
| 237 const content::NavigationEntry* entry) { | 233 const content::NavigationEntry* entry) { |
| 238 if (!contents || !IsQueryExtractionEnabled()) | 234 if (!contents || !IsQueryExtractionEnabled()) |
| 239 return string16(); | 235 return string16(); |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 384 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 389 if (!IsRenderedInInstantProcess(contents, profile)) | 385 if (!IsRenderedInInstantProcess(contents, profile)) |
| 390 return false; | 386 return false; |
| 391 | 387 |
| 392 if (entry->GetVirtualURL() == GetLocalInstantURL(profile)) | 388 if (entry->GetVirtualURL() == GetLocalInstantURL(profile)) |
| 393 return true; | 389 return true; |
| 394 | 390 |
| 395 if (ShouldUseCacheableNTP()) { | 391 if (ShouldUseCacheableNTP()) { |
| 396 GURL new_tab_url(GetNewTabPageURL(profile)); | 392 GURL new_tab_url(GetNewTabPageURL(profile)); |
| 397 return new_tab_url.is_valid() && | 393 return new_tab_url.is_valid() && |
| 398 MatchesOriginAndPath(entry->GetURL(), new_tab_url); | 394 search::MatchesOriginAndPath(entry->GetURL(), new_tab_url); |
| 399 } | 395 } |
| 400 | 396 |
| 401 return IsInstantURL(entry->GetVirtualURL(), profile) && | 397 return IsInstantURL(entry->GetVirtualURL(), profile) && |
| 402 GetSearchTermsImpl(contents, entry).empty(); | 398 GetSearchTermsImpl(contents, entry).empty(); |
| 403 } | 399 } |
| 404 | 400 |
| 405 bool IsSuggestPrefEnabled(Profile* profile) { | 401 bool IsSuggestPrefEnabled(Profile* profile) { |
| 406 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && | 402 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && |
| 407 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled); | 403 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled); |
| 408 } | 404 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 426 // IsSuitableURLForInstant()). | 422 // IsSuitableURLForInstant()). |
| 427 if (instant_url.SchemeIsSecure() || | 423 if (instant_url.SchemeIsSecure() || |
| 428 google_util::StartsWithCommandLineGoogleBaseURL(instant_url)) | 424 google_util::StartsWithCommandLineGoogleBaseURL(instant_url)) |
| 429 return instant_url; | 425 return instant_url; |
| 430 GURL::Replacements replacements; | 426 GURL::Replacements replacements; |
| 431 const std::string secure_scheme(content::kHttpsScheme); | 427 const std::string secure_scheme(content::kHttpsScheme); |
| 432 replacements.SetSchemeStr(secure_scheme); | 428 replacements.SetSchemeStr(secure_scheme); |
| 433 return instant_url.ReplaceComponents(replacements); | 429 return instant_url.ReplaceComponents(replacements); |
| 434 } | 430 } |
| 435 | 431 |
| 432 // Returns URLs associated with the default search engine for |profile|. |
| 433 std::vector<GURL> GetSearchURLs(Profile* profile) { |
| 434 std::vector<GURL> result; |
| 435 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
| 436 for (size_t i = 0; i < template_url->URLCount(); ++i) { |
| 437 TemplateURLRef ref(template_url, i); |
| 438 result.push_back(TemplateURLRefToGURL(ref, kDisableStartMargin, false)); |
| 439 } |
| 440 return result; |
| 441 } |
| 442 |
| 443 GURL GetNewTabPageURL(Profile* profile) { |
| 444 if (!ShouldUseCacheableNTP()) |
| 445 return GURL(); |
| 446 |
| 447 if (!profile || !IsSuggestPrefEnabled(profile)) |
| 448 return GURL(); |
| 449 |
| 450 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
| 451 if (!template_url) |
| 452 return GURL(); |
| 453 |
| 454 return TemplateURLRefToGURL(template_url->new_tab_url_ref(), |
| 455 kDisableStartMargin, false); |
| 456 } |
| 457 |
| 436 GURL GetLocalInstantURL(Profile* profile) { | 458 GURL GetLocalInstantURL(Profile* profile) { |
| 437 return GURL(chrome::kChromeSearchLocalNtpUrl); | 459 return GURL(chrome::kChromeSearchLocalNtpUrl); |
| 438 } | 460 } |
| 439 | 461 |
| 440 bool ShouldPreferRemoteNTPOnStartup() { | 462 bool ShouldPreferRemoteNTPOnStartup() { |
| 441 // Check the command-line/about:flags setting first, which should have | 463 // Check the command-line/about:flags setting first, which should have |
| 442 // precedence and allows the trial to not be reported (if it's never queried). | 464 // precedence and allows the trial to not be reported (if it's never queried). |
| 443 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 465 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 444 if (command_line->HasSwitch(switches::kDisableInstantExtendedAPI) || | 466 if (command_line->HasSwitch(switches::kDisableInstantExtendedAPI) || |
| 445 command_line->HasSwitch(switches::kEnableLocalFirstLoadNTP)) { | 467 command_line->HasSwitch(switches::kEnableLocalFirstLoadNTP)) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 bool ShouldSuppressInstantExtendedOnSRP() { | 524 bool ShouldSuppressInstantExtendedOnSRP() { |
| 503 FieldTrialFlags flags; | 525 FieldTrialFlags flags; |
| 504 if (GetFieldTrialInfo(&flags, NULL)) { | 526 if (GetFieldTrialInfo(&flags, NULL)) { |
| 505 return GetBoolValueForFlagWithDefault( | 527 return GetBoolValueForFlagWithDefault( |
| 506 kSuppressInstantExtendedOnSRPFlagName, false, flags); | 528 kSuppressInstantExtendedOnSRPFlagName, false, flags); |
| 507 } | 529 } |
| 508 | 530 |
| 509 return false; | 531 return false; |
| 510 } | 532 } |
| 511 | 533 |
| 512 bool MatchesOriginAndPath(const GURL& my_url, const GURL& other_url) { | |
| 513 return MatchesOrigin(my_url, other_url) && my_url.path() == other_url.path(); | |
| 514 } | |
| 515 | |
| 516 GURL GetEffectiveURLForInstant(const GURL& url, Profile* profile) { | 534 GURL GetEffectiveURLForInstant(const GURL& url, Profile* profile) { |
| 517 CHECK(ShouldAssignURLToInstantRenderer(url, profile)) | 535 CHECK(ShouldAssignURLToInstantRenderer(url, profile)) |
| 518 << "Error granting Instant access."; | 536 << "Error granting Instant access."; |
| 519 | 537 |
| 520 if (url.SchemeIs(chrome::kChromeSearchScheme)) | 538 if (url.SchemeIs(chrome::kChromeSearchScheme)) |
| 521 return url; | 539 return url; |
| 522 | 540 |
| 523 GURL effective_url(url); | 541 GURL effective_url(url); |
| 524 | 542 |
| 525 // Replace the scheme with "chrome-search:". | 543 // Replace the scheme with "chrome-search:". |
| 526 url_canon::Replacements<char> replacements; | 544 url_canon::Replacements<char> replacements; |
| 527 std::string search_scheme(chrome::kChromeSearchScheme); | 545 std::string search_scheme(chrome::kChromeSearchScheme); |
| 528 replacements.SetScheme(search_scheme.data(), | 546 replacements.SetScheme(search_scheme.data(), |
| 529 url_parse::Component(0, search_scheme.length())); | 547 url_parse::Component(0, search_scheme.length())); |
| 530 | 548 |
| 531 // If the URL corresponds to an online NTP, replace the host with | 549 // If the URL corresponds to an online NTP, replace the host with |
| 532 // "online-ntp". | 550 // "online-ntp". |
| 533 std::string online_ntp_host(chrome::kChromeSearchOnlineNtpHost); | 551 std::string online_ntp_host(chrome::kChromeSearchOnlineNtpHost); |
| 534 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 552 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
| 535 if (template_url) { | 553 if (template_url) { |
| 536 const GURL instant_url = TemplateURLRefToGURL( | 554 const GURL instant_url = TemplateURLRefToGURL( |
| 537 template_url->instant_url_ref(), kDisableStartMargin, false); | 555 template_url->instant_url_ref(), kDisableStartMargin, false); |
| 538 if (instant_url.is_valid() && MatchesOriginAndPath(url, instant_url)) { | 556 if (instant_url.is_valid() && |
| 557 search::MatchesOriginAndPath(url, instant_url)) { |
| 539 replacements.SetHost(online_ntp_host.c_str(), | 558 replacements.SetHost(online_ntp_host.c_str(), |
| 540 url_parse::Component(0, online_ntp_host.length())); | 559 url_parse::Component(0, online_ntp_host.length())); |
| 541 } | 560 } |
| 542 } | 561 } |
| 543 | 562 |
| 544 effective_url = effective_url.ReplaceComponents(replacements); | 563 effective_url = effective_url.ReplaceComponents(replacements); |
| 545 return effective_url; | 564 return effective_url; |
| 546 } | 565 } |
| 547 | 566 |
| 548 int GetInstantLoaderStalenessTimeoutSec() { | 567 int GetInstantLoaderStalenessTimeoutSec() { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 return true; | 619 return true; |
| 601 } | 620 } |
| 602 | 621 |
| 603 bool HandleNewTabURLReverseRewrite(GURL* url, | 622 bool HandleNewTabURLReverseRewrite(GURL* url, |
| 604 content::BrowserContext* browser_context) { | 623 content::BrowserContext* browser_context) { |
| 605 if (!IsInstantExtendedAPIEnabled()) | 624 if (!IsInstantExtendedAPIEnabled()) |
| 606 return false; | 625 return false; |
| 607 | 626 |
| 608 Profile* profile = Profile::FromBrowserContext(browser_context); | 627 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 609 GURL new_tab_url(GetNewTabPageURL(profile)); | 628 GURL new_tab_url(GetNewTabPageURL(profile)); |
| 610 if (!new_tab_url.is_valid() || !MatchesOriginAndPath(new_tab_url, *url)) | 629 if (!new_tab_url.is_valid() || |
| 630 !search::MatchesOriginAndPath(new_tab_url, *url)) |
| 611 return false; | 631 return false; |
| 612 | 632 |
| 613 *url = GURL(chrome::kChromeUINewTabURL); | 633 *url = GURL(chrome::kChromeUINewTabURL); |
| 614 return true; | 634 return true; |
| 615 } | 635 } |
| 616 | 636 |
| 617 void SetInstantSupportStateInNavigationEntry(InstantSupportState state, | 637 void SetInstantSupportStateInNavigationEntry(InstantSupportState state, |
| 618 content::NavigationEntry* entry) { | 638 content::NavigationEntry* entry) { |
| 619 if (!entry) | 639 if (!entry) |
| 620 return; | 640 return; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 } | 753 } |
| 734 | 754 |
| 735 // Given a FieldTrialFlags object, returns the boolean value of the provided | 755 // Given a FieldTrialFlags object, returns the boolean value of the provided |
| 736 // flag. | 756 // flag. |
| 737 bool GetBoolValueForFlagWithDefault(const std::string& flag, | 757 bool GetBoolValueForFlagWithDefault(const std::string& flag, |
| 738 bool default_value, | 758 bool default_value, |
| 739 const FieldTrialFlags& flags) { | 759 const FieldTrialFlags& flags) { |
| 740 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); | 760 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
| 741 } | 761 } |
| 742 | 762 |
| 743 GURL GetNewTabPageURL(Profile* profile) { | |
| 744 if (!ShouldUseCacheableNTP()) | |
| 745 return GURL(); | |
| 746 | |
| 747 if (!profile || !IsSuggestPrefEnabled(profile)) | |
| 748 return GURL(); | |
| 749 | |
| 750 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | |
| 751 if (!template_url) | |
| 752 return GURL(); | |
| 753 | |
| 754 return TemplateURLRefToGURL(template_url->new_tab_url_ref(), | |
| 755 kDisableStartMargin, false); | |
| 756 } | |
| 757 | |
| 758 void ResetInstantExtendedOptInStateGateForTest() { | 763 void ResetInstantExtendedOptInStateGateForTest() { |
| 759 instant_extended_opt_in_state_gate = false; | 764 instant_extended_opt_in_state_gate = false; |
| 760 } | 765 } |
| 761 | 766 |
| 762 } // namespace chrome | 767 } // namespace chrome |
| OLD | NEW |