From d833add76af1fc178b68ef71cc7b733b2f1ece38 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Wed, 3 Jun 2020 10:48:01 +0200 Subject: [PATCH] Adds download option and smaller log --- .../ui/webui/brave_rewards_internals_ui.cc | 52 +++++++++++++++---- browser/ui/webui/brave_webui_source.cc | 2 + .../browser/ads_service_impl_unittest.cc | 2 +- .../actions/rewards_internals_actions.ts | 15 ++++-- .../internals/brave_rewards_internals.tsx | 11 ++-- .../resources/internals/components/app.tsx | 37 ++++++++----- .../resources/internals/components/log.tsx | 44 ++++++++++++++-- .../constants/rewards_internals_types.ts | 8 +-- .../reducers/rewards_internals_reducer.ts | 17 ++++-- .../resources/internals/storage.ts | 3 +- .../resources/internals/style.ts | 5 ++ components/definitions/rewardsInternals.d.ts | 1 + .../resources/brave_components_strings.grd | 2 + 13 files changed, 156 insertions(+), 43 deletions(-) diff --git a/browser/ui/webui/brave_rewards_internals_ui.cc b/browser/ui/webui/brave_rewards_internals_ui.cc index eff8070713bf..514da9943569 100644 --- a/browser/ui/webui/brave_rewards_internals_ui.cc +++ b/browser/ui/webui/brave_rewards_internals_ui.cc @@ -29,6 +29,8 @@ namespace { +const int g_partial_log_max_lines = 5000; + class RewardsInternalsDOMHandler : public content::WebUIMessageHandler { public: RewardsInternalsDOMHandler(); @@ -52,8 +54,10 @@ class RewardsInternalsDOMHandler : public content::WebUIMessageHandler { std::unique_ptr balance); void GetPromotions(const base::ListValue* args); void OnGetPromotions(const std::vector& list); - void GetLog(const base::ListValue* args); - void OnGetLog(const std::string& log); + void GetPartialLog(const base::ListValue* args); + void OnGetPartialLog(const std::string& log); + void GetFulllLog(const base::ListValue* args); + void OnGetFulllLog(const std::string& log); void ClearLog(const base::ListValue* args); void OnClearLog(const bool success); @@ -91,9 +95,14 @@ void RewardsInternalsDOMHandler::RegisterMessages() { &RewardsInternalsDOMHandler::GetPromotions, base::Unretained(this))); web_ui()->RegisterMessageCallback( - "brave_rewards_internals.getLog", + "brave_rewards_internals.getPartialLog", base::BindRepeating( - &RewardsInternalsDOMHandler::GetLog, + &RewardsInternalsDOMHandler::GetPartialLog, + base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "brave_rewards_internals.getFullLog", + base::BindRepeating( + &RewardsInternalsDOMHandler::GetFulllLog, base::Unretained(this))); web_ui()->RegisterMessageCallback( "brave_rewards_internals.clearLog", @@ -238,24 +247,47 @@ void RewardsInternalsDOMHandler::OnGetPromotions( std::move(promotions)); } -void RewardsInternalsDOMHandler::GetLog(const base::ListValue *args) { +void RewardsInternalsDOMHandler::GetPartialLog(const base::ListValue *args) { + if (!rewards_service_) { + return; + } + + rewards_service_->LoadDiagnosticLog( + g_partial_log_max_lines, + base::BindOnce( + &RewardsInternalsDOMHandler::OnGetPartialLog, + weak_ptr_factory_.GetWeakPtr())); +} + +void RewardsInternalsDOMHandler::OnGetPartialLog(const std::string& log) { + if (!web_ui()->CanCallJavascript()) { + return; + } + + web_ui()->CallJavascriptFunctionUnsafe( + "brave_rewards_internals.partialLog", + base::Value(log)); +} + +void RewardsInternalsDOMHandler::GetFulllLog(const base::ListValue *args) { if (!rewards_service_) { return; } - rewards_service_->LoadDiagnosticLog(-1, + rewards_service_->LoadDiagnosticLog( + -1, base::BindOnce( - &RewardsInternalsDOMHandler::OnGetLog, + &RewardsInternalsDOMHandler::OnGetFulllLog, weak_ptr_factory_.GetWeakPtr())); } -void RewardsInternalsDOMHandler::OnGetLog(const std::string& log) { +void RewardsInternalsDOMHandler::OnGetFulllLog(const std::string& log) { if (!web_ui()->CanCallJavascript()) { return; } web_ui()->CallJavascriptFunctionUnsafe( - "brave_rewards_internals.log", + "brave_rewards_internals.fullLog", base::Value(log)); } @@ -280,7 +312,7 @@ void RewardsInternalsDOMHandler::OnClearLog(const bool success) { } web_ui()->CallJavascriptFunctionUnsafe( - "brave_rewards_internals.log", + "brave_rewards_internals.partialLog", base::Value("")); } diff --git a/browser/ui/webui/brave_webui_source.cc b/browser/ui/webui/brave_webui_source.cc index 938cca2f8d1d..9c402251c1fe 100644 --- a/browser/ui/webui/brave_webui_source.cc +++ b/browser/ui/webui/brave_webui_source.cc @@ -835,8 +835,10 @@ void CustomizeWebUIHTMLSource(const std::string &name, { "clearButton", IDS_BRAVE_REWARDS_INTERNALS_CLEAR_BUTTON }, { "contributionsInProgress", IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTIONS_IN_PROGRESS }, // NOLINT { "currentReconcile", IDS_BRAVE_REWARDS_INTERNALS_CURRENT_RECONCILE }, + { "downloadButton", IDS_BRAVE_REWARDS_INTERNALS_DOWNLOAD_BUTTON }, { "invalid", IDS_BRAVE_REWARDS_INTERNALS_INVALID }, { "keyInfoSeed", IDS_BRAVE_REWARDS_INTERNALS_KEY_INFO_SEED }, + { "logNotice", IDS_BRAVE_REWARDS_INTERNALS_LOG_NOTICE }, { "mainTitle", IDS_BRAVE_REWARDS_INTERNALS_MAIN_TITLE }, { "personaId", IDS_BRAVE_REWARDS_INTERNALS_PERSONA_ID }, { "processorBraveTokens", IDS_BRAVE_UI_PROCESSOR_BRAVE_TOKENS }, diff --git a/components/brave_ads/browser/ads_service_impl_unittest.cc b/components/brave_ads/browser/ads_service_impl_unittest.cc index e3642a443e5a..cdf9717f8b6b 100644 --- a/components/brave_ads/browser/ads_service_impl_unittest.cc +++ b/components/brave_ads/browser/ads_service_impl_unittest.cc @@ -221,7 +221,7 @@ class MockRewardsService : public RewardsService { const int verbose_level, const std::string& message)); - MOCK_METHOD1(LoadDiagnosticLog, void( + MOCK_METHOD2(LoadDiagnosticLog, void( const int num_lines, brave_rewards::LoadDiagnosticLogCallback callback)); diff --git a/components/brave_rewards/resources/internals/actions/rewards_internals_actions.ts b/components/brave_rewards/resources/internals/actions/rewards_internals_actions.ts index a809d8bd183b..c25bee0907a6 100644 --- a/components/brave_rewards/resources/internals/actions/rewards_internals_actions.ts +++ b/components/brave_rewards/resources/internals/actions/rewards_internals_actions.ts @@ -35,13 +35,20 @@ export const onPromotions = (promotions: RewardsInternals.Promotion[]) => promotions }) -export const getLog = () => action(types.GET_LOG) +export const getPartialLog = () => action(types.GET_PARTIAL_LOG) -export const onGetLog = (log: string) => - action(types.ON_GET_LOG, { +export const onGetPartialLog = (log: string) => + action(types.ON_GET_PARTIAL_LOG, { + log + }) + +export const getFullLog = () => action(types.GET_FULL_LOG) + +export const onGetFullLog = (log: string) => + action(types.ON_GET_FULL_LOG, { log }) export const clearLog = () => action(types.CLEAR_LOG) -export const onClearLog = () => action(types.ON_CLEAR_LOG) +export const downloadCompleted = () => action(types.DOWNLOAD_COMPLETED) diff --git a/components/brave_rewards/resources/internals/brave_rewards_internals.tsx b/components/brave_rewards/resources/internals/brave_rewards_internals.tsx index 966b3b1987e2..0c139c8730b5 100644 --- a/components/brave_rewards/resources/internals/brave_rewards_internals.tsx +++ b/components/brave_rewards/resources/internals/brave_rewards_internals.tsx @@ -47,8 +47,12 @@ window.cr.define('brave_rewards_internals', function () { getActions().onPromotions(promotions) } - function log (log: string) { - getActions().onGetLog(log) + function partialLog (log: string) { + getActions().onGetPartialLog(log) + } + + function fullLog (log: string) { + getActions().onGetFullLog(log) } function initialize () { @@ -69,7 +73,8 @@ window.cr.define('brave_rewards_internals', function () { onGetRewardsInternalsInfo, balance, promotions, - log + partialLog, + fullLog } }) diff --git a/components/brave_rewards/resources/internals/components/app.tsx b/components/brave_rewards/resources/internals/components/app.tsx index 5218f2189043..559bcb735313 100644 --- a/components/brave_rewards/resources/internals/components/app.tsx +++ b/components/brave_rewards/resources/internals/components/app.tsx @@ -33,10 +33,6 @@ export class RewardsInternalsPage extends React.Component { this.state = { currentTabId: 'generalInfo' } - this.getLog = this.getLog.bind(this) - this.clearLog = this.clearLog.bind(this) - this.getGeneralInfo = this.getGeneralInfo.bind(this) - this.getContributions = this.getContributions.bind(this) } componentDidMount () { @@ -57,6 +53,10 @@ export class RewardsInternalsPage extends React.Component { this.setState({ currentTabId: tabId }) switch (tabId) { + case 'generalInfo': { + this.getGeneralInfo() + break + } case 'promotions': { this.getPromotions() break @@ -65,10 +65,6 @@ export class RewardsInternalsPage extends React.Component { this.getContributions() break } - case 'generalInfo': { - this.getGeneralInfo() - break - } } } @@ -76,8 +72,16 @@ export class RewardsInternalsPage extends React.Component { this.actions.clearLog() } - getLog = () => { - this.actions.getLog() + getPartialLog = () => { + this.actions.getPartialLog() + } + + getFullLog = () => { + this.actions.getFullLog() + } + + downloadCompleted = () => { + this.actions.downloadCompleted() } getPromotions = () => { @@ -85,11 +89,11 @@ export class RewardsInternalsPage extends React.Component { } getContributions = () => { - // TODO add + // TODO(https://github.com/brave/brave-browser/issues/8633): implement } render () { - const { isRewardsEnabled, info, promotions, log } = this.props.rewardsInternalsData + const { isRewardsEnabled, info, promotions, log, fullLog } = this.props.rewardsInternalsData if (!isRewardsEnabled) { return ( @@ -112,7 +116,14 @@ export class RewardsInternalsPage extends React.Component {
- +
diff --git a/components/brave_rewards/resources/internals/components/log.tsx b/components/brave_rewards/resources/internals/components/log.tsx index 008be0f7da1d..4ce247a153f1 100644 --- a/components/brave_rewards/resources/internals/components/log.tsx +++ b/components/brave_rewards/resources/internals/components/log.tsx @@ -6,19 +6,23 @@ import * as React from 'react' // Components import { Checkbox, Button } from 'brave-ui/components' -import { LogTextArea, LogControls, ButtonGroup } from '../style' +import { LogTextArea, LogControls, ButtonGroup, Notice } from '../style' // Utils import { getLocale } from '../../../../common/locale' interface Props { log: string + fullLog: string onClear: () => void onGet: () => void + onFullLog: () => void + onDownloadCompleted: () => void } interface State { autoRefresh: boolean + downloadStarted: boolean } export class Log extends React.Component { @@ -27,7 +31,15 @@ export class Log extends React.Component { constructor (props: Props) { super(props) this.state = { - autoRefresh: false + autoRefresh: false, + downloadStarted: false + } + } + + componentDidUpdate (prevProps: Props) { + if (this.props.fullLog.length !== 0) { + this.downloadFile(this.props.fullLog) + this.props.onDownloadCompleted() } } @@ -46,6 +58,23 @@ export class Log extends React.Component { clearInterval(this.interval) } + downloadFile = (log: string) => { + const filename = 'brave_rewards_log.txt' + let element = document.createElement('a') + element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(log)) + element.setAttribute('download', filename) + + element.style.display = 'none' + document.body.appendChild(element) + + element.click() + document.body.removeChild(element) + } + + preventDefault = (event: React.MouseEvent) => { + event.preventDefault() + } + render () { return ( <> @@ -71,9 +100,18 @@ export class Log extends React.Component { type={'accent'} onClick={this.props.onGet} /> +