| Index: chrome/browser/search/search.cc
|
| ===================================================================
|
| --- chrome/browser/search/search.cc (revision 207943)
|
| +++ 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"
|
| @@ -101,12 +102,6 @@
|
| 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);
|
| @@ -167,42 +162,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;
|
| + return instant_url.is_valid() &&
|
| + (MatchesOriginAndPath(url, instant_url) ||
|
| + (extended_api_enabled && MatchesAnySearchURL(url, template_url)));
|
| }
|
|
|
| string16 GetSearchTermsImpl(const content::WebContents* contents,
|
| @@ -221,7 +212,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)
|
| @@ -301,21 +292,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;
|
| }
|
|
|
| @@ -473,37 +454,37 @@
|
| 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();
|
| - }
|
| - 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;
|
| +
|
| + GURL::Replacements replacements;
|
| +
|
| + // 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()).
|
| + const std::string secure_scheme(chrome::kHttpsScheme);
|
| + if (extended_api_enabled && !instant_url.SchemeIsSecure() &&
|
| + !google_util::StartsWithCommandLineGoogleBaseURL(instant_url))
|
| replacements.SetSchemeStr(secure_scheme);
|
| - instant_url = instant_url.ReplaceComponents(replacements);
|
| +
|
| + // If the user specified additional query params on the command line, add
|
| + // them.
|
| + std::string query_params(CommandLine::ForCurrentProcess()->
|
| + GetSwitchValueASCII(switches::kExtraSearchQueryParams));
|
| + if (!query_params.empty()) {
|
| + const std::string existing_query_params(instant_url.query());
|
| + if (!existing_query_params.empty())
|
| + query_params += "&" + existing_query_params;
|
| + replacements.SetQueryStr(query_params);
|
| }
|
|
|
| - return instant_url;
|
| + return instant_url.ReplaceComponents(replacements);
|
| }
|
|
|
| GURL GetLocalInstantURL(Profile* profile) {
|
| @@ -713,27 +694,6 @@
|
| 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)
|
|
|