Skip to content

[webview_flutter] Add support for limitsNavigationsToAppBoundDomains #4026

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 19 commits into from
Jun 15, 2023
Merged
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,8 @@
## 3.5.0

* Adds support to limit navigation to pages within the app’s domain. See
`WebKitWebViewControllerCreationParams.limitsNavigationsToAppBoundDomains`.

## 3.4.4

* Removes obsolete null checks on non-nullable values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ - (void)testSetAllowsInlineMediaPlayback {
XCTAssertNil(error);
}

- (void)testSetLimitsNavigationsToAppBoundDomains API_AVAILABLE(ios(14.0)) {
WKWebViewConfiguration *mockWebViewConfiguration = OCMClassMock([WKWebViewConfiguration class]);

FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebViewConfiguration withIdentifier:0];

FWFWebViewConfigurationHostApiImpl *hostAPI = [[FWFWebViewConfigurationHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI setLimitsNavigationsToAppBoundDomainsForConfigurationWithIdentifier:@0
isLimited:@NO
error:&error];
OCMVerify([mockWebViewConfiguration setLimitsNavigationsToAppBoundDomains:NO]);
XCTAssertNil(error);
}

- (void)testSetMediaTypesRequiringUserActionForPlayback {
WKWebViewConfiguration *mockWebViewConfiguration = OCMClassMock([WKWebViewConfiguration class]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 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.
// Autogenerated from Pigeon (v9.2.4), do not edit directly.
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
// See also: https://pub.dev/packages/pigeon

#import <Foundation/Foundation.h>
Expand Down Expand Up @@ -480,6 +480,10 @@ NSObject<FlutterMessageCodec> *FWFWKWebViewConfigurationHostApiGetCodec(void);
error:
(FlutterError *_Nullable *_Nonnull)
error;
- (void)setLimitsNavigationsToAppBoundDomainsForConfigurationWithIdentifier:(NSNumber *)identifier
isLimited:(NSNumber *)limit
error:(FlutterError *_Nullable
*_Nonnull)error;
- (void)
setMediaTypesRequiresUserActionForConfigurationWithIdentifier:(NSNumber *)identifier
forTypes:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 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.
// Autogenerated from Pigeon (v9.2.4), do not edit directly.
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
// See also: https://pub.dev/packages/pigeon

#import "FWFGeneratedWebKitApis.h"
Expand Down Expand Up @@ -1002,6 +1002,34 @@ void FWFWKWebViewConfigurationHostApiSetup(id<FlutterBinaryMessenger> binaryMess
[channel setMessageHandler:nil];
}
}
{
FlutterBasicMessageChannel *channel = [[FlutterBasicMessageChannel alloc]
initWithName:@"dev.flutter.pigeon.WKWebViewConfigurationHostApi."
@"setLimitsNavigationsToAppBoundDomains"
binaryMessenger:binaryMessenger
codec:FWFWKWebViewConfigurationHostApiGetCodec()];
if (api) {
NSCAssert([api respondsToSelector:@selector
(setLimitsNavigationsToAppBoundDomainsForConfigurationWithIdentifier:
isLimited:error:)],
@"FWFWKWebViewConfigurationHostApi api (%@) doesn't respond to "
@"@selector(setLimitsNavigationsToAppBoundDomainsForConfigurationWithIdentifier:"
@"isLimited:error:)",
api);
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
NSArray *args = message;
NSNumber *arg_identifier = GetNullableObjectAtIndex(args, 0);
NSNumber *arg_limit = GetNullableObjectAtIndex(args, 1);
FlutterError *error;
[api setLimitsNavigationsToAppBoundDomainsForConfigurationWithIdentifier:arg_identifier
isLimited:arg_limit
error:&error];
callback(wrapResult(nil, error));
}];
} else {
[channel setMessageHandler:nil];
}
}
{
FlutterBasicMessageChannel *channel = [[FlutterBasicMessageChannel alloc]
initWithName:@"dev.flutter.pigeon.WKWebViewConfigurationHostApi."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ - (void)setAllowsInlineMediaPlaybackForConfigurationWithIdentifier:(nonnull NSNu
setAllowsInlineMediaPlayback:allow.boolValue];
}

- (void)setLimitsNavigationsToAppBoundDomainsForConfigurationWithIdentifier:
(nonnull NSNumber *)identifier
isLimited:
(nonnull NSNumber *)limit
error:(FlutterError *_Nullable
*_Nonnull)error {
if (@available(iOS 14, *)) {
[[self webViewConfigurationForIdentifier:identifier]
setLimitsNavigationsToAppBoundDomains:limit.boolValue];
} else {
*error = [FlutterError
errorWithCode:@"FWFUnsupportedVersionError"
message:@"setLimitsNavigationsToAppBoundDomains is only supported on versions 14+."
details:nil];
}
}

- (void)
setMediaTypesRequiresUserActionForConfigurationWithIdentifier:(nonnull NSNumber *)identifier
forTypes:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 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.
// Autogenerated from Pigeon (v9.2.4), do not edit directly.
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import

Expand Down Expand Up @@ -1067,6 +1067,30 @@ class WKWebViewConfigurationHostApi {
}
}

