120 lines
4.4 KiB
C++
120 lines
4.4 KiB
C++
/* 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_TAB_HELPER_H_
|
|
#define BRAVE_BROWSER_IPFS_IPFS_TAB_HELPER_H_
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "brave/browser/ipfs/import/ipfs_import_controller.h"
|
|
#include "brave/browser/ipfs/ipfs_host_resolver.h"
|
|
#include "components/prefs/pref_change_registrar.h"
|
|
#include "content/public/browser/web_contents_observer.h"
|
|
#include "content/public/browser/web_contents_user_data.h"
|
|
|
|
namespace content {
|
|
class NavigationHandle;
|
|
class WebContents;
|
|
} // namespace content
|
|
|
|
class PrefService;
|
|
|
|
namespace ipfs {
|
|
class IPFSHostResolver;
|
|
class IpfsImportController;
|
|
|
|
// Determines if IPFS should be active for a given top-level navigation.
|
|
class IPFSTabHelper : public content::WebContentsObserver,
|
|
public IpfsImportController,
|
|
public content::WebContentsUserData<IPFSTabHelper> {
|
|
public:
|
|
IPFSTabHelper(const IPFSTabHelper&) = delete;
|
|
IPFSTabHelper& operator=(IPFSTabHelper&) = delete;
|
|
~IPFSTabHelper() override;
|
|
|
|
static bool MaybeCreateForWebContents(content::WebContents* web_contents);
|
|
GURL GetIPFSResolvedURL() const;
|
|
|
|
void SetResolverForTesting(std::unique_ptr<IPFSHostResolver> resolver) {
|
|
resolver_ = std::move(resolver);
|
|
}
|
|
|
|
IpfsImportController* GetImportController() {
|
|
return static_cast<IpfsImportController*>(this);
|
|
}
|
|
|
|
void SetPageURLForTesting(const GURL& url) {
|
|
current_page_url_for_testing_ = url;
|
|
}
|
|
|
|
private:
|
|
FRIEND_TEST_ALL_PREFIXES(IpfsTabHelperUnitTest, CanResolveURLTest);
|
|
FRIEND_TEST_ALL_PREFIXES(
|
|
IpfsTabHelperUnitTest,
|
|
TranslateUrlToIpns_When_HasDNSLinkRecord_AndXIPFSPathHeader);
|
|
FRIEND_TEST_ALL_PREFIXES(
|
|
IpfsTabHelperUnitTest,
|
|
TranslateUrlToIpns_When_HasDNSLinkRecord_AndOriginalPageFails_500);
|
|
FRIEND_TEST_ALL_PREFIXES(
|
|
IpfsTabHelperUnitTest,
|
|
DoNotTranslateUrlToIpns_When_HasDNSLinkRecord_AndOriginalPageFails_400);
|
|
FRIEND_TEST_ALL_PREFIXES(
|
|
IpfsTabHelperUnitTest,
|
|
TranslateUrlToIpns_When_HasDNSLinkRecord_AndOriginalPageFails_505);
|
|
FRIEND_TEST_ALL_PREFIXES(IpfsTabHelperUnitTest,
|
|
DoNotTranslateUrlToIpns_When_NoDNSLinkRecord);
|
|
FRIEND_TEST_ALL_PREFIXES(IpfsTabHelperUnitTest,
|
|
DoNotTranslateUrlToIpns_When_NoHeader_And_NoError);
|
|
FRIEND_TEST_ALL_PREFIXES(IpfsTabHelperUnitTest,
|
|
DNSLinkRecordResolved_AutoRedirectDNSLink);
|
|
FRIEND_TEST_ALL_PREFIXES(IpfsTabHelperUnitTest,
|
|
XIpfsPathHeaderUsed_IfNoDnsLinkRecord_IPFS);
|
|
FRIEND_TEST_ALL_PREFIXES(IpfsTabHelperUnitTest,
|
|
XIpfsPathHeaderUsed_IfNoDnsLinkRecord_IPNS);
|
|
FRIEND_TEST_ALL_PREFIXES(IpfsTabHelperUnitTest, ResolveXIPFSPathUrl);
|
|
FRIEND_TEST_ALL_PREFIXES(IpfsTabHelperUnitTest, GatewayResolving);
|
|
|
|
friend class content::WebContentsUserData<IPFSTabHelper>;
|
|
explicit IPFSTabHelper(content::WebContents* web_contents);
|
|
|
|
GURL GetCurrentPageURL() const;
|
|
bool CanResolveURL(const GURL& url) const;
|
|
bool IsDNSLinkCheckEnabled() const;
|
|
void XIPFSPathLinkResolved(const GURL& ipfs);
|
|
void DNSLinkResolved(const GURL& ipfs);
|
|
void MaybeCheckDNSLinkRecord(const net::HttpResponseHeaders* headers);
|
|
void UpdateDnsLinkButtonState();
|
|
|
|
GURL ResolveDNSLinkUrl(const GURL& url);
|
|
GURL ResolveXIPFSPathUrl(const std::string& x_ipfs_path_header_value);
|
|
|
|
void MaybeSetupIpfsProtocolHandlers(const GURL& url);
|
|
|
|
// content::WebContentsObserver
|
|
void DidFinishNavigation(
|
|
content::NavigationHandle* navigation_handle) override;
|
|
void UpdateLocationBar();
|
|
|
|
void CheckDNSLinkRecord(absl::optional<std::string> x_ipfs_path_header);
|
|
void HostResolvedCallback(absl::optional<std::string> x_ipfs_path_header,
|
|
const std::string& host,
|
|
const absl::optional<std::string>& dnslink);
|
|
|
|
PrefService* pref_service_ = nullptr;
|
|
PrefChangeRegistrar pref_change_registrar_;
|
|
GURL ipfs_resolved_url_;
|
|
GURL current_page_url_for_testing_;
|
|
std::unique_ptr<IPFSHostResolver> resolver_;
|
|
base::WeakPtrFactory<IPFSTabHelper> weak_ptr_factory_{this};
|
|
WEB_CONTENTS_USER_DATA_KEY_DECL();
|
|
};
|
|
|
|
} // namespace ipfs
|
|
|
|
#endif // BRAVE_BROWSER_IPFS_IPFS_TAB_HELPER_H_
|