diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index 59cf80afdda2..39855e8b3260 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -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 diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index a95601a1d79b..321a04833b5e 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -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", diff --git a/browser/ui/webui/brave_new_tab_message_handler.cc b/browser/ui/webui/brave_new_tab_message_handler.cc new file mode 100644 index 000000000000..ed78f15e8748 --- /dev/null +++ b/browser/ui/webui/brave_new_tab_message_handler.cc @@ -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 + +#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"); +} diff --git a/browser/ui/webui/brave_new_tab_message_handler.h b/browser/ui/webui/brave_new_tab_message_handler.h new file mode 100644 index 000000000000..b47f8a023c3a --- /dev/null +++ b/browser/ui/webui/brave_new_tab_message_handler.h @@ -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_ diff --git a/browser/ui/webui/brave_new_tab_ui.cc b/browser/ui/webui/brave_new_tab_ui.cc index 85420f165261..f16b6d1a8d1b 100644 --- a/browser/ui/webui/brave_new_tab_ui.cc +++ b/browser/ui/webui/brave_new_tab_ui.cc @@ -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 + #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" @@ -15,60 +19,27 @@ #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(); - 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()); + web_ui->AddMessageHandler(std::make_unique(this)); } BraveNewTabUI::~BraveNewTabUI() { } -void BraveNewTabUI::CustomizeNewTabWebUIProperties(content::RenderViewHost* render_view_host) { +void BraveNewTabUI::UpdateWebUIProperties() { + // 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(); @@ -99,13 +70,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()); + } } diff --git a/browser/ui/webui/brave_new_tab_ui.h b/browser/ui/webui/brave_new_tab_ui.h index 14778a722880..37e849d02008 100644 --- a/browser/ui/webui/brave_new_tab_ui.h +++ b/browser/ui/webui/brave_new_tab_ui.h @@ -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 +#include #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 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); }; diff --git a/browser/ui/webui/brave_new_tab_ui_browsertest.cc b/browser/ui/webui/brave_new_tab_ui_browsertest.cc index 72572f807e21..d627f1430110 100644 --- a/browser/ui/webui/brave_new_tab_ui_browsertest.cc +++ b/browser/ui/webui/brave_new_tab_ui_browsertest.cc @@ -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); } diff --git a/common/pref_names.cc b/common/pref_names.cc index 09ddf949cb68..3a12d7b7d011 100644 --- a/common/pref_names.cc +++ b/common/pref_names.cc @@ -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"; diff --git a/common/pref_names.h b/common/pref_names.h index b42d737430ba..a7513eb4abb9 100644 --- a/common/pref_names.h +++ b/common/pref_names.h @@ -46,5 +46,6 @@ extern const char kWebTorrentEnabled[]; extern const char kHangoutsEnabled[]; extern const char kHideBraveRewardsButton[]; extern const char kIPFSCompanionEnabled[]; +extern const char kNewTabPageShowBackgroundImage[]; #endif // BRAVE_COMMON_PREF_NAMES_H_ diff --git a/components/brave_new_tab_ui/actions/new_tab_actions.ts b/components/brave_new_tab_ui/actions/new_tab_actions.ts index 22720b0fd209..a2a84a3224b4 100644 --- a/components/brave_new_tab_ui/actions/new_tab_actions.ts +++ b/components/brave_new_tab_ui/actions/new_tab_actions.ts @@ -6,6 +6,7 @@ import { action } from 'typesafe-actions' // Constants import { types } from '../constants/new_tab_types' +import { Preferences } from '../api/preferences' export const topSitesDataUpdated = (topSites: NewTab.Site[]) => action(types.NEW_TAB_TOP_SITES_DATA_UPDATED, { topSites @@ -71,4 +72,6 @@ export const showSettingsMenu = () => action(types.NEW_TAB_SHOW_SETTINGS_MENU) export const closeSettingsMenu = () => action(types.NEW_TAB_CLOSE_SETTINGS_MENU) -export const toggleShowBackgroundImage = () => action(types.NEW_TAB_TOGGLE_SHOW_BACKGROUND_IMAGE) +export const preferencesUpdated = (preferences: Preferences) => action(types.NEW_TAB_PREFERENCES_UPDATED, { + preferences +}) diff --git a/components/brave_new_tab_ui/api/dataFetch.ts b/components/brave_new_tab_ui/api/dataFetch.ts index 886169d16a7f..325139533064 100644 --- a/components/brave_new_tab_ui/api/dataFetch.ts +++ b/components/brave_new_tab_ui/api/dataFetch.ts @@ -9,7 +9,7 @@ import store from '../store' /** * Get actions from the C++ back-end down to front-end components */ -let actions: any = null +let actions: typeof newTabActions export const getActions = () => { if (actions) { return actions diff --git a/components/brave_new_tab_ui/api/preferences.ts b/components/brave_new_tab_ui/api/preferences.ts new file mode 100644 index 000000000000..f6eab6abcf17 --- /dev/null +++ b/components/brave_new_tab_ui/api/preferences.ts @@ -0,0 +1,41 @@ +// 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/. + +// +// Manages get and set of NTP preference data +// Ensures everything to do with communication +// with the WebUI backend is all in 1 place, +// especially string keys. +// + +export type Preferences = { + showBackgroundImage: boolean +} + +function getWebUIBooleanVal (key: string): boolean { + return (chrome.getVariableValue(key).toLowerCase() === 'true') +} + +export function getPreferences (): Promise { + // Note(petemill): Returning as promise allows this + // to be async even though it isn't right now. + // Enforces practice of not setting directly + // in a redux reducer. + return Promise.resolve({ + showBackgroundImage: getWebUIBooleanVal('showBackgroundImage') + }) +} + +function sendSavePref (key: string, value: any) { + chrome.send('saveNewTabPagePref', [key, value]) +} + +export function saveShowBackgroundImage (value: boolean): void { + sendSavePref('showBackgroundImage', value) +} + +export function addChangeListener (listener: () => void): void { + window.cr.addWebUIListener('preferences-changed', listener) +} diff --git a/components/brave_new_tab_ui/api/topSites/bookmarks.ts b/components/brave_new_tab_ui/api/topSites/bookmarks.ts index 35c986f40b49..db5d9b77feb0 100644 --- a/components/brave_new_tab_ui/api/topSites/bookmarks.ts +++ b/components/brave_new_tab_ui/api/topSites/bookmarks.ts @@ -9,7 +9,7 @@ import { getActions } from '../dataFetch' */ export const fetchBookmarkInfo = (url: string) => { chrome.bookmarks.search(url.replace(/^https?:\/\//, ''), - (bookmarkTreeNodes) => getActions().bookmarkInfoAvailable(url, bookmarkTreeNodes[0]) + (bookmarkTreeNodes) => getActions().bookmarkInfoAvailable(url, bookmarkTreeNodes[0] as NewTab.Bookmark) ) } diff --git a/components/brave_new_tab_ui/brave_new_tab.html b/components/brave_new_tab_ui/brave_new_tab.html index fd8a4132705d..06ed758177bb 100644 --- a/components/brave_new_tab_ui/brave_new_tab.html +++ b/components/brave_new_tab_ui/brave_new_tab.html @@ -1,5 +1,5 @@  - + diff --git a/components/brave_new_tab_ui/brave_new_tab.tsx b/components/brave_new_tab_ui/brave_new_tab.tsx index c24439a5d376..708d5fdf7b4b 100644 --- a/components/brave_new_tab_ui/brave_new_tab.tsx +++ b/components/brave_new_tab_ui/brave_new_tab.tsx @@ -1,14 +1,15 @@ -/* 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/. import * as React from 'react' import { render } from 'react-dom' import { Provider } from 'react-redux' -import * as dataFetchAPI from './api/dataFetch' - import Theme from 'brave-ui/theme/brave-default' import { ThemeProvider } from 'brave-ui/theme' +import * as dataFetchAPI from './api/dataFetch' +import * as preferencesAPI from './api/preferences' // Components import App from './components/app' @@ -21,29 +22,35 @@ import 'emptykit.css' import '../fonts/poppins.css' import '../fonts/muli.css' -window.cr.define('brave_new_tab', function () { - 'use strict' - - function initialize () { - render( - - - - - , - document.getElementById('root')) - window.i18nTemplate.process(window.document, window.loadTimeData) - } - - function statsUpdated () { - const actions = dataFetchAPI.getActions() - actions.statsUpdated() - } - - return { - initialize, - statsUpdated - } -}) - -document.addEventListener('DOMContentLoaded', window.brave_new_tab.initialize) +async function initialize () { + render( + + + + + , + document.getElementById('root') + ) + window.i18nTemplate.process(window.document, window.loadTimeData) + handleAPIEvents() + await updatePreferences() +} + +async function updatePreferences () { + const preferences = await preferencesAPI.getPreferences() + const actions = dataFetchAPI.getActions() + actions.preferencesUpdated(preferences) +} + +function updateStats () { + const actions = dataFetchAPI.getActions() + actions.statsUpdated() +} + +function handleAPIEvents () { + chrome.send('newTabPageInitialized', []) + window.cr.addWebUIListener('stats-updated', updateStats) + preferencesAPI.addChangeListener(updatePreferences) +} + +document.addEventListener('DOMContentLoaded', initialize) diff --git a/components/brave_new_tab_ui/components/app.tsx b/components/brave_new_tab_ui/components/app.tsx index 0f8cac0a82c6..22ad13fed00b 100644 --- a/components/brave_new_tab_ui/components/app.tsx +++ b/components/brave_new_tab_ui/components/app.tsx @@ -1,6 +1,7 @@ -/* 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/. import * as React from 'react' import { bindActionCreators, Dispatch } from 'redux' @@ -12,6 +13,7 @@ import NewTabPage from './newTab' // Utils import * as newTabActions from '../actions/new_tab_actions' +import * as PreferencesAPI from '../api/preferences' interface Props { actions: any @@ -29,7 +31,13 @@ class DefaultPage extends React.Component { return this.props.newTabData.isIncognito ? - : + : ( + + ) } } diff --git a/components/brave_new_tab_ui/components/newTab/index.tsx b/components/brave_new_tab_ui/components/newTab/index.tsx index 245d583470dd..a504a0444dfd 100644 --- a/components/brave_new_tab_ui/components/newTab/index.tsx +++ b/components/brave_new_tab_ui/components/newTab/index.tsx @@ -24,8 +24,9 @@ import FooterInfo from './footerInfo' import SiteRemovalNotification from './notification' interface Props { - actions: any newTabData: NewTab.State + actions: any + saveShowBackgroundImage: (value: boolean) => void } class NewTabPage extends React.Component { @@ -63,7 +64,9 @@ class NewTabPage extends React.Component { } toggleShowBackgroundImage = () => { - this.props.actions.toggleShowBackgroundImage() + this.props.saveShowBackgroundImage( + !this.props.newTabData.showBackgroundImage + ) } showSettings = () => { diff --git a/components/brave_new_tab_ui/constants/new_tab_types.ts b/components/brave_new_tab_ui/constants/new_tab_types.ts index 4bc9788ed2c5..62958751a90f 100644 --- a/components/brave_new_tab_ui/constants/new_tab_types.ts +++ b/components/brave_new_tab_ui/constants/new_tab_types.ts @@ -20,5 +20,5 @@ export const enum types { NEW_TAB_USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE = '@@newtab/NEW_TAB_USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE', NEW_TAB_SHOW_SETTINGS_MENU = '@@newtab/NEW_TAB_SHOW_SETTINGS_MENU', NEW_TAB_CLOSE_SETTINGS_MENU = '@@newtab/NEW_TAB_CLOSE_SETTINGS_MENU', - NEW_TAB_TOGGLE_SHOW_BACKGROUND_IMAGE = '@@newtab/NEW_TAB_TOGGLE_SHOW_BACKGROUND_IMAGE' + NEW_TAB_PREFERENCES_UPDATED = '@@newtab/NEW_TAB_PREFERENCES_UPDATED' } diff --git a/components/brave_new_tab_ui/reducers/new_tab_reducer.tsx b/components/brave_new_tab_ui/reducers/new_tab_reducer.tsx index 1e2ebd015479..7a50ed211f4f 100644 --- a/components/brave_new_tab_ui/reducers/new_tab_reducer.tsx +++ b/components/brave_new_tab_ui/reducers/new_tab_reducer.tsx @@ -6,6 +6,7 @@ import { Reducer } from 'redux' // Constants import { types } from '../constants/new_tab_types' +import { Preferences } from '../api/preferences' // API import * as gridAPI from '../api/topSites/grid' @@ -34,10 +35,6 @@ export const newTabReducer: Reducer = (state: NewTab.S state = { ...state, showSettings: false } break - case types.NEW_TAB_TOGGLE_SHOW_BACKGROUND_IMAGE: - state = { ...state, showBackgroundImage: !state.showBackgroundImage } - break - case types.BOOKMARK_ADDED: const topSite: NewTab.Site | undefined = state.topSites.find((site) => site.url === payload.url) if (topSite) { @@ -157,6 +154,14 @@ export const newTabReducer: Reducer = (state: NewTab.S state = { ...state, useAlternativePrivateSearchEngine: payload.shouldUse } break + case types.NEW_TAB_PREFERENCES_UPDATED: + const preferences: Preferences = payload.preferences + state = { + ...state, + ...preferences + } + break + default: break } diff --git a/components/brave_new_tab_ui/storage.ts b/components/brave_new_tab_ui/storage.ts index e2c0b3792471..49c9c7d05977 100644 --- a/components/brave_new_tab_ui/storage.ts +++ b/components/brave_new_tab_ui/storage.ts @@ -10,7 +10,7 @@ import { debounce } from '../common/debounce' const keyName = 'new-tab-data' const defaultState: NewTab.State = { - showBackgroundImage: true, + showBackgroundImage: false, showSettings: false, topSites: [], ignoredTopSites: [], diff --git a/components/definitions/global.d.ts b/components/definitions/global.d.ts index 273946ec8db3..6fefe4cd578f 100644 --- a/components/definitions/global.d.ts +++ b/components/definitions/global.d.ts @@ -1,6 +1,8 @@ -/* 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/. + export {} type loadTimeData = { @@ -14,6 +16,7 @@ declare global { cr: { define: (name: string, init: () => void) => void sendWithPromise: (method: string, ...args: any[]) => any + addWebUIListener: (eventName: string, callback: (...args: any[]) => void) => void } i18nTemplate: { process: (document: Document, translations: loadTimeData) => void diff --git a/components/test/brave_new_tab_ui/actions/new_tab_actions_test.ts b/components/test/brave_new_tab_ui/actions/new_tab_actions_test.ts index 3321ea5337f0..f7dd302569ab 100644 --- a/components/test/brave_new_tab_ui/actions/new_tab_actions_test.ts +++ b/components/test/brave_new_tab_ui/actions/new_tab_actions_test.ts @@ -154,9 +154,4 @@ describe('newTabActions', () => { type: types.NEW_TAB_SHOW_SETTINGS_MENU }) }) - it('toggleShowBackgroundImage', () => { - expect(actions.toggleShowBackgroundImage()).toEqual({ - type: types.NEW_TAB_TOGGLE_SHOW_BACKGROUND_IMAGE - }) - }) }) diff --git a/components/test/brave_new_tab_ui/reducers/new_tab_reducer_test.ts b/components/test/brave_new_tab_ui/reducers/new_tab_reducer_test.ts index 3619603ef248..36c4323651dc 100644 --- a/components/test/brave_new_tab_ui/reducers/new_tab_reducer_test.ts +++ b/components/test/brave_new_tab_ui/reducers/new_tab_reducer_test.ts @@ -352,15 +352,4 @@ describe('newTabReducer', () => { expect(assertion).toEqual(expected) }) }) - - describe('NEW_TAB_TOGGLE_SHOW_BACKGROUND_IMAGE', () => { - it('should toggle showBackgroundimage status to be true', () => { - const mockState = { ...fakeState, showBackgroundImage: false } - const expected = { ...mockState, showBackgroundImage: true } - const assertion = newTabReducer(mockState, { - type: types.NEW_TAB_TOGGLE_SHOW_BACKGROUND_IMAGE - }) - expect(assertion).toEqual(expected) - }) - }) })