Skip to content

Commit

Permalink
Launch IPFS daemon when navigating to an IPFS URL if gateway setting …
Browse files Browse the repository at this point in the history
…is local node
  • Loading branch information
yrliou committed Aug 27, 2020
1 parent caa4c28 commit 68e8cd4
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 17 deletions.
3 changes: 3 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,13 @@ source_set("browser_process") {
sources += [
"ipfs/content_browser_client_helper.cc",
"ipfs/content_browser_client_helper.h",
"ipfs/ipfs_navigation_throttle.cc",
"ipfs/ipfs_navigation_throttle.h",
"ipfs/ipfs_service.cc",
"ipfs/ipfs_service.h",
"ipfs/ipfs_service_factory.cc",
"ipfs/ipfs_service_factory.h",
"ipfs/ipfs_service_observer.h",
"ipfs/ipfs_tab_helper.cc",
"ipfs/ipfs_tab_helper.h",
]
Expand Down
6 changes: 6 additions & 0 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ using extensions::ChromeContentBrowserClientExtensionsPart;

#if BUILDFLAG(IPFS_ENABLED)
#include "brave/browser/ipfs/content_browser_client_helper.h"
#include "brave/browser/ipfs/ipfs_navigation_throttle.h"
#endif

#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
Expand Down Expand Up @@ -519,5 +520,10 @@ BraveContentBrowserClient::CreateThrottlesForNavigation(
throttles.push_back(std::move(tor_navigation_throttle));
#endif

#if BUILDFLAG(IPFS_ENABLED)
throttles.push_back(
std::make_unique<ipfs::IpfsNavigationThrottle>(handle));
#endif

return throttles;
}
68 changes: 68 additions & 0 deletions browser/ipfs/ipfs_navigation_throttle.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Copyright (c) 2020 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_navigation_throttle.h"

#include "brave/browser/ipfs/ipfs_service.h"
#include "brave/browser/ipfs/ipfs_service_factory.h"
#include "brave/common/pref_names.h"
#include "brave/components/ipfs/common/ipfs_constants.h"
#include "brave/components/ipfs/common/ipfs_utils.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"

namespace ipfs {

IpfsNavigationThrottle::IpfsNavigationThrottle(
content::NavigationHandle* navigation_handle)
: content::NavigationThrottle(navigation_handle) {
content::BrowserContext* context =
navigation_handle->GetWebContents()->GetBrowserContext();
ipfs_service_ = IpfsServiceFactory::GetForContext(context);
pref_service_ = user_prefs::UserPrefs::Get(context);

DCHECK(ipfs_service_);
ipfs_service_->AddObserver(this);
}

IpfsNavigationThrottle::~IpfsNavigationThrottle() {
ipfs_service_->RemoveObserver(this);
}

content::NavigationThrottle::ThrottleCheckResult
IpfsNavigationThrottle::WillStartRequest() {
GURL url = navigation_handle()->GetURL();
if (!IpfsUtils::IsIPFSURL(url)) {
return content::NavigationThrottle::PROCEED;
}

bool is_local_mode = pref_service_->FindPreference(kIPFSResolveMethod) &&
pref_service_->GetInteger(kIPFSResolveMethod) ==
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_LOCAL);

if (is_local_mode && !ipfs_service_->IsDaemonLaunched()) {
resume_pending_ = true;
ipfs_service_->RegisterIpfsClientUpdater();
return content::NavigationThrottle::DEFER;
}

return content::NavigationThrottle::PROCEED;
}

const char* IpfsNavigationThrottle::GetNameForLogging() {
return "IpfsNavigationThrottle";
}

void IpfsNavigationThrottle::OnIpfsLaunched(bool result, int64_t pid) {
if (result && resume_pending_) {
resume_pending_ = false;
Resume();
}
}

} // namespace ipfs
48 changes: 48 additions & 0 deletions browser/ipfs/ipfs_navigation_throttle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Copyright (c) 2020 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_NAVIGATION_THROTTLE_H_
#define BRAVE_BROWSER_IPFS_IPFS_NAVIGATION_THROTTLE_H_

