-
Notifications
You must be signed in to change notification settings - Fork 887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Removes std::function<>
from the utility process (Part5).
#19792
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,6 @@ | |
#include "brave/components/brave_rewards/core/rewards_engine_impl.h" | ||
#include "brave/components/brave_rewards/core/state/state_keys.h" | ||
|
||
using std::placeholders::_1; | ||
using std::placeholders::_2; | ||
|
||
namespace brave_rewards::internal { | ||
namespace database { | ||
|
||
|
@@ -21,7 +18,7 @@ DatabaseInitialize::DatabaseInitialize(RewardsEngineImpl& engine) | |
|
||
DatabaseInitialize::~DatabaseInitialize() = default; | ||
|
||
void DatabaseInitialize::Start(LegacyResultCallback callback) { | ||
void DatabaseInitialize::Start(ResultCallback callback) { | ||
auto transaction = mojom::DBTransaction::New(); | ||
transaction->version = GetCurrentVersion(); | ||
transaction->compatible_version = GetCompatibleVersion(); | ||
|
@@ -31,27 +28,26 @@ void DatabaseInitialize::Start(LegacyResultCallback callback) { | |
|
||
engine_->RunDBTransaction( | ||
std::move(transaction), | ||
std::bind(&DatabaseInitialize::OnInitialize, this, _1, callback)); | ||
base::BindOnce(&DatabaseInitialize::OnInitialize, base::Unretained(this), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reported by reviewdog 🐶 |
||
std::move(callback))); | ||
} | ||
|
||
void DatabaseInitialize::OnInitialize(mojom::DBCommandResponsePtr response, | ||
LegacyResultCallback callback) { | ||
void DatabaseInitialize::OnInitialize(ResultCallback callback, | ||
mojom::DBCommandResponsePtr response) { | ||
if (!response || | ||
response->status != mojom::DBCommandResponse::Status::RESPONSE_OK) { | ||
BLOG(0, "Response is wrong"); | ||
callback(mojom::Result::DATABASE_INIT_FAILED); | ||
return; | ||
return std::move(callback).Run(mojom::Result::DATABASE_INIT_FAILED); | ||
} | ||
|
||
if (!response->result || !response->result->get_value()->is_int_value()) { | ||
BLOG(0, "DB init failed"); | ||
callback(mojom::Result::DATABASE_INIT_FAILED); | ||
return; | ||
return std::move(callback).Run(mojom::Result::DATABASE_INIT_FAILED); | ||
} | ||
|
||
const auto current_table_version = | ||
response->result->get_value()->get_int_value(); | ||
migration_.Start(current_table_version, callback); | ||
migration_.Start(current_table_version, std::move(callback)); | ||
} | ||
|
||
} // namespace database | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,8 +71,7 @@ DatabaseMigration::DatabaseMigration(RewardsEngineImpl& engine) | |
|
||
DatabaseMigration::~DatabaseMigration() = default; | ||
|
||
void DatabaseMigration::Start(uint32_t table_version, | ||
LegacyResultCallback callback) { | ||
void DatabaseMigration::Start(uint32_t table_version, ResultCallback callback) { | ||
const uint32_t start_version = table_version + 1; | ||
DCHECK_GT(start_version, 0u); | ||
|
||
|
@@ -83,8 +82,7 @@ void DatabaseMigration::Start(uint32_t table_version, | |
: database::GetCurrentVersion(); | ||
|
||
if (target_version == table_version) { | ||
callback(mojom::Result::OK); | ||
return; | ||
return std::move(callback).Run(mojom::Result::OK); | ||
} | ||
|
||
// Migration 30 archives and clears the user's unblinded tokens table. It | ||
|
@@ -164,24 +162,11 @@ void DatabaseMigration::Start(uint32_t table_version, | |
command->type = mojom::DBCommand::Type::VACUUM; | ||
transaction->commands.push_back(std::move(command)); | ||
|
||
const std::string message = | ||
base::StringPrintf("%d->%d", start_version, migrated_version); | ||
|
||
engine_->RunDBTransaction( | ||
std::move(transaction), [this, callback, message, migrated_version]( | ||
mojom::DBCommandResponsePtr response) { | ||
if (response && | ||
response->status == mojom::DBCommandResponse::Status::RESPONSE_OK) { | ||
// The event_log table was introduced in v29. | ||
if (migrated_version >= 29) { | ||
engine_->database()->SaveEventLog(log::kDatabaseMigrated, message); | ||
} | ||
callback(mojom::Result::OK); | ||
return; | ||
} | ||
|
||
callback(mojom::Result::FAILED); | ||
}); | ||
std::move(transaction), | ||
base::BindOnce(&DatabaseMigration::RunDBTransactionCallback, | ||
base::Unretained(this), std::move(callback), start_version, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reported by reviewdog 🐶 |
||
migrated_version)); | ||
} | ||
|
||
void DatabaseMigration::SetTargetVersionForTesting(uint32_t version) { | ||
|
@@ -202,5 +187,25 @@ void DatabaseMigration::GenerateCommand(mojom::DBTransaction* transaction, | |
transaction->commands.push_back(std::move(command)); | ||
} | ||
|
||
void DatabaseMigration::RunDBTransactionCallback( | ||
ResultCallback callback, | ||
uint32_t start_version, | ||
int migrated_version, | ||
mojom::DBCommandResponsePtr response) { | ||
if (response && | ||
response->status == mojom::DBCommandResponse::Status::RESPONSE_OK) { | ||
// The event_log table was introduced in v29. | ||
if (migrated_version >= 29) { | ||
engine_->database()->SaveEventLog( | ||
log::kDatabaseMigrated, | ||
base::StringPrintf("%d->%d", start_version, migrated_version)); | ||
} | ||
|
||
return std::move(callback).Run(mojom::Result::OK); | ||
} | ||
|
||
std::move(callback).Run(mojom::Result::FAILED); | ||
} | ||
|
||
} // namespace database | ||
} // namespace brave_rewards::internal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reported by reviewdog 🐶
[semgrep] base::Unretained is most of the time unrequited, and a weak reference is better suited for secure coding.
Consider swapping Unretained for a weak reference.
base::Unretained usage may be acceptable when a callback owner is guaranteed
to be destroyed with the object base::Unretained is pointing to, for example:
- PrefChangeRegistrar
- base::*Timer
- mojo::Receiver
- any other class member destroyed when the class is deallocated
Source: https://github.com/brave/security-action/blob/main/assets/semgrep_rules/c/chromium-uaf.yaml
Cc @thypon @goodov @iefremov