Skip to content

Commit 1a01f13

Browse files
committed
Add wrapper for androidx.webkit.WebViewFeature
1 parent 2d07746 commit 1a01f13

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,12 @@ abstract class AndroidWebkitLibraryPigeonProxyApiRegistrar(val binaryMessenger:
573573
*/
574574
abstract fun getPigeonApiWebSettingsCompat(): PigeonApiWebSettingsCompat
575575

576+
/**
577+
* An implementation of [PigeonApiWebViewFeature] used to add a new Dart instance of
578+
* `WebViewFeature` to the Dart `InstanceManager`.
579+
*/
580+
abstract fun getPigeonApiWebViewFeature(): PigeonApiWebViewFeature
581+
576582
fun setUp() {
577583
AndroidWebkitLibraryPigeonInstanceManagerApi.setUpMessageHandlers(
578584
binaryMessenger, instanceManager)
@@ -606,6 +612,7 @@ abstract class AndroidWebkitLibraryPigeonProxyApiRegistrar(val binaryMessenger:
606612
PigeonApiCertificate.setUpMessageHandlers(binaryMessenger, getPigeonApiCertificate())
607613
PigeonApiWebSettingsCompat.setUpMessageHandlers(
608614
binaryMessenger, getPigeonApiWebSettingsCompat())
615+
PigeonApiWebViewFeature.setUpMessageHandlers(binaryMessenger, getPigeonApiWebViewFeature())
609616
}
610617

611618
fun tearDown() {
@@ -632,6 +639,7 @@ abstract class AndroidWebkitLibraryPigeonProxyApiRegistrar(val binaryMessenger:
632639
PigeonApiSslCertificate.setUpMessageHandlers(binaryMessenger, null)
633640
PigeonApiCertificate.setUpMessageHandlers(binaryMessenger, null)
634641
PigeonApiWebSettingsCompat.setUpMessageHandlers(binaryMessenger, null)
642+
PigeonApiWebViewFeature.setUpMessageHandlers(binaryMessenger, null)
635643
}
636644
}
637645

@@ -738,6 +746,8 @@ private class AndroidWebkitLibraryPigeonProxyApiBaseCodec(
738746
registrar.getPigeonApiCertificate().pigeon_newInstance(value) {}
739747
} else if (value is androidx.webkit.WebSettingsCompat) {
740748
registrar.getPigeonApiWebSettingsCompat().pigeon_newInstance(value) {}
749+
} else if (value is androidx.webkit.WebViewFeature) {
750+
registrar.getPigeonApiWebViewFeature().pigeon_newInstance(value) {}
741751
}
742752

743753
when {
@@ -6397,3 +6407,75 @@ abstract class PigeonApiWebSettingsCompat(
63976407
}
63986408
}
63996409

6410+
@Suppress("UNCHECKED_CAST")
6411+
abstract class PigeonApiWebViewFeature(
6412+
open val pigeonRegistrar: AndroidWebkitLibraryPigeonProxyApiRegistrar
6413+
) {
6414+
abstract fun isFeatureSupported(feature: String): Boolean
6415+
6416+
companion object {
6417+
@Suppress("LocalVariableName")
6418+
fun setUpMessageHandlers(binaryMessenger: BinaryMessenger, api: PigeonApiWebViewFeature?) {
6419+
val codec = api?.pigeonRegistrar?.codec ?: AndroidWebkitLibraryPigeonCodec()
6420+
run {
6421+
val channel =
6422+
BasicMessageChannel<Any?>(
6423+
binaryMessenger,
6424+
"dev.flutter.pigeon.webview_flutter_android.WebViewFeature.isFeatureSupported",
6425+
codec)
6426+
if (api != null) {
6427+
channel.setMessageHandler { message, reply ->
6428+
val args = message as List<Any?>
6429+
val featureArg = args[0] as String
6430+
val wrapped: List<Any?> =
6431+
try {
6432+
listOf(api.isFeatureSupported(featureArg))
6433+
} catch (exception: Throwable) {
6434+
AndroidWebkitLibraryPigeonUtils.wrapError(exception)
6435+
}
6436+
reply.reply(wrapped)
6437+
}
6438+
} else {
6439+
channel.setMessageHandler(null)
6440+
}
6441+
}
6442+
}
6443+
}
6444+
6445+
@Suppress("LocalVariableName", "FunctionName")
6446+
/** Creates a Dart instance of WebViewFeature and attaches it to [pigeon_instanceArg]. */
6447+
fun pigeon_newInstance(
6448+
pigeon_instanceArg: androidx.webkit.WebViewFeature,
6449+
callback: (Result<Unit>) -> Unit
6450+
) {
6451+
if (pigeonRegistrar.ignoreCallsToDart) {
6452+
callback(
6453+
Result.failure(
6454+
AndroidWebKitError("ignore-calls-error", "Calls to Dart are being ignored.", "")))
6455+
} else if (pigeonRegistrar.instanceManager.containsInstance(pigeon_instanceArg)) {
6456+
callback(Result.success(Unit))
6457+
} else {
6458+
val pigeon_identifierArg =
6459+
pigeonRegistrar.instanceManager.addHostCreatedInstance(pigeon_instanceArg)
6460+
val binaryMessenger = pigeonRegistrar.binaryMessenger
6461+
val codec = pigeonRegistrar.codec
6462+
val channelName =
6463+
"dev.flutter.pigeon.webview_flutter_android.WebViewFeature.pigeon_newInstance"
6464+
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
6465+
channel.send(listOf(pigeon_identifierArg)) {
6466+
if (it is List<*>) {
6467+
if (it.size > 1) {
6468+
callback(
6469+
Result.failure(
6470+
AndroidWebKitError(it[0] as String, it[1] as String, it[2] as String?)))
6471+
} else {
6472+
callback(Result.success(Unit))
6473+
}
6474+
} else {
6475+
callback(
6476+
Result.failure(AndroidWebkitLibraryPigeonUtils.createConnectionError(channelName)))
6477+
}
6478+
}
6479+
}
6480+
}
6481+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ public FlutterAssetManager getFlutterAssetManager() {
252252
return flutterAssetManager;
253253
}
254254

255+
@NonNull
256+
@Override
257+
public PigeonApiWebViewFeature getPigeonApiWebViewFeature() {
258+
return new WebViewFeatureProxyApi(this);
259+
}
260+
255261
@NonNull
256262
@Override
257263
public PigeonApiWebSettingsCompat getPigeonApiWebSettingsCompat() {

packages/webview_flutter/webview_flutter_android/pigeons/android_webkit.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,3 +1104,13 @@ abstract class WebSettingsCompat {
11041104
bool enabled,
11051105
);
11061106
}
1107+
1108+
@ProxyApi(
1109+
kotlinOptions: KotlinProxyApiOptions(
1110+
fullClassName: 'androidx.webkit.WebViewFeature',
1111+
),
1112+
)
1113+
abstract class WebViewFeature {
1114+
@static
1115+
bool isFeatureSupported(String feature);
1116+
}

0 commit comments

Comments
 (0)