Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[webview_flutter_wkwebview] Fixes crash with nil WKFrameInfo.request #8766

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.18.4

* Fixes crash when native `WKFrameInfo.request` is nil.

## 3.18.3

* Fixes crash where the native `AuthenticationChallengeResponse` could not be found for auth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class FrameInfoProxyAPITests: XCTestCase {

XCTAssertEqual(value?.value, instance!.request)
}

@MainActor func testNilRequest() {
let registrar = TestProxyApiRegistrar()
let api = registrar.apiDelegate.pigeonApiWKFrameInfo(registrar)

let instance = TestFrameInfoWithNilRequest()
let value = try? api.pigeonDelegate.request(pigeonApi: api, pigeonInstance: instance)

XCTAssertNil(value)
}
}

class TestFrameInfo: WKFrameInfo {
Expand All @@ -39,3 +49,6 @@ class TestFrameInfo: WKFrameInfo {
return URLRequest(url: URL(string: "https://google.com")!)
}
}

class TestFrameInfoWithNilRequest: WKFrameInfo {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

import WebKit

extension WKFrameInfo {
// It's possible that `WKFrameInfo.request` can be a nil value despite the Swift code considering
// it to be nonnull. This causes a crash when accessing the value with Swift. Accessing the value
// this way prevents the crash when the value is nil.
//
// See https://github.com/flutter/flutter/issues/163549 and https://developer.apple.com/forums/thread/77888.
var maybeRequest: URLRequest? {
return self.perform(#selector(getter:WKFrameInfo.request))?.takeUnretainedValue()
as! URLRequest?
}
}

/// ProxyApi implementation for `WKFrameInfo`.
///
/// This class may handle instantiating native object instances that are attached to a Dart instance
Expand All @@ -14,8 +26,12 @@ class FrameInfoProxyAPIDelegate: PigeonApiDelegateWKFrameInfo {
}

func request(pigeonApi: PigeonApiWKFrameInfo, pigeonInstance: WKFrameInfo) throws
-> URLRequestWrapper
-> URLRequestWrapper?
{
return URLRequestWrapper(pigeonInstance.request)
let request = pigeonInstance.maybeRequest
if let request = request {
return URLRequestWrapper(request)
}
return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2125,7 +2125,7 @@ protocol PigeonApiDelegateWKFrameInfo {
func isMainFrame(pigeonApi: PigeonApiWKFrameInfo, pigeonInstance: WKFrameInfo) throws -> Bool
/// The frame’s current request.
func request(pigeonApi: PigeonApiWKFrameInfo, pigeonInstance: WKFrameInfo) throws
-> URLRequestWrapper
-> URLRequestWrapper?
}

protocol PigeonApiProtocolWKFrameInfo {
Expand Down Expand Up @@ -3993,8 +3993,15 @@ protocol PigeonApiProtocolWKNavigationDelegate {
completion: @escaping (Result<Void, PigeonError>) -> Void)
/// Asks the delegate to respond to an authentication challenge.
///
/// Returns a List with a `UrlSessionAuthChallengeDisposition` and a nullable
/// `URLCredential`.
/// This return value expects a List with:
///
/// 1. `UrlSessionAuthChallengeDisposition`
/// 2. A nullable map to instantiate a `URLCredential`. The map structure is
/// [
/// "user": "<nonnull String username>",
/// "password": "<nonnull String user password>",
/// "persistence": <nonnull enum value of `UrlCredentialPersistence`>,
/// ]
func didReceiveAuthenticationChallenge(
pigeonInstance pigeonInstanceArg: WKNavigationDelegate, webView webViewArg: WKWebView,
challenge challengeArg: URLAuthenticationChallenge,
Expand Down Expand Up @@ -4337,8 +4344,15 @@ final class PigeonApiWKNavigationDelegate: PigeonApiProtocolWKNavigationDelegate

/// Asks the delegate to respond to an authentication challenge.
///
/// Returns a List with a `UrlSessionAuthChallengeDisposition` and a nullable
/// `URLCredential`.
/// This return value expects a List with:
///
/// 1. `UrlSessionAuthChallengeDisposition`
/// 2. A nullable map to instantiate a `URLCredential`. The map structure is
/// [
/// "user": "<nonnull String username>",
/// "password": "<nonnull String user password>",
/// "persistence": <nonnull enum value of `UrlCredentialPersistence`>,
/// ]
func didReceiveAuthenticationChallenge(
pigeonInstance pigeonInstanceArg: WKNavigationDelegate, webView webViewArg: WKWebView,
challenge challengeArg: URLAuthenticationChallenge,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1756,7 +1756,7 @@ class WKFrameInfo extends NSObject {
super.pigeon_binaryMessenger,
super.pigeon_instanceManager,
required this.isMainFrame,
required this.request,
this.request,
super.observeValue,
}) : super.pigeon_detached();

Expand All @@ -1765,15 +1765,15 @@ class WKFrameInfo extends NSObject {
final bool isMainFrame;

/// The frame’s current request.
final URLRequest request;
final URLRequest? request;

static void pigeon_setUpMessageHandlers({
bool pigeon_clearHandlers = false,
BinaryMessenger? pigeon_binaryMessenger,
PigeonInstanceManager? pigeon_instanceManager,
WKFrameInfo Function(
bool isMainFrame,
URLRequest request,
URLRequest? request,
)? pigeon_newInstance,
}) {
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
Expand Down Expand Up @@ -1801,17 +1801,15 @@ class WKFrameInfo extends NSObject {
assert(arg_isMainFrame != null,
'Argument for dev.flutter.pigeon.webview_flutter_wkwebview.WKFrameInfo.pigeon_newInstance was null, expected non-null bool.');
final URLRequest? arg_request = (args[2] as URLRequest?);
assert(arg_request != null,
'Argument for dev.flutter.pigeon.webview_flutter_wkwebview.WKFrameInfo.pigeon_newInstance was null, expected non-null URLRequest.');
try {
(pigeon_instanceManager ?? PigeonInstanceManager.instance)
.addHostCreatedInstance(
pigeon_newInstance?.call(arg_isMainFrame!, arg_request!) ??
pigeon_newInstance?.call(arg_isMainFrame!, arg_request) ??
WKFrameInfo.pigeon_detached(
pigeon_binaryMessenger: pigeon_binaryMessenger,
pigeon_instanceManager: pigeon_instanceManager,
isMainFrame: arg_isMainFrame!,
request: arg_request!,
request: arg_request,
),
arg_pigeon_instanceIdentifier!,
);
Expand Down Expand Up @@ -4326,8 +4324,15 @@ class WKNavigationDelegate extends NSObject {

/// Asks the delegate to respond to an authentication challenge.
///
/// Returns a List with a `UrlSessionAuthChallengeDisposition` and a nullable
/// `URLCredential`.
/// This return value expects a List with:
///
/// 1. `UrlSessionAuthChallengeDisposition`
/// 2. A nullable map to instantiate a `URLCredential`. The map structure is
/// [
/// "user": "<nonnull String username>",
/// "password": "<nonnull String user password>",
/// "persistence": <nonnull enum value of `UrlCredentialPersistence`>,
/// ]
///
/// For the associated Native object to be automatically garbage collected,
/// it is required that the implementation of this `Function` doesn't have a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class WebKitWebViewController extends PlatformWebViewController {
final JavaScriptAlertDialogRequest request =
JavaScriptAlertDialogRequest(
message: message,
url: await frame.request.getUrl() ?? '',
url: await frame.request?.getUrl() ?? '',
);
await callback.call(request);
return;
Expand All @@ -253,7 +253,7 @@ class WebKitWebViewController extends PlatformWebViewController {
final JavaScriptConfirmDialogRequest request =
JavaScriptConfirmDialogRequest(
message: message,
url: await frame.request.getUrl() ?? '',
url: await frame.request?.getUrl() ?? '',
);
final bool result = await callback.call(request);
return result;
Expand All @@ -274,7 +274,7 @@ class WebKitWebViewController extends PlatformWebViewController {
final JavaScriptTextInputDialogRequest request =
JavaScriptTextInputDialogRequest(
message: prompt,
url: await frame.request.getUrl() ?? '',
url: await frame.request?.getUrl() ?? '',
defaultText: defaultText);
final String result = await callback.call(request);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ abstract class WKFrameInfo extends NSObject {
late bool isMainFrame;

/// The frame’s current request.
late URLRequest request;
late URLRequest? request;
}

/// Information about an error condition including a domain, a domain-specific
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter_wkwebview
description: A Flutter plugin that provides a WebView widget based on Apple's WKWebView control.
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_wkwebview
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 3.18.3
version: 3.18.4

environment:
sdk: ^3.5.0
Expand Down