Skip to content

Commit

Permalink
Cleans up prepopulated search engines.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mkarolin authored and bsclifton committed Feb 14, 2019
1 parent 01f4eee commit a9b7b64
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 753 deletions.
39 changes: 17 additions & 22 deletions browser/importer/brave_profile_writer.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
/* 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/. */

#include "brave/browser/importer/brave_profile_writer.h"

#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <utility>

#include "brave/common/importer/brave_stats.h"
#include "brave/common/importer/brave_referral.h"
#include "brave/common/importer/imported_browser_window.h"
Expand Down Expand Up @@ -43,8 +51,6 @@
#include "services/network/public/mojom/cookie_manager.mojom.h"
#include "ui/base/ui_base_types.h"

#include <sstream>

BraveProfileWriter::BraveProfileWriter(Profile* profile)
: ProfileWriter(profile),
task_runner_(base::CreateSequencedTaskRunnerWithTraits({
Expand Down Expand Up @@ -119,7 +125,8 @@ void BraveProfileWriter::BackupWallet() {
const base::FilePath profile_default_directory = profile_->GetPath();
std::ostringstream backup_filename;
backup_filename << "ledger_import_backup_"
<< base::NumberToString((unsigned long long)base::Time::Now().ToJsTime());
<< base::NumberToString(static_cast<unsigned long long>(
base::Time::Now().ToJsTime()));

LOG(INFO) << "Making backup of current \"ledger_state\" as "
<< "\"" << backup_filename.str() << "\"";
Expand Down Expand Up @@ -244,8 +251,9 @@ void BraveProfileWriter::SetWalletProperties(brave_rewards::RewardsService*
pinned_item_count_ = 0;
for (const auto& publisher : ledger_.pinned_publishers) {
// NOTE: this will truncate (ex: 0.90 would be 0, not 1)
const int amount_in_bat = (int)((publisher.pin_percentage / 100.0) *
ledger_.settings.payments.contribution_amount);
const int amount_in_bat =
static_cast<int>((publisher.pin_percentage / 100.0) *
ledger_.settings.payments.contribution_amount);
if (amount_in_bat > 0) {
pinned_item_count_++;
sum_of_monthly_tips += amount_in_bat;
Expand Down Expand Up @@ -400,15 +408,14 @@ void OpenImportedBrowserTabs(Browser* browser,
"", false, pinned, true,
base::TimeTicks::UnixEpoch(), nullptr,
"", true /* from_session_restore */);

}
}

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

// NOTE: the strings used as keys match the values found in Muon:
// browser-laptop/js/data/searchProviders.js
// Providers that aren't in this map are no longer prepopulated (Amazon,
// Ecosia, GitHub, etc.) and the current default provider won't be changed.
const std::map<std::string,
const TemplateURLPrepopulateData::PrepopulatedEngine>
importable_engines = {
{"Amazon", TemplateURLPrepopulateData::amazon},
{"Bing", TemplateURLPrepopulateData::bing},
{"DuckDuckGo", TemplateURLPrepopulateData::duckduckgo},
{"Ecosia", TemplateURLPrepopulateData::ecosia},
{"GitHub", TemplateURLPrepopulateData::github},
{"Google", TemplateURLPrepopulateData::google},
{"Infogalactic", TemplateURLPrepopulateData::infogalactic},
{"MDN Web Docs", TemplateURLPrepopulateData::mdnwebdocs},
{"Qwant", TemplateURLPrepopulateData::qwant},
{"searx", TemplateURLPrepopulateData::searx},
{"Semantic Scholar", TemplateURLPrepopulateData::semanticscholar},
{"Stack Overflow", TemplateURLPrepopulateData::stackoverflow},
{"StartPage", TemplateURLPrepopulateData::startpage},
{"Twitter", TemplateURLPrepopulateData::twitter},
{"Wikipedia", TemplateURLPrepopulateData::wikipedia},
{"Wolfram Alpha", TemplateURLPrepopulateData::wolframalpha},
{"Yahoo", TemplateURLPrepopulateData::yahoo},
{"Yandex", TemplateURLPrepopulateData::yandex},
{"YouTube", TemplateURLPrepopulateData::youtube}
};

void BraveProfileWriter::UpdateSettings(const SessionStoreSettings& settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <stddef.h>

#include <memory>
#include <string>
#include <unordered_set>
#include <utility>

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

const PrepopulatedEngine* const kBraveAddedEngines[] = {
&amazon, &ecosia, &github, &mdnwebdocs, &searx,
&semanticscholar, &stackoverflow, &startpage, &twitter,
&wikipedia, &wolframalpha, &yandex, &youtube,
&startpage,
};

const std::unordered_set<std::wstring> kOverriddenEnginesNames = {L"DuckDuckGo",
L"Qwant"};

std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() {
std::vector<const PrepopulatedEngine*> engines =
TemplateURLPrepopulateData::GetAllPrepopulatedEngines();
Expand Down Expand Up @@ -78,11 +81,22 @@ TEST_F(BraveTemplateURLPrepopulateDataTest, UniqueKeywords) {
}
}

// Verifies that engines we override are used and not the original engines.
TEST_F(BraveTemplateURLPrepopulateDataTest, OverriddenEngines) {
using PrepopulatedEngine = TemplateURLPrepopulateData::PrepopulatedEngine;
const std::vector<const PrepopulatedEngine*> all_engines =
::GetAllPrepopulatedEngines();
for (const PrepopulatedEngine* engine : all_engines) {
if (kOverriddenEnginesNames.count(engine->name) > 0)
ASSERT_GE(static_cast<unsigned int>(engine->id),
TemplateURLPrepopulateData::BRAVE_PREPOPULATED_ENGINES_START);
}
}

// Verifies that the set of prepopulate data for each locale
// doesn't contain entries with duplicate ids.
TEST_F(BraveTemplateURLPrepopulateDataTest, UniqueIDs) {
const int kCountryIds[] = {
'C' << 8 | 'A',
'D' << 8 | 'E',
'F' << 8 | 'R',
'U' << 8 | 'S',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/* 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 <stddef.h>

Expand Down Expand Up @@ -47,30 +48,24 @@ class BraveTemplateURLServiceUtilTest : public testing::Test {
TEST_F(BraveTemplateURLServiceUtilTest, GetSearchProvidersUsingKeywordResult) {
std::vector<TemplateURLData> local_turls;
// Create a sets of TURLs in order different from prepopulated TURLs.
local_turls.push_back(*CreatePrepopulateTemplateURLData(508, ":x", 1));
local_turls.push_back(
*CreatePrepopulateTemplateURLData(502, ":e", 2));
local_turls.push_back(*CreatePrepopulateTemplateURLData(15, ":ya", 3));
local_turls.push_back(
*CreatePrepopulateTemplateURLData(511, ":sp", 4));
local_turls.push_back(*CreatePrepopulateTemplateURLData(505, ":i", 5));
local_turls.push_back(*CreatePrepopulateTemplateURLData(3, ":b", 6));
local_turls.push_back(*CreatePrepopulateTemplateURLData(507, ":q", 7));
local_turls.push_back(
*CreatePrepopulateTemplateURLData(501, ":d", 8));
local_turls.push_back(*CreatePrepopulateTemplateURLData(1, ":g", 9));
local_turls.push_back(*CreatePrepopulateTemplateURLData(511, ":sp", 1));
local_turls.push_back(*CreatePrepopulateTemplateURLData(15, ":ya", 2));
local_turls.push_back(*CreatePrepopulateTemplateURLData(3, ":b", 3));
local_turls.push_back(*CreatePrepopulateTemplateURLData(507, ":q", 4));
local_turls.push_back(*CreatePrepopulateTemplateURLData(501, ":d", 5));
local_turls.push_back(*CreatePrepopulateTemplateURLData(1, ":g", 6));
std::unique_ptr<TemplateURL> default_turl =
std::make_unique<TemplateURL>(local_turls.back());

// Add TURLs with PrepopulateID that doesn't exist in prepopulated TURLs.
local_turls.push_back(*CreatePrepopulateTemplateURLData(0, "random1", 10));
local_turls.push_back(*CreatePrepopulateTemplateURLData(1004, "random2", 11));
local_turls.push_back(*CreatePrepopulateTemplateURLData(0, "random1", 7));
local_turls.push_back(*CreatePrepopulateTemplateURLData(1004, "random2", 8));

// Prepare call arguments
WDKeywordsResult kwResult;
kwResult.builtin_keyword_version =
TemplateURLPrepopulateData::GetDataVersion(&prefs_);
kwResult.default_search_provider_id = 9;
kwResult.default_search_provider_id = 2;
kwResult.keywords = local_turls;
WDResult<WDKeywordsResult> result(KEYWORDS_RESULT, kwResult);

Expand All @@ -88,13 +83,10 @@ TEST_F(BraveTemplateURLServiceUtilTest, GetSearchProvidersUsingKeywordResult) {
EXPECT_EQ(template_urls[1]->keyword(), base::ASCIIToUTF16(":d"));
EXPECT_EQ(template_urls[2]->keyword(), base::ASCIIToUTF16(":q"));
EXPECT_EQ(template_urls[3]->keyword(), base::ASCIIToUTF16(":b"));
EXPECT_EQ(template_urls[4]->keyword(), base::ASCIIToUTF16(":e"));
EXPECT_EQ(template_urls[5]->keyword(), base::ASCIIToUTF16(":i"));
EXPECT_EQ(template_urls[6]->keyword(), base::ASCIIToUTF16(":x"));
EXPECT_EQ(template_urls[7]->keyword(), base::ASCIIToUTF16(":sp"));
EXPECT_EQ(template_urls[8]->keyword(), base::ASCIIToUTF16(":ya"));
EXPECT_EQ(template_urls[9]->keyword(), base::ASCIIToUTF16("random1"));
EXPECT_EQ(template_urls[10]->keyword(), base::ASCIIToUTF16("random2"));
EXPECT_EQ(template_urls[4]->keyword(), base::ASCIIToUTF16(":sp"));
EXPECT_EQ(template_urls[5]->keyword(), base::ASCIIToUTF16(":ya"));
EXPECT_EQ(template_urls[6]->keyword(), base::ASCIIToUTF16("random1"));
EXPECT_EQ(template_urls[7]->keyword(), base::ASCIIToUTF16("random2"));

// Reset
template_urls.clear();
Expand All @@ -114,11 +106,8 @@ TEST_F(BraveTemplateURLServiceUtilTest, GetSearchProvidersUsingKeywordResult) {
EXPECT_EQ(template_urls[1]->keyword(), base::ASCIIToUTF16(":g"));
EXPECT_EQ(template_urls[2]->keyword(), base::ASCIIToUTF16(":d"));
EXPECT_EQ(template_urls[3]->keyword(), base::ASCIIToUTF16(":b"));
EXPECT_EQ(template_urls[4]->keyword(), base::ASCIIToUTF16(":e"));
EXPECT_EQ(template_urls[5]->keyword(), base::ASCIIToUTF16(":i"));
EXPECT_EQ(template_urls[6]->keyword(), base::ASCIIToUTF16(":x"));
EXPECT_EQ(template_urls[7]->keyword(), base::ASCIIToUTF16(":sp"));
EXPECT_EQ(template_urls[8]->keyword(), base::ASCIIToUTF16(":ya"));
EXPECT_EQ(template_urls[9]->keyword(), base::ASCIIToUTF16("random1"));
EXPECT_EQ(template_urls[10]->keyword(), base::ASCIIToUTF16("random2"));
EXPECT_EQ(template_urls[4]->keyword(), base::ASCIIToUTF16(":sp"));
EXPECT_EQ(template_urls[5]->keyword(), base::ASCIIToUTF16(":ya"));
EXPECT_EQ(template_urls[6]->keyword(), base::ASCIIToUTF16("random1"));
EXPECT_EQ(template_urls[7]->keyword(), base::ASCIIToUTF16("random2"));
}
Loading

0 comments on commit a9b7b64

Please sign in to comment.