Skip to content

Commit

Permalink
encapsulate rust json code in cpp (#25975)
Browse files Browse the repository at this point in the history
encapsulate rust json code in cpp so rust managed memory stays within the component
  • Loading branch information
bridiver authored Oct 15, 2024
1 parent 7a362dd commit a327ced
Show file tree
Hide file tree
Showing 23 changed files with 422 additions and 410 deletions.
4 changes: 2 additions & 2 deletions components/brave_wallet/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static_library("browser") {
"//brave/components/decentralized_dns/core",
"//brave/components/filecoin/rs:rust_lib",
"//brave/components/ipfs",
"//brave/components/json/rs:rust_lib",
"//brave/components/json",
"//brave/components/p3a_utils",
"//brave/components/resources:strings_grit",
"//brave/components/services/brave_wallet/public/cpp",
Expand Down Expand Up @@ -458,7 +458,7 @@ source_set("transaction") {
"//brave/components/brave_wallet/common",
"//brave/components/brave_wallet/common:mojom",
"//brave/components/filecoin/rs:rust_lib",
"//brave/components/json/rs:rust_lib",
"//brave/components/json",
]
}

Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/bitcoin/bitcoin_rpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "brave/components/brave_wallet/browser/json_rpc_response_parser.h"
#include "brave/components/brave_wallet/browser/network_manager.h"
#include "brave/components/brave_wallet/common/features.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"
#include "components/grit/brave_components_strings.h"
#include "net/http/http_request_headers.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "base/test/values_test_util.h"
#include "brave/components/brave_wallet/browser/bitcoin/bitcoin_serializer.h"
#include "brave/components/brave_wallet/browser/bitcoin_rpc_responses.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand Down Expand Up @@ -88,7 +88,7 @@ TEST(BitcoinTransaction, TxInput_FromRpcUtxo) {
)";

auto rpc_utxo = bitcoin_rpc::UnspentOutput::FromValue(base::test::ParseJson(
std::string(json::convert_all_numbers_to_string(rpc_utxo_json, ""))));
json::convert_all_numbers_to_string(rpc_utxo_json, "")));
ASSERT_TRUE(rpc_utxo);

auto input = BitcoinTransaction::TxInput::FromRpcUtxo(kAddress1, *rpc_utxo);
Expand Down
5 changes: 2 additions & 3 deletions components/brave_wallet/browser/blockchain_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "brave/components/brave_wallet/browser/network_manager.h"
#include "brave/components/brave_wallet/browser/wallet_data_files_installer.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"
#include "net/base/url_util.h"
#include "services/data_decoder/public/cpp/json_sanitizer.h"

Expand Down Expand Up @@ -171,8 +171,7 @@ void DoParseDappLists(const base::FilePath& dir, ParseListsResult& out) {
return;
}

auto converted_json =
std::string(json::convert_all_numbers_to_string(*result, ""));
auto converted_json = json::convert_all_numbers_to_string(*result, "");
if (converted_json.empty()) {
return;
}
Expand Down
18 changes: 5 additions & 13 deletions components/brave_wallet/browser/fil_requests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "brave/components/brave_wallet/browser/fil_transaction.h"
#include "brave/components/brave_wallet/browser/json_rpc_requests_helper.h"
#include "brave/components/brave_wallet/common/fil_address.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"

