Skip to content

Commit

Permalink
Adds download option and smaller log
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jun 3, 2020
1 parent d9bc26a commit d833add
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 43 deletions.
52 changes: 42 additions & 10 deletions browser/ui/webui/brave_rewards_internals_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

namespace {

const int g_partial_log_max_lines = 5000;

class RewardsInternalsDOMHandler : public content::WebUIMessageHandler {
public:
RewardsInternalsDOMHandler();
Expand All @@ -52,8 +54,10 @@ class RewardsInternalsDOMHandler : public content::WebUIMessageHandler {
std::unique_ptr<brave_rewards::Balance> balance);
void GetPromotions(const base::ListValue* args);
void OnGetPromotions(const std::vector<brave_rewards::Promotion>& 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);

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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));
}

Expand All @@ -280,7 +312,7 @@ void RewardsInternalsDOMHandler::OnClearLog(const bool success) {
}

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards_internals.log",
"brave_rewards_internals.partialLog",
base::Value(""));
}

Expand Down
2 changes: 2 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -69,7 +73,8 @@ window.cr.define('brave_rewards_internals', function () {
onGetRewardsInternalsInfo,
balance,
promotions,
log
partialLog,
fullLog
}
})

Expand Down
37 changes: 24 additions & 13 deletions components/brave_rewards/resources/internals/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ export class RewardsInternalsPage extends React.Component<Props, State> {
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 () {
Expand All @@ -57,6 +53,10 @@ export class RewardsInternalsPage extends React.Component<Props, State> {
this.setState({ currentTabId: tabId })

switch (tabId) {
case 'generalInfo': {
this.getGeneralInfo()
break
}
case 'promotions': {
this.getPromotions()
break
Expand All @@ -65,31 +65,35 @@ export class RewardsInternalsPage extends React.Component<Props, State> {
this.getContributions()
break
}
case 'generalInfo': {
this.getGeneralInfo()
break
}
}
}

clearLog = () => {
this.actions.clearLog()
}

getLog = () => {
this.actions.getLog()
getPartialLog = () => {
this.actions.getPartialLog()
}

getFullLog = () => {
this.actions.getFullLog()
}

downloadCompleted = () => {
this.actions.downloadCompleted()
}

getPromotions = () => {
this.actions.getPromotions()
}

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 (
Expand All @@ -112,7 +116,14 @@ export class RewardsInternalsPage extends React.Component<Props, State> {
<General data={this.props.rewardsInternalsData} onGet={this.getGeneralInfo} />
</div>
<div data-key='logs' data-title={getLocale('tabLogs')}>
<Log log={log} onGet={this.getLog} onClear={this.clearLog} />
<Log
log={log}
fullLog={fullLog}
onGet={this.getPartialLog}
onFullLog={this.getFullLog}
onClear={this.clearLog}
onDownloadCompleted={this.downloadCompleted}
/>
</div>
<div data-key='promotions' data-title={getLocale('tabPromotions')}>
<Promotions items={promotions} onGet={this.getPromotions} />
Expand Down
44 changes: 41 additions & 3 deletions components/brave_rewards/resources/internals/components/log.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Props, State> {
Expand All @@ -27,7 +31,15 @@ export class Log extends React.Component<Props, State> {
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()
}
}

Expand All @@ -46,6 +58,23 @@ export class Log extends React.Component<Props, State> {
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 (
<>
Expand All @@ -71,9 +100,18 @@ export class Log extends React.Component<Props, State> {
type={'accent'}
onClick={this.props.onGet}
/>
<Button
text={getLocale('downloadButton')}
size={'medium'}
type={'accent'}
onClick={this.props.onFullLog}
/>
</ButtonGroup>
</LogControls>
<LogTextArea value={this.props.log} readOnly={true}/>
<LogTextArea value={this.props.log} readOnly={true} onContextMenu={this.preventDefault}/>
<Notice>
{getLocale('logNotice', { numberOfLines: '5,000' })}
</Notice>
</>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export const enum types {
ON_BALANCE = '@@rewards_internals/ON_BALANCE',
GET_PROMOTIONS = '@@rewards_internals/GET_PROMOTIONS',
ON_PROMOTIONS = '@@rewards_internals/ON_PROMOTIONS',
GET_LOG = '@@rewards_internals/GET_LOG',
ON_GET_LOG = '@@rewards_internals/ON_GET_LOG',
GET_PARTIAL_LOG = '@@rewards_internals/GET_PARTIAL_LOG',
ON_GET_PARTIAL_LOG = '@@rewards_internals/ON_GET_PARTIAL_LOG',
GET_FULL_LOG = '@@rewards_internals/GET_FULL_LOG',
ON_GET_FULL_LOG = '@@rewards_internals/ON_GET_FULL_LOG',
CLEAR_LOG = '@@rewards_internals/CLEAR_LOG',
ON_CLEAR_LOG = '@@rewards_internals/ON_CLEAR_LOG'
DOWNLOAD_COMPLETED = '@@rewards_internals/DOWNLOAD_COMPLETED'
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,25 @@ const rewardsInternalsReducer: Reducer<RewardsInternals.State | undefined> = (st
state = { ...state }
state.promotions = action.payload.promotions
break
case types.GET_LOG:
chrome.send('brave_rewards_internals.getLog')
case types.GET_PARTIAL_LOG:
chrome.send('brave_rewards_internals.getPartialLog')
break
case types.ON_GET_LOG:
case types.ON_GET_PARTIAL_LOG:
state = { ...state }
state.log = action.payload.log
break
case types.GET_FULL_LOG:
chrome.send('brave_rewards_internals.getFullLog')
break
case types.ON_GET_FULL_LOG:
state = { ...state }
state.fullLog = action.payload.log
break
case types.CLEAR_LOG:
chrome.send('brave_rewards_internals.clearLog')
break
case types.ON_CLEAR_LOG:
state.log = ''
case types.DOWNLOAD_COMPLETED:
state.fullLog = ''
break
default:
break
Expand Down
3 changes: 2 additions & 1 deletion components/brave_rewards/resources/internals/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const defaultState: RewardsInternals.State = {
bootStamp: 0
},
promotions: [],
log: ''
log: '',
fullLog: ''
}

export const load = (): RewardsInternals.State => {
Expand Down
Loading

0 comments on commit d833add

Please sign in to comment.