Skip to content

Commit

Permalink
Add a preference to control Windows desktop search redirection.
Browse files Browse the repository at this point in the history
This CL adds a new preference called
"windows_desktop_search_redirection", that controls whether Windows
desktop searches should be redirected to the default search engine.

The preference can be enabled, disabled or unset. Enabled and disabled
mean that redirection of desktop searches has been explicitly enabled
or disabled. Unset means no value has been explicitly set and that the
browser should ask the user whether they want to use the feature
(prompt + checkbox to edit the preference from chrome://settings will
be implemented in an upcoming CL).

BUG=539963

Review URL: https://codereview.chromium.org/1477783004

Cr-Commit-Position: refs/heads/master@{#363064}
  • Loading branch information
fdoray authored and Commit bot committed Dec 3, 2015
1 parent 1fd302b commit ee51106
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 47 deletions.
8 changes: 8 additions & 0 deletions chrome/app/generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -6508,6 +6508,14 @@ Keep your key file in a safe place. You will need it to create new versions of y
<message name="IDS_FLAGS_WIFI_CREDENTIAL_SYNC_DESCRIPTION" desc="Decription for the flag to enable WiFi credential sync, a feature which enables synchronizing WiFi network settings across devices.">
Enables synchronizing WiFi network settings across devices. When enabled, the WiFi credential datatype is registered with Chrome Sync, and WiFi credentials are synchronized subject to user preferences. (See also, chrome://settings/syncSetup.)
</message>
<if expr="is_win">
<message name="IDS_FLAGS_WINDOWS_DESKTOP_SEARCH_REDIRECTION_DESCRIPTION" desc="Description for the flag to enable a preference that allows redirection of Windows desktop searches to the default search engine.">
Enables a preference that allows redirection of Windows desktop searches to the default search engine.
</message>
<message name="IDS_FLAGS_WINDOWS_DESKTOP_SEARCH_REDIRECTION_NAME" desc="Title for the flag to enable a preference that allows redirection of Windows desktop searches to the default search engine.">
Windows desktop search redirection preference
</message>
</if>
<message name="IDS_FLAGS_SYNC_SANDBOX_NAME" desc="Name for the flag that causes Chrome to use the sandbox (testing) server for Sync.">
Use Chrome Sync sandbox
</message>
Expand Down
19 changes: 15 additions & 4 deletions chrome/browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@
#include "ui/message_center/message_center_switches.h"
#endif

#if defined(USE_ASH)
#include "ash/ash_switches.h"
#endif

#if defined(OS_CHROMEOS)
#include "chromeos/chromeos_switches.h"
#include "third_party/cros_system_api/switches/chrome_switches.h"
#endif

#if defined(OS_WIN)
#include "components/search_engines/desktop_search_win.h"
#endif // defined(OS_WIN)

#if defined(ENABLE_APP_LIST)
#include "ui/app_list/app_list_switches.h"
#endif
Expand All @@ -88,6 +88,10 @@
#include "chrome/browser/ui/webui/print_preview/print_preview_distiller.h"
#endif

#if defined(USE_ASH)
#include "ash/ash_switches.h"
#endif

#if defined(USE_OZONE)
#include "ui/ozone/public/ozone_switches.h"
#endif
Expand Down Expand Up @@ -2078,6 +2082,13 @@ const FeatureEntry kFeatureEntries[] = {
IDS_FLAGS_ENABLE_MATERIAL_DESIGN_HISTORY_DESCRIPTION,
kOsDesktop | kOsAndroid,
SINGLE_VALUE_TYPE(switches::kEnableMaterialDesignHistory)},
#if defined(OS_WIN)
{"enable-windows-desktop-search-redirection",
IDS_FLAGS_WINDOWS_DESKTOP_SEARCH_REDIRECTION_NAME,
IDS_FLAGS_WINDOWS_DESKTOP_SEARCH_REDIRECTION_DESCRIPTION,
kOsWin,
FEATURE_VALUE_TYPE(kWindowsDesktopSearchRedirectionFeature)},
#endif // defined(OS_WIN)
// NOTE: Adding new command-line switches requires adding corresponding
// entries to enum "LoginCustomFlags" in histograms.xml. See note in
// histograms.xml and don't forget to run AboutFlagsHistogramTest unit test.
Expand Down
7 changes: 7 additions & 0 deletions chrome/browser/search_engines/template_url_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include "components/search_engines/search_engines_pref_names.h"
#include "components/search_engines/template_url_service.h"

#if defined(OS_WIN)
#include "components/search_engines/desktop_search_win.h"
#endif // defined(OS_WIN)

#if defined(ENABLE_RLZ)
#include "components/rlz/rlz_tracker.h"
#endif
Expand Down Expand Up @@ -79,6 +83,9 @@ void TemplateURLServiceFactory::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
DefaultSearchManager::RegisterProfilePrefs(registry);
TemplateURLService::RegisterProfilePrefs(registry);
#if defined(OS_WIN)
RegisterWindowsDesktopSearchRedirectionPref(registry);
#endif
}

content::BrowserContext* TemplateURLServiceFactory::GetBrowserContextToUse(
Expand Down
11 changes: 5 additions & 6 deletions chrome/browser/ui/startup/startup_browser_creator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@

#if defined(OS_WIN)
#include "chrome/browser/metrics/jumplist_metrics_win.h"
#include "components/search_engines/detect_desktop_search_win.h"
#include "components/search_engines/search_engines_switches.h"
#include "components/search_engines/desktop_search_win.h"
#endif

#if defined(ENABLE_PRINT_PREVIEW)
Expand Down Expand Up @@ -553,16 +552,16 @@ std::vector<GURL> StartupBrowserCreator::GetURLsFromCommandLine(
GURL url = GURL(param.MaybeAsASCII());

#if defined(OS_WIN)
if (command_line.HasSwitch(
switches::kUseDefaultSearchProviderForDesktopSearch)) {
if (ShouldRedirectWindowsDesktopSearchToDefaultSearchEngine(
profile->GetPrefs())) {
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile);
DCHECK(template_url_service);
base::string16 search_terms;
if (DetectWindowsDesktopSearch(
url, template_url_service->search_terms_data(), &search_terms)) {
GURL search_url(GetDefaultSearchURLForSearchTerms(template_url_service,
search_terms));
const GURL search_url(GetDefaultSearchURLForSearchTerms(
template_url_service, search_terms));
if (search_url.is_valid()) {
urls.push_back(search_url);
continue;
Expand Down
2 changes: 2 additions & 0 deletions chrome/browser/ui/startup/startup_browser_creator.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class StartupBrowserCreator {
ReadingWasRestartedAfterRestart);
FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, UpdateWithTwoProfiles);
FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, LastUsedProfileActivated);
FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorWinTest,
GetURLsFromCommandLineWithDesktopSearchURL);

// Returns the list of URLs to open from the command line. The returned
// vector is empty if the user didn't specify any URLs on the command line.
Expand Down
90 changes: 90 additions & 0 deletions chrome/browser/ui/startup/startup_browser_creator_win_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2015 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.

#include "chrome/browser/ui/startup/startup_browser_creator.h"

#include <vector>

#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/macros.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/search_engines/template_url_service_factory_test_util.h"
#include "chrome/test/base/testing_profile.h"
#include "components/search_engines/desktop_search_win.h"
#include "components/search_engines/util.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

class StartupBrowserCreatorWinTest : public testing::Test {
public:
StartupBrowserCreatorWinTest() {}

protected:
void SetWindowsDesktopSearchFeatureEnabled(bool enabled) {
base::FeatureList::ClearInstanceForTesting();
scoped_ptr<base::FeatureList> feature_list(new base::FeatureList);
if (enabled) {
feature_list->InitializeFromCommandLine(
kWindowsDesktopSearchRedirectionFeature.name, std::string());
}
base::FeatureList::SetInstance(std::move(feature_list));
}

private:
content::TestBrowserThreadBundle thread_bundle_;

DISALLOW_COPY_AND_ASSIGN(StartupBrowserCreatorWinTest);
};

TEST_F(StartupBrowserCreatorWinTest,
GetURLsFromCommandLineWithDesktopSearchURL) {
const char kDesktopSearchURL[] =
"https://www.bing.com/search?q=keyword&form=WNSGPH";

TestingProfile profile;
TemplateURLServiceFactoryTestUtil template_url_service_factory_test_util(
&profile);

base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendArg(kDesktopSearchURL);

// Expected vectors of URLs.
const std::vector<GURL> desktop_search_url_vector({GURL(kDesktopSearchURL)});
const std::vector<GURL> default_search_url_vector(
{GetDefaultSearchURLForSearchTerms(
TemplateURLServiceFactory::GetForProfile(&profile), L"keyword")});

// Preference unset, feature enabled.
SetWindowsDesktopSearchFeatureEnabled(true);
EXPECT_EQ(desktop_search_url_vector,
StartupBrowserCreator::GetURLsFromCommandLine(
command_line, base::FilePath(), &profile));

// Preference set to disabled, feature enabled.
profile.GetPrefs()->SetBoolean(prefs::kWindowsDesktopSearchRedirectionPref,
false);
SetWindowsDesktopSearchFeatureEnabled(true);
EXPECT_EQ(desktop_search_url_vector,
StartupBrowserCreator::GetURLsFromCommandLine(
command_line, base::FilePath(), &profile));

// Preference set to enabled, feature enabled.
profile.GetPrefs()->SetBoolean(prefs::kWindowsDesktopSearchRedirectionPref,
true);
SetWindowsDesktopSearchFeatureEnabled(true);
EXPECT_EQ(default_search_url_vector,
StartupBrowserCreator::GetURLsFromCommandLine(
command_line, base::FilePath(), &profile));

// Preference set to enabled, feature disabled.
profile.GetPrefs()->SetBoolean(prefs::kWindowsDesktopSearchRedirectionPref,
true);
SetWindowsDesktopSearchFeatureEnabled(false);
EXPECT_EQ(desktop_search_url_vector,
StartupBrowserCreator::GetURLsFromCommandLine(
command_line, base::FilePath(), &profile));
}
1 change: 1 addition & 0 deletions chrome/chrome_tests_unit.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@
'browser/ui/webui/browsing_history_handler_unittest.cc',
'browser/ui/webui/fileicon_source_unittest.cc',
'browser/ui/webui/log_web_ui_url_unittest.cc',
'browser/ui/startup/startup_browser_creator_win_unittest.cc',
'browser/update_client/chrome_update_query_params_delegate_unittest.cc',
'common/chrome_content_client_unittest.cc',
'common/chrome_paths_unittest.cc',
Expand Down
2 changes: 1 addition & 1 deletion components/components_tests.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@
'search_engines_unittest_sources': [
'search_engines/default_search_manager_unittest.cc',
'search_engines/default_search_pref_migration_unittest.cc',
'search_engines/detect_desktop_search_win_unittest.cc',
'search_engines/desktop_search_win_unittest.cc',
'search_engines/keyword_table_unittest.cc',
'search_engines/search_host_to_urls_map_unittest.cc',
'search_engines/template_url_prepopulate_data_unittest.cc',
Expand Down
4 changes: 2 additions & 2 deletions components/search_engines.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
'search_engines/default_search_policy_handler.h',
'search_engines/default_search_pref_migration.cc',
'search_engines/default_search_pref_migration.h',
'search_engines/detect_desktop_search_win.cc',
'search_engines/detect_desktop_search_win.h',
'search_engines/desktop_search_win.cc',
'search_engines/desktop_search_win.h',
'search_engines/keyword_table.cc',
'search_engines/keyword_table.h',
'search_engines/keyword_web_data_service.cc',
Expand Down
6 changes: 3 additions & 3 deletions components/search_engines/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ static_library("search_engines") {
"default_search_manager.h",
"default_search_pref_migration.cc",
"default_search_pref_migration.h",
"detect_desktop_search_win.cc",
"detect_desktop_search_win.h",
"desktop_search_win.cc",
"desktop_search_win.h",
"keyword_table.cc",
"keyword_table.h",
"keyword_web_data_service.cc",
Expand Down Expand Up @@ -114,7 +114,7 @@ source_set("unit_tests") {
sources = [
"default_search_manager_unittest.cc",
"default_search_pref_migration_unittest.cc",
"detect_desktop_search_win_unittest.cc",
"desktop_search_win_unittest.cc",
"keyword_table_unittest.cc",
"search_host_to_urls_map_unittest.cc",
"template_url_prepopulate_data_unittest.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,42 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/search_engines/detect_desktop_search_win.h"
#include "components/search_engines/desktop_search_win.h"

#include <string>

#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_util.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/search_engines/prepopulated_engines.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_prepopulate_data.h"
#include "net/base/url_util.h"

namespace prefs {
const char kWindowsDesktopSearchRedirectionPref[] =
"windows_desktop_search_redirection";
} // namespace prefs

const base::Feature kWindowsDesktopSearchRedirectionFeature = {
"WindowsDesktopSearchRedirection", base::FEATURE_DISABLED_BY_DEFAULT
};

void RegisterWindowsDesktopSearchRedirectionPref(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(prefs::kWindowsDesktopSearchRedirectionPref,
false);
}

bool ShouldRedirectWindowsDesktopSearchToDefaultSearchEngine(
PrefService* pref_service) {
DCHECK(pref_service);
return base::FeatureList::IsEnabled(
kWindowsDesktopSearchRedirectionFeature) &&
pref_service->GetBoolean(prefs::kWindowsDesktopSearchRedirectionPref);
}

bool DetectWindowsDesktopSearch(const GURL& url,
const SearchTermsData& search_terms_data,
base::string16* search_terms) {
Expand All @@ -37,7 +62,7 @@ bool DetectWindowsDesktopSearch(const GURL& url,
// Use a case-insensitive comparison because the key is sometimes in capital
// letters.
if (base::EqualsCaseInsensitiveASCII(it.GetKey(), kBingSourceQueryKey)) {
std::string source = it.GetValue();
const std::string source = it.GetValue();
if (source == kBingSourceDesktopText || source == kBingSourceDesktopVoice)
return true;
}
Expand Down
44 changes: 44 additions & 0 deletions components/search_engines/desktop_search_win.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2015 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.

#ifndef COMPONENTS_SEARCH_ENGINES_DESKTOP_SEARCH_WIN_H_
#define COMPONENTS_SEARCH_ENGINES_DESKTOP_SEARCH_WIN_H_

#include "base/feature_list.h"
#include "base/strings/string16.h"

class GURL;
class PrefService;
class SearchTermsData;

namespace user_prefs {
class PrefRegistrySyncable;
}

namespace prefs {
// Name of the Windows desktop search redirection preference.
extern const char kWindowsDesktopSearchRedirectionPref[];
}

// Windows desktop search redirection feature. This is exposed in the header
// file so that it can be referenced from about_flags.cc.
extern const base::Feature kWindowsDesktopSearchRedirectionFeature;

// Registers the Windows desktop search redirection preference into |registry|.
void RegisterWindowsDesktopSearchRedirectionPref(
user_prefs::PrefRegistrySyncable* registry);

// Indicates whether Windows desktop searches should be redirected to the
// default search engine. This is only true when both the preference and the
// feature are enabled. The preference value is read from |pref_service|.
bool ShouldRedirectWindowsDesktopSearchToDefaultSearchEngine(
PrefService* pref_service);

// Detects whether a URL comes from a Windows Desktop search. If so, puts the
// search terms in |search_terms| and returns true.
bool DetectWindowsDesktopSearch(const GURL& url,
const SearchTermsData& search_terms_data,
base::string16* search_terms);

#endif // COMPONENTS_SEARCH_ENGINES_DESKTOP_SEARCH_WIN_H_
Loading

0 comments on commit ee51106

Please sign in to comment.