Skip to content

Commit

Permalink
Merge pull request #310 from brave/support_alternate_private_search_e…
Browse files Browse the repository at this point in the history
…ngine

Support alternate private search engine provider
  • Loading branch information
bbondy authored Aug 10, 2018
2 parents aa0838b + 0bb0660 commit 181cfa9
Show file tree
Hide file tree
Showing 18 changed files with 403 additions and 8 deletions.
7 changes: 7 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import("//build/config/features.gni")

source_set("browser_process") {
sources = [
"alternate_private_search_engine_controller.cc",
"alternate_private_search_engine_controller.h",
"alternate_private_search_engine_util.cc",
"alternate_private_search_engine_util.h",
"autocomplete/brave_autocomplete_scheme_classifier.cc",
"autocomplete/brave_autocomplete_scheme_classifier.h",
"brave_browser_main_extra_parts.cc",
Expand Down Expand Up @@ -44,6 +48,8 @@ source_set("browser_process") {
"mac/sparkle_glue.mm",
"mac/sparkle_glue.h",
"mac/su_updater.h",
"profile_creation_monitor.cc",
"profile_creation_monitor.h",
"update_util.cc",
"update_util.h",

Expand All @@ -57,6 +63,7 @@ source_set("browser_process") {
"//brave/common",
"//components/component_updater",
"//components/safe_browsing/common:safe_browsing_prefs",
"//components/search_engines",
"//components/spellcheck/browser",
"//content/public/browser",
"//brave/chromium_src:browser",
Expand Down
49 changes: 49 additions & 0 deletions browser/alternate_private_search_engine_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* 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 "base/strings/utf_string_conversions.h"
#include "brave/browser/alternate_private_search_engine_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/search_engines/template_url_service.h"
#include "components/search_engines/template_url_service_observer.h"

using AlternatePrivateSearchEngineTest = InProcessBrowserTest;

IN_PROC_BROWSER_TEST_F(AlternatePrivateSearchEngineTest, PrefTest) {
Profile* profile = browser()->profile();
Profile* incognito_profile = profile->GetOffTheRecordProfile();

auto* service = TemplateURLServiceFactory::GetForProfile(profile);
auto* incognito_service =
TemplateURLServiceFactory::GetForProfile(incognito_profile);

// Test pref is initially disabled.
EXPECT_FALSE(brave::UseAlternatePrivateSearchEngineEnabled(profile));

// Both mode should use same search engine if alternate pref is disabled.
base::string16 normal_search_engine =
service->GetDefaultSearchProvider()->data().short_name();
EXPECT_EQ(service->GetDefaultSearchProvider()->data().short_name(),
incognito_service->GetDefaultSearchProvider()->data().short_name());

// Toggle pref and check incognito_service uses duckduckgo search engine and
// normal mode service uses existing one.
brave::ToggleUseAlternatePrivateSearchEngine(profile);
EXPECT_TRUE(brave::UseAlternatePrivateSearchEngineEnabled(profile));
EXPECT_EQ(incognito_service->GetDefaultSearchProvider()->data().short_name(),
base::ASCIIToUTF16("DuckDuckGo"));
EXPECT_EQ(service->GetDefaultSearchProvider()->data().short_name(),
normal_search_engine);

// Toggle pref again and check both mode uses same search engine.
brave::ToggleUseAlternatePrivateSearchEngine(profile);
EXPECT_FALSE(brave::UseAlternatePrivateSearchEngineEnabled(profile));
EXPECT_EQ(service->GetDefaultSearchProvider()->data().short_name(),
normal_search_engine);
EXPECT_EQ(incognito_service->GetDefaultSearchProvider()->data().short_name(),
normal_search_engine);
}
102 changes: 102 additions & 0 deletions browser/alternate_private_search_engine_controller.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* 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/alternate_private_search_engine_controller.h"

#include "base/strings/utf_string_conversions.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "components/prefs/pref_service.h"
#include "components/search_engines/template_url_service.h"

namespace {

TemplateURLData GetPrivateSearchEngineData() {
TemplateURLData private_search_engine_data;
private_search_engine_data.SetShortName(base::ASCIIToUTF16("DuckDuckGo"));
private_search_engine_data.SetKeyword(base::ASCIIToUTF16("duckduckgo.com"));
private_search_engine_data.SetURL(
"https://duckduckgo.com/?q={searchTerms}&t=brave");
private_search_engine_data.favicon_url =
GURL("https://duckduckgo.com/favicon.ico");
private_search_engine_data.suggestions_url =
"https://duckduckgo.com/ac/?q={searchTerms}&type=list";
return private_search_engine_data;
}

} // namespace

// static
void AlternatePrivateSearchEngineController::Create(Profile* profile) {
// This controller is deleted by itself when observed TemplateURLSearvice is
// destroyed.
new AlternatePrivateSearchEngineController(profile);
}

AlternatePrivateSearchEngineController::AlternatePrivateSearchEngineController(
Profile* profile)
: profile_(profile),
template_url_service_(
TemplateURLServiceFactory::GetForProfile(profile_)) {
DCHECK(profile_->GetProfileType() == Profile::INCOGNITO_PROFILE);

use_alternate_private_search_engine_enabled_.Init(
kUseAlternatePrivateSearchEngine,
profile_->GetOriginalProfile()->GetPrefs(),
base::Bind(&AlternatePrivateSearchEngineController::OnPreferenceChanged,
base::Unretained(this)));

template_url_service_->AddObserver(this);
private_search_engine_url_.reset(
new TemplateURL(GetPrivateSearchEngineData()));
ConfigureAlternatePrivateSearchEngineProvider();
}

AlternatePrivateSearchEngineController::
~AlternatePrivateSearchEngineController() {
}

void AlternatePrivateSearchEngineController::OnTemplateURLServiceChanged() {
// When normal profile's default search provider is changed, it changes
// incognito's default search provider.
// Set alternate search engine again if needed.
ConfigureAlternatePrivateSearchEngineProvider();
}

void
AlternatePrivateSearchEngineController::OnTemplateURLServiceShuttingDown() {
template_url_service_->RemoveObserver(this);
delete this;
}

void AlternatePrivateSearchEngineController::
SetAlternateDefaultPrivateSearchEngine() {
template_url_service_->SetUserSelectedDefaultSearchProvider(
private_search_engine_url_.get());
}

void AlternatePrivateSearchEngineController::
SetNormalModeDefaultSearchEngineAsDefaultPrivateSearchProvider() {
auto* normal_mode_service =
TemplateURLServiceFactory::GetForProfile(profile_->GetOriginalProfile());

TemplateURL normal_url(normal_mode_service->GetDefaultSearchProvider()->data());
template_url_service_->SetUserSelectedDefaultSearchProvider(&normal_url);
}

void AlternatePrivateSearchEngineController::
ConfigureAlternatePrivateSearchEngineProvider() {
if (use_alternate_private_search_engine_enabled_.GetValue())
SetAlternateDefaultPrivateSearchEngine();
else
SetNormalModeDefaultSearchEngineAsDefaultPrivateSearchProvider();
}

void AlternatePrivateSearchEngineController::OnPreferenceChanged(
const std::string& pref_name) {
DCHECK(pref_name == kUseAlternatePrivateSearchEngine);

ConfigureAlternatePrivateSearchEngineProvider();
}
49 changes: 49 additions & 0 deletions browser/alternate_private_search_engine_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* 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_ALTERNATE_PRIVATE_SEARCH_ENGINE_CONTROLLER_H_
#define BRAVE_BROWSER_ALTERNATE_PRIVATE_SEARCH_ENGINE_CONTROLLER_H_

#include <memory>
#include <string>

#include "base/macros.h"
#include "components/prefs/pref_member.h"
#include "components/search_engines/template_url_service_observer.h"

class Profile;
class TemplateURL;
class TemplateURLService;

class AlternatePrivateSearchEngineController
: public TemplateURLServiceObserver {
public:
static void Create(Profile* profile);

private:
explicit AlternatePrivateSearchEngineController(Profile* profile);
~AlternatePrivateSearchEngineController() override;

void SetAlternateDefaultPrivateSearchEngine();
void SetNormalModeDefaultSearchEngineAsDefaultPrivateSearchProvider();

void ConfigureAlternatePrivateSearchEngineProvider();

void OnPreferenceChanged(const std::string& pref_name);

// TemplateURLServiceObserver overrides:
void OnTemplateURLServiceChanged() override;
void OnTemplateURLServiceShuttingDown() override;

std::unique_ptr<TemplateURL> private_search_engine_url_;
BooleanPrefMember use_alternate_private_search_engine_enabled_;

Profile* profile_;
TemplateURLService* template_url_service_;

DISALLOW_COPY_AND_ASSIGN(AlternatePrivateSearchEngineController);
};


#endif // BRAVE_BROWSER_ALTERNATE_PRIVATE_SEARCH_ENGINE_CONTROLLER_H_
30 changes: 30 additions & 0 deletions browser/alternate_private_search_engine_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* 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/alternate_private_search_engine_util.h"

#include "brave/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"
#include "components/pref_registry/pref_registry_syncable.h"

namespace brave {

bool UseAlternatePrivateSearchEngineEnabled(Profile* profile) {
return profile->GetOriginalProfile()->GetPrefs()->GetBoolean(
kUseAlternatePrivateSearchEngine);
}

void ToggleUseAlternatePrivateSearchEngine(Profile* profile) {
profile->GetOriginalProfile()->GetPrefs()->SetBoolean(
kUseAlternatePrivateSearchEngine,
!UseAlternatePrivateSearchEngineEnabled(profile));
}

void RegisterAlternatePrivateSearchEngineProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(kUseAlternatePrivateSearchEngine, false);
}

} // namespace brave
25 changes: 25 additions & 0 deletions browser/alternate_private_search_engine_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* 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_ALTERNATE_PRIVATE_SEARCH_ENGINE_UTIL_H_
#define BRAVE_BROWSER_ALTERNATE_PRIVATE_SEARCH_ENGINE_UTIL_H_

class Profile;

namespace user_prefs {
class PrefRegistrySyncable;
}

namespace brave {

bool UseAlternatePrivateSearchEngineEnabled(Profile* profile);

void RegisterAlternatePrivateSearchEngineProfilePrefs(
user_prefs::PrefRegistrySyncable* registry);

void ToggleUseAlternatePrivateSearchEngine(Profile* profile);

} // namespace brave

#endif // BRAVE_BROWSER_ALTERNATE_PRIVATE_SEARCH_ENGINE_UTIL_H_
6 changes: 4 additions & 2 deletions browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include "base/bind.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "brave/browser/component_updater/brave_component_updater_configurator.h"
#include "brave/browser/brave_stats_updater.h"
#include "brave/browser/component_updater/brave_component_updater_configurator.h"
#include "brave/browser/extensions/brave_tor_client_updater.h"
#include "brave/browser/profile_creation_monitor.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service.h"
#include "brave/components/brave_shields/browser/https_everywhere_service.h"
Expand All @@ -28,7 +29,8 @@ BraveBrowserProcessImpl::~BraveBrowserProcessImpl() {

BraveBrowserProcessImpl::BraveBrowserProcessImpl(
base::SequencedTaskRunner* local_state_task_runner)
: BrowserProcessImpl(local_state_task_runner) {
: BrowserProcessImpl(local_state_task_runner),
profile_creation_monitor_(new ProfileCreationMonitor) {
g_browser_process = this;
g_brave_browser_process = this;
brave_stats_updater_ = brave::BraveStatsUpdaterFactory(local_state());
Expand Down
4 changes: 4 additions & 0 deletions browser/brave_browser_process_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "chrome/browser/browser_process_impl.h"

class ProfileCreationMonitor;

namespace brave {
class BraveStatsUpdater;
}
Expand Down Expand Up @@ -56,6 +58,8 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
std::unique_ptr<component_updater::ComponentUpdateService>
brave_component_updater_;

std::unique_ptr<ProfileCreationMonitor> profile_creation_monitor_;

DISALLOW_COPY_AND_ASSIGN(BraveBrowserProcessImpl);
};

Expand Down
3 changes: 3 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "brave/browser/brave_profile_prefs.h"

#include "brave/browser/alternate_private_search_engine_util.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "chrome/browser/prefs/session_startup_pref.h"
Expand All @@ -19,6 +20,8 @@ namespace brave {
void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
brave_shields::BraveShieldsWebContentsObserver::RegisterProfilePrefs(registry);

RegisterAlternatePrivateSearchEngineProfilePrefs(registry);

registry->RegisterBooleanPref(kWidevineOptedIn, false);

// No sign into Brave functionality
Expand Down
33 changes: 33 additions & 0 deletions browser/profile_creation_monitor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* 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/profile_creation_monitor.h"

#include "brave/browser/alternate_private_search_engine_controller.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/notification_service.h"

ProfileCreationMonitor::ProfileCreationMonitor() {
registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED,
content::NotificationService::AllSources());
}

ProfileCreationMonitor::~ProfileCreationMonitor() {}

void ProfileCreationMonitor::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_PROFILE_CREATED: {
Profile* profile = content::Source<Profile>(source).ptr();
if (profile->GetProfileType() == Profile::INCOGNITO_PROFILE)
AlternatePrivateSearchEngineController::Create(profile);
break;
}
default:
NOTREACHED();
}
}
Loading

0 comments on commit 181cfa9

Please sign in to comment.