Skip to content

Commit

Permalink
[CodeHealth] APIRequestResult::TakeBody to return an rvalue
Browse files Browse the repository at this point in the history
This improvement to this function allows us to make the moving of the
value explicit. It also allows us to remove the flag indicating the
value has been moved, and actually move this validation to profiling
tools, also known as poison-after-use.

This change also removes the unnecessary uses of `TakeBody` for cases
where a const reference were enough.
  • Loading branch information
cdesouza-chromium committed Feb 27, 2025
1 parent 62c5910 commit 37715ff
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 30 deletions.
4 changes: 1 addition & 3 deletions components/api_request_helper/api_request_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ bool APIRequestResult::IsResponseCodeValid() const {
return response_code_ >= 100 && response_code_ <= 599;
}

base::Value APIRequestResult::TakeBody() {
CHECK(!body_consumed_);
body_consumed_ = true;
base::Value APIRequestResult::TakeBody() && {
return std::move(value_body_);
}

Expand Down
3 changes: 1 addition & 2 deletions components/api_request_helper/api_request_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class APIRequestResult {
int response_code() const { return response_code_; }

// Extract the sanitized response as base::Value.
base::Value TakeBody();
base::Value TakeBody() &&;

// Returns the sanitized response as base::Value.
// Note: don't clone large responses, use TakeBody() instead.
Expand Down Expand Up @@ -87,7 +87,6 @@ class APIRequestResult {
base::flat_map<std::string, std::string> headers_;
int error_code_ = -1;
GURL final_url_;
bool body_consumed_ = false;
};

struct APIRequestOptions {
Expand Down
2 changes: 1 addition & 1 deletion components/brave_news/browser/feed_fetcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void FeedFetcher::OnFetchFeedFetchedFeed(
}

base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(&ParseFeedItems, result.TakeBody()),
FROM_HERE, base::BindOnce(&ParseFeedItems, std::move(result).TakeBody()),
base::BindOnce(
[](base::WeakPtr<FeedFetcher> fetcher, std::string locale,
std::string etag, FetchFeedSourceCallback callback,
Expand Down
2 changes: 1 addition & 1 deletion components/brave_news/browser/publishers_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ void PublishersController::EnsurePublishersIsUpdating(
<< ", final_url: " << api_request_result.final_url();
// TODO(petemill): handle bad status or response
std::optional<Publishers> publisher_list =
ParseCombinedPublisherList(api_request_result.TakeBody());
ParseCombinedPublisherList(api_request_result.value_body());

// Update failed, we'll just reuse whatever publishers we had before.
if (!publisher_list) {
Expand Down
5 changes: 3 additions & 2 deletions components/brave_news/browser/suggestions_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ double GetVisitWeighting(
}

SuggestionsController::PublisherSimilarities ParseSimilarityResponse(
base::Value records_v,
const base::Value& records_v,
const std::string& locale) {
if (!records_v.is_dict()) {
return {};
Expand Down Expand Up @@ -315,7 +315,8 @@ void SuggestionsController::EnsureSimilarityMatrixIsUpdating(
controller->locale_ = locale;
controller->similarities_ =
ParseSimilarityResponse(
api_request_result.TakeBody(), locale);
api_request_result.value_body(),
locale);
controller->on_current_update_complete_
->Signal();
controller->is_update_in_progress_ = false;
Expand Down
4 changes: 2 additions & 2 deletions components/brave_news/browser/topics_fetcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ void TopicsFetcher::OnFetchedTopicArticles(

state.topic_articles_result = std::move(result);

auto topics = ParseTopics(state.topics_result.TakeBody(),
state.topic_articles_result.TakeBody());
auto topics = ParseTopics(state.topics_result.value_body(),
state.topic_articles_result.value_body());
std::move(state.callback).Run(std::move(topics));
}

Expand Down
4 changes: 2 additions & 2 deletions components/brave_vpn/browser/api/brave_vpn_api_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ void BraveVpnAPIRequest::OnGetSubscriberCredential(
APIRequestResult api_request_result) {
bool success = api_request_result.response_code() == 200;
std::string error;
std::string subscriber_credential =
ParseSubscriberCredentialFromJson(api_request_result.TakeBody(), &error);
std::string subscriber_credential = ParseSubscriberCredentialFromJson(
api_request_result.value_body(), &error);
if (!success) {
subscriber_credential = error;
VLOG(1) << __func__ << " Response from API was not HTTP 200 (Received "
Expand Down
2 changes: 1 addition & 1 deletion components/brave_vpn/browser/api/vpn_response_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace brave_vpn {

std::string ParseSubscriberCredentialFromJson(base::Value records_v,
std::string ParseSubscriberCredentialFromJson(const base::Value& records_v,
std::string* error) {
if (!records_v.is_dict()) {
VLOG(1) << __func__ << "Invalid response, could not parse JSON.";
Expand Down
2 changes: 1 addition & 1 deletion components/brave_vpn/browser/api/vpn_response_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace brave_vpn {

std::string ParseSubscriberCredentialFromJson(base::Value records_v,
std::string ParseSubscriberCredentialFromJson(const base::Value& records_v,
std::string* error);

} // namespace brave_vpn
Expand Down
27 changes: 18 additions & 9 deletions components/brave_wallet/browser/meld_integration_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ void MeldIntegrationService::OnGetServiceProviders(

base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&ParseServiceProviders, api_request_result.TakeBody()),
base::BindOnce(&ParseServiceProviders,
std::move(api_request_result).TakeBody()),
base::BindOnce(&MeldIntegrationService::OnParseServiceProviders,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
Expand Down Expand Up @@ -454,7 +455,8 @@ void MeldIntegrationService::OnGetCryptoQuotes(

base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&ParseCryptoQuotes, api_request_result.TakeBody()),
base::BindOnce(&ParseCryptoQuotes,
std::move(api_request_result).TakeBody()),
base::BindOnce(&MeldIntegrationService::OnParseCryptoQuotes,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
Expand Down Expand Up @@ -524,7 +526,8 @@ void MeldIntegrationService::OnGetPaymentMethods(

base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&ParsePaymentMethods, api_request_result.TakeBody()),
base::BindOnce(&ParsePaymentMethods,
std::move(api_request_result).TakeBody()),
base::BindOnce(&MeldIntegrationService::OnParsePaymentMethods,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
Expand Down Expand Up @@ -587,7 +590,8 @@ void MeldIntegrationService::OnGetFiatCurrencies(

base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&ParseFiatCurrencies, api_request_result.TakeBody()),
base::BindOnce(&ParseFiatCurrencies,
std::move(api_request_result).TakeBody()),
base::BindOnce(&MeldIntegrationService::OnParseFiatCurrencies,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
Expand Down Expand Up @@ -650,7 +654,8 @@ void MeldIntegrationService::OnGetCryptoCurrencies(

base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&ParseCryptoCurrencies, api_request_result.TakeBody()),
base::BindOnce(&ParseCryptoCurrencies,
std::move(api_request_result).TakeBody()),
base::BindOnce(&MeldIntegrationService::OnParseCryptoCurrencies,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
Expand Down Expand Up @@ -710,7 +715,8 @@ void MeldIntegrationService::OnGetCountries(
}

base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(&ParseCountries, api_request_result.TakeBody()),
FROM_HERE,
base::BindOnce(&ParseCountries, std::move(api_request_result).TakeBody()),
base::BindOnce(&MeldIntegrationService::OnParseCountries,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
Expand Down Expand Up @@ -784,7 +790,8 @@ void MeldIntegrationService::OnCryptoBuyWidgetCreate(

base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&ParseCryptoWidgetCreate, api_request_result.TakeBody()),
base::BindOnce(&ParseCryptoWidgetCreate,
std::move(api_request_result).TakeBody()),
base::BindOnce(&MeldIntegrationService::OnParseCryptoBuyWidgetCreate,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
Expand Down Expand Up @@ -846,7 +853,8 @@ void MeldIntegrationService::OnCryptoSellWidgetCreate(

base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&ParseCryptoWidgetCreate, api_request_result.TakeBody()),
base::BindOnce(&ParseCryptoWidgetCreate,
std::move(api_request_result).TakeBody()),
base::BindOnce(&MeldIntegrationService::OnParseCryptoSellWidgetCreate,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
Expand Down Expand Up @@ -908,7 +916,8 @@ void MeldIntegrationService::OnCryptoTransferWidgetCreate(

base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&ParseCryptoWidgetCreate, api_request_result.TakeBody()),
base::BindOnce(&ParseCryptoWidgetCreate,
std::move(api_request_result).TakeBody()),
base::BindOnce(&MeldIntegrationService::OnParseCryptoTransferWidgetCreate,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
Expand Down
12 changes: 6 additions & 6 deletions components/brave_wallet/browser/simple_hash_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ void SimpleHashClient::OnFetchNFTsFromSimpleHash(

std::optional<std::pair<std::optional<std::string>,
std::vector<mojom::BlockchainTokenPtr>>>
result = ParseNFTsFromSimpleHash(api_request_result.TakeBody(), skip_spam,
only_spam);
result = ParseNFTsFromSimpleHash(api_request_result.value_body(),
skip_spam, only_spam);
if (!result) {
std::move(callback).Run(std::move(nfts), std::nullopt);
return;
Expand Down Expand Up @@ -369,7 +369,7 @@ void SimpleHashClient::OnFetchSolCompressedNftProofData(
}

std::move(callback).Run(
ParseSolCompressedNftProofData(api_request_result.TakeBody()));
ParseSolCompressedNftProofData(api_request_result.value_body()));
}

void SimpleHashClient::GetNftBalances(
Expand Down Expand Up @@ -410,7 +410,7 @@ void SimpleHashClient::OnGetNftsForBalances(
return;
}

auto owners = ParseBalances(api_request_result.TakeBody());
auto owners = ParseBalances(api_request_result.value_body());

if (!owners) {
std::move(callback).Run(base::unexpected(InternalError()));
Expand Down Expand Up @@ -475,7 +475,7 @@ void SimpleHashClient::OnGetNftsForMetadatas(
}

// A map of NftIdentifierPtr to their metadata
auto metadatas = ParseMetadatas(api_request_result.TakeBody());
auto metadatas = ParseMetadatas(api_request_result.value_body());
if (!metadatas) {
std::move(callback).Run(base::unexpected(InternalError()));
return;
Expand Down Expand Up @@ -534,7 +534,7 @@ void SimpleHashClient::OnGetNfts(
}

auto result =
ParseNFTsFromSimpleHash(api_request_result.TakeBody(),
ParseNFTsFromSimpleHash(api_request_result.value_body(),
false /* skip_spam */, false /* only_spam */);

// Add the NFT results
Expand Down

0 comments on commit 37715ff

Please sign in to comment.