Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core part for local nft pinning #16073

Merged
merged 15 commits into from
Jan 25, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ public GetNftErc721MetadataContext(Runnable responseCompleteCallback) {
}

@Override
public void call(String erc721Metadata, Integer errorCode, String errorMessage) {
public void call(
String tokenUrl, String erc721Metadata, Integer errorCode, String errorMessage) {
this.tokenMetadata = erc721Metadata;
this.errorCode = errorCode;
this.errorMessage = errorMessage;
Expand Down
9 changes: 9 additions & 0 deletions browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Copyright (c) 2019 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/.

import("//brave/browser/ethereum_remote_client/buildflags/buildflags.gni")
import("//extensions/buildflags/buildflags.gni")
import("//testing/test.gni")
Expand All @@ -8,8 +13,12 @@ source_set("brave_wallet") {
"asset_ratio_service_factory.h",
"blockchain_images_source.cc",
"blockchain_images_source.h",
"brave_wallet_auto_pin_service_factory.cc",
"brave_wallet_auto_pin_service_factory.h",
"brave_wallet_context_utils.cc",
"brave_wallet_context_utils.h",
"brave_wallet_pin_service_factory.cc",
"brave_wallet_pin_service_factory.h",
"brave_wallet_service_factory.cc",
"brave_wallet_service_factory.h",
"json_rpc_service_factory.cc",
Expand Down
98 changes: 98 additions & 0 deletions browser/brave_wallet/brave_wallet_auto_pin_service_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 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/browser/brave_wallet/brave_wallet_auto_pin_service_factory.h"

#include <memory>
#include <utility>

#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
#include "brave/browser/brave_wallet/brave_wallet_pin_service_factory.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
// TODO(cypt4) : Refactor brave/browser/ipfs into separate component (#27486)
#include "brave/browser/ipfs/ipfs_service_factory.h" // nogncheck

#include "brave/components/brave_wallet/browser/brave_wallet_pin_service.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
BraveWalletAutoPinServiceFactory*
BraveWalletAutoPinServiceFactory::GetInstance() {
return base::Singleton<BraveWalletAutoPinServiceFactory>::get();
}

// static
mojo::PendingRemote<mojom::WalletAutoPinService>
BraveWalletAutoPinServiceFactory::GetForContext(
content::BrowserContext* context) {
if (!IsAllowedForContext(context)) {
return mojo::PendingRemote<mojom::WalletAutoPinService>();
}

auto* service = GetServiceForContext(context);

if (!service) {
return mojo::PendingRemote<mojom::WalletAutoPinService>();
}

return service->MakeRemote();
}

// static
BraveWalletAutoPinService*
BraveWalletAutoPinServiceFactory::GetServiceForContext(
content::BrowserContext* context) {
if (!IsAllowedForContext(context)) {
return nullptr;
}
if (!ipfs::IpfsServiceFactory::IsIpfsEnabled(context)) {
return nullptr;
}
return static_cast<BraveWalletAutoPinService*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}

// static
void BraveWalletAutoPinServiceFactory::BindForContext(
content::BrowserContext* context,
mojo::PendingReceiver<mojom::WalletAutoPinService> receiver) {
auto* service =
BraveWalletAutoPinServiceFactory::GetServiceForContext(context);
if (service) {
service->Bind(std::move(receiver));
}
}

BraveWalletAutoPinServiceFactory::BraveWalletAutoPinServiceFactory()
: BrowserContextKeyedServiceFactory(
"BraveWalletAutoPinService",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(BraveWalletServiceFactory::GetInstance());
DependsOn(BraveWalletPinServiceFactory::GetInstance());
}

BraveWalletAutoPinServiceFactory::~BraveWalletAutoPinServiceFactory() = default;

KeyedService* BraveWalletAutoPinServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new BraveWalletAutoPinService(
user_prefs::UserPrefs::Get(context),
BraveWalletServiceFactory::GetServiceForContext(context),
BraveWalletPinServiceFactory::GetServiceForContext(context));
}

content::BrowserContext*
BraveWalletAutoPinServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}

} // namespace brave_wallet
54 changes: 54 additions & 0 deletions browser/brave_wallet/brave_wallet_auto_pin_service_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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_BROWSER_BRAVE_WALLET_BRAVE_WALLET_AUTO_PIN_SERVICE_FACTORY_H_
#define BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_AUTO_PIN_SERVICE_FACTORY_H_

#include "base/memory/singleton.h"

#include "brave/components/brave_wallet/browser/brave_wallet_auto_pin_service.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_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"