Future<void> setLimitsNavigationsToAppBoundDomains(
int arg_identifier, bool arg_limit) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.WKWebViewConfigurationHostApi.setLimitsNavigationsToAppBoundDomains',
codec,
binaryMessenger: _binaryMessenger);
final List<Object?>? replyList = await channel
.send(<Object?>[arg_identifier, arg_limit]) as List<Object?>?;
if (replyList == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
);
} else if (replyList.length > 1) {
throw PlatformException(
code: replyList[0]! as String,
message: replyList[1] as String?,
details: replyList[2],
);
} else {
return;
}
}

Future<void> setMediaTypesRequiringUserActionForPlayback(int arg_identifier,
List<WKAudiovisualMediaTypeEnumData?> arg_types) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,20 @@ class WKWebViewConfiguration extends NSObject {
);
}

/// Indicates whether the web view limits navigation to pages within the app’s domain.
///
/// When navigation is limited, Javascript evaluation is unrestricted.
/// See https://webkit.org/blog/10882/app-bound-domains/
///
/// Sets [WKWebViewConfiguration.limitsNavigationsToAppBoundDomains](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/3585117-limitsnavigationstoappbounddomai?language=objc).
Future<void> setLimitsNavigationsToAppBoundDomains(bool limit) {
return _webViewConfigurationApi
.setLimitsNavigationsToAppBoundDomainsForInstances(
this,
limit,
);
}

/// The media types that require a user gesture to begin playing.
///
/// Use [WKAudiovisualMediaType.none] to indicate that no user gestures are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,17 @@ class WKWebViewConfigurationHostApiImpl extends WKWebViewConfigurationHostApi {
);
}

/// Calls [setLimitsNavigationsToAppBoundDomains] with the ids of the provided object instances.
Future<void> setLimitsNavigationsToAppBoundDomainsForInstances(
WKWebViewConfiguration instance,
bool limit,
) {
return setLimitsNavigationsToAppBoundDomains(
instanceManager.getIdentifier(instance)!,
limit,
);
}

/// Calls [setMediaTypesRequiringUserActionForPlayback] with the ids of the provided object instances.
Future<void> setMediaTypesRequiringUserActionForPlaybackForInstances(
WKWebViewConfiguration instance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class WebKitWebViewControllerCreationParams
PlaybackMediaTypes.video,
},
this.allowsInlineMediaPlayback = false,
this.limitsNavigationsToAppBoundDomains = false,
@visibleForTesting InstanceManager? instanceManager,
}) : _instanceManager = instanceManager ?? NSObject.globalInstanceManager {
_configuration = webKitProxy.createWebViewConfiguration(
Expand All @@ -68,6 +69,8 @@ class WebKitWebViewControllerCreationParams
);
}
_configuration.setAllowsInlineMediaPlayback(allowsInlineMediaPlayback);
_configuration.setLimitsNavigationsToAppBoundDomains(
limitsNavigationsToAppBoundDomains);
}

/// Constructs a [WebKitWebViewControllerCreationParams] using a
Expand All @@ -83,11 +86,14 @@ class WebKitWebViewControllerCreationParams
PlaybackMediaTypes.video,
},
bool allowsInlineMediaPlayback = false,
bool limitsNavigationsToAppBoundDomains = false,
@visibleForTesting InstanceManager? instanceManager,
}) : this(
webKitProxy: webKitProxy,
mediaTypesRequiringUserAction: mediaTypesRequiringUserAction,
allowsInlineMediaPlayback: allowsInlineMediaPlayback,
limitsNavigationsToAppBoundDomains:
limitsNavigationsToAppBoundDomains,
instanceManager: instanceManager,
);

Expand All @@ -104,6 +110,13 @@ class WebKitWebViewControllerCreationParams
/// Defaults to false.
final bool allowsInlineMediaPlayback;

