From cf5de7e1d87a5275dbde2fdeb9837a080ed6a7fe Mon Sep 17 00:00:00 2001 From: Simon Hong Date: Thu, 15 Apr 2021 00:43:03 +0900 Subject: [PATCH] WIP: Fixed crash with search provider extension fix: https://github.com/brave/brave-browser/issues/15224 fix: https://github.com/brave/brave-browser/issues/10601 fix: https://github.com/brave/brave-browser/issues/14218 Changed to use same TemplateURLService for normal & private profile. --- browser/BUILD.gn | 6 - ...t_window_search_engine_provider_service.cc | 69 ------- ...st_window_search_engine_provider_service.h | 39 ---- ...e_window_search_engine_provider_service.cc | 51 ----- ...te_window_search_engine_provider_service.h | 31 ---- .../search_engine_provider_service.cc | 51 +++-- .../search_engine_provider_service.h | 35 ++-- .../search_engine_provider_service_factory.cc | 51 ++--- .../search_engine_provider_service_factory.h | 4 +- ...r_window_search_engine_provider_service.cc | 53 ------ ...or_window_search_engine_provider_service.h | 32 ---- browser/ui/views/frame/brave_browser_frame.cc | 174 +++++++++++++++++- browser/ui/views/frame/brave_browser_frame.h | 17 +- .../template_url_service_factory.cc | 18 -- .../template_url_service_factory.h | 20 -- common/pref_names.cc | 2 + common/pref_names.h | 1 + 17 files changed, 260 insertions(+), 394 deletions(-) delete mode 100644 browser/search_engines/guest_window_search_engine_provider_service.cc delete mode 100644 browser/search_engines/guest_window_search_engine_provider_service.h delete mode 100644 browser/search_engines/private_window_search_engine_provider_service.cc delete mode 100644 browser/search_engines/private_window_search_engine_provider_service.h delete mode 100644 browser/search_engines/tor_window_search_engine_provider_service.cc delete mode 100644 browser/search_engines/tor_window_search_engine_provider_service.h delete mode 100644 chromium_src/chrome/browser/search_engines/template_url_service_factory.cc delete mode 100644 chromium_src/chrome/browser/search_engines/template_url_service_factory.h diff --git a/browser/BUILD.gn b/browser/BUILD.gn index 420227633b88..b3a45ee8de1e 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -74,10 +74,6 @@ source_set("browser_process") { "geolocation/brave_geolocation_permission_context_delegate.h", "metrics/metrics_reporting_util.cc", "metrics/metrics_reporting_util.h", - "search_engines/guest_window_search_engine_provider_service.cc", - "search_engines/guest_window_search_engine_provider_service.h", - "search_engines/private_window_search_engine_provider_service.cc", - "search_engines/private_window_search_engine_provider_service.h", "search_engines/search_engine_provider_service.cc", "search_engines/search_engine_provider_service.h", "search_engines/search_engine_provider_service_factory.cc", @@ -86,8 +82,6 @@ source_set("browser_process") { "search_engines/search_engine_provider_util.h", "search_engines/search_engine_tracker.cc", "search_engines/search_engine_tracker.h", - "search_engines/tor_window_search_engine_provider_service.cc", - "search_engines/tor_window_search_engine_provider_service.h", "update_util.cc", "update_util.h", ] diff --git a/browser/search_engines/guest_window_search_engine_provider_service.cc b/browser/search_engines/guest_window_search_engine_provider_service.cc deleted file mode 100644 index 586512dbf426..000000000000 --- a/browser/search_engines/guest_window_search_engine_provider_service.cc +++ /dev/null @@ -1,69 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * 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_engines/guest_window_search_engine_provider_service.h" - -#include "base/auto_reset.h" -#include "brave/browser/profiles/profile_util.h" -#include "brave/browser/search_engines/search_engine_provider_util.h" -#include "brave/components/search_engines/brave_prepopulated_engines.h" -#include "chrome/browser/profiles/profile.h" -#include "components/search_engines/template_url_prepopulate_data.h" -#include "components/search_engines/template_url_service.h" - -// Guest windows starts with default search engine provider because it's guest. -GuestWindowSearchEngineProviderService::GuestWindowSearchEngineProviderService( - Profile* otr_profile) - : SearchEngineProviderService(otr_profile) { - DCHECK(brave::IsGuestProfile(otr_profile)); - DCHECK(otr_profile->IsOffTheRecord()); - DCHECK(!brave::IsRegionForQwant(otr_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 in guest window. - otr_template_url_service_->AddObserver(this); -} - -GuestWindowSearchEngineProviderService:: -~GuestWindowSearchEngineProviderService() { - otr_template_url_service_->RemoveObserver(this); -} - -void GuestWindowSearchEngineProviderService::OnTemplateURLServiceChanged() { - // When this change is came from pref changing, we don't need to control - // prefs again. - if (ignore_template_url_service_changing_) - return; - - // The purpose of below code is toggling alternative prefs - // when user changes from ddg to different search engine provider - // (or vice versa) from settings ui. - bool is_ddg_is_set = false; - switch (otr_template_url_service_->GetDefaultSearchProvider() - ->data() - .prepopulate_id) { - case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO: - case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO_DE: - case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO_AU_NZ_IE: - is_ddg_is_set = true; - } - - if (UseAlternativeSearchEngineProvider() || is_ddg_is_set) - brave::ToggleUseAlternativeSearchEngineProvider(otr_profile_); -} - -void GuestWindowSearchEngineProviderService:: -OnUseAlternativeSearchEngineProviderChanged() { - // When this call is from setting's change, we don't need to set provider - // again. - if (ignore_template_url_service_changing_) - return; - - base::AutoReset reset(&ignore_template_url_service_changing_, true); - UseAlternativeSearchEngineProvider() - ? ChangeToAlternativeSearchEngineProvider() - : ChangeToNormalWindowSearchEngineProvider(); -} diff --git a/browser/search_engines/guest_window_search_engine_provider_service.h b/browser/search_engines/guest_window_search_engine_provider_service.h deleted file mode 100644 index 0311a99d924d..000000000000 --- a/browser/search_engines/guest_window_search_engine_provider_service.h +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * 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_ENGINES_GUEST_WINDOW_SEARCH_ENGINE_PROVIDER_SERVICE_H_ -#define BRAVE_BROWSER_SEARCH_ENGINES_GUEST_WINDOW_SEARCH_ENGINE_PROVIDER_SERVICE_H_ - -#include "brave/browser/search_engines/search_engine_provider_service.h" -#include "components/search_engines/template_url_service_observer.h" - -// This controller is only used by non Qwant region. -// This class manage's newtab's ddg toggle button state. -// Toggle button state should be reflected setting value. -// Ex, when user changes from ddg to others, toggle button should be off and -// vice versa. -class GuestWindowSearchEngineProviderService - : public SearchEngineProviderService, - public TemplateURLServiceObserver { - public: - explicit GuestWindowSearchEngineProviderService(Profile* otr_profile); - ~GuestWindowSearchEngineProviderService() override; - - private: - FRIEND_TEST_ALL_PREFIXES(SearchEngineProviderServiceTest, - GuestWindowControllerTest); - - // TemplateURLServiceObserver overrides: - void OnTemplateURLServiceChanged() override; - - void OnUseAlternativeSearchEngineProviderChanged() override; - - bool ignore_template_url_service_changing_ = false; - - DISALLOW_COPY_AND_ASSIGN(GuestWindowSearchEngineProviderService); -}; - - -#endif // BRAVE_BROWSER_SEARCH_ENGINES_GUEST_WINDOW_SEARCH_ENGINE_PROVIDER_SERVICE_H_ diff --git a/browser/search_engines/private_window_search_engine_provider_service.cc b/browser/search_engines/private_window_search_engine_provider_service.cc deleted file mode 100644 index 6f521e13b951..000000000000 --- a/browser/search_engines/private_window_search_engine_provider_service.cc +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * 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_engines/private_window_search_engine_provider_service.h" - -#include "chrome/browser/profiles/profile.h" -#include "components/search_engines/template_url_service.h" - -PrivateWindowSearchEngineProviderService:: -PrivateWindowSearchEngineProviderService(Profile* otr_profile) - : SearchEngineProviderService(otr_profile) { - DCHECK(otr_profile->IsIncognitoProfile()); - - // 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(); -} - -PrivateWindowSearchEngineProviderService:: -~PrivateWindowSearchEngineProviderService() { - original_template_url_service_->RemoveObserver(this); -} - -void PrivateWindowSearchEngineProviderService:: -OnUseAlternativeSearchEngineProviderChanged() { - ConfigureSearchEngineProvider(); -} - -void PrivateWindowSearchEngineProviderService:: -ConfigureSearchEngineProvider() { - UseAlternativeSearchEngineProvider() - ? ChangeToAlternativeSearchEngineProvider() - : ChangeToNormalWindowSearchEngineProvider(); -} - -void -PrivateWindowSearchEngineProviderService::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/search_engines/private_window_search_engine_provider_service.h b/browser/search_engines/private_window_search_engine_provider_service.h deleted file mode 100644 index 28fe7d39b9d3..000000000000 --- a/browser/search_engines/private_window_search_engine_provider_service.h +++ /dev/null @@ -1,31 +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_SEARCH_ENGINES_PRIVATE_WINDOW_SEARCH_ENGINE_PROVIDER_SERVICE_H_ -#define BRAVE_BROWSER_SEARCH_ENGINES_PRIVATE_WINDOW_SEARCH_ENGINE_PROVIDER_SERVICE_H_ - -#include "brave/browser/search_engines/search_engine_provider_service.h" -#include "components/search_engines/template_url_service_observer.h" - -class PrivateWindowSearchEngineProviderService - : public SearchEngineProviderService, - public TemplateURLServiceObserver { - public: - explicit PrivateWindowSearchEngineProviderService(Profile* otr_profile); - ~PrivateWindowSearchEngineProviderService() override; - - private: - // Configure appropriate provider according to prefs. - void ConfigureSearchEngineProvider(); - - // TemplateURLServiceObserver overrides: - void OnTemplateURLServiceChanged() override; - - // SearchEngineProviderService overrides: - void OnUseAlternativeSearchEngineProviderChanged() override; - - DISALLOW_COPY_AND_ASSIGN(PrivateWindowSearchEngineProviderService); -}; - -#endif // BRAVE_BROWSER_SEARCH_ENGINES_PRIVATE_WINDOW_SEARCH_ENGINE_PROVIDER_SERVICE_H_ diff --git a/browser/search_engines/search_engine_provider_service.cc b/browser/search_engines/search_engine_provider_service.cc index 14a1fcb3082c..543a31b0453a 100644 --- a/browser/search_engines/search_engine_provider_service.cc +++ b/browser/search_engines/search_engine_provider_service.cc @@ -12,15 +12,11 @@ #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" SearchEngineProviderService::SearchEngineProviderService( Profile* otr_profile) : otr_profile_(otr_profile), - original_template_url_service_( - TemplateURLServiceFactory::GetForProfile( - otr_profile_->GetOriginalProfile())), - otr_template_url_service_( + template_url_service_( TemplateURLServiceFactory::GetForProfile(otr_profile_)) { use_alternative_search_engine_provider_.Init( kUseAlternativeSearchEngineProvider, @@ -47,17 +43,49 @@ SearchEngineProviderService::SearchEngineProviderService( // There should ALWAYS be one entry DCHECK(data); alternative_search_engine_url_.reset(new TemplateURL(*data)); + observation_.Observe(template_url_service_); } -SearchEngineProviderService::~SearchEngineProviderService() { +SearchEngineProviderService::~SearchEngineProviderService() = default; + +void SearchEngineProviderService::OnTemplateURLServiceChanged() { + // When this change is came from pref changing, we don't need to control + // prefs again. + if (ignore_template_url_service_changing_) + return; + + // The purpose of below code is toggling alternative prefs + // when user changes from ddg to different search engine provider + // (or vice versa) from settings ui. + bool is_ddg_is_set = false; + switch (template_url_service_->GetDefaultSearchProvider()->data() + .prepopulate_id) { + case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO: + case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO_DE: + case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO_AU_NZ_IE: + is_ddg_is_set = true; + break; + default: + break; + } + + if (UseAlternativeSearchEngineProvider() || is_ddg_is_set) + brave::ToggleUseAlternativeSearchEngineProvider(otr_profile_); } void SearchEngineProviderService::OnPreferenceChanged( const std::string& pref_name) { DCHECK(pref_name == kUseAlternativeSearchEngineProvider); - DCHECK(!brave::IsRegionForQwant(otr_profile_)); - OnUseAlternativeSearchEngineProviderChanged(); + // When this call is from setting's change, we don't need to set provider + // again. + if (ignore_template_url_service_changing_) + return; + + base::AutoReset reset(&ignore_template_url_service_changing_, true); + UseAlternativeSearchEngineProvider() + ? ChangeToAlternativeSearchEngineProvider() + : ChangeToNormalWindowSearchEngineProvider(); } bool @@ -66,13 +94,10 @@ SearchEngineProviderService::UseAlternativeSearchEngineProvider() const { } void SearchEngineProviderService::ChangeToAlternativeSearchEngineProvider() { - otr_template_url_service_->SetUserSelectedDefaultSearchProvider( + template_url_service_->SetUserSelectedDefaultSearchProvider( alternative_search_engine_url_.get()); } void SearchEngineProviderService::ChangeToNormalWindowSearchEngineProvider() { - TemplateURL normal_url( - original_template_url_service_->GetDefaultSearchProvider()->data()); - otr_template_url_service_->SetUserSelectedDefaultSearchProvider( - &normal_url); + template_url_service_->SetUserSelectedDefaultSearchProvider(nullptr); } diff --git a/browser/search_engines/search_engine_provider_service.h b/browser/search_engines/search_engine_provider_service.h index 65ccaa56f898..e3b3a92f9810 100644 --- a/browser/search_engines/search_engine_provider_service.h +++ b/browser/search_engines/search_engine_provider_service.h @@ -8,42 +8,41 @@ #include #include -#include "base/macros.h" +#include "base/scoped_observation.h" #include "components/keyed_service/core/keyed_service.h" #include "components/prefs/pref_member.h" +#include "components/search_engines/template_url_service.h" +#include "components/search_engines/template_url_service_observer.h" class Profile; class TemplateURL; class TemplateURLService; -class SearchEngineProviderService : public KeyedService { +// TODO(simonhong): Rename this to GuestWindowSearchEngineProviderService. +class SearchEngineProviderService : public KeyedService, + public TemplateURLServiceObserver { public: explicit SearchEngineProviderService(Profile* otr_profile); ~SearchEngineProviderService() override; - protected: - // If subclass want to know and configure according to prefs change, override - // this. - virtual void OnUseAlternativeSearchEngineProviderChanged() {} - - bool UseAlternativeSearchEngineProvider() const; - void ChangeToAlternativeSearchEngineProvider(); - void ChangeToNormalWindowSearchEngineProvider(); + // TemplateURLServiceObserver overrides: + void OnTemplateURLServiceChanged() override; + private: // 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_; - + TemplateURLService* template_url_service_; std::unique_ptr alternative_search_engine_url_; + BooleanPrefMember use_alternative_search_engine_provider_; + base::ScopedObservation + observation_{this}; + bool ignore_template_url_service_changing_ = false; - private: + bool UseAlternativeSearchEngineProvider() const; + void ChangeToAlternativeSearchEngineProvider(); + void ChangeToNormalWindowSearchEngineProvider(); void OnPreferenceChanged(const std::string& pref_name); - BooleanPrefMember use_alternative_search_engine_provider_; - DISALLOW_COPY_AND_ASSIGN(SearchEngineProviderService); }; diff --git a/browser/search_engines/search_engine_provider_service_factory.cc b/browser/search_engines/search_engine_provider_service_factory.cc index eef5bd64f4c5..b060425951d3 100644 --- a/browser/search_engines/search_engine_provider_service_factory.cc +++ b/browser/search_engines/search_engine_provider_service_factory.cc @@ -6,10 +6,8 @@ #include "brave/browser/search_engines/search_engine_provider_service_factory.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/search_engines/guest_window_search_engine_provider_service.h" -#include "brave/browser/search_engines/private_window_search_engine_provider_service.h" +#include "brave/browser/search_engines/search_engine_provider_service.h" #include "brave/browser/search_engines/search_engine_provider_util.h" -#include "brave/browser/search_engines/tor_window_search_engine_provider_service.h" #include "brave/common/pref_names.h" #include "brave/components/search_engines/brave_prepopulated_engines.h" #include "chrome/browser/profiles/incognito_helpers.h" @@ -18,40 +16,6 @@ #include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/pref_registry/pref_registry_syncable.h" -namespace { - -// Factory owns service object. -KeyedService* InitializeSearchEngineProviderServiceIfNeeded(Profile* profile) { - // Regardless of qwant region, tor profile needs controller to store - // previously set search engine provider. - if (profile->IsTor()) { - return new TorWindowSearchEngineProviderService(profile); - } - - // Guest profile in qwant region doesn't need special handling of search - // engine provider because its newtab doesn't have ddg toggle button. - if (brave::IsRegionForQwant(profile)) - return nullptr; - - if (brave::IsGuestProfile(profile) && profile->IsOffTheRecord()) { - return new GuestWindowSearchEngineProviderService(profile); - } - - // In non qwant region, controller is also needed for private profile. - // We use separate TemplateURLService for normal and off the recored profile. - // That means changing normal profile's provider doesn't affect otr profile's. - // This controller monitor's normal profile's service and apply its change to - // otr profile to use same provider. - // Private profile's setting is shared with normal profile's setting. - if (profile->IsIncognitoProfile()) { - return new PrivateWindowSearchEngineProviderService(profile); - } - - return nullptr; -} - -} // namespace - // static SearchEngineProviderService* SearchEngineProviderServiceFactory::GetForProfile( Profile* profile) { @@ -76,14 +40,20 @@ SearchEngineProviderServiceFactory::~SearchEngineProviderServiceFactory() {} KeyedService* SearchEngineProviderServiceFactory::BuildServiceInstanceFor( content::BrowserContext* context) const { - return InitializeSearchEngineProviderServiceIfNeeded( - Profile::FromBrowserContext(context)); + // Guest profile in qwant region doesn't need special handling of alternative + // search engine provider because its newtab doesn't have ddg toggle button. + auto* profile = Profile::FromBrowserContext(context); + if (brave::IsGuestProfile(profile) && !brave::IsRegionForQwant(profile)) { + return new SearchEngineProviderService(profile); + } + + return nullptr; } content::BrowserContext* SearchEngineProviderServiceFactory::GetBrowserContextToUse( content::BrowserContext* context) const { - return chrome::GetBrowserContextOwnInstanceInIncognito(context); + return chrome::GetBrowserContextRedirectedInIncognito(context); } bool SearchEngineProviderServiceFactory::ServiceIsNULLWhileTesting() const { @@ -100,4 +70,5 @@ SearchEngineProviderServiceFactory::ServiceIsCreatedWithBrowserContext() const { void SearchEngineProviderServiceFactory::RegisterProfilePrefs( user_prefs::PrefRegistrySyncable* registry) { registry->RegisterBooleanPref(kUseAlternativeSearchEngineProvider, false); + registry->RegisterDictionaryPref(kCachedNormalSearchProvider); } diff --git a/browser/search_engines/search_engine_provider_service_factory.h b/browser/search_engines/search_engine_provider_service_factory.h index a236b2e1396f..2b56093db687 100644 --- a/browser/search_engines/search_engine_provider_service_factory.h +++ b/browser/search_engines/search_engine_provider_service_factory.h @@ -13,9 +13,9 @@ class Profile; class SearchEngineProviderService; // The purpose of this factory is to configure proper search engine provider to -// private/guest/tor profile before it is referenced. +// guest profile before it is referenced. // Also, this factory doesn't have public api. Instead, service is instantiated -// when profile is inistialized. +// when profile is initialized. class SearchEngineProviderServiceFactory : public BrowserContextKeyedServiceFactory { public: diff --git a/browser/search_engines/tor_window_search_engine_provider_service.cc b/browser/search_engines/tor_window_search_engine_provider_service.cc deleted file mode 100644 index 438585ddfa4e..000000000000 --- a/browser/search_engines/tor_window_search_engine_provider_service.cc +++ /dev/null @@ -1,53 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * 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_engines/tor_window_search_engine_provider_service.h" - -#include "brave/components/search_engines/brave_prepopulated_engines.h" -#include "chrome/browser/profiles/profile.h" -#include "components/search_engines/template_url_prepopulate_data.h" -#include "components/search_engines/template_url_service.h" - -TorWindowSearchEngineProviderService:: -TorWindowSearchEngineProviderService(Profile* otr_profile) - : SearchEngineProviderService(otr_profile) { - DCHECK(otr_profile->IsTor()); - - // Config default provider for tor window. - auto provider_data = GetInitialSearchEngineProvider(otr_profile->GetPrefs()); - TemplateURL provider_url(*provider_data); - otr_template_url_service_->SetUserSelectedDefaultSearchProvider( - &provider_url); -} - -TorWindowSearchEngineProviderService::~TorWindowSearchEngineProviderService() = - default; - -std::unique_ptr -TorWindowSearchEngineProviderService::GetInitialSearchEngineProvider( - PrefService* prefs) const { - std::unique_ptr provider_data; - - int initial_id = TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch( - otr_profile_->GetPrefs()) - ->prepopulate_id; - switch (initial_id) { - case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_QWANT: - case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO: - case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO_DE: - case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO_AU_NZ_IE: - break; - - default: - initial_id = - TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO; - break; - } - provider_data = - TemplateURLPrepopulateData::GetPrepopulatedEngine(prefs, initial_id); - - DCHECK(provider_data); - return provider_data; -} diff --git a/browser/search_engines/tor_window_search_engine_provider_service.h b/browser/search_engines/tor_window_search_engine_provider_service.h deleted file mode 100644 index 32396d49fa92..000000000000 --- a/browser/search_engines/tor_window_search_engine_provider_service.h +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * 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_ENGINES_TOR_WINDOW_SEARCH_ENGINE_PROVIDER_SERVICE_H_ -#define BRAVE_BROWSER_SEARCH_ENGINES_TOR_WINDOW_SEARCH_ENGINE_PROVIDER_SERVICE_H_ - -#include - -#include "brave/browser/search_engines/search_engine_provider_service.h" - -class PrefService; -struct TemplateURLData; - -// The purpose of this service for tor is making user changed search engine -// provider persist across the sessions. -// Also, BraveProfileManager::SetNonPersonalProfilePrefs() overrides for it. -class TorWindowSearchEngineProviderService - : public SearchEngineProviderService { - public: - explicit TorWindowSearchEngineProviderService(Profile* otr_profile); - ~TorWindowSearchEngineProviderService() override; - - private: - std::unique_ptr GetInitialSearchEngineProvider( - PrefService* prefs) const; - - DISALLOW_COPY_AND_ASSIGN(TorWindowSearchEngineProviderService); -}; - -#endif // BRAVE_BROWSER_SEARCH_ENGINES_TOR_WINDOW_SEARCH_ENGINE_PROVIDER_SERVICE_H_ diff --git a/browser/ui/views/frame/brave_browser_frame.cc b/browser/ui/views/frame/brave_browser_frame.cc index 99844399c2a5..689ec909724e 100644 --- a/browser/ui/views/frame/brave_browser_frame.cc +++ b/browser/ui/views/frame/brave_browser_frame.cc @@ -5,16 +5,133 @@ #include "brave/browser/ui/views/frame/brave_browser_frame.h" +#include "base/values.h" #include "brave/browser/profiles/profile_util.h" +#include "brave/browser/search_engines/search_engine_provider_util.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 "chrome/browser/themes/theme_service.h" #include "chrome/browser/themes/theme_service_factory.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/views/frame/browser_view.h" +#include "components/search_engines/template_url_data_util.h" +#include "components/search_engines/template_url_prepopulate_data.h" +#include "components/search_engines/template_url_service.h" + +namespace { + +std::unique_ptr GetSearchEngineProviderForTor( + PrefService* prefs) { + std::unique_ptr provider_data; + + int id = TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch( + prefs)->prepopulate_id; + switch (id) { + case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_QWANT: + case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO: + case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO_DE: + case TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO_AU_NZ_IE: + break; + + default: + id = TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO; + break; + } + provider_data = + TemplateURLPrepopulateData::GetPrepopulatedEngine(prefs, id); + + DCHECK(provider_data); + return provider_data; +} + +void HandleTorWindowActivationStateChange(Profile* profile, bool active) { + if (!active) + return; + + auto provider_data = GetSearchEngineProviderForTor(profile->GetPrefs()); + TemplateURL provider_url(*provider_data); + auto* tus = TemplateURLServiceFactory::GetForProfile(profile); + tus->SetUserSelectedDefaultSearchProvider(&provider_url); +} + +void ChangeToAlternativeSearchEngineProvider(Profile* profile) { + // TODO(simonhong): De-duplicate this + std::vector + alt_search_providers = { + TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO, + TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO_DE, + TemplateURLPrepopulateData:: + PREPOPULATED_ENGINE_ID_DUCKDUCKGO_AU_NZ_IE + }; + + std::unique_ptr data; + for (const auto& id : alt_search_providers) { + data = TemplateURLPrepopulateData::GetPrepopulatedEngine( + profile->GetPrefs(), id); + if (data) + break; + } + + // There should ALWAYS be one entry + DCHECK(data); + // alternative_search_engine_url.reset(new TemplateURL(*data)); + auto* tus = TemplateURLServiceFactory::GetForProfile(profile); + tus->SetUserSelectedDefaultSearchProvider(new TemplateURL(*data)); +} + +void ChangeToNormalWindowSearchEngineProvider(Profile* profile) { + const base::Value* data_value = + profile->GetOriginalProfile()->GetPrefs()->Get(kCachedNormalSearchProvider); + if (data_value && data_value->is_dict()) { + std::unique_ptr data = + TemplateURLDataFromDictionary(base::Value::AsDictionaryValue(*data_value)); + auto* tus = TemplateURLServiceFactory::GetForProfile(profile); + tus->SetUserSelectedDefaultSearchProvider(new TemplateURL(*data)); + } +} + +void HandlePrivateWindowActivationStateChange(Profile* profile, bool active) { + if (!active) + return; + + brave::UseAlternativeSearchEngineProviderEnabled(profile) + ? ChangeToAlternativeSearchEngineProvider(profile) + : ChangeToNormalWindowSearchEngineProvider(profile); +} + +void HandleNormalWindowActivationStateChange(Profile* profile, bool active) { + auto* tus = TemplateURLServiceFactory::GetForProfile(profile); + auto* tu = tus->GetDefaultSearchProvider(); + // Cache DSE. + if (!active) { + std::unique_ptr data_value = + TemplateURLDataToDictionary(tu->data()); + profile->GetPrefs()->Set(kCachedNormalSearchProvider, data_value->Clone()); + return; + } + + const base::Value* data_value = + profile->GetPrefs()->Get(kCachedNormalSearchProvider); + // Apply cached DSE. + if (data_value && data_value->is_dict()) { + std::unique_ptr data = + TemplateURLDataFromDictionary(base::Value::AsDictionaryValue(*data_value)); + tus->SetUserSelectedDefaultSearchProvider(new TemplateURL(*data)); + } +} + +} // namespace BraveBrowserFrame::BraveBrowserFrame(BrowserView* browser_view) : BrowserFrame(browser_view), - view_(browser_view) {} + view_(browser_view) { + ObserveWidget(); + ObserveSearchEngineProviderPrefs(); +} + +BraveBrowserFrame::~BraveBrowserFrame() = default; // Tor/Guest profile should use DarkAura. If not, their native ui is affected by // normal windows theme change. @@ -28,3 +145,58 @@ const ui::NativeTheme* BraveBrowserFrame::GetNativeTheme() const { } return views::Widget::GetNativeTheme(); } + +void BraveBrowserFrame::ObserveWidget() { + auto* profile = view_->browser()->profile(); + if (!brave::IsGuestProfile(profile)) { + observation_.Observe(this); + } +} + +void BraveBrowserFrame::ObserveSearchEngineProviderPrefs() { + auto* profile = view_->browser()->profile(); + if (profile->IsTor() || brave::IsGuestProfile(profile)) { + return; + } + + if (profile->IsIncognitoProfile()) { + use_alternative_search_engine_provider_.Init( + kUseAlternativeSearchEngineProvider, + profile->GetOriginalProfile()->GetPrefs(), + base::Bind(&BraveBrowserFrame::OnPreferenceChanged, + base::Unretained(this))); + } +} + +void BraveBrowserFrame::OnPreferenceChanged() { + HandlePrivateWindowActivationStateChange(view_->browser()->profile(), true); +} + +// TODO(simonhong): Create separate class for this. +void BraveBrowserFrame::OnWidgetActivationChanged(Widget* widget, bool active) { + auto* profile = view_->browser()->profile(); + auto* tus = TemplateURLServiceFactory::GetForProfile(profile); + if (!tus) + return; + + auto* tu = tus->GetDefaultSearchProvider(); + if (tu && tu->type() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) + return; + + if (profile->IsTor()) { + HandleTorWindowActivationStateChange(profile, active); + return; + } + + if (brave::IsGuestProfile(profile)) { + NOTREACHED(); + return; + } + + if (profile->IsIncognitoProfile()) { + HandlePrivateWindowActivationStateChange(profile, active); + return; + } + + HandleNormalWindowActivationStateChange(profile, active); +} diff --git a/browser/ui/views/frame/brave_browser_frame.h b/browser/ui/views/frame/brave_browser_frame.h index 92ca89bab307..9bc978738078 100644 --- a/browser/ui/views/frame/brave_browser_frame.h +++ b/browser/ui/views/frame/brave_browser_frame.h @@ -6,17 +6,32 @@ #ifndef BRAVE_BROWSER_UI_VIEWS_FRAME_BRAVE_BROWSER_FRAME_H_ #define BRAVE_BROWSER_UI_VIEWS_FRAME_BRAVE_BROWSER_FRAME_H_ +#include "base/scoped_observation.h" #include "chrome/browser/ui/views/frame/browser_frame.h" +#include "components/prefs/pref_member.h" +#include "ui/views/widget/widget_observer.h" -class BraveBrowserFrame : public BrowserFrame { +class BraveBrowserFrame : public BrowserFrame, + public views::WidgetObserver { public: explicit BraveBrowserFrame(BrowserView* browser_view); + ~BraveBrowserFrame() override; // BrowserFrame overrides: const ui::NativeTheme* GetNativeTheme() const override; + // views::WidgetObserver overrides: + void OnWidgetActivationChanged(Widget* widget, bool active) override; + private: BrowserView* view_; + BooleanPrefMember use_alternative_search_engine_provider_; + base::ScopedObservation + observation_{this}; + + void ObserveWidget(); + void ObserveSearchEngineProviderPrefs(); + void OnPreferenceChanged(); DISALLOW_COPY_AND_ASSIGN(BraveBrowserFrame); }; diff --git a/chromium_src/chrome/browser/search_engines/template_url_service_factory.cc b/chromium_src/chrome/browser/search_engines/template_url_service_factory.cc deleted file mode 100644 index 6bd2e8f6d43b..000000000000 --- a/chromium_src/chrome/browser/search_engines/template_url_service_factory.cc +++ /dev/null @@ -1,18 +0,0 @@ -/* Copyright (c) 2021 The Brave Authors. All rights reserved. - * 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 "chrome/browser/search_engines/template_url_service_factory.h" -#include "chrome/browser/history/history_service_factory.h" -#include "chrome/browser/web_data_service_factory.h" - -#define GetBrowserContextToUse GetBrowserContextToUse_unused -#include "../../../../../chrome/browser/search_engines/template_url_service_factory.cc" -#undef GetBrowserContextToUse - -content::BrowserContext* TemplateURLServiceFactory::GetBrowserContextToUse( - content::BrowserContext* context) const { - // To make different service for normal and incognito profile. - return chrome::GetBrowserContextOwnInstanceInIncognito(context); -} diff --git a/chromium_src/chrome/browser/search_engines/template_url_service_factory.h b/chromium_src/chrome/browser/search_engines/template_url_service_factory.h deleted file mode 100644 index 0c61d79612eb..000000000000 --- a/chromium_src/chrome/browser/search_engines/template_url_service_factory.h +++ /dev/null @@ -1,20 +0,0 @@ -/* Copyright 2021 The Brave Authors. All rights reserved. - * 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_CHROMIUM_SRC_CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_FACTORY_H_ -#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_FACTORY_H_ - -#include "components/keyed_service/content/browser_context_keyed_service_factory.h" -#include "content/public/browser/browser_context.h" - -#define GetBrowserContextToUse \ - GetBrowserContextToUse_unused(content::BrowserContext* context) const; \ - content::BrowserContext* GetBrowserContextToUse - -#include "../../../../../chrome/browser/search_engines/template_url_service_factory.h" - -#undef GetBrowserContextToUse - -#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_FACTORY_H_ diff --git a/common/pref_names.cc b/common/pref_names.cc index 038cf7b3aa09..ce60bdb37419 100644 --- a/common/pref_names.cc +++ b/common/pref_names.cc @@ -33,6 +33,8 @@ const char kWidevineOptedIn[] = "brave.widevine_opted_in"; const char kAskWidevineInstall[] = "brave.ask_widevine_install"; const char kUseAlternativeSearchEngineProvider[] = "brave.use_alternate_private_search_engine"; +const char kCachedNormalSearchProvider[] = + "brave.cached_normal_search_provider"; const char kAlternativeSearchEngineProviderInTor[] = "brave.alternate_private_search_engine_in_tor"; const char kLocationBarIsWide[] = "brave.location_bar_is_wide"; diff --git a/common/pref_names.h b/common/pref_names.h index 12e7dda3fcc8..5e3284aaa715 100644 --- a/common/pref_names.h +++ b/common/pref_names.h @@ -27,6 +27,7 @@ extern const char kAdBlockRegionalFilters[]; extern const char kWidevineOptedIn[]; extern const char kAskWidevineInstall[]; extern const char kUseAlternativeSearchEngineProvider[]; +extern const char kCachedNormalSearchProvider[]; extern const char kAlternativeSearchEngineProviderInTor[]; extern const char kBraveThemeType[]; extern const char kUseOverriddenBraveThemeType[];