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

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: Rebase. 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
« no previous file with comments | « chrome/browser/search/search.h ('k') | chrome/browser/search/search_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 if (!ShouldUseCacheableNTP())
126 return GURL();
127
128 if (!profile)
129 return GURL();
130
131 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
132 if (!template_url)
133 return GURL();
134
135 return TemplateURLRefToGURL(template_url->new_tab_url_ref(),
136 kDisableStartMargin, false);
137 }
138
123 bool MatchesOrigin(const GURL& my_url, const GURL& other_url) { 139 bool MatchesOrigin(const GURL& my_url, const GURL& other_url) {
124 return my_url.host() == other_url.host() && 140 return my_url.host() == other_url.host() &&
125 my_url.port() == other_url.port() && 141 my_url.port() == other_url.port() &&
126 (my_url.scheme() == other_url.scheme() || 142 (my_url.scheme() == other_url.scheme() ||
127 (my_url.SchemeIs(chrome::kHttpsScheme) && 143 (my_url.SchemeIs(chrome::kHttpsScheme) &&
128 other_url.SchemeIs(chrome::kHttpScheme))); 144 other_url.SchemeIs(chrome::kHttpScheme)));
129 } 145 }
130 146
131 bool MatchesAnySearchURL(const GURL& url, TemplateURL* template_url) { 147 bool MatchesAnySearchURL(const GURL& url, TemplateURL* template_url) {
132 GURL search_url = 148 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. 200 // 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 201 // (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 202 // --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.) 203 // --google-base-url to point at non-HTTPS servers, which eases testing.)
188 bool IsSuitableURLForInstant(const GURL& url, const TemplateURL* template_url) { 204 bool IsSuitableURLForInstant(const GURL& url, const TemplateURL* template_url) {
189 return template_url->HasSearchTermsReplacementKey(url) && 205 return template_url->HasSearchTermsReplacementKey(url) &&
190 (url.SchemeIsSecure() || 206 (url.SchemeIsSecure() ||
191 google_util::StartsWithCommandLineGoogleBaseURL(url)); 207 google_util::StartsWithCommandLineGoogleBaseURL(url));
192 } 208 }
193 209
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|. 210 // Returns true if |url| can be used as an Instant URL for |profile|.
202 bool IsInstantURL(const GURL& url, Profile* profile) { 211 bool IsInstantURL(const GURL& url, Profile* profile) {
203 if (!IsInstantExtendedAPIEnabled()) 212 if (!IsInstantExtendedAPIEnabled())
204 return false; 213 return false;
205 214
206 if (!url.is_valid()) 215 if (!url.is_valid())
207 return false; 216 return false;
208 217
209 if (IsCommandLineInstantNTPURL(url)) 218 const GURL new_tab_url(GetNewTabPageURL(profile));
219 if (new_tab_url.is_valid() && MatchesOriginAndPath(url, new_tab_url))
210 return true; 220 return true;
211 221
212 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); 222 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
213 if (!template_url) 223 if (!template_url)
214 return false; 224 return false;
215 225
216 if (!IsSuitableURLForInstant(url, template_url)) 226 if (!IsSuitableURLForInstant(url, template_url))
217 return false; 227 return false;
218 228
219 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); 229 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref();
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 391
382 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); 392 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
383 if (!IsInstantExtendedAPIEnabled() || 393 if (!IsInstantExtendedAPIEnabled() ||
384 !IsRenderedInInstantProcess(contents, profile)) 394 !IsRenderedInInstantProcess(contents, profile))
385 return false; 395 return false;
386 396
387 if (IsInstantURL(entry->GetVirtualURL(), profile) || 397 if (IsInstantURL(entry->GetVirtualURL(), profile) ||
388 entry->GetVirtualURL() == GetLocalInstantURL(profile)) 398 entry->GetVirtualURL() == GetLocalInstantURL(profile))
389 return GetSearchTermsImpl(contents, entry).empty(); 399 return GetSearchTermsImpl(contents, entry).empty();
390 400
401 GURL new_tab_url(GetNewTabPageURL(profile));
391 return entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL) && 402 return entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL) &&
392 IsCommandLineInstantNTPURL(entry->GetURL()); 403 new_tab_url.is_valid() &&
404 MatchesOriginAndPath(entry->GetURL(), new_tab_url);
393 } 405 }
394 406
395 bool IsSuggestPrefEnabled(Profile* profile) { 407 bool IsSuggestPrefEnabled(Profile* profile) {
396 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && 408 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() &&
397 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled); 409 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled);
398 } 410 }
399 411
400 GURL GetInstantURL(Profile* profile, int start_margin) { 412 GURL GetInstantURL(Profile* profile, int start_margin) {
401 if (!IsInstantExtendedAPIEnabled() || !IsSuggestPrefEnabled(profile)) 413 if (!IsInstantExtendedAPIEnabled() || !IsSuggestPrefEnabled(profile))
402 return GURL(); 414 return GURL();
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 bool ShouldHideTopVerbatimMatch() { 463 bool ShouldHideTopVerbatimMatch() {
452 FieldTrialFlags flags; 464 FieldTrialFlags flags;
453 if (GetFieldTrialInfo( 465 if (GetFieldTrialInfo(
454 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName), 466 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
455 &flags, NULL)) { 467 &flags, NULL)) {
456 return GetBoolValueForFlagWithDefault(kHideVerbatimFlagName, false, flags); 468 return GetBoolValueForFlagWithDefault(kHideVerbatimFlagName, false, flags);
457 } 469 }
458 return false; 470 return false;
459 } 471 }
460 472
473 bool ShouldUseCacheableNTP() {
474 FieldTrialFlags flags;
475 if (GetFieldTrialInfo(
476 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
477 &flags, NULL)) {
478 return GetBoolValueForFlagWithDefault(kUseCacheableNTP, false, flags);
479 }
480 return false;
481 }
482
461 bool ShouldShowInstantNTP() { 483 bool ShouldShowInstantNTP() {
462 // If the instant-new-tab-url flag is set, we'll always just load the NTP 484 // If using the cacheable NTP, load the NTP directly instead of preloading its
463 // directly instead of preloading contents using InstantNTP. 485 // contents using InstantNTP.
464 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 486 if (ShouldUseCacheableNTP())
465 if (command_line->HasSwitch(switches::kInstantNewTabURL))
466 return false; 487 return false;
467 488
468 FieldTrialFlags flags; 489 FieldTrialFlags flags;
469 if (GetFieldTrialInfo( 490 if (GetFieldTrialInfo(
470 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName), 491 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
471 &flags, NULL)) { 492 &flags, NULL)) {
472 return GetBoolValueForFlagWithDefault(kShowNtpFlagName, true, flags); 493 return GetBoolValueForFlagWithDefault(kShowNtpFlagName, true, flags);
473 } 494 }
474 return true; 495 return true;
475 } 496 }
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 584
564 bool HandleNewTabURLRewrite(GURL* url, 585 bool HandleNewTabURLRewrite(GURL* url,
565 content::BrowserContext* browser_context) { 586 content::BrowserContext* browser_context) {
566 if (!IsInstantExtendedAPIEnabled()) 587 if (!IsInstantExtendedAPIEnabled())
567 return false; 588 return false;
568 589
569 if (!url->SchemeIs(chrome::kChromeUIScheme) || 590 if (!url->SchemeIs(chrome::kChromeUIScheme) ||
570 url->host() != chrome::kChromeUINewTabHost) 591 url->host() != chrome::kChromeUINewTabHost)
571 return false; 592 return false;
572 593
573 const GURL ntp_url(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 594 Profile* profile = Profile::FromBrowserContext(browser_context);
574 switches::kInstantNewTabURL)); 595 GURL new_tab_url(GetNewTabPageURL(profile));
575 if (!ntp_url.is_valid()) 596 if (!new_tab_url.is_valid())
576 return false; 597 return false;
577 598
578 *url = ntp_url; 599 *url = new_tab_url;
579 return true; 600 return true;
580 } 601 }
581 602
582 bool HandleNewTabURLReverseRewrite(GURL* url, 603 bool HandleNewTabURLReverseRewrite(GURL* url,
583 content::BrowserContext* browser_context) { 604 content::BrowserContext* browser_context) {
584 if (!IsInstantExtendedAPIEnabled()) 605 if (!IsInstantExtendedAPIEnabled())
585 return false; 606 return false;
586 607
587 const GURL ntp_url(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 608 Profile* profile = Profile::FromBrowserContext(browser_context);
588 switches::kInstantNewTabURL)); 609 GURL new_tab_url(GetNewTabPageURL(profile));
589 if (!MatchesOriginAndPath(ntp_url, *url)) 610 if (!new_tab_url.is_valid() || !MatchesOriginAndPath(new_tab_url, *url))
590 return false; 611 return false;
591 612
592 *url = GURL(chrome::kChromeUINewTabURL); 613 *url = GURL(chrome::kChromeUINewTabURL);
593 return true; 614 return true;
594 } 615 }
595 616
596 void SetInstantSupportStateInNavigationEntry(InstantSupportState state, 617 void SetInstantSupportStateInNavigationEntry(InstantSupportState state,
597 content::NavigationEntry* entry) { 618 content::NavigationEntry* entry) {
598 if (!entry) 619 if (!entry)
599 return; 620 return;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 bool default_value, 713 bool default_value,
693 const FieldTrialFlags& flags) { 714 const FieldTrialFlags& flags) {
694 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); 715 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags);
695 } 716 }
696 717
697 void ResetInstantExtendedOptInStateGateForTest() { 718 void ResetInstantExtendedOptInStateGateForTest() {
698 instant_extended_opt_in_state_gate = false; 719 instant_extended_opt_in_state_gate = false;
699 } 720 }
700 721
701 } // namespace chrome 722 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/search/search.h ('k') | chrome/browser/search/search_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698