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

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

Issue 22945004: InstantExtended: Add new_tab_url to TemplateURL. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 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"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 55
56 // The staleness timeout can be set (in seconds) via this config. 56 // The staleness timeout can be set (in seconds) via this config.
57 const char kStalePageTimeoutFlagName[] = "stale"; 57 const char kStalePageTimeoutFlagName[] = "stale";
58 const int kStalePageTimeoutDefault = 3 * 3600; // 3 hours. 58 const int kStalePageTimeoutDefault = 3 * 3600; // 3 hours.
59 const int kStalePageTimeoutDisabled = 0; 59 const int kStalePageTimeoutDisabled = 0;
60 60
61 const char kHideVerbatimFlagName[] = "hide_verbatim"; 61 const char kHideVerbatimFlagName[] = "hide_verbatim";
62 const char kUseRemoteNTPOnStartupFlagName[] = "use_remote_ntp_on_startup"; 62 const char kUseRemoteNTPOnStartupFlagName[] = "use_remote_ntp_on_startup";
63 const char kShowNtpFlagName[] = "show_ntp"; 63 const char kShowNtpFlagName[] = "show_ntp";
64 const char kRecentTabsOnNTPFlagName[] = "show_recent_tabs"; 64 const char kRecentTabsOnNTPFlagName[] = "show_recent_tabs";
65 const char kUseCacheableNTP[] = "use_cacheable_ntp";
65 66
66 // Constants for the field trial name and group prefix. 67 // Constants for the field trial name and group prefix.
67 const char kInstantExtendedFieldTrialName[] = "InstantExtended"; 68 const char kInstantExtendedFieldTrialName[] = "InstantExtended";
68 const char kGroupNumberPrefix[] = "Group"; 69 const char kGroupNumberPrefix[] = "Group";
69 70
70 // If the field trial's group name ends with this string its configuration will 71 // If the field trial's group name ends with this string its configuration will
71 // be ignored and Instant Extended will not be enabled by default. 72 // be ignored and Instant Extended will not be enabled by default.
72 const char kDisablingSuffix[] = "DISABLED"; 73 const char kDisablingSuffix[] = "DISABLED";
73 74
74 // Remember if we reported metrics about opt-in/out state. 75 // Remember if we reported metrics about opt-in/out state.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 GURL TemplateURLRefToGURL(const TemplateURLRef& ref, 114 GURL TemplateURLRefToGURL(const TemplateURLRef& ref,
114 int start_margin, 115 int start_margin,
115 bool append_extra_query_params) { 116 bool append_extra_query_params) {
116 TemplateURLRef::SearchTermsArgs search_terms_args = 117 TemplateURLRef::SearchTermsArgs search_terms_args =
117 TemplateURLRef::SearchTermsArgs(string16()); 118 TemplateURLRef::SearchTermsArgs(string16());
118 search_terms_args.omnibox_start_margin = start_margin; 119 search_terms_args.omnibox_start_margin = start_margin;
119 search_terms_args.append_extra_query_params = append_extra_query_params; 120 search_terms_args.append_extra_query_params = append_extra_query_params;
120 return GURL(ref.ReplaceSearchTerms(search_terms_args)); 121 return GURL(ref.ReplaceSearchTerms(search_terms_args));
121 } 122 }
122 123
124 GURL GetNewTabPageURL(Profile* profile) {
125 FieldTrialFlags flags;
126 if (!GetFieldTrialInfo(
127 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
128 &flags, NULL))
129 return GURL();
130
131 if (!GetBoolValueForFlagWithDefault(kUseCacheableNTP, false, flags))
132 return GURL();
133
134 if (!profile)
135 return GURL();
136
137 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
138 if (!template_url)
139 return GURL();
140
141 return TemplateURLRefToGURL(template_url->new_tab_url_ref(),
142 kDisableStartMargin, false);
143 }
144
123 bool MatchesOrigin(const GURL& my_url, const GURL& other_url) { 145 bool MatchesOrigin(const GURL& my_url, const GURL& other_url) {
124 return my_url.host() == other_url.host() && 146 return my_url.host() == other_url.host() &&
125 my_url.port() == other_url.port() && 147 my_url.port() == other_url.port() &&
126 (my_url.scheme() == other_url.scheme() || 148 (my_url.scheme() == other_url.scheme() ||
127 (my_url.SchemeIs(chrome::kHttpsScheme) && 149 (my_url.SchemeIs(chrome::kHttpsScheme) &&
128 other_url.SchemeIs(chrome::kHttpScheme))); 150 other_url.SchemeIs(chrome::kHttpScheme)));
129 } 151 }
130 152
131 bool MatchesAnySearchURL(const GURL& url, TemplateURL* template_url) { 153 bool MatchesAnySearchURL(const GURL& url, TemplateURL* template_url) {
132 GURL search_url = 154 GURL search_url =
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 // expected to be the TemplateURL* for the default search provider. 206 // expected to be the TemplateURL* for the default search provider.
185 // (2) Either it has a secure scheme, or else the user has manually specified a 207 // (2) Either it has a secure scheme, or else the user has manually specified a
186 // --google-base-url and it uses that base URL. (This allows testers to use 208 // --google-base-url and it uses that base URL. (This allows testers to use
187 // --google-base-url to point at non-HTTPS servers, which eases testing.) 209 // --google-base-url to point at non-HTTPS servers, which eases testing.)
188 bool IsSuitableURLForInstant(const GURL& url, const TemplateURL* template_url) { 210 bool IsSuitableURLForInstant(const GURL& url, const TemplateURL* template_url) {
189 return template_url->HasSearchTermsReplacementKey(url) && 211 return template_url->HasSearchTermsReplacementKey(url) &&
190 (url.SchemeIsSecure() || 212 (url.SchemeIsSecure() ||
191 google_util::StartsWithCommandLineGoogleBaseURL(url)); 213 google_util::StartsWithCommandLineGoogleBaseURL(url));
192 } 214 }
193 215
194 // Returns true if |url| matches --instant-new-tab-url.
195 bool IsCommandLineInstantNTPURL(const GURL& url) {
196 const GURL ntp_url(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
197 switches::kInstantNewTabURL));
198 return ntp_url.is_valid() && MatchesOriginAndPath(ntp_url, url);
199 }
200
201 // Returns true if |url| can be used as an Instant URL for |profile|. 216 // Returns true if |url| can be used as an Instant URL for |profile|.
202 bool IsInstantURL(const GURL& url, Profile* profile) { 217 bool IsInstantURL(const GURL& url, Profile* profile) {
203 if (!IsInstantExtendedAPIEnabled()) 218 if (!IsInstantExtendedAPIEnabled())
204 return false; 219 return false;
205 220
206 if (!url.is_valid()) 221 if (!url.is_valid())
207 return false; 222 return false;
208 223
209 if (IsCommandLineInstantNTPURL(url)) 224 const GURL new_tab_url(GetNewTabPageURL(profile));
225 if (new_tab_url.is_valid() && MatchesOriginAndPath(url, new_tab_url))
210 return true; 226 return true;
211 227
212 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); 228 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
213 if (!template_url) 229 if (!template_url)
214 return false; 230 return false;
215 231
216 if (!IsSuitableURLForInstant(url, template_url)) 232 if (!IsSuitableURLForInstant(url, template_url))
217 return false; 233 return false;
218 234
219 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); 235 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref();
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); 398 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
383 if (!IsInstantExtendedAPIEnabled() || 399 if (!IsInstantExtendedAPIEnabled() ||
384 !IsRenderedInInstantProcess(contents, profile)) 400 !IsRenderedInInstantProcess(contents, profile))
385 return false; 401 return false;
386 402
387 if (IsInstantURL(entry->GetVirtualURL(), profile) || 403 if (IsInstantURL(entry->GetVirtualURL(), profile) ||
388 entry->GetVirtualURL() == GetLocalInstantURL(profile)) 404 entry->GetVirtualURL() == GetLocalInstantURL(profile))
389 return GetSearchTermsImpl(contents, entry).empty(); 405 return GetSearchTermsImpl(contents, entry).empty();
390 406
391 return entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL) && 407 return entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL) &&
392 IsCommandLineInstantNTPURL(entry->GetURL()); 408 MatchesOriginAndPath(entry->GetURL(), GetNewTabPageURL(profile));
samarth 2013/08/13 23:47:59 In the previous check you check that GetNewTabPage
Jered 2013/08/14 15:41:49 Done.
393 } 409 }
394 410
395 bool IsSuggestPrefEnabled(Profile* profile) { 411 bool IsSuggestPrefEnabled(Profile* profile) {
396 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && 412 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() &&
397 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled); 413 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled);
398 } 414 }
399 415
400 GURL GetInstantURL(Profile* profile, int start_margin) { 416 GURL GetInstantURL(Profile* profile, int start_margin) {
401 if (!IsInstantExtendedAPIEnabled() || !IsSuggestPrefEnabled(profile)) 417 if (!IsInstantExtendedAPIEnabled() || !IsSuggestPrefEnabled(profile))
402 return GURL(); 418 return GURL();
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 FieldTrialFlags flags; 468 FieldTrialFlags flags;
453 if (GetFieldTrialInfo( 469 if (GetFieldTrialInfo(
454 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName), 470 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
455 &flags, NULL)) { 471 &flags, NULL)) {
456 return GetBoolValueForFlagWithDefault(kHideVerbatimFlagName, false, flags); 472 return GetBoolValueForFlagWithDefault(kHideVerbatimFlagName, false, flags);
457 } 473 }
458 return false; 474 return false;
459 } 475 }
460 476
461 bool ShouldShowInstantNTP() { 477 bool ShouldShowInstantNTP() {
462 // If the instant-new-tab-url flag is set, we'll always just load the NTP
463 // directly instead of preloading contents using InstantNTP.
464 const CommandLine* command_line = CommandLine::ForCurrentProcess();
465 if (command_line->HasSwitch(switches::kInstantNewTabURL))
466 return false;
467
468 FieldTrialFlags flags; 478 FieldTrialFlags flags;
469 if (GetFieldTrialInfo( 479 if (GetFieldTrialInfo(
470 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName), 480 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
471 &flags, NULL)) { 481 &flags, NULL)) {
482 // If the use_cacheable_ntp field trial flag is set, load the NTP directly
samarth 2013/08/13 23:47:59 Please make this into a helper (ShouldUseCacheable
Jered 2013/08/14 15:41:49 Done.
483 // instead of preloading contents using InstantNTP.
484 if (GetBoolValueForFlagWithDefault(kUseCacheableNTP, false, flags))
485 return false;
472 return GetBoolValueForFlagWithDefault(kShowNtpFlagName, true, flags); 486 return GetBoolValueForFlagWithDefault(kShowNtpFlagName, true, flags);
473 } 487 }
474 return true; 488 return true;
475 } 489 }
476 490
477 bool ShouldShowRecentTabsOnNTP() { 491 bool ShouldShowRecentTabsOnNTP() {
478 FieldTrialFlags flags; 492 FieldTrialFlags flags;
479 if (GetFieldTrialInfo( 493 if (GetFieldTrialInfo(
480 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName), 494 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
481 &flags, NULL)) { 495 &flags, NULL)) {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 577
564 bool HandleNewTabURLRewrite(GURL* url, 578 bool HandleNewTabURLRewrite(GURL* url,
565 content::BrowserContext* browser_context) { 579 content::BrowserContext* browser_context) {
566 if (!IsInstantExtendedAPIEnabled()) 580 if (!IsInstantExtendedAPIEnabled())
567 return false; 581 return false;
568 582
569 if (!url->SchemeIs(chrome::kChromeUIScheme) || 583 if (!url->SchemeIs(chrome::kChromeUIScheme) ||
570 url->host() != chrome::kChromeUINewTabHost) 584 url->host() != chrome::kChromeUINewTabHost)
571 return false; 585 return false;
572 586
573 const GURL ntp_url(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 587 GURL new_tab_url(GetNewTabPageURL(
samarth 2013/08/13 23:47:59 nit: fits on one line?
Jered 2013/08/14 15:41:49 Split out profile.
574 switches::kInstantNewTabURL)); 588 Profile::FromBrowserContext(browser_context)));
575 if (!ntp_url.is_valid()) 589 if (!new_tab_url.is_valid())
576 return false; 590 return false;
577 591
578 *url = ntp_url; 592 *url = new_tab_url;
579 return true; 593 return true;
580 } 594 }
581 595
582 bool HandleNewTabURLReverseRewrite(GURL* url, 596 bool HandleNewTabURLReverseRewrite(GURL* url,
583 content::BrowserContext* browser_context) { 597 content::BrowserContext* browser_context) {
584 if (!IsInstantExtendedAPIEnabled()) 598 if (!IsInstantExtendedAPIEnabled())
585 return false; 599 return false;
586 600
587 const GURL ntp_url(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 601 GURL new_tab_url(GetNewTabPageURL(
588 switches::kInstantNewTabURL)); 602 Profile::FromBrowserContext(browser_context)));
589 if (!MatchesOriginAndPath(ntp_url, *url)) 603 if (!MatchesOriginAndPath(new_tab_url, *url))
590 return false; 604 return false;
591 605
592 *url = GURL(chrome::kChromeUINewTabURL); 606 *url = GURL(chrome::kChromeUINewTabURL);
593 return true; 607 return true;
594 } 608 }
595 609
596 void SetInstantSupportStateInNavigationEntry(InstantSupportState state, 610 void SetInstantSupportStateInNavigationEntry(InstantSupportState state,
597 content::NavigationEntry* entry) { 611 content::NavigationEntry* entry) {
598 if (!entry) 612 if (!entry)
599 return; 613 return;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 bool default_value, 706 bool default_value,
693 const FieldTrialFlags& flags) { 707 const FieldTrialFlags& flags) {
694 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); 708 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags);
695 } 709 }
696 710
697 void ResetInstantExtendedOptInStateGateForTest() { 711 void ResetInstantExtendedOptInStateGateForTest() {
698 instant_extended_opt_in_state_gate = false; 712 instant_extended_opt_in_state_gate = false;
699 } 713 }
700 714
701 } // namespace chrome 715 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698