Skip to content

Commit

Permalink
Merge pull request brave#24386 from brave/ios-xcode16b2-support
Browse files Browse the repository at this point in the history
[iOS] Add support for compiling with Xcode 16 beta 2
  • Loading branch information
kylehickinson authored Jun 26, 2024
2 parents 5b9c1ad + 5e7f4f7 commit 3080a44
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ class TabManagerNavDelegate: NSObject, WKNavigationDelegate {
delegates.insert(delegate)
}

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation) {
for delegate in delegates {
delegate.webView?(webView, didCommit: navigation)
}
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
func webView(_ webView: WKWebView, didFail navigation: WKNavigation, withError error: Error) {
for delegate in delegates {
delegate.webView?(webView, didFail: navigation, withError: error)
}
}

func webView(
_ webView: WKWebView,
didFailProvisionalNavigation navigation: WKNavigation!,
didFailProvisionalNavigation navigation: WKNavigation,
withError error: Error
) {
for delegate in delegates {
delegate.webView?(webView, didFailProvisionalNavigation: navigation, withError: error)
}
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation) {
for delegate in delegates {
delegate.webView?(webView, didFinish: navigation)
}
Expand All @@ -45,23 +45,27 @@ class TabManagerNavDelegate: NSObject, WKNavigationDelegate {
}
}

public func webView(
func webView(
_ webView: WKWebView,
respondTo challenge: URLAuthenticationChallenge
) async -> (URLSession.AuthChallengeDisposition, URLCredential?) {
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
) {
let authenticatingDelegates = delegates.filter { wv in
return wv.responds(
to: #selector(WKNavigationDelegate.webView(_:didReceive:completionHandler:))
)
}

guard let firstAuthenticatingDelegate = authenticatingDelegates.first else {
return (.performDefaultHandling, nil)
completionHandler(.performDefaultHandling, nil)
return
}

// Do NOT change to `delegate.webView?(....)` the optional operator makes async-await calls crash the compiler atm!
// It must be force-unwrapped at the time of writing `January 17th, 2023`.
return await firstAuthenticatingDelegate.webView!(webView, respondTo: challenge)
firstAuthenticatingDelegate.webView?(
webView,
didReceive: challenge,
completionHandler: completionHandler
)
}

func webView(
Expand Down Expand Up @@ -103,95 +107,87 @@ class TabManagerNavDelegate: NSObject, WKNavigationDelegate {
return .allow
}

@MainActor
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
preferences: WKWebpagePreferences
) async -> (WKNavigationActionPolicy, WKWebpagePreferences) {
preferences: WKWebpagePreferences,
decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void
) {
var res = defaultAllowPolicy(for: navigationAction)
var pref = preferences

for delegate in delegates {
// Needed to resolve ambiguous delegate signatures: https://github.com/apple/swift/issues/45652#issuecomment-1149235081
typealias WKNavigationActionSignature = (WKNavigationDelegate) -> (
(
WKWebView, WKNavigationAction, WKWebpagePreferences,
@escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void
) -> Void
)?

// Needed to detect if async implementations exist as we cannot detect them directly
if delegate.responds(
to: #selector(
WKNavigationDelegate.webView(_:decidePolicyFor:preferences:decisionHandler:)
as WKNavigationActionSignature
)
) {
// Do NOT change to `delegate.webView?(....)` the optional operator makes async-await calls crash the compiler atm!
// It must be force-unwrapped at the time of writing `January 10th, 2023`.
let (policy, preferences) = await delegate.webView!(
webView,
decidePolicyFor: navigationAction,
preferences: preferences
)
if policy == .cancel {
res = policy
}

if policy == .download {
res = policy
}
let group = DispatchGroup()

pref = preferences
for delegate in delegates {
if !delegate.responds(to: #selector(webView(_:decidePolicyFor:preferences:decisionHandler:)))
{
continue
}
group.enter()
delegate.webView?(
webView,
decidePolicyFor: navigationAction,
preferences: pref,
decisionHandler: { policy, preferences in
if policy == .cancel {
res = policy
}

if policy == .download {
res = policy
}

pref = preferences

group.leave()
}
)
}

return (res, pref)
group.notify(queue: .main) {
decisionHandler(res, pref)
}
}

@MainActor
func webView(
_ webView: WKWebView,
decidePolicyFor navigationResponse: WKNavigationResponse
) async -> WKNavigationResponsePolicy {
decidePolicyFor navigationResponse: WKNavigationResponse,
decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void
) {
var res = WKNavigationResponsePolicy.allow
let group = DispatchGroup()
for delegate in delegates {
// Needed to resolve ambiguous delegate signatures: https://github.com/apple/swift/issues/45652#issuecomment-1149235081
typealias WKNavigationResponseSignature = (WKNavigationDelegate) -> (
(WKWebView, WKNavigationResponse, @escaping (WKNavigationResponsePolicy) -> Void) -> Void
)?

// Needed to detect if async implementations exist as we cannot detect them directly
if delegate.responds(
to: #selector(
WKNavigationDelegate.webView(_:decidePolicyFor:decisionHandler:)
as WKNavigationResponseSignature
)
) {
// Do NOT change to `delegate.webView?(....)` the optional operator makes async-await calls crash the compiler atm!
// It must be force-unwrapped at the time of writing `January 10th, 2023`.
let policy = await delegate.webView!(webView, decidePolicyFor: navigationResponse)
if policy == .cancel {
res = policy
}

if policy == .download {
res = policy
}
if !delegate.responds(to: #selector(webView(_:decidePolicyFor:decisionHandler:))) {
continue
}
group.enter()
delegate.webView?(
webView,
decidePolicyFor: navigationResponse,
decisionHandler: { policy in
if policy == .cancel {
res = policy
}

if policy == .download {
res = policy
}
group.leave()
}
)
}

if res == .allow {
let tab = tabManager?[webView]
tab?.mimeType = navigationResponse.response.mimeType
}

return res
group.notify(queue: .main) {
decisionHandler(res)
}
}

@MainActor
public func webView(
func webView(
_ webView: WKWebView,
navigationAction: WKNavigationAction,
didBecome download: WKDownload
Expand All @@ -204,8 +200,7 @@ class TabManagerNavDelegate: NSObject, WKNavigationDelegate {
}
}

@MainActor
public func webView(
func webView(
_ webView: WKWebView,
navigationResponse: WKNavigationResponse,
didBecome download: WKDownload
Expand Down
14 changes: 10 additions & 4 deletions ios/brave-ios/Tests/ClientTests/TabManagerNavDelegateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,14 @@ import XCTest

sut.insert(delegate1)
sut.insert(delegate2)
_ = await sut.webView(
let e = expectation(description: "decisionHandler")
sut.webView(
anyWebView(),
decidePolicyFor: WKNavigationAction(),
preferences: WKWebpagePreferences()
preferences: WKWebpagePreferences(),
decisionHandler: { _, _ in e.fulfill() }
)
await fulfillment(of: [e])

XCTAssertEqual(delegate1.receivedMessages, [.webViewDecidePolicyWithActionPolicy])
XCTAssertEqual(delegate2.receivedMessages, [.webViewDecidePolicyWithActionPolicy])
Expand All @@ -138,10 +141,13 @@ import XCTest

sut.insert(delegate1)
sut.insert(delegate2)
_ = await sut.webView(
let e = expectation(description: "decisionHandler")
sut.webView(
anyWebView(),
decidePolicyFor: WKNavigationResponse()
decidePolicyFor: WKNavigationResponse(),
decisionHandler: { _ in e.fulfill() }
)
await fulfillment(of: [e])

XCTAssertEqual(delegate1.receivedMessages, [.webViewDecidePolicyWithResponsePolicy])
XCTAssertEqual(delegate2.receivedMessages, [.webViewDecidePolicyWithResponsePolicy])
Expand Down

0 comments on commit 3080a44

Please sign in to comment.