| Index: chrome/browser/ui/search/search.cc
|
| diff --git a/chrome/browser/ui/search/search.cc b/chrome/browser/ui/search/search.cc
|
| index b83d97e6419322ef20cb0087847dcdd219a81ba2..eeae36ce18eadf2e4b026a3230f21b525535def4 100644
|
| --- a/chrome/browser/ui/search/search.cc
|
| +++ b/chrome/browser/ui/search/search.cc
|
| @@ -1,4 +1,4 @@
|
| -// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Copyright 2012 The Chromium Authors. All rights reserved.
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| @@ -7,14 +7,17 @@
|
| #include "base/command_line.h"
|
| #include "base/metrics/field_trial.h"
|
| #include "base/metrics/histogram.h"
|
| +#include "base/prefs/pref_service.h"
|
| #include "base/string_split.h"
|
| #include "base/strings/string_number_conversions.h"
|
| #include "chrome/browser/instant/instant_service.h"
|
| #include "chrome/browser/instant/instant_service_factory.h"
|
| +#include "chrome/browser/prefs/pref_registry_syncable.h"
|
| #include "chrome/browser/profiles/profile.h"
|
| #include "chrome/browser/search_engines/template_url_service.h"
|
| #include "chrome/browser/search_engines/template_url_service_factory.h"
|
| #include "chrome/common/chrome_switches.h"
|
| +#include "chrome/common/pref_names.h"
|
| #include "chrome/common/url_constants.h"
|
| #include "content/public/browser/navigation_entry.h"
|
| #include "content/public/browser/render_process_host.h"
|
| @@ -22,6 +25,15 @@
|
|
|
| namespace {
|
|
|
| +// The default value we should assign to the instant_extended.enabled pref.
|
| +// As with other prefs, the default is used only when the user hasn't toggled
|
| +// the pref explicitly.
|
| +enum InstantExtendedDefault {
|
| + INSTANT_DEFAULT_ON, // Default the pref to be enabled.
|
| + INSTANT_USE_EXISTING, // Use the current value of the instant.enabled pref.
|
| + INSTANT_DEFAULT_OFF, // Default the pref to be disabled.
|
| +};
|
| +
|
| // Configuration options for Embedded Search.
|
| // InstantExtended field trials are named in such a way that we can parse out
|
| // the experiment configuration from the trial's group name in order to give
|
| @@ -34,8 +46,8 @@ const uint64 kEmbeddedPageVersionDisabled = 0;
|
| const uint64 kEmbeddedPageVersionDefault = 2;
|
|
|
| const char kInstantExtendedActivationName[] = "instant";
|
| -const chrome::search::InstantExtendedDefault kInstantExtendedActivationDefault =
|
| - chrome::search::INSTANT_DEFAULT_ON;
|
| +const InstantExtendedDefault kInstantExtendedActivationDefault =
|
| + INSTANT_DEFAULT_ON;
|
|
|
| // Constants for the field trial name and group prefix.
|
| const char kInstantExtendedFieldTrialName[] = "InstantExtended";
|
| @@ -45,16 +57,6 @@ const char kGroupNumberPrefix[] = "Group";
|
| // be ignored and Instant Extended will not be enabled by default.
|
| const char kDisablingSuffix[] = "DISABLED";
|
|
|
| -chrome::search::InstantExtendedDefault InstantExtendedDefaultFromInt64(
|
| - int64 default_value) {
|
| - switch (default_value) {
|
| - case 0: return chrome::search::INSTANT_DEFAULT_ON;
|
| - case 1: return chrome::search::INSTANT_USE_EXISTING;
|
| - case 2: return chrome::search::INSTANT_DEFAULT_OFF;
|
| - default: return chrome::search::INSTANT_USE_EXISTING;
|
| - }
|
| -}
|
| -
|
| TemplateURL* GetDefaultSearchProviderTemplateURL(Profile* profile) {
|
| TemplateURLService* template_url_service =
|
| TemplateURLServiceFactory::GetForProfile(profile);
|
| @@ -100,25 +102,12 @@ bool MatchesAnySearchURL(const GURL& url, TemplateURL* template_url) {
|
| }
|
|
|
| enum OptInState {
|
| - // The user has not manually opted-in to or opted-out of InstantExtended.
|
| - NOT_SET,
|
| - // The user has opted-in to InstantExtended.
|
| - OPT_IN,
|
| - // The user has opted-out of InstantExtended.
|
| - OPT_OUT,
|
| - // Number of enum entries, used for UMA histogram reporting macros.
|
| + NOT_SET, // The user has not manually opted into or out of InstantExtended.
|
| + OPT_IN, // The user has opted-in to InstantExtended.
|
| + OPT_OUT, // The user has opted-out of InstantExtended.
|
| OPT_IN_STATE_ENUM_COUNT,
|
| };
|
|
|
| -void RecordInstantExtendedOptInState(OptInState state) {
|
| - static bool recorded = false;
|
| - if (!recorded) {
|
| - UMA_HISTOGRAM_ENUMERATION("InstantExtended.OptInState", state,
|
| - OPT_IN_STATE_ENUM_COUNT);
|
| - recorded = true;
|
| - }
|
| -}
|
| -
|
| } // namespace
|
|
|
| namespace chrome {
|
| @@ -129,29 +118,6 @@ const char kInstantExtendedSearchTermsKey[] = "search_terms";
|
| const char kLocalOmniboxPopupURL[] =
|
| "chrome://local-omnibox-popup/local-omnibox-popup.html";
|
|
|
| -InstantExtendedDefault GetInstantExtendedDefaultSetting() {
|
| - // Check the command-line/about:flags setting first, which should have
|
| - // precedence and allows the trial to not be reported (if it's never queried).
|
| - const CommandLine* command_line = CommandLine::ForCurrentProcess();
|
| - if (command_line->HasSwitch(switches::kDisableInstantExtendedAPI))
|
| - return chrome::search::INSTANT_DEFAULT_OFF;
|
| - if (command_line->HasSwitch(switches::kEnableInstantExtendedAPI))
|
| - return chrome::search::INSTANT_DEFAULT_ON;
|
| -
|
| - FieldTrialFlags flags;
|
| - if (GetFieldTrialInfo(
|
| - base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
|
| - &flags, NULL)) {
|
| - uint64 trial_default = GetUInt64ValueForFlagWithDefault(
|
| - kInstantExtendedActivationName,
|
| - kInstantExtendedActivationDefault,
|
| - flags);
|
| - return InstantExtendedDefaultFromInt64(trial_default);
|
| - }
|
| -
|
| - return kInstantExtendedActivationDefault;
|
| -}
|
| -
|
| bool IsInstantExtendedAPIEnabled(const Profile* profile) {
|
| return EmbeddedSearchPageVersion(profile) != kEmbeddedPageVersionDisabled;
|
| }
|
| @@ -162,31 +128,39 @@ uint64 EmbeddedSearchPageVersion(const Profile* profile) {
|
| if (!profile || profile->IsOffTheRecord())
|
| return kEmbeddedPageVersionDisabled;
|
|
|
| + static uint64 embedded_page_version = kuint64max;
|
| + if (embedded_page_version != kuint64max)
|
| + return embedded_page_version;
|
| +
|
| + embedded_page_version = kEmbeddedPageVersionDisabled;
|
| +
|
| // Check the command-line/about:flags setting first, which should have
|
| // precedence and allows the trial to not be reported (if it's never queried).
|
| const CommandLine* command_line = CommandLine::ForCurrentProcess();
|
| - if (command_line->HasSwitch(switches::kDisableInstantExtendedAPI)) {
|
| - RecordInstantExtendedOptInState(OPT_OUT);
|
| - return kEmbeddedPageVersionDisabled;
|
| - }
|
| if (command_line->HasSwitch(switches::kEnableInstantExtendedAPI)) {
|
| // The user has set the about:flags switch to Enabled - give the default
|
| // UI version.
|
| - RecordInstantExtendedOptInState(OPT_IN);
|
| - return kEmbeddedPageVersionDefault;
|
| - }
|
| -
|
| - RecordInstantExtendedOptInState(NOT_SET);
|
| - FieldTrialFlags flags;
|
| - if (GetFieldTrialInfo(
|
| - base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
|
| - &flags, NULL)) {
|
| - return GetUInt64ValueForFlagWithDefault(kEmbeddedPageVersionFlagName,
|
| - kEmbeddedPageVersionDefault,
|
| - flags);
|
| + UMA_HISTOGRAM_ENUMERATION("InstantExtended.OptInState", OPT_IN,
|
| + OPT_IN_STATE_ENUM_COUNT);
|
| + embedded_page_version = kEmbeddedPageVersionDefault;
|
| + } else if (command_line->HasSwitch(switches::kDisableInstantExtendedAPI)) {
|
| + UMA_HISTOGRAM_ENUMERATION("InstantExtended.OptInState", OPT_OUT,
|
| + OPT_IN_STATE_ENUM_COUNT);
|
| + } else {
|
| + UMA_HISTOGRAM_ENUMERATION("InstantExtended.OptInState", NOT_SET,
|
| + OPT_IN_STATE_ENUM_COUNT);
|
| + FieldTrialFlags flags;
|
| + if (GetFieldTrialInfo(
|
| + base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
|
| + &flags, NULL)) {
|
| + embedded_page_version = GetUInt64ValueForFlagWithDefault(
|
| + kEmbeddedPageVersionFlagName,
|
| + kEmbeddedPageVersionDefault,
|
| + flags);
|
| + }
|
| }
|
|
|
| - return kEmbeddedPageVersionDisabled;
|
| + return embedded_page_version;
|
| }
|
|
|
| bool IsQueryExtractionEnabled(const Profile* profile) {
|
| @@ -279,17 +253,133 @@ bool ShouldAssignURLToInstantRenderer(const GURL& url, Profile* profile) {
|
| template_url);
|
| }
|
|
|
| +void RegisterUserPrefs(PrefRegistrySyncable* registry) {
|
| + registry->RegisterBooleanPref(prefs::kInstantConfirmDialogShown, false,
|
| + PrefRegistrySyncable::SYNCABLE_PREF);
|
| + registry->RegisterBooleanPref(prefs::kInstantEnabled, false,
|
| + PrefRegistrySyncable::SYNCABLE_PREF);
|
| + // This default is overridden by the InstantService constructor.
|
| + registry->RegisterBooleanPref(prefs::kInstantExtendedEnabled, false,
|
| + PrefRegistrySyncable::SYNCABLE_PREF);
|
| +}
|
| +
|
| +const char* GetInstantPrefName(const Profile* profile) {
|
| + return IsInstantExtendedAPIEnabled(profile) ? prefs::kInstantExtendedEnabled :
|
| + prefs::kInstantEnabled;
|
| +}
|
| +
|
| +bool IsInstantPrefEnabled(Profile* profile) {
|
| + if (!profile || profile->IsOffTheRecord())
|
| + return false;
|
| +
|
| + const PrefService* prefs = profile->GetPrefs();
|
| + if (!prefs)
|
| + return false;
|
| +
|
| + return prefs->GetBoolean(GetInstantPrefName(profile));
|
| +}
|
| +
|
| +void SetInstantExtendedPrefDefault(Profile* profile) {
|
| + PrefService* prefs = profile ? profile->GetPrefs() : NULL;
|
| + if (!prefs)
|
| + return;
|
| +
|
| + bool pref_default = false;
|
| +
|
| + // Check the command-line/about:flags setting first, which should have
|
| + // precedence and allows the trial to not be reported (if it's never queried).
|
| + const CommandLine* command_line = CommandLine::ForCurrentProcess();
|
| + if (command_line->HasSwitch(switches::kEnableInstantExtendedAPI)) {
|
| + pref_default = true;
|
| + } else if (!command_line->HasSwitch(switches::kDisableInstantExtendedAPI)) {
|
| + uint64 trial_default = kInstantExtendedActivationDefault;
|
| +
|
| + FieldTrialFlags flags;
|
| + if (GetFieldTrialInfo(
|
| + base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName),
|
| + &flags, NULL)) {
|
| + trial_default = GetUInt64ValueForFlagWithDefault(
|
| + kInstantExtendedActivationName,
|
| + kInstantExtendedActivationDefault,
|
| + flags);
|
| + }
|
| +
|
| + if (trial_default == INSTANT_DEFAULT_ON) {
|
| + pref_default = true;
|
| + } else if (trial_default != INSTANT_DEFAULT_OFF) {
|
| + pref_default = prefs->GetBoolean(prefs::kInstantEnabled);
|
| + }
|
| + }
|
| +
|
| +// TODO(sreeram): Fix when https://codereview.chromium.org/12315116/ lands.
|
| +#if 0
|
| + prefs->SetDefaultPrefValue(prefs::kInstantExtendedEnabled,
|
| + Value::CreateBooleanValue(pref_default));
|
| +#else
|
| + prefs->SetBoolean(prefs::kInstantExtendedEnabled, pref_default);
|
| +#endif
|
| +}
|
| +
|
| +GURL GetInstantURL(Profile* profile) {
|
| + bool extended_api_enabled = IsInstantExtendedAPIEnabled(profile);
|
| +
|
| + const PrefService* prefs = profile && !profile->IsOffTheRecord() ?
|
| + profile->GetPrefs() : NULL;
|
| + if (!IsInstantPrefEnabled(profile) &&
|
| + !(extended_api_enabled && prefs &&
|
| + prefs->GetBoolean(prefs::kSearchSuggestEnabled)))
|
| + return GURL();
|
| +
|
| + TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
|
| + if (!template_url)
|
| + return GURL();
|
| +
|
| + 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());
|
| + if (!template_url->HasSearchTermsReplacementKey(coerced_url))
|
| + return GURL();
|
| + }
|
| + return instant_url;
|
| + }
|
| +
|
| + GURL instant_url = TemplateURLRefToGURL(template_url->instant_url_ref());
|
| +
|
| + if (extended_api_enabled) {
|
| + // Extended mode won't work if the search terms replacement key is absent.
|
| + if (!template_url->HasSearchTermsReplacementKey(instant_url))
|
| + return GURL();
|
| +
|
| + // Extended mode requires HTTPS. Force it if necessary.
|
| + if (!instant_url.SchemeIsSecure()) {
|
| + GURL::Replacements replacements;
|
| + replacements.SetSchemeStr(chrome::kHttpsScheme);
|
| + instant_url = instant_url.ReplaceComponents(replacements);
|
| + }
|
| + }
|
| +
|
| + return instant_url;
|
| +}
|
| +
|
| +bool IsInstantEnabled(Profile* profile) {
|
| + return GetInstantURL(profile).is_valid();
|
| +}
|
| +
|
| void EnableInstantExtendedAPIForTesting() {
|
| CommandLine* cl = CommandLine::ForCurrentProcess();
|
| cl->AppendSwitch(switches::kEnableInstantExtendedAPI);
|
| }
|
|
|
| void EnableQueryExtractionForTesting() {
|
| -#if defined(OS_IOS)
|
| CommandLine* cl = CommandLine::ForCurrentProcess();
|
| +#if defined(OS_IOS)
|
| cl->AppendSwitch(switches::kEnableQueryExtraction);
|
| #else
|
| - EnableInstantExtendedAPIForTesting();
|
| + cl->AppendSwitch(switches::kEnableInstantExtendedAPI);
|
| #endif
|
| }
|
|
|
|
|