namespace brave_wallet {

class BraveWalletAutoPinService;

class BraveWalletAutoPinServiceFactory
: public BrowserContextKeyedServiceFactory {
public:
static mojo::PendingRemote<mojom::WalletAutoPinService> GetForContext(
content::BrowserContext* context);
static BraveWalletAutoPinService* GetServiceForContext(
content::BrowserContext* context);
static BraveWalletAutoPinServiceFactory* GetInstance();
static void BindForContext(
content::BrowserContext* context,
mojo::PendingReceiver<mojom::WalletAutoPinService> receiver);

private:
friend struct base::DefaultSingletonTraits<BraveWalletAutoPinServiceFactory>;

BraveWalletAutoPinServiceFactory();
~BraveWalletAutoPinServiceFactory() override;

BraveWalletAutoPinServiceFactory(const BraveWalletAutoPinServiceFactory&) =
delete;
BraveWalletAutoPinServiceFactory& operator=(
const BraveWalletAutoPinServiceFactory&) = 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_AUTO_PIN_SERVICE_FACTORY_H_
93 changes: 93 additions & 0 deletions browser/brave_wallet/brave_wallet_pin_service_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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/browser/brave_wallet/brave_wallet_pin_service_factory.h"

#include <memory>
#include <utility>

#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
#include "brave/browser/brave_wallet/json_rpc_service_factory.h"
// TODO(cypt4) : Refactor brave/browser into separate component (#27486)
cypt4 marked this conversation as resolved.
Show resolved Hide resolved
#include "brave/browser/ipfs/ipfs_local_pin_service_factory.h" // nogncheck
#include "brave/browser/ipfs/ipfs_service_factory.h" // nogncheck
#include "brave/components/brave_wallet/browser/brave_wallet_pin_service.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service_delegate.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
BraveWalletPinServiceFactory* BraveWalletPinServiceFactory::GetInstance() {
return base::Singleton<BraveWalletPinServiceFactory>::get();
}

// static
mojo::PendingRemote<mojom::WalletPinService>
BraveWalletPinServiceFactory::GetForContext(content::BrowserContext* context) {
if (!IsAllowedForContext(context)) {
return mojo::PendingRemote<mojom::WalletPinService>();
}

auto* service = GetServiceForContext(context);
if (!service) {
return mojo::PendingRemote<mojom::WalletPinService>();
}

return service->MakeRemote();
}

// static
BraveWalletPinService* BraveWalletPinServiceFactory::GetServiceForContext(
content::BrowserContext* context) {
if (!IsAllowedForContext(context)) {
return nullptr;
}
if (!ipfs::IpfsServiceFactory::IsIpfsEnabled(context)) {
return nullptr;
}
return static_cast<BraveWalletPinService*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}

// static
void BraveWalletPinServiceFactory::BindForContext(
content::BrowserContext* context,
mojo::PendingReceiver<mojom::WalletPinService> receiver) {
auto* service = BraveWalletPinServiceFactory::GetServiceForContext(context);
if (service) {
service->Bind(std::move(receiver));
}
}

BraveWalletPinServiceFactory::BraveWalletPinServiceFactory()
: BrowserContextKeyedServiceFactory(
"BraveWalletPinService",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(brave_wallet::JsonRpcServiceFactory::GetInstance());
DependsOn(ipfs::IpfsLocalPinServiceFactory::GetInstance());
DependsOn(ipfs::IpfsServiceFactory::GetInstance());
}

BraveWalletPinServiceFactory::~BraveWalletPinServiceFactory() = default;

KeyedService* BraveWalletPinServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new BraveWalletPinService(
user_prefs::UserPrefs::Get(context),
JsonRpcServiceFactory::GetServiceForContext(context),
ipfs::IpfsLocalPinServiceFactory::GetServiceForContext(context),
ipfs::IpfsServiceFactory::GetForContext(context));
}

content::BrowserContext* BraveWalletPinServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}

} // namespace brave_wallet
50 changes: 50 additions & 0 deletions browser/brave_wallet/brave_wallet_pin_service_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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_BROWSER_BRAVE_WALLET_BRAVE_WALLET_PIN_SERVICE_FACTORY_H_
#define BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_PIN_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_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"

namespace brave_wallet {

class BraveWalletPinService;

class BraveWalletPinServiceFactory : public BrowserContextKeyedServiceFactory {
public:
static mojo::PendingRemote<mojom::WalletPinService> GetForContext(
content::BrowserContext* context);
static BraveWalletPinService* GetServiceForContext(
content::BrowserContext* context);
static BraveWalletPinServiceFactory* GetInstance();
static void BindForContext(
content::BrowserContext* context,
mojo::PendingReceiver<mojom::WalletPinService> receiver);

private:
friend struct base::DefaultSingletonTraits<BraveWalletPinServiceFactory>;

BraveWalletPinServiceFactory();
~BraveWalletPinServiceFactory() override;

BraveWalletPinServiceFactory(const BraveWalletPinServiceFactory&) = delete;
BraveWalletPinServiceFactory& operator=(const BraveWalletPinServiceFactory&) =
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_PIN_SERVICE_FACTORY_H_
4 changes: 4 additions & 0 deletions browser/browser_context_keyed_service_factories.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
#endif

#if BUILDFLAG(ENABLE_IPFS)
#include "brave/browser/brave_wallet/brave_wallet_auto_pin_service_factory.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After d517807#diff-87dbeae647f32aa0670c977a84d99be9862a63bf9cc1f0a8b191da86f1d1a21f
we should consider removing ENABLE_IPFS build flag. But not in this PR I think

#include "brave/browser/brave_wallet/brave_wallet_pin_service_factory.h"
#include "brave/browser/ipfs/ipfs_service_factory.h"
#endif

Expand Down Expand Up @@ -107,6 +109,8 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
#endif

#if BUILDFLAG(ENABLE_IPFS)
brave_wallet::BraveWalletAutoPinServiceFactory::GetInstance();
brave_wallet::BraveWalletPinServiceFactory::GetInstance();
ipfs::IpfsServiceFactory::GetInstance();
#endif

Expand Down
Loading