Skip to content

[webview_flutter] Adds support for receiving a url with WebResourceError #3884

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

Merged
merged 27 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d1d326d
finish dart side
bparrishMines May 2, 2023
aaf059b
objective c implementation
bparrishMines May 2, 2023
987452b
Merge branch 'main' of github.com:flutter/packages into wk_fulldesc
bparrishMines May 2, 2023
af72995
fix dev uri
bparrishMines May 2, 2023
9cd84c5
Merge branch 'main' of github.com:flutter/packages into wk_fulldesc
bparrishMines May 3, 2023
9d38f91
platform impls and the dart side of ios
bparrishMines May 3, 2023
3b7fbc4
dart side again of ios
bparrishMines May 3, 2023
f4f8fcd
objc side of impl
bparrishMines May 3, 2023
03628af
fixes and version bumps
bparrishMines May 3, 2023
84113fc
tests for constants and fix constants
bparrishMines May 3, 2023
59a7b5b
fix changelogs and update integration tests
bparrishMines May 3, 2023
041802d
unneccessary cast
bparrishMines May 3, 2023
7b4878e
Merge branch 'main' of github.com:flutter/packages into wk_fulldesc
bparrishMines May 18, 2023
dec7cb6
Merge branch 'main' of github.com:flutter/packages into wk_fulldesc
bparrishMines May 31, 2023
b2a5cec
Merge branch 'main' of github.com:flutter/packages into wk_fulldesc
bparrishMines Jun 14, 2023
fa36fd6
change filed to url
bparrishMines Jun 14, 2023
00af29a
fix wkwebview
bparrishMines Jun 14, 2023
6f0270f
dont add the deprecated flag
bparrishMines Jun 15, 2023
70fd3a1
Merge branch 'main' of github.com:flutter/packages into wk_fulldesc
bparrishMines Jun 15, 2023
c26cc3e
version bump android too
bparrishMines Jun 15, 2023
19c5ae8
review comments
bparrishMines Jun 29, 2023
b78d7fc
Merge branch 'main' of github.com:flutter/packages into wk_fulldesc
bparrishMines Jul 11, 2023
a8f1f8d
fix field name
bparrishMines Jul 11, 2023
bcf94e9
Merge branch 'main' of github.com:flutter/packages into wk_fulldesc
bparrishMines Jul 13, 2023
8026435
update print info
bparrishMines Jul 13, 2023
29e0822
fix pubspecs
bparrishMines Jul 13, 2023
8d9bfc0
use url in example
bparrishMines Jul 13, 2023
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.9.0

* Adds support for `WebResouceError.url`.

## 3.8.2

