diff --git a/browser/BUILD.gn b/browser/BUILD.gn index de892705f51e..ba0af563df04 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -3,10 +3,6 @@ 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", @@ -31,6 +27,8 @@ source_set("browser_process") { "component_updater/brave_component_installer.h", "component_updater/brave_component_updater_configurator.cc", "component_updater/brave_component_updater_configurator.h", + "guest_window_search_engine_provider_controller.cc", + "guest_window_search_engine_provider_controller.h", "importer/brave_external_process_importer_client.cc", "importer/brave_external_process_importer_client.h", "importer/brave_external_process_importer_host.cc", @@ -48,8 +46,14 @@ source_set("browser_process") { "mac/sparkle_glue.mm", "mac/sparkle_glue.h", "mac/su_updater.h", + "private_window_search_engine_provider_controller.cc", + "private_window_search_engine_provider_controller.h", "profile_creation_monitor.cc", "profile_creation_monitor.h", + "search_engine_provider_controller_base.cc", + "search_engine_provider_controller_base.h", + "search_engine_provider_util.cc", + "search_engine_provider_util.h", "update_util.cc", "update_util.h", diff --git a/browser/alternate_private_search_engine_browsertest.cc b/browser/alternate_private_search_engine_browsertest.cc deleted file mode 100644 index b7ea4392d6b0..000000000000 --- a/browser/alternate_private_search_engine_browsertest.cc +++ /dev/null @@ -1,49 +0,0 @@ -/* 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 deleted file mode 100644 index ce46447d58a8..000000000000 --- a/browser/alternate_private_search_engine_controller.cc +++ /dev/null @@ -1,102 +0,0 @@ -/* 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}&t=brave"); - 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 deleted file mode 100644 index de639589769d..000000000000 --- a/browser/alternate_private_search_engine_controller.h +++ /dev/null @@ -1,49 +0,0 @@ -/* 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 deleted file mode 100644 index d1c3c9e5e2fb..000000000000 --- a/browser/alternate_private_search_engine_util.cc +++ /dev/null @@ -1,30 +0,0 @@ -/* 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 deleted file mode 100644 index aaf7c776edf1..000000000000 --- a/browser/alternate_private_search_engine_util.h +++ /dev/null @@ -1,25 +0,0 @@ -/* 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_profile_prefs.cc b/browser/brave_profile_prefs.cc index 80df07aade37..f9851505aa64 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -4,7 +4,7 @@ #include "brave/browser/brave_profile_prefs.h" -#include "brave/browser/alternate_private_search_engine_util.h" +#include "brave/browser/search_engine_provider_util.h" #include "brave/browser/themes/brave_theme_service.h" #include "brave/common/pref_names.h" #include "brave/browser/tor/tor_profile_service.h" @@ -24,7 +24,7 @@ namespace brave { void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { brave_shields::BraveShieldsWebContentsObserver::RegisterProfilePrefs(registry); - RegisterAlternatePrivateSearchEngineProfilePrefs(registry); + RegisterAlternativeSearchEngineProviderProfilePrefs(registry); // appearance BraveThemeService::RegisterProfilePrefs(registry); diff --git a/browser/guest_window_search_engine_provider_controller.cc b/browser/guest_window_search_engine_provider_controller.cc new file mode 100644 index 000000000000..e45e3b499669 --- /dev/null +++ b/browser/guest_window_search_engine_provider_controller.cc @@ -0,0 +1,59 @@ +/* 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/guest_window_search_engine_provider_controller.h" + +#include "base/auto_reset.h" +#include "brave/browser/search_engine_provider_util.h" +#include "chrome/browser/profiles/profile.h" +#include "components/search_engines/template_url_service.h" + +GuestWindowSearchEngineProviderController:: +GuestWindowSearchEngineProviderController(Profile* profile) + : SearchEngineProviderControllerBase(profile) { + DCHECK_EQ(profile->GetProfileType(), Profile::GUEST_PROFILE); + + // Monitor otr(off the record) profile's search engine changing to tracking + // user's default search engine provider. + // OTR profile's service is used for that. + otr_template_url_service_->AddObserver(this); + ConfigureSearchEngineProvider(); +} + +GuestWindowSearchEngineProviderController:: +~GuestWindowSearchEngineProviderController() { + otr_template_url_service_->RemoveObserver(this); +} + +void GuestWindowSearchEngineProviderController::OnTemplateURLServiceChanged() { + if (ignore_template_url_service_changing_) + return; + + // Prevent search engine changing from settings page for tor profile. + // TODO(simonhong): Revisit when related ux is determined. + if (otr_profile_->IsTorProfile()) { + base::AutoReset reset(&ignore_template_url_service_changing_, true); + ChangeToAlternativeSearchEngineProvider(); + return; + } + + + // The purpose of below code is turn off alternative prefs + // when user changes to different search engine provider from settings. + // However, this callback is also called during the TemplateURLService + // initialization phase. Because of this, guest view always starts with + // this prefs off state when browser restarted(persisted during the runtime). + // Currently I don't know how to determine who is caller of this callback. + // TODO(simonhong): Revisit here when brave's related ux is determined. + if (UseAlternativeSearchEngineProvider()) + brave::ToggleUseAlternativeSearchEngineProvider(otr_profile_); +} + +void +GuestWindowSearchEngineProviderController::ConfigureSearchEngineProvider() { + base::AutoReset reset(&ignore_template_url_service_changing_, true); + UseAlternativeSearchEngineProvider() + ? ChangeToAlternativeSearchEngineProvider() + : ChangeToNormalWindowSearchEngineProvider(); +} diff --git a/browser/guest_window_search_engine_provider_controller.h b/browser/guest_window_search_engine_provider_controller.h new file mode 100644 index 000000000000..8a03acbded61 --- /dev/null +++ b/browser/guest_window_search_engine_provider_controller.h @@ -0,0 +1,28 @@ +/* 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_GUEST_WINDOW_SEARCH_ENGINE_PROVIDER_CONTROLLER_H_ +#define BRAVE_BROWSER_GUEST_WINDOW_SEARCH_ENGINE_PROVIDER_CONTROLLER_H_ + +#include "brave/browser/search_engine_provider_controller_base.h" + +class GuestWindowSearchEngineProviderController + : public SearchEngineProviderControllerBase { + public: + explicit GuestWindowSearchEngineProviderController(Profile* profile); + ~GuestWindowSearchEngineProviderController() override; + + private: + // TemplateURLServiceObserver overrides: + void OnTemplateURLServiceChanged() override; + + void ConfigureSearchEngineProvider() override; + + bool ignore_template_url_service_changing_ = false; + + DISALLOW_COPY_AND_ASSIGN(GuestWindowSearchEngineProviderController); +}; + + +#endif // BRAVE_BROWSER_GUEST_WINDOW_SEARCH_ENGINE_PROVIDER_CONTROLLER_H_ diff --git a/browser/private_window_search_engine_provider_controller.cc b/browser/private_window_search_engine_provider_controller.cc new file mode 100644 index 000000000000..d931940915eb --- /dev/null +++ b/browser/private_window_search_engine_provider_controller.cc @@ -0,0 +1,45 @@ +/* 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/private_window_search_engine_provider_controller.h" + +#include "chrome/browser/profiles/profile.h" +#include "components/search_engines/template_url_service.h" + +PrivateWindowSearchEngineProviderController:: +PrivateWindowSearchEngineProviderController(Profile* profile) + : SearchEngineProviderControllerBase(profile) { + DCHECK_EQ(profile->GetProfileType(), Profile::INCOGNITO_PROFILE); + + // Monitor normal profile's search engine changing because private window + // should that search engine provider when alternative search engine isn't + // used. + original_template_url_service_->AddObserver(this); + ConfigureSearchEngineProvider(); +} + +PrivateWindowSearchEngineProviderController:: +~PrivateWindowSearchEngineProviderController() { + original_template_url_service_->RemoveObserver(this); +} + +void PrivateWindowSearchEngineProviderController:: +ConfigureSearchEngineProvider() { + UseAlternativeSearchEngineProvider() + ? ChangeToAlternativeSearchEngineProvider() + : ChangeToNormalWindowSearchEngineProvider(); +} + +void +PrivateWindowSearchEngineProviderController::OnTemplateURLServiceChanged() { + // If private window uses alternative, search provider changing of normal + // profile should not affect private window's provider. + if (UseAlternativeSearchEngineProvider()) + return; + + // When normal profile's default search provider is changed, apply it to + // private window's provider. + ChangeToNormalWindowSearchEngineProvider(); +} + diff --git a/browser/private_window_search_engine_provider_controller.h b/browser/private_window_search_engine_provider_controller.h new file mode 100644 index 000000000000..4a4db9b50244 --- /dev/null +++ b/browser/private_window_search_engine_provider_controller.h @@ -0,0 +1,26 @@ +/* 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_PRIVATE_WINDOW_SEARCH_ENGINE_PRIVATE_CONTROLLER_H_ +#define BRAVE_BROWSER_PRIVATE_WINDOW_SEARCH_ENGINE_PRIVATE_CONTROLLER_H_ + +#include "brave/browser/search_engine_provider_controller_base.h" + +class PrivateWindowSearchEngineProviderController + : public SearchEngineProviderControllerBase { + public: + explicit PrivateWindowSearchEngineProviderController(Profile* profile); + ~PrivateWindowSearchEngineProviderController() override; + + private: + // TemplateURLServiceObserver overrides: + void OnTemplateURLServiceChanged() override; + + // SearchEngineProviderControllerBase overrides: + void ConfigureSearchEngineProvider() override; + + DISALLOW_COPY_AND_ASSIGN(PrivateWindowSearchEngineProviderController); +}; + +#endif // BRAVE_BROWSER_PRIVATE_WINDOW_SEARCH_ENGINE_PROVIDER_CONTROLLER_H_ diff --git a/browser/profile_creation_monitor.cc b/browser/profile_creation_monitor.cc index 78b53eaac052..1ee7f05ee374 100644 --- a/browser/profile_creation_monitor.cc +++ b/browser/profile_creation_monitor.cc @@ -4,7 +4,7 @@ #include "brave/browser/profile_creation_monitor.h" -#include "brave/browser/alternate_private_search_engine_controller.h" +#include "brave/browser/search_engine_provider_util.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/profiles/profile.h" #include "content/public/browser/notification_service.h" @@ -23,8 +23,7 @@ void ProfileCreationMonitor::Observe( switch (type) { case chrome::NOTIFICATION_PROFILE_CREATED: { Profile* profile = content::Source(source).ptr(); - if (profile->GetProfileType() == Profile::INCOGNITO_PROFILE) - AlternatePrivateSearchEngineController::Create(profile); + brave::InitializeSearchEngineProviderIfNeeded(profile); break; } default: diff --git a/browser/profiles/brave_profile_manager.cc b/browser/profiles/brave_profile_manager.cc index 70412ed926d6..3c14f7cf8479 100644 --- a/browser/profiles/brave_profile_manager.cc +++ b/browser/profiles/brave_profile_manager.cc @@ -15,7 +15,9 @@ #include "chrome/common/chrome_constants.h" #include "chrome/common/pref_names.h" #include "chrome/grit/generated_resources.h" +#include "components/bookmarks/common/bookmark_pref_names.h" #include "components/prefs/pref_service.h" +#include "components/signin/core/browser/signin_pref_names.h" #include "content/public/browser/browser_thread.h" #include "content/public/common/webrtc_ip_handling_policy.h" #include "ui/base/l10n/l10n_util.h" @@ -99,3 +101,13 @@ void BraveProfileManager::LaunchTorProcess(Profile* profile) { tor_profile_service->LaunchTor(config); } } + +// This overridden method doesn't clear |kDefaultSearchProviderDataPrefName|. +// W/o this, prefs set by GuestWindowSearchEngineProviderController is cleared +// during the initialization. +void BraveProfileManager::SetNonPersonalProfilePrefs(Profile* profile) { + PrefService* prefs = profile->GetPrefs(); + prefs->SetBoolean(prefs::kSigninAllowed, false); + prefs->SetBoolean(bookmarks::prefs::kEditBookmarksEnabled, false); + prefs->SetBoolean(bookmarks::prefs::kShowBookmarkBar, false); +} diff --git a/browser/profiles/brave_profile_manager.h b/browser/profiles/brave_profile_manager.h index c6ea864ce9e1..02690bdde3aa 100644 --- a/browser/profiles/brave_profile_manager.h +++ b/browser/profiles/brave_profile_manager.h @@ -18,6 +18,8 @@ class BraveProfileManager : public ProfileManager { void InitProfileUserPrefs(Profile* profile) override; std::string GetLastUsedProfileName() override; + void SetNonPersonalProfilePrefs(Profile* profile) override; + protected: // ProfileManager implementation. Profile* CreateProfileHelper(const base::FilePath& path) override; diff --git a/browser/search_engine_provider_controller_base.cc b/browser/search_engine_provider_controller_base.cc new file mode 100644 index 000000000000..525b123abceb --- /dev/null +++ b/browser/search_engine_provider_controller_base.cc @@ -0,0 +1,73 @@ +/* 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/search_engine_provider_controller_base.h" + +#include "brave/common/pref_names.h" +#include "brave/components/search_engines/brave_prepopulated_engines.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/prepopulated_engines.h" +#include "components/search_engines/template_url_prepopulate_data.h" +#include "components/search_engines/template_url_service.h" + +SearchEngineProviderControllerBase::SearchEngineProviderControllerBase( + Profile* profile) + : otr_profile_(profile), + original_template_url_service_( + TemplateURLServiceFactory::GetForProfile( + otr_profile_->GetOriginalProfile())), + otr_template_url_service_( + TemplateURLServiceFactory::GetForProfile(otr_profile_)) { + use_alternative_search_engine_provider_.Init( + kUseAlternativeSearchEngineProvider, + otr_profile_->GetOriginalProfile()->GetPrefs(), + base::Bind(&SearchEngineProviderControllerBase::OnPreferenceChanged, + base::Unretained(this))); + + auto data = TemplateURLPrepopulateData::GetPrepopulatedEngine( + profile->GetPrefs(), + TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO); + alternative_search_engine_url_.reset(new TemplateURL(*data)); +} + +SearchEngineProviderControllerBase::~SearchEngineProviderControllerBase() { +} + +void SearchEngineProviderControllerBase::OnTemplateURLServiceShuttingDown() { + delete this; +} + +void SearchEngineProviderControllerBase::OnPreferenceChanged( + const std::string& pref_name) { + DCHECK(pref_name == kUseAlternativeSearchEngineProvider); + + ConfigureSearchEngineProvider(); +} + +bool +SearchEngineProviderControllerBase::UseAlternativeSearchEngineProvider() const { + // Currently, use alternative search engine provider for tor profile. + // TODO(simonhong): Revisit when related setting ux is determined. + if (otr_profile_->IsTorProfile()) + return true; + + return use_alternative_search_engine_provider_.GetValue(); +} + +void SearchEngineProviderControllerBase:: +ChangeToAlternativeSearchEngineProvider() { + otr_template_url_service_->SetUserSelectedDefaultSearchProvider( + alternative_search_engine_url_.get()); +} + +void SearchEngineProviderControllerBase:: +ChangeToNormalWindowSearchEngineProvider() { + TemplateURL normal_url( + original_template_url_service_->GetDefaultSearchProvider()->data()); + otr_template_url_service_->SetUserSelectedDefaultSearchProvider( + &normal_url); +} + diff --git a/browser/search_engine_provider_controller_base.h b/browser/search_engine_provider_controller_base.h new file mode 100644 index 000000000000..430fcdbc9a68 --- /dev/null +++ b/browser/search_engine_provider_controller_base.h @@ -0,0 +1,51 @@ +/* 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_SEARCH_ENGINE_PROVIDER_CONTROLLER_BASE_H_ +#define BRAVE_BROWSER_SEARCH_ENGINE_PROVIDER_CONTROLLER_BASE_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 SearchEngineProviderControllerBase : public TemplateURLServiceObserver { + public: + explicit SearchEngineProviderControllerBase(Profile* profile); + ~SearchEngineProviderControllerBase() override; + + protected: + virtual void ConfigureSearchEngineProvider() = 0; + + // TemplateURLServiceObserver overrides: + void OnTemplateURLServiceShuttingDown() override; + + bool UseAlternativeSearchEngineProvider() const; + void ChangeToAlternativeSearchEngineProvider(); + void ChangeToNormalWindowSearchEngineProvider(); + + // Points off the record profile. + Profile* otr_profile_; + // Service for original profile of |otr_profile_|. + TemplateURLService* original_template_url_service_; + // Service for off the record profile. + TemplateURLService* otr_template_url_service_; + + std::unique_ptr alternative_search_engine_url_; + + private: + void OnPreferenceChanged(const std::string& pref_name); + + BooleanPrefMember use_alternative_search_engine_provider_; + + DISALLOW_COPY_AND_ASSIGN(SearchEngineProviderControllerBase); +}; + +#endif // BRAVE_BROWSER_SEARCH_ENGINE_PROVIDER_CONTROLLER_BASE_H_ diff --git a/browser/search_engine_provider_controller_browsertest.cc b/browser/search_engine_provider_controller_browsertest.cc new file mode 100644 index 000000000000..2735146d16cd --- /dev/null +++ b/browser/search_engine_provider_controller_browsertest.cc @@ -0,0 +1,94 @@ +/* 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/profiles/brave_profile_manager.h" +#include "brave/browser/search_engine_provider_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 "chrome/test/base/testing_browser_process.h" +#include "components/search_engines/template_url_service.h" +#include "components/search_engines/template_url_service_observer.h" +#include "content/public/test/test_utils.h" + +using SearchEngineProviderControllerTest = InProcessBrowserTest; + +class TorSearchEngineProviderControllerTest : public InProcessBrowserTest { + public: + void SetUp() override { + disable_io_checks(); + InProcessBrowserTest::SetUp(); + } +}; + +TemplateURLData CreateTestSearchEngine() { + TemplateURLData result; + result.SetShortName(base::ASCIIToUTF16("test1")); + result.SetKeyword(base::ASCIIToUTF16("test.com")); + result.SetURL("http://test.com/search?t={searchTerms}"); + return result; +} + +IN_PROC_BROWSER_TEST_F(SearchEngineProviderControllerTest, 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::UseAlternativeSearchEngineProviderEnabled(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::ToggleUseAlternativeSearchEngineProvider(profile); + EXPECT_TRUE(brave::UseAlternativeSearchEngineProviderEnabled(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::ToggleUseAlternativeSearchEngineProvider(profile); + EXPECT_FALSE(brave::UseAlternativeSearchEngineProviderEnabled(profile)); + EXPECT_EQ(service->GetDefaultSearchProvider()->data().short_name(), + normal_search_engine); + EXPECT_EQ(incognito_service->GetDefaultSearchProvider()->data().short_name(), + normal_search_engine); + + // Check private search engine uses normal mode search engine when alternative + // search engine pref is false. + TemplateURLData test_data = CreateTestSearchEngine(); + std::unique_ptr test_url(new TemplateURL(test_data)); + service->SetUserSelectedDefaultSearchProvider(test_url.get()); + EXPECT_EQ(incognito_service->GetDefaultSearchProvider()->data().short_name(), + base::ASCIIToUTF16("test1")); +} + +// This test crashes with below. I don't know how to deal with now. +// [FATAL:brave_content_browser_client.cc(217)] Check failed: !path.empty(). +// TODO(simonhong): Enable this later. +IN_PROC_BROWSER_TEST_F(TorSearchEngineProviderControllerTest, + DISABLED_CheckTorProfileSearchProviderTest) { + base::FilePath tor_path = BraveProfileManager::GetTorProfilePath(); + ProfileManager* profile_manager = g_browser_process->profile_manager(); + Profile* tor_profile = profile_manager->GetProfile(tor_path); + EXPECT_TRUE(tor_profile->IsTorProfile()); + + auto* service = TemplateURLServiceFactory::GetForProfile(tor_profile); + //Check tor profile's search provider is set to ddg. + EXPECT_EQ(service->GetDefaultSearchProvider()->data().short_name(), + base::ASCIIToUTF16("DuckDuckGo")); + + content::RunAllTasksUntilIdle(); +} diff --git a/browser/search_engine_provider_util.cc b/browser/search_engine_provider_util.cc new file mode 100644 index 000000000000..9c2ecd6641b2 --- /dev/null +++ b/browser/search_engine_provider_util.cc @@ -0,0 +1,45 @@ +/* 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/search_engine_provider_util.h" + +#include "brave/browser/guest_window_search_engine_provider_controller.h" +#include "brave/browser/private_window_search_engine_provider_controller.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 UseAlternativeSearchEngineProviderEnabled(Profile* profile) { + return profile->GetOriginalProfile()->GetPrefs()->GetBoolean( + kUseAlternativeSearchEngineProvider); +} + +void ToggleUseAlternativeSearchEngineProvider(Profile* profile) { + profile->GetOriginalProfile()->GetPrefs()->SetBoolean( + kUseAlternativeSearchEngineProvider, + !UseAlternativeSearchEngineProviderEnabled(profile)); +} + +void RegisterAlternativeSearchEngineProviderProfilePrefs( + user_prefs::PrefRegistrySyncable* registry) { + registry->RegisterBooleanPref(kUseAlternativeSearchEngineProvider, false); +} + +void InitializeSearchEngineProviderIfNeeded(Profile* profile) { + // These search engine provider will be destroyed when observing template url + // service is terminated. + if (profile->GetProfileType() == Profile::INCOGNITO_PROFILE) { + new PrivateWindowSearchEngineProviderController(profile); + return; + } + + if (profile->GetProfileType() == Profile::GUEST_PROFILE) { + new GuestWindowSearchEngineProviderController(profile); + } +} + +} // namespace brave diff --git a/browser/search_engine_provider_util.h b/browser/search_engine_provider_util.h new file mode 100644 index 000000000000..7a990ff5dced --- /dev/null +++ b/browser/search_engine_provider_util.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_SEARCH_ENGINE_PROVIDER_UTIL_H_ +#define BRAVE_BROWSER_SEARCH_ENGINE_PROVIDER_UTIL_H_ + +class Profile; + +namespace user_prefs { +class PrefRegistrySyncable; +} + +namespace brave { + +bool UseAlternativeSearchEngineProviderEnabled(Profile* profile); + +void RegisterAlternativeSearchEngineProviderProfilePrefs( + user_prefs::PrefRegistrySyncable* registry); + +void ToggleUseAlternativeSearchEngineProvider(Profile* profile); + +void InitializeSearchEngineProviderIfNeeded(Profile* profile); + +} // namespace brave + +#endif // BRAVE_BROWSER_SEARCH_ENGINE_PROVIDER_UTIL_H_ diff --git a/browser/ui/webui/brave_new_tab_ui.cc b/browser/ui/webui/brave_new_tab_ui.cc index b0577775e8bb..f384fae7cdfc 100644 --- a/browser/ui/webui/brave_new_tab_ui.cc +++ b/browser/ui/webui/brave_new_tab_ui.cc @@ -4,7 +4,7 @@ #include "brave/browser/ui/webui/brave_new_tab_ui.h" -#include "brave/browser/alternate_private_search_engine_util.h" +#include "brave/browser/search_engine_provider_util.h" #include "brave/common/pref_names.h" #include "brave/common/webui_url_constants.h" #include "chrome/browser/profiles/profile.h" @@ -28,12 +28,14 @@ class NewTabDOMHandler : public content::WebUIMessageHandler { web_ui()->RegisterMessageCallback( "toggleAlternativePrivateSearchEngine", base::BindRepeating( - &NewTabDOMHandler::HandleToggleAlternativePrivateSearchEngine, + &NewTabDOMHandler::HandleToggleAlternativeSearchEngineProvider, base::Unretained(this))); } - void HandleToggleAlternativePrivateSearchEngine(const base::ListValue* args) { - brave::ToggleUseAlternatePrivateSearchEngine(Profile::FromWebUI(web_ui())); + void HandleToggleAlternativeSearchEngineProvider( + const base::ListValue* args) { + brave::ToggleUseAlternativeSearchEngineProvider( + Profile::FromWebUI(web_ui())); } DISALLOW_COPY_AND_ASSIGN(NewTabDOMHandler); @@ -54,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))); + pref_change_registrar_->Add(kUseAlternativeSearchEngineProvider, + base::Bind(&BraveNewTabUI::OnPreferenceChanged, base::Unretained(this))); web_ui->AddMessageHandler(std::make_unique()); } @@ -82,8 +86,8 @@ void BraveNewTabUI::CustomizeNewTabWebUIProperties(content::RenderViewHost* rend std::to_string(prefs->GetUint64(kFingerprintingBlocked))); render_view_host->SetWebUIProperty( "useAlternativePrivateSearchEngine", - prefs->GetBoolean(kUseAlternatePrivateSearchEngine) ? "true" - : "false"); + prefs->GetBoolean(kUseAlternativeSearchEngineProvider) ? "true" + : "false"); render_view_host->SetWebUIProperty( "isTor", profile->IsTorProfile() ? "true" : "false"); } diff --git a/common/pref_names.cc b/common/pref_names.cc index 043a0b12d2f9..8431b6b5876e 100644 --- a/common/pref_names.cc +++ b/common/pref_names.cc @@ -16,7 +16,7 @@ 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[] = +const char kUseAlternativeSearchEngineProvider[] = "brave.use_alternate_private_search_engine"; const char kBraveThemeType[] = "brave.theme.type"; const char kLocationBarIsWide[] = "brave.location_bar_is_wide"; diff --git a/common/pref_names.h b/common/pref_names.h index d6dc18096280..8939ee99be49 100644 --- a/common/pref_names.h +++ b/common/pref_names.h @@ -17,7 +17,7 @@ extern const char kFirstCheckMade[]; extern const char kWeekOfInstallation[]; extern const char kAdBlockCurrentRegion[]; extern const char kWidevineOptedIn[]; -extern const char kUseAlternatePrivateSearchEngine[]; +extern const char kUseAlternativeSearchEngineProvider[]; extern const char kBraveThemeType[]; extern const char kLocationBarIsWide[]; extern const char kReferralPromoCode[]; diff --git a/components/search_engines/brave_prepopulated_engines.h b/components/search_engines/brave_prepopulated_engines.h index 02eca979038e..7119cc37641e 100644 --- a/components/search_engines/brave_prepopulated_engines.h +++ b/components/search_engines/brave_prepopulated_engines.h @@ -9,11 +9,13 @@ #include "components/search_engines/search_engine_type.h" +struct PrepopulatedEngine; + namespace TemplateURLPrepopulateData { extern const int kBraveCurrentDataVersion; -// See comments on prepopulated engines ids in +// See comments on prepopulated engines ids in // components/search_engines/prepopulated_engines_schema.json above the // definition of the id field and in // components/search_engines/prepopulated_engines.json at the top of the file. diff --git a/patches/chrome-browser-profiles-profile_manager.h.patch b/patches/chrome-browser-profiles-profile_manager.h.patch index b1321acb982c..730e76948e5c 100644 --- a/patches/chrome-browser-profiles-profile_manager.h.patch +++ b/patches/chrome-browser-profiles-profile_manager.h.patch @@ -1,5 +1,5 @@ diff --git a/chrome/browser/profiles/profile_manager.h b/chrome/browser/profiles/profile_manager.h -index 8a62433e9107a48d4e7ae919af5e82c3639d612a..113f24bab445cb6e31b55c5d2064002e909204ff 100644 +index 8a62433e9107a48d4e7ae919af5e82c3639d612a..3e4b3c4f303bbebeeba8cc44447f14ff57a77a52 100644 --- a/chrome/browser/profiles/profile_manager.h +++ b/chrome/browser/profiles/profile_manager.h @@ -140,7 +140,7 @@ class ProfileManager : public content::NotificationObserver, @@ -20,3 +20,12 @@ index 8a62433e9107a48d4e7ae919af5e82c3639d612a..113f24bab445cb6e31b55c5d2064002e // Register and add testing profile to the ProfileManager. Use ONLY in tests. // This allows the creation of Profiles outside of the standard creation path +@@ -347,7 +347,7 @@ class ProfileManager : public content::NotificationObserver, + + // Apply settings for profiles created by the system rather than users: The + // (desktop) Guest User profile and (desktop) System Profile. +- void SetNonPersonalProfilePrefs(Profile* profile); ++ virtual void SetNonPersonalProfilePrefs(Profile* profile); + + // For ChromeOS, determines if profile should be otr. + bool ShouldGoOffTheRecord(Profile* profile); diff --git a/test/BUILD.gn b/test/BUILD.gn index 848979d9df70..a138e03bb103 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -166,7 +166,6 @@ 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", @@ -175,6 +174,7 @@ test("brave_browser_tests") { "//brave/browser/extensions/brave_tor_client_updater_browsertest.cc", "//brave/browser/extensions/api/brave_shields_api_browsertest.cc", "//brave/browser/extensions/brave_webstore_inline_installer_browsertest.cc", + "//brave/browser/search_engine_provider_controller_browsertest.cc", "//brave/browser/ui/content_settings/brave_autoplay_blocked_image_model_browsertest.cc", "//brave/browser/ui/content_settings/brave_widevine_blocked_image_model_browsertest.cc", "//brave/browser/ui/webui/brave_new_tab_ui_browsertest.cc",