Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: chrome/browser/search/search.cc

Issue 23455047: InstantExtended: Send search URLs to renderers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Small test Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 GURL TemplateURLRefToGURL(const TemplateURLRef& ref, 117 GURL TemplateURLRefToGURL(const TemplateURLRef& ref,
117 int start_margin, 118 int start_margin,
118 bool append_extra_query_params) { 119 bool append_extra_query_params) {
119 TemplateURLRef::SearchTermsArgs search_terms_args = 120 TemplateURLRef::SearchTermsArgs search_terms_args =
120 TemplateURLRef::SearchTermsArgs(string16()); 121 TemplateURLRef::SearchTermsArgs(string16());
121 search_terms_args.omnibox_start_margin = start_margin; 122 search_terms_args.omnibox_start_margin = start_margin;
122 search_terms_args.append_extra_query_params = append_extra_query_params; 123 search_terms_args.append_extra_query_params = append_extra_query_params;
123 return GURL(ref.ReplaceSearchTerms(search_terms_args)); 124 return GURL(ref.ReplaceSearchTerms(search_terms_args));
124 } 125 }
125 126
126 bool MatchesOrigin(const GURL& my_url, const GURL& other_url) {
127 return my_url.host() == other_url.host() &&
128 my_url.port() == other_url.port() &&
129 (my_url.scheme() == other_url.scheme() ||
130 (my_url.SchemeIs(content::kHttpsScheme) &&
131 other_url.SchemeIs(content::kHttpScheme)));
132 }
133
134 bool MatchesAnySearchURL(const GURL& url, TemplateURL* template_url) { 127 bool MatchesAnySearchURL(const GURL& url, TemplateURL* template_url) {
135 GURL search_url = 128 GURL search_url =
136 TemplateURLRefToGURL(template_url->url_ref(), kDisableStartMargin, false); 129 TemplateURLRefToGURL(template_url->url_ref(), kDisableStartMargin, false);
137 if (search_url.is_valid() && MatchesOriginAndPath(url, search_url)) 130 if (search_url.is_valid() &&
131 search::MatchesOriginAndPath(url, search_url))
138 return true; 132 return true;
139 133
140 // "URLCount() - 1" because we already tested url_ref above. 134 // "URLCount() - 1" because we already tested url_ref above.
141 for (size_t i = 0; i < template_url->URLCount() - 1; ++i) { 135 for (size_t i = 0; i < template_url->URLCount() - 1; ++i) {
142 TemplateURLRef ref(template_url, i); 136 TemplateURLRef ref(template_url, i);
143 search_url = TemplateURLRefToGURL(ref, kDisableStartMargin, false); 137 search_url = TemplateURLRefToGURL(ref, kDisableStartMargin, false);
144 if (search_url.is_valid() && MatchesOriginAndPath(url, search_url)) 138 if (search_url.is_valid() &&
139 search::MatchesOriginAndPath(url, search_url))
145 return true; 140 return true;
146 } 141 }
147 142
148 return false; 143 return false;
149 } 144 }
150 145
151 void RecordInstantExtendedOptInState() { 146 void RecordInstantExtendedOptInState() {
152 if (instant_extended_opt_in_state_gate) 147 if (instant_extended_opt_in_state_gate)
153 return; 148 return;
154 149
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 191
197 // Returns true if |url| can be used as an Instant URL for |profile|. 192 // Returns true if |url| can be used as an Instant URL for |profile|.
198 bool IsInstantURL(const GURL& url, Profile* profile) { 193 bool IsInstantURL(const GURL& url, Profile* profile) {
199 if (!IsInstantExtendedAPIEnabled()) 194 if (!IsInstantExtendedAPIEnabled())
200 return false; 195 return false;
201 196
202 if (!url.is_valid()) 197 if (!url.is_valid())
203 return false; 198 return false;
204 199
205 const GURL new_tab_url(GetNewTabPageURL(profile)); 200 const GURL new_tab_url(GetNewTabPageURL(profile));
206 if (new_tab_url.is_valid() && MatchesOriginAndPath(url, new_tab_url)) 201 if (new_tab_url.is_valid() &&
202 search::MatchesOriginAndPath(url, new_tab_url))
207 return true; 203 return true;
208 204
209 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); 205 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
210 if (!template_url) 206 if (!template_url)
211 return false; 207 return false;
212 208
213 if (!IsSuitableURLForInstant(url, template_url)) 209 if (!IsSuitableURLForInstant(url, template_url))
214 return false; 210 return false;
215 211
216 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); 212 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref();
217 const GURL instant_url = 213 const GURL instant_url =
218 TemplateURLRefToGURL(instant_url_ref, kDisableStartMargin, false); 214 TemplateURLRefToGURL(instant_url_ref, kDisableStartMargin, false);
219 if (!instant_url.is_valid()) 215 if (!instant_url.is_valid())
220 return false; 216 return false;
221 217
222 if (MatchesOriginAndPath(url, instant_url)) 218 if (search::MatchesOriginAndPath(url, instant_url))
223 return true; 219 return true;
224 220
225 return !ShouldSuppressInstantExtendedOnSRP() && 221 return !ShouldSuppressInstantExtendedOnSRP() &&
226 MatchesAnySearchURL(url, template_url); 222 MatchesAnySearchURL(url, template_url);
227 } 223 }
228 224
229 string16 GetSearchTermsImpl(const content::WebContents* contents, 225 string16 GetSearchTermsImpl(const content::WebContents* contents,
230 const content::NavigationEntry* entry) { 226 const content::NavigationEntry* entry) {
231 if (!contents || !IsQueryExtractionEnabled()) 227 if (!contents || !IsQueryExtractionEnabled())
232 return string16(); 228 return string16();
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); 379 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
384 if (!IsRenderedInInstantProcess(contents, profile)) 380 if (!IsRenderedInInstantProcess(contents, profile))
385 return false; 381 return false;
386 382
387 if (entry->GetVirtualURL() == GetLocalInstantURL(profile)) 383 if (entry->GetVirtualURL() == GetLocalInstantURL(profile))
388 return true; 384 return true;
389 385
390 if (ShouldUseCacheableNTP()) { 386 if (ShouldUseCacheableNTP()) {
391 GURL new_tab_url(GetNewTabPageURL(profile)); 387 GURL new_tab_url(GetNewTabPageURL(profile));
392 return new_tab_url.is_valid() && 388 return new_tab_url.is_valid() &&
393 MatchesOriginAndPath(entry->GetURL(), new_tab_url); 389 search::MatchesOriginAndPath(entry->GetURL(), new_tab_url);
394 } 390 }
395 391
396 return IsInstantURL(entry->GetVirtualURL(), profile) && 392 return IsInstantURL(entry->GetVirtualURL(), profile) &&
397 GetSearchTermsImpl(contents, entry).empty(); 393 GetSearchTermsImpl(contents, entry).empty();
398 } 394 }
399 395
400 bool IsSuggestPrefEnabled(Profile* profile) { 396 bool IsSuggestPrefEnabled(Profile* profile) {
401 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && 397 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() &&
402 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled); 398 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled);
403 } 399 }
(...skipping 17 matching lines...) Expand all
421 // IsSuitableURLForInstant()). 417 // IsSuitableURLForInstant()).
422 if (instant_url.SchemeIsSecure() || 418 if (instant_url.SchemeIsSecure() ||
423 google_util::StartsWithCommandLineGoogleBaseURL(instant_url)) 419 google_util::StartsWithCommandLineGoogleBaseURL(instant_url))
424 return instant_url; 420 return instant_url;
425 GURL::Replacements replacements; 421 GURL::Replacements replacements;
426 const std::string secure_scheme(content::kHttpsScheme); 422 const std::string secure_scheme(content::kHttpsScheme);
427 replacements.SetSchemeStr(secure_scheme); 423 replacements.SetSchemeStr(secure_scheme);
428 return instant_url.ReplaceComponents(replacements); 424 return instant_url.ReplaceComponents(replacements);
429 } 425 }
430 426
427 // Returns URLs associated with the default search engine for |profile|.
428 std::vector<GURL> GetSearchURLs(Profile* profile) {
429 std::vector<GURL> result;
430 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
431 for (size_t i = 0; i < template_url->URLCount(); ++i) {
432 TemplateURLRef ref(template_url, i);
433 result.push_back(TemplateURLRefToGURL(ref, kDisableStartMargin, false));
434 }
435 return result;
436 }
437
438 GURL GetNewTabPageURL(Profile* profile) {
439 if (!ShouldUseCacheableNTP())
440 return GURL();
441
442 if (!profile)
443 return GURL();
444
445 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
446 if (!template_url)
447 return GURL();
448
449 return TemplateURLRefToGURL(template_url->new_tab_url_ref(),
450 kDisableStartMargin, false);
451 }
452
431 GURL GetLocalInstantURL(Profile* profile) { 453 GURL GetLocalInstantURL(Profile* profile) {
432 return GURL(chrome::kChromeSearchLocalNtpUrl); 454 return GURL(chrome::kChromeSearchLocalNtpUrl);
433 } 455 }
434 456
435 bool ShouldPreferRemoteNTPOnStartup() { 457 bool ShouldPreferRemoteNTPOnStartup() {
436 // Check the command-line/about:flags setting first, which should have 458 // Check the command-line/about:flags setting first, which should have
437 // precedence and allows the trial to not be reported (if it's never queried). 459 // precedence and allows the trial to not be reported (if it's never queried).
438 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 460 const CommandLine* command_line = CommandLine::ForCurrentProcess();
439 if (command_line->HasSwitch(switches::kDisableInstantExtendedAPI) || 461 if (command_line->HasSwitch(switches::kDisableInstantExtendedAPI) ||
440 command_line->HasSwitch(switches::kEnableLocalFirstLoadNTP)) { 462 command_line->HasSwitch(switches::kEnableLocalFirstLoadNTP)) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 if (GetFieldTrialInfo( 527 if (GetFieldTrialInfo(
506 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName), 528 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
507 &flags, NULL)) { 529 &flags, NULL)) {
508 return GetBoolValueForFlagWithDefault( 530 return GetBoolValueForFlagWithDefault(
509 kSuppressInstantExtendedOnSRPFlagName, false, flags); 531 kSuppressInstantExtendedOnSRPFlagName, false, flags);
510 } 532 }
511 533
512 return false; 534 return false;
513 } 535 }
514 536
515 bool MatchesOriginAndPath(const GURL& my_url, const GURL& other_url) {
516 return MatchesOrigin(my_url, other_url) && my_url.path() == other_url.path();
517 }
518
519 GURL GetEffectiveURLForInstant(const GURL& url, Profile* profile) { 537 GURL GetEffectiveURLForInstant(const GURL& url, Profile* profile) {
520 CHECK(ShouldAssignURLToInstantRenderer(url, profile)) 538 CHECK(ShouldAssignURLToInstantRenderer(url, profile))
521 << "Error granting Instant access."; 539 << "Error granting Instant access.";
522 540
523 if (url.SchemeIs(chrome::kChromeSearchScheme)) 541 if (url.SchemeIs(chrome::kChromeSearchScheme))
524 return url; 542 return url;
525 543
526 GURL effective_url(url); 544 GURL effective_url(url);
527 545
528 // Replace the scheme with "chrome-search:". 546 // Replace the scheme with "chrome-search:".
529 url_canon::Replacements<char> replacements; 547 url_canon::Replacements<char> replacements;
530 std::string search_scheme(chrome::kChromeSearchScheme); 548 std::string search_scheme(chrome::kChromeSearchScheme);
531 replacements.SetScheme(search_scheme.data(), 549 replacements.SetScheme(search_scheme.data(),
532 url_parse::Component(0, search_scheme.length())); 550 url_parse::Component(0, search_scheme.length()));
533 551
534 // If the URL corresponds to an online NTP, replace the host with 552 // If the URL corresponds to an online NTP, replace the host with
535 // "online-ntp". 553 // "online-ntp".
536 std::string online_ntp_host(chrome::kChromeSearchOnlineNtpHost); 554 std::string online_ntp_host(chrome::kChromeSearchOnlineNtpHost);
537 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); 555 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
538 if (template_url) { 556 if (template_url) {
539 const GURL instant_url = TemplateURLRefToGURL( 557 const GURL instant_url = TemplateURLRefToGURL(
540 template_url->instant_url_ref(), kDisableStartMargin, false); 558 template_url->instant_url_ref(), kDisableStartMargin, false);
541 if (instant_url.is_valid() && MatchesOriginAndPath(url, instant_url)) { 559 if (instant_url.is_valid() &&
560 search::MatchesOriginAndPath(url, instant_url)) {
542 replacements.SetHost(online_ntp_host.c_str(), 561 replacements.SetHost(online_ntp_host.c_str(),
543 url_parse::Component(0, online_ntp_host.length())); 562 url_parse::Component(0, online_ntp_host.length()));
544 } 563 }
545 } 564 }
546 565
547 effective_url = effective_url.ReplaceComponents(replacements); 566 effective_url = effective_url.ReplaceComponents(replacements);
548 return effective_url; 567 return effective_url;
549 } 568 }
550 569
551 int GetInstantLoaderStalenessTimeoutSec() { 570 int GetInstantLoaderStalenessTimeoutSec() {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
605 return true; 624 return true;
606 } 625 }
607 626
608 bool HandleNewTabURLReverseRewrite(GURL* url, 627 bool HandleNewTabURLReverseRewrite(GURL* url,
609 content::BrowserContext* browser_context) { 628 content::BrowserContext* browser_context) {
610 if (!IsInstantExtendedAPIEnabled()) 629 if (!IsInstantExtendedAPIEnabled())
611 return false; 630 return false;
612 631
613 Profile* profile = Profile::FromBrowserContext(browser_context); 632 Profile* profile = Profile::FromBrowserContext(browser_context);
614 GURL new_tab_url(GetNewTabPageURL(profile)); 633 GURL new_tab_url(GetNewTabPageURL(profile));
615 if (!new_tab_url.is_valid() || !MatchesOriginAndPath(new_tab_url, *url)) 634 if (!new_tab_url.is_valid() ||
635 !search::MatchesOriginAndPath(new_tab_url, *url))
616 return false; 636 return false;
617 637
618 *url = GURL(chrome::kChromeUINewTabURL); 638 *url = GURL(chrome::kChromeUINewTabURL);
619 return true; 639 return true;
620 } 640 }
621 641
622 void SetInstantSupportStateInNavigationEntry(InstantSupportState state, 642 void SetInstantSupportStateInNavigationEntry(InstantSupportState state,
623 content::NavigationEntry* entry) { 643 content::NavigationEntry* entry) {
624 if (!entry) 644 if (!entry)
625 return; 645 return;
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
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)
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698