-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #310 from brave/support_alternate_private_search_e…
…ngine Support alternate private search engine provider
- Loading branch information
Showing
18 changed files
with
403 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.