Index: chrome/browser/search/search.cc |
=================================================================== |
--- chrome/browser/search/search.cc (revision 208572) |
+++ chrome/browser/search/search.cc (working copy) |
@@ -11,6 +11,7 @@ |
#include "base/rand_util.h" |
#include "base/strings/string_number_conversions.h" |
#include "base/strings/string_split.h" |
+#include "chrome/browser/google/google_util.h" |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/browser/search/instant_service.h" |
#include "chrome/browser/search/instant_service_factory.h" |
@@ -87,10 +88,13 @@ |
return NULL; |
} |
-GURL TemplateURLRefToGURL(const TemplateURLRef& ref, int start_margin) { |
+GURL TemplateURLRefToGURL(const TemplateURLRef& ref, |
+ int start_margin, |
+ bool append_extra_query_params) { |
TemplateURLRef::SearchTermsArgs search_terms_args = |
TemplateURLRef::SearchTermsArgs(string16()); |
search_terms_args.omnibox_start_margin = start_margin; |
+ search_terms_args.append_extra_query_params = append_extra_query_params; |
return GURL(ref.ReplaceSearchTerms(search_terms_args)); |
} |
@@ -102,22 +106,16 @@ |
other_url.SchemeIs(chrome::kHttpScheme))); |
} |
-bool IsCommandLineInstantURL(const GURL& url) { |
- const CommandLine* cl = CommandLine::ForCurrentProcess(); |
- const GURL instant_url(cl->GetSwitchValueASCII(switches::kInstantURL)); |
- return instant_url.is_valid() && MatchesOrigin(url, instant_url); |
-} |
- |
bool MatchesAnySearchURL(const GURL& url, TemplateURL* template_url) { |
GURL search_url = |
- TemplateURLRefToGURL(template_url->url_ref(), kDisableStartMargin); |
+ TemplateURLRefToGURL(template_url->url_ref(), kDisableStartMargin, false); |
if (search_url.is_valid() && MatchesOriginAndPath(url, search_url)) |
return true; |
// "URLCount() - 1" because we already tested url_ref above. |
for (size_t i = 0; i < template_url->URLCount() - 1; ++i) { |
TemplateURLRef ref(template_url, i); |
- search_url = TemplateURLRefToGURL(ref, kDisableStartMargin); |
+ search_url = TemplateURLRefToGURL(ref, kDisableStartMargin, false); |
if (search_url.is_valid() && MatchesOriginAndPath(url, search_url)) |
return true; |
} |
@@ -168,42 +166,38 @@ |
return instant_service->IsInstantProcess(process_host->GetID()); |
} |
+// Returns true if |url| passes some basic checks that must succeed for it to be |
+// usable as an instant URL: |
+// (1) It contains the search terms replacement key of |template_url|, which is |
+// expected to be the TemplateURL* for the default search provider. |
+// (2) Either it has a secure scheme, or else the user has manually specified a |
+// --google-base-url and it uses that base URL. (This allows testers to use |
+// --google-base-url to point at non-HTTPS servers, which eases testing.) |
+bool IsSuitableURLForInstant(const GURL& url, const TemplateURL* template_url) { |
+ return template_url->HasSearchTermsReplacementKey(url) && |
+ (url.SchemeIsSecure() || |
+ google_util::StartsWithCommandLineGoogleBaseURL(url)); |
+} |
+ |
// Returns true if |url| can be used as an Instant URL for |profile|. |
bool IsInstantURL(const GURL& url, Profile* profile) { |
+ if (!url.is_valid()) |
+ return false; |
+ |
TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
if (!template_url) |
return false; |
- const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); |
const bool extended_api_enabled = IsInstantExtendedAPIEnabled(); |
- GURL effective_url = url; |
- |
- if (IsCommandLineInstantURL(url)) |
- effective_url = CoerceCommandLineURLToTemplateURL(url, instant_url_ref, |
- kDisableStartMargin); |
- |
- if (!effective_url.is_valid()) |
+ if (extended_api_enabled && !IsSuitableURLForInstant(url, template_url)) |
return false; |
- if (extended_api_enabled && !effective_url.SchemeIsSecure()) |
- return false; |
- |
- if (extended_api_enabled && |
- !template_url->HasSearchTermsReplacementKey(effective_url)) |
- return false; |
- |
+ const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); |
const GURL instant_url = |
- TemplateURLRefToGURL(instant_url_ref, kDisableStartMargin); |
- if (!instant_url.is_valid()) |
- return false; |
- |
- if (MatchesOriginAndPath(effective_url, instant_url)) |
- return true; |
- |
- if (extended_api_enabled && MatchesAnySearchURL(effective_url, template_url)) |
- return true; |
- |
- return false; |
+ TemplateURLRefToGURL(instant_url_ref, kDisableStartMargin, false); |
+ return instant_url.is_valid() && |
+ (MatchesOriginAndPath(url, instant_url) || |
+ (extended_api_enabled && MatchesAnySearchURL(url, template_url))); |
} |
string16 GetSearchTermsImpl(const content::WebContents* contents, |
@@ -222,7 +216,7 @@ |
Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
#if !defined(OS_IOS) && !defined(OS_ANDROID) |
if (!IsRenderedInInstantProcess(contents, profile) && |
- (contents->GetController().GetLastCommittedEntry() == entry || |
+ ((entry == contents->GetController().GetLastCommittedEntry()) || |
!ShouldAssignURLToInstantRenderer(entry->GetURL(), profile))) |
return string16(); |
#endif // !defined(OS_IOS) && !defined(OS_ANDROID) |
@@ -302,21 +296,11 @@ |
return false; |
} |
-string16 GetSearchTermsFromURL(Profile* profile, const GURL& in_url) { |
- GURL url(in_url); |
+string16 GetSearchTermsFromURL(Profile* profile, const GURL& url) { |
string16 search_terms; |
- |
TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
- if (!template_url) |
- return string16(); |
- |
- if (IsCommandLineInstantURL(url)) |
- url = CoerceCommandLineURLToTemplateURL(url, template_url->url_ref(), |
- kDisableStartMargin); |
- |
- if (url.SchemeIsSecure() && template_url->HasSearchTermsReplacementKey(url)) |
+ if (template_url && IsSuitableURLForInstant(url, template_url)) |
template_url->ExtractSearchTermsFromURL(url, &search_terms); |
- |
return search_terms; |
} |
@@ -474,37 +458,26 @@ |
if (!IsInstantCheckboxEnabled(profile)) |
return GURL(); |
+ // In non-extended mode, the checkbox must be checked. |
const bool extended_api_enabled = IsInstantExtendedAPIEnabled(); |
- |
- // In non-extended mode, the checkbox must be checked. |
if (!extended_api_enabled && !IsInstantCheckboxChecked(profile)) |
return GURL(); |
TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
- CommandLine* cl = CommandLine::ForCurrentProcess(); |
- if (cl->HasSwitch(switches::kInstantURL)) { |
- GURL instant_url(cl->GetSwitchValueASCII(switches::kInstantURL)); |
- if (extended_api_enabled) { |
- // Extended mode won't work if the search terms replacement key is absent. |
- GURL coerced_url = CoerceCommandLineURLToTemplateURL( |
- instant_url, template_url->instant_url_ref(), start_margin); |
- if (!template_url->HasSearchTermsReplacementKey(coerced_url)) |
- return GURL(); |
- } |
+ GURL instant_url = |
+ TemplateURLRefToGURL(template_url->instant_url_ref(), start_margin, true); |
+ |
+ // Extended mode requires HTTPS. Force it unless the base URL was overridden |
+ // on the command line, in which case we allow HTTP (see comments on |
+ // IsSuitableURLForInstant()). |
+ if (!extended_api_enabled || instant_url.SchemeIsSecure() || |
+ google_util::StartsWithCommandLineGoogleBaseURL(instant_url)) |
return instant_url; |
- } |
- GURL instant_url = |
- TemplateURLRefToGURL(template_url->instant_url_ref(), start_margin); |
- if (extended_api_enabled && !instant_url.SchemeIsSecure()) { |
- // Extended mode requires HTTPS. Force it if necessary. |
- const std::string secure_scheme = chrome::kHttpsScheme; |
- GURL::Replacements replacements; |
- replacements.SetSchemeStr(secure_scheme); |
- instant_url = instant_url.ReplaceComponents(replacements); |
- } |
- |
- return instant_url; |
+ GURL::Replacements replacements; |
+ const std::string secure_scheme(chrome::kHttpsScheme); |
+ replacements.SetSchemeStr(secure_scheme); |
+ return instant_url.ReplaceComponents(replacements); |
} |
GURL GetLocalInstantURL(Profile* profile) { |
@@ -726,34 +699,13 @@ |
return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
} |
-// Coerces the commandline Instant URL to look like a template URL, so that we |
-// can extract search terms from it. |
-GURL CoerceCommandLineURLToTemplateURL(const GURL& instant_url, |
- const TemplateURLRef& ref, |
- int start_margin) { |
- GURL search_url = TemplateURLRefToGURL(ref, start_margin); |
- |
- // NOTE(samarth): GURL returns temporaries which we must save because |
- // GURL::Replacements expects the replacements to live until |
- // ReplaceComponents is called. |
- const std::string search_scheme = chrome::kHttpsScheme; |
- const std::string search_host = search_url.host(); |
- const std::string search_port = search_url.port(); |
- |
- GURL::Replacements replacements; |
- replacements.SetSchemeStr(search_scheme); |
- replacements.SetHostStr(search_host); |
- replacements.SetPortStr(search_port); |
- return instant_url.ReplaceComponents(replacements); |
-} |
- |
bool DefaultSearchProviderSupportsInstant(Profile* profile) { |
TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
if (!template_url) |
return false; |
GURL instant_url = TemplateURLRefToGURL(template_url->instant_url_ref(), |
- kDisableStartMargin); |
+ kDisableStartMargin, false); |
// Extended mode instant requires a search terms replacement key. |
return instant_url.is_valid() && |
(!IsInstantExtendedAPIEnabled() || |