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

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: Handle TemplateURL change 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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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
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
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) {
samarth 2013/09/20 16:43:40 Test?
Jered 2013/09/20 20:55:49 Done.
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)
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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 bool ShouldSuppressInstantExtendedOnSRP() { 520 bool ShouldSuppressInstantExtendedOnSRP() {
499 FieldTrialFlags flags; 521 FieldTrialFlags flags;
500 if (GetFieldTrialInfo(&flags, NULL)) { 522 if (GetFieldTrialInfo(&flags, NULL)) {
501 return GetBoolValueForFlagWithDefault( 523 return GetBoolValueForFlagWithDefault(
502 kSuppressInstantExtendedOnSRPFlagName, false, flags); 524 kSuppressInstantExtendedOnSRPFlagName, false, flags);
503 } 525 }
504 526
505 return false; 527 return false;
506 } 528 }
507 529
508 bool MatchesOriginAndPath(const GURL& my_url, const GURL& other_url) {
509 return MatchesOrigin(my_url, other_url) && my_url.path() == other_url.path();
510 }
511
512 GURL GetEffectiveURLForInstant(const GURL& url, Profile* profile) { 530 GURL GetEffectiveURLForInstant(const GURL& url, Profile* profile) {
513 CHECK(ShouldAssignURLToInstantRenderer(url, profile)) 531 CHECK(ShouldAssignURLToInstantRenderer(url, profile))
514 << "Error granting Instant access."; 532 << "Error granting Instant access.";
515 533
516 if (url.SchemeIs(chrome::kChromeSearchScheme)) 534 if (url.SchemeIs(chrome::kChromeSearchScheme))
517 return url; 535 return url;
518 536
519 GURL effective_url(url); 537 GURL effective_url(url);
520 538
521 // Replace the scheme with "chrome-search:". 539 // Replace the scheme with "chrome-search:".
522 url_canon::Replacements<char> replacements; 540 url_canon::Replacements<char> replacements;
523 std::string search_scheme(chrome::kChromeSearchScheme); 541 std::string search_scheme(chrome::kChromeSearchScheme);
524 replacements.SetScheme(search_scheme.data(), 542 replacements.SetScheme(search_scheme.data(),
525 url_parse::Component(0, search_scheme.length())); 543 url_parse::Component(0, search_scheme.length()));
526 544
527 // If the URL corresponds to an online NTP, replace the host with 545 // If the URL corresponds to an online NTP, replace the host with
528 // "online-ntp". 546 // "online-ntp".
529 std::string online_ntp_host(chrome::kChromeSearchOnlineNtpHost); 547 std::string online_ntp_host(chrome::kChromeSearchOnlineNtpHost);
530 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); 548 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
531 if (template_url) { 549 if (template_url) {
532 const GURL instant_url = TemplateURLRefToGURL( 550 const GURL instant_url = TemplateURLRefToGURL(
533 template_url->instant_url_ref(), kDisableStartMargin, false); 551 template_url->instant_url_ref(), kDisableStartMargin, false);
534 if (instant_url.is_valid() && MatchesOriginAndPath(url, instant_url)) { 552 if (instant_url.is_valid() &&
553 search::MatchesOriginAndPath(url, instant_url)) {
535 replacements.SetHost(online_ntp_host.c_str(), 554 replacements.SetHost(online_ntp_host.c_str(),
536 url_parse::Component(0, online_ntp_host.length())); 555 url_parse::Component(0, online_ntp_host.length()));
537 } 556 }
538 } 557 }
539 558
540 effective_url = effective_url.ReplaceComponents(replacements); 559 effective_url = effective_url.ReplaceComponents(replacements);
541 return effective_url; 560 return effective_url;
542 } 561 }
543 562
544 int GetInstantLoaderStalenessTimeoutSec() { 563 int GetInstantLoaderStalenessTimeoutSec() {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
596 return true; 615 return true;
597 } 616 }
598 617
599 bool HandleNewTabURLReverseRewrite(GURL* url, 618 bool HandleNewTabURLReverseRewrite(GURL* url,
600 content::BrowserContext* browser_context) { 619 content::BrowserContext* browser_context) {
601 if (!IsInstantExtendedAPIEnabled()) 620 if (!IsInstantExtendedAPIEnabled())
602 return false; 621 return false;
603 622
604 Profile* profile = Profile::FromBrowserContext(browser_context); 623 Profile* profile = Profile::FromBrowserContext(browser_context);
605 GURL new_tab_url(GetNewTabPageURL(profile)); 624 GURL new_tab_url(GetNewTabPageURL(profile));
606 if (!new_tab_url.is_valid() || !MatchesOriginAndPath(new_tab_url, *url)) 625 if (!new_tab_url.is_valid() ||
626 !search::MatchesOriginAndPath(new_tab_url, *url))
607 return false; 627 return false;
608 628
609 *url = GURL(chrome::kChromeUINewTabURL); 629 *url = GURL(chrome::kChromeUINewTabURL);
610 return true; 630 return true;
611 } 631 }
612 632
613 void SetInstantSupportStateInNavigationEntry(InstantSupportState state, 633 void SetInstantSupportStateInNavigationEntry(InstantSupportState state,
614 content::NavigationEntry* entry) { 634 content::NavigationEntry* entry) {
615 if (!entry) 635 if (!entry)
616 return; 636 return;
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
729 } 749 }
730 750
731 // Given a FieldTrialFlags object, returns the boolean value of the provided 751 // Given a FieldTrialFlags object, returns the boolean value of the provided
732 // flag. 752 // flag.
733 bool GetBoolValueForFlagWithDefault(const std::string& flag, 753 bool GetBoolValueForFlagWithDefault(const std::string& flag,
734 bool default_value, 754 bool default_value,
735 const FieldTrialFlags& flags) { 755 const FieldTrialFlags& flags) {
736 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); 756 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags);
737 } 757 }
738 758
739 GURL GetNewTabPageURL(Profile* profile) {
740 if (!ShouldUseCacheableNTP())
741 return GURL();
742
743 if (!profile)
744 return GURL();
745
746 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
747 if (!template_url)
748 return GURL();
749
750 return TemplateURLRefToGURL(template_url->new_tab_url_ref(),
751 kDisableStartMargin, false);
752 }
753
754 void ResetInstantExtendedOptInStateGateForTest() { 759 void ResetInstantExtendedOptInStateGateForTest() {
755 instant_extended_opt_in_state_gate = false; 760 instant_extended_opt_in_state_gate = false;
756 } 761 }
757 762
758 } // namespace chrome 763 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698