Skip to content

Commit

Permalink
Add wrappers for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
cypt4 committed Oct 14, 2023
1 parent d553237 commit d2d5cbc
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ios/browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ source_set("brave_wallet") {
"swap_service_factory.h",
"tx_service_factory.cc",
"tx_service_factory.h",
"zcash_wallet_service_factory.h",
"zcash_wallet_service_factory.mm",
]
deps = [
"//brave/components/brave_wallet/browser",
Expand Down
8 changes: 7 additions & 1 deletion ios/browser/brave_wallet/brave_wallet_factory_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
, BraveWalletBraveWalletService, BraveWalletJsonRpcService,
BraveWalletEthTxManagerProxy, BraveWalletSolanaTxManagerProxy,
BraveWalletTxService, BraveWalletKeyringService, BraveWalletSwapService,
BraveWalletIpfsService;
BraveWalletIpfsService, BraveWalletZCashWalletService;

OBJC_EXPORT
NS_SWIFT_NAME(BraveWallet.AssetRatioServiceFactory)
Expand Down Expand Up @@ -69,4 +69,10 @@ NS_SWIFT_NAME(BraveWallet.IpfsServiceFactory)
: KeyedServiceFactoryWrapper <id <BraveWalletIpfsService>>
@end

OBJC_EXPORT
NS_SWIFT_NAME(BraveWallet.ZCashWalletServiceFactory)
@interface BraveWalletZCashWalletServiceFactory
: KeyedServiceFactoryWrapper <id <BraveWalletZCashWalletService>>
@end

#endif // BRAVE_IOS_BROWSER_BRAVE_WALLET_BRAVE_WALLET_FACTORY_WRAPPERS_H_
13 changes: 13 additions & 0 deletions ios/browser/brave_wallet/brave_wallet_factory_wrappers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "brave/ios/browser/brave_wallet/keyring_service_factory.h"
#include "brave/ios/browser/brave_wallet/swap_service_factory.h"
#include "brave/ios/browser/brave_wallet/tx_service_factory.h"
#include "brave/ios/browser/brave_wallet/zcash_wallet_service_factory.h"
#include "brave/ios/browser/keyed_service/keyed_service_factory_wrapper+private.h"
#include "ios/chrome/browser/shared/model/browser_state/chrome_browser_state.h"

Expand Down Expand Up @@ -130,3 +131,15 @@ + (nullable id)serviceForBrowserState:(ChromeBrowserState*)browserState {
initWithIpfsService:std::move(service)];
}
@end

@implementation BraveWalletZCashWalletServiceFactory
+ (nullable id)serviceForBrowserState:(ChromeBrowserState*)browserState {
auto service =
brave_wallet::ZCashWalletServiceFactory::GetForBrowserState(browserState);
if (!service) {
return nil;
}
return [[BraveWalletZCashWalletServiceMojoImpl alloc]
initWithZCashWalletService:std::move(service)];
}
@end
63 changes: 63 additions & 0 deletions ios/browser/brave_wallet/zcash_wallet_service_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Copyright (c) 2023 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_IOS_BROWSER_BRAVE_WALLET_ZCASH_WALLET_SERVICE_FACTORY_H_
#define BRAVE_IOS_BROWSER_BRAVE_WALLET_ZCASH_WALLET_SERVICE_FACTORY_H_

#include <memory>

#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "components/keyed_service/ios/browser_state_keyed_service_factory.h"
#include "mojo/public/cpp/bindings/pending_remote.h"

class ChromeBrowserState;
class KeyedService;

namespace base {
template <typename T>
class NoDestructor;
} // namespace base

namespace web {
class BrowserState;
} // namespace web

