Skip to content

Commit

Permalink
Fix #8305 - Ref #8297: Allow Syncing All History - Update brave-core …
Browse files Browse the repository at this point in the history
…to 1.60.105 (#8306)
  • Loading branch information
soner-yuksel authored Oct 27, 2023
1 parent 22dc4d0 commit b14b83e
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 153 deletions.
49 changes: 17 additions & 32 deletions Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public class BrowserViewController: UIViewController {
var addToPlayListActivityItem: (enabled: Bool, item: PlaylistInfo?)? // A boolean to determine If AddToListActivity should be added
var openInPlaylistActivityItem: (enabled: Bool, item: PlaylistInfo?)? // A boolean to determine if OpenInPlaylistActivity should be shown

var typedNavigation = [URL: VisitType]()
var navigationToolbar: ToolbarProtocol {
return toolbar ?? topToolbar
}
Expand Down Expand Up @@ -263,8 +262,6 @@ public class BrowserViewController: UIViewController {
/// The currently open WalletStore
weak var walletStore: WalletStore?

var lastEnteredURLVisitType: VisitType = .unknown

var processAddressBarTask: Task<(), Never>?
var topToolbarDidPressReloadTask: Task<(), Never>?

Expand Down Expand Up @@ -1579,7 +1576,7 @@ public class BrowserViewController: UIViewController {
UIApplication.shared.shortcutItems = Preferences.Privacy.privateBrowsingOnly.value ? [privateTabItem, scanQRCodeItem] : [newTabItem, privateTabItem, scanQRCodeItem]
}

func finishEditingAndSubmit(_ url: URL, visitType: VisitType) {
func finishEditingAndSubmit(_ url: URL) {
if url.isBookmarklet {
topToolbar.leaveOverlayMode()

Expand All @@ -1591,7 +1588,7 @@ public class BrowserViewController: UIViewController {
// Disable any sort of privileged execution contexts
// IE: The user must explicitly tap a bookmark they have saved.
// Block all other contexts such as redirects, downloads, embed, linked, etc..
if visitType == .bookmark, let webView = tab.webView, let code = url.bookmarkletCodeComponent {
if let webView = tab.webView, let code = url.bookmarkletCodeComponent {
webView.evaluateSafeJavaScript(
functionName: code,
contentWorld: .bookmarkletSandbox,
Expand All @@ -1612,26 +1609,23 @@ public class BrowserViewController: UIViewController {

tab.loadRequest(URLRequest(url: url))

// Recording the last Visit Type for the url submitted
lastEnteredURLVisitType = visitType
updateWebViewPageZoom(tab: tab)
}
}

func showIPFSInterstitialPage(originalURL: URL, visitType: VisitType) {
func showIPFSInterstitialPage(originalURL: URL) {
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 showWeb3ServiceInterstitialPage(service: Web3Service, originalURL: URL, visitType: VisitType = .unknown) {
func showWeb3ServiceInterstitialPage(service: Web3Service, originalURL: URL) {
topToolbar.leaveOverlayMode()

guard let tab = tabManager.selectedTab,
Expand All @@ -1641,7 +1635,6 @@ public class BrowserViewController: UIViewController {
}
let scriptHandler = tab.getContentScript(name: Web3NameServiceScriptHandler.scriptName) as? Web3NameServiceScriptHandler
scriptHandler?.originalURL = originalURL
scriptHandler?.visitType = visitType

tab.webView?.load(PrivilegedRequest(url: internalUrl) as URLRequest)
}
Expand Down Expand Up @@ -1960,7 +1953,7 @@ public class BrowserViewController: UIViewController {
if let url = url {
// If only empty tab present, the url will open in existing tab
if tabManager.isBrowserEmptyForCurrentMode {
finishEditingAndSubmit(url, visitType: .link)
finishEditingAndSubmit(url)
return
}
request = isPrivileged ? PrivilegedRequest(url: url) as URLRequest : URLRequest(url: url)
Expand Down Expand Up @@ -2371,16 +2364,8 @@ public class BrowserViewController: UIViewController {

// Only add history of a url which is not a localhost url
if !url.isReaderModeURL {
// The visitType is checked If it is "typed" or not to determine the History object we are adding
// should be synced or not. This limitation exists on browser side so we are aligning with this
if !tab.isPrivate {
if let visitType = typedNavigation.first(where: {
$0.key.typedDisplayString == url.typedDisplayString
})?.value, visitType == .typed {
braveCore.historyAPI.add(url: url, title: tab.title, dateAdded: Date())
} else {
braveCore.historyAPI.add(url: url, title: tab.title, dateAdded: Date(), isURLTyped: false)
}
braveCore.historyAPI.add(url: url, title: tab.title, dateAdded: Date())
}

// Saving Tab.
Expand Down Expand Up @@ -2823,7 +2808,7 @@ extension BrowserViewController: TabDelegate {
}

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

func didReloadTab(_ tab: Tab) {
Expand Down Expand Up @@ -2860,11 +2845,11 @@ extension BrowserViewController: TabDelegate {
extension BrowserViewController: SearchViewControllerDelegate {
func searchViewController(_ searchViewController: SearchViewController, didSubmit query: String, braveSearchPromotion: Bool) {
topToolbar.leaveOverlayMode()
processAddressBar(text: query, visitType: .typed, isBraveSearchPromotion: braveSearchPromotion)
processAddressBar(text: query, isBraveSearchPromotion: braveSearchPromotion)
}

func searchViewController(_ searchViewController: SearchViewController, didSelectURL url: URL) {
finishEditingAndSubmit(url, visitType: .typed)
finishEditingAndSubmit(url)
}

func searchViewController(_ searchViewController: SearchViewController, didSelectOpenTab tabInfo: (id: UUID?, url: URL)) {
Expand Down Expand Up @@ -2958,30 +2943,30 @@ extension BrowserViewController: ToolbarUrlActionsDelegate {
func openInNewTab(_ url: URL, isPrivate: Bool) {
topToolbar.leaveOverlayMode()

select(url, visitType: .unknown, action: .openInNewTab(isPrivate: isPrivate))
select(url, action: .openInNewTab(isPrivate: isPrivate))
}

func copy(_ url: URL) {
select(url, visitType: .unknown, action: .copy)
select(url, action: .copy)
}

func share(_ url: URL) {
select(url, visitType: .unknown, action: .share)
select(url, action: .share)
}

func batchOpen(_ urls: [URL]) {
let tabIsPrivate = TabType.of(tabManager.selectedTab).isPrivate
self.tabManager.addTabsForURLs(urls, zombie: false, isPrivate: tabIsPrivate)
}

func select(url: URL, visitType: VisitType) {
select(url, visitType: visitType, action: .openInCurrentTab)
func select(url: URL) {
select(url, action: .openInCurrentTab)
}

private func select(_ url: URL, visitType: VisitType, action: ToolbarURLAction) {
private func select(_ url: URL, action: ToolbarURLAction) {
switch action {
case .openInCurrentTab:
finishEditingAndSubmit(url, visitType: visitType)
finishEditingAndSubmit(url)
updateURLBarWalletButton()
case .openInNewTab(let isPrivate):
let tab = tabManager.addTab(PrivilegedRequest(url: url) as URLRequest, afterTab: tabManager.selectedTab, isPrivate: isPrivate)
Expand Down Expand Up @@ -3034,7 +3019,7 @@ extension BrowserViewController: NewTabPageDelegate {
if inNewTab {
tabManager.addTabAndSelect(isPrivate: isPrivate)
}
processAddressBar(text: input, visitType: .bookmark)
processAddressBar(text: input)
}

func handleFavoriteAction(favorite: Favorite, action: BookmarksAction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension BrowserViewController {
var components = URLComponents()
components.host = currentHost
components.scheme = url.scheme
self.select(url: components.url!, visitType: .link)
self.select(components.url!)
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import BraveCore
import Data

extension BrowserViewController: Web3IPFSScriptHandlerDelegate {
func web3IPFSDecisionHandler(_ proceed: Bool, originalURL: URL, visitType: VisitType) {
func web3IPFSDecisionHandler(_ proceed: Bool, originalURL: URL) {
if proceed {
if let resolvedUrl = braveCore.ipfsAPI.resolveGatewayUrl(for: originalURL) {
finishEditingAndSubmit(resolvedUrl, visitType: visitType)
finishEditingAndSubmit(resolvedUrl)
} else {
finishEditingAndSubmit(originalURL, visitType: visitType)
finishEditingAndSubmit(originalURL)
}
Preferences.Wallet.resolveIPFSResources.value = Preferences.Wallet.Web3IPFSOption.enabled.rawValue
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ extension BrowserViewController {
guard let url = URL(string: "https://talk.brave.com/") else { return }

self.popToBVC()
self.finishEditingAndSubmit(url, visitType: .typed)
self.finishEditingAndSubmit(url)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,6 @@ import Shared
import WebKit
import Storage

// MARK: VisitType

enum VisitType: Int {
case unknown
/// Transition type where user followed a link and got a new top-level window
case link
/// Transition type where user typed the page's URL in the URL bar or selected it from URL bar autocomplete results.
case typed

/// Transition type where user opened a link from bookmarks.
case bookmark
case download
}

// MARK: - ReaderModeDelegate

extension BrowserViewController: ReaderModeScriptHandlerDelegate {
Expand Down Expand Up @@ -211,8 +197,4 @@ extension BrowserViewController {
ReaderModeStyleViewController(selectedStyle: readerModeStyle),
didConfigureStyle: readerModeStyle)
}

func recordNavigationInTab(_ url: URL, visitType: VisitType) {
typedNavigation[url] = visitType
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ extension BrowserViewController: TopToolbarDelegate {
func topToolbarDidPressReload(_ topToolbar: TopToolbarView) {
if let url = topToolbar.currentURL {
if url.isIPFSScheme {
if !handleIPFSSchemeURL(url, visitType: .unknown) {
if !handleIPFSSchemeURL(url) {
tabManager.selectedTab?.reload()
}
} else if let decentralizedDNSHelper = decentralizedDNSHelperFor(url: topToolbar.currentURL) {
Expand All @@ -138,7 +138,7 @@ extension BrowserViewController: TopToolbarDelegate {
showWeb3ServiceInterstitialPage(service: service, originalURL: url)
case let .load(resolvedURL):
if resolvedURL.isIPFSScheme {
handleIPFSSchemeURL(resolvedURL, visitType: .unknown)
handleIPFSSchemeURL(resolvedURL)
} else {
tabManager.selectedTab?.loadRequest(URLRequest(url: resolvedURL))
}
Expand Down Expand Up @@ -279,16 +279,13 @@ extension BrowserViewController: TopToolbarDelegate {
}

func topToolbar(_ topToolbar: TopToolbarView, didSubmitText text: String) {
// TopToolBar Submit Text is Typed URL Visit Type
// This visit type will be used while adding History
// And it will determine either to sync the data or not
processAddressBar(text: text, visitType: .typed)
processAddressBar(text: text)
}

func processAddressBar(text: String, visitType: VisitType, isBraveSearchPromotion: Bool = false) {
func processAddressBar(text: String, isBraveSearchPromotion: Bool = false) {
processAddressBarTask?.cancel()
processAddressBarTask = Task { @MainActor in
if !isBraveSearchPromotion, await submitValidURL(text, visitType: visitType) {
if !isBraveSearchPromotion, await submitValidURL(text) {
return
} else {
// We couldn't build a URL, so pass it on to the search engine.
Expand All @@ -302,7 +299,7 @@ extension BrowserViewController: TopToolbarDelegate {
}

@discardableResult
func handleIPFSSchemeURL(_ url: URL, visitType: VisitType) -> Bool {
func handleIPFSSchemeURL(_ url: URL) -> Bool {
guard !privateBrowsingManager.isPrivateBrowsing else {
topToolbar.leaveOverlayMode()
if let errorPageHelper = tabManager.selectedTab?.getContentScript(name: ErrorPageHelper.scriptName) as? ErrorPageHelper, let webView = tabManager.selectedTab?.webView {
Expand All @@ -317,11 +314,11 @@ extension BrowserViewController: TopToolbarDelegate {

switch ipfsPref {
case .ask:
showIPFSInterstitialPage(originalURL: url, visitType: visitType)
showIPFSInterstitialPage(originalURL: url)
return true
case .enabled:
if let resolvedUrl = braveCore.ipfsAPI.resolveGatewayUrl(for: url) {
finishEditingAndSubmit(resolvedUrl, visitType: visitType)
finishEditingAndSubmit(resolvedUrl)
return true
}
case .disabled:
Expand All @@ -335,9 +332,9 @@ extension BrowserViewController: TopToolbarDelegate {
return false
}

@MainActor func submitValidURL(_ text: String, visitType: VisitType) async -> Bool {
@MainActor func submitValidURL(_ text: String) async -> Bool {
if let url = URL(string: text), url.isIPFSScheme {
return handleIPFSSchemeURL(url, visitType: visitType)
return handleIPFSSchemeURL(url)
} else if let fixupURL = URIFixup.getURL(text) {
// Do not allow users to enter URLs with the following schemes.
// Instead, submit them to the search engine like Chrome-iOS does.
Expand All @@ -352,13 +349,13 @@ extension BrowserViewController: TopToolbarDelegate {
guard !Task.isCancelled else { return true } // user pressed stop, or typed new url
switch result {
case let .loadInterstitial(service):
showWeb3ServiceInterstitialPage(service: service, originalURL: fixupURL, visitType: visitType)
showWeb3ServiceInterstitialPage(service: service, originalURL: fixupURL)
return true
case let .load(resolvedURL):
if resolvedURL.isIPFSScheme {
return handleIPFSSchemeURL(resolvedURL, visitType: visitType)
return handleIPFSSchemeURL(resolvedURL)
} else {
finishEditingAndSubmit(resolvedURL, visitType: visitType)
finishEditingAndSubmit(resolvedURL)
return true
}
case .none:
Expand All @@ -367,7 +364,7 @@ extension BrowserViewController: TopToolbarDelegate {
}

// The user entered a URL, so use it.
finishEditingAndSubmit(fixupURL, visitType: visitType)
finishEditingAndSubmit(fixupURL)
return true
}
}
Expand All @@ -390,7 +387,7 @@ extension BrowserViewController: TopToolbarDelegate {

if let searchURL = engine.searchURLForQuery(text, isBraveSearchPromotion: isBraveSearchPromotion) {
// We couldn't find a matching search keyword, so do a search query.
finishEditingAndSubmit(searchURL, visitType: .typed)
finishEditingAndSubmit(searchURL)
} else {
// We still don't have a valid URL, so something is broken. Give up.
print("Error handling URL entry: \"\(text)\".")
Expand Down Expand Up @@ -723,7 +720,7 @@ extension BrowserViewController: TopToolbarDelegate {

let submitSearch = { [weak self] (text: String) in
if let fixupURL = URIFixup.getURL(text) {
self?.finishEditingAndSubmit(fixupURL, visitType: .unknown)
self?.finishEditingAndSubmit(fixupURL)
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension BrowserViewController: UIDropInteractionDelegate {
return
}

self.finishEditingAndSubmit(url, visitType: .typed)
self.finishEditingAndSubmit(url)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ extension BrowserViewController: WKNavigationDelegate {
// handles IPFS URL schemes
if requestURL.isIPFSScheme {
if navigationAction.targetFrame?.isMainFrame == true {
handleIPFSSchemeURL(requestURL, visitType: .link)
handleIPFSSchemeURL(requestURL)
}
return (.cancel, preferences)
}
Expand All @@ -220,11 +220,11 @@ extension BrowserViewController: WKNavigationDelegate {
}
switch result {
case let .loadInterstitial(service):
showWeb3ServiceInterstitialPage(service: service, originalURL: requestURL, visitType: .link)
showWeb3ServiceInterstitialPage(service: service, originalURL: requestURL)
return (.cancel, preferences)
case let .load(resolvedURL):
if resolvedURL.isIPFSScheme {
handleIPFSSchemeURL(resolvedURL, visitType: .link)
handleIPFSSchemeURL(resolvedURL)
return (.cancel, preferences)
} else { // non-ipfs, treat as normal url / link tapped
requestURL = resolvedURL
Expand Down Expand Up @@ -513,13 +513,7 @@ extension BrowserViewController: WKNavigationDelegate {

tab.mimeType = response.mimeType
}

// Record the navigation visit type for the URL after navigation actions
// this is done in decidePolicyFor to handle all the cases like redirects etc.
if let url = responseURL, let tab = tab, !url.isReaderModeURL, !url.isFileURL, url.isWebPage(), !tab.isPrivate {
recordNavigationInTab(url, visitType: lastEnteredURLVisitType)
}


// If none of our helpers are responsible for handling this response,
// just let the webview handle it as normal.
return .allow
Expand Down
Loading

0 comments on commit b14b83e

Please sign in to comment.