Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Tab Page: fetch data from JS -> C++ instead of WebUIProperties #2957

Merged
merged 5 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 155 additions & 35 deletions browser/ui/webui/brave_new_tab_message_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,152 @@

#include "base/bind.h"
#include "base/values.h"
#include "brave/browser/ui/webui/brave_new_tab_ui.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/browser/search_engines/search_engine_provider_util.h"
#include "brave/browser/ui/webui/brave_new_tab_ui.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/web_ui_data_source.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"

BraveNewTabMessageHandler::BraveNewTabMessageHandler(BraveNewTabUI* web_ui)
: new_tab_web_ui_(web_ui) {
namespace {

bool IsPrivateNewTab(Profile* profile) {
return brave::IsTorProfile(profile) || profile->IsIncognitoProfile();
}

base::DictionaryValue GetStatsDictionary(PrefService* prefs) {
base::DictionaryValue stats_data;
stats_data.SetInteger(
"adsBlockedStat",
prefs->GetUint64(kAdsBlocked) + prefs->GetUint64(kTrackersBlocked));
stats_data.SetInteger(
"javascriptBlockedStat",
prefs->GetUint64(kJavascriptBlocked));
stats_data.SetInteger(
"httpsUpgradesStat",
prefs->GetUint64(kHttpsUpgrades));
stats_data.SetInteger(
"fingerprintingBlockedStat",
prefs->GetUint64(kFingerprintingBlocked));
return stats_data;
}

base::DictionaryValue GetPreferencesDictionary(PrefService* prefs) {
base::DictionaryValue pref_data;
pref_data.SetBoolean(
"showBackgroundImage",
prefs->GetBoolean(kNewTabPageShowBackgroundImage));
pref_data.SetBoolean(
"showClock",
prefs->GetBoolean(kNewTabPageShowClock));
pref_data.SetBoolean(
"showTopSites",
prefs->GetBoolean(kNewTabPageShowTopSites));
pref_data.SetBoolean(
"showStats",
prefs->GetBoolean(kNewTabPageShowStats));
return pref_data;
}

base::DictionaryValue GetPrivatePropertiesDictionary(PrefService* prefs) {
base::DictionaryValue private_data;
private_data.SetBoolean(
"useAlternativePrivateSearchEngine",
prefs->GetBoolean(kUseAlternativeSearchEngineProvider));
return private_data;
}

} // namespace

// static
BraveNewTabMessageHandler* BraveNewTabMessageHandler::Create(
content::WebUIDataSource* source, Profile* profile) {
//
// Initial Values
// Should only contain data that is static
//
// Private Tab info
if (IsPrivateNewTab(profile)) {
source->AddBoolean(
"isTor", brave::IsTorProfile(profile));
source->AddBoolean(
"isQwant", brave::IsRegionForQwant(profile));
}
return new BraveNewTabMessageHandler(profile);
}

BraveNewTabMessageHandler::BraveNewTabMessageHandler(Profile* profile)
: profile_(profile) {
}

BraveNewTabMessageHandler::~BraveNewTabMessageHandler() {}

void BraveNewTabMessageHandler::RegisterMessages() {
// TODO(petemill): This MessageHandler can be split up to
// individual MessageHandlers for each individual topic area,
// should other WebUI pages wish to consume the APIs:
// - Stats
// - Preferences
// - PrivatePage properties
web_ui()->RegisterMessageCallback(
"getNewTabPagePreferences",
base::BindRepeating(
&BraveNewTabMessageHandler::HandleGetPreferences,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getNewTabPageStats",
base::BindRepeating(
&BraveNewTabMessageHandler::HandleGetStats,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getNewTabPagePrivateProperties",
base::BindRepeating(
&BraveNewTabMessageHandler::HandleGetPrivateProperties,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"toggleAlternativePrivateSearchEngine",
base::BindRepeating(
&BraveNewTabMessageHandler::HandleToggleAlternativeSearchEngineProvider,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"saveNewTabPagePref",
base::BindRepeating(
&BraveNewTabMessageHandler::HandleSaveNewTabPagePref,
base::Unretained(this)));
}

void BraveNewTabMessageHandler::OnJavascriptAllowed() {
// Observe relevant preferences
PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
PrefService* prefs = profile_->GetPrefs();
pref_change_registrar_.Init(prefs);
// Stats
pref_change_registrar_.Add(kAdsBlocked,
base::Bind(&BraveNewTabMessageHandler::OnStatsChanged,
base::Unretained(this)));
pref_change_registrar_.Add(kHttpsUpgrades,
pref_change_registrar_.Add(kTrackersBlocked,
base::Bind(&BraveNewTabMessageHandler::OnStatsChanged,
base::Unretained(this)));
pref_change_registrar_.Add(kJavascriptBlocked,
base::Bind(&BraveNewTabMessageHandler::OnStatsChanged,
base::Unretained(this)));
// Private New Tab Page preferences
pref_change_registrar_.Add(kUseAlternativeSearchEngineProvider,
base::Bind(&BraveNewTabMessageHandler::OnPrivatePropertiesChanged,
pref_change_registrar_.Add(kHttpsUpgrades,
base::Bind(&BraveNewTabMessageHandler::OnStatsChanged,
base::Unretained(this)));
pref_change_registrar_.Add(kAlternativeSearchEngineProviderInTor,
base::Bind(&BraveNewTabMessageHandler::OnPrivatePropertiesChanged,
pref_change_registrar_.Add(kFingerprintingBlocked,
base::Bind(&BraveNewTabMessageHandler::OnStatsChanged,
base::Unretained(this)));

if (IsPrivateNewTab(profile_)) {
// Private New Tab Page preferences
pref_change_registrar_.Add(kUseAlternativeSearchEngineProvider,
base::Bind(&BraveNewTabMessageHandler::OnPrivatePropertiesChanged,
base::Unretained(this)));
pref_change_registrar_.Add(kAlternativeSearchEngineProviderInTor,
base::Bind(&BraveNewTabMessageHandler::OnPrivatePropertiesChanged,
base::Unretained(this)));
}
// New Tab Page preferences
pref_change_registrar_.Add(kNewTabPageShowBackgroundImage,
base::Bind(&BraveNewTabMessageHandler::OnPreferencesChanged,
Expand All @@ -58,32 +174,32 @@ void BraveNewTabMessageHandler::OnJavascriptDisallowed() {
pref_change_registrar_.RemoveAll();
}

void BraveNewTabMessageHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"newTabPageInitialized",
base::BindRepeating(
&BraveNewTabMessageHandler::HandleInitialized,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"toggleAlternativePrivateSearchEngine",
base::BindRepeating(
&BraveNewTabMessageHandler::HandleToggleAlternativeSearchEngineProvider,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"saveNewTabPagePref",
base::BindRepeating(
&BraveNewTabMessageHandler::HandleSaveNewTabPagePref,
base::Unretained(this)));
void BraveNewTabMessageHandler::HandleGetPreferences(
const base::ListValue* args) {
AllowJavascript();
PrefService* prefs = profile_->GetPrefs();
auto data = GetPreferencesDictionary(prefs);
ResolveJavascriptCallback(args->GetList()[0], data);
}

void BraveNewTabMessageHandler::HandleGetStats(const base::ListValue* args) {
AllowJavascript();
PrefService* prefs = profile_->GetPrefs();
auto data = GetStatsDictionary(prefs);
ResolveJavascriptCallback(args->GetList()[0], data);
}

void BraveNewTabMessageHandler::HandleInitialized(const base::ListValue* args) {
void BraveNewTabMessageHandler::HandleGetPrivateProperties(
const base::ListValue* args) {
AllowJavascript();
PrefService* prefs = profile_->GetPrefs();
auto data = GetPrivatePropertiesDictionary(prefs);
ResolveJavascriptCallback(args->GetList()[0], data);
}

void BraveNewTabMessageHandler::HandleToggleAlternativeSearchEngineProvider(
const base::ListValue* args) {
brave::ToggleUseAlternativeSearchEngineProvider(
Profile::FromWebUI(web_ui()));
brave::ToggleUseAlternativeSearchEngineProvider(profile_);
}

void BraveNewTabMessageHandler::HandleSaveNewTabPagePref(
Expand All @@ -92,7 +208,7 @@ void BraveNewTabMessageHandler::HandleSaveNewTabPagePref(
LOG(ERROR) << "Invalid input";
return;
}
PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
PrefService* prefs = profile_->GetPrefs();
// Collect args
std::string settingsKeyInput = args->GetList()[0].GetString();
auto settingsValue = args->GetList()[1].Clone();
Expand Down Expand Up @@ -121,15 +237,19 @@ void BraveNewTabMessageHandler::HandleSaveNewTabPagePref(
}

void BraveNewTabMessageHandler::OnPrivatePropertiesChanged() {
new_tab_web_ui_->OnPrivatePropertiesChanged();
PrefService* prefs = profile_->GetPrefs();
auto data = GetPrivatePropertiesDictionary(prefs);
FireWebUIListener("private-tab-data-updated", data);
}

void BraveNewTabMessageHandler::OnStatsChanged() {
new_tab_web_ui_->OnStatsChanged();
FireWebUIListener("stats-updated");
PrefService* prefs = profile_->GetPrefs();
auto data = GetStatsDictionary(prefs);
FireWebUIListener("stats-updated", data);
}

void BraveNewTabMessageHandler::OnPreferencesChanged() {
new_tab_web_ui_->OnPreferencesChanged();
FireWebUIListener("preferences-changed");
PrefService* prefs = profile_->GetPrefs();
auto data = GetPreferencesDictionary(prefs);
FireWebUIListener("preferences-changed", data);
}
16 changes: 12 additions & 4 deletions browser/ui/webui/brave_new_tab_message_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@
#include "content/public/browser/web_ui_message_handler.h"

class Profile;
class BraveNewTabUI;
namespace content {
class WebUIDataSource;
}

// Handles messages to and from the New Tab Page javascript
class BraveNewTabMessageHandler : public content::WebUIMessageHandler {
public:
explicit BraveNewTabMessageHandler(BraveNewTabUI* web_ui);
explicit BraveNewTabMessageHandler(Profile* profile);
~BraveNewTabMessageHandler() override;

static BraveNewTabMessageHandler* Create(
content::WebUIDataSource* html_source, Profile* profile);

private:
// WebUIMessageHandler implementation.
void RegisterMessages() override;
void OnJavascriptAllowed() override;
void OnJavascriptDisallowed() override;

void HandleInitialized(const base::ListValue* args);
void HandleGetPreferences(const base::ListValue* args);
void HandleGetStats(const base::ListValue* args);
void HandleGetPrivateProperties(const base::ListValue* args);
void HandleSaveNewTabPagePref(const base::ListValue* args);
void HandleToggleAlternativeSearchEngineProvider(
const base::ListValue* args);
Expand All @@ -34,7 +41,8 @@ class BraveNewTabMessageHandler : public content::WebUIMessageHandler {
void OnPrivatePropertiesChanged();

PrefChangeRegistrar pref_change_registrar_;
BraveNewTabUI* new_tab_web_ui_;
// Weak pointer.
Profile* profile_;

DISALLOW_COPY_AND_ASSIGN(BraveNewTabMessageHandler);
};
Expand Down
Loading