Skip to content

Commit

Permalink
Fix brave/brave-ios#7013: IPFS navigation in omnibox (brave/brave-ios…
Browse files Browse the repository at this point in the history
  • Loading branch information
nuo-xu authored Apr 6, 2023
1 parent b428f42 commit edd9b3b
Show file tree
Hide file tree
Showing 27 changed files with 711 additions and 57 deletions.
3 changes: 2 additions & 1 deletion App/iOS/Delegates/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
(SessionRestoreHandler.path, SessionRestoreHandler()),
(ErrorPageHandler.path, ErrorPageHandler()),
(ReaderModeHandler.path, ReaderModeHandler(profile: profile)),
(SNSDomainHandler.path, SNSDomainHandler())
(SNSDomainHandler.path, SNSDomainHandler()),
(IPFSSchemeHandler.path, IPFSSchemeHandler())
]

responders.forEach { (path, responder) in
Expand Down
1 change: 1 addition & 0 deletions App/iOS/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<array>
<string>http</string>
<string>https</string>
<string>ipfs</string>
<string>$(BRAVE_URL_SCHEME)</string>
</array>
</dict>
Expand Down
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ var braveTarget: PackageDescription.Target = .target(
.copy("Assets/Interstitial Pages/Pages/GenericError.html"),
.copy("Assets/Interstitial Pages/Pages/NetworkError.html"),
.copy("Assets/Interstitial Pages/Pages/SNSDomain.html"),
.copy("Assets/Interstitial Pages/Pages/IPFSPreference.html"),
.copy("Assets/Interstitial Pages/Images/Carret.png"),
.copy("Assets/Interstitial Pages/Images/Clock.svg"),
.copy("Assets/Interstitial Pages/Images/Cloud.svg"),
Expand All @@ -378,10 +379,13 @@ var braveTarget: PackageDescription.Target = .target(
.copy("Assets/Interstitial Pages/Images/Globe.svg"),
.copy("Assets/Interstitial Pages/Images/Info.svg"),
.copy("Assets/Interstitial Pages/Images/Warning.svg"),
.copy("Assets/Interstitial Pages/Images/BraveIPFS.svg"),
.copy("Assets/Interstitial Pages/Images/IPFSBackground.svg"),
.copy("Assets/Interstitial Pages/Styles/CertificateError.css"),
.copy("Assets/Interstitial Pages/Styles/InterstitialStyles.css"),
.copy("Assets/Interstitial Pages/Styles/NetworkError.css"),
.copy("Assets/Interstitial Pages/Styles/SNSDomain.css"),
.copy("Assets/Interstitial Pages/Styles/IPFSPreference.css"),
.copy("Assets/SearchPlugins"),
.copy("Frontend/Reader/Reader.css"),
.copy("Frontend/Reader/Reader.html"),
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions Sources/Brave/Assets/Interstitial Pages/Images/IPFSBackground.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions Sources/Brave/Assets/Interstitial Pages/Pages/IPFSPreference.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!--
Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/.
-->

<head>
<meta name='viewport' content='initial-scale=1, maximum-scale=1, viewport-fit=cover'>
<meta name="referrer" content="no-referrer">
<title>%page_title%</title>
<link rel="stylesheet" href="internal://local/interstitial-style/InterstitialStyles.css">
<link rel="stylesheet" href="internal://local/interstitial-style/IPFSPreference.css">
</head>

<body dir="&locale.dir;" class="background">
<div class="container post">
<div class="header-logo">
<img src="internal://local/interstitial-icon/BraveIPFS.svg" alt="Icon" class="icon" />
</div>
<div>
<span class="title">%interstitial_ipfs_title%</span>
<p class="description">%interstitial_ipfs_privacy%
</p>
</div>
<div class="control-box">
<p class="option">%interstitial_ipfs_public_gateway%
</p>
<button id="disableIPFSButton" class="disableButton disableButtonTitle">%button_disable%</button>
<button id="proceedIPFSButton" class="proceedButton proceedButtonTitle">%button_procced%</button>
</div>

<div class="footer-container">
<div class="ipfs-footer"></div>
</div>

<script type="text/javascript">
var disableButton = document.getElementById("disableIPFSButton")
disableButton.addEventListener('click', function(e) {
e.preventDefault();
webkit.messageHandlers["%message_handler%"].postMessage({
"type": "IPFSDisable",
});
});
var proceedButton = document.getElementById("proceedIPFSButton")
proceedButton.addEventListener('click', function(e) {
e.preventDefault();
webkit.messageHandlers["%message_handler%"].postMessage({
"type": "IPFSProceed",
});
});

</script>
</div>
</body>
137 changes: 137 additions & 0 deletions Sources/Brave/Assets/Interstitial Pages/Styles/IPFSPreference.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/.
*/

* {
-webkit-tap-highlight-color: transparent;
}

.background {
background-color: rgb(250, 249, 254);
}

.icon {
width: 128px;
height: 128px;
}

.footer-container {
box-sizing: border-box;
width: 100%;
aspect-ratio: 1440 / 136;
bottom: 0;
position: fixed;
}

.ipfs-footer {
box-sizing: border-box;
background: url('internal://local/interstitial-icon/IPFSBackground.svg');
width: 100%;
aspect-ratio: 1440 / 136;
background-size: cover;
background-position-x: center;
}

.header-logo {
margin: 20px 0 20px 0;
}

.disableButton {
background: rgb(255, 255, 255);
border-radius: 100px;
background-repeat: no-repeat;
border: 1px solid rgb(76, 84, 210);
height: 44px;
margin-right: auto;
padding-left: 20px;
padding-right: 20px;
margin-left: auto;
margin-bottom: 20px;
}

.disableButtonTitle {
color: rgb(76, 84, 210);
font-size: 16px;
font-family: SFProText-Semibold, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
font-weight: 600;
line-height: 16px;

cursor: pointer;
overflow: hidden;
outline: none;
}

.proceedButton {
background: rgb(76, 84, 210);
border-radius: 100px;
background-repeat: no-repeat;
border: none;
height: 44px;
margin-right: auto;
padding-left: 20px;
padding-right: 20px;
margin-left: auto;
}

.proceedButtonTitle {
color: rgb(255, 255, 255);
font-size: 16px;
font-family: SFProText-Semibold, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
font-weight: 600;
text-align: center;
line-height: 16px;

cursor: pointer;
overflow: hidden;
outline: none;
}

.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
}

.control-box {
display: block;
}

.description {
color: rgb(0, 0, 0);
}

.option {
color: rgb(82, 82, 82);
font-size: 14px;
font-family: SFProText-Regular, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
font-weight: normal;
letter-spacing: 0px;
line-height: 19px;
}

@media (prefers-color-scheme: dark) {
.background {
background: #17171F;
}

.title {
color: white;
}

.description {
color: white;
}

.option {
color: white;
}

a {
color: white;
}
}
19 changes: 19 additions & 0 deletions Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,19 @@ public class BrowserViewController: UIViewController {
updateWebViewPageZoom(tab: tab)
}
}

func showIPFSInterstitialPage(originalURL: URL, visitType: VisitType) {
topToolbar.leaveOverlayMode()

guard let tab = tabManager.selectedTab, let encodedURL = originalURL.absoluteString.addingPercentEncoding(withAllowedCharacters: .alphanumerics), let internalUrl = URL(string: "\(InternalURL.baseUrl)/\(IPFSSchemeHandler.path)?url=\(encodedURL)") else {
return
}
let scriptHandler = tab.getContentScript(name: Web3IPFSScriptHandler.scriptName) as? Web3IPFSScriptHandler
scriptHandler?.originalURL = originalURL
scriptHandler?.visitType = visitType

tab.webView?.load(PrivilegedRequest(url: internalUrl) as URLRequest)
}

func showSNSDomainInterstitialPage(originalURL: URL, visitType: VisitType) {
topToolbar.leaveOverlayMode()
Expand Down Expand Up @@ -2485,6 +2498,7 @@ extension BrowserViewController: TabDelegate {
CosmeticFiltersScriptHandler(tab: tab),
FaviconScriptHandler(tab: tab),
Web3NameServiceScriptHandler(tab: tab),
Web3IPFSScriptHandler(tab: tab),

tab.contentBlocker,
tab.requestBlockingContentHelper,
Expand Down Expand Up @@ -2523,6 +2537,7 @@ extension BrowserViewController: TabDelegate {
(tab.getContentScript(name: PlaylistScriptHandler.scriptName) as? PlaylistScriptHandler)?.delegate = self
(tab.getContentScript(name: PlaylistFolderSharingScriptHandler.scriptName) as? PlaylistFolderSharingScriptHandler)?.delegate = self
(tab.getContentScript(name: Web3NameServiceScriptHandler.scriptName) as? Web3NameServiceScriptHandler)?.delegate = self
(tab.getContentScript(name: Web3IPFSScriptHandler.scriptName) as? Web3IPFSScriptHandler)?.delegate = self
}

func tab(_ tab: Tab, willDeleteWebView webView: WKWebView) {
Expand Down Expand Up @@ -2664,6 +2679,10 @@ extension BrowserViewController: TabDelegate {
topToolbar.updateWalletButtonState(.inactive)
}
}

func reloadIPFSSchemeUrl(_ url: URL) {
handleIPFSSchemeURL(url, visitType: .unknown)
}

@MainActor
private func isPendingRequestAvailable() async -> Bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 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 https://mozilla.org/MPL/2.0/.

import Foundation
import BraveWallet
import BraveShared
import BraveCore
import Data

extension BrowserViewController: Web3IPFSScriptHandlerDelegate {
func web3IPFSDecisionHandler(_ proceed: Bool, originalURL: URL, visitType: VisitType) {
if proceed {
if let resolvedUrl = braveCore.ipfsAPI.resolveGatewayUrl(for: originalURL) {
finishEditingAndSubmit(resolvedUrl, visitType: visitType)
} else {
finishEditingAndSubmit(originalURL, visitType: visitType)
}
Preferences.Wallet.resolveIPFSResources.value = Preferences.Wallet.Web3IPFSOption.enabled.rawValue
} else {
if let errorPageHelper = tabManager.selectedTab?.getContentScript(name: ErrorPageHelper.scriptName) as? ErrorPageHelper, let webView = tabManager.selectedTab?.webView {
errorPageHelper.loadPage(IPFSErrorPageHandler.disabledError, forUrl: originalURL, inWebView: webView)
}

Preferences.Wallet.resolveIPFSResources.value = Preferences.Wallet.Web3IPFSOption.disabled.rawValue
}
}
}
Loading

0 comments on commit edd9b3b

Please sign in to comment.