Skip to content

Commit

Permalink
Merge pull request #2834 from brave/pr2831_ntp-pref-store_0.68.x
Browse files Browse the repository at this point in the history
New Tab Page WebUI: Use Profile Preferences to store page options instead of localStorage (uplift to 0.68.x)
  • Loading branch information
bsclifton authored Jul 3, 2019
2 parents 7ef74e7 + 669c4c2 commit f39ec4f
Show file tree
Hide file tree
Showing 23 changed files with 372 additions and 129 deletions.
3 changes: 3 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {

// IPFS companion extension
registry->RegisterBooleanPref(kIPFSCompanionEnabled, false);

// New Tab Page
registry->RegisterBooleanPref(kNewTabPageShowBackgroundImage, true);
}

} // namespace brave
2 changes: 2 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ source_set("ui") {
"webui/basic_ui.h",
"webui/brave_adblock_ui.cc",
"webui/brave_adblock_ui.h",
"webui/brave_new_tab_message_handler.cc",
"webui/brave_new_tab_message_handler.h",
"webui/brave_new_tab_ui.cc",
"webui/brave_new_tab_ui.h",
"webui/brave_web_ui_controller_factory.cc",
Expand Down
123 changes: 123 additions & 0 deletions browser/ui/webui/brave_new_tab_message_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// 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/ui/webui/brave_new_tab_message_handler.h"

#include <string>

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

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

BraveNewTabMessageHandler::~BraveNewTabMessageHandler() {}

void BraveNewTabMessageHandler::OnJavascriptAllowed() {
// Observe relevant preferences
PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
pref_change_registrar_.Init(prefs);
// Stats
pref_change_registrar_.Add(kAdsBlocked,
base::Bind(&BraveNewTabMessageHandler::OnStatsChanged,
base::Unretained(this)));
pref_change_registrar_.Add(kTrackersBlocked,
base::Bind(&BraveNewTabMessageHandler::OnStatsChanged,
base::Unretained(this)));
pref_change_registrar_.Add(kHttpsUpgrades,
base::Bind(&BraveNewTabMessageHandler::OnStatsChanged,
base::Unretained(this)));
// 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,
base::Unretained(this)));
}

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::HandleInitialized(const base::ListValue* args) {
AllowJavascript();
}

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

void BraveNewTabMessageHandler::HandleSaveNewTabPagePref(
const base::ListValue* args) {
if (args->GetSize() != 2) {
LOG(ERROR) << "Invalid input";
return;
}
PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
// Collect args
std::string settingsKeyInput = args->GetList()[0].GetString();
auto settingsValue = args->GetList()[1].Clone();
// Validate args
// Note: if we introduce any non-bool settings values
// then perform this type check within the appropriate key conditionals.
if (!settingsValue.is_bool()) {
LOG(ERROR) << "Invalid value type";
return;
}
const auto settingsValueBool = settingsValue.GetBool();
std::string settingsKey;
if (settingsKeyInput == "showBackgroundImage") {
settingsKey = kNewTabPageShowBackgroundImage;
} else {
LOG(ERROR) << "Invalid setting key";
return;
}
prefs->SetBoolean(settingsKey, settingsValueBool);
}

void BraveNewTabMessageHandler::OnPrivatePropertiesChanged() {
new_tab_web_ui_->OnPrivatePropertiesChanged();
}

void BraveNewTabMessageHandler::OnStatsChanged() {
new_tab_web_ui_->OnStatsChanged();
FireWebUIListener("stats-updated");
}

void BraveNewTabMessageHandler::OnPreferencesChanged() {
new_tab_web_ui_->OnPreferencesChanged();
FireWebUIListener("preferences-changed");
}
42 changes: 42 additions & 0 deletions browser/ui/webui/brave_new_tab_message_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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/.

#ifndef BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_MESSAGE_HANDLER_H_
#define BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_MESSAGE_HANDLER_H_

#include "components/prefs/pref_change_registrar.h"
#include "content/public/browser/web_ui_message_handler.h"

class Profile;
class BraveNewTabUI;

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

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

void HandleInitialized(const base::ListValue* args);
void HandleSaveNewTabPagePref(const base::ListValue* args);
void HandleToggleAlternativeSearchEngineProvider(
const base::ListValue* args);

