Add BraveWalletService

This commit is contained in:
Jocelyn Liu
2021-09-10 11:54:33 -07:00
parent 4d9222789a
commit a82c98f511
8 changed files with 185 additions and 0 deletions
+2
View File
@@ -12,6 +12,8 @@ source_set("brave_wallet") {
"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",
"eth_tx_controller_factory.cc",
"eth_tx_controller_factory.h",
"keyring_controller_factory.cc",
@@ -0,0 +1,60 @@
/* Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/. */
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/user_prefs/user_prefs.h"
namespace brave_wallet {
// static
BraveWalletServiceFactory* BraveWalletServiceFactory::GetInstance() {
return base::Singleton<BraveWalletServiceFactory>::get();
}
// static
mojo::PendingRemote<mojom::BraveWalletService>
BraveWalletServiceFactory::GetForContext(content::BrowserContext* context) {
if (!IsAllowedForContext(context)) {
return mojo::PendingRemote<mojom::BraveWalletService>();
}
return static_cast<BraveWalletService*>(
GetInstance()->GetServiceForBrowserContext(context, true))
->MakeRemote();
}
// static
BraveWalletService* BraveWalletServiceFactory::GetServiceForContext(
content::BrowserContext* context) {
if (!IsAllowedForContext(context)) {
return nullptr;
}
return static_cast<BraveWalletService*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}
BraveWalletServiceFactory::BraveWalletServiceFactory()
: BrowserContextKeyedServiceFactory(
"BraveWalletService",
BrowserContextDependencyManager::GetInstance()) {}
BraveWalletServiceFactory::~BraveWalletServiceFactory() = default;
KeyedService* BraveWalletServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new BraveWalletService(user_prefs::UserPrefs::Get(context));
}
content::BrowserContext* BraveWalletServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}
} // namespace brave_wallet
@@ -0,0 +1,46 @@
/* Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/. */
#ifndef BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_SERVICE_FACTORY_H_
#define BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_SERVICE_FACTORY_H_
#include "base/memory/singleton.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/browser_context.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
namespace brave_wallet {
class BraveWalletService;
class BraveWalletServiceFactory : public BrowserContextKeyedServiceFactory {
public:
static mojo::PendingRemote<mojom::BraveWalletService> GetForContext(
content::BrowserContext* context);
static BraveWalletService* GetServiceForContext(
content::BrowserContext* context);
static BraveWalletServiceFactory* GetInstance();
private:
friend struct base::DefaultSingletonTraits<BraveWalletServiceFactory>;
BraveWalletServiceFactory();
~BraveWalletServiceFactory() override;
BraveWalletServiceFactory(const BraveWalletServiceFactory&) = delete;
BraveWalletServiceFactory& operator=(const BraveWalletServiceFactory&) =
delete;
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
};
} // namespace brave_wallet
#endif // BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_SERVICE_FACTORY_H_
@@ -33,6 +33,7 @@
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
#include "brave/browser/brave_wallet/asset_ratio_controller_factory.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/browser/brave_wallet/eth_tx_controller_factory.h"
#include "brave/browser/brave_wallet/keyring_controller_factory.h"
#include "brave/browser/brave_wallet/rpc_controller_factory.h"
@@ -85,6 +86,7 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
brave_wallet::RpcControllerFactory::GetInstance();
brave_wallet::SwapControllerFactory::GetInstance();
brave_wallet::EthTxControllerFactory::GetInstance();
brave_wallet::BraveWalletServiceFactory::GetInstance();
#endif
#if BUILDFLAG(ETHEREUM_REMOTE_CLIENT_ENABLED)
+2
View File
@@ -21,6 +21,8 @@ static_library("browser") {
"brave_wallet_provider_delegate.h",
"brave_wallet_provider_impl.cc",
"brave_wallet_provider_impl.h",
"brave_wallet_service.cc",
"brave_wallet_service.h",
"brave_wallet_types.cc",
"brave_wallet_types.h",
"brave_wallet_utils.cc",
@@ -0,0 +1,30 @@
/* Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/. */
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "components/prefs/pref_service.h"
namespace brave_wallet {
BraveWalletService::BraveWalletService(PrefService* prefs) : prefs_(prefs) {
DCHECK(prefs_);
}
BraveWalletService::~BraveWalletService() = default;
mojo::PendingRemote<mojom::BraveWalletService>
BraveWalletService::MakeRemote() {
mojo::PendingRemote<mojom::BraveWalletService> remote;
receivers_.Add(this, remote.InitWithNewPipeAndPassReceiver());
return remote;
}
void BraveWalletService::Bind(
mojo::PendingReceiver<mojom::BraveWalletService> receiver) {
receivers_.Add(this, std::move(receiver));
}
} // namespace brave_wallet
@@ -0,0 +1,40 @@
/* Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/. */
#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BRAVE_WALLET_SERVICE_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BRAVE_WALLET_SERVICE_H_
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "components/keyed_service/core/keyed_service.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
class PrefService;
namespace brave_wallet {
class BraveWalletService : public KeyedService,
public mojom::BraveWalletService {
public:
BraveWalletService(PrefService* prefs);
~BraveWalletService() override;
BraveWalletService(const BraveWalletService&) = delete;
BraveWalletService& operator=(const BraveWalletService&) = delete;
mojo::PendingRemote<mojom::BraveWalletService> MakeRemote();
void Bind(mojo::PendingReceiver<mojom::BraveWalletService> receiver);
// mojom::BraveWalletService:
private:
PrefService* prefs_;
mojo::ReceiverSet<mojom::BraveWalletService> receivers_;
};
} // namespace brave_wallet
#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BRAVE_WALLET_SERVICE_H_
@@ -322,3 +322,6 @@ interface EthTxController {
GetAllTransactionInfo(string from) => (array<TransactionInfo> transaction_infos);
AddObserver(pending_remote<EthTxControllerObserver> observer);
};
interface BraveWalletService {
};