Skip to content

Commit

Permalink
Uplift of #16709 (squashed) Expose snsResolveHost api via mojom for m…
Browse files Browse the repository at this point in the history
…obile (uplift to 1.48.x) (#16827)

Co-authored-by: Kyle Hickinson <kylehickinson@users.noreply.github.com>
  • Loading branch information
nuo-xu and kylehickinson authored Jan 24, 2023
1 parent e049323 commit 3f5fa47
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 31 deletions.
8 changes: 4 additions & 4 deletions browser/net/decentralized_dns_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ void OnBeforeURLRequest_EnsRedirectWork(
void OnBeforeURLRequest_SnsRedirectWork(
const brave::ResponseCallback& next_callback,
std::shared_ptr<brave::BraveRequestInfo> ctx,
const GURL& url,
const absl::optional<GURL>& url,
brave_wallet::mojom::SolanaProviderError error,
const std::string& error_message) {
if (error == brave_wallet::mojom::SolanaProviderError::kSuccess &&
url.is_valid()) {
ctx->new_url_spec = url.spec();
if (error == brave_wallet::mojom::SolanaProviderError::kSuccess && url &&
url->is_valid()) {
ctx->new_url_spec = url->spec();
}

if (!next_callback.is_null())
Expand Down
2 changes: 1 addition & 1 deletion browser/net/decentralized_dns_network_delegate_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void OnBeforeURLRequest_EnsRedirectWork(
void OnBeforeURLRequest_SnsRedirectWork(
const brave::ResponseCallback& next_callback,
std::shared_ptr<brave::BraveRequestInfo> ctx,
const GURL& url,
const absl::optional<GURL>& url,
brave_wallet::mojom::SolanaProviderError error,
const std::string& error_message);

Expand Down
20 changes: 14 additions & 6 deletions build/ios/mojom/mojom_objc_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,11 +576,15 @@ def _ObjCToCppAssign(self, field, obj=None):
raise Exception("No typemap found for the given kind: %s" % kind)
cpp_assign = typemap.ObjCToCpp(accessor)
if mojom.IsNullableKind(kind):
if (isinstance(typemap, (StructMojoTypemap, UnionMojoTypemap))):
cpp_assign = "%s ? %s : nullptr" % (accessor, cpp_assign)
else:
if ((self._IsTypemappedKind(kind)
and not self.typemap[self._GetFullMojomNameForKind(
kind)]["nullable_is_same_type"])
or not isinstance(typemap,
(StructMojoTypemap, UnionMojoTypemap))):
cpp_assign = "%s ? absl::make_optional(%s) : absl::nullopt" % (
accessor, cpp_assign)
else:
cpp_assign = "%s ? %s : nullptr" % (accessor, cpp_assign)
return cpp_assign

def _CppToObjCAssign(self, field, prefix=None, suffix=None):
Expand All @@ -589,10 +593,14 @@ def _CppToObjCAssign(self, field, prefix=None, suffix=None):
field.name, (suffix if suffix else ""))
typemap = MojoTypemapForKind(kind)
if mojom.IsNullableKind(kind):
if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
value_accessor = accessor
else:
if ((self._IsTypemappedKind(kind)
and not self.typemap[self._GetFullMojomNameForKind(
kind)]["nullable_is_same_type"])
or not isinstance(typemap,
(StructMojoTypemap, UnionMojoTypemap))):
value_accessor = "%s.value()" % accessor
else:
value_accessor = accessor
return "%s ? %s : nil" % (
accessor, typemap.CppToObjC(value_accessor))
return typemap.CppToObjC(accessor)
Expand Down
4 changes: 2 additions & 2 deletions components/brave_wallet/browser/json_rpc_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1433,14 +1433,14 @@ void JsonRpcService::SnsResolveHost(const std::string& domain,
SnsResolveHostCallback callback) {
if (!base::FeatureList::IsEnabled(features::kBraveWalletSnsFeature)) {
std::move(callback).Run(
GURL(), mojom::SolanaProviderError::kInvalidParams,
absl::nullopt, mojom::SolanaProviderError::kInvalidParams,
l10n_util::GetStringUTF8(IDS_WALLET_INVALID_PARAMETERS));
return;
}

if (!IsValidDomain(domain)) {
std::move(callback).Run(
GURL(), mojom::SolanaProviderError::kInvalidParams,
absl::nullopt, mojom::SolanaProviderError::kInvalidParams,
l10n_util::GetStringUTF8(IDS_WALLET_INVALID_PARAMETERS));
return;
}
Expand Down
6 changes: 1 addition & 5 deletions components/brave_wallet/browser/json_rpc_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,8 @@ class JsonRpcService : public KeyedService, public mojom::JsonRpcService {
void SnsGetSolAddr(const std::string& domain,
SnsGetSolAddrCallback callback) override;

using SnsResolveHostCallback =
base::OnceCallback<void(const GURL& url,
mojom::SolanaProviderError error,
const std::string& error_message)>;
void SnsResolveHost(const std::string& domain,
SnsResolveHostCallback callback);
SnsResolveHostCallback callback) override;

bool SetNetwork(const std::string& chain_id,
mojom::CoinType coin,
Expand Down
29 changes: 16 additions & 13 deletions components/brave_wallet/browser/json_rpc_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5499,17 +5499,18 @@ TEST_F(SnsJsonRpcServiceUnitTest, GetWalletAddr_SolRecordOwner) {

TEST_F(SnsJsonRpcServiceUnitTest, ResolveHost_UrlValue) {
base::MockCallback<JsonRpcService::SnsResolveHostCallback> callback;
EXPECT_CALL(callback,
Run(url_value(), mojom::SolanaProviderError::kSuccess, ""));
EXPECT_CALL(callback, Run(testing::Eq(url_value()),
mojom::SolanaProviderError::kSuccess, ""));
json_rpc_service_->SnsResolveHost(sns_host(), callback.Get());
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(&callback);

// HTTP error for url record account. Fail resolution.
url_record_address_handler_->FailWithTimeout();
EXPECT_CALL(callback,
Run(GURL(), mojom::SolanaProviderError::kInternalError,
l10n_util::GetStringUTF8(IDS_WALLET_INTERNAL_ERROR)));
EXPECT_CALL(
callback,
Run(testing::Eq(GURL()), mojom::SolanaProviderError::kInternalError,
l10n_util::GetStringUTF8(IDS_WALLET_INTERNAL_ERROR)));
json_rpc_service_->SnsResolveHost(sns_host(), callback.Get());
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(&callback);
Expand All @@ -5521,27 +5522,29 @@ TEST_F(SnsJsonRpcServiceUnitTest, ResolveHost_IpfsValue) {

// No url record. Will return ipfs record.
base::MockCallback<JsonRpcService::SnsResolveHostCallback> callback;
EXPECT_CALL(callback,
Run(ipfs_value(), mojom::SolanaProviderError::kSuccess, ""));
EXPECT_CALL(callback, Run(testing::Eq(ipfs_value()),
mojom::SolanaProviderError::kSuccess, ""));
json_rpc_service_->SnsResolveHost(sns_host(), callback.Get());
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(&callback);

// HTTP error for ipfs record account. Fail resolution.
ipfs_record_address_handler_->FailWithTimeout();
EXPECT_CALL(callback,
Run(GURL(), mojom::SolanaProviderError::kInternalError,
l10n_util::GetStringUTF8(IDS_WALLET_INTERNAL_ERROR)));
EXPECT_CALL(
callback,
Run(testing::Eq(GURL()), mojom::SolanaProviderError::kInternalError,
l10n_util::GetStringUTF8(IDS_WALLET_INTERNAL_ERROR)));
json_rpc_service_->SnsResolveHost(sns_host(), callback.Get());
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(&callback);
ipfs_record_address_handler_->FailWithTimeout(false);

// No ipfs record account. Fail resolution.
ipfs_record_address_handler_->Disable();
EXPECT_CALL(callback,
Run(GURL(), mojom::SolanaProviderError::kInternalError,
l10n_util::GetStringUTF8(IDS_WALLET_INTERNAL_ERROR)));
EXPECT_CALL(
callback,
Run(testing::Eq(GURL()), mojom::SolanaProviderError::kInternalError,
l10n_util::GetStringUTF8(IDS_WALLET_INTERNAL_ERROR)));
json_rpc_service_->SnsResolveHost(sns_host(), callback.Get());
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(&callback);
Expand Down
3 changes: 3 additions & 0 deletions components/brave_wallet/common/brave_wallet.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,9 @@ interface JsonRpcService {
// SNS lookups
SnsGetSolAddr(string domain) => (string address, SolanaProviderError error, string error_message);

// SNS Domain lookups
SnsResolveHost(string domain) => (url.mojom.Url? url, SolanaProviderError error, string error_message);

// Unstoppable Domains lookups
UnstoppableDomainsGetWalletAddr(string domain, BlockchainToken? token) => (string address, ProviderError error, string error_message);

Expand Down

0 comments on commit 3f5fa47

Please sign in to comment.