Skip to content

Commit

Permalink
[cr132] JSONReader using rust's implementation by default
Browse files Browse the repository at this point in the history
There were some overrides in place to enforce the use of the rust parser
for `APIRequestHelper` and similar APIs. However, the rust parser is now
switched by default in upstream. This change does away with the
overrides that were added, and uses the regular `JSONReader` interface.

Chromium change:
https://chromium.googlesource.com/chromium/src/+/9ddc1624637c8cfa8ef50a95abd779e0ba4d67f6

commit 9ddc1624637c8cfa8ef50a95abd779e0ba4d67f6
Author: danakj <[email protected]>
Date:   Thu Nov 7 22:14:14 2024 +0000

    Make rust toolchain non-optional in chromium and move flags out of build

    The //build dir should contain things that apply to all projects using
    the directly. Moving the gn args for specific chromium projects out of
    //build/config/rust.gni into the Chromium tree next to the features they
    are flags for.

    Removed the flags that now default to on. They were gated on enable_rust
    but enable_rust is always true in Chromium, so in Chromium code we can
    just assume it's true. Thus the features are always enabled. Removed
    buildflags etc for these removed GN args.

    Gating Rust in //base on IS_NACL rather than rust-specific toolchain
    flags, as Rust is always available in Chromium in all of our build
    configurations except NaCl (and it will be going away in under a year
    now).
  • Loading branch information
cdesouza-chromium committed Nov 10, 2024
1 parent 9c05bff commit 2ba3994
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 60 deletions.
17 changes: 0 additions & 17 deletions chromium_src/base/json/json_reader.cc

This file was deleted.

20 changes: 0 additions & 20 deletions chromium_src/base/json/json_reader.h

This file was deleted.

37 changes: 18 additions & 19 deletions components/api_request_helper/api_request_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/ranges/algorithm.h"
#include "base/rust_buildflags.h"
#include "base/strings/string_split.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
Expand All @@ -32,27 +31,24 @@
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/mojom/url_response_head.mojom.h"

static_assert(BUILDFLAG(BUILD_RUST_JSON_READER),
"To use Rust sanitizer BUILD_RUST_JSON_READER should be enabled");

namespace api_request_helper {

namespace {

const unsigned int kRetriesCountOnNetworkChange = 1;

BASE_FEATURE(kUseBraveRustJSONSanitizer,
"UseBraveRustJSONSanitizer",
base::FEATURE_ENABLED_BY_DEFAULT);

void ParseJsonUsingRust(
void ParseJsonInWorkerTaskRunner(
std::string json,
data_decoder::DataDecoder::ValueParseCallback callback,
const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
base::SequencedTaskRunner* task_runner,
data_decoder::DataDecoder::ValueParseCallback callback) {
task_runner->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&base::DecodeJSONInRust, std::move(json),
base::JSON_PARSE_RFC),
base::BindOnce(
[](std::string json) {
return base::JSONReader::ReadAndReturnValueWithError(
json, base::JSON_PARSE_RFC);
},
std::move(json)),
base::BindOnce(
[](data_decoder::DataDecoder::ValueParseCallback callback,
base::JSONReader::Result result) {
Expand All @@ -63,6 +59,7 @@ void ParseJsonUsingRust(
}
},
std::move(callback)));
return;
}

scoped_refptr<base::SequencedTaskRunner> MakeDecoderTaskRunner() {
Expand Down Expand Up @@ -390,8 +387,9 @@ void APIRequestHelper::URLLoaderHandler::send_sse_data_for_testing(
void APIRequestHelper::URLLoaderHandler::ParseJsonImpl(
std::string json,
base::OnceCallback<void(ValueOrError)> callback) {
if (base::FeatureList::IsEnabled(kUseBraveRustJSONSanitizer)) {
ParseJsonUsingRust(std::move(json), std::move(callback), task_runner_);
if (base::JSONReader::UsingRust()) {
ParseJsonInWorkerTaskRunner(std::move(json), task_runner_.get(),
std::move(callback));
return;
}

Expand Down Expand Up @@ -591,11 +589,12 @@ void APIRequestHelper::SetUrlLoaderFactoryForTesting(
url_loader_factory_ = std::move(url_loader_factory);
}

void SanitizeAndParseJson(std::string json,
void ParseJsonNonBlocking(std::string json,
base::OnceCallback<void(ValueOrError)> callback) {
if (base::FeatureList::IsEnabled(kUseBraveRustJSONSanitizer)) {
ParseJsonUsingRust(std::move(json), std::move(callback),
MakeDecoderTaskRunner());
if (base::JSONReader::UsingRust()) {
auto task_runner = MakeDecoderTaskRunner();
ParseJsonInWorkerTaskRunner(std::move(json), task_runner.get(),
std::move(callback));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion components/api_request_helper/api_request_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class APIRequestHelper {
base::WeakPtrFactory<APIRequestHelper> weak_ptr_factory_{this};
};

void SanitizeAndParseJson(std::string json,
void ParseJsonNonBlocking(std::string json,
base::OnceCallback<void(ValueOrError)> callback);

} // namespace api_request_helper
Expand Down
2 changes: 1 addition & 1 deletion components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ void RewardsServiceImpl::OnURLLoaderComplete(
[](std::unique_ptr<std::string> response_body,
LoadURLCallback callback, mojom::UrlResponsePtr response,
scoped_refptr<base::SequencedTaskRunner> post_response_runner) {
api_request_helper::SanitizeAndParseJson(
api_request_helper::ParseJsonNonBlocking(
*response_body,
base::BindOnce(
[](LoadURLCallback callback,
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/ethereum_provider_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ void EthereumProviderImpl::Decrypt(
return;
}

api_request_helper::SanitizeAndParseJson(
api_request_helper::ParseJsonNonBlocking(
untrusted_encrypted_data_json,
base::BindOnce(&EthereumProviderImpl::ContinueDecryptWithSanitizedJson,
weak_factory_.GetWeakPtr(), std::move(callback),
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/nft_metadata_fetcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void NftMetadataFetcher::FetchMetadata(
}

// Sanitize JSON
api_request_helper::SanitizeAndParseJson(
api_request_helper::ParseJsonNonBlocking(
std::move(metadata_json),
base::BindOnce(&NftMetadataFetcher::OnSanitizeTokenMetadata,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
Expand Down

0 comments on commit 2ba3994

Please sign in to comment.