Skip to content

Commit

Permalink
Merge pull request #9527 from brave/wallet-mojo
Browse files Browse the repository at this point in the history
use mojo for wallet controllers
  • Loading branch information
bridiver committed Jul 23, 2021
2 parents 51cbfc0 + c4cf911 commit 9ca35e9
Show file tree
Hide file tree
Showing 62 changed files with 1,169 additions and 975 deletions.
15 changes: 6 additions & 9 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ using extensions::ChromeContentBrowserClientExtensionsPart;

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/browser/brave_wallet/rpc_controller_factory.h"
#include "brave/components/brave_wallet/browser/brave_wallet_provider_impl.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#if !defined(OS_ANDROID)
Expand Down Expand Up @@ -207,19 +206,17 @@ void BindCosmeticFiltersResources(
void MaybeBindBraveWalletProvider(
content::RenderFrameHost* const frame_host,
mojo::PendingReceiver<brave_wallet::mojom::BraveWalletProvider> receiver) {
auto* context = frame_host->GetBrowserContext();
if (!brave_wallet::IsAllowedForContext(context))
return;
auto* rpc_controller = brave_wallet::RpcControllerFactory::GetForContext(
frame_host->GetBrowserContext());

brave_wallet::BraveWalletService* service =
brave_wallet::BraveWalletServiceFactory::GetInstance()->GetForContext(
context);
if (!rpc_controller)
return;

content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(frame_host);
mojo::MakeSelfOwnedReceiver(
std::make_unique<brave_wallet::BraveWalletProviderImpl>(
service->AsWeakPtr(),
rpc_controller,
#if defined(OS_ANDROID)
std::make_unique<
brave_wallet::BraveWalletProviderDelegateImplAndroid>(
Expand Down
4 changes: 2 additions & 2 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
#endif

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/browser/keyring_controller.h"
#endif

#if BUILDFLAG(ETHEREUM_REMOTE_CLIENT_ENABLED)
Expand Down Expand Up @@ -331,7 +331,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {

// Brave Wallet
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
brave_wallet::BraveWalletService::RegisterProfilePrefs(registry);
brave_wallet::KeyringController::RegisterProfilePrefs(registry);
#endif

// Brave Search
Expand Down
11 changes: 9 additions & 2 deletions browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ source_set("brave_wallet") {
check_includes = false

sources = [
"asset_ratio_controller_factory.cc",
"asset_ratio_controller_factory.h",
"brave_wallet_context_utils.cc",
"brave_wallet_context_utils.h",
"brave_wallet_service_factory.cc",
"brave_wallet_service_factory.h",
"keyring_controller_factory.cc",
"keyring_controller_factory.h",
"rpc_controller_factory.cc",
"rpc_controller_factory.h",
"swap_controller_factory.cc",
"swap_controller_factory.h",
]
deps = [
"//brave/components/brave_wallet/browser",
"//brave/components/brave_wallet/common",
"//brave/components/brave_wallet/common:mojom",
"//chrome/browser/profiles:profiles",
"//components/keyed_service/content:content",
"//components/user_prefs",
Expand Down
179 changes: 101 additions & 78 deletions browser/brave_wallet/android/brave_wallet_native_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,14 @@
#include "base/android/jni_string.h"
#include "base/json/json_writer.h"
#include "base/values.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/browser/brave_wallet/asset_ratio_controller_factory.h"
#include "brave/browser/brave_wallet/keyring_controller_factory.h"
#include "brave/build/android/jni_headers/BraveWalletNativeWorker_jni.h"
#include "brave/components/brave_wallet/browser/asset_ratio_controller.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/browser/hd_keyring.h"
#include "brave/components/brave_wallet/browser/keyring_controller.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"

namespace {

brave_wallet::BraveWalletService* GetBraveWalletService() {
return brave_wallet::BraveWalletServiceFactory::GetForContext(
ProfileManager::GetActiveUserProfile()->GetOriginalProfile());
}

} // namespace
#include "mojo/public/cpp/bindings/pending_receiver.h"

namespace chrome {
namespace android {
Expand All @@ -40,6 +31,37 @@ BraveWalletNativeWorker::BraveWalletNativeWorker(

BraveWalletNativeWorker::~BraveWalletNativeWorker() {}

void BraveWalletNativeWorker::EnsureConnected() {
auto* profile = ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
if (!keyring_controller_) {
auto pending =
brave_wallet::KeyringControllerFactory::GetInstance()->GetForContext(
profile);
keyring_controller_.Bind(std::move(pending));
}
DCHECK(keyring_controller_);
keyring_controller_.set_disconnect_handler(
base::BindOnce(&BraveWalletNativeWorker::OnConnectionError,
weak_ptr_factory_.GetWeakPtr()));

if (!asset_ratio_controller_) {
auto pending =
brave_wallet::AssetRatioControllerFactory::GetInstance()->GetForContext(
profile);
asset_ratio_controller_.Bind(std::move(pending));
}
DCHECK(asset_ratio_controller_);
asset_ratio_controller_.set_disconnect_handler(
base::BindOnce(&BraveWalletNativeWorker::OnConnectionError,
weak_ptr_factory_.GetWeakPtr()));
}

void BraveWalletNativeWorker::OnConnectionError() {
keyring_controller_.reset();
asset_ratio_controller_.reset();
EnsureConnected();
}

void BraveWalletNativeWorker::Destroy(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller) {
Expand All @@ -48,80 +70,86 @@ void BraveWalletNativeWorker::Destroy(

base::android::ScopedJavaLocalRef<jstring>
BraveWalletNativeWorker::GetRecoveryWords(JNIEnv* env) {
auto* keyring_controller = GetBraveWalletService()->keyring_controller();
keyring_controller->GetMnemonicForDefaultKeyring();
return base::android::ConvertUTF8ToJavaString(
env, keyring_controller->GetMnemonicForDefaultKeyring());
EnsureConnected();

auto callback = base::BindOnce([](const std::string& mnemonic) {});
keyring_controller_->GetMnemonicForDefaultKeyring(std::move(callback));

// TODO(bridiver) - convert this to callback
return base::android::ConvertUTF8ToJavaString(env, "");
}

bool BraveWalletNativeWorker::IsWalletLocked(JNIEnv* env) {
auto* keyring_controller = GetBraveWalletService()->keyring_controller();
return keyring_controller->IsLocked();
EnsureConnected();
// return keyring_controller_->IsLocked();
return false;
}

base::android::ScopedJavaLocalRef<jstring>
BraveWalletNativeWorker::CreateWallet(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& password) {
auto* keyring_controller = GetBraveWalletService()->keyring_controller();
auto* keyring = keyring_controller->CreateDefaultKeyring(
base::android::ConvertJavaStringToUTF8(env, password));
if (keyring) {
keyring->AddAccounts();
}
EnsureConnected();

return base::android::ConvertUTF8ToJavaString(
env, keyring_controller->GetMnemonicForDefaultKeyring());
auto callback = base::BindOnce([](const std::string& mnemonic) {});
keyring_controller_->CreateWallet(
base::android::ConvertJavaStringToUTF8(env, password),
std::move(callback));

// TODO(bridiver) - convert this to callback
return base::android::ConvertUTF8ToJavaString(env, "");
}

void BraveWalletNativeWorker::LockWallet(JNIEnv* env) {
auto* keyring_controller = GetBraveWalletService()->keyring_controller();
keyring_controller->Lock();
EnsureConnected();
keyring_controller_->Lock();
}

bool BraveWalletNativeWorker::UnlockWallet(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& password) {
auto* keyring_controller = GetBraveWalletService()->keyring_controller();
EnsureConnected();

auto callback = base::BindOnce([](bool unlocked) {});
keyring_controller_->Unlock(
base::android::ConvertJavaStringToUTF8(env, password),
std::move(callback));

return keyring_controller->Unlock(
base::android::ConvertJavaStringToUTF8(env, password));
// TODO(bridiver) - convert to callback
return true;
}

base::android::ScopedJavaLocalRef<jstring>
BraveWalletNativeWorker::RestoreWallet(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& mnemonic,
const base::android::JavaParamRef<jstring>& password) {
auto* keyring_controller = GetBraveWalletService()->keyring_controller();
auto* keyring = keyring_controller->RestoreDefaultKeyring(
EnsureConnected();
auto callback = base::BindOnce([](bool is_valid) {});
keyring_controller_->RestoreWallet(
base::android::ConvertJavaStringToUTF8(env, mnemonic),
base::android::ConvertJavaStringToUTF8(env, password));
if (keyring) {
keyring->AddAccounts();
}
return base::android::ConvertUTF8ToJavaString(
env, keyring_controller->GetMnemonicForDefaultKeyring());
base::android::ConvertJavaStringToUTF8(env, password),
std::move(callback));

// TODO(bridiver) - convert to callback and this should return true/false not
// the mnemonic
return base::android::ConvertUTF8ToJavaString(env, "");
}

void BraveWalletNativeWorker::ResetWallet(JNIEnv* env) {
auto* keyring_controller = GetBraveWalletService()->keyring_controller();
keyring_controller->Reset();
EnsureConnected();
keyring_controller_->Reset();
}

void BraveWalletNativeWorker::GetAssetPrice(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& asset) {
brave_wallet::BraveWalletService* brave_wallet_service =
GetBraveWalletService();
if (brave_wallet_service) {
auto* asset_ratio_controller =
brave_wallet_service->asset_ratio_controller();
asset_ratio_controller->GetPrice(
base::android::ConvertJavaStringToUTF8(env, asset),
base::BindOnce(&BraveWalletNativeWorker::OnGetPrice,
weak_ptr_factory_.GetWeakPtr()));
}
EnsureConnected();

asset_ratio_controller_->GetPrice(
base::android::ConvertJavaStringToUTF8(env, asset),
base::BindOnce(&BraveWalletNativeWorker::OnGetPrice,
weak_ptr_factory_.GetWeakPtr()));
}

void BraveWalletNativeWorker::OnGetPrice(bool success,
Expand All @@ -136,34 +164,29 @@ void BraveWalletNativeWorker::GetAssetPriceHistory(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& asset,
const jint timeFrameType) {
brave_wallet::BraveWalletService* brave_wallet_service =
GetBraveWalletService();
if (brave_wallet_service) {
auto* asset_ratio_controller =
brave_wallet_service->asset_ratio_controller();

brave_wallet::mojom::AssetPriceTimeframe time_frame;
if (timeFrameType == 0) {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::Live;
} else if (timeFrameType == 1) {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::OneDay;
} else if (timeFrameType == 2) {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::OneWeek;
} else if (timeFrameType == 3) {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::OneMonth;
} else if (timeFrameType == 4) {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::ThreeMonths;
} else if (timeFrameType == 5) {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::OneYear;
} else {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::All;
}

asset_ratio_controller->GetPriceHistory(
base::android::ConvertJavaStringToUTF8(env, asset), time_frame,
base::BindOnce(&BraveWalletNativeWorker::OnGetPriceHistory,
weak_ptr_factory_.GetWeakPtr()));
EnsureConnected();

brave_wallet::mojom::AssetPriceTimeframe time_frame;
if (timeFrameType == 0) {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::Live;
} else if (timeFrameType == 1) {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::OneDay;
} else if (timeFrameType == 2) {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::OneWeek;
} else if (timeFrameType == 3) {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::OneMonth;
} else if (timeFrameType == 4) {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::ThreeMonths;
} else if (timeFrameType == 5) {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::OneYear;
} else {
time_frame = brave_wallet::mojom::AssetPriceTimeframe::All;
}

asset_ratio_controller_->GetPriceHistory(
base::android::ConvertJavaStringToUTF8(env, asset), time_frame,
base::BindOnce(&BraveWalletNativeWorker::OnGetPriceHistory,
weak_ptr_factory_.GetWeakPtr()));
}

void BraveWalletNativeWorker::OnGetPriceHistory(
Expand Down
8 changes: 8 additions & 0 deletions browser/brave_wallet/android/brave_wallet_native_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/android/jni_weak_ref.h"
#include "base/memory/weak_ptr.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace chrome {
namespace android {
Expand Down Expand Up @@ -51,6 +52,13 @@ class BraveWalletNativeWorker {
std::vector<brave_wallet::mojom::AssetTimePricePtr> values);

private:
void EnsureConnected();
void OnConnectionError();

mojo::Remote<brave_wallet::mojom::KeyringController> keyring_controller_;
mojo::Remote<brave_wallet::mojom::AssetRatioController>
asset_ratio_controller_;

JavaObjectWeakGlobalRef weak_java_brave_wallet_native_worker_;
base::WeakPtrFactory<BraveWalletNativeWorker> weak_ptr_factory_;
};
Expand Down
Loading

0 comments on commit 9ca35e9

Please sign in to comment.