* Fixes unawaited_futures violations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,9 +826,7 @@ Future<void> main() async {

expect(error.errorType, isNotNull);
expect(
(error as AndroidWebResourceError)
.failingUrl
?.startsWith('https://www.notawebsite..com'),
error.url?.startsWith('https://www.notawebsite..com'),
isTrue,
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Page resource error:
description: ${error.description}
errorType: ${error.errorType}
isForMainFrame: ${error.isForMainFrame}
url: ${error.url}
''');
})
..setOnNavigationRequest((NavigationRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies:
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../
webview_flutter_platform_interface: ^2.3.0
webview_flutter_platform_interface: ^2.4.0

dev_dependencies:
espresso: ^0.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -825,12 +825,14 @@ class AndroidWebResourceError extends WebResourceError {
required super.errorCode,
required super.description,
super.isForMainFrame,
this.failingUrl,
}) : super(
super.url,
}) : failingUrl = url,
super(
errorType: _errorCodeToErrorType(errorCode),
);

/// Gets the URL for which the failing resource request was made.
@Deprecated('Please use `url`.')
final String? failingUrl;

static WebResourceErrorType? _errorCodeToErrorType(int errorCode) {
Expand Down Expand Up @@ -954,7 +956,7 @@ class AndroidNavigationDelegate extends PlatformNavigationDelegate {
callback(AndroidWebResourceError._(
errorCode: error.errorCode,
description: error.description,
failingUrl: request.url,
url: request.url,
isForMainFrame: request.isForMainFrame,
));
}
Expand All @@ -971,7 +973,7 @@ class AndroidNavigationDelegate extends PlatformNavigationDelegate {
callback(AndroidWebResourceError._(
errorCode: errorCode,
description: description,
failingUrl: failingUrl,
url: failingUrl,
isForMainFrame: true,
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter_android
description: A Flutter plugin that provides a WebView widget on Android.
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 3.8.2
version: 3.9.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand All @@ -20,7 +20,7 @@ flutter:
dependencies:
flutter:
sdk: flutter
webview_flutter_platform_interface: ^2.3.0
webview_flutter_platform_interface: ^2.4.0

dev_dependencies:
build_runner: ^2.1.4
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.7.0

* Adds support for `WebResouceError.url`.

## 3.6.3

* Introduces `NSError.toString` for better diagnostics.
Expand All @@ -16,7 +20,7 @@

* Adds support to enable debugging of web contents on the latest versions of WebKit. See
`WebKitWebViewController.setInspectable`.

## 3.5.0

* Adds support to limit navigation to pages within the app’s domain. See
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,10 @@ Future<void> main() async {

final WebResourceError error = await errorCompleter.future;
expect(error, isNotNull);
expect(
error.url?.startsWith('https://www.notawebsite..com'),
isTrue,
);

expect((error as WebKitWebResourceError).domain, isNotNull);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
8F4FF949299ADC2D000A6586 /* FWFWebViewFlutterWKWebViewExternalAPITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F4FF948299ADC2D000A6586 /* FWFWebViewFlutterWKWebViewExternalAPITests.m */; };
8F4FF94B29AC223F000A6586 /* FWFURLTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F4FF94A29AC223F000A6586 /* FWFURLTests.m */; };
8F78EAAA2A02CB9100C2E520 /* FWFErrorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F78EAA92A02CB9100C2E520 /* FWFErrorTests.m */; };
8FA6A87928062CD000A4B183 /* FWFInstanceManagerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8FA6A87828062CD000A4B183 /* FWFInstanceManagerTests.m */; };
8FB79B5328134C3100C101D3 /* FWFWebViewHostApiTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8FB79B5228134C3100C101D3 /* FWFWebViewHostApiTests.m */; };
8FB79B55281B24F600C101D3 /* FWFDataConvertersTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8FB79B54281B24F600C101D3 /* FWFDataConvertersTests.m */; };
Expand Down Expand Up @@ -80,6 +81,7 @@
7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
8F4FF948299ADC2D000A6586 /* FWFWebViewFlutterWKWebViewExternalAPITests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FWFWebViewFlutterWKWebViewExternalAPITests.m; sourceTree = "<group>"; };
8F4FF94A29AC223F000A6586 /* FWFURLTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FWFURLTests.m; sourceTree = "<group>"; };
8F78EAA92A02CB9100C2E520 /* FWFErrorTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FWFErrorTests.m; sourceTree = "<group>"; };
8FA6A87828062CD000A4B183 /* FWFInstanceManagerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FWFInstanceManagerTests.m; sourceTree = "<group>"; };
8FB79B5228134C3100C101D3 /* FWFWebViewHostApiTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FWFWebViewHostApiTests.m; sourceTree = "<group>"; };
8FB79B54281B24F600C101D3 /* FWFDataConvertersTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FWFDataConvertersTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -165,6 +167,7 @@
8FB79B902820BAC700C101D3 /* FWFUIViewHostApiTests.m */,
8FB79B962821985200C101D3 /* FWFObjectHostApiTests.m */,
8F4FF94A29AC223F000A6586 /* FWFURLTests.m */,
8F78EAA92A02CB9100C2E520 /* FWFErrorTests.m */,
);
path = RunnerTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -466,6 +469,7 @@
buildActionMask = 2147483647;
files = (
8FA6A87928062CD000A4B183 /* FWFInstanceManagerTests.m in Sources */,
8F78EAAA2A02CB9100C2E520 /* FWFErrorTests.m in Sources */,
8F4FF94B29AC223F000A6586 /* FWFURLTests.m in Sources */,
8FB79B852820A3A400C101D3 /* FWFUIDelegateHostApiTests.m in Sources */,
8FB79B972821985200C101D3 /* FWFObjectHostApiTests.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,20 @@ - (void)testFWFWKFrameInfoDataFromWKFrameInfo {
}

- (void)testFWFNSErrorDataFromNSError {
NSObject *unsupportedType = [[NSObject alloc] init];
NSError *error = [NSError errorWithDomain:@"domain"
code:23
userInfo:@{NSLocalizedDescriptionKey : @"description"}];
userInfo:@{@"a" : @"b", @"c" : unsupportedType}];

FWFNSErrorData *data = FWFNSErrorDataFromNativeNSError(error);
XCTAssertEqualObjects(data.code, @23);
XCTAssertEqualObjects(data.domain, @"domain");
XCTAssertEqualObjects(data.localizedDescription, @"description");

NSDictionary *userInfo = @{
@"a" : @"b",
@"c" : [NSString stringWithFormat:@"Unsupported Type: %@", unsupportedType.description]
};
XCTAssertEqualObjects(data.userInfo, userInfo);
}

- (void)testFWFWKScriptMessageDataFromWKScriptMessage {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

@import XCTest;

#import <XCTest/XCTest.h>

@interface FWFErrorTests : XCTestCase
@end

@implementation FWFErrorTests
- (void)testNSErrorUserInfoKey {
// These MUST match the String values in the Dart class NSErrorUserInfoKey.
XCTAssertEqualObjects(NSLocalizedDescriptionKey, @"NSLocalizedDescription");
XCTAssertEqualObjects(NSURLErrorFailingURLStringErrorKey, @"NSErrorFailingURLStringKey");
}
@end
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ - (void)testEvaluateJavaScriptReturnsNSErrorData {
XCTAssertTrue([errorData isKindOfClass:[FWFNSErrorData class]]);
XCTAssertEqualObjects(errorData.code, @0);
XCTAssertEqualObjects(errorData.domain, @"errorDomain");
XCTAssertEqualObjects(errorData.localizedDescription, @"description");
XCTAssertEqualObjects(errorData.userInfo, @{NSLocalizedDescriptionKey : @"description"});
}

- (void)testWebViewContentInsetBehaviorShouldBeNever {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Page resource error:
description: ${error.description}
errorType: ${error.errorType}
isForMainFrame: ${error.isForMainFrame}
url: ${error.url}
''');
})
..setOnNavigationRequest((NavigationRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
flutter:
sdk: flutter
path_provider: ^2.0.6
webview_flutter_platform_interface: ^2.3.0
webview_flutter_platform_interface: ^2.4.0
webview_flutter_wkwebview:
# When depending on this package from a real application you should use:
# webview_flutter: ^x.y.z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,19 @@ WKNavigationActionPolicy FWFNativeWKNavigationActionPolicyFromEnumData(
}

FWFNSErrorData *FWFNSErrorDataFromNativeNSError(NSError *error) {
return [FWFNSErrorData makeWithCode:@(error.code)
domain:error.domain
localizedDescription:error.localizedDescription];
NSMutableDictionary *userInfo;
if (error.userInfo) {
userInfo = [NSMutableDictionary dictionary];
for (NSErrorUserInfoKey key in error.userInfo.allKeys) {
NSObject *value = error.userInfo[key];
if ([value isKindOfClass:[NSString class]]) {
userInfo[key] = value;
} else {
userInfo[key] = [NSString stringWithFormat:@"Unsupported Type: %@", value.description];
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value can be any object from any library, so this only checks for Strings. This could also check for the other pigeon supported values too, e.g. NSNumbers too if needed. We currently only use the failingURL and localizedDescription.

}
return [FWFNSErrorData makeWithCode:@(error.code) domain:error.domain userInfo:userInfo];
}

FWFNSKeyValueChangeKeyEnumData *FWFNSKeyValueChangeKeyEnumDataFromNativeNSKeyValueChangeKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,10 @@ typedef NS_ENUM(NSUInteger, FWFWKMediaCaptureType) {
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)makeWithCode:(NSNumber *)code
domain:(NSString *)domain
localizedDescription:(NSString *)localizedDescription;
userInfo:(nullable NSDictionary<NSString *, id> *)userInfo;
@property(nonatomic, strong) NSNumber *code;
@property(nonatomic, copy) NSString *domain;
@property(nonatomic, copy) NSString *localizedDescription;
@property(nonatomic, strong, nullable) NSDictionary<NSString *, id> *userInfo;
@end

/// Mirror of WKScriptMessage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,11 @@ - (NSArray *)toList {
@implementation FWFNSErrorData
+ (instancetype)makeWithCode:(NSNumber *)code
domain:(NSString *)domain
localizedDescription:(NSString *)localizedDescription {
userInfo:(nullable NSDictionary<NSString *, id> *)userInfo {
FWFNSErrorData *pigeonResult = [[FWFNSErrorData alloc] init];
pigeonResult.code = code;
pigeonResult.domain = domain;
pigeonResult.localizedDescription = localizedDescription;
pigeonResult.userInfo = userInfo;
return pigeonResult;
}
+ (FWFNSErrorData *)fromList:(NSArray *)list {
Expand All @@ -468,8 +468,7 @@ + (FWFNSErrorData *)fromList:(NSArray *)list {
NSAssert(pigeonResult.code != nil, @"");
pigeonResult.domain = GetNullableObjectAtIndex(list, 1);
NSAssert(pigeonResult.domain != nil, @"");
pigeonResult.localizedDescription = GetNullableObjectAtIndex(list, 2);
NSAssert(pigeonResult.localizedDescription != nil, @"");
pigeonResult.userInfo = GetNullableObjectAtIndex(list, 2);
return pigeonResult;
}
+ (nullable FWFNSErrorData *)nullableFromList:(NSArray *)list {
Expand All @@ -479,7 +478,7 @@ - (NSArray *)toList {
return @[
(self.code ?: [NSNull null]),
(self.domain ?: [NSNull null]),
(self.localizedDescription ?: [NSNull null]),
(self.userInfo ?: [NSNull null]),
];
}
@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,20 +522,20 @@ class NSErrorData {
NSErrorData({
required this.code,
required this.domain,
required this.localizedDescription,
this.userInfo,
});

int code;

String domain;

String localizedDescription;
Map<String?, Object?>? userInfo;

Object encode() {
return <Object?>[
code,
domain,
localizedDescription,
userInfo,
];
}

Expand All @@ -544,7 +544,7 @@ class NSErrorData {
return NSErrorData(
code: result[0]! as int,
domain: result[1]! as String,
localizedDescription: result[2]! as String,
userInfo: (result[2] as Map<Object?, Object?>?)?.cast<String?, Object?>(),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,23 @@ class NSUrlRequest {
final Map<String, String> allHttpHeaderFields;
}

/// Keys that may exist in the user info map of `NSError`.
class NSErrorUserInfoKey {
NSErrorUserInfoKey._();

/// The corresponding value is a localized string representation of the error
/// that, if present, will be returned by [NSError.localizedDescription].
///
/// See https://developer.apple.com/documentation/foundation/nslocalizeddescriptionkey.
static const String NSLocalizedDescription = 'NSLocalizedDescription';

/// The URL which caused a load to fail.
///
/// See https://developer.apple.com/documentation/foundation/nsurlerrorfailingurlstringerrorkey?language=objc.
static const String NSURLErrorFailingURLStringError =
'NSErrorFailingURLStringKey';
}

/// Information about an error condition.
///
/// Wraps [NSError](https://developer.apple.com/documentation/foundation/nserror?language=objc).
Expand All @@ -210,7 +227,7 @@ class NSError {
const NSError({
required this.code,
required this.domain,
required this.localizedDescription,
this.userInfo = const <String, Object?>{},
});

/// The error code.
Expand All @@ -221,15 +238,23 @@ class NSError {
/// A string containing the error domain.
final String domain;

/// Map of arbitrary data.
///
/// See [NSErrorUserInfoKey] for possible keys (non-exhaustive).
///
/// This currently only supports values that are a String.
final Map<String, Object?> userInfo;

/// A string containing the localized description of the error.
final String localizedDescription;
String? get localizedDescription =>
userInfo[NSErrorUserInfoKey.NSLocalizedDescription] as String?;

@override
String toString() {
if (localizedDescription.isEmpty) {
return 'Error $domain:$code';
if (localizedDescription?.isEmpty ?? true) {
return 'Error $domain:$code:$userInfo';
}
return '$localizedDescription ($domain:$code)';
return '$localizedDescription ($domain:$code:$userInfo)';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ class WebKitWebViewPlatformController extends WebViewPlatformController {
return WebResourceError(
errorCode: error.code,
domain: error.domain,
description: error.localizedDescription,
description: error.localizedDescription ?? '',
errorType: errorType,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ extension _WKNSErrorDataConverter on NSErrorData {
return NSError(
domain: domain,
code: code,
localizedDescription: localizedDescription,
userInfo: userInfo?.cast<String, Object?>() ?? <String, Object?>{},
);
}
}
Expand Down
Loading