namespace brave_wallet::fil {

Expand Down Expand Up @@ -72,15 +72,9 @@ std::string getEstimateGas(const std::string& from_address,
dict.Set("id", 1);
std::string json;
base::JSONWriter::Write(dict, &json);
json = std::string(json::convert_string_value_to_int64("/params/0/GasLimit",
json.c_str(), false)
.c_str());
json = std::string(json::convert_string_value_to_uint64("/params/0/Nonce",
json.c_str(), false)
.c_str());
return std::string(json::convert_string_value_to_uint64("/params/0/Method",
json.c_str(), false)
.c_str());
json = json::convert_string_value_to_int64("/params/0/GasLimit", json, false);
json = json::convert_string_value_to_uint64("/params/0/Nonce", json, false);
return json::convert_string_value_to_uint64("/params/0/Method", json, false);
}

std::string getChainHead() {
Expand All @@ -93,9 +87,7 @@ std::string getStateSearchMsgLimited(const std::string& cid, uint64_t period) {
auto result =
GetJsonRpcString("Filecoin.StateSearchMsgLimited", std::move(cid_value),
base::Value(std::to_string(period)));
result = std::string(
json::convert_string_value_to_uint64("/params/1", result.c_str(), false)
.c_str());
result = json::convert_string_value_to_uint64("/params/1", result, false);
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/fil_response_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include "base/strings/string_number_conversions.h"
#include "brave/components/brave_wallet/browser/json_rpc_response_parser.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"

namespace brave_wallet {

Expand Down
21 changes: 8 additions & 13 deletions components/brave_wallet/browser/fil_transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "base/strings/string_util.h"
#include "base/values.h"
#include "brave/components/filecoin/rs/src/lib.rs.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"

namespace brave_wallet {

Expand Down Expand Up @@ -222,12 +222,10 @@ std::optional<std::string> FilTransaction::ConvertMesssageStringFieldsToInt64(
std::string converted_json =
json::convert_string_value_to_int64(path + "/GasLimit", json, true)
.c_str();
converted_json = json::convert_string_value_to_uint64(
path + "/Nonce", converted_json.c_str(), true)
.c_str();
converted_json = json::convert_string_value_to_uint64(
path + "/Method", converted_json.c_str(), true)
.c_str();
converted_json = json::convert_string_value_to_uint64(path + "/Nonce",
converted_json, true);
converted_json = json::convert_string_value_to_uint64(path + "/Method",
converted_json, true);
if (converted_json.empty()) {
return std::nullopt;
}
Expand All @@ -245,12 +243,9 @@ std::optional<std::string> FilTransaction::ConvertSignedTxStringFieldsToInt64(
std::optional<base::Value> FilTransaction::DeserializeSignedTx(
const std::string& signed_tx) {
std::string json =
json::convert_int64_value_to_string("/Message/GasLimit", signed_tx, true)
.c_str();
json =
json::convert_int64_value_to_string("/Message/Nonce", json, true).c_str();
json = json::convert_int64_value_to_string("/Message/Method", json, true)
.c_str();
json::convert_int64_value_to_string("/Message/GasLimit", signed_tx, true);
json = json::convert_int64_value_to_string("/Message/Nonce", json, true);
json = json::convert_int64_value_to_string("/Message/Method", json, true);
return base::JSONReader::Read(json);
}

Expand Down
9 changes: 4 additions & 5 deletions components/brave_wallet/browser/json_rpc_response_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/hex_utils.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"

namespace brave_wallet {

Expand Down Expand Up @@ -143,8 +143,8 @@ std::optional<std::string> ConvertMultiUint64InObjectArrayToString(
if (key.empty()) {
return std::nullopt;
}
converted_json = std::string(json::convert_uint64_in_object_array_to_string(
path_to_list, path_to_object, key, converted_json));
converted_json = json::convert_uint64_in_object_array_to_string(
path_to_list, path_to_object, key, converted_json);
if (converted_json.empty()) {
return std::nullopt;
}
Expand Down Expand Up @@ -195,8 +195,7 @@ bool GetUint64FromDictValue(const base::Value::Dict& dict_value,

std::optional<std::string> ConvertAllNumbersToString(const std::string& path,
const std::string& json) {
auto converted_json =
std::string(json::convert_all_numbers_to_string(json, path));
auto converted_json = json::convert_all_numbers_to_string(json, path);
if (converted_json.empty()) {
return std::nullopt;
}
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/json_rpc_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#include "brave/components/brave_wallet/common/hex_utils.h"
#include "brave/components/decentralized_dns/core/constants.h"
#include "brave/components/decentralized_dns/core/utils.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"
#include "brave/net/base/url_util.h"
#include "components/grit/brave_components_strings.h"
#include "components/prefs/pref_service.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "brave/components/brave_wallet/common/buildflags.h"
#include "brave/components/brave_wallet/common/meld_integration.mojom-forward.h"
#include "brave/components/constants/brave_services_key.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"
#include "net/base/url_util.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/simple_hash_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "brave/components/brave_wallet/common/solana_utils.h"
#include "brave/components/brave_wallet/common/string_utils.h"
#include "brave/components/constants/brave_services_key.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"
#include "net/base/url_util.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

Expand Down
6 changes: 3 additions & 3 deletions components/brave_wallet/browser/solana_requests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "base/strings/string_number_conversions.h"
#include "brave/components/brave_wallet/browser/json_rpc_requests_helper.h"
#include "brave/components/brave_wallet/common/solana_utils.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"

namespace brave_wallet::solana {

Expand Down Expand Up @@ -51,8 +51,8 @@ std::string sendTransaction(

base::Value::Dict dictionary =
GetJsonRpcDictionary("sendTransaction", std::move(params));
return std::string(json::convert_string_value_to_uint64(
"/params/1/maxRetries", GetJSON(dictionary), true));
return json::convert_string_value_to_uint64("/params/1/maxRetries",
GetJSON(dictionary), true);
}

std::string getLatestBlockhash() {
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/solana_response_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "base/values.h"
#include "brave/components/brave_wallet/browser/json_rpc_response_parser.h"
#include "brave/components/brave_wallet/common/brave_wallet_types.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"

namespace brave_wallet {

Expand Down
14 changes: 7 additions & 7 deletions components/brave_wallet/browser/swap_request_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/encoding_utils.h"
#include "brave/components/brave_wallet/common/hex_utils.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"

namespace brave_wallet {

Expand Down Expand Up @@ -121,18 +121,18 @@ std::optional<std::string> EncodeTransactionParams(
// FIXME - GetJSON should be refactored to accept a base::Value::Dict
std::string result = GetJSON(base::Value(std::move(tx_params)));

result = std::string(json::convert_string_value_to_uint64(
"/quoteResponse/slippageBps", result, false));
result = json::convert_string_value_to_uint64("/quoteResponse/slippageBps",
result, false);

if (params.quote->platform_fee) {
result = std::string(json::convert_string_value_to_uint64(
"/quoteResponse/platformFee/feeBps", result, false));
result = json::convert_string_value_to_uint64(
"/quoteResponse/platformFee/feeBps", result, false);
}

for (int i = 0; i < static_cast<int>(params.quote->route_plan.size()); i++) {
result = std::string(json::convert_string_value_to_uint64(
result = json::convert_string_value_to_uint64(
base::StringPrintf("/quoteResponse/routePlan/%d/percent", i), result,
false));
false);
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/swap_response_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "brave/components/brave_wallet/common/hex_utils.h"
#include "brave/components/brave_wallet/common/string_utils.h"
#include "brave/components/json/rs/src/lib.rs.h"
#include "brave/components/json/json_helper.h"

namespace brave_wallet {

Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ source_set("brave_wallet_unit_tests") {
"//brave/components/decentralized_dns/core",
"//brave/components/filecoin/rs:rust_lib",
"//brave/components/ipfs",
"//brave/components/json/rs:rust_lib",
"//brave/components/json",
"//brave/components/resources:strings_grit",
"//brave/components/services/brave_wallet/public/cpp/utils",
"//brave/components/services/brave_wallet/public/mojom",
Expand Down
20 changes: 18 additions & 2 deletions components/json/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,27 @@

import("//testing/test.gni")

component("json") {
sources = [
"json_export.h",
"json_helper.cc",
"json_helper.h",
]

defines = [ "JSON_HELPER_IMPLEMENTATION" ]

deps = [
"rs:rust_lib",
"//base",
]
}

source_set("brave_json_unit_tests") {
testonly = true
sources = [ "//brave/components/json/json_parser_unittest.cc" ]
deps = [
"//brave/components/json/rs:rust_lib",
"//base",
"//brave/components/json",
"//testing/gtest",
]
} # source_set("brave_wallet_unit_tests")
}
30 changes: 30 additions & 0 deletions components/json/json_export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_COMPONENTS_JSON_JSON_EXPORT_H_
#define BRAVE_COMPONENTS_JSON_JSON_EXPORT_H_

#if defined(COMPONENT_BUILD)
#if defined(WIN32)

#if defined(JSON_HELPER_IMPLEMENTATION)
#define JSON_HELPER_EXPORT __declspec(dllexport)
#else
#define JSON_HELPER_EXPORT __declspec(dllimport)
#endif // defined(JSON_HELPER_IMPLEMENTATION)

#else // defined(WIN32)
#if defined(JSON_HELPER_IMPLEMENTATION)
#define JSON_HELPER_EXPORT __attribute__((visibility("default")))
#else
#define JSON_HELPER_EXPORT
#endif // defined(JSON_HELPER_IMPLEMENTATION)
#endif

#else // defined(COMPONENT_BUILD)
#define JSON_HELPER_EXPORT
#endif

#endif // BRAVE_COMPONENTS_JSON_JSON_EXPORT_H_
Loading

0 comments on commit a327ced

Please sign in to comment.