From deb5af255ebf247ecfaeae7d56318ea9d6fa9d81 Mon Sep 17 00:00:00 2001 From: Simon Hong Date: Mon, 6 Aug 2018 22:33:57 +0900 Subject: [PATCH 1/3] Support alternate private search engine provider Use different TemplateURLService for normal and incognito mode to set default search engine provider independently. Use duckduckgo as a default search engine provider in incognito mode. kUseAlternatePrivateSearchEngine pref is introduced to toggle alternate search engine. If it is set to false, incognito mode uses default search engine of normal mode. --- browser/BUILD.gn | 7 ++ ...rnate_private_search_engine_browsertest.cc | 49 +++++++++ ...ernate_private_search_engine_controller.cc | 102 ++++++++++++++++++ ...ternate_private_search_engine_controller.h | 49 +++++++++ .../alternate_private_search_engine_util.cc | 30 ++++++ .../alternate_private_search_engine_util.h | 25 +++++ browser/brave_browser_process_impl.cc | 6 +- browser/brave_browser_process_impl.h | 4 + browser/brave_profile_prefs.cc | 3 + browser/profile_creation_monitor.cc | 33 ++++++ browser/profile_creation_monitor.h | 27 +++++ common/pref_names.cc | 2 + common/pref_names.h | 1 + ...ines-template_url_service_factory.cc.patch | 17 +++ test/BUILD.gn | 1 + 15 files changed, 354 insertions(+), 2 deletions(-) create mode 100644 browser/alternate_private_search_engine_browsertest.cc create mode 100644 browser/alternate_private_search_engine_controller.cc create mode 100644 browser/alternate_private_search_engine_controller.h create mode 100644 browser/alternate_private_search_engine_util.cc create mode 100644 browser/alternate_private_search_engine_util.h create mode 100644 browser/profile_creation_monitor.cc create mode 100644 browser/profile_creation_monitor.h create mode 100644 patches/chrome-browser-search_engines-template_url_service_factory.cc.patch diff --git a/browser/BUILD.gn b/browser/BUILD.gn index 6f603d262249..6c183a7828c2 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -3,6 +3,10 @@ import("//build/config/features.gni") source_set("browser_process") { sources = [ + "alternate_private_search_engine_controller.cc", + "alternate_private_search_engine_controller.h", + "alternate_private_search_engine_util.cc", + "alternate_private_search_engine_util.h", "autocomplete/brave_autocomplete_scheme_classifier.cc", "autocomplete/brave_autocomplete_scheme_classifier.h", "brave_browser_main_extra_parts.cc", @@ -44,6 +48,8 @@ source_set("browser_process") { "mac/sparkle_glue.mm", "mac/sparkle_glue.h", "mac/su_updater.h", + "profile_creation_monitor.cc", + "profile_creation_monitor.h", "update_util.cc", "update_util.h", @@ -57,6 +63,7 @@ source_set("browser_process") { "//brave/common", "//components/component_updater", "//components/safe_browsing/common:safe_browsing_prefs", + "//components/search_engines", "//components/spellcheck/browser", "//content/public/browser", "//brave/chromium_src:browser", diff --git a/browser/alternate_private_search_engine_browsertest.cc b/browser/alternate_private_search_engine_browsertest.cc new file mode 100644 index 000000000000..b7ea4392d6b0 --- /dev/null +++ b/browser/alternate_private_search_engine_browsertest.cc @@ -0,0 +1,49 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "base/strings/utf_string_conversions.h" +#include "brave/browser/alternate_private_search_engine_util.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/search_engines/template_url_service_factory.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "components/search_engines/template_url_service.h" +#include "components/search_engines/template_url_service_observer.h" + +using AlternatePrivateSearchEngineTest = InProcessBrowserTest; + +IN_PROC_BROWSER_TEST_F(AlternatePrivateSearchEngineTest, PrefTest) { + Profile* profile = browser()->profile(); + Profile* incognito_profile = profile->GetOffTheRecordProfile(); + + auto* service = TemplateURLServiceFactory::GetForProfile(profile); + auto* incognito_service = + TemplateURLServiceFactory::GetForProfile(incognito_profile); + + // Test pref is initially disabled. + EXPECT_FALSE(brave::UseAlternatePrivateSearchEngineEnabled(profile)); + + // Both mode should use same search engine if alternate pref is disabled. + base::string16 normal_search_engine = + service->GetDefaultSearchProvider()->data().short_name(); + EXPECT_EQ(service->GetDefaultSearchProvider()->data().short_name(), + incognito_service->GetDefaultSearchProvider()->data().short_name()); + + // Toggle pref and check incognito_service uses duckduckgo search engine and + // normal mode service uses existing one. + brave::ToggleUseAlternatePrivateSearchEngine(profile); + EXPECT_TRUE(brave::UseAlternatePrivateSearchEngineEnabled(profile)); + EXPECT_EQ(incognito_service->GetDefaultSearchProvider()->data().short_name(), + base::ASCIIToUTF16("DuckDuckGo")); + EXPECT_EQ(service->GetDefaultSearchProvider()->data().short_name(), + normal_search_engine); + + // Toggle pref again and check both mode uses same search engine. + brave::ToggleUseAlternatePrivateSearchEngine(profile); + EXPECT_FALSE(brave::UseAlternatePrivateSearchEngineEnabled(profile)); + EXPECT_EQ(service->GetDefaultSearchProvider()->data().short_name(), + normal_search_engine); + EXPECT_EQ(incognito_service->GetDefaultSearchProvider()->data().short_name(), + normal_search_engine); +} diff --git a/browser/alternate_private_search_engine_controller.cc b/browser/alternate_private_search_engine_controller.cc new file mode 100644 index 000000000000..90582312923a --- /dev/null +++ b/browser/alternate_private_search_engine_controller.cc @@ -0,0 +1,102 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/alternate_private_search_engine_controller.h" + +#include "base/strings/utf_string_conversions.h" +#include "brave/common/pref_names.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/search_engines/template_url_service_factory.h" +#include "components/prefs/pref_service.h" +#include "components/search_engines/template_url_service.h" + +namespace { + +TemplateURLData GetPrivateSearchEngineData() { + TemplateURLData private_search_engine_data; + private_search_engine_data.SetShortName(base::ASCIIToUTF16("DuckDuckGo")); + private_search_engine_data.SetKeyword(base::ASCIIToUTF16("duckduckgo.com")); + private_search_engine_data.SetURL( + "https://duckduckgo.com/?q={searchTerms}&atb=v128-6_e"); + private_search_engine_data.favicon_url = + GURL("https://duckduckgo.com/favicon.ico"); + private_search_engine_data.suggestions_url = + "https://duckduckgo.com/ac/?q={searchTerms}&type=list"; + return private_search_engine_data; +} + +} // namespace + +// static +void AlternatePrivateSearchEngineController::Create(Profile* profile) { + // This controller is deleted by itself when observed TemplateURLSearvice is + // destroyed. + new AlternatePrivateSearchEngineController(profile); +} + +AlternatePrivateSearchEngineController::AlternatePrivateSearchEngineController( + Profile* profile) + : profile_(profile), + template_url_service_( + TemplateURLServiceFactory::GetForProfile(profile_)) { + DCHECK(profile_->GetProfileType() == Profile::INCOGNITO_PROFILE); + + use_alternate_private_search_engine_enabled_.Init( + kUseAlternatePrivateSearchEngine, + profile_->GetOriginalProfile()->GetPrefs(), + base::Bind(&AlternatePrivateSearchEngineController::OnPreferenceChanged, + base::Unretained(this))); + + template_url_service_->AddObserver(this); + private_search_engine_url_.reset( + new TemplateURL(GetPrivateSearchEngineData())); + ConfigureAlternatePrivateSearchEngineProvider(); +} + +AlternatePrivateSearchEngineController:: +~AlternatePrivateSearchEngineController() { +} + +void AlternatePrivateSearchEngineController::OnTemplateURLServiceChanged() { + // When normal profile's default search provider is changed, it changes + // incognito's default search provider. + // Set alternate search engine again if needed. + ConfigureAlternatePrivateSearchEngineProvider(); +} + +void +AlternatePrivateSearchEngineController::OnTemplateURLServiceShuttingDown() { + template_url_service_->RemoveObserver(this); + delete this; +} + +void AlternatePrivateSearchEngineController:: +SetAlternateDefaultPrivateSearchEngine() { + template_url_service_->SetUserSelectedDefaultSearchProvider( + private_search_engine_url_.get()); +} + +void AlternatePrivateSearchEngineController:: +SetNormalModeDefaultSearchEngineAsDefaultPrivateSearchProvider() { + auto* normal_mode_service = + TemplateURLServiceFactory::GetForProfile(profile_->GetOriginalProfile()); + + TemplateURL normal_url(normal_mode_service->GetDefaultSearchProvider()->data()); + template_url_service_->SetUserSelectedDefaultSearchProvider(&normal_url); +} + +void AlternatePrivateSearchEngineController:: +ConfigureAlternatePrivateSearchEngineProvider() { + if (use_alternate_private_search_engine_enabled_.GetValue()) + SetAlternateDefaultPrivateSearchEngine(); + else + SetNormalModeDefaultSearchEngineAsDefaultPrivateSearchProvider(); +} + +void AlternatePrivateSearchEngineController::OnPreferenceChanged( + const std::string& pref_name) { + DCHECK(pref_name == kUseAlternatePrivateSearchEngine); + + ConfigureAlternatePrivateSearchEngineProvider(); +} diff --git a/browser/alternate_private_search_engine_controller.h b/browser/alternate_private_search_engine_controller.h new file mode 100644 index 000000000000..de639589769d --- /dev/null +++ b/browser/alternate_private_search_engine_controller.h @@ -0,0 +1,49 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_BROWSER_ALTERNATE_PRIVATE_SEARCH_ENGINE_CONTROLLER_H_ +#define BRAVE_BROWSER_ALTERNATE_PRIVATE_SEARCH_ENGINE_CONTROLLER_H_ + +#include +#include + +#include "base/macros.h" +#include "components/prefs/pref_member.h" +#include "components/search_engines/template_url_service_observer.h" + +class Profile; +class TemplateURL; +class TemplateURLService; + +class AlternatePrivateSearchEngineController + : public TemplateURLServiceObserver { + public: + static void Create(Profile* profile); + + private: + explicit AlternatePrivateSearchEngineController(Profile* profile); + ~AlternatePrivateSearchEngineController() override; + + void SetAlternateDefaultPrivateSearchEngine(); + void SetNormalModeDefaultSearchEngineAsDefaultPrivateSearchProvider(); + + void ConfigureAlternatePrivateSearchEngineProvider(); + + void OnPreferenceChanged(const std::string& pref_name); + + // TemplateURLServiceObserver overrides: + void OnTemplateURLServiceChanged() override; + void OnTemplateURLServiceShuttingDown() override; + + std::unique_ptr private_search_engine_url_; + BooleanPrefMember use_alternate_private_search_engine_enabled_; + + Profile* profile_; + TemplateURLService* template_url_service_; + + DISALLOW_COPY_AND_ASSIGN(AlternatePrivateSearchEngineController); +}; + + +#endif // BRAVE_BROWSER_ALTERNATE_PRIVATE_SEARCH_ENGINE_CONTROLLER_H_ diff --git a/browser/alternate_private_search_engine_util.cc b/browser/alternate_private_search_engine_util.cc new file mode 100644 index 000000000000..d1c3c9e5e2fb --- /dev/null +++ b/browser/alternate_private_search_engine_util.cc @@ -0,0 +1,30 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/alternate_private_search_engine_util.h" + +#include "brave/common/pref_names.h" +#include "chrome/browser/profiles/profile.h" +#include "components/prefs/pref_service.h" +#include "components/pref_registry/pref_registry_syncable.h" + +namespace brave { + +bool UseAlternatePrivateSearchEngineEnabled(Profile* profile) { + return profile->GetOriginalProfile()->GetPrefs()->GetBoolean( + kUseAlternatePrivateSearchEngine); +} + +void ToggleUseAlternatePrivateSearchEngine(Profile* profile) { + profile->GetOriginalProfile()->GetPrefs()->SetBoolean( + kUseAlternatePrivateSearchEngine, + !UseAlternatePrivateSearchEngineEnabled(profile)); +} + +void RegisterAlternatePrivateSearchEngineProfilePrefs( + user_prefs::PrefRegistrySyncable* registry) { + registry->RegisterBooleanPref(kUseAlternatePrivateSearchEngine, false); +} + +} // namespace brave diff --git a/browser/alternate_private_search_engine_util.h b/browser/alternate_private_search_engine_util.h new file mode 100644 index 000000000000..aaf7c776edf1 --- /dev/null +++ b/browser/alternate_private_search_engine_util.h @@ -0,0 +1,25 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_BROWSER_ALTERNATE_PRIVATE_SEARCH_ENGINE_UTIL_H_ +#define BRAVE_BROWSER_ALTERNATE_PRIVATE_SEARCH_ENGINE_UTIL_H_ + +class Profile; + +namespace user_prefs { +class PrefRegistrySyncable; +} + +namespace brave { + +bool UseAlternatePrivateSearchEngineEnabled(Profile* profile); + +void RegisterAlternatePrivateSearchEngineProfilePrefs( + user_prefs::PrefRegistrySyncable* registry); + +void ToggleUseAlternatePrivateSearchEngine(Profile* profile); + +} // namespace brave + +#endif // BRAVE_BROWSER_ALTERNATE_PRIVATE_SEARCH_ENGINE_UTIL_H_ diff --git a/browser/brave_browser_process_impl.cc b/browser/brave_browser_process_impl.cc index 1781af07ba98..b5e284752793 100644 --- a/browser/brave_browser_process_impl.cc +++ b/browser/brave_browser_process_impl.cc @@ -7,9 +7,10 @@ #include "base/bind.h" #include "base/task_scheduler/post_task.h" #include "base/threading/sequenced_task_runner_handle.h" -#include "brave/browser/component_updater/brave_component_updater_configurator.h" #include "brave/browser/brave_stats_updater.h" +#include "brave/browser/component_updater/brave_component_updater_configurator.h" #include "brave/browser/extensions/brave_tor_client_updater.h" +#include "brave/browser/profile_creation_monitor.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/browser/ad_block_regional_service.h" #include "brave/components/brave_shields/browser/https_everywhere_service.h" @@ -28,7 +29,8 @@ BraveBrowserProcessImpl::~BraveBrowserProcessImpl() { BraveBrowserProcessImpl::BraveBrowserProcessImpl( base::SequencedTaskRunner* local_state_task_runner) - : BrowserProcessImpl(local_state_task_runner) { + : BrowserProcessImpl(local_state_task_runner), + profile_creation_monitor_(new ProfileCreationMonitor) { g_browser_process = this; g_brave_browser_process = this; brave_stats_updater_ = brave::BraveStatsUpdaterFactory(local_state()); diff --git a/browser/brave_browser_process_impl.h b/browser/brave_browser_process_impl.h index e7a6eaef6a9d..aac37832b586 100644 --- a/browser/brave_browser_process_impl.h +++ b/browser/brave_browser_process_impl.h @@ -7,6 +7,8 @@ #include "chrome/browser/browser_process_impl.h" +class ProfileCreationMonitor; + namespace brave { class BraveStatsUpdater; } @@ -56,6 +58,8 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl { std::unique_ptr brave_component_updater_; + std::unique_ptr profile_creation_monitor_; + DISALLOW_COPY_AND_ASSIGN(BraveBrowserProcessImpl); }; diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index e4b0143186b3..1fd57b016ad3 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -4,6 +4,7 @@ #include "brave/browser/brave_profile_prefs.h" +#include "brave/browser/alternate_private_search_engine_util.h" #include "brave/common/pref_names.h" #include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h" #include "chrome/browser/prefs/session_startup_pref.h" @@ -19,6 +20,8 @@ namespace brave { void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { brave_shields::BraveShieldsWebContentsObserver::RegisterProfilePrefs(registry); + RegisterAlternatePrivateSearchEngineProfilePrefs(registry); + registry->RegisterBooleanPref(kWidevineOptedIn, false); // No sign into Brave functionality diff --git a/browser/profile_creation_monitor.cc b/browser/profile_creation_monitor.cc new file mode 100644 index 000000000000..78b53eaac052 --- /dev/null +++ b/browser/profile_creation_monitor.cc @@ -0,0 +1,33 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/profile_creation_monitor.h" + +#include "brave/browser/alternate_private_search_engine_controller.h" +#include "chrome/browser/chrome_notification_types.h" +#include "chrome/browser/profiles/profile.h" +#include "content/public/browser/notification_service.h" + +ProfileCreationMonitor::ProfileCreationMonitor() { + registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, + content::NotificationService::AllSources()); +} + +ProfileCreationMonitor::~ProfileCreationMonitor() {} + +void ProfileCreationMonitor::Observe( + int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) { + switch (type) { + case chrome::NOTIFICATION_PROFILE_CREATED: { + Profile* profile = content::Source(source).ptr(); + if (profile->GetProfileType() == Profile::INCOGNITO_PROFILE) + AlternatePrivateSearchEngineController::Create(profile); + break; + } + default: + NOTREACHED(); + } +} diff --git a/browser/profile_creation_monitor.h b/browser/profile_creation_monitor.h new file mode 100644 index 000000000000..d22c2f71aae9 --- /dev/null +++ b/browser/profile_creation_monitor.h @@ -0,0 +1,27 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_BROWSER_PROFILE_CREATION_MONITOR_H_ +#define BRAVE_BROWSER_PROFILE_CREATION_MONITOR_H_ + +#include "content/public/browser/notification_observer.h" +#include "content/public/browser/notification_registrar.h" + +class ProfileCreationMonitor : public content::NotificationObserver { + public: + ProfileCreationMonitor(); + ~ProfileCreationMonitor() override; + + private: + // content::NotificationObserver overrides: + void Observe(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) override; + + content::NotificationRegistrar registrar_; + + DISALLOW_COPY_AND_ASSIGN(ProfileCreationMonitor); +}; + +#endif // BRAVE_BROWSER_PROFILE_CREATION_MONITOR_H_ diff --git a/common/pref_names.cc b/common/pref_names.cc index a6d5968cddd2..d386f68ffdcc 100644 --- a/common/pref_names.cc +++ b/common/pref_names.cc @@ -16,3 +16,5 @@ const char kFirstCheckMade[] = "brave.stats.first_check_made"; const char kWeekOfInstallation[] = "brave.stats.week_of_installation"; const char kAdBlockCurrentRegion[] = "brave.ad_block.current_region"; const char kWidevineOptedIn[] = "brave.widevine_opted_in"; +const char kUseAlternatePrivateSearchEngine[] = + "brave.use_alternate_private_search_engine"; diff --git a/common/pref_names.h b/common/pref_names.h index e1f35e8ae0e3..1740bb9ec239 100644 --- a/common/pref_names.h +++ b/common/pref_names.h @@ -17,5 +17,6 @@ extern const char kFirstCheckMade[]; extern const char kWeekOfInstallation[]; extern const char kAdBlockCurrentRegion[]; extern const char kWidevineOptedIn[]; +extern const char kUseAlternatePrivateSearchEngine[]; #endif diff --git a/patches/chrome-browser-search_engines-template_url_service_factory.cc.patch b/patches/chrome-browser-search_engines-template_url_service_factory.cc.patch new file mode 100644 index 000000000000..60a554cbd014 --- /dev/null +++ b/patches/chrome-browser-search_engines-template_url_service_factory.cc.patch @@ -0,0 +1,17 @@ +diff --git a/chrome/browser/search_engines/template_url_service_factory.cc b/chrome/browser/search_engines/template_url_service_factory.cc +index c137bd1e4cfdf4a4479201d849240245741af390..36785c9794bead23ec5c3c92b2c6c7e29a2bec6a 100644 +--- a/chrome/browser/search_engines/template_url_service_factory.cc ++++ b/chrome/browser/search_engines/template_url_service_factory.cc +@@ -86,7 +86,12 @@ void TemplateURLServiceFactory::RegisterProfilePrefs( + + content::BrowserContext* TemplateURLServiceFactory::GetBrowserContextToUse( + content::BrowserContext* context) const { ++#if defined(BRAVE_CHROMIUM_BUILD) ++ // To make different service for normal and incognito profile. ++ return chrome::GetBrowserContextOwnInstanceInIncognito(context); ++#else + return chrome::GetBrowserContextRedirectedInIncognito(context); ++#endif + } + + bool TemplateURLServiceFactory::ServiceIsNULLWhileTesting() const { diff --git a/test/BUILD.gn b/test/BUILD.gn index 7648bc408cd8..15cadfe86fd9 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -148,6 +148,7 @@ test("brave_browser_tests") { "//brave/chromium_src/third_party/blink/renderer/modules/battery/navigator_batterytest.cc", "//brave/chromium_src/third_party/blink/renderer/modules/bluetooth/navigator_bluetoothtest.cc", "//brave/chromium_src/third_party/blink/renderer/modules/credentialmanager/credentials_containertest.cc", + "//brave/browser/alternate_private_search_engine_browsertest.cc", "//brave/browser/autoplay/autoplay_permission_context_browsertest.cc", "//brave/browser/brave_content_browser_client_browsertest.cc", "//brave/browser/brave_features_browsertest.cc", From f92c1e99b37776b1f6b34c849eedb41d508be49b Mon Sep 17 00:00:00 2001 From: Simon Hong Date: Wed, 8 Aug 2018 15:57:10 +0900 Subject: [PATCH 2/3] Make private search engine button in private new tab works Connect that button with kUseAlternatePrivateSearchEngine pref. --- browser/ui/webui/brave_new_tab_ui.cc | 53 ++++++++++++++++--- .../reducers/new_tab_reducer.tsx | 1 + components/brave_new_tab_ui/storage.ts | 1 + 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/browser/ui/webui/brave_new_tab_ui.cc b/browser/ui/webui/brave_new_tab_ui.cc index a3e13cc0e6a6..3b37b21fa126 100644 --- a/browser/ui/webui/brave_new_tab_ui.cc +++ b/browser/ui/webui/brave_new_tab_ui.cc @@ -4,6 +4,7 @@ #include "brave/browser/ui/webui/brave_new_tab_ui.h" +#include "brave/browser/alternate_private_search_engine_util.h" #include "brave/common/pref_names.h" #include "brave/common/webui_url_constants.h" #include "chrome/browser/profiles/profile.h" @@ -17,6 +18,31 @@ #include "content/public/browser/web_ui_message_handler.h" #include "content/public/common/bindings_policy.h" +namespace { +class NewTabDOMHandler : public content::WebUIMessageHandler { + public: + NewTabDOMHandler() = default; + ~NewTabDOMHandler() override = default; + + private: + // WebUIMessageHandler implementation. + void RegisterMessages() override { + web_ui()->RegisterMessageCallback( + "toggleAlternativePrivateSearchEngine", + base::BindRepeating( + &NewTabDOMHandler::HandleToggleAlternativePrivateSearchEngine, + base::Unretained(this))); + } + + void HandleToggleAlternativePrivateSearchEngine(const base::ListValue* args) { + brave::ToggleUseAlternatePrivateSearchEngine(Profile::FromWebUI(web_ui())); + } + + DISALLOW_COPY_AND_ASSIGN(NewTabDOMHandler); +}; + +} // namespace + BraveNewTabUI::BraveNewTabUI(content::WebUI* web_ui, const std::string& name) : BasicUI(web_ui, name, kBraveNewTabJS, IDR_BRAVE_NEW_TAB_JS, IDR_BRAVE_NEW_TAB_HTML) { @@ -30,6 +56,8 @@ BraveNewTabUI::BraveNewTabUI(content::WebUI* web_ui, const std::string& name) base::Bind(&BraveNewTabUI::OnPreferenceChanged, base::Unretained(this))); pref_change_registrar_->Add(kHttpsUpgrades, base::Bind(&BraveNewTabUI::OnPreferenceChanged, base::Unretained(this))); + + web_ui->AddMessageHandler(std::make_unique()); } BraveNewTabUI::~BraveNewTabUI() { @@ -42,12 +70,25 @@ void BraveNewTabUI::CustomizeNewTabWebUIProperties() { if (web_contents) { auto* render_view_host = web_contents->GetRenderViewHost(); if (render_view_host) { - render_view_host->SetWebUIProperty("adsBlockedStat", std::to_string(prefs->GetUint64(kAdsBlocked))); - render_view_host->SetWebUIProperty("trackersBlockedStat", std::to_string(prefs->GetUint64(kTrackersBlocked))); - render_view_host->SetWebUIProperty("javascriptBlockedStat", std::to_string(prefs->GetUint64(kJavascriptBlocked))); - render_view_host->SetWebUIProperty("javascriptBlockedStat", std::to_string(prefs->GetUint64(kJavascriptBlocked))); - render_view_host->SetWebUIProperty("httpsUpgradesStat", std::to_string(prefs->GetUint64(kHttpsUpgrades))); - render_view_host->SetWebUIProperty("fingerprintingBlockedStat", std::to_string(prefs->GetUint64(kFingerprintingBlocked))); + render_view_host->SetWebUIProperty( + "adsBlockedStat", + std::to_string(prefs->GetUint64(kAdsBlocked))); + render_view_host->SetWebUIProperty( + "trackersBlockedStat", + std::to_string(prefs->GetUint64(kTrackersBlocked))); + render_view_host->SetWebUIProperty( + "javascriptBlockedStat", + std::to_string(prefs->GetUint64(kJavascriptBlocked))); + render_view_host->SetWebUIProperty( + "httpsUpgradesStat", + std::to_string(prefs->GetUint64(kHttpsUpgrades))); + render_view_host->SetWebUIProperty( + "fingerprintingBlockedStat", + std::to_string(prefs->GetUint64(kFingerprintingBlocked))); + render_view_host->SetWebUIProperty( + "useAlternativePrivateSearchEngine", + prefs->GetBoolean(kUseAlternatePrivateSearchEngine) ? "true" + : "false"); } } } diff --git a/components/brave_new_tab_ui/reducers/new_tab_reducer.tsx b/components/brave_new_tab_ui/reducers/new_tab_reducer.tsx index 8790298c2907..b8d08bb458fb 100644 --- a/components/brave_new_tab_ui/reducers/new_tab_reducer.tsx +++ b/components/brave_new_tab_ui/reducers/new_tab_reducer.tsx @@ -220,6 +220,7 @@ export const newTabReducer: Reducer = (state: NewTab.S break case types.NEW_TAB_USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE: + chrome.send('toggleAlternativePrivateSearchEngine', []) state = { ...state, useAlternativePrivateSearchEngine: payload.shouldUse } break diff --git a/components/brave_new_tab_ui/storage.ts b/components/brave_new_tab_ui/storage.ts index c1f3e8b6addd..24e6a1c738e5 100644 --- a/components/brave_new_tab_ui/storage.ts +++ b/components/brave_new_tab_ui/storage.ts @@ -46,6 +46,7 @@ export const getLoadTimeData = (state: NewTab.State) => { 'httpsUpgradesStat', 'fingerprintingBlockedStat'].forEach((stat) => { state.stats[stat] = parseInt(chrome.getVariableValue(stat), 10) || 0 }) + state.useAlternativePrivateSearchEngine = chrome.getVariableValue('useAlternativePrivateSearchEngine') === 'true' return state } From 0bb066082361a98249b7824df48924dc423c73d7 Mon Sep 17 00:00:00 2001 From: Simon Hong Date: Fri, 10 Aug 2018 13:49:11 +0900 Subject: [PATCH 3/3] Change duckduckgo search query string --- browser/alternate_private_search_engine_controller.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/alternate_private_search_engine_controller.cc b/browser/alternate_private_search_engine_controller.cc index 90582312923a..ce46447d58a8 100644 --- a/browser/alternate_private_search_engine_controller.cc +++ b/browser/alternate_private_search_engine_controller.cc @@ -18,7 +18,7 @@ TemplateURLData GetPrivateSearchEngineData() { private_search_engine_data.SetShortName(base::ASCIIToUTF16("DuckDuckGo")); private_search_engine_data.SetKeyword(base::ASCIIToUTF16("duckduckgo.com")); private_search_engine_data.SetURL( - "https://duckduckgo.com/?q={searchTerms}&atb=v128-6_e"); + "https://duckduckgo.com/?q={searchTerms}&t=brave"); private_search_engine_data.favicon_url = GURL("https://duckduckgo.com/favicon.ico"); private_search_engine_data.suggestions_url =