Skip to content

Commit a9b7b64

Browse files
mkarolinbsclifton
authored andcommitted
Cleans up prepopulated search engines.
Engines kept: - Google - Duckduckgo - Qwant - Bing - Startpage. Adds a unit test to verify the search engines overridden by us are used instead of the original ones. Lint fixes. Fixes brave/brave-browser#3316
1 parent 01f4eee commit a9b7b64

6 files changed

+197
-753
lines changed

browser/importer/brave_profile_writer.cc

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
/* This Source Code Form is subject to the terms of the Mozilla Public
1+
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
23
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
34
* You can obtain one at http://mozilla.org/MPL/2.0/. */
45

56
#include "brave/browser/importer/brave_profile_writer.h"
7+
8+
#include <map>
9+
#include <memory>
10+
#include <sstream>
11+
#include <string>
12+
#include <utility>
13+
614
#include "brave/common/importer/brave_stats.h"
715
#include "brave/common/importer/brave_referral.h"
816
#include "brave/common/importer/imported_browser_window.h"
@@ -43,8 +51,6 @@
4351
#include "services/network/public/mojom/cookie_manager.mojom.h"
4452
#include "ui/base/ui_base_types.h"
4553

46-
#include <sstream>
47-
4854
BraveProfileWriter::BraveProfileWriter(Profile* profile)
4955
: ProfileWriter(profile),
5056
task_runner_(base::CreateSequencedTaskRunnerWithTraits({
@@ -119,7 +125,8 @@ void BraveProfileWriter::BackupWallet() {
119125
const base::FilePath profile_default_directory = profile_->GetPath();
120126
std::ostringstream backup_filename;
121127
backup_filename << "ledger_import_backup_"
122-
<< base::NumberToString((unsigned long long)base::Time::Now().ToJsTime());
128+
<< base::NumberToString(static_cast<unsigned long long>(
129+
base::Time::Now().ToJsTime()));
123130

124131
LOG(INFO) << "Making backup of current \"ledger_state\" as "
125132
<< "\"" << backup_filename.str() << "\"";
@@ -244,8 +251,9 @@ void BraveProfileWriter::SetWalletProperties(brave_rewards::RewardsService*
244251
pinned_item_count_ = 0;
245252
for (const auto& publisher : ledger_.pinned_publishers) {
246253
// NOTE: this will truncate (ex: 0.90 would be 0, not 1)
247-
const int amount_in_bat = (int)((publisher.pin_percentage / 100.0) *
248-
ledger_.settings.payments.contribution_amount);
254+
const int amount_in_bat =
255+
static_cast<int>((publisher.pin_percentage / 100.0) *
256+
ledger_.settings.payments.contribution_amount);
249257
if (amount_in_bat > 0) {
250258
pinned_item_count_++;
251259
sum_of_monthly_tips += amount_in_bat;
@@ -400,15 +408,14 @@ void OpenImportedBrowserTabs(Browser* browser,
400408
"", false, pinned, true,
401409
base::TimeTicks::UnixEpoch(), nullptr,
402410
"", true /* from_session_restore */);
403-
404411
}
405412
}
406413

407414
int GetSelectedTabIndex(const ImportedBrowserWindow& window) {
408415
// The window has an activeFrameKey, which may be equal to the key for one of
409416
// its tabs. Find the matching tab, if one exists, and return its index in
410417
// the tabs vector.
411-
for (int i = 0; i < (int)window.tabs.size(); i++) {
418+
for (int i = 0; i < static_cast<int>(window.tabs.size()); i++) {
412419
if (window.activeFrameKey == window.tabs[i].key)
413420
return i;
414421
}
@@ -455,28 +462,16 @@ void BraveProfileWriter::UpdateWindows(
455462

456463
// NOTE: the strings used as keys match the values found in Muon:
457464
// browser-laptop/js/data/searchProviders.js
465+
// Providers that aren't in this map are no longer prepopulated (Amazon,
466+
// Ecosia, GitHub, etc.) and the current default provider won't be changed.
458467
const std::map<std::string,
459468
const TemplateURLPrepopulateData::PrepopulatedEngine>
460469
importable_engines = {
461-
{"Amazon", TemplateURLPrepopulateData::amazon},
462470
{"Bing", TemplateURLPrepopulateData::bing},
463471
{"DuckDuckGo", TemplateURLPrepopulateData::duckduckgo},
464-
{"Ecosia", TemplateURLPrepopulateData::ecosia},
465-
{"GitHub", TemplateURLPrepopulateData::github},
466472
{"Google", TemplateURLPrepopulateData::google},
467-
{"Infogalactic", TemplateURLPrepopulateData::infogalactic},
468-
{"MDN Web Docs", TemplateURLPrepopulateData::mdnwebdocs},
469473
{"Qwant", TemplateURLPrepopulateData::qwant},
470-
{"searx", TemplateURLPrepopulateData::searx},
471-
{"Semantic Scholar", TemplateURLPrepopulateData::semanticscholar},
472-
{"Stack Overflow", TemplateURLPrepopulateData::stackoverflow},
473474
{"StartPage", TemplateURLPrepopulateData::startpage},
474-
{"Twitter", TemplateURLPrepopulateData::twitter},
475-
{"Wikipedia", TemplateURLPrepopulateData::wikipedia},
476-
{"Wolfram Alpha", TemplateURLPrepopulateData::wolframalpha},
477-
{"Yahoo", TemplateURLPrepopulateData::yahoo},
478-
{"Yandex", TemplateURLPrepopulateData::yandex},
479-
{"YouTube", TemplateURLPrepopulateData::youtube}
480475
};
481476

482477
void BraveProfileWriter::UpdateSettings(const SessionStoreSettings& settings) {

chromium_src/components/search_engines/brave_template_url_prepopulate_data_unittest.cc

+18-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <stddef.h>
66

77
#include <memory>
8+
#include <string>
9+
#include <unordered_set>
810
#include <utility>
911

1012
#include "base/command_line.h"
@@ -39,11 +41,12 @@ std::string GetHostFromTemplateURLData(const TemplateURLData& data) {
3941
using namespace TemplateURLPrepopulateData; // NOLINT
4042

4143
const PrepopulatedEngine* const kBraveAddedEngines[] = {
42-
&amazon, &ecosia, &github, &mdnwebdocs, &searx,
43-
&semanticscholar, &stackoverflow, &startpage, &twitter,
44-
&wikipedia, &wolframalpha, &yandex, &youtube,
44+
&startpage,
4545
};
4646

47+
const std::unordered_set<std::wstring> kOverriddenEnginesNames = {L"DuckDuckGo",
48+
L"Qwant"};
49+
4750
std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() {
4851
std::vector<const PrepopulatedEngine*> engines =
4952
TemplateURLPrepopulateData::GetAllPrepopulatedEngines();
@@ -78,11 +81,22 @@ TEST_F(BraveTemplateURLPrepopulateDataTest, UniqueKeywords) {
7881
}
7982
}
8083

84+
// Verifies that engines we override are used and not the original engines.
85+
TEST_F(BraveTemplateURLPrepopulateDataTest, OverriddenEngines) {
86+
using PrepopulatedEngine = TemplateURLPrepopulateData::PrepopulatedEngine;
87+
const std::vector<const PrepopulatedEngine*> all_engines =
88+
::GetAllPrepopulatedEngines();
89+
for (const PrepopulatedEngine* engine : all_engines) {
90+
if (kOverriddenEnginesNames.count(engine->name) > 0)
91+
ASSERT_GE(static_cast<unsigned int>(engine->id),
92+
TemplateURLPrepopulateData::BRAVE_PREPOPULATED_ENGINES_START);
93+
}
94+
}
95+
8196
// Verifies that the set of prepopulate data for each locale
8297
// doesn't contain entries with duplicate ids.
8398
TEST_F(BraveTemplateURLPrepopulateDataTest, UniqueIDs) {
8499
const int kCountryIds[] = {
85-
'C' << 8 | 'A',
86100
'D' << 8 | 'E',
87101
'F' << 8 | 'R',
88102
'U' << 8 | 'S',

chromium_src/components/search_engines/brave_template_url_service_util_unittest.cc

+21-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// Copyright 2014 The Chromium Authors. All rights reserved.
2-
// Use of this source code is governed by a BSD-style license that can be
3-
// found in the LICENSE file.
1+
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
45

56
#include <stddef.h>
67

@@ -47,30 +48,24 @@ class BraveTemplateURLServiceUtilTest : public testing::Test {
4748
TEST_F(BraveTemplateURLServiceUtilTest, GetSearchProvidersUsingKeywordResult) {
4849
std::vector<TemplateURLData> local_turls;
4950
// Create a sets of TURLs in order different from prepopulated TURLs.
50-
local_turls.push_back(*CreatePrepopulateTemplateURLData(508, ":x", 1));
51-
local_turls.push_back(
52-
*CreatePrepopulateTemplateURLData(502, ":e", 2));
53-
local_turls.push_back(*CreatePrepopulateTemplateURLData(15, ":ya", 3));
54-
local_turls.push_back(
55-
*CreatePrepopulateTemplateURLData(511, ":sp", 4));
56-
local_turls.push_back(*CreatePrepopulateTemplateURLData(505, ":i", 5));
57-
local_turls.push_back(*CreatePrepopulateTemplateURLData(3, ":b", 6));
58-
local_turls.push_back(*CreatePrepopulateTemplateURLData(507, ":q", 7));
59-
local_turls.push_back(
60-
*CreatePrepopulateTemplateURLData(501, ":d", 8));
61-
local_turls.push_back(*CreatePrepopulateTemplateURLData(1, ":g", 9));
51+
local_turls.push_back(*CreatePrepopulateTemplateURLData(511, ":sp", 1));
52+
local_turls.push_back(*CreatePrepopulateTemplateURLData(15, ":ya", 2));
53+
local_turls.push_back(*CreatePrepopulateTemplateURLData(3, ":b", 3));
54+
local_turls.push_back(*CreatePrepopulateTemplateURLData(507, ":q", 4));
55+
local_turls.push_back(*CreatePrepopulateTemplateURLData(501, ":d", 5));
56+
local_turls.push_back(*CreatePrepopulateTemplateURLData(1, ":g", 6));
6257
std::unique_ptr<TemplateURL> default_turl =
6358
std::make_unique<TemplateURL>(local_turls.back());
6459

6560
// Add TURLs with PrepopulateID that doesn't exist in prepopulated TURLs.
66-
local_turls.push_back(*CreatePrepopulateTemplateURLData(0, "random1", 10));
67-
local_turls.push_back(*CreatePrepopulateTemplateURLData(1004, "random2", 11));
61+
local_turls.push_back(*CreatePrepopulateTemplateURLData(0, "random1", 7));
62+
local_turls.push_back(*CreatePrepopulateTemplateURLData(1004, "random2", 8));
6863

6964
// Prepare call arguments
7065
WDKeywordsResult kwResult;
7166
kwResult.builtin_keyword_version =
7267
TemplateURLPrepopulateData::GetDataVersion(&prefs_);
73-
kwResult.default_search_provider_id = 9;
68+
kwResult.default_search_provider_id = 2;
7469
kwResult.keywords = local_turls;
7570
WDResult<WDKeywordsResult> result(KEYWORDS_RESULT, kwResult);
7671

@@ -88,13 +83,10 @@ TEST_F(BraveTemplateURLServiceUtilTest, GetSearchProvidersUsingKeywordResult) {
8883
EXPECT_EQ(template_urls[1]->keyword(), base::ASCIIToUTF16(":d"));
8984
EXPECT_EQ(template_urls[2]->keyword(), base::ASCIIToUTF16(":q"));
9085
EXPECT_EQ(template_urls[3]->keyword(), base::ASCIIToUTF16(":b"));
91-
EXPECT_EQ(template_urls[4]->keyword(), base::ASCIIToUTF16(":e"));
92-
EXPECT_EQ(template_urls[5]->keyword(), base::ASCIIToUTF16(":i"));
93-
EXPECT_EQ(template_urls[6]->keyword(), base::ASCIIToUTF16(":x"));
94-
EXPECT_EQ(template_urls[7]->keyword(), base::ASCIIToUTF16(":sp"));
95-
EXPECT_EQ(template_urls[8]->keyword(), base::ASCIIToUTF16(":ya"));
96-
EXPECT_EQ(template_urls[9]->keyword(), base::ASCIIToUTF16("random1"));
97-
EXPECT_EQ(template_urls[10]->keyword(), base::ASCIIToUTF16("random2"));
86+
EXPECT_EQ(template_urls[4]->keyword(), base::ASCIIToUTF16(":sp"));
87+
EXPECT_EQ(template_urls[5]->keyword(), base::ASCIIToUTF16(":ya"));
88+
EXPECT_EQ(template_urls[6]->keyword(), base::ASCIIToUTF16("random1"));
89+
EXPECT_EQ(template_urls[7]->keyword(), base::ASCIIToUTF16("random2"));
9890

9991
// Reset
10092
template_urls.clear();
@@ -114,11 +106,8 @@ TEST_F(BraveTemplateURLServiceUtilTest, GetSearchProvidersUsingKeywordResult) {
114106
EXPECT_EQ(template_urls[1]->keyword(), base::ASCIIToUTF16(":g"));
115107
EXPECT_EQ(template_urls[2]->keyword(), base::ASCIIToUTF16(":d"));
116108
EXPECT_EQ(template_urls[3]->keyword(), base::ASCIIToUTF16(":b"));
117-
EXPECT_EQ(template_urls[4]->keyword(), base::ASCIIToUTF16(":e"));
118-
EXPECT_EQ(template_urls[5]->keyword(), base::ASCIIToUTF16(":i"));
119-
EXPECT_EQ(template_urls[6]->keyword(), base::ASCIIToUTF16(":x"));
120-
EXPECT_EQ(template_urls[7]->keyword(), base::ASCIIToUTF16(":sp"));
121-
EXPECT_EQ(template_urls[8]->keyword(), base::ASCIIToUTF16(":ya"));
122-
EXPECT_EQ(template_urls[9]->keyword(), base::ASCIIToUTF16("random1"));
123-
EXPECT_EQ(template_urls[10]->keyword(), base::ASCIIToUTF16("random2"));
109+
EXPECT_EQ(template_urls[4]->keyword(), base::ASCIIToUTF16(":sp"));
110+
EXPECT_EQ(template_urls[5]->keyword(), base::ASCIIToUTF16(":ya"));
111+
EXPECT_EQ(template_urls[6]->keyword(), base::ASCIIToUTF16("random1"));
112+
EXPECT_EQ(template_urls[7]->keyword(), base::ASCIIToUTF16("random2"));
124113
}

0 commit comments

Comments
 (0)