void OnStatsChanged();
void OnPreferencesChanged();
void OnPrivatePropertiesChanged();

PrefChangeRegistrar pref_change_registrar_;
BraveNewTabUI* new_tab_web_ui_;

DISALLOW_COPY_AND_ASSIGN(BraveNewTabMessageHandler);
};

#endif // BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_MESSAGE_HANDLER_H_
117 changes: 64 additions & 53 deletions browser/ui/webui/brave_new_tab_ui.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/* 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/. */
// 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/ui/webui/brave_new_tab_ui.h"

#include <string>

#include "brave/browser/search_engines/search_engine_provider_util.h"
#include "brave/browser/ui/webui/brave_new_tab_message_handler.h"
#include "brave/common/pref_names.h"
#include "brave/common/webui_url_constants.h"
#include "brave/components/brave_new_tab/resources/grit/brave_new_tab_generated_map.h"
Expand All @@ -15,60 +19,29 @@
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"

namespace {
class NewTabDOMHandler : public content::WebUIMessageHandler {
public:
NewTabDOMHandler() = default;
~NewTabDOMHandler() override = default;

private:
// WebUIMessageHandler implementation.
void RegisterMessages() override {
web_ui()->RegisterMessageCallback(
"toggleAlternativePrivateSearchEngine",
base::BindRepeating(
&NewTabDOMHandler::HandleToggleAlternativeSearchEngineProvider,
base::Unretained(this)));
}

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

DISALLOW_COPY_AND_ASSIGN(NewTabDOMHandler);
};

} // namespace

BraveNewTabUI::BraveNewTabUI(content::WebUI* web_ui, const std::string& name)
: BasicUI(web_ui, name, kBraveNewTabGenerated,
kBraveNewTabGeneratedSize, IDR_BRAVE_NEW_TAB_HTML) {
Profile* profile = Profile::FromWebUI(web_ui);
PrefService* prefs = profile->GetPrefs();
pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
pref_change_registrar_->Init(prefs);
pref_change_registrar_->Add(kAdsBlocked,
base::Bind(&BraveNewTabUI::OnPreferenceChanged, base::Unretained(this)));
pref_change_registrar_->Add(kTrackersBlocked,
base::Bind(&BraveNewTabUI::OnPreferenceChanged, base::Unretained(this)));
pref_change_registrar_->Add(kHttpsUpgrades,
base::Bind(&BraveNewTabUI::OnPreferenceChanged, base::Unretained(this)));
pref_change_registrar_->Add(kUseAlternativeSearchEngineProvider,
base::Bind(&BraveNewTabUI::OnPreferenceChanged, base::Unretained(this)));
pref_change_registrar_->Add(kAlternativeSearchEngineProviderInTor,
base::Bind(&BraveNewTabUI::OnPreferenceChanged, base::Unretained(this)));

web_ui->AddMessageHandler(std::make_unique<NewTabDOMHandler>());
web_ui->AddMessageHandler(std::make_unique<BraveNewTabMessageHandler>(this));
}

BraveNewTabUI::~BraveNewTabUI() {
}

