diff --git a/browser/brave_wallet/BUILD.gn b/browser/brave_wallet/BUILD.gn index 5085868c95e..53fe1e6e3f7 100644 --- a/browser/brave_wallet/BUILD.gn +++ b/browser/brave_wallet/BUILD.gn @@ -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", diff --git a/browser/brave_wallet/brave_wallet_service_factory.cc b/browser/brave_wallet/brave_wallet_service_factory.cc new file mode 100644 index 00000000000..b82ef36843c --- /dev/null +++ b/browser/brave_wallet/brave_wallet_service_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::get(); +} + +// static +mojo::PendingRemote +BraveWalletServiceFactory::GetForContext(content::BrowserContext* context) { + if (!IsAllowedForContext(context)) { + return mojo::PendingRemote(); + } + + return static_cast( + GetInstance()->GetServiceForBrowserContext(context, true)) + ->MakeRemote(); +} + +// static +BraveWalletService* BraveWalletServiceFactory::GetServiceForContext( + content::BrowserContext* context) { + if (!IsAllowedForContext(context)) { + return nullptr; + } + return static_cast( + 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 diff --git a/browser/brave_wallet/brave_wallet_service_factory.h b/browser/brave_wallet/brave_wallet_service_factory.h new file mode 100644 index 00000000000..025529cb0ab --- /dev/null +++ b/browser/brave_wallet/brave_wallet_service_factory.h @@ -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 GetForContext( + content::BrowserContext* context); + static BraveWalletService* GetServiceForContext( + content::BrowserContext* context); + static BraveWalletServiceFactory* GetInstance(); + + private: + friend struct base::DefaultSingletonTraits; + + 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_ diff --git a/browser/browser_context_keyed_service_factories.cc b/browser/browser_context_keyed_service_factories.cc index 6ecf95e3c5b..973f28f3a89 100644 --- a/browser/browser_context_keyed_service_factories.cc +++ b/browser/browser_context_keyed_service_factories.cc @@ -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) diff --git a/components/brave_wallet/browser/BUILD.gn b/components/brave_wallet/browser/BUILD.gn index a6d9b0b9f5d..7dbe1abe7e3 100644 --- a/components/brave_wallet/browser/BUILD.gn +++ b/components/brave_wallet/browser/BUILD.gn @@ -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", diff --git a/components/brave_wallet/browser/brave_wallet_service.cc b/components/brave_wallet/browser/brave_wallet_service.cc new file mode 100644 index 00000000000..9a9631f0ba1 --- /dev/null +++ b/components/brave_wallet/browser/brave_wallet_service.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 +BraveWalletService::MakeRemote() { + mojo::PendingRemote remote; + receivers_.Add(this, remote.InitWithNewPipeAndPassReceiver()); + return remote; +} + +void BraveWalletService::Bind( + mojo::PendingReceiver receiver) { + receivers_.Add(this, std::move(receiver)); +} + +} // namespace brave_wallet diff --git a/components/brave_wallet/browser/brave_wallet_service.h b/components/brave_wallet/browser/brave_wallet_service.h new file mode 100644 index 00000000000..092c4d14cce --- /dev/null +++ b/components/brave_wallet/browser/brave_wallet_service.h @@ -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 MakeRemote(); + void Bind(mojo::PendingReceiver receiver); + + // mojom::BraveWalletService: + + private: + PrefService* prefs_; + mojo::ReceiverSet receivers_; +}; + +} // namespace brave_wallet + +#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BRAVE_WALLET_SERVICE_H_ diff --git a/components/brave_wallet/common/brave_wallet.mojom b/components/brave_wallet/common/brave_wallet.mojom index ab16ece324a..be287e6d422 100644 --- a/components/brave_wallet/common/brave_wallet.mojom +++ b/components/brave_wallet/common/brave_wallet.mojom @@ -322,3 +322,6 @@ interface EthTxController { GetAllTransactionInfo(string from) => (array transaction_infos); AddObserver(pending_remote observer); }; + +interface BraveWalletService { +};