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