diff --git a/browser/autocomplete/brave_autocomplete_scheme_classifier.cc b/browser/autocomplete/brave_autocomplete_scheme_classifier.cc index 5625fe031e69..58ad49a8cc8e 100644 --- a/browser/autocomplete/brave_autocomplete_scheme_classifier.cc +++ b/browser/autocomplete/brave_autocomplete_scheme_classifier.cc @@ -16,6 +16,10 @@ #include "brave/components/brave_webtorrent/browser/webtorrent_util.h" #endif +#if BUILDFLAG(IPFS_ENABLED) +#include "brave/components/ipfs/common/ipfs_constants.h" +#endif + BraveAutocompleteSchemeClassifier::BraveAutocompleteSchemeClassifier( Profile* profile) : ChromeAutocompleteSchemeClassifier(profile) { @@ -49,8 +53,8 @@ BraveAutocompleteSchemeClassifier::GetInputTypeForScheme( #if BUILDFLAG(IPFS_ENABLED) if (base::IsStringASCII(scheme) && - (base::LowerCaseEqualsASCII(scheme, kIPFSScheme) || - base::LowerCaseEqualsASCII(scheme, kIPNSScheme))) { + (base::LowerCaseEqualsASCII(scheme, ipfs::kIPFSScheme) || + base::LowerCaseEqualsASCII(scheme, ipfs::kIPNSScheme))) { return metrics::OmniboxInputType::URL; } #endif diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 8fb0cda0b22e..56fce0a7af4b 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -170,9 +170,6 @@ void BraveContentBrowserClient::BrowserURLHandlerCreated( #endif #if BUILDFLAG(IPFS_ENABLED) if (base::FeatureList::IsEnabled(ipfs::features::kIpfsFeature)) { - handler->AddHandlerPair( - &ipfs::ContentBrowserClientHelper::HandleIPFSURLRewrite, - content::BrowserURLHandler::null_handler()); handler->AddHandlerPair( &ipfs::ContentBrowserClientHelper::HandleIPFSURLRewrite, &ipfs::ContentBrowserClientHelper::HandleIPFSURLReverseRewrite); diff --git a/browser/ipfs/content_browser_client_helper.cc b/browser/ipfs/content_browser_client_helper.cc index a28268ed44d6..cfe8a5b5b721 100644 --- a/browser/ipfs/content_browser_client_helper.cc +++ b/browser/ipfs/content_browser_client_helper.cc @@ -11,6 +11,7 @@ #include "base/task/post_task.h" #include "brave/common/url_constants.h" #include "brave/common/pref_names.h" +#include "brave/components/ipfs/browser/translate_ipfs_uri.h" #include "brave/components/ipfs/common/ipfs_constants.h" #include "chrome/browser/external_protocol/external_protocol_handler.h" #include "components/prefs/pref_service.h" @@ -42,49 +43,6 @@ bool IsIPFSLocalGateway(content::BrowserContext* browser_context) { namespace ipfs { -// static -bool ContentBrowserClientHelper::TranslateIPFSURL( - const GURL& url, GURL* new_url, bool local) { - if (!url.SchemeIs(kIPFSScheme) && !url.SchemeIs(kIPNSScheme)) { - return false; - } - - std::string path = url.path(); - // In the case of a URL like ipfs://[cid]/wiki/Vincent_van_Gogh.html - // host is empty and path is //wiki/Vincent_van_Gogh.html - if (url.host().empty() && path.length() > 2 && - path.substr(0, 2) == "//") { - std::string cid(path.substr(2)); - // If we have a path after the CID, get at the real resource path - size_t pos = cid.find("/"); - std::string path; - if (pos != std::string::npos && pos != 0) { - // path would be /wiki/Vincent_van_Gogh.html - path = cid.substr(pos, cid.length() - pos); - // cid would be [cid] - cid = cid.substr(0, pos); - } - bool ipfs_scheme = url.scheme() == "ipfs"; - bool ipns_scheme = url.scheme() == "ipns"; - if ((ipfs_scheme || ipns_scheme) && std::all_of(cid.begin(), cid.end(), - [loc = std::locale{}](char c) { - return std::isalnum(c, loc); - })) { - // new_url would be: - // https://dweb.link/ipfs/[cid]//wiki/Vincent_van_Gogh.html - if (new_url) { - *new_url = GURL(std::string( - local ? kDefaultIPFSLocalGateway : kDefaultIPFSGateway) + - (ipfs_scheme ? "/ipfs/" : "/ipns/") + cid + path); - VLOG(1) << "[IPFS] " << __func__ << " new URL: " << *new_url; - } - - return true; - } - } - return false; -} - // static bool ContentBrowserClientHelper::HandleIPFSURLReverseRewrite(GURL* url, content::BrowserContext* browser_context) { @@ -119,7 +77,7 @@ bool ContentBrowserClientHelper::HandleIPFSURLRewrite(GURL* url, content::BrowserContext* browser_context) { if (!IsIPFSDisabled(browser_context) && (url->SchemeIs(kIPFSScheme) || url->SchemeIs(kIPNSScheme))) { - return TranslateIPFSURL(*url, url, IsIPFSLocalGateway(browser_context)); + return TranslateIPFSURI(*url, url, IsIPFSLocalGateway(browser_context)); } return false; @@ -141,7 +99,7 @@ void ContentBrowserClientHelper::HandleIPFSProtocol( // static bool ContentBrowserClientHelper::IsIPFSProtocol(const GURL& url) { - return TranslateIPFSURL(url, nullptr, false); + return TranslateIPFSURI(url, nullptr, false); } } // namespace ipfs diff --git a/browser/ipfs/content_browser_client_helper.h b/browser/ipfs/content_browser_client_helper.h index 79392dfdfc08..7ed011d54fcd 100644 --- a/browser/ipfs/content_browser_client_helper.h +++ b/browser/ipfs/content_browser_client_helper.h @@ -27,8 +27,6 @@ namespace ipfs { class ContentBrowserClientHelper { public: - static bool TranslateIPFSURL(const GURL& url, GURL* new_url, bool local); - static bool HandleIPFSURLReverseRewrite(GURL* url, content::BrowserContext* browser_context); diff --git a/browser/net/BUILD.gn b/browser/net/BUILD.gn index 299cb6a3f0f8..8a39c8c3cc68 100644 --- a/browser/net/BUILD.gn +++ b/browser/net/BUILD.gn @@ -1,6 +1,7 @@ import("//brave/browser/translate/buildflags/buildflags.gni") import("//brave/components/brave_referrals/buildflags/buildflags.gni") import("//brave/components/brave_webtorrent/browser/buildflags/buildflags.gni") +import("//brave/components/ipfs/browser/buildflags/buildflags.gni") import("//build/config/features.gni") # Refer to the keyed API spec for more details about the Brave Services Key @@ -49,8 +50,10 @@ source_set("net") { "//brave/components/brave_referrals/buildflags", "//brave/components/brave_shields/browser", "//brave/components/brave_webtorrent/browser/buildflags", + "//brave/components/ipfs/browser/buildflags", "//brave/extensions:common", "//components/prefs", + "//components/user_prefs", "//content/public/browser", "//content/public/common", "//components/content_settings/core/browser", @@ -65,6 +68,17 @@ source_set("net") { "//url", ] + if (ipfs_enabled) { + sources += [ + "ipfs_redirect_network_delegate_helper.cc", + "ipfs_redirect_network_delegate_helper.h", + ] + deps += [ + "//brave/components/ipfs/browser", + "//brave/components/ipfs/common", + ] + } + if (enable_brave_referrals) { sources += [ "brave_referrals_network_delegate_helper.cc", diff --git a/browser/net/brave_request_handler.cc b/browser/net/brave_request_handler.cc index 2ddaf03862c9..79b950d1a947 100644 --- a/browser/net/brave_request_handler.cc +++ b/browser/net/brave_request_handler.cc @@ -20,6 +20,7 @@ #include "brave/components/brave_referrals/buildflags/buildflags.h" #include "brave/components/brave_rewards/browser/buildflags/buildflags.h" #include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h" +#include "brave/components/ipfs/browser/buildflags/buildflags.h" #include "chrome/browser/browser_process.h" #include "components/prefs/pref_change_registrar.h" #include "components/prefs/pref_service.h" @@ -44,6 +45,10 @@ #include "brave/browser/net/brave_translate_redirect_network_delegate_helper.h" #endif +#if BUILDFLAG(IPFS_ENABLED) +#include "brave/browser/net/ipfs_redirect_network_delegate_helper.h" +#endif + static bool IsInternalScheme(std::shared_ptr ctx) { DCHECK(ctx); return ctx->request_url.SchemeIs(extensions::kExtensionScheme) || @@ -84,6 +89,12 @@ void BraveRequestHandler::SetupCallbacks() { before_url_request_callbacks_.push_back(callback); #endif +#if BUILDFLAG(IPFS_ENABLED) + callback = + base::BindRepeating(ipfs::OnBeforeURLRequest_IPFSRedirectWork); + before_url_request_callbacks_.push_back(callback); +#endif + brave::OnBeforeStartTransactionCallback start_transaction_callback = base::Bind(brave::OnBeforeStartTransaction_SiteHacksWork); before_start_transaction_callbacks_.push_back(start_transaction_callback); diff --git a/browser/net/ipfs_redirect_network_delegate_helper.cc b/browser/net/ipfs_redirect_network_delegate_helper.cc new file mode 100644 index 000000000000..dc6b92330fd8 --- /dev/null +++ b/browser/net/ipfs_redirect_network_delegate_helper.cc @@ -0,0 +1,22 @@ +/* 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/net/ipfs_redirect_network_delegate_helper.h" + +#include "brave/components/ipfs/browser/translate_ipfs_uri.h" + +namespace ipfs { + +int OnBeforeURLRequest_IPFSRedirectWork( + const brave::ResponseCallback& next_callback, + std::shared_ptr ctx) { + GURL new_url; + if (ipfs::TranslateIPFSURI(ctx->request_url, &new_url, ctx->ipfs_local)) { + ctx->new_url_spec = new_url.spec(); + } + return net::OK; +} + +} // namespace ipfs diff --git a/browser/net/ipfs_redirect_network_delegate_helper.h b/browser/net/ipfs_redirect_network_delegate_helper.h new file mode 100644 index 000000000000..9cc00b0321b5 --- /dev/null +++ b/browser/net/ipfs_redirect_network_delegate_helper.h @@ -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_BROWSER_NET_IPFS_REDIRECT_NETWORK_DELEGATE_HELPER_H_ +#define BRAVE_BROWSER_NET_IPFS_REDIRECT_NETWORK_DELEGATE_HELPER_H_ + +#include +#include "brave/browser/net/url_context.h" + +namespace ipfs { + +int OnBeforeURLRequest_IPFSRedirectWork( + const brave::ResponseCallback& next_callback, + std::shared_ptr ctx); + +} // namespace ipfs + +#endif // BRAVE_BROWSER_NET_IPFS_REDIRECT_NETWORK_DELEGATE_HELPER_H_ diff --git a/browser/net/ipfs_redirect_network_delegate_helper_unittest.cc b/browser/net/ipfs_redirect_network_delegate_helper_unittest.cc new file mode 100644 index 000000000000..007782a5cddd --- /dev/null +++ b/browser/net/ipfs_redirect_network_delegate_helper_unittest.cc @@ -0,0 +1,76 @@ +/* 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/net/ipfs_redirect_network_delegate_helper.h" + +#include +#include +#include + +#include "brave/browser/net/url_context.h" +#include "brave/common/translate_network_constants.h" +#include "chrome/test/base/chrome_render_view_host_test_harness.h" +#include "net/traffic_annotation/network_traffic_annotation_test_helper.h" +#include "net/url_request/url_request_test_util.h" +#include "url/gurl.h" + +namespace { + +TEST(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIHTTPScheme) { + GURL url("http://a.com/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG"); + auto brave_request_info = std::make_shared(url); + int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(), + brave_request_info); + EXPECT_EQ(rc, net::OK); + EXPECT_TRUE(brave_request_info->new_url_spec.empty()); +} + +TEST(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPFSSchemeLocal) { + GURL url("ipfs://QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG"); + auto brave_request_info = std::make_shared(url); + brave_request_info->ipfs_local = true; + int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(), + brave_request_info); + EXPECT_EQ(rc, net::OK); + EXPECT_EQ(brave_request_info->new_url_spec, + "http://127.0.0.1:8080/ipfs/" + "QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG"); +} + +TEST(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPFSScheme) { + GURL url("ipfs://QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG"); + auto brave_request_info = std::make_shared(url); + brave_request_info->ipfs_local = false; + int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(), + brave_request_info); + EXPECT_EQ(rc, net::OK); + EXPECT_EQ(brave_request_info->new_url_spec, + "https://dweb.link/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG"); +} + +TEST(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPNSSchemeLocal) { + GURL url("ipns://QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd"); + auto brave_request_info = std::make_shared(url); + brave_request_info->ipfs_local = true; + int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(), + brave_request_info); + EXPECT_EQ(rc, net::OK); + EXPECT_EQ(brave_request_info->new_url_spec, + "http://127.0.0.1:8080/ipns/" + "QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd"); +} + +TEST(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPNSScheme) { + GURL url("ipns://QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd"); + auto brave_request_info = std::make_shared(url); + brave_request_info->ipfs_local = false; + int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(), + brave_request_info); + EXPECT_EQ(rc, net::OK); + EXPECT_EQ(brave_request_info->new_url_spec, + "https://dweb.link/ipns/QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd"); +} + +} // namespace diff --git a/browser/net/url_context.cc b/browser/net/url_context.cc index b49e4878bcc3..42b0b61f14f4 100644 --- a/browser/net/url_context.cc +++ b/browser/net/url_context.cc @@ -12,11 +12,19 @@ #include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h" #include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h" #include "brave/components/brave_webtorrent/browser/webtorrent_util.h" +#include "brave/components/ipfs/browser/buildflags/buildflags.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h" #include "chrome/browser/profiles/profile.h" #include "content/public/browser/browser_thread.h" #include "net/base/isolation_info.h" +#if BUILDFLAG(IPFS_ENABLED) +#include "brave/common/pref_names.h" +#include "brave/components/ipfs/common/ipfs_constants.h" +#include "components/prefs/testing_pref_service.h" +#include "components/user_prefs/user_prefs.h" +#endif + namespace brave { namespace { @@ -109,6 +117,13 @@ void BraveRequestInfo::FillCTX(const network::ResourceRequest& request, !brave_shields::GetHTTPSEverywhereEnabled(map, ctx->tab_origin); ctx->allow_referrers = brave_shields::AllowReferrers(map, ctx->tab_origin); ctx->upload_data = GetUploadData(request); + +#if BUILDFLAG(IPFS_ENABLED) + auto* prefs = user_prefs::UserPrefs::Get(browser_context); + ctx->ipfs_local = static_cast( + prefs->GetInteger(kIPFSResolveMethod)) == + ipfs::IPFSResolveMethodTypes::IPFS_LOCAL; +#endif } } // namespace brave diff --git a/browser/net/url_context.h b/browser/net/url_context.h index e8f4337eba91..8fb415544514 100644 --- a/browser/net/url_context.h +++ b/browser/net/url_context.h @@ -91,6 +91,7 @@ struct BraveRequestInfo { BlockedBy blocked_by = kNotBlocked; bool cancel_request_explicitly = false; std::string mock_data_url; + bool ipfs_local = true; // Default to invalid type for resource_type, so delegate helpers // can properly detect that the info couldn't be obtained. diff --git a/common/url_constants.cc b/common/url_constants.cc index 38ed1cd27b3f..636cd1587ba5 100644 --- a/common/url_constants.cc +++ b/common/url_constants.cc @@ -9,10 +9,6 @@ const char kChromeExtensionScheme[] = "chrome-extension"; const char kBraveUIScheme[] = "brave"; const char kMagnetScheme[] = "magnet"; -const char kIPFSScheme[] = "ipfs"; -const char kIPNSScheme[] = "ipns"; -const char kDefaultIPFSGateway[] = "https://dweb.link"; -const char kDefaultIPFSLocalGateway[] = "http://127.0.0.1:8080"; const char kBinanceScheme[] = "com.brave.binance"; const char kGeminiScheme[] = "com.brave.gemini"; const char kWidevineMoreInfoURL[] = "https://www.eff.org/issues/drm"; diff --git a/common/url_constants.h b/common/url_constants.h index 31e0c48cfc9b..fd2d5417e245 100644 --- a/common/url_constants.h +++ b/common/url_constants.h @@ -10,10 +10,6 @@ extern const char kChromeExtensionScheme[]; extern const char kBraveUIScheme[]; extern const char kMagnetScheme[]; -extern const char kIPFSScheme[]; -extern const char kIPNSScheme[]; -extern const char kDefaultIPFSGateway[]; -extern const char kDefaultIPFSLocalGateway[]; extern const char kBinanceScheme[]; extern const char kGeminiScheme[]; extern const char kWidevineMoreInfoURL[]; diff --git a/components/ipfs/browser/BUILD.gn b/components/ipfs/browser/BUILD.gn index 0ea84fda323e..010e8f4a713f 100644 --- a/components/ipfs/browser/BUILD.gn +++ b/components/ipfs/browser/BUILD.gn @@ -8,11 +8,15 @@ source_set("browser") { "features.h", "ipfs_json_parser.cc", "ipfs_json_parser.h", + "translate_ipfs_uri.cc", + "translate_ipfs_uri.h", ] deps = [ "//base", "//brave/components/brave_component_updater/browser/", + "//brave/components/ipfs/common", "//third_party/re2", + "//url", ] } diff --git a/components/ipfs/browser/translate_ipfs_uri.cc b/components/ipfs/browser/translate_ipfs_uri.cc new file mode 100644 index 000000000000..477ea4063a77 --- /dev/null +++ b/components/ipfs/browser/translate_ipfs_uri.cc @@ -0,0 +1,57 @@ +/* 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/browser/translate_ipfs_uri.h" + +#include + +#include "base/logging.h" +#include "brave/components/ipfs/common/ipfs_constants.h" + +namespace ipfs { + +bool TranslateIPFSURI( + const GURL& url, GURL* new_url, bool local) { + if (!url.SchemeIs(kIPFSScheme) && !url.SchemeIs(kIPNSScheme)) { + return false; + } + + std::string path = url.path(); + // In the case of a URL like ipfs://[cid]/wiki/Vincent_van_Gogh.html + // host is empty and path is //wiki/Vincent_van_Gogh.html + if (url.host().empty() && path.length() > 2 && + path.substr(0, 2) == "//") { + std::string cid(path.substr(2)); + // If we have a path after the CID, get at the real resource path + size_t pos = cid.find("/"); + std::string path; + if (pos != std::string::npos && pos != 0) { + // path would be /wiki/Vincent_van_Gogh.html + path = cid.substr(pos, cid.length() - pos); + // cid would be [cid] + cid = cid.substr(0, pos); + } + bool ipfs_scheme = url.scheme() == kIPFSScheme; + bool ipns_scheme = url.scheme() == kIPNSScheme; + if ((ipfs_scheme || ipns_scheme) && std::all_of(cid.begin(), cid.end(), + [loc = std::locale{}](char c) { + return std::isalnum(c, loc); + })) { + // new_url would be: + // https://dweb.link/ipfs/[cid]//wiki/Vincent_van_Gogh.html + if (new_url) { + *new_url = GURL(std::string( + local ? kDefaultIPFSLocalGateway : kDefaultIPFSGateway) + + (ipfs_scheme ? "/ipfs/" : "/ipns/") + cid + path); + VLOG(1) << "[IPFS] " << __func__ << " new URL: " << *new_url; + } + + return true; + } + } + return false; +} + +} // namespace ipfs diff --git a/components/ipfs/browser/translate_ipfs_uri.h b/components/ipfs/browser/translate_ipfs_uri.h new file mode 100644 index 000000000000..d6963e246131 --- /dev/null +++ b/components/ipfs/browser/translate_ipfs_uri.h @@ -0,0 +1,17 @@ +/* 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_BROWSER_TRANSLATE_IPFS_URI_H_ +#define BRAVE_COMPONENTS_IPFS_BROWSER_TRANSLATE_IPFS_URI_H_ + +#include "url/gurl.h" + +namespace ipfs { + +bool TranslateIPFSURI(const GURL& url, GURL* new_url, bool local); + +} // namespace ipfs + +#endif // BRAVE_COMPONENTS_IPFS_BROWSER_TRANSLATE_IPFS_URI_H_ diff --git a/browser/ipfs/content_browser_client_helper_unittest.cc b/components/ipfs/browser/translate_ipfs_uri_unittest.cc similarity index 73% rename from browser/ipfs/content_browser_client_helper_unittest.cc rename to components/ipfs/browser/translate_ipfs_uri_unittest.cc index c5601bfd1708..925ae0c4278e 100644 --- a/browser/ipfs/content_browser_client_helper_unittest.cc +++ b/components/ipfs/browser/translate_ipfs_uri_unittest.cc @@ -3,7 +3,7 @@ * 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/content_browser_client_helper.h" +#include "brave/components/ipfs/browser/translate_ipfs_uri.h" #include #include @@ -28,57 +28,57 @@ using IPFSBraveContentBrowserClientTest = testing::Test; namespace ipfs { -TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURLNotIPFSScheme) { +TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURINotIPFSScheme) { GURL url("http://a.com/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG"); GURL new_url; ASSERT_FALSE( - ContentBrowserClientHelper::TranslateIPFSURL(url, &new_url, false)); + ipfs::TranslateIPFSURI(url, &new_url, false)); } -TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURLIPFSScheme) { +TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURIIPFSScheme) { GURL url("ipfs://QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG"); GURL new_url; ASSERT_TRUE( - ContentBrowserClientHelper::TranslateIPFSURL(url, &new_url, false)); + ipfs::TranslateIPFSURI(url, &new_url, false)); EXPECT_EQ(new_url, GURL( "https://dweb.link/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG")); } -TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURLIPNSScheme) { +TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURIIPNSScheme) { GURL url("ipns://QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd"); GURL new_url; ASSERT_TRUE( - ContentBrowserClientHelper::TranslateIPFSURL(url, &new_url, false)); + ipfs::TranslateIPFSURI(url, &new_url, false)); EXPECT_EQ(new_url, GURL( "https://dweb.link/ipns/QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd")); } -TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURLIPFSSchemeLocal) { +TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURIIPFSSchemeLocal) { GURL url("ipfs://QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG"); GURL new_url; ASSERT_TRUE( - ContentBrowserClientHelper::TranslateIPFSURL(url, &new_url, true)); + ipfs::TranslateIPFSURI(url, &new_url, true)); EXPECT_EQ(new_url, GURL( "http://127.0.0.1:8080/ipfs/" "QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG")); } -TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURLIPNSSchemeLocal) { +TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURIIPNSSchemeLocal) { GURL url("ipns://QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd"); GURL new_url; ASSERT_TRUE( - ContentBrowserClientHelper::TranslateIPFSURL(url, &new_url, true)); + ipfs::TranslateIPFSURI(url, &new_url, true)); EXPECT_EQ(new_url, GURL( "http://127.0.0.1:8080/ipns/" "QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd")); } -TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURLIPFSSchemeWithPath) { +TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURIIPFSSchemeWithPath) { GURL url("ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq" "/wiki/Vincent_van_Gogh.html"); GURL new_url; ASSERT_TRUE( - ContentBrowserClientHelper::TranslateIPFSURL(url, &new_url, false)); + ipfs::TranslateIPFSURI(url, &new_url, false)); EXPECT_EQ(new_url, GURL( "https://dweb.link/ipfs/" "bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq" diff --git a/components/ipfs/common/ipfs_constants.cc b/components/ipfs/common/ipfs_constants.cc index c58bf34ca89d..ac4641e13b6c 100644 --- a/components/ipfs/common/ipfs_constants.cc +++ b/components/ipfs/common/ipfs_constants.cc @@ -13,4 +13,9 @@ const char kConfigPath[] = "/api/v0/config"; const char kArgQueryParam[] = "arg"; const char kAddressesField[] = "Addresses"; const char kShutdownPath[] = "/api/v0/shutdown"; +const char kIPFSScheme[] = "ipfs"; +const char kIPNSScheme[] = "ipns"; +const char kDefaultIPFSGateway[] = "https://dweb.link"; +const char kDefaultIPFSLocalGateway[] = "http://127.0.0.1:8080"; + } // namespace ipfs diff --git a/components/ipfs/common/ipfs_constants.h b/components/ipfs/common/ipfs_constants.h index 0a2edc7adf39..222e59fe1e34 100644 --- a/components/ipfs/common/ipfs_constants.h +++ b/components/ipfs/common/ipfs_constants.h @@ -14,6 +14,10 @@ extern const char kConfigPath[]; extern const char kArgQueryParam[]; extern const char kAddressesField[]; extern const char kShutdownPath[]; +extern const char kIPFSScheme[]; +extern const char kIPNSScheme[]; +extern const char kDefaultIPFSGateway[]; +extern const char kDefaultIPFSLocalGateway[]; enum class IPFSResolveMethodTypes { IPFS_GATEWAY, diff --git a/test/BUILD.gn b/test/BUILD.gn index 7d001972b0b2..814bf8e09f3e 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -232,8 +232,9 @@ test("brave_unit_tests") { if (ipfs_enabled) { sources += [ "//brave/browser/ipfs/ipfs_navigation_throttle_unittest.cc", - "//brave/browser/ipfs/content_browser_client_helper_unittest.cc", + "//brave/browser/net/ipfs_redirect_network_delegate_helper_unittest.cc", "//brave/components/ipfs/browser/ipfs_json_parser_unittest.cc", + "//brave/components/ipfs/browser/translate_ipfs_uri_unittest.cc", "//brave/components/ipfs/common/ipfs_utils_unittest.cc", ]