namespace brave_wallet {

class ZCashWalletService;

class ZCashWalletServiceFactory : public BrowserStateKeyedServiceFactory {
public:
// Creates the service if it doesn't exist already for |browser_state|.
static mojo::PendingRemote<mojom::ZCashWalletService> GetForBrowserState(
ChromeBrowserState* browser_state);

static ZCashWalletService* GetServiceForState(
ChromeBrowserState* browser_state);

static ZCashWalletServiceFactory* GetInstance();

private:
friend base::NoDestructor<ZCashWalletServiceFactory>;

ZCashWalletServiceFactory();
~ZCashWalletServiceFactory() override;

// BrowserContextKeyedServiceFactory:
// BrowserStateKeyedServiceFactory implementation.
std::unique_ptr<KeyedService> BuildServiceInstanceFor(
web::BrowserState* context) const override;
bool ServiceIsNULLWhileTesting() const override;
web::BrowserState* GetBrowserStateToUse(
web::BrowserState* context) const override;

ZCashWalletServiceFactory(const ZCashWalletServiceFactory&) = delete;
ZCashWalletServiceFactory& operator=(const ZCashWalletServiceFactory&) =
delete;
};

} // namespace brave_wallet

#endif // BRAVE_IOS_BROWSER_BRAVE_WALLET_ZCASH_WALLET_SERVICE_FACTORY_H_
74 changes: 74 additions & 0 deletions ios/browser/brave_wallet/zcash_wallet_service_factory.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Copyright (c) 2023 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/. */

#include "brave/ios/browser/brave_wallet/zcash_wallet_service_factory.h"

#include "brave/components/brave_wallet/browser/pref_names.h"
#include "brave/components/brave_wallet/browser/zcash/zcash_wallet_service.h"
#include "brave/components/brave_wallet/common/common_utils.h"
#include "brave/ios/browser/brave_wallet/keyring_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/keyed_service/ios/browser_state_dependency_manager.h"
#include "ios/chrome/browser/shared/model/application_context/application_context.h"
#include "ios/chrome/browser/shared/model/browser_state/browser_state_otr_helper.h"
#include "ios/chrome/browser/shared/model/browser_state/chrome_browser_state.h"
#include "ios/web/public/browser_state.h"

namespace brave_wallet {

// static
mojo::PendingRemote<mojom::ZCashWalletService>
ZCashWalletServiceFactory::GetForBrowserState(
ChromeBrowserState* browser_state) {
return static_cast<ZCashWalletService*>(
GetInstance()->GetServiceForBrowserState(browser_state, true))
->MakeRemote();
}

// static
ZCashWalletService* ZCashWalletServiceFactory::GetServiceForState(
ChromeBrowserState* browser_state) {
return static_cast<ZCashWalletService*>(
GetInstance()->GetServiceForBrowserState(browser_state, true));
}

// static
ZCashWalletServiceFactory* ZCashWalletServiceFactory::GetInstance() {
static base::NoDestructor<ZCashWalletServiceFactory> instance;
return instance.get();
}

ZCashWalletServiceFactory::ZCashWalletServiceFactory()
: BrowserStateKeyedServiceFactory(
"ZCashWalletService",
BrowserStateDependencyManager::GetInstance()) {
DependsOn(KeyringServiceFactory::GetInstance());
}

ZCashWalletServiceFactory::~ZCashWalletServiceFactory() = default;

std::unique_ptr<KeyedService>
ZCashWalletServiceFactory::BuildServiceInstanceFor(
web::BrowserState* context) const {
auto* browser_state = ChromeBrowserState::FromBrowserState(context);
if (!IsZCashEnabled()) {
return nullptr;
}
std::unique_ptr<ZCashWalletService> zcash_service(new ZCashWalletService(
KeyringServiceFactory::GetServiceForState(browser_state),
browser_state->GetPrefs(), browser_state->GetSharedURLLoaderFactory()));
return zcash_service;
}

bool ZCashWalletServiceFactory::ServiceIsNULLWhileTesting() const {
return true;
}

web::BrowserState* ZCashWalletServiceFactory::GetBrowserStateToUse(
web::BrowserState* context) const {
return GetBrowserStateRedirectedInIncognito(context);
}

} // namespace brave_wallet

0 comments on commit d2d5cbc

Please sign in to comment.