Skip to content

[webview_flutter_android] Add additional WebSettings methods #8270

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
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,9 @@
## 4.3.0

* Adds support for disabling content URL access within WebView and disabling the Geolocation API.
See `AndroidWebViewController.setAllowContentAccess` and
`AndroidWebViewController.setGeolocationEnabled`.

## 4.2.0

* Adds support for configuring file access permissions. See `AndroidWebViewController.setAllowFileAccess`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,12 @@ abstract class PigeonApiWebSettings(
/** Enables or disables file access within WebView. */
abstract fun setAllowFileAccess(pigeon_instance: android.webkit.WebSettings, enabled: Boolean)

/** Enables or disables content URL access within WebView. */
abstract fun setAllowContentAccess(pigeon_instance: android.webkit.WebSettings, enabled: Boolean)

/** Sets whether Geolocation is enabled within WebView. */
abstract fun setGeolocationEnabled(pigeon_instance: android.webkit.WebSettings, enabled: Boolean)

/** Sets the text zoom of the page in percent. */
abstract fun setTextZoom(pigeon_instance: android.webkit.WebSettings, textZoom: Long)

Expand Down Expand Up @@ -2402,6 +2408,54 @@ abstract class PigeonApiWebSettings(
channel.setMessageHandler(null)
}
}
run {
val channel =
BasicMessageChannel<Any?>(
binaryMessenger,
"dev.flutter.pigeon.webview_flutter_android.WebSettings.setAllowContentAccess",
codec)
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val pigeon_instanceArg = args[0] as android.webkit.WebSettings
val enabledArg = args[1] as Boolean
val wrapped: List<Any?> =
try {
api.setAllowContentAccess(pigeon_instanceArg, enabledArg)
listOf(null)
} catch (exception: Throwable) {
wrapError(exception)
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
}
}
run {
val channel =
BasicMessageChannel<Any?>(
binaryMessenger,
"dev.flutter.pigeon.webview_flutter_android.WebSettings.setGeolocationEnabled",
codec)
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val pigeon_instanceArg = args[0] as android.webkit.WebSettings
val enabledArg = args[1] as Boolean
val wrapped: List<Any?> =
try {
api.setGeolocationEnabled(pigeon_instanceArg, enabledArg)
listOf(null)
} catch (exception: Throwable) {
wrapError(exception)
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
}
}
run {
val channel =
BasicMessageChannel<Any?>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ public void setAllowFileAccess(@NonNull WebSettings pigeon_instance, boolean ena
pigeon_instance.setAllowFileAccess(enabled);
}

@Override
public void setAllowContentAccess(@NonNull WebSettings pigeon_instance, boolean enabled) {
pigeon_instance.setAllowContentAccess(enabled);
}

@Override
public void setGeolocationEnabled(@NonNull WebSettings pigeon_instance, boolean enabled) {
pigeon_instance.setGeolocationEnabled(enabled);
}

@Override
public void setTextZoom(@NonNull WebSettings pigeon_instance, long textZoom) {
pigeon_instance.setTextZoom((int) textZoom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2615,6 +2615,62 @@ class WebSettings extends PigeonInternalProxyApiBaseClass {
}
}

/// Enables or disables content URL access within WebView.
Future<void> setAllowContentAccess(bool enabled) async {
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
_pigeonVar_codecWebSettings;
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
const String pigeonVar_channelName =
'dev.flutter.pigeon.webview_flutter_android.WebSettings.setAllowContentAccess';
final BasicMessageChannel<Object?> pigeonVar_channel =
BasicMessageChannel<Object?>(
pigeonVar_channelName,
pigeonChannelCodec,
binaryMessenger: pigeonVar_binaryMessenger,
);
final List<Object?>? pigeonVar_replyList = await pigeonVar_channel
.send(<Object?>[this, enabled]) as List<Object?>?;
if (pigeonVar_replyList == null) {
throw _createConnectionError(pigeonVar_channelName);
} else if (pigeonVar_replyList.length > 1) {
throw PlatformException(
code: pigeonVar_replyList[0]! as String,
message: pigeonVar_replyList[1] as String?,
details: pigeonVar_replyList[2],
);
} else {
return;
}
}

/// Sets whether Geolocation is enabled within WebView.
Future<void> setGeolocationEnabled(bool enabled) async {
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
_pigeonVar_codecWebSettings;
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
const String pigeonVar_channelName =
'dev.flutter.pigeon.webview_flutter_android.WebSettings.setGeolocationEnabled';
final BasicMessageChannel<Object?> pigeonVar_channel =
BasicMessageChannel<Object?>(
pigeonVar_channelName,
pigeonChannelCodec,
binaryMessenger: pigeonVar_binaryMessenger,
);
final List<Object?>? pigeonVar_replyList = await pigeonVar_channel
.send(<Object?>[this, enabled]) as List<Object?>?;
if (pigeonVar_replyList == null) {
throw _createConnectionError(pigeonVar_channelName);
} else if (pigeonVar_replyList.length > 1) {
throw PlatformException(
code: pigeonVar_replyList[0]! as String,
message: pigeonVar_replyList[1] as String?,
details: pigeonVar_replyList[2],
);
} else {
return;
}
}

/// Sets the text zoom of the page in percent.
Future<void> setTextZoom(int textZoom) async {
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,18 @@ class AndroidWebViewController extends PlatformWebViewController {
Future<void> setTextZoom(int textZoom) =>
_webView.settings.setTextZoom(textZoom);

/// Enables or disables content URL access.
///
/// The default is true.
Future<void> setAllowContentAccess(bool enabled) =>
_webView.settings.setAllowContentAccess(enabled);

/// Sets whether Geolocation is enabled.
///
/// The default is true.
Future<void> setGeolocationEnabled(bool enabled) =>
_webView.settings.setGeolocationEnabled(enabled);

/// Sets the callback that is invoked when the client should show a file
/// selector.
Future<void> setOnShowFileSelector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ abstract class WebSettings {
/// Enables or disables file access within WebView.
void setAllowFileAccess(bool enabled);

/// Enables or disables content URL access within WebView.
void setAllowContentAccess(bool enabled);

/// Sets whether Geolocation is enabled within WebView.
void setGeolocationEnabled(bool enabled);

/// Sets the text zoom of the page in percent.
void setTextZoom(int textZoom);

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: 4.2.0
version: 4.3.0

environment:
sdk: ^3.5.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,38 @@ void main() {
verify(mockSettings.setMediaPlaybackRequiresUserGesture(true)).called(1);
});

test('setAllowContentAccess', () async {
final MockWebView mockWebView = MockWebView();
final MockWebSettings mockSettings = MockWebSettings();
final AndroidWebViewController controller = createControllerWithMocks(
mockWebView: mockWebView,
mockSettings: mockSettings,
);

clearInteractions(mockWebView);

await controller.setAllowContentAccess(false);

verify(mockWebView.settings).called(1);
verify(mockSettings.setAllowContentAccess(false)).called(1);
});

test('setGeolocationEnabled', () async {
final MockWebView mockWebView = MockWebView();
final MockWebSettings mockSettings = MockWebSettings();
final AndroidWebViewController controller = createControllerWithMocks(
mockWebView: mockWebView,
mockSettings: mockSettings,
);

clearInteractions(mockWebView);

await controller.setGeolocationEnabled(false);

verify(mockWebView.settings).called(1);
verify(mockSettings.setGeolocationEnabled(false)).called(1);
});

test('setTextZoom', () async {
final MockWebView mockWebView = MockWebView();
final MockWebSettings mockSettings = MockWebSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,16 @@ class MockAndroidWebViewController extends _i1.Mock
),
) as _i3.PlatformWebViewControllerCreationParams);

@override
_i8.Future<void> setAllowFileAccess(bool? allow) => (super.noSuchMethod(
Invocation.method(
#setAllowFileAccess,
[allow],
),
returnValue: _i8.Future<void>.value(),
returnValueForMissingStub: _i8.Future<void>.value(),
) as _i8.Future<void>);

@override
_i8.Future<void> loadFile(String? absoluteFilePath) => (super.noSuchMethod(
Invocation.method(
Expand Down Expand Up @@ -789,6 +799,26 @@ class MockAndroidWebViewController extends _i1.Mock
returnValueForMissingStub: _i8.Future<void>.value(),
) as _i8.Future<void>);

@override
_i8.Future<void> setAllowContentAccess(bool? enabled) => (super.noSuchMethod(
Invocation.method(
#setAllowContentAccess,
[enabled],
),
returnValue: _i8.Future<void>.value(),
returnValueForMissingStub: _i8.Future<void>.value(),
) as _i8.Future<void>);

@override
_i8.Future<void> setGeolocationEnabled(bool? enabled) => (super.noSuchMethod(
Invocation.method(
#setGeolocationEnabled,
[enabled],
),
returnValue: _i8.Future<void>.value(),
returnValueForMissingStub: _i8.Future<void>.value(),
) as _i8.Future<void>);

@override
_i8.Future<void> setOnShowFileSelector(
_i8.Future<List<String>> Function(_i7.FileSelectorParams)?
Expand Down Expand Up @@ -2638,6 +2668,26 @@ class MockWebSettings extends _i1.Mock implements _i2.WebSettings {
returnValueForMissingStub: _i8.Future<void>.value(),
) as _i8.Future<void>);

@override
_i8.Future<void> setAllowContentAccess(bool? enabled) => (super.noSuchMethod(
Invocation.method(
#setAllowContentAccess,
[enabled],
),
returnValue: _i8.Future<void>.value(),
returnValueForMissingStub: _i8.Future<void>.value(),
) as _i8.Future<void>);

@override
_i8.Future<void> setGeolocationEnabled(bool? enabled) => (super.noSuchMethod(
Invocation.method(
#setGeolocationEnabled,
[enabled],
),
returnValue: _i8.Future<void>.value(),
returnValueForMissingStub: _i8.Future<void>.value(),
) as _i8.Future<void>);

@override
_i8.Future<void> setTextZoom(int? textZoom) => (super.noSuchMethod(
Invocation.method(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ class MockAndroidWebViewController extends _i1.Mock
),
) as _i3.PlatformWebViewControllerCreationParams);

@override
_i5.Future<void> setAllowFileAccess(bool? allow) => (super.noSuchMethod(
Invocation.method(
#setAllowFileAccess,
[allow],
),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);

@override
_i5.Future<void> loadFile(String? absoluteFilePath) => (super.noSuchMethod(
Invocation.method(
Expand Down Expand Up @@ -494,6 +504,26 @@ class MockAndroidWebViewController extends _i1.Mock
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);

@override
_i5.Future<void> setAllowContentAccess(bool? enabled) => (super.noSuchMethod(
Invocation.method(
#setAllowContentAccess,
[enabled],
),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);

@override
_i5.Future<void> setGeolocationEnabled(bool? enabled) => (super.noSuchMethod(
Invocation.method(
#setGeolocationEnabled,
[enabled],
),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);

@override
_i5.Future<void> setOnShowFileSelector(
_i5.Future<List<String>> Function(_i6.FileSelectorParams)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,26 @@ class MockWebSettings extends _i1.Mock implements _i2.WebSettings {
returnValueForMissingStub: _i4.Future<void>.value(),
) as _i4.Future<void>);

@override
_i4.Future<void> setAllowContentAccess(bool? enabled) => (super.noSuchMethod(
Invocation.method(
#setAllowContentAccess,
[enabled],
),
returnValue: _i4.Future<void>.value(),
returnValueForMissingStub: _i4.Future<void>.value(),
) as _i4.Future<void>);

@override
_i4.Future<void> setGeolocationEnabled(bool? enabled) => (super.noSuchMethod(
Invocation.method(
#setGeolocationEnabled,
[enabled],
),
returnValue: _i4.Future<void>.value(),
returnValueForMissingStub: _i4.Future<void>.value(),
) as _i4.Future<void>);

@override
_i4.Future<void> setTextZoom(int? textZoom) => (super.noSuchMethod(
Invocation.method(
Expand Down
Loading