Skip to content

Commit

Permalink
Fixing web view delegate proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoEmbrace committed Oct 30, 2024
1 parent 5e157ee commit dd4e5b2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 113 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Improved performance during the startup of the SDK.
* Fixes
* Fixed compilation errors in WatchOS.
* Fixed visbility of `LogLevel`.
* Fixed visibility of `LogLevel`.

## 6.5.0
*Oct 18th, 2024*
Expand Down
118 changes: 18 additions & 100 deletions Sources/EmbraceCore/Capture/WebView/WKNavigationDelegateProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ class WKNavigationDelegateProxy: NSObject {

// callback triggered the webview loads an url or errors
var callback: ((URL?, Int?) -> Void)?

override func responds(to aSelector: Selector!) -> Bool {
if super.responds(to: aSelector) {
return true
} else if let originalDelegate = originalDelegate, originalDelegate.responds(to: aSelector) {
return true
}
return false
}

override func forwardingTarget(for aSelector: Selector!) -> Any? {
if super.responds(to: aSelector) {
return self
} else if let originalDelegate = originalDelegate, originalDelegate.responds(to: aSelector) {
return originalDelegate
}
return nil
}
}

extension WKNavigationDelegateProxy: WKNavigationDelegate {
Expand Down Expand Up @@ -55,105 +73,5 @@ extension WKNavigationDelegateProxy: WKNavigationDelegate {
// call original
originalDelegate?.webView?(webView, didFail: navigation, withError: error)
}

// forwarded methods without capture
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void
) {
originalDelegate?.webView?(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler)
?? decisionHandler(.allow)
}

func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
preferences: WKWebpagePreferences,
decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void
) {
originalDelegate?.webView?(
webView,
decidePolicyFor: navigationAction,
preferences: preferences,
decisionHandler: decisionHandler
)
?? decisionHandler(.allow, preferences)
}

func webView(
_ webView: WKWebView,
didStartProvisionalNavigation navigation: WKNavigation!
) {
originalDelegate?.webView?(webView, didStartProvisionalNavigation: navigation)
}

func webView(
_ webView: WKWebView,
didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!
) {
originalDelegate?.webView?(webView, didReceiveServerRedirectForProvisionalNavigation: navigation)
}

func webView(
_ webView: WKWebView,
didCommit navigation: WKNavigation!
) {
originalDelegate?.webView?(webView, didCommit: navigation)
}

func webView(
_ webView: WKWebView,
didFinish navigation: WKNavigation!
) {
originalDelegate?.webView?(webView, didFinish: navigation)
}

func webView(
_ webView: WKWebView,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
) {
originalDelegate?.webView?(webView, didReceive: challenge, completionHandler: completionHandler)
?? completionHandler(.performDefaultHandling, nil)
}

func webViewWebContentProcessDidTerminate(
_ webView: WKWebView
) {
originalDelegate?.webViewWebContentProcessDidTerminate?(webView)
}

@available(iOS 14, *)
func webView(
_ webView: WKWebView,
authenticationChallenge challenge: URLAuthenticationChallenge,
shouldAllowDeprecatedTLS decisionHandler: @escaping (Bool) -> Void
) {
originalDelegate?.webView?(
webView,
authenticationChallenge: challenge,
shouldAllowDeprecatedTLS: decisionHandler
)
?? decisionHandler(true)
}

@available(iOS 14.5, *)
func webView(
_ webView: WKWebView,
navigationAction: WKNavigationAction,
didBecome download: WKDownload
) {
originalDelegate?.webView?(webView, navigationAction: navigationAction, didBecome: download)
}

@available(iOS 14.5, *)
func webView(
_ webView: WKWebView,
navigationResponse: WKNavigationResponse,
didBecome download: WKDownload
) {
originalDelegate?.webView?(webView, navigationResponse: navigationResponse, didBecome: download)
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import EmbraceCommonInternal

extension LogType {
public static let networkCapture = LogType(system: "network_capture")

}

public extension LogSemantics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,25 @@ class WKNavigationDelegateProxyTests: XCTestCase {
proxy.webView(webView, decidePolicyFor: WKNavigationResponse(), decisionHandler: block)
proxy.webView(webView, didFailProvisionalNavigation: navigation, withError: error)
proxy.webView(webView, didFail: navigation, withError: error)
proxy.webView(webView, decidePolicyFor: WKNavigationAction(), decisionHandler: block1)
proxy.webView(

let delegate = proxy as WKNavigationDelegate
delegate.webView?(webView, decidePolicyFor: WKNavigationAction(), decisionHandler: block1)

delegate.webView?(
webView,
decidePolicyFor: WKNavigationAction(),
preferences: WKWebpagePreferences(),
decisionHandler: block2
)
proxy.webView(webView, didStartProvisionalNavigation: navigation)
proxy.webView(webView, didReceiveServerRedirectForProvisionalNavigation: navigation)
proxy.webView(webView, didCommit: navigation)
proxy.webView(webView, didFinish: navigation)
proxy.webView(webView, didReceive: URLAuthenticationChallenge(), completionHandler: block3)
proxy.webViewWebContentProcessDidTerminate(webView)
proxy.webView(webView, authenticationChallenge: URLAuthenticationChallenge(), shouldAllowDeprecatedTLS: block4)
proxy.webView(webView, navigationAction: WKNavigationAction(), didBecome: download)
proxy.webView(webView, navigationResponse: WKNavigationResponse(), didBecome: download)
delegate.webView?(webView, didStartProvisionalNavigation: navigation)
delegate.webView?(webView, didReceiveServerRedirectForProvisionalNavigation: navigation)
delegate.webView?(webView, didCommit: navigation)
delegate.webView?(webView, didFinish: navigation)
delegate.webView?(webView, didReceive: URLAuthenticationChallenge(), completionHandler: block3)
delegate.webViewWebContentProcessDidTerminate?(webView)
delegate.webView?(webView, authenticationChallenge: URLAuthenticationChallenge(), shouldAllowDeprecatedTLS: block4)
delegate.webView?(webView, navigationAction: WKNavigationAction(), didBecome: download)
delegate.webView?(webView, navigationResponse: WKNavigationResponse(), didBecome: download)

// then the delegate calls are forwarded
XCTAssertEqual(originalDelegate.callCount, 14)
Expand Down

0 comments on commit dd4e5b2

Please sign in to comment.