/// Whether to limit navigation to configured domains.
///
/// See https://webkit.org/blog/10882/app-bound-domains/
/// (Only available for iOS > 14.0)
/// Defaults to false.
final bool limitsNavigationsToAppBoundDomains;

/// Handles constructing objects and calling static methods for the WebKit
/// native library.
@visibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,11 @@ abstract class WKWebViewConfigurationHostApi {
)
void setAllowsInlineMediaPlayback(int identifier, bool allow);

@ObjCSelector(
'setLimitsNavigationsToAppBoundDomainsForConfigurationWithIdentifier:isLimited:',
)
void setLimitsNavigationsToAppBoundDomains(int identifier, bool limit);

@ObjCSelector(
'setMediaTypesRequiresUserActionForConfigurationWithIdentifier:forTypes:',
)
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.4.4
version: 3.5.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 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.
// Autogenerated from Pigeon (v9.2.4), do not edit directly.
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import
// ignore_for_file: avoid_relative_lib_imports
Expand Down Expand Up @@ -369,6 +369,8 @@ abstract class TestWKWebViewConfigurationHostApi {

void setAllowsInlineMediaPlayback(int identifier, bool allow);

void setLimitsNavigationsToAppBoundDomains(int identifier, bool limit);

void setMediaTypesRequiringUserActionForPlayback(
int identifier, List<WKAudiovisualMediaTypeEnumData?> types);

Expand Down Expand Up @@ -448,6 +450,33 @@ abstract class TestWKWebViewConfigurationHostApi {
});
}
}
{
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.WKWebViewConfigurationHostApi.setLimitsNavigationsToAppBoundDomains',
codec,
binaryMessenger: binaryMessenger);
if (api == null) {
_testBinaryMessengerBinding!.defaultBinaryMessenger
.setMockDecodedMessageHandler<Object?>(channel, null);
} else {
_testBinaryMessengerBinding!.defaultBinaryMessenger
.setMockDecodedMessageHandler<Object?>(channel,
(Object? message) async {
assert(message != null,
'Argument for dev.flutter.pigeon.WKWebViewConfigurationHostApi.setLimitsNavigationsToAppBoundDomains was null.');
final List<Object?> args = (message as List<Object?>?)!;
final int? arg_identifier = (args[0] as int?);
assert(arg_identifier != null,
'Argument for dev.flutter.pigeon.WKWebViewConfigurationHostApi.setLimitsNavigationsToAppBoundDomains was null, expected non-null int.');
final bool? arg_limit = (args[1] as bool?);
assert(arg_limit != null,
'Argument for dev.flutter.pigeon.WKWebViewConfigurationHostApi.setLimitsNavigationsToAppBoundDomains was null, expected non-null bool.');
api.setLimitsNavigationsToAppBoundDomains(
arg_identifier!, arg_limit!);
return <Object?>[];
});
}
}
{
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.WKWebViewConfigurationHostApi.setMediaTypesRequiringUserActionForPlayback',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ class MockTestWKWebViewConfigurationHostApi extends _i1.Mock
returnValueForMissingStub: null,
);
@override
void setLimitsNavigationsToAppBoundDomains(
int? identifier,
bool? limit,
) =>
super.noSuchMethod(
Invocation.method(
#setLimitsNavigationsToAppBoundDomains,
[
identifier,
limit,
],
),
returnValueForMissingStub: null,
);
@override
void setMediaTypesRequiringUserActionForPlayback(
int? identifier,
List<_i3.WKAudiovisualMediaTypeEnumData?>? types,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,14 @@ void main() {
));
});

test('limitsNavigationsToAppBoundDomains', () {
webViewConfiguration.setLimitsNavigationsToAppBoundDomains(true);
verify(mockPlatformHostApi.setLimitsNavigationsToAppBoundDomains(
instanceManager.getIdentifier(webViewConfiguration),
true,
));
});

test('mediaTypesRequiringUserActionForPlayback', () {
webViewConfiguration.setMediaTypesRequiringUserActionForPlayback(
<WKAudiovisualMediaType>{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,21 @@ class MockTestWKWebViewConfigurationHostApi extends _i1.Mock
returnValueForMissingStub: null,
);
@override
void setLimitsNavigationsToAppBoundDomains(
int? identifier,
bool? limit,
) =>
super.noSuchMethod(
Invocation.method(
#setLimitsNavigationsToAppBoundDomains,
[
identifier,
limit,
],
),
returnValueForMissingStub: null,
);
@override
void setMediaTypesRequiringUserActionForPlayback(
int? identifier,
List<_i4.WKAudiovisualMediaTypeEnumData?>? types,
Expand Down
Loading