-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Launch IPFS daemon when navigating to an IPFS URL if gateway setting …
…is local node
- Loading branch information
Showing
11 changed files
with
226 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |