Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #7103: Shields show Session Restore - Reader Mode Internal URL (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
soner-yuksel authored Apr 19, 2023
1 parent 459299a commit 9c95048
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
11 changes: 3 additions & 8 deletions Sources/Brave/Frontend/Shields/ShieldsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ class ShieldsViewController: UIViewController, PopoverContentComponent {
let tab: Tab
private lazy var url: URL? = {
guard let _url = tab.url else { return nil }

if InternalURL.isValid(url: _url),
let internalURL = InternalURL(_url) {
if internalURL.isErrorPage {
return internalURL.originalURLFromErrorPage
} else if internalURL.isWeb3URL {
return internalURL.extractedUrlParam?.displayURL
}

if let tabURL = _url.stippedInternalURL {
return tabURL
}

return _url
Expand Down
57 changes: 57 additions & 0 deletions Sources/BraveShared/Extensions/URLExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import Foundation
import BraveCore
import Shared

extension URL {
/// Obtains a URLOrigin given the current URL
Expand All @@ -22,4 +23,60 @@ extension URL {
public var origin: URLOrigin {
.init(url: self)
}

/// Obtains a clean stripped url from the current Internal URL
///
/// Returns the original url without internal parameters
public var stippedInternalURL: URL? {
if InternalURL.isValid(url: self),
let internalURL = InternalURL(self) {

switch internalURL.urlType {
case .errorPage:
return internalURL.originalURLFromErrorPage
case .web3Page, .sessionRestorePage, .readerModePage, .aboutHomePage:
return internalURL.extractedUrlParam
default:
return nil
}
}

return nil
}
}

extension InternalURL {

enum URLType {
case sessionRestorePage
case errorPage
case readerModePage
case aboutHomePage
case web3Page
case other
}

var urlType: URLType {
if isErrorPage {
return .errorPage
}

if isWeb3URL {
return .web3Page
}

if isReaderModePage {
return .readerModePage
}

if isSessionRestore {
return .sessionRestorePage
}

if isAboutHomeURL {
return .aboutHomePage
}

return .other
}
}
10 changes: 10 additions & 0 deletions Tests/BraveSharedTests/URLExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,15 @@ class URLExtensionTests: XCTestCase {
urls.forEach { XCTAssertEqual(URL(string: $0.0)!.origin.serialized, $0.1) }
badurls.forEach { XCTAssertTrue(URL(string: $0)!.origin.isOpaque) }
}

func testStrippedInternalURL() {
let urls = [
("internal://local/web3/ddns?service_id=ethereum&url=http%3A%2F%2Fvitalik%2Eeth%2F", URL(string: "http://vitalik.eth/")),
("internal://local/sessionrestore?url=https://en.m.wikipedia.org/wiki/Main_Page", URL(string: "https://en.m.wikipedia.org/wiki/Main_Page")),
("internal://local/reader-mode?url=https://en.m.wikipedia.org/wiki/Main_Page", URL(string: "https://en.m.wikipedia.org/wiki/Main_Page"))
]

urls.forEach { XCTAssertEqual(URL(string: $0.0)!.stippedInternalURL, $0.1) }
}

}

0 comments on commit 9c95048

Please sign in to comment.