From db71ef773715fc02440ff8f479bd666e005f954c Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 18 Aug 2020 12:59:56 +0200 Subject: [PATCH] Disable logs Resolves https://github.com/brave/brave-browser/issues/11260 --- .../browser/rewards_service_impl.cc | 36 ++++++++++++++++++- .../browser/rewards_service_impl.h | 7 ++++ .../bat_ledger_client_mojo_bridge.cc | 12 +++++++ .../bat_ledger_client_mojo_bridge.h | 2 ++ .../public/cpp/ledger_client_mojo_bridge.cc | 21 +++++++++++ .../public/cpp/ledger_client_mojo_bridge.h | 6 ++++ .../public/interfaces/bat_ledger.mojom | 2 ++ vendor/bat-native-ledger/BUILD.gn | 2 ++ .../include/bat/ledger/ledger_client.h | 2 ++ .../bat/ledger/internal/ledger_client_mock.h | 2 ++ .../ledger/internal/state/state_migration.cc | 9 +++-- .../ledger/internal/state/state_migration.h | 2 ++ .../internal/state/state_migration_v4.cc | 22 ++++++++++++ .../internal/state/state_migration_v4.h | 30 ++++++++++++++++ vendor/brave-ios/Ledger/BATBraveLedger.mm | 5 +++ .../Ledger/Generated/NativeLedgerClient.h | 1 + .../Ledger/Generated/NativeLedgerClient.mm | 3 ++ .../Generated/NativeLedgerClientBridge.h | 1 + 18 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v4.cc create mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v4.h diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 708d52337216..15d7a729d656 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -2323,7 +2323,7 @@ void RewardsServiceImpl::DiagnosticLog( const int line, const int verbose_level, const std::string& message) { - if (ledger_for_testing_) { + if (ledger_for_testing_ || !should_persist_logs_) { return; } @@ -2571,6 +2571,16 @@ void RewardsServiceImpl::HandleFlags(const std::string& options) { continue; } + + if (name == "persist-logs") { + const std::string lower = base::ToLowerASCII(value); + + if (lower == "true" || lower == "1") { + should_persist_logs_ = true; + } else { + should_persist_logs_ = false; + } + } } } @@ -3773,4 +3783,28 @@ void RewardsServiceImpl::WalletDisconnected(const std::string& wallet_type) { OnDisconnectWallet(wallet_type, ledger::Result::LEDGER_OK); } +void RewardsServiceImpl::DeleteLog(ledger::ResultCallback callback) { + diagnostic_log_.Close(); + base::PostTaskAndReplyWithResult( + file_task_runner_.get(), + FROM_HERE, + base::BindOnce( + &RewardsServiceImpl::DeleteLogTaskRunner, + base::Unretained(this)), + base::BindOnce( + &RewardsServiceImpl::OnDeleteLog, + AsWeakPtr(), + std::move(callback))); +} + +bool RewardsServiceImpl::DeleteLogTaskRunner() { + return base::DeleteFile(diagnostic_log_path_); +} + +void RewardsServiceImpl::OnDeleteLog( + ledger::ResultCallback callback, + const bool success) { + callback(success ? ledger::Result::LEDGER_OK : ledger::Result::LEDGER_ERROR); +} + } // namespace brave_rewards diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index c778bad630e8..35c28102f9bc 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -634,6 +634,8 @@ class RewardsServiceImpl : public RewardsService, void WalletDisconnected(const std::string& wallet_type) override; + void DeleteLog(ledger::ResultCallback callback) override; + // end ledger::LedgerClient // Mojo Proxy methods @@ -702,6 +704,10 @@ class RewardsServiceImpl : public RewardsService, void OnCompleteReset(SuccessCallback callback, const bool success); + bool DeleteLogTaskRunner(); + + void OnDeleteLog(ledger::ResultCallback callback, const bool success); + #if defined(OS_ANDROID) ledger::Environment GetServerEnvironmentForAndroid(); void CreateWalletAttestationResult( @@ -748,6 +754,7 @@ class RewardsServiceImpl : public RewardsService, bool is_wallet_initialized_ = false; bool ledger_for_testing_ = false; bool resetting_rewards_ = false; + bool should_persist_logs_ = false; GetTestResponseCallback test_response_callback_; diff --git a/components/services/bat_ledger/bat_ledger_client_mojo_bridge.cc b/components/services/bat_ledger/bat_ledger_client_mojo_bridge.cc index f3054f54b5fb..4a8aa8437c02 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_bridge.cc +++ b/components/services/bat_ledger/bat_ledger_client_mojo_bridge.cc @@ -392,4 +392,16 @@ void BatLedgerClientMojoBridge::WalletDisconnected( bat_ledger_client_->WalletDisconnected(wallet_type); } +void OnDeleteLog( + const ledger::ResultCallback callback, + const ledger::Result result) { + callback(result); +} + +void BatLedgerClientMojoBridge::DeleteLog( + ledger::ResultCallback callback) { + bat_ledger_client_->DeleteLog( + base::BindOnce(&OnDeleteLog, std::move(callback))); +} + } // namespace bat_ledger diff --git a/components/services/bat_ledger/bat_ledger_client_mojo_bridge.h b/components/services/bat_ledger/bat_ledger_client_mojo_bridge.h index f8daf93987bf..ff5872c116db 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_bridge.h +++ b/components/services/bat_ledger/bat_ledger_client_mojo_bridge.h @@ -135,6 +135,8 @@ class BatLedgerClientMojoBridge : void WalletDisconnected(const std::string& wallet_type) override; + void DeleteLog(ledger::ResultCallback callback) override; + private: bool Connected() const; diff --git a/components/services/bat_ledger/public/cpp/ledger_client_mojo_bridge.cc b/components/services/bat_ledger/public/cpp/ledger_client_mojo_bridge.cc index 281a7db4f509..8ceee6034d1c 100644 --- a/components/services/bat_ledger/public/cpp/ledger_client_mojo_bridge.cc +++ b/components/services/bat_ledger/public/cpp/ledger_client_mojo_bridge.cc @@ -385,4 +385,25 @@ void LedgerClientMojoBridge::WalletDisconnected( ledger_client_->WalletDisconnected(wallet_type); } +// static +void LedgerClientMojoBridge::OnDeleteLog( + CallbackHolder* holder, + const ledger::Result result) { + DCHECK(holder); + if (holder->is_valid()) { + std::move(holder->get()).Run(result); + } + delete holder; +} + +void LedgerClientMojoBridge::DeleteLog(DeleteLogCallback callback) { + auto* holder = new CallbackHolder( + AsWeakPtr(), + std::move(callback)); + ledger_client_->DeleteLog( + std::bind(LedgerClientMojoBridge::OnDeleteLog, + holder, + _1)); +} + } // namespace bat_ledger diff --git a/components/services/bat_ledger/public/cpp/ledger_client_mojo_bridge.h b/components/services/bat_ledger/public/cpp/ledger_client_mojo_bridge.h index 9f79eb852547..52c028827679 100644 --- a/components/services/bat_ledger/public/cpp/ledger_client_mojo_bridge.h +++ b/components/services/bat_ledger/public/cpp/ledger_client_mojo_bridge.h @@ -149,6 +149,8 @@ class LedgerClientMojoBridge : void WalletDisconnected(const std::string& wallet_type) override; + void DeleteLog(DeleteLogCallback callback) override; + private: // workaround to pass base::OnceCallback into std::bind template @@ -199,6 +201,10 @@ class LedgerClientMojoBridge : const std::string& script, const int table_version); + static void OnDeleteLog( + CallbackHolder* holder, + const ledger::Result result); + ledger::LedgerClient* ledger_client_; }; diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index a7bb3d3f80b2..a7111fbe4485 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -237,4 +237,6 @@ interface BatLedgerClient { ClearAllNotifications(); WalletDisconnected(string wallet_type); + + DeleteLog() => (ledger.mojom.Result result); }; diff --git a/vendor/bat-native-ledger/BUILD.gn b/vendor/bat-native-ledger/BUILD.gn index 41cdbb9e454f..7b09b43e12d3 100644 --- a/vendor/bat-native-ledger/BUILD.gn +++ b/vendor/bat-native-ledger/BUILD.gn @@ -386,6 +386,8 @@ source_set("ledger") { "src/bat/ledger/internal/state/state_migration_v2.h", "src/bat/ledger/internal/state/state_migration_v3.cc", "src/bat/ledger/internal/state/state_migration_v3.h", + "src/bat/ledger/internal/state/state_migration_v4.cc", + "src/bat/ledger/internal/state/state_migration_v4.h", "src/bat/ledger/internal/legacy/client_state.cc", "src/bat/ledger/internal/legacy/client_state.h", "src/bat/ledger/internal/legacy/publisher_settings_state.cc", diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h index b77cf25d2323..f8d185b6ae0a 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h @@ -191,6 +191,8 @@ class LEDGER_EXPORT LedgerClient { virtual void ClearAllNotifications() = 0; virtual void WalletDisconnected(const std::string& wallet_type) = 0; + + virtual void DeleteLog(ledger::ResultCallback callback) = 0; }; } // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_client_mock.h b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_client_mock.h index 9fc3db6206a8..9c84868740ff 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_client_mock.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_client_mock.h @@ -176,6 +176,8 @@ class MockLedgerClient : public LedgerClient { MOCK_METHOD0(ClearAllNotifications, void()); MOCK_METHOD1(WalletDisconnected, void(const std::string& wallet_type)); + + MOCK_METHOD1(DeleteLog, void(const ledger::ResultCallback callback)); }; } // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.cc index f680555f2fcb..bb4b39123cac 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.cc @@ -12,7 +12,7 @@ using std::placeholders::_1; namespace { -const int kCurrentVersionNumber = 3; +const int kCurrentVersionNumber = 4; } // namespace @@ -22,8 +22,9 @@ StateMigration::StateMigration(bat_ledger::LedgerImpl* ledger) : v1_(std::make_unique(ledger)), v2_(std::make_unique(ledger)), v3_(std::make_unique()), + v4_(std::make_unique(ledger)), ledger_(ledger) { - DCHECK(v1_ && v2_ && v3_); + DCHECK(v1_ && v2_ && v3_ && v4_); } StateMigration::~StateMigration() = default; @@ -56,6 +57,10 @@ void StateMigration::Migrate(ledger::ResultCallback callback) { v3_->Migrate(migrate_callback); return; } + case 4: { + v4_->Migrate(migrate_callback); + return; + } } BLOG(0, "Migration version is not handled " << new_version); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.h index 0bcc7b361db6..0c9bac053bdc 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.h @@ -12,6 +12,7 @@ #include "bat/ledger/internal/state/state_migration_v1.h" #include "bat/ledger/internal/state/state_migration_v2.h" #include "bat/ledger/internal/state/state_migration_v3.h" +#include "bat/ledger/internal/state/state_migration_v4.h" #include "bat/ledger/ledger.h" namespace bat_ledger { @@ -36,6 +37,7 @@ class StateMigration { std::unique_ptr v1_; std::unique_ptr v2_; std::unique_ptr v3_; + std::unique_ptr v4_; bat_ledger::LedgerImpl* ledger_; // NOT OWNED }; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v4.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v4.cc new file mode 100644 index 000000000000..cfac7183f82b --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v4.cc @@ -0,0 +1,22 @@ +/* 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 "bat/ledger/internal/state/state_migration_v4.h" + +#include "bat/ledger/internal/ledger_impl.h" + +namespace braveledger_state { + +StateMigrationV4::StateMigrationV4(bat_ledger::LedgerImpl* ledger) : + ledger_(ledger) { + DCHECK(ledger_); +} + +StateMigrationV4::~StateMigrationV4() = default; + +void StateMigrationV4::Migrate(ledger::ResultCallback callback) { + ledger_->ledger_client()->DeleteLog(callback); +} + +} // namespace braveledger_state diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v4.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v4.h new file mode 100644 index 000000000000..8c057d668356 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v4.h @@ -0,0 +1,30 @@ +/* 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 BRAVELEDGER_BAT_STATE_STATE_MIGRATION_V4_H_ +#define BRAVELEDGER_BAT_STATE_STATE_MIGRATION_V4_H_ + +#include "bat/ledger/ledger.h" + +namespace bat_ledger { +class LedgerImpl; +} + +namespace braveledger_state { + +class StateMigrationV4 { + public: + explicit StateMigrationV4(bat_ledger::LedgerImpl* ledger); + ~StateMigrationV4(); + + void Migrate(ledger::ResultCallback callback); + + private: + bat_ledger::LedgerImpl* ledger_; // NOT OWNED +}; + +} // namespace braveledger_state + +#endif // BRAVELEDGER_BAT_STATE_STATE_MIGRATION_V4_H_ diff --git a/vendor/brave-ios/Ledger/BATBraveLedger.mm b/vendor/brave-ios/Ledger/BATBraveLedger.mm index 874e811e365d..6bcb7e3d264e 100644 --- a/vendor/brave-ios/Ledger/BATBraveLedger.mm +++ b/vendor/brave-ios/Ledger/BATBraveLedger.mm @@ -1921,4 +1921,9 @@ - (void)walletDisconnected:(const std::string &)wallet_type } } +- (void)deleteLog:(ledger::ResultCallback)callback +{ + // TODO implement +} + @end diff --git a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h index 4cd363ca2f0d..e535919828f4 100644 --- a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h +++ b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h @@ -58,4 +58,5 @@ class NativeLedgerClient : public ledger::LedgerClient { void PendingContributionSaved(const ledger::Result result) override; void ClearAllNotifications() override; void WalletDisconnected(const std::string& wallet_type) override; + void DeleteLog(ledger::ResultCallback callback) override; }; diff --git a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm index ac3862cde751..13a91c072035 100644 --- a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm +++ b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm @@ -140,3 +140,6 @@ void NativeLedgerClient::WalletDisconnected(const std::string& wallet_type) { [bridge_ walletDisconnected:wallet_type]; } +void NativeLedgerClient::DeleteLog(ledger::ResultCallback callback) { + [bridge_ deleteLog:callback]; +} diff --git a/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h b/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h index 9d059ee25676..bfc4442668a2 100644 --- a/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h +++ b/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h @@ -51,5 +51,6 @@ - (void)pendingContributionSaved:(const ledger::Result)result; - (void)clearAllNotifications; - (void)walletDisconnected:(const std::string&)wallet_type; +- (void)deleteLog:(ledger::ResultCallback)callback; @end