also looking at this
feat(android): the reply channel, so async actions become reachable
#152
5 files
+464
-0
| @@ -58,6 +58,39 @@ object CraftNative { | ||
| 58 | 58 | false |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | /** | |
| 62 | * How Zig reaches the page. | |
| 63 | * | |
| 64 | * Zig cannot make a `Runnable`, and `evaluateJavascript` is main-thread | |
| 65 | * only — JNI can only produce a `Runnable` by registering natives on a | |
| 66 | * class that already exists in the APK, which means shipping Kotlin to | |
| 67 | * avoid writing Kotlin. So the hop stays here: Zig owns what to say, this | |
| 68 | * owns which thread says it. | |
| 69 | * | |
| 70 | * Installed by `CraftBridge` once its WebView exists, and `@Volatile` | |
| 71 | * because Zig calls `deliver` from whatever thread a device callback | |
| 72 | * arrives on. | |
| 73 | */ | |
| 74 | @Volatile | |
| 75 | private var deliverer: ((String) -> Unit)? = null | |
| 76 | ||
| 77 | fun setDeliverer(deliver: ((String) -> Unit)?) { | |
| 78 | deliverer = deliver | |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Run `script` in the page. Called from Zig, on any thread. | |
| 83 | * | |
| 84 | * Silent when no deliverer is installed — which is every moment before the | |
| 85 | * WebView exists, and after it is torn down. A throw here would cross back | |
| 86 | * into Zig as a pending exception on a thread that has nowhere to report | |
| 87 | * it, and the caller already counts what it could not deliver. | |
| 88 | */ | |
| 89 | @JvmStatic | |
| 90 | fun deliver(script: String) { | |
| 91 | deliverer?.invoke(script) | |
| 92 | } | |
| 93 | ||
| 61 | 94 | private external fun nativeGetDeviceInfo(activity: Activity): String? |
| 62 | 95 | private external fun nativeGetMemoryUsage(): String? |
| 63 | 96 | private external fun nativeLog(message: String): Boolean |