void BraveNewTabUI::CustomizeNewTabWebUIProperties(content::RenderViewHost* render_view_host) {
void BraveNewTabUI::UpdateWebUIProperties() {
if (IsSafeToSetWebUIProperties()) {
// TODO(petemill): move all this data to set on loadTimeData
// on the DataSource via the MessageHandler
auto* render_view_host = GetRenderViewHost();
SetStatsWebUIProperties(render_view_host);
SetPrivateWebUIProperties(render_view_host);
SetPreferencesWebUIProperties(render_view_host);
}
}

void BraveNewTabUI::SetStatsWebUIProperties(
content::RenderViewHost* render_view_host) {
DCHECK(IsSafeToSetWebUIProperties());
Profile* profile = Profile::FromWebUI(web_ui());
PrefService* prefs = profile->GetPrefs();
Expand Down Expand Up @@ -99,13 +72,51 @@ void BraveNewTabUI::CustomizeNewTabWebUIProperties(content::RenderViewHost* rend
}
}

void BraveNewTabUI::UpdateWebUIProperties() {
void BraveNewTabUI::SetPrivateWebUIProperties(
content::RenderViewHost* render_view_host) {
DCHECK(IsSafeToSetWebUIProperties());
Profile* profile = Profile::FromWebUI(web_ui());
PrefService* prefs = profile->GetPrefs();
if (render_view_host) {
render_view_host->SetWebUIProperty(
"useAlternativePrivateSearchEngine",
prefs->GetBoolean(kUseAlternativeSearchEngineProvider) ? "true"
: "false");
render_view_host->SetWebUIProperty(
"isTor", profile->IsTorProfile() ? "true" : "false");
render_view_host->SetWebUIProperty(
"isQwant", brave::IsRegionForQwant(profile) ? "true" : "false");
}
}

void BraveNewTabUI::SetPreferencesWebUIProperties(
content::RenderViewHost* render_view_host) {
DCHECK(IsSafeToSetWebUIProperties());
Profile* profile = Profile::FromWebUI(web_ui());
PrefService* prefs = profile->GetPrefs();
if (render_view_host) {
render_view_host->SetWebUIProperty(
"showBackgroundImage",
prefs->GetBoolean(kNewTabPageShowBackgroundImage) ? "true"
: "false");
}
}


void BraveNewTabUI::OnPreferencesChanged() {
if (IsSafeToSetWebUIProperties()) {
CustomizeNewTabWebUIProperties(GetRenderViewHost());
web_ui()->CallJavascriptFunctionUnsafe("brave_new_tab.statsUpdated");
SetPreferencesWebUIProperties(GetRenderViewHost());
}
}

void BraveNewTabUI::OnPreferenceChanged() {
UpdateWebUIProperties();
void BraveNewTabUI::OnPrivatePropertiesChanged() {
if (IsSafeToSetWebUIProperties()) {
SetPrivateWebUIProperties(GetRenderViewHost());
}
}

void BraveNewTabUI::OnStatsChanged() {
if (IsSafeToSetWebUIProperties()) {
SetStatsWebUIProperties(GetRenderViewHost());
}
}
23 changes: 14 additions & 9 deletions browser/ui/webui/brave_new_tab_ui.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
/* 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/. */
// 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/.

#ifndef BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_UI_H_
#define BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_UI_H_

#include <memory>
#include <string>

#include "brave/browser/ui/webui/basic_ui.h"

class PrefChangeRegistrar;
namespace content {
class RenderViewHost;
}

class BraveNewTabUI : public BasicUI {
public:
BraveNewTabUI(content::WebUI* web_ui, const std::string& host);
~BraveNewTabUI() override;
void OnPreferencesChanged();
void OnPrivatePropertiesChanged();
void OnStatsChanged();

private:
// BasicUI overrides
void UpdateWebUIProperties() override;

void CustomizeNewTabWebUIProperties(content::RenderViewHost* render_view_host);
void OnPreferenceChanged();

std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
void SetPreferencesWebUIProperties(content::RenderViewHost* render_view_host);
void SetStatsWebUIProperties(content::RenderViewHost* render_view_host);
void SetPrivateWebUIProperties(content::RenderViewHost* render_view_host);

DISALLOW_COPY_AND_ASSIGN(BraveNewTabUI);
};
Expand Down
2 changes: 1 addition & 1 deletion browser/ui/webui/brave_new_tab_ui_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ IN_PROC_BROWSER_TEST_F(BraveNewTabUIBrowserTest, BraveNewTabIsDefault) {
bool is_brave_new_tab = false;
ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
contents,
"window.domAutomationController.send(!!window.brave_new_tab)",
"window.domAutomationController.send(!!document.querySelector(`html[data-test-id='brave-new-tab-page']`))",
&is_brave_new_tab));
ASSERT_TRUE(is_brave_new_tab);
}
Expand Down
2 changes: 2 additions & 0 deletions common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ const char kWebTorrentEnabled[] = "brave.webtorrent_enabled";
const char kHangoutsEnabled[] = "brave.hangouts_enabled";
const char kHideBraveRewardsButton[] = "brave.hide_brave_rewards_button";
const char kIPFSCompanionEnabled[] = "brave.ipfs_companion_enabled";
const char kNewTabPageShowBackgroundImage[] =
"brave.new_tab_page.show_background_image";
Loading

0 comments on commit f39ec4f

Please sign in to comment.