Skip to content

Commit 91567cb

Browse files
authored
Merge pull request #1 from QuickBirdEng/webview_android_websettings_methods
[webview_flutter_android] Add additional WebSettings methods
2 parents ffd311c + 85d1a8a commit 91567cb

File tree

11 files changed

+277
-1
lines changed

11 files changed

+277
-1
lines changed

packages/webview_flutter/webview_flutter_android/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 4.3.0
2+
3+
* Adds support for disabling content URL access within WebView and disabling the Geolocation API.
4+
See `AndroidWebViewController.setAllowContentAccess` and
5+
`AndroidWebViewController.setGeolocationEnabled`.
6+
17
## 4.2.0
28

39
* Adds support for configuring file access permissions. See `AndroidWebViewController.setAllowFileAccess`.

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/AndroidWebkitLibrary.g.kt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,12 @@ abstract class PigeonApiWebSettings(
21042104
/** Enables or disables file access within WebView. */
21052105
abstract fun setAllowFileAccess(pigeon_instance: android.webkit.WebSettings, enabled: Boolean)
21062106

2107+
/** Enables or disables content URL access within WebView. */
2108+
abstract fun setAllowContentAccess(pigeon_instance: android.webkit.WebSettings, enabled: Boolean)
2109+
2110+
/** Sets whether Geolocation is enabled within WebView. */
2111+
abstract fun setGeolocationEnabled(pigeon_instance: android.webkit.WebSettings, enabled: Boolean)
2112+
21072113
/** Sets the text zoom of the page in percent. */
21082114
abstract fun setTextZoom(pigeon_instance: android.webkit.WebSettings, textZoom: Long)
21092115

@@ -2402,6 +2408,54 @@ abstract class PigeonApiWebSettings(
24022408
channel.setMessageHandler(null)
24032409
}
24042410
}
2411+
run {
2412+
val channel =
2413+
BasicMessageChannel<Any?>(
2414+
binaryMessenger,
2415+
"dev.flutter.pigeon.webview_flutter_android.WebSettings.setAllowContentAccess",
2416+
codec)
2417+
if (api != null) {
2418+
channel.setMessageHandler { message, reply ->
2419+
val args = message as List<Any?>
2420+
val pigeon_instanceArg = args[0] as android.webkit.WebSettings
2421+
val enabledArg = args[1] as Boolean
2422+
val wrapped: List<Any?> =
2423+
try {
2424+
api.setAllowContentAccess(pigeon_instanceArg, enabledArg)
2425+
listOf(null)
2426+
} catch (exception: Throwable) {
2427+
wrapError(exception)
2428+
}
2429+
reply.reply(wrapped)
2430+
}
2431+
} else {
2432+
channel.setMessageHandler(null)
2433+
}
2434+
}
2435+
run {
2436+
val channel =
2437+
BasicMessageChannel<Any?>(
2438+
binaryMessenger,
2439+
"dev.flutter.pigeon.webview_flutter_android.WebSettings.setGeolocationEnabled",
2440+
codec)
2441+
if (api != null) {
2442+
channel.setMessageHandler { message, reply ->
2443+
val args = message as List<Any?>
2444+
val pigeon_instanceArg = args[0] as android.webkit.WebSettings
2445+
val enabledArg = args[1] as Boolean
2446+
val wrapped: List<Any?> =
2447+
try {
2448+
api.setGeolocationEnabled(pigeon_instanceArg, enabledArg)
2449+
listOf(null)
2450+
} catch (exception: Throwable) {
2451+
wrapError(exception)
2452+
}
2453+
reply.reply(wrapped)
2454+
}
2455+
} else {
2456+
channel.setMessageHandler(null)
2457+
}
2458+
}
24052459
run {
24062460
val channel =
24072461
BasicMessageChannel<Any?>(

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebSettingsProxyApi.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public void setAllowFileAccess(@NonNull WebSettings pigeon_instance, boolean ena
8181
pigeon_instance.setAllowFileAccess(enabled);
8282
}
8383

84+
@Override
85+
public void setAllowContentAccess(@NonNull WebSettings pigeon_instance, boolean enabled) {
86+
pigeon_instance.setAllowContentAccess(enabled);
87+
}
88+
89+
@Override
90+
public void setGeolocationEnabled(@NonNull WebSettings pigeon_instance, boolean enabled) {
91+
pigeon_instance.setGeolocationEnabled(enabled);
92+
}
93+
8494
@Override
8595
public void setTextZoom(@NonNull WebSettings pigeon_instance, long textZoom) {
8696
pigeon_instance.setTextZoom((int) textZoom);

packages/webview_flutter/webview_flutter_android/lib/src/android_webkit.g.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,6 +2615,62 @@ class WebSettings extends PigeonInternalProxyApiBaseClass {
26152615
}
26162616
}
26172617

2618+
/// Enables or disables content URL access within WebView.
2619+
Future<void> setAllowContentAccess(bool enabled) async {
2620+
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
2621+
_pigeonVar_codecWebSettings;
2622+
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
2623+
const String pigeonVar_channelName =
2624+
'dev.flutter.pigeon.webview_flutter_android.WebSettings.setAllowContentAccess';
2625+
final BasicMessageChannel<Object?> pigeonVar_channel =
2626+
BasicMessageChannel<Object?>(
2627+
pigeonVar_channelName,
2628+
pigeonChannelCodec,
2629+
binaryMessenger: pigeonVar_binaryMessenger,
2630+
);
2631+
final List<Object?>? pigeonVar_replyList = await pigeonVar_channel
2632+
.send(<Object?>[this, enabled]) as List<Object?>?;
2633+
if (pigeonVar_replyList == null) {
2634+
throw _createConnectionError(pigeonVar_channelName);
2635+
} else if (pigeonVar_replyList.length > 1) {
2636+
throw PlatformException(
2637+
code: pigeonVar_replyList[0]! as String,
2638+
message: pigeonVar_replyList[1] as String?,
2639+
details: pigeonVar_replyList[2],
2640+
);
2641+
} else {
2642+
return;
2643+
}
2644+
}
2645+
2646+
/// Sets whether Geolocation is enabled within WebView.
2647+
Future<void> setGeolocationEnabled(bool enabled) async {
2648+
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
2649+
_pigeonVar_codecWebSettings;
2650+
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
2651+
const String pigeonVar_channelName =
2652+
'dev.flutter.pigeon.webview_flutter_android.WebSettings.setGeolocationEnabled';
2653+
final BasicMessageChannel<Object?> pigeonVar_channel =
2654+
BasicMessageChannel<Object?>(
2655+
pigeonVar_channelName,
2656+
pigeonChannelCodec,
2657+
binaryMessenger: pigeonVar_binaryMessenger,
2658+
);
2659+
final List<Object?>? pigeonVar_replyList = await pigeonVar_channel
2660+
.send(<Object?>[this, enabled]) as List<Object?>?;
2661+
if (pigeonVar_replyList == null) {
2662+
throw _createConnectionError(pigeonVar_channelName);
2663+
} else if (pigeonVar_replyList.length > 1) {
2664+
throw PlatformException(
2665+
code: pigeonVar_replyList[0]! as String,
2666+
message: pigeonVar_replyList[1] as String?,
2667+
details: pigeonVar_replyList[2],
2668+
);
2669+
} else {
2670+
return;
2671+
}
2672+
}
2673+
26182674
/// Sets the text zoom of the page in percent.
26192675
Future<void> setTextZoom(int textZoom) async {
26202676
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =

packages/webview_flutter/webview_flutter_android/lib/src/android_webview_controller.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,18 @@ class AndroidWebViewController extends PlatformWebViewController {
599599
Future<void> setTextZoom(int textZoom) =>
600600
_webView.settings.setTextZoom(textZoom);
601601

602+
/// Enables or disables content URL access.
603+
///
604+
/// The default is true.
605+
Future<void> setAllowContentAccess(bool enabled) =>
606+
_webView.settings.setAllowContentAccess(enabled);
607+
608+
/// Sets whether Geolocation is enabled.
609+
///
610+
/// The default is true.
611+
Future<void> setGeolocationEnabled(bool enabled) =>
612+
_webView.settings.setGeolocationEnabled(enabled);
613+
602614
/// Sets the callback that is invoked when the client should show a file
603615
/// selector.
604616
Future<void> setOnShowFileSelector(

packages/webview_flutter/webview_flutter_android/pigeons/android_webkit.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,12 @@ abstract class WebSettings {
354354
/// Enables or disables file access within WebView.
355355
void setAllowFileAccess(bool enabled);
356356

357+
/// Enables or disables content URL access within WebView.
358+
void setAllowContentAccess(bool enabled);
359+
360+
/// Sets whether Geolocation is enabled within WebView.
361+
void setGeolocationEnabled(bool enabled);
362+
357363
/// Sets the text zoom of the page in percent.
358364
void setTextZoom(int textZoom);
359365

packages/webview_flutter/webview_flutter_android/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: webview_flutter_android
22
description: A Flutter plugin that provides a WebView widget on Android.
33
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
5-
version: 4.2.0
5+
version: 4.3.0
66

77
environment:
88
sdk: ^3.5.0

packages/webview_flutter/webview_flutter_android/test/android_webview_controller_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,38 @@ void main() {
15261526
verify(mockSettings.setMediaPlaybackRequiresUserGesture(true)).called(1);
15271527
});
15281528

1529+
test('setAllowContentAccess', () async {
1530+
final MockWebView mockWebView = MockWebView();
1531+
final MockWebSettings mockSettings = MockWebSettings();
1532+
final AndroidWebViewController controller = createControllerWithMocks(
1533+
mockWebView: mockWebView,
1534+
mockSettings: mockSettings,
1535+
);
1536+
1537+
clearInteractions(mockWebView);
1538+
1539+
await controller.setAllowContentAccess(false);
1540+
1541+
verify(mockWebView.settings).called(1);
1542+
verify(mockSettings.setAllowContentAccess(false)).called(1);
1543+
});
1544+
1545+
test('setGeolocationEnabled', () async {
1546+
final MockWebView mockWebView = MockWebView();
1547+
final MockWebSettings mockSettings = MockWebSettings();
1548+
final AndroidWebViewController controller = createControllerWithMocks(
1549+
mockWebView: mockWebView,
1550+
mockSettings: mockSettings,
1551+
);
1552+
1553+
clearInteractions(mockWebView);
1554+
1555+
await controller.setGeolocationEnabled(false);
1556+
1557+
verify(mockWebView.settings).called(1);
1558+
verify(mockSettings.setGeolocationEnabled(false)).called(1);
1559+
});
1560+
15291561
test('setTextZoom', () async {
15301562
final MockWebView mockWebView = MockWebView();
15311563
final MockWebSettings mockSettings = MockWebSettings();

packages/webview_flutter/webview_flutter_android/test/android_webview_controller_test.mocks.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,16 @@ class MockAndroidWebViewController extends _i1.Mock
455455
),
456456
) as _i3.PlatformWebViewControllerCreationParams);
457457

458+
@override
459+
_i8.Future<void> setAllowFileAccess(bool? allow) => (super.noSuchMethod(
460+
Invocation.method(
461+
#setAllowFileAccess,
462+
[allow],
463+
),
464+
returnValue: _i8.Future<void>.value(),
465+
returnValueForMissingStub: _i8.Future<void>.value(),
466+
) as _i8.Future<void>);
467+
458468
@override
459469
_i8.Future<void> loadFile(String? absoluteFilePath) => (super.noSuchMethod(
460470
Invocation.method(
@@ -789,6 +799,26 @@ class MockAndroidWebViewController extends _i1.Mock
789799
returnValueForMissingStub: _i8.Future<void>.value(),
790800
) as _i8.Future<void>);
791801

802+
@override
803+
_i8.Future<void> setAllowContentAccess(bool? enabled) => (super.noSuchMethod(
804+
Invocation.method(
805+
#setAllowContentAccess,
806+
[enabled],
807+
),
808+
returnValue: _i8.Future<void>.value(),
809+
returnValueForMissingStub: _i8.Future<void>.value(),
810+
) as _i8.Future<void>);
811+
812+
@override
813+
_i8.Future<void> setGeolocationEnabled(bool? enabled) => (super.noSuchMethod(
814+
Invocation.method(
815+
#setGeolocationEnabled,
816+
[enabled],
817+
),
818+
returnValue: _i8.Future<void>.value(),
819+
returnValueForMissingStub: _i8.Future<void>.value(),
820+
) as _i8.Future<void>);
821+
792822
@override
793823
_i8.Future<void> setOnShowFileSelector(
794824
_i8.Future<List<String>> Function(_i7.FileSelectorParams)?
@@ -2638,6 +2668,26 @@ class MockWebSettings extends _i1.Mock implements _i2.WebSettings {
26382668
returnValueForMissingStub: _i8.Future<void>.value(),
26392669
) as _i8.Future<void>);
26402670

2671+
@override
2672+
_i8.Future<void> setAllowContentAccess(bool? enabled) => (super.noSuchMethod(
2673+
Invocation.method(
2674+
#setAllowContentAccess,
2675+
[enabled],
2676+
),
2677+
returnValue: _i8.Future<void>.value(),
2678+
returnValueForMissingStub: _i8.Future<void>.value(),
2679+
) as _i8.Future<void>);
2680+
2681+
@override
2682+
_i8.Future<void> setGeolocationEnabled(bool? enabled) => (super.noSuchMethod(
2683+
Invocation.method(
2684+
#setGeolocationEnabled,
2685+
[enabled],
2686+
),
2687+
returnValue: _i8.Future<void>.value(),
2688+
returnValueForMissingStub: _i8.Future<void>.value(),
2689+
) as _i8.Future<void>);
2690+
26412691
@override
26422692
_i8.Future<void> setTextZoom(int? textZoom) => (super.noSuchMethod(
26432693
Invocation.method(

packages/webview_flutter/webview_flutter_android/test/android_webview_cookie_manager_test.mocks.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ class MockAndroidWebViewController extends _i1.Mock
178178
),
179179
) as _i3.PlatformWebViewControllerCreationParams);
180180

181+
@override
182+
_i5.Future<void> setAllowFileAccess(bool? allow) => (super.noSuchMethod(
183+
Invocation.method(
184+
#setAllowFileAccess,
185+
[allow],
186+
),
187+
returnValue: _i5.Future<void>.value(),
188+
returnValueForMissingStub: _i5.Future<void>.value(),
189+
) as _i5.Future<void>);
190+
181191
@override
182192
_i5.Future<void> loadFile(String? absoluteFilePath) => (super.noSuchMethod(
183193
Invocation.method(
@@ -494,6 +504,26 @@ class MockAndroidWebViewController extends _i1.Mock
494504
returnValueForMissingStub: _i5.Future<void>.value(),
495505
) as _i5.Future<void>);
496506

507+
@override
508+
_i5.Future<void> setAllowContentAccess(bool? enabled) => (super.noSuchMethod(
509+
Invocation.method(
510+
#setAllowContentAccess,
511+
[enabled],
512+
),
513+
returnValue: _i5.Future<void>.value(),
514+
returnValueForMissingStub: _i5.Future<void>.value(),
515+
) as _i5.Future<void>);
516+
517+
@override
518+
_i5.Future<void> setGeolocationEnabled(bool? enabled) => (super.noSuchMethod(
519+
Invocation.method(
520+
#setGeolocationEnabled,
521+
[enabled],
522+
),
523+
returnValue: _i5.Future<void>.value(),
524+
returnValueForMissingStub: _i5.Future<void>.value(),
525+
) as _i5.Future<void>);
526+
497527
@override
498528
_i5.Future<void> setOnShowFileSelector(
499529
_i5.Future<List<String>> Function(_i6.FileSelectorParams)?

0 commit comments

Comments
 (0)