#include "content/public/browser/navigation_throttle.h"

#include "brave/browser/ipfs/ipfs_service_observer.h"

namespace content {
class NavigationHandle;
} // namespace content

class PrefService;

namespace ipfs {

class IpfsService;
class IpfsServiceObserver;

class IpfsNavigationThrottle : public content::NavigationThrottle,
public IpfsServiceObserver {
public:
explicit IpfsNavigationThrottle(
content::NavigationHandle* navigation_handle);
~IpfsNavigationThrottle() override;

// content::NavigationThrottle implementation:
ThrottleCheckResult WillStartRequest() override;
const char* GetNameForLogging() override;

private:
// IpfsServiceObserver:
void OnIpfsLaunched(bool result, int64_t pid) override;

bool resume_pending_ = false;
IpfsService* ipfs_service_ = nullptr;
PrefService* pref_service_ = nullptr;

DISALLOW_COPY_AND_ASSIGN(IpfsNavigationThrottle);
};

} // namespace ipfs

#endif // BRAVE_BROWSER_IPFS_IPFS_NAVIGATION_THROTTLE_H_
17 changes: 17 additions & 0 deletions browser/ipfs/ipfs_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/browser/ipfs/ipfs_service_observer.h"
#include "brave/common/pref_names.h"
#include "brave/components/ipfs/browser/ipfs_json_parser.h"
#include "brave/components/ipfs/common/ipfs_constants.h"
Expand Down Expand Up @@ -157,6 +158,10 @@ void IpfsService::OnIpfsLaunched(bool result, int64_t pid) {
if (!launch_daemon_callback_.is_null()) {
std::move(launch_daemon_callback_).Run(result && pid > 0);
}

for (auto& observer : observers_) {
observer.OnIpfsLaunched(result, pid);
}
}

void IpfsService::Shutdown() {
Expand Down Expand Up @@ -296,4 +301,16 @@ bool IpfsService::IsIPFSExecutableAvailable() const {
return prefs->GetBoolean(kIPFSBinaryAvailable);
}

void IpfsService::AddObserver(IpfsServiceObserver* observer) {
observers_.AddObserver(observer);
}

void IpfsService::RemoveObserver(IpfsServiceObserver* observer) {
observers_.RemoveObserver(observer);
}

void IpfsService::RegisterIpfsClientUpdater() {
g_brave_browser_process->ipfs_client_updater()->Register();
}

} // namespace ipfs
8 changes: 8 additions & 0 deletions browser/ipfs/ipfs_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string>
#include <vector>

#include "base/observer_list.h"
#include "brave/components/ipfs/browser/addresses_config.h"
#include "brave/components/ipfs/browser/brave_ipfs_client_updater.h"
#include "brave/components/services/ipfs/public/mojom/ipfs_service.mojom.h"
Expand All @@ -32,6 +33,8 @@ class PrefRegistrySimple;

