From bb0d4f36d14ee97048bf9f01afac194698b3abed Mon Sep 17 00:00:00 2001 From: brave-builds Date: Thu, 4 Feb 2021 23:07:44 +0000 Subject: [PATCH] Uplift of #7811 (squashed) to beta --- browser/ui/webui/basic_ui.cc | 52 +------------------ browser/ui/webui/basic_ui.h | 16 +----- browser/ui/webui/brave_adblock_ui.cc | 41 ++------------- browser/ui/webui/brave_adblock_ui.h | 11 ---- .../actions/adblock_actions.ts | 2 - components/brave_adblock_ui/brave_adblock.tsx | 8 +-- .../brave_adblock_ui/components/app.tsx | 2 - .../components/numBlockedStat.tsx | 15 ------ .../constants/adblock_types.ts | 1 - .../reducers/adblock_reducer.ts | 3 -- components/brave_adblock_ui/storage.ts | 23 +------- components/definitions/adBlock.d.ts | 4 -- .../actions/adblock_actions_test.ts | 16 ------ .../brave_adblock_ui/components/app_test.tsx | 19 +------ .../reducers/adblock_reducer_test.ts | 43 --------------- 15 files changed, 10 insertions(+), 246 deletions(-) delete mode 100644 components/brave_adblock_ui/components/numBlockedStat.tsx delete mode 100644 components/test/brave_adblock_ui/actions/adblock_actions_test.ts delete mode 100644 components/test/brave_adblock_ui/reducers/adblock_reducer_test.ts diff --git a/browser/ui/webui/basic_ui.cc b/browser/ui/webui/basic_ui.cc index 8be506e6d268..4e9d148c9b11 100644 --- a/browser/ui/webui/basic_ui.cc +++ b/browser/ui/webui/basic_ui.cc @@ -7,11 +7,10 @@ #include "brave/browser/ui/webui/brave_webui_source.h" #include "chrome/browser/profiles/profile.h" -#include "content/public/browser/web_contents.h" -#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_ui.h" #include "content/public/browser/web_ui_data_source.h" #include "content/public/common/bindings_policy.h" +#include "services/network/public/mojom/content_security_policy.mojom.h" #include "ui/resources/grit/webui_resources_map.h" content::WebUIDataSource* CreateBasicUIHTMLSource( @@ -50,30 +49,6 @@ content::WebUIDataSource* CreateBasicUIHTMLSource( return source; } -// This is used to know proper timing for setting webui properties. -// So far, content::WebUIController::RenderFrameCreated() is used. -// However, it doesn't get called sometimes when reloading, or called when -// RenderFrameHost is not prepared during renderer process is in changing. -class BasicUI::BasicUIWebContentsObserver - : public content::WebContentsObserver { - public: - BasicUIWebContentsObserver(BasicUI* host, content::WebContents* web_contents) - : WebContentsObserver(web_contents), - host_(host) { - } - ~BasicUIWebContentsObserver() override {} - - // content::WebContentsObserver overrides: - void RenderViewReady() override { - host_->UpdateWebUIProperties(); - } - - private: - BasicUI* host_; - - DISALLOW_COPY_AND_ASSIGN(BasicUIWebContentsObserver); -}; - BasicUI::BasicUI(content::WebUI* web_ui, const std::string& name, const GritResourceMap* resource_map, @@ -81,8 +56,6 @@ BasicUI::BasicUI(content::WebUI* web_ui, int html_resource_id, bool disable_trusted_types_csp) : WebUIController(web_ui) { - observer_.reset( - new BasicUIWebContentsObserver(this, web_ui->GetWebContents())); Profile* profile = Profile::FromWebUI(web_ui); content::WebUIDataSource* source = CreateBasicUIHTMLSource(profile, name, resource_map, resource_map_size, html_resource_id, @@ -90,25 +63,4 @@ BasicUI::BasicUI(content::WebUI* web_ui, content::WebUIDataSource::Add(profile, source); } -BasicUI::~BasicUI() { -} - -content::RenderFrameHost* BasicUI::GetRenderFrameHost() { - auto* web_contents = web_ui()->GetWebContents(); - if (web_contents) { - return web_contents->GetMainFrame(); - } - return nullptr; -} - -bool BasicUI::IsSafeToSetWebUIProperties() const { - // Allow `web_ui()->CanCallJavascript()` to be false. - // Allow `web_ui()->CanCallJavascript()` to be true if - // `(web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI) != 0` - // Disallow `web_ui()->CanCallJavascript()` to be true if - // `(web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI) == 0` - DCHECK(!web_ui()->CanCallJavascript() || - (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)); - return web_ui()->CanCallJavascript() && - (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI); -} +BasicUI::~BasicUI() = default; diff --git a/browser/ui/webui/basic_ui.h b/browser/ui/webui/basic_ui.h index 7a75a443ba79..bdc614464b68 100644 --- a/browser/ui/webui/basic_ui.h +++ b/browser/ui/webui/basic_ui.h @@ -6,8 +6,6 @@ #ifndef BRAVE_BROWSER_UI_WEBUI_BASIC_UI_H_ #define BRAVE_BROWSER_UI_WEBUI_BASIC_UI_H_ -#include - #include #include "base/compiler_specific.h" @@ -15,10 +13,9 @@ #include "content/public/browser/web_ui_controller.h" namespace content { -class RenderFrameHost; class WebUIDataSource; class WebUI; -} +} // namespace content class Profile; @@ -42,18 +39,7 @@ class BasicUI : public content::WebUIController { bool disable_trusted_types_csp = false); ~BasicUI() override; - // Called when subclass can set its webui properties. - virtual void UpdateWebUIProperties() {} - - protected: - bool IsSafeToSetWebUIProperties() const; - content::RenderFrameHost* GetRenderFrameHost(); - private: - class BasicUIWebContentsObserver; - - std::unique_ptr observer_; - DISALLOW_COPY_AND_ASSIGN(BasicUI); }; diff --git a/browser/ui/webui/brave_adblock_ui.cc b/browser/ui/webui/brave_adblock_ui.cc index d4526f12d901..2a4b8701a807 100644 --- a/browser/ui/webui/brave_adblock_ui.cc +++ b/browser/ui/webui/brave_adblock_ui.cc @@ -5,19 +5,14 @@ #include "brave/browser/ui/webui/brave_adblock_ui.h" +#include + #include "brave/browser/brave_browser_process_impl.h" -#include "brave/common/pref_names.h" #include "brave/common/webui_url_constants.h" #include "brave/components/brave_adblock/resources/grit/brave_adblock_generated_map.h" #include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h" #include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" -#include "chrome/browser/profiles/profile.h" #include "components/grit/brave_components_resources.h" -#include "components/prefs/pref_change_registrar.h" -#include "components/prefs/pref_service.h" -#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 { @@ -111,37 +106,7 @@ void AdblockDOMHandler::HandleUpdateCustomFilters(const base::ListValue* args) { BraveAdblockUI::BraveAdblockUI(content::WebUI* web_ui, const std::string& name) : BasicUI(web_ui, name, kBraveAdblockGenerated, kBraveAdblockGeneratedSize, IDR_BRAVE_ADBLOCK_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(&BraveAdblockUI::OnPreferenceChanged, base::Unretained(this))); web_ui->AddMessageHandler(std::make_unique()); } -BraveAdblockUI::~BraveAdblockUI() { -} - -void BraveAdblockUI::CustomizeWebUIProperties( - content::RenderFrameHost* render_frame_host) { - DCHECK(IsSafeToSetWebUIProperties()); - Profile* profile = Profile::FromWebUI(web_ui()); - PrefService* prefs = profile->GetPrefs(); - if (render_frame_host) { - render_frame_host->SetWebUIProperty( - "adsBlockedStat", std::to_string(prefs->GetUint64(kAdsBlocked) + - prefs->GetUint64(kTrackersBlocked))); - } -} - -void BraveAdblockUI::UpdateWebUIProperties() { - if (IsSafeToSetWebUIProperties()) { - CustomizeWebUIProperties(GetRenderFrameHost()); - web_ui()->CallJavascriptFunctionUnsafe("brave_adblock.statsUpdated"); - } -} - -void BraveAdblockUI::OnPreferenceChanged() { - UpdateWebUIProperties(); -} +BraveAdblockUI::~BraveAdblockUI() = default; diff --git a/browser/ui/webui/brave_adblock_ui.h b/browser/ui/webui/brave_adblock_ui.h index f452ec624783..9e1a3bebfc66 100644 --- a/browser/ui/webui/brave_adblock_ui.h +++ b/browser/ui/webui/brave_adblock_ui.h @@ -6,27 +6,16 @@ #ifndef BRAVE_BROWSER_UI_WEBUI_BRAVE_ADBLOCK_UI_H_ #define BRAVE_BROWSER_UI_WEBUI_BRAVE_ADBLOCK_UI_H_ -#include #include #include "brave/browser/ui/webui/basic_ui.h" -class PrefChangeRegistrar; - class BraveAdblockUI : public BasicUI { public: BraveAdblockUI(content::WebUI* web_ui, const std::string& host); ~BraveAdblockUI() override; private: - // BasicUI overrides: - void UpdateWebUIProperties() override; - - void CustomizeWebUIProperties(content::RenderFrameHost* render_frame_host); - void OnPreferenceChanged(); - - std::unique_ptr pref_change_registrar_; - DISALLOW_COPY_AND_ASSIGN(BraveAdblockUI); }; diff --git a/components/brave_adblock_ui/actions/adblock_actions.ts b/components/brave_adblock_ui/actions/adblock_actions.ts index 45abe7a1b7a4..40749253a1fb 100644 --- a/components/brave_adblock_ui/actions/adblock_actions.ts +++ b/components/brave_adblock_ui/actions/adblock_actions.ts @@ -27,8 +27,6 @@ export const onGetRegionalLists = (regionalLists: AdBlock.FilterList[]) => regionalLists }) -export const statsUpdated = () => action(types.ADBLOCK_STATS_UPDATED) - export const updateCustomFilters = (customFilters: string) => action(types.ADBLOCK_UPDATE_CUSTOM_FILTERS, { customFilters diff --git a/components/brave_adblock_ui/brave_adblock.tsx b/components/brave_adblock_ui/brave_adblock.tsx index 230b1bae7524..9b10ca95c663 100644 --- a/components/brave_adblock_ui/brave_adblock.tsx +++ b/components/brave_adblock_ui/brave_adblock.tsx @@ -48,16 +48,10 @@ window.cr.define('brave_adblock', function () { actions.onGetRegionalLists(regionalLists) } - function statsUpdated () { - const actions = bindActionCreators(adblockActions, store.dispatch.bind(store)) - actions.statsUpdated() - } - return { initialize, onGetCustomFilters, - onGetRegionalLists, - statsUpdated + onGetRegionalLists } }) diff --git a/components/brave_adblock_ui/components/app.tsx b/components/brave_adblock_ui/components/app.tsx index f4bdc84827de..1186cc31228a 100644 --- a/components/brave_adblock_ui/components/app.tsx +++ b/components/brave_adblock_ui/components/app.tsx @@ -9,7 +9,6 @@ import { connect } from 'react-redux' // Components import { AdBlockItemList } from './adBlockItemList' import { CustomFilters } from './customFilters' -import { NumBlockedStat } from './numBlockedStat' // Utils import * as adblockActions from '../actions/adblock_actions' @@ -32,7 +31,6 @@ export class AdblockPage extends React.Component { const { actions, adblockData } = this.props return (
- ( -
- {props.adsBlockedStat || 0} -
-) diff --git a/components/brave_adblock_ui/constants/adblock_types.ts b/components/brave_adblock_ui/constants/adblock_types.ts index 2a146be141f5..3e0749ace348 100644 --- a/components/brave_adblock_ui/constants/adblock_types.ts +++ b/components/brave_adblock_ui/constants/adblock_types.ts @@ -8,6 +8,5 @@ export const enum types { ADBLOCK_GET_REGIONAL_LISTS = '@@adblock/ADBLOCK_GET_REGIONAL_LISTS', ADBLOCK_ON_GET_CUSTOM_FILTERS = '@@adblock/ADBLOCK_ON_GET_CUSTOM_FILTERS', ADBLOCK_ON_GET_REGIONAL_LISTS = '@@adblock/ADBLOCK_ON_GET_REGIONAL_LISTS', - ADBLOCK_STATS_UPDATED = '@@adblock/ADBLOCK_STATS_UPDATED', ADBLOCK_UPDATE_CUSTOM_FILTERS = '@@adblock/ADBLOCK_UPDATE_CUSTOM_FILTERS' } diff --git a/components/brave_adblock_ui/reducers/adblock_reducer.ts b/components/brave_adblock_ui/reducers/adblock_reducer.ts index 2a14d39bd8d8..7ecbc2d23551 100644 --- a/components/brave_adblock_ui/reducers/adblock_reducer.ts +++ b/components/brave_adblock_ui/reducers/adblock_reducer.ts @@ -46,9 +46,6 @@ const adblockReducer: Reducer = (state: AdBlock.State case types.ADBLOCK_ON_GET_REGIONAL_LISTS: state = { ...state, settings: { ...state.settings, regionalLists: action.payload.regionalLists } } break - case types.ADBLOCK_STATS_UPDATED: - state = storage.getLoadTimeData(state) - break case types.ADBLOCK_UPDATE_CUSTOM_FILTERS: state = { ...state, settings: { ...state.settings, customFilters: action.payload.customFilters } } updateCustomFilters(state.settings.customFilters) diff --git a/components/brave_adblock_ui/storage.ts b/components/brave_adblock_ui/storage.ts index 186130f8d821..6772b01d0a7a 100644 --- a/components/brave_adblock_ui/storage.ts +++ b/components/brave_adblock_ui/storage.ts @@ -10,28 +10,9 @@ export const defaultState: AdBlock.State = { settings: { customFilters: '', regionalLists: [] - }, - stats: { - numBlocked: 0 } } -export const getLoadTimeData = (state: AdBlock.State): AdBlock.State => { - state = { ...state } - state.stats = defaultState.stats - - // Expected to be numbers - ;['adsBlockedStat'].forEach((stat) => { - state.stats[stat] = parseInt(chrome.getVariableValue(stat), 10) - }) - - return state -} - -export const cleanData = (state: AdBlock.State): AdBlock.State => { - return getLoadTimeData(state) -} - export const load = (): AdBlock.State => { const data = window.localStorage.getItem(keyName) let state: AdBlock.State = defaultState @@ -42,11 +23,11 @@ export const load = (): AdBlock.State => { console.error('Could not parse local storage data: ', e) } } - return cleanData(state) + return state } export const debouncedSave = debounce((data: AdBlock.State) => { if (data) { - window.localStorage.setItem(keyName, JSON.stringify(cleanData(data))) + window.localStorage.setItem(keyName, JSON.stringify(data)) } }, 50) diff --git a/components/definitions/adBlock.d.ts b/components/definitions/adBlock.d.ts index a2b421918266..ba41f583d6e6 100644 --- a/components/definitions/adBlock.d.ts +++ b/components/definitions/adBlock.d.ts @@ -7,10 +7,6 @@ declare namespace AdBlock { settings: { customFilters: string regionalLists: FilterList[] - }, - stats: { - adsBlockedStat?: number - numBlocked: number } } diff --git a/components/test/brave_adblock_ui/actions/adblock_actions_test.ts b/components/test/brave_adblock_ui/actions/adblock_actions_test.ts deleted file mode 100644 index bc7d7f8139d6..000000000000 --- a/components/test/brave_adblock_ui/actions/adblock_actions_test.ts +++ /dev/null @@ -1,16 +0,0 @@ -/* 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 { types } from '../../../brave_adblock_ui/constants/adblock_types' -import * as actions from '../../../brave_adblock_ui/actions/adblock_actions' - -describe('adblock_actions', () => { - it('statsUpdated', () => { - expect(actions.statsUpdated()).toEqual({ - type: types.ADBLOCK_STATS_UPDATED, - meta: undefined, - payload: undefined - }) - }) -}) diff --git a/components/test/brave_adblock_ui/components/app_test.tsx b/components/test/brave_adblock_ui/components/app_test.tsx index 1de2b9456b8a..68bb72369bb8 100644 --- a/components/test/brave_adblock_ui/components/app_test.tsx +++ b/components/test/brave_adblock_ui/components/app_test.tsx @@ -1,11 +1,9 @@ import * as React from 'react' import { shallow } from 'enzyme' -import { types } from '../../../brave_adblock_ui/constants/adblock_types' import { adblockInitialState } from '../../testData' import { AdblockPage, - mapStateToProps, - mapDispatchToProps + mapStateToProps } from '../../../brave_adblock_ui/components/app' describe('adblockPage component', () => { @@ -15,21 +13,6 @@ describe('adblockPage component', () => { }) }) - describe('mapDispatchToProps', () => { - it('should fire statsUpdated', () => { - const dispatch = jest.fn() - - // For the `mapDispatchToProps`, call it directly but pass in - // a mock function and check the arguments passed in are as expected - mapDispatchToProps(dispatch).actions.statsUpdated() - expect(dispatch.mock.calls[0][0]).toEqual({ - type: types.ADBLOCK_STATS_UPDATED, - meta: undefined, - payload: undefined - }) - }) - }) - describe('adblockPage dumb component', () => { it('renders the component', () => { const wrapper = shallow( diff --git a/components/test/brave_adblock_ui/reducers/adblock_reducer_test.ts b/components/test/brave_adblock_ui/reducers/adblock_reducer_test.ts deleted file mode 100644 index 35259ca17d06..000000000000 --- a/components/test/brave_adblock_ui/reducers/adblock_reducer_test.ts +++ /dev/null @@ -1,43 +0,0 @@ -/* 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/. */ -/* global chrome */ - -import adblockReducer from '../../../brave_adblock_ui/reducers/adblock_reducer' -import * as actions from '../../../brave_adblock_ui/actions/adblock_actions' -import { types } from '../../../brave_adblock_ui/constants/adblock_types' - -describe('adblockReducer', () => { - it('should handle initial state', () => { - const assertion = adblockReducer(undefined, actions.statsUpdated()) - expect(assertion).toEqual({ - settings: { - customFilters: '', - regionalLists: [] - }, - stats: { - adsBlockedStat: NaN, - numBlocked: 0 - } - }) - }) - - describe('ADBLOCK_STATS_UPDATED', () => { - it('stats updated', () => { - const assertion = adblockReducer(undefined, { - type: types.ADBLOCK_STATS_UPDATED, - payload: undefined - }) - expect(assertion).toEqual({ - settings: { - customFilters: '', - regionalLists: [] - }, - stats: { - adsBlockedStat: NaN, - numBlocked: 0 - } - }) - }) - }) -})