Skip to content

Commit

Permalink
Merge pull request #3759 from brave/pr3738_bsc-search-engine-fixes_0.…
Browse files Browse the repository at this point in the history
…70.x

Fix default search logic for Tor window and guest window (uplift to 0.70.x)
  • Loading branch information
bsclifton authored Oct 21, 2019
2 parents 21b9af2 + 508ab9e commit 0ac6ae5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ void GuestWindowSearchEngineProviderService::OnTemplateURLServiceChanged() {
if (ignore_template_url_service_changing_)
return;

// The purpose of below code is togging alternative prefs
// 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 =
otr_template_url_service_->GetDefaultSearchProvider()->
data().prepopulate_id ==
TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO;
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_);
Expand Down
50 changes: 33 additions & 17 deletions browser/search_engines/tor_window_search_engine_provider_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ TorWindowSearchEngineProviderService(Profile* otr_profile)

// Configure previously used provider because effective tor profile is
// off the recored profile.
auto provider_data =
TemplateURLPrepopulateData::GetPrepopulatedEngine(
otr_profile->GetPrefs(), GetInitialSearchEngineProvider());
auto provider_data = GetInitialSearchEngineProvider(otr_profile->GetPrefs());
TemplateURL provider_url(*provider_data);
otr_template_url_service_->SetUserSelectedDefaultSearchProvider(
&provider_url);
Expand All @@ -48,23 +46,41 @@ void TorWindowSearchEngineProviderService::OnTemplateURLServiceChanged() {
data().prepopulate_id);
}

int TorWindowSearchEngineProviderService::
GetInitialSearchEngineProvider() const {
std::unique_ptr<TemplateURLData>
TorWindowSearchEngineProviderService::GetInitialSearchEngineProvider(
PrefService* prefs) const {
std::unique_ptr<TemplateURLData> provider_data = nullptr;
int initial_id = alternative_search_engine_provider_in_tor_.GetValue();

bool region_for_qwant =
TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(
otr_profile_->GetPrefs())->prepopulate_id ==
TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_QWANT;
if (initial_id !=
TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_INVALID) {
provider_data = std::move(
TemplateURLPrepopulateData::GetPrepopulatedEngine(prefs, initial_id));
}

// If this is first run, |initial_id| is invalid. Then, use qwant or ddg
// depends on default prepopulate data.
if (initial_id ==
TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_INVALID) {
initial_id = region_for_qwant ?
TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_QWANT :
TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_DUCKDUCKGO;
// depends on default prepopulate data. If not, check that the initial_id
// returned data.
if (!provider_data) {
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 = std::move(
TemplateURLPrepopulateData::GetPrepopulatedEngine(prefs, initial_id));
}

return initial_id;
DCHECK(provider_data);
return provider_data;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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 <memory>
#include "brave/browser/search_engines/search_engine_provider_service.h"
#include "components/prefs/pref_member.h"
#include "components/search_engines/template_url_service_observer.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.
Expand All @@ -23,7 +28,8 @@ class TorWindowSearchEngineProviderService
// TemplateURLServiceObserver overrides:
void OnTemplateURLServiceChanged() override;

int GetInitialSearchEngineProvider() const;
std::unique_ptr<TemplateURLData> GetInitialSearchEngineProvider(
PrefService* prefs) const;

IntegerPrefMember alternative_search_engine_provider_in_tor_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ TEST_F(BraveTemplateURLServiceUtilTest, GetSearchProvidersUsingKeywordResult) {
TemplateURL::OwnedTemplateURLVector template_urls;
int new_resource_keyword_version = 0;

prefs_.SetInteger(kCountryIDAtInstall, 'U' << 8 | 'S');
GetSearchProvidersUsingKeywordResult(result, nullptr, &prefs_, &template_urls,
default_turl.get(), SearchTermsData(),
&new_resource_keyword_version, nullptr);
Expand Down

0 comments on commit 0ac6ae5

Please sign in to comment.