namespace ipfs {

class IpfsServiceObserver;

class IpfsService : public KeyedService,
public BraveIpfsClientUpdater::Observer {
public:
Expand All @@ -47,9 +50,13 @@ class IpfsService : public KeyedService,
using LaunchDaemonCallback = base::OnceCallback<void(bool)>;
using ShutdownDaemonCallback = base::OnceCallback<void(bool)>;

void AddObserver(IpfsServiceObserver* observer);
void RemoveObserver(IpfsServiceObserver* observer);

bool IsDaemonLaunched() const;
static void RegisterPrefs(PrefRegistrySimple* registry);
bool IsIPFSExecutableAvailable() const;
void RegisterIpfsClientUpdater();

// KeyedService
void Shutdown() override;
Expand Down Expand Up @@ -92,6 +99,7 @@ class IpfsService : public KeyedService,

int64_t ipfs_pid_ = -1;
content::BrowserContext* context_;
base::ObserverList<IpfsServiceObserver> observers_;

scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
SimpleURLLoaderList url_loaders_;
Expand Down
21 changes: 21 additions & 0 deletions browser/ipfs/ipfs_service_observer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Copyright 2020 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_SERVICE_OBSERVER_H_
#define BRAVE_BROWSER_IPFS_IPFS_SERVICE_OBSERVER_H_

#include "base/observer_list_types.h"

namespace ipfs {

class IpfsServiceObserver : public base::CheckedObserver {
public:
~IpfsServiceObserver() override {}
virtual void OnIpfsLaunched(bool result, int64_t pid) {}
};

} // namespace ipfs

#endif // BRAVE_BROWSER_IPFS_IPFS_SERVICE_OBSERVER_H_
19 changes: 2 additions & 17 deletions browser/ipfs/ipfs_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,12 @@
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/browser/infobars/ipfs_infobar_delegate.h"
#include "brave/common/pref_names.h"
#include "brave/components/ipfs/common/ipfs_utils.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "extensions/common/url_pattern.h"

namespace {

bool IsIPFSURL(const GURL& gurl) {
// Temporary to manually test the infobar
static std::vector<URLPattern> updater_patterns({
URLPattern(URLPattern::SCHEME_ALL, "*://*/ipfs/*"),
URLPattern(URLPattern::SCHEME_ALL, "*://*/ipfn/*")
});
return std::any_of(
updater_patterns.begin(), updater_patterns.end(),
[&gurl](URLPattern pattern) { return pattern.MatchesURL(gurl); });
}

} // namespace

namespace ipfs {

Expand All @@ -46,7 +31,7 @@ void IPFSTabHelper::UpdateActiveState(content::NavigationHandle* handle) {
DCHECK(handle->IsInMainFrame());
active_ = true;
if (!pref_service_->GetBoolean(kIPFSBinaryAvailable) &&
IsIPFSURL(handle->GetURL())) {
IpfsUtils::IsIPFSURL(handle->GetURL())) {
InfoBarService* infobar_service =
InfoBarService::FromWebContents(web_contents());
if (infobar_service) {
Expand Down
7 changes: 7 additions & 0 deletions components/ipfs/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ source_set("common") {
sources = [
"ipfs_constants.cc",
"ipfs_constants.h",
"ipfs_utils.cc",
"ipfs_utils.h",
]

deps = [
"//extensions/common",
"//url",
]
}
26 changes: 26 additions & 0 deletions components/ipfs/common/ipfs_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* Copyright (c) 2020 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/ipfs/common/ipfs_utils.h"

#include <vector>

#include "url/gurl.h"
#include "extensions/common/url_pattern.h"

namespace ipfs {

// static
bool IpfsUtils::IsIPFSURL(const GURL& gurl) {
static std::vector<URLPattern> updater_patterns({
URLPattern(URLPattern::SCHEME_ALL, "*://*/ipfs/*"),
URLPattern(URLPattern::SCHEME_ALL, "*://*/ipfn/*")
});
return std::any_of(
updater_patterns.begin(), updater_patterns.end(),
[&gurl](URLPattern pattern) { return pattern.MatchesURL(gurl); });
}

} // namespace ipfs
20 changes: 20 additions & 0 deletions components/ipfs/common/ipfs_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Copyright (c) 2020 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_IPFS_COMMON_IPFS_UTILS_H_
#define BRAVE_COMPONENTS_IPFS_COMMON_IPFS_UTILS_H_

class GURL;

namespace ipfs {

class IpfsUtils {
public:
static bool IsIPFSURL(const GURL& url);
};

} // namespace ipfs

#endif // BRAVE_COMPONENTS_IPFS_COMMON_IPFS_UTILS_H_

0 comments on commit 68e8cd4

Please sign in to comment.