Skip to content

Commit

Permalink
Core part for local nft pinning
Browse files Browse the repository at this point in the history
  • Loading branch information
cypt4 committed Nov 26, 2022
1 parent 0ac7e36 commit fa5f781
Show file tree
Hide file tree
Showing 59 changed files with 4,541 additions and 62 deletions.
4 changes: 4 additions & 0 deletions browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,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
88 changes: 88 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,88 @@
/* Copyright (c) 2022 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_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"

#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>();
}

return GetServiceForContext(context)->MakeRemote();
}

// static
BraveWalletAutoPinService*
BraveWalletAutoPinServiceFactory::GetServiceForContext(
content::BrowserContext* context) {
if (!IsAllowedForContext(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) 2022 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_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_
81 changes: 81 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,81 @@
/* Copyright (c) 2022 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_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"
#include "brave/browser/ipfs/ipfs_local_pin_service_factory.h"
#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>();
}

return GetServiceForContext(context)->MakeRemote();
}

// static
BraveWalletPinService* BraveWalletPinServiceFactory::GetServiceForContext(
content::BrowserContext* context) {
if (!IsAllowedForContext(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());
}

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));
}

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

} // namespace brave_wallet
51 changes: 51 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,51 @@
/* Copyright (c) 2022 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_PIN_SERVICE_FACTORY_H_
#define BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_PIN_SERVICE_FACTORY_H_

#include "base/memory/singleton.h"
#include "brave/browser/ipfs/ipfs_service_factory.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_
52 changes: 52 additions & 0 deletions browser/ipfs/ipfs_local_pin_service_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* Copyright (c) 2022 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/ipfs/ipfs_local_pin_service_factory.h"

#include <memory>
#include <utility>

#include "brave/browser/ipfs/ipfs_service_factory.h"
#include "brave/components/ipfs/pin/ipfs_local_pin_service.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"

namespace ipfs {

// static
IpfsLocalPinServiceFactory* IpfsLocalPinServiceFactory::GetInstance() {
return base::Singleton<IpfsLocalPinServiceFactory>::get();
}

// static
IpfsLocalPinService* IpfsLocalPinServiceFactory::GetServiceForContext(
content::BrowserContext* context) {
return static_cast<IpfsLocalPinService*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}

IpfsLocalPinServiceFactory::IpfsLocalPinServiceFactory()
: BrowserContextKeyedServiceFactory(
"IpfsLocalPinService",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(ipfs::IpfsServiceFactory::GetInstance());
}

IpfsLocalPinServiceFactory::~IpfsLocalPinServiceFactory() = default;

KeyedService* IpfsLocalPinServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new IpfsLocalPinService(user_prefs::UserPrefs::Get(context),
IpfsServiceFactory::GetForContext(context));
}

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

} // namespace ipfs
42 changes: 42 additions & 0 deletions browser/ipfs/ipfs_local_pin_service_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Copyright (c) 2022 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_IPFS_IPFS_LOCAL_PIN_SERVICE_FACTORY_H_
#define BRAVE_BROWSER_IPFS_IPFS_LOCAL_PIN_SERVICE_FACTORY_H_

#include "base/memory/singleton.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"

namespace ipfs {

class IpfsLocalPinService;

class IpfsLocalPinServiceFactory : public BrowserContextKeyedServiceFactory {
public:
static IpfsLocalPinService* GetServiceForContext(
content::BrowserContext* context);
static IpfsLocalPinServiceFactory* GetInstance();

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

IpfsLocalPinServiceFactory();
~IpfsLocalPinServiceFactory() override;

IpfsLocalPinServiceFactory(const IpfsLocalPinServiceFactory&) = delete;
IpfsLocalPinServiceFactory& operator=(const IpfsLocalPinServiceFactory&) =
delete;

KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
};

} // namespace ipfs

#endif // BRAVE_BROWSER_IPFS_IPFS_LOCAL_PIN_SERVICE_FACTORY_H_
2 changes: 2 additions & 0 deletions browser/ipfs/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ if (enable_ipfs) {
"//brave/browser/ipfs/ipfs_dns_resolver_impl.h",
"//brave/browser/ipfs/ipfs_host_resolver.cc",
"//brave/browser/ipfs/ipfs_host_resolver.h",
"//brave/browser/ipfs/ipfs_local_pin_service_factory.cc",
"//brave/browser/ipfs/ipfs_local_pin_service_factory.h",
"//brave/browser/ipfs/ipfs_service_factory.cc",
"//brave/browser/ipfs/ipfs_service_factory.h",
"//brave/browser/ipfs/ipfs_subframe_navigation_throttle.cc",
Expand Down
Loading

0 comments on commit fa5f781

Please sign in to comment.