Skip to content

Commit

Permalink
Adds event logging system
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Aug 19, 2020
1 parent 0d4b268 commit 80991a5
Show file tree
Hide file tree
Showing 59 changed files with 933 additions and 30 deletions.
41 changes: 40 additions & 1 deletion browser/ui/webui/brave_rewards_internals_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class RewardsInternalsDOMHandler : public content::WebUIMessageHandler {
void OnGetExternalWallet(
int32_t result,
std::unique_ptr<brave_rewards::ExternalWallet> wallet);
void GetEventLogs(const base::ListValue* args);
void OnGetEventLogs(const std::vector<brave_rewards::EventLog>&);

brave_rewards::RewardsService* rewards_service_; // NOT OWNED
Profile* profile_;
Expand Down Expand Up @@ -121,12 +123,16 @@ void RewardsInternalsDOMHandler::RegisterMessages() {
base::BindRepeating(
&RewardsInternalsDOMHandler::ClearLog,
base::Unretained(this)));

web_ui()->RegisterMessageCallback(
"brave_rewards_internals.getExternalWallet",
base::BindRepeating(
&RewardsInternalsDOMHandler::GetExternalWallet,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"brave_rewards_internals.getEventLogs",
base::BindRepeating(
&RewardsInternalsDOMHandler::GetEventLogs,
base::Unretained(this)));
}

void RewardsInternalsDOMHandler::Init() {
Expand Down Expand Up @@ -410,6 +416,39 @@ void RewardsInternalsDOMHandler::OnGetExternalWallet(
data);
}

void RewardsInternalsDOMHandler::GetEventLogs(const base::ListValue* args) {
if (!rewards_service_) {
return;
}

rewards_service_->GetEventLogs(
base::BindOnce(
&RewardsInternalsDOMHandler::OnGetEventLogs,
weak_ptr_factory_.GetWeakPtr()));
}

void RewardsInternalsDOMHandler::OnGetEventLogs(
const std::vector<brave_rewards::EventLog>& logs) {
if (!web_ui()->CanCallJavascript()) {
return;
}

base::Value data(base::Value::Type::LIST);

for (const auto& log : logs) {
base::Value item(base::Value::Type::DICTIONARY);
item.SetStringKey("id", log.event_log_id);
item.SetStringKey("key", log.key);
item.SetStringKey("value", log.value);
item.SetIntKey("createdAt", log.created_at);
data.Append(std::move(item));
}

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards_internals.eventLogs",
std::move(data));
}

} // namespace

BraveRewardsInternalsUI::BraveRewardsInternalsUI(content::WebUI* web_ui,
Expand Down
4 changes: 4 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,9 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "contributionStepRewardsOff", IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_REWARDS_OFF }, // NOLINT
{ "contributionStepAutoContributeOff", IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_AUTO_CONTRIBUTE_OFF }, // NOLINT
{ "contributionStepRetryCount", IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_RETRY_COUNT }, // NOLINT
{ "eventLogKey", IDS_BRAVE_REWARDS_INTERNALS_EVENT_LOG_KEY },
{ "eventLogValue", IDS_BRAVE_REWARDS_INTERNALS_EVENT_LOG_VALUE },
{ "eventLogTime", IDS_BRAVE_REWARDS_INTERNALS_EVENT_LOG_TIME },
{ "mainDisclaimer", IDS_BRAVE_REWARDS_INTERNALS_MAIN_DISCLAIMER },
{ "rewardsNotEnabled", IDS_BRAVE_REWARDS_INTERNALS_REWARDS_NOT_ENABLED }, // NOLINT
{ "rewardsTypeAuto", IDS_BRAVE_REWARDS_INTERNALS_REWARDS_TYPE_AUTO }, // NOLINT
Expand Down Expand Up @@ -893,6 +896,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "tabLogs", IDS_BRAVE_REWARDS_INTERNALS_TAB_LOGS },
{ "tabPromotions", IDS_BRAVE_REWARDS_INTERNALS_TAB_PROMOTIONS },
{ "tabContributions", IDS_BRAVE_REWARDS_INTERNALS_TAB_CONTRIBUTIONS },
{ "tabEventLogs", IDS_BRAVE_REWARDS_INTERNALS_TAB_EVENT_LOGS },
{ "totalAmount", IDS_BRAVE_REWARDS_INTERNALS_TOTAL_AMOUNT },
{ "totalBalance", IDS_BRAVE_REWARDS_INTERNALS_TOTAL_BALANCE },
{ "userId", IDS_BRAVE_REWARDS_INTERNALS_USER_ID },
Expand Down
4 changes: 4 additions & 0 deletions components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ class MockRewardsService : public RewardsService {
brave_rewards::ClearDiagnosticLogCallback callback));

