Skip to content

Commit ac78c15

Browse files
committed
[webview_flutter_android] Add WebSettings methods setAllowFileAccess, setAllowContentAccess, setGeolocationEnabled, setCacheMode
1 parent 0f1fd49 commit ac78c15

11 files changed

+531
-1
lines changed

packages/webview_flutter/webview_flutter_android/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.2.0
2+
3+
* Adds WebSettings methods: `AndroidWebViewController.setAllowFileAccess`, `AndroidWebViewController.setAllowContentAccess`, `AndroidWebViewController.setGeolocationEnabled` and `AndroidWebViewController.setCacheMode`.
4+
15
## 4.1.0
26

37
* Updates internal API wrapper to use `ProxyApi`s.

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

+128
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ private class AndroidWebkitLibraryPigeonProxyApiBaseCodec(
571571
value is String ||
572572
value is FileChooserMode ||
573573
value is ConsoleMessageLevel ||
574+
value is CacheMode ||
574575
value == null) {
575576
super.writeValue(stream, value)
576577
return
@@ -726,6 +727,45 @@ enum class ConsoleMessageLevel(val raw: Int) {
726727
}
727728
}
728729

730+
/**
731+
* Describes the way the cache is used.
732+
*
733+
* See https://developer.android.com/reference/android/webkit/WebSettings#setCacheMode(int).
734+
*/
735+
enum class CacheMode(val raw: Int) {
736+
/**
737+
* Normal cache usage mode.
738+
*
739+
* See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_DEFAULT
740+
*/
741+
LOAD_DEFAULT(0),
742+
/**
743+
* Use cached resources when they are available, even if they have expired. Otherwise load
744+
* resources from the network.
745+
*
746+
* See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_CACHE_ELSE_NETWORK
747+
*/
748+
LOAD_CACHE_ELSE_NETWORK(1),
749+
/**
750+
* Don't use the cache, load from the network.
751+
*
752+
* See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_CACHE_ELSE_NETWORK
753+
*/
754+
LOAD_NO_CACHE(2),
755+
/**
756+
* Don't use the network, load from the cache.
757+
*
758+
* See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_CACHE_ONLY
759+
*/
760+
LOAD_CACHE_ONLY(3);
761+
762+
companion object {
763+
fun ofRaw(raw: Int): CacheMode? {
764+
return values().firstOrNull { it.raw == raw }
765+
}
766+
}
767+
}
768+
729769
private open class AndroidWebkitLibraryPigeonCodec : StandardMessageCodec() {
730770
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
731771
return when (type) {
@@ -735,6 +775,9 @@ private open class AndroidWebkitLibraryPigeonCodec : StandardMessageCodec() {
735775
130.toByte() -> {
736776
return (readValue(buffer) as Long?)?.let { ConsoleMessageLevel.ofRaw(it.toInt()) }
737777
}
778+
131.toByte() -> {
779+
return (readValue(buffer) as Long?)?.let { CacheMode.ofRaw(it.toInt()) }
780+
}
738781
else -> super.readValueOfType(type, buffer)
739782
}
740783
}
@@ -749,6 +792,10 @@ private open class AndroidWebkitLibraryPigeonCodec : StandardMessageCodec() {
749792
stream.write(130)
750793
writeValue(stream, value.raw)
751794
}
795+
is CacheMode -> {
796+
stream.write(131)
797+
writeValue(stream, value.raw)
798+
}
752799
else -> super.writeValue(stream, value)
753800
}
754801
}
@@ -2104,6 +2151,15 @@ abstract class PigeonApiWebSettings(
21042151
/** Enables or disables file access within WebView. */
21052152
abstract fun setAllowFileAccess(pigeon_instance: android.webkit.WebSettings, enabled: Boolean)
21062153

2154+
/** Enables or disables content URL access within WebView. */
2155+
abstract fun setAllowContentAccess(pigeon_instance: android.webkit.WebSettings, enabled: Boolean)
2156+
2157+
/** Sets whether Geolocation is enabled within WebView. */
2158+
abstract fun setGeolocationEnabled(pigeon_instance: android.webkit.WebSettings, enabled: Boolean)
2159+
2160+
/** Overrides the way the cache is used. */
2161+
abstract fun setCacheMode(pigeon_instance: android.webkit.WebSettings, mode: CacheMode)
2162+
21072163
/** Sets the text zoom of the page in percent. */
21082164
abstract fun setTextZoom(pigeon_instance: android.webkit.WebSettings, textZoom: Long)
21092165

@@ -2402,6 +2458,78 @@ abstract class PigeonApiWebSettings(
24022458
channel.setMessageHandler(null)
24032459
}
24042460
}
2461+
run {
2462+
val channel =
2463+
BasicMessageChannel<Any?>(
2464+
binaryMessenger,
2465+
"dev.flutter.pigeon.webview_flutter_android.WebSettings.setAllowContentAccess",
2466+
codec)
2467+
if (api != null) {
2468+
channel.setMessageHandler { message, reply ->
2469+
val args = message as List<Any?>
2470+
val pigeon_instanceArg = args[0] as android.webkit.WebSettings
2471+
val enabledArg = args[1] as Boolean
2472+
val wrapped: List<Any?> =
2473+
try {
2474+
api.setAllowContentAccess(pigeon_instanceArg, enabledArg)
2475+
listOf(null)
2476+
} catch (exception: Throwable) {
2477+
wrapError(exception)
2478+
}
2479+
reply.reply(wrapped)
2480+
}
2481+
} else {
2482+
channel.setMessageHandler(null)
2483+
}
2484+
}
2485+
run {
2486+
val channel =
2487+
BasicMessageChannel<Any?>(
2488+
binaryMessenger,
2489+
"dev.flutter.pigeon.webview_flutter_android.WebSettings.setGeolocationEnabled",
2490+
codec)
2491+
if (api != null) {
2492+
channel.setMessageHandler { message, reply ->
2493+
val args = message as List<Any?>
2494+
val pigeon_instanceArg = args[0] as android.webkit.WebSettings
2495+
val enabledArg = args[1] as Boolean
2496+
val wrapped: List<Any?> =
2497+
try {
2498+
api.setGeolocationEnabled(pigeon_instanceArg, enabledArg)
2499+
listOf(null)
2500+
} catch (exception: Throwable) {
2501+
wrapError(exception)
2502+
}
2503+
reply.reply(wrapped)
2504+
}
2505+
} else {
2506+
channel.setMessageHandler(null)
2507+
}
2508+
}
2509+
run {
2510+
val channel =
2511+
BasicMessageChannel<Any?>(
2512+
binaryMessenger,
2513+
"dev.flutter.pigeon.webview_flutter_android.WebSettings.setCacheMode",
2514+
codec)
2515+
if (api != null) {
2516+
channel.setMessageHandler { message, reply ->
2517+
val args = message as List<Any?>
2518+
val pigeon_instanceArg = args[0] as android.webkit.WebSettings
2519+
val modeArg = args[1] as CacheMode
2520+
val wrapped: List<Any?> =
2521+
try {
2522+
api.setCacheMode(pigeon_instanceArg, modeArg)
2523+
listOf(null)
2524+
} catch (exception: Throwable) {
2525+
wrapError(exception)
2526+
}
2527+
reply.reply(wrapped)
2528+
}
2529+
} else {
2530+
channel.setMessageHandler(null)
2531+
}
2532+
}
24052533
run {
24062534
val channel =
24072535
BasicMessageChannel<Any?>(

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

+30
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ public WebSettingsProxyApi(@NonNull ProxyApiRegistrar pigeonRegistrar) {
1818
super(pigeonRegistrar);
1919
}
2020

21+
private static int mapCacheMode(CacheMode pigeonMode) {
22+
switch (pigeonMode) {
23+
case LOAD_DEFAULT:
24+
return WebSettings.LOAD_DEFAULT;
25+
case LOAD_CACHE_ELSE_NETWORK:
26+
return WebSettings.LOAD_CACHE_ELSE_NETWORK;
27+
case LOAD_NO_CACHE:
28+
return WebSettings.LOAD_NO_CACHE;
29+
case LOAD_CACHE_ONLY:
30+
return WebSettings.LOAD_CACHE_ONLY;
31+
}
32+
33+
return WebSettings.LOAD_DEFAULT;
34+
}
35+
2136
@Override
2237
public void setDomStorageEnabled(@NonNull WebSettings pigeon_instance, boolean flag) {
2338
pigeon_instance.setDomStorageEnabled(flag);
@@ -81,6 +96,21 @@ public void setAllowFileAccess(@NonNull WebSettings pigeon_instance, boolean ena
8196
pigeon_instance.setAllowFileAccess(enabled);
8297
}
8398

99+
@Override
100+
public void setAllowContentAccess(@NonNull WebSettings pigeon_instance, boolean enabled) {
101+
pigeon_instance.setAllowContentAccess(enabled);
102+
}
103+
104+
@Override
105+
public void setGeolocationEnabled(@NonNull WebSettings pigeon_instance, boolean enabled) {
106+
pigeon_instance.setGeolocationEnabled(enabled);
107+
}
108+
109+
@Override
110+
public void setCacheMode(@NonNull WebSettings pigeon_instance, @NonNull CacheMode mode) {
111+
pigeon_instance.setCacheMode(mapCacheMode(mode));
112+
}
113+
84114
@Override
85115
public void setTextZoom(@NonNull WebSettings pigeon_instance, long textZoom) {
86116
pigeon_instance.setTextZoom((int) textZoom);

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

+116
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,32 @@ enum ConsoleMessageLevel {
503503
unknown,
504504
}
505505

506+
/// Describes the way the cache is used.
507+
///
508+
/// See https://developer.android.com/reference/android/webkit/WebSettings#setCacheMode(int).
509+
enum CacheMode {
510+
/// Normal cache usage mode.
511+
///
512+
/// See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_DEFAULT
513+
loadDefault,
514+
515+
/// Use cached resources when they are available, even if they have expired.
516+
/// Otherwise load resources from the network.
517+
///
518+
/// See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_CACHE_ELSE_NETWORK
519+
loadCacheElseNetwork,
520+
521+
/// Don't use the cache, load from the network.
522+
///
523+
/// See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_CACHE_ELSE_NETWORK
524+
loadNoCache,
525+
526+
/// Don't use the network, load from the cache.
527+
///
528+
/// See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_CACHE_ONLY
529+
loadCacheOnly,
530+
}
531+
506532
class _PigeonCodec extends StandardMessageCodec {
507533
const _PigeonCodec();
508534
@override
@@ -516,6 +542,9 @@ class _PigeonCodec extends StandardMessageCodec {
516542
} else if (value is ConsoleMessageLevel) {
517543
buffer.putUint8(130);
518544
writeValue(buffer, value.index);
545+
} else if (value is CacheMode) {
546+
buffer.putUint8(131);
547+
writeValue(buffer, value.index);
519548
} else {
520549
super.writeValue(buffer, value);
521550
}
@@ -530,6 +559,9 @@ class _PigeonCodec extends StandardMessageCodec {
530559
case 130:
531560
final int? value = readValue(buffer) as int?;
532561
return value == null ? null : ConsoleMessageLevel.values[value];
562+
case 131:
563+
final int? value = readValue(buffer) as int?;
564+
return value == null ? null : CacheMode.values[value];
533565
default:
534566
return super.readValueOfType(type, buffer);
535567
}
@@ -2615,6 +2647,90 @@ class WebSettings extends PigeonInternalProxyApiBaseClass {
26152647
}
26162648
}
26172649

2650+
/// Enables or disables content URL access within WebView.
2651+
Future<void> setAllowContentAccess(bool enabled) async {
2652+
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
2653+
_pigeonVar_codecWebSettings;
2654+
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
2655+
const String pigeonVar_channelName =
2656+
'dev.flutter.pigeon.webview_flutter_android.WebSettings.setAllowContentAccess';
2657+
final BasicMessageChannel<Object?> pigeonVar_channel =
2658+
BasicMessageChannel<Object?>(
2659+
pigeonVar_channelName,
2660+
pigeonChannelCodec,
2661+
binaryMessenger: pigeonVar_binaryMessenger,
2662+
);
2663+
final List<Object?>? pigeonVar_replyList = await pigeonVar_channel
2664+
.send(<Object?>[this, enabled]) as List<Object?>?;
2665+
if (pigeonVar_replyList == null) {
2666+
throw _createConnectionError(pigeonVar_channelName);
2667+
} else if (pigeonVar_replyList.length > 1) {
2668+
throw PlatformException(
2669+
code: pigeonVar_replyList[0]! as String,
2670+
message: pigeonVar_replyList[1] as String?,
2671+
details: pigeonVar_replyList[2],
2672+
);
2673+
} else {
2674+
return;
2675+
}
2676+
}
2677+
2678+
/// Sets whether Geolocation is enabled within WebView.
2679+
Future<void> setGeolocationEnabled(bool enabled) async {
2680+
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
2681+
_pigeonVar_codecWebSettings;
2682+
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
2683+
const String pigeonVar_channelName =
2684+
'dev.flutter.pigeon.webview_flutter_android.WebSettings.setGeolocationEnabled';
2685+
final BasicMessageChannel<Object?> pigeonVar_channel =
2686+
BasicMessageChannel<Object?>(
2687+
pigeonVar_channelName,
2688+
pigeonChannelCodec,
2689+
binaryMessenger: pigeonVar_binaryMessenger,
2690+
);
2691+
final List<Object?>? pigeonVar_replyList = await pigeonVar_channel
2692+
.send(<Object?>[this, enabled]) as List<Object?>?;
2693+
if (pigeonVar_replyList == null) {
2694+
throw _createConnectionError(pigeonVar_channelName);
2695+
} else if (pigeonVar_replyList.length > 1) {
2696+
throw PlatformException(
2697+
code: pigeonVar_replyList[0]! as String,
2698+
message: pigeonVar_replyList[1] as String?,
2699+
details: pigeonVar_replyList[2],
2700+
);
2701+
} else {
2702+
return;
2703+
}
2704+
}
2705+
2706+
/// Overrides the way the cache is used.
2707+
Future<void> setCacheMode(CacheMode mode) async {
2708+
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
2709+
_pigeonVar_codecWebSettings;
2710+
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
2711+
const String pigeonVar_channelName =
2712+
'dev.flutter.pigeon.webview_flutter_android.WebSettings.setCacheMode';
2713+
final BasicMessageChannel<Object?> pigeonVar_channel =
2714+
BasicMessageChannel<Object?>(
2715+
pigeonVar_channelName,
2716+
pigeonChannelCodec,
2717+
binaryMessenger: pigeonVar_binaryMessenger,
2718+
);
2719+
final List<Object?>? pigeonVar_replyList =
2720+
await pigeonVar_channel.send(<Object?>[this, mode]) as List<Object?>?;
2721+
if (pigeonVar_replyList == null) {
2722+
throw _createConnectionError(pigeonVar_channelName);
2723+
} else if (pigeonVar_replyList.length > 1) {
2724+
throw PlatformException(
2725+
code: pigeonVar_replyList[0]! as String,
2726+
message: pigeonVar_replyList[1] as String?,
2727+
details: pigeonVar_replyList[2],
2728+
);
2729+
} else {
2730+
return;
2731+
}
2732+
}
2733+
26182734
/// Sets the text zoom of the page in percent.
26192735
Future<void> setTextZoom(int textZoom) async {
26202736
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =

0 commit comments

Comments
 (0)