Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed crash with search provider extension #8539

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
]
Expand Down
7 changes: 2 additions & 5 deletions browser/profiles/brave_profile_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,11 @@ bool BraveProfileManager::LoadProfileByPath(const base::FilePath& profile_path,
std::move(callback));
}

// This overridden method doesn't clear |kDefaultSearchProviderDataPrefName|.
// W/o this, prefs set by TorWindowSearchEngineProviderService is cleared
// during the initialization.
void BraveProfileManager::SetNonPersonalProfilePrefs(Profile* profile) {
ProfileManager::SetNonPersonalProfilePrefs(profile);

PrefService* prefs = profile->GetPrefs();
prefs->SetBoolean(prefs::kSigninAllowed, false);
prefs->SetBoolean(bookmarks::prefs::kEditBookmarksEnabled, false);
prefs->SetBoolean(bookmarks::prefs::kShowBookmarkBar, false);
}

void BraveProfileManager::MigrateProfileNames() {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

78 changes: 40 additions & 38 deletions browser/search_engines/search_engine_provider_service.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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 "brave/browser/search_engines/search_engine_provider_service.h"

#include <vector>

#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"
Expand All @@ -12,52 +15,54 @@
#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_(
TemplateURLServiceFactory::GetForProfile(otr_profile_)) {
SearchEngineProviderService::SearchEngineProviderService(Profile* profile)
: profile_(profile),
template_url_service_(
TemplateURLServiceFactory::GetForProfile(profile_)) {
use_alternative_search_engine_provider_.Init(
kUseAlternativeSearchEngineProvider,
otr_profile_->GetOriginalProfile()->GetPrefs(),
profile_->GetOriginalProfile()->GetPrefs(),
base::Bind(&SearchEngineProviderService::OnPreferenceChanged,
base::Unretained(this)));

std::vector<TemplateURLPrepopulateData::BravePrepopulatedEngineID>
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<TemplateURLData> data;
for (const auto& id : alt_search_providers) {
data = TemplateURLPrepopulateData::GetPrepopulatedEngine(
otr_profile->GetPrefs(), id);
if (data)
break;
}

// There should ALWAYS be one entry
DCHECK(data);
std::unique_ptr<TemplateURLData> data =
brave::GetDDGTemplateURLData(profile->GetPrefs());
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.
const bool is_ddg_is_set =
template_url_service_->GetDefaultSearchProvider()->prepopulate_id() ==
alternative_search_engine_url_->prepopulate_id();
if (UseAlternativeSearchEngineProvider() || is_ddg_is_set)
brave::ToggleUseAlternativeSearchEngineProvider(profile_);
darkdh marked this conversation as resolved.
Show resolved Hide resolved
}

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<bool> reset(&ignore_template_url_service_changing_, true);
Copy link
Member

@bsclifton bsclifton Apr 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AutoReset<> is a super nice base library offering 🙂 Had never worked with this before, just looked into it now! Really nice to temporarily change the value and then it resets after going out of scope

UseAlternativeSearchEngineProvider()
? ChangeToAlternativeSearchEngineProvider()
: ChangeToDefaultSearchEngineProvider();
}

bool
Expand All @@ -66,13 +71,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);
void SearchEngineProviderService::ChangeToDefaultSearchEngineProvider() {
template_url_service_->SetUserSelectedDefaultSearchProvider(nullptr);
}
Loading