MOCK_METHOD1(CompleteReset, void(brave_rewards::SuccessCallback callback));

MOCK_METHOD1(
GetEventLogs,
void(brave_rewards::GetEventLogsCallback callback));
};

class AdsServiceTest : public testing::Test {
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ source_set("browser") {
"contribution_report_info.h",
"rewards_database.h",
"rewards_database.cc",
"event_log.h",
"event_log.cc",
]

deps = [
Expand Down
16 changes: 16 additions & 0 deletions components/brave_rewards/browser/event_log.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Copyright (c) 2020 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/components/brave_rewards/browser/event_log.h"

namespace brave_rewards {

EventLog::EventLog() = default;

EventLog::~EventLog() = default;

EventLog::EventLog(const EventLog &properties) = default;

} // namespace brave_rewards
26 changes: 26 additions & 0 deletions components/brave_rewards/browser/event_log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* Copyright (c) 2020 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_COMPONENTS_BRAVE_REWARDS_BROWSER_EVENT_LOG_H_
#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_EVENT_LOG_H_

#include <string>

namespace brave_rewards {

struct EventLog {
EventLog();
~EventLog();
EventLog(const EventLog& properties);

std::string event_log_id;
std::string key;
std::string value;
uint64_t created_at;
};

} // namespace brave_rewards

#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_EVENT_LOG_H_
13 changes: 12 additions & 1 deletion components/brave_rewards/browser/rewards_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ void RewardsDatabase::RunTransaction(
return;
}

// Close command must always be sent as single command in transaction
if (transaction->commands.size() == 1 &&
transaction->commands[0]->type == ledger::DBCommand::Type::CLOSE) {
db_.Close();
initialized_ = false;
command_response->status = ledger::DBCommandResponse::Status::RESPONSE_OK;
return;
}

sql::Transaction committer(&db_);
if (!committer.Begin()) {
command_response->status =
Expand Down Expand Up @@ -170,8 +179,10 @@ void RewardsDatabase::RunTransaction(
status = ledger::DBCommandResponse::Status::RESPONSE_OK;
break;
}
default: {
case ledger::DBCommand::Type::CLOSE: {
NOTREACHED();
status = ledger::DBCommandResponse::Status::COMMAND_ERROR;
break;
}
}

Expand Down
6 changes: 6 additions & 0 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "brave/components/brave_rewards/browser/balance_report.h"
#include "brave/components/brave_rewards/browser/content_site.h"
#include "brave/components/brave_rewards/browser/contribution_info.h"
#include "brave/components/brave_rewards/browser/event_log.h"
#include "brave/components/brave_rewards/browser/external_wallet.h"
#include "brave/components/brave_rewards/browser/publisher_banner.h"
#include "brave/components/brave_rewards/browser/pending_contribution.h"
Expand Down Expand Up @@ -129,6 +130,9 @@ using ClearDiagnosticLogCallback = base::OnceCallback<void(const bool success)>;

using SuccessCallback = base::OnceCallback<void(const bool success)>;

using GetEventLogsCallback =
base::OnceCallback<void(const std::vector<brave_rewards::EventLog>&)>;

class RewardsService : public KeyedService {
public:
RewardsService();
Expand Down Expand Up @@ -333,6 +337,8 @@ class RewardsService : public KeyedService {

virtual void CompleteReset(SuccessCallback callback) = 0;

virtual void GetEventLogs(GetEventLogsCallback callback) = 0;

protected:
base::ObserverList<RewardsServiceObserver> observers_;

Expand Down
27 changes: 27 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "brave/components/brave_rewards/browser/balance_report.h"
#include "brave/components/brave_rewards/browser/content_site.h"
#include "brave/components/brave_rewards/browser/contribution_info.h"
#include "brave/components/brave_rewards/browser/event_log.h"
#include "brave/components/brave_rewards/browser/file_util.h"
#include "brave/components/brave_rewards/browser/logging.h"
#include "brave/components/brave_rewards/browser/logging_util.h"
Expand Down Expand Up @@ -3807,4 +3808,30 @@ void RewardsServiceImpl::OnDeleteLog(
callback(success ? ledger::Result::LEDGER_OK : ledger::Result::LEDGER_ERROR);
}

void RewardsServiceImpl::GetEventLogs(GetEventLogsCallback callback) {
if (!Connected()) {
return;
}

bat_ledger_->GetEventLogs(
base::BindOnce(&RewardsServiceImpl::OnGetEventLogs,
AsWeakPtr(),
std::move(callback)));
}

void RewardsServiceImpl::OnGetEventLogs(
GetEventLogsCallback callback,
ledger::EventLogs logs) {
std::vector<brave_rewards::EventLog> event_logs;
for (const auto& log : logs) {
brave_rewards::EventLog properties;
properties.event_log_id = log->event_log_id;
properties.key = log->key;
properties.value = log->value;
properties.created_at = log->created_at;
event_logs.push_back(properties);
}
std::move(callback).Run(std::move(event_logs));
}

} // namespace brave_rewards
11 changes: 9 additions & 2 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ class RewardsServiceImpl : public RewardsService,

void GetAllPromotions(GetAllPromotionsCallback callback) override;

void GetEventLogs(GetEventLogsCallback callback) override;

void StopLedger(StopLedgerCallback callback);

// Testing methods
void SetLedgerEnvForTesting();
void PrepareLedgerEnvForTesting();
Expand All @@ -335,11 +339,10 @@ class RewardsServiceImpl : public RewardsService,

void EnableGreaseLion(const bool enabled);

void StopLedger(StopLedgerCallback callback);

void OnStopLedger(
StopLedgerCallback callback,
const ledger::Result result);

void OnStopLedgerForCompleteReset(
SuccessCallback callback,
const ledger::Result result);
Expand Down Expand Up @@ -708,6 +711,10 @@ class RewardsServiceImpl : public RewardsService,

void OnDeleteLog(ledger::ResultCallback callback, const bool success);

void OnGetEventLogs(
GetEventLogsCallback callback,
ledger::EventLogs logs);

#if defined(OS_ANDROID)
ledger::Environment GetServerEnvironmentForAndroid();
void CreateWalletAttestationResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* 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 <utility>

#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/run_loop.h"
Expand Down Expand Up @@ -111,4 +113,13 @@ void NavigateToPublisherPage(
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
}

void WaitForLedgerStop(brave_rewards::RewardsServiceImpl* rewards_service) {
base::RunLoop run_loop;
rewards_service->StopLedger(
base::BindLambdaForTesting([&](const ledger::Result) {
run_loop.Quit();
}));
run_loop.Run();
}

} // namespace rewards_browsertest_util
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ void NavigateToPublisherPage(
const std::string& publisher_key,
const std::string& path = "");

void WaitForLedgerStop(brave_rewards::RewardsServiceImpl* rewards_service);

} // namespace rewards_browsertest_util

#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_TEST_COMMON_REWARDS_BROWSERTEST_UTIL_H_
Loading

0 comments on commit 80991a5

Please sign in to comment.