Skip to content

Commit

Permalink
[webview_flutter_android] Add WebSettings methods setAllowFileAccess,…
Browse files Browse the repository at this point in the history
… setAllowContentAccess, setGeolocationEnabled, setCacheMode
  • Loading branch information
westracer committed Dec 11, 2024
1 parent 0f1fd49 commit ac78c15
Show file tree
Hide file tree
Showing 11 changed files with 531 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/webview_flutter/webview_flutter_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.2.0

* Adds WebSettings methods: `AndroidWebViewController.setAllowFileAccess`, `AndroidWebViewController.setAllowContentAccess`, `AndroidWebViewController.setGeolocationEnabled` and `AndroidWebViewController.setCacheMode`.

## 4.1.0

* Updates internal API wrapper to use `ProxyApi`s.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ private class AndroidWebkitLibraryPigeonProxyApiBaseCodec(
value is String ||
value is FileChooserMode ||
value is ConsoleMessageLevel ||
value is CacheMode ||
value == null) {
super.writeValue(stream, value)
return
Expand Down Expand Up @@ -726,6 +727,45 @@ enum class ConsoleMessageLevel(val raw: Int) {
}
}

/**
* Describes the way the cache is used.
*
* See https://developer.android.com/reference/android/webkit/WebSettings#setCacheMode(int).
*/
enum class CacheMode(val raw: Int) {
/**
* Normal cache usage mode.
*
* See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_DEFAULT
*/
LOAD_DEFAULT(0),
/**
* Use cached resources when they are available, even if they have expired. Otherwise load
* resources from the network.
*
* See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_CACHE_ELSE_NETWORK
*/
LOAD_CACHE_ELSE_NETWORK(1),
/**
* Don't use the cache, load from the network.
*
* See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_CACHE_ELSE_NETWORK
*/
LOAD_NO_CACHE(2),
/**
* Don't use the network, load from the cache.
*
* See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_CACHE_ONLY
*/
LOAD_CACHE_ONLY(3);

companion object {
fun ofRaw(raw: Int): CacheMode? {
return values().firstOrNull { it.raw == raw }
}
}
}

private open class AndroidWebkitLibraryPigeonCodec : StandardMessageCodec() {
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
return when (type) {
Expand All @@ -735,6 +775,9 @@ private open class AndroidWebkitLibraryPigeonCodec : StandardMessageCodec() {
130.toByte() -> {
return (readValue(buffer) as Long?)?.let { ConsoleMessageLevel.ofRaw(it.toInt()) }
}
131.toByte() -> {
return (readValue(buffer) as Long?)?.let { CacheMode.ofRaw(it.toInt()) }
}
else -> super.readValueOfType(type, buffer)
}
}
Expand All @@ -749,6 +792,10 @@ private open class AndroidWebkitLibraryPigeonCodec : StandardMessageCodec() {
stream.write(130)
writeValue(stream, value.raw)
}
is CacheMode -> {
stream.write(131)
writeValue(stream, value.raw)
}
else -> super.writeValue(stream, value)
}
}
Expand Down Expand Up @@ -2104,6 +2151,15 @@ 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)

/** Overrides the way the cache is used. */
abstract fun setCacheMode(pigeon_instance: android.webkit.WebSettings, mode: CacheMode)

/** 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 +2458,78 @@ 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?>(
binaryMessenger,
"dev.flutter.pigeon.webview_flutter_android.WebSettings.setCacheMode",
codec)
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val pigeon_instanceArg = args[0] as android.webkit.WebSettings
val modeArg = args[1] as CacheMode
val wrapped: List<Any?> =
try {
api.setCacheMode(pigeon_instanceArg, modeArg)
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 @@ -18,6 +18,21 @@ public WebSettingsProxyApi(@NonNull ProxyApiRegistrar pigeonRegistrar) {
super(pigeonRegistrar);
}

private static int mapCacheMode(CacheMode pigeonMode) {
switch (pigeonMode) {
case LOAD_DEFAULT:
return WebSettings.LOAD_DEFAULT;
case LOAD_CACHE_ELSE_NETWORK:
return WebSettings.LOAD_CACHE_ELSE_NETWORK;
case LOAD_NO_CACHE:
return WebSettings.LOAD_NO_CACHE;
case LOAD_CACHE_ONLY:
return WebSettings.LOAD_CACHE_ONLY;
}

return WebSettings.LOAD_DEFAULT;
}

@Override
public void setDomStorageEnabled(@NonNull WebSettings pigeon_instance, boolean flag) {
pigeon_instance.setDomStorageEnabled(flag);
Expand Down Expand Up @@ -81,6 +96,21 @@ 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 setCacheMode(@NonNull WebSettings pigeon_instance, @NonNull CacheMode mode) {
pigeon_instance.setCacheMode(mapCacheMode(mode));
}

@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 @@ -503,6 +503,32 @@ enum ConsoleMessageLevel {
unknown,
}

/// Describes the way the cache is used.
///
/// See https://developer.android.com/reference/android/webkit/WebSettings#setCacheMode(int).
enum CacheMode {
/// Normal cache usage mode.
///
/// See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_DEFAULT
loadDefault,

/// Use cached resources when they are available, even if they have expired.
/// Otherwise load resources from the network.
///
/// See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_CACHE_ELSE_NETWORK
loadCacheElseNetwork,

/// Don't use the cache, load from the network.
///
/// See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_CACHE_ELSE_NETWORK
loadNoCache,

/// Don't use the network, load from the cache.
///
/// See https://developer.android.com/reference/android/webkit/WebSettings#LOAD_CACHE_ONLY
loadCacheOnly,
}

class _PigeonCodec extends StandardMessageCodec {
const _PigeonCodec();
@override
Expand All @@ -516,6 +542,9 @@ class _PigeonCodec extends StandardMessageCodec {
} else if (value is ConsoleMessageLevel) {
buffer.putUint8(130);
writeValue(buffer, value.index);
} else if (value is CacheMode) {
buffer.putUint8(131);
writeValue(buffer, value.index);
} else {
super.writeValue(buffer, value);
}
Expand All @@ -530,6 +559,9 @@ class _PigeonCodec extends StandardMessageCodec {
case 130:
final int? value = readValue(buffer) as int?;
return value == null ? null : ConsoleMessageLevel.values[value];
case 131:
final int? value = readValue(buffer) as int?;
return value == null ? null : CacheMode.values[value];
default:
return super.readValueOfType(type, buffer);
}
Expand Down Expand Up @@ -2615,6 +2647,90 @@ 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;
}
}

/// Overrides the way the cache is used.
Future<void> setCacheMode(CacheMode mode) async {
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
_pigeonVar_codecWebSettings;
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
const String pigeonVar_channelName =
'dev.flutter.pigeon.webview_flutter_android.WebSettings.setCacheMode';
final BasicMessageChannel<Object?> pigeonVar_channel =
BasicMessageChannel<Object?>(
pigeonVar_channelName,
pigeonChannelCodec,
binaryMessenger: pigeonVar_binaryMessenger,
);
final List<Object?>? pigeonVar_replyList =
await pigeonVar_channel.send(<Object?>[this, mode]) 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
Loading

0 comments on commit ac78c15

Please sign in to comment.