react-native-minimal-webview 1.2.1 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/build.gradle +1 -1
- package/android/src/main/java/expo/modules/minimalwebview/Asks.kt +5 -11
- package/android/src/main/java/expo/modules/minimalwebview/Hooks.kt +17 -11
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebView.kt +67 -120
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewClients.kt +23 -32
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewModule.kt +8 -18
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewNavigation.kt +30 -49
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewScripts.kt +48 -36
- package/android/src/main/java/expo/modules/minimalwebview/NavigationRules.kt +19 -20
- package/android/src/main/java/expo/modules/minimalwebview/PlatformSettings.kt +30 -40
- package/android/src/main/java/expo/modules/minimalwebview/PullToRefresh.kt +14 -18
- package/android/src/main/java/expo/modules/minimalwebview/requests/Downloads.kt +12 -23
- package/android/src/main/java/expo/modules/minimalwebview/requests/FileUpload.kt +0 -1
- package/android/src/main/java/expo/modules/minimalwebview/requests/FullscreenVideo.kt +1 -4
- package/android/src/main/java/expo/modules/minimalwebview/requests/Permissions.kt +34 -24
- package/build/WebView.d.ts +8 -1
- package/build/WebView.d.ts.map +1 -1
- package/build/WebView.js +30 -80
- package/build/WebView.js.map +1 -1
- package/build/bridge/createBridge.d.ts +1 -5
- package/build/bridge/createBridge.d.ts.map +1 -1
- package/build/bridge/createBridge.js +3 -15
- package/build/bridge/createBridge.js.map +1 -1
- package/build/bridge/endpoint.d.ts +1 -2
- package/build/bridge/endpoint.d.ts.map +1 -1
- package/build/bridge/endpoint.js +5 -6
- package/build/bridge/endpoint.js.map +1 -1
- package/build/bridge/matchesOrigin.d.ts +11 -0
- package/build/bridge/matchesOrigin.d.ts.map +1 -0
- package/build/bridge/matchesOrigin.js +26 -0
- package/build/bridge/matchesOrigin.js.map +1 -0
- package/build/bridge/origins.d.ts +2 -13
- package/build/bridge/origins.d.ts.map +1 -1
- package/build/bridge/origins.js +1 -30
- package/build/bridge/origins.js.map +1 -1
- package/build/bridge/pageClient.generated.d.ts +1 -1
- package/build/bridge/pageClient.generated.d.ts.map +1 -1
- package/build/bridge/pageClient.generated.js +2 -2
- package/build/bridge/pageClient.generated.js.map +1 -1
- package/build/bridge/protocol.d.ts +3 -5
- package/build/bridge/protocol.d.ts.map +1 -1
- package/build/bridge/protocol.js +12 -14
- package/build/bridge/protocol.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.js +1 -1
- package/build/navigationRules.d.ts +0 -5
- package/build/navigationRules.d.ts.map +1 -1
- package/build/navigationRules.js +3 -12
- package/build/navigationRules.js.map +1 -1
- package/build/reactNativeWebViewDefaults.d.ts +0 -4
- package/build/reactNativeWebViewDefaults.d.ts.map +1 -1
- package/build/reactNativeWebViewDefaults.js +0 -4
- package/build/reactNativeWebViewDefaults.js.map +1 -1
- package/build/reactNativeWebViewMessages.d.ts.map +1 -1
- package/build/reactNativeWebViewMessages.js +0 -1
- package/build/reactNativeWebViewMessages.js.map +1 -1
- package/build/refStore.d.ts +0 -2
- package/build/refStore.d.ts.map +1 -1
- package/build/refStore.js +0 -6
- package/build/refStore.js.map +1 -1
- package/build/types.d.ts +33 -16
- package/build/types.d.ts.map +1 -1
- package/ios/Asks.swift +6 -9
- package/ios/DelegateProxy.swift +6 -20
- package/ios/DownloadName.swift +0 -2
- package/ios/Downloads.swift +0 -2
- package/ios/Hooks.swift +6 -7
- package/ios/Loads.swift +37 -19
- package/ios/MinimalWebView+Dialogs.swift +2 -6
- package/ios/MinimalWebView+Navigation.swift +25 -57
- package/ios/MinimalWebView+Scripts.swift +4 -16
- package/ios/MinimalWebView.podspec +1 -2
- package/ios/MinimalWebView.swift +58 -144
- package/ios/MinimalWebViewModule.swift +1 -18
- package/ios/NavigationRules.swift +2 -9
- package/ios/OriginRules.swift +4 -13
- package/ios/PlatformSettings.swift +19 -38
- package/ios/ReadAccess.swift +0 -2
- package/ios/Records.swift +18 -0
- package/ios/Recovery.swift +2 -8
- package/ios/SharedCookies.swift +10 -25
- package/package.json +4 -4
- package/src/WebView.tsx +45 -95
- package/src/bridge/createBridge.ts +5 -20
- package/src/bridge/endpoint.ts +6 -9
- package/src/bridge/matchesOrigin.ts +27 -0
- package/src/bridge/origins.ts +2 -30
- package/src/bridge/pageClient.generated.ts +2 -2
- package/src/bridge/pageClient.ts +1 -7
- package/src/bridge/protocol.ts +16 -18
- package/src/index.ts +1 -1
- package/src/navigationRules.ts +6 -12
- package/src/reactNativeWebViewDefaults.ts +0 -4
- package/src/reactNativeWebViewMessages.ts +0 -1
- package/src/refStore.ts +0 -7
- package/src/types.ts +33 -16
package/android/build.gradle
CHANGED
|
@@ -1,22 +1,16 @@
|
|
|
1
1
|
package expo.modules.minimalwebview
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Every `ask` (a page request, a navigation request or a hook's) waits here, by id, until JS answers it through [answer]
|
|
6
|
-
* or native gives up (null, and the core's `askCancelled` to JS): a Document end, a timeout or the platform. [send] puts an
|
|
7
|
-
* event on the view's `onHookEvent`, which reaches the app's extensions' `onEvent`. Main thread only.
|
|
8
|
-
*/
|
|
3
|
+
/** Main thread only. */
|
|
9
4
|
internal class Asks(private val send: (Map<String, Any?>) -> Unit) {
|
|
10
5
|
private val pending = mutableMapOf<Int, (Any?) -> Unit>()
|
|
11
6
|
private var nextId = 0
|
|
12
|
-
/** The view closed: JS is unmounted, and a hook may still hold its context. */
|
|
13
7
|
private var closed = false
|
|
14
8
|
|
|
15
|
-
/** Sends the ask to JS and returns its id, for [cancel]. Once the view closed it answers null at once. */
|
|
16
|
-
fun ask(key: String, name: String, payload: Map<String, Any?>, onAnswer: (Any?) -> Unit): Int {
|
|
9
|
+
/** Sends the ask to JS and returns its id, for [cancel]. Once the view closed it answers null at once and returns null. */
|
|
10
|
+
fun ask(key: String, name: String, payload: Map<String, Any?>, onAnswer: (Any?) -> Unit): Int? {
|
|
17
11
|
if (closed) {
|
|
18
12
|
onAnswer(null)
|
|
19
|
-
return
|
|
13
|
+
return null
|
|
20
14
|
}
|
|
21
15
|
val id = ++nextId
|
|
22
16
|
pending[id] = onAnswer
|
|
@@ -36,7 +30,7 @@ internal class Asks(private val send: (Map<String, Any?>) -> Unit) {
|
|
|
36
30
|
onAnswer(null)
|
|
37
31
|
}
|
|
38
32
|
|
|
39
|
-
/** Cancels every pending ask. At closing JS isn't told
|
|
33
|
+
/** Cancels every pending ask. At closing JS isn't told. */
|
|
40
34
|
fun documentEnded(reason: DocumentEnd) {
|
|
41
35
|
if (reason == DocumentEnd.CLOSING) closed = true
|
|
42
36
|
pending.keys.toList().forEach { cancel(it, tell = reason != DocumentEnd.CLOSING) }
|
|
@@ -33,8 +33,16 @@ object MinimalWebViewHooks {
|
|
|
33
33
|
val key: String,
|
|
34
34
|
val webViewFactory: ((Context) -> WebView)?,
|
|
35
35
|
val configure: ((WebView, WebViewContext) -> Unit)?,
|
|
36
|
+
val newWebViewsOnly: Boolean,
|
|
36
37
|
val attach: (WebView, WebViewContext) -> Unit
|
|
37
|
-
)
|
|
38
|
+
) {
|
|
39
|
+
val whyNotLate get() = when {
|
|
40
|
+
configure != null -> "configure"
|
|
41
|
+
webViewFactory != null -> "a webViewFactory"
|
|
42
|
+
newWebViewsOnly -> "newWebViewsOnly"
|
|
43
|
+
else -> null
|
|
44
|
+
}
|
|
45
|
+
}
|
|
38
46
|
|
|
39
47
|
internal val hooks = mutableListOf<Hook>()
|
|
40
48
|
|
|
@@ -64,6 +72,8 @@ object MinimalWebViewHooks {
|
|
|
64
72
|
* @param configure runs on the main thread as soon as each WebView this hook is attached to, on or off, is made,
|
|
65
73
|
* before the package sets it up ([WebViewCompat.setProfile][androidx.webkit.WebViewCompat.setProfile] goes here).
|
|
66
74
|
* It gets the same context that [attach] gets next for that WebView, so state it makes can be keyed by the context.
|
|
75
|
+
* @param newWebViewsOnly true attaches the hook only when a WebView is created, never to one whose key turns on
|
|
76
|
+
* later, as a hook with [configure] or [webViewFactory] does.
|
|
67
77
|
* @param attach runs on the main thread once the WebView has its settings and is in the view hierarchy, so what it
|
|
68
78
|
* sets wins over those settings. Register what to run when that WebView goes away with
|
|
69
79
|
* [WebViewContext.onDestroyed]. Document-start scripts you add here run before the package's, so
|
|
@@ -73,13 +83,14 @@ object MinimalWebViewHooks {
|
|
|
73
83
|
key: String,
|
|
74
84
|
webViewFactory: ((Context) -> WebView)? = null,
|
|
75
85
|
configure: ((WebView, WebViewContext) -> Unit)? = null,
|
|
86
|
+
newWebViewsOnly: Boolean = false,
|
|
76
87
|
attach: (WebView, WebViewContext) -> Unit
|
|
77
88
|
) {
|
|
78
89
|
if (key == "core") {
|
|
79
90
|
Log.e(TAG, "the hook key `core` is reserved: the hook is not registered")
|
|
80
91
|
return
|
|
81
92
|
}
|
|
82
|
-
if (putHook(hooks, Hook(key, webViewFactory, configure, attach))) {
|
|
93
|
+
if (putHook(hooks, Hook(key, webViewFactory, configure, newWebViewsOnly, attach))) {
|
|
83
94
|
Log.w(
|
|
84
95
|
TAG,
|
|
85
96
|
"a hook under `$key` replaces the one registered before: a JS reload registers every hook again; " +
|
|
@@ -228,8 +239,8 @@ interface WebViewContext {
|
|
|
228
239
|
fun emit(name: String, payload: Map<String, Any?>)
|
|
229
240
|
|
|
230
241
|
/**
|
|
231
|
-
* Sends the event as [emit] does,
|
|
232
|
-
* promise awaited, in JSON types as [handle]'s arguments are.
|
|
242
|
+
* Sends the event as [emit] does, but to the extension's `onAsk` when it has one, and calls [onAnswer] with JS's
|
|
243
|
+
* answer: that handler's return value, a promise awaited, in JSON types as [handle]'s arguments are.
|
|
233
244
|
*
|
|
234
245
|
* It answers null when the handler throws or there is none, or when the Document ends first; there is no timeout. It
|
|
235
246
|
* answers null at once while the hook is off, and after the context's attach ended. An ask made before the WebView's
|
|
@@ -310,9 +321,8 @@ class HookReply internal constructor(
|
|
|
310
321
|
}
|
|
311
322
|
|
|
312
323
|
/**
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
* are volatile; the rest is main thread only.
|
|
324
|
+
* Chromium's IO thread reads [routed] and [clients] (shouldInterceptRequest), so [options] and the detach are volatile;
|
|
325
|
+
* the rest is main thread only.
|
|
316
326
|
*/
|
|
317
327
|
internal open class Attachment(value: Any?) {
|
|
318
328
|
@Volatile var options: Any? = value
|
|
@@ -325,7 +335,6 @@ internal open class Attachment(value: Any?) {
|
|
|
325
335
|
// own changes write them.
|
|
326
336
|
val clients: MutableList<WebViewClient> = CopyOnWriteArrayList()
|
|
327
337
|
val chromeClients = mutableListOf<WebChromeClient>()
|
|
328
|
-
// Copy-on-write too: the core iterates them live, and a listener may remove itself while it runs.
|
|
329
338
|
val touchListeners: MutableList<View.OnTouchListener> = CopyOnWriteArrayList()
|
|
330
339
|
val scrollListeners: MutableList<View.OnScrollChangeListener> = CopyOnWriteArrayList()
|
|
331
340
|
|
|
@@ -374,10 +383,8 @@ internal open class Attachment(value: Any?) {
|
|
|
374
383
|
internal fun <T> routed(attachments: List<Attachment>, pick: (Attachment) -> List<T>) =
|
|
375
384
|
attachments.filter { it.routed }.flatMap(pick)
|
|
376
385
|
|
|
377
|
-
/** The core's [WebViewContext] for one hook's attach: the hook's key goes on its events and asks. */
|
|
378
386
|
internal class HookContext(val hook: MinimalWebViewHooks.Hook, view: MinimalWebView) :
|
|
379
387
|
Attachment(view.hookOptions[hook.key]), WebViewContext {
|
|
380
|
-
/** Null once the attach ended. Main thread only. */
|
|
381
388
|
private var view: MinimalWebView? = view
|
|
382
389
|
val key get() = hook.key
|
|
383
390
|
override val url get() = view?.url.orEmpty()
|
|
@@ -385,7 +392,6 @@ internal class HookContext(val hook: MinimalWebViewHooks.Hook, view: MinimalWebV
|
|
|
385
392
|
private val destroyedHandlers = mutableListOf<(DocumentEnd) -> Unit>()
|
|
386
393
|
val handlers = mutableMapOf<String, (List<Any?>, HookReply) -> Unit>()
|
|
387
394
|
|
|
388
|
-
/** The attach ended: also drops the view and what the hook gave it, bar the [onDestroyed] handlers [destroyed] runs next. */
|
|
389
395
|
override fun detach() {
|
|
390
396
|
super.detach()
|
|
391
397
|
view = null
|
|
@@ -42,17 +42,14 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
42
42
|
private val navigation = MinimalWebViewNavigation(this) { onNavigation(it) }
|
|
43
43
|
internal val scripts = MinimalWebViewScripts(this) { onBridge(it) }
|
|
44
44
|
|
|
45
|
-
/** Set by the first prop batch and replaced after a renderer crash; see [recovering]. */
|
|
46
45
|
internal lateinit var webView: WebView
|
|
47
46
|
private set
|
|
48
47
|
private val hasWebView get() = ::webView.isInitialized
|
|
49
48
|
|
|
50
|
-
// Props, applied together in applyProps()
|
|
51
49
|
internal var source: WebViewSource? = null
|
|
52
50
|
internal var navigationRules: List<Map<String, Any?>>? = null
|
|
53
51
|
internal var bridgeScript: String? = null
|
|
54
52
|
internal var bridgeOrigins: List<String>? = null
|
|
55
|
-
/** The `hookOptions` prop, made from the `extensions` prop's options: each hook's value by key. A change reaches the attached hooks (onOptions). */
|
|
56
53
|
internal var hookOptions: Map<String, Any?> = emptyMap()
|
|
57
54
|
set(value) {
|
|
58
55
|
val old = field
|
|
@@ -60,7 +57,6 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
60
57
|
if (hasWebView) optionsChanged(old)
|
|
61
58
|
}
|
|
62
59
|
internal var injectedScripts: List<InjectedScript>? = null
|
|
63
|
-
/** The `android` prop: WebSettings and WebView setters by name, applied by [PlatformSettings]. */
|
|
64
60
|
internal var settings: Map<String, Any?>? = null
|
|
65
61
|
internal val platformSettings = PlatformSettings({ hasNavigationListener }) { PlatformSettings.targets(webView, it) }
|
|
66
62
|
private val refresher = PullToRefresh(this)
|
|
@@ -69,63 +65,49 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
69
65
|
field = value
|
|
70
66
|
refresher.layout.isEnabled = value
|
|
71
67
|
}
|
|
72
|
-
/**
|
|
73
|
-
* The page requests JS has a prop for, by prop name (`onFileRequest`, `onDownload`, ...), and `onNavigation`, read when a
|
|
74
|
-
* request comes or a decision is reported.
|
|
75
|
-
*/
|
|
68
|
+
/** The page requests JS has a prop for, by prop name (`onFileRequest`, `onDownload`, ...), and `onNavigation`. */
|
|
76
69
|
internal var requestHandlers: Set<String> = emptySet()
|
|
77
70
|
private var loadedSource: WebViewSource? = null
|
|
78
71
|
|
|
79
|
-
// The hooks' lists: each context owns its own, and the core routes those of the hooks that are on, in attach order.
|
|
80
72
|
val clients: List<WebViewClient> get() = routed(hookContexts) { it.clients }
|
|
81
73
|
val chromeClients: List<WebChromeClient> get() = routed(hookContexts) { it.chromeClients }
|
|
82
74
|
internal val fileUpload = FileUpload(this)
|
|
83
75
|
internal val fullscreen = FullscreenVideo(this)
|
|
84
76
|
internal val permissions = Permissions(this)
|
|
85
77
|
private val downloads = Downloads(this)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
/** The hooks' contexts for the current WebView, in attach order. Chromium's IO thread reads it (shouldInterceptRequest). */
|
|
78
|
+
private var keysToAttach: Set<String> = emptySet()
|
|
79
|
+
/** Chromium's IO thread reads it (shouldInterceptRequest). */
|
|
89
80
|
@Volatile private var hookContexts: List<HookContext> = emptyList()
|
|
90
81
|
|
|
91
82
|
internal val asks = Asks { onHookEvent(it) }
|
|
92
83
|
internal val deciders = Deciders { message -> mainHandler.post { warn(message) } }
|
|
93
84
|
|
|
94
|
-
// Navigation state: native owns it and tells JS. The clients (MinimalWebViewClients.kt) write it.
|
|
95
85
|
internal var url = ""
|
|
96
86
|
internal var pendingUrl: String? = null
|
|
97
87
|
internal var title = ""
|
|
98
88
|
internal var loading = false
|
|
99
89
|
internal var progress = 0.0
|
|
100
|
-
|
|
101
|
-
internal var
|
|
90
|
+
private var outcome: LoadOutcome? = null
|
|
91
|
+
internal var rendererGone = false
|
|
102
92
|
private var lastEmitted: Map<String, Any?>? = null
|
|
103
93
|
private var emitScheduled = false
|
|
104
94
|
|
|
105
|
-
/**
|
|
106
|
-
* From a renderer death until [recover] builds the new WebView: whether it loads, the dead one's history, and whether
|
|
107
|
-
* that history can come back on its own (the page on screen isn't the source).
|
|
108
|
-
*/
|
|
109
95
|
private var pendingRecovery: Recovery? = null
|
|
110
96
|
private class Recovery(val reload: Boolean, val history: Bundle?, val restorable: Boolean)
|
|
111
|
-
/** The ref's `load` while a recovery waits: [recover] loads it instead of the dead WebView's page. */
|
|
112
97
|
private var pendingLoad: String? = null
|
|
113
98
|
private var destroyed = false
|
|
114
99
|
|
|
115
100
|
init {
|
|
116
|
-
// The WebView goes into the layout in setUp(); the prop only turns the pull on and off.
|
|
117
101
|
addView(refresher.layout, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
|
|
118
102
|
}
|
|
119
103
|
|
|
120
|
-
private fun createWebView(): WebView {
|
|
121
|
-
|
|
122
|
-
val
|
|
123
|
-
if (first) hookKeys = hookOptions.keys
|
|
124
|
-
val (attaching, unregistered) = selectHooks(MinimalWebViewHooks.hooks, hookKeys)
|
|
104
|
+
private fun createWebView(first: Boolean): WebView {
|
|
105
|
+
if (first) keysToAttach = hookOptions.keys
|
|
106
|
+
val (attaching, unregistered) = selectHooks(MinimalWebViewHooks.hooks, keysToAttach)
|
|
125
107
|
val (maker, unused) = pickFactory(attaching)
|
|
126
108
|
val view = maker?.webViewFactory?.invoke(context) ?: WebView(context)
|
|
109
|
+
rendererGone = false
|
|
127
110
|
hookContexts = attaching.map { HookContext(it, this) }
|
|
128
|
-
// A typo, or a hook's module not linked or not rebuilt. Once: the WebView after a crash has the same keys.
|
|
129
111
|
if (first) unregistered.forEach { warn("extension $it: no hook is registered under this key") }
|
|
130
112
|
if (first) unused.forEach { warn("extension $it: its webViewFactory is not used, ${maker?.key}'s is") }
|
|
131
113
|
// Before the package touches the WebView: some calls (WebViewCompat.setProfile) must come first.
|
|
@@ -143,14 +125,12 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
143
125
|
}
|
|
144
126
|
if (!taken && !downloads.onDownloadStart(download)) navigation.openDownload(url)
|
|
145
127
|
}
|
|
146
|
-
// View's slots hold one listener each: the hooks share them through their lists.
|
|
147
128
|
view.setOnTouchListener(::touched)
|
|
148
129
|
view.setOnScrollChangeListener(::scrolled)
|
|
149
130
|
if (hasNavigationListener) WebViewCompat.addNavigationListener(view, context.mainExecutor, Listener(this))
|
|
150
131
|
return view
|
|
151
132
|
}
|
|
152
133
|
|
|
153
|
-
// The on hooks' own lists, not copies: this runs for every touch event and scroll change.
|
|
154
134
|
private fun touched(view: View, event: MotionEvent) =
|
|
155
135
|
hookContexts.any { c -> c.routed && c.touchListeners.any { it.onTouch(view, event) } }
|
|
156
136
|
|
|
@@ -163,38 +143,30 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
163
143
|
@SuppressLint("SetJavaScriptEnabled") // because a browser-tab WebView needs JS; no addJavascriptInterface is used
|
|
164
144
|
private fun configure(settings: WebSettings) {
|
|
165
145
|
settings.javaScriptEnabled = true
|
|
166
|
-
// A tap's window.open and target=_blank reach onCreateWindow; the navigation rules decide (newWindow: true).
|
|
167
|
-
// Without a gesture only when the app opts in (javaScriptCanOpenWindowsAutomatically, in applySettings).
|
|
168
146
|
settings.setSupportMultipleWindows(true)
|
|
169
147
|
settings.domStorageEnabled = true
|
|
170
148
|
settings.allowFileAccess = false
|
|
171
149
|
settings.allowContentAccess = false
|
|
172
150
|
settings.mixedContentMode = WebSettings.MIXED_CONTENT_NEVER_ALLOW
|
|
173
|
-
// Pinch-zoom (an accessibility basic; pages opt out with a viewport meta), without the legacy +/- buttons.
|
|
174
151
|
settings.builtInZoomControls = true
|
|
175
152
|
settings.displayZoomControls = false
|
|
176
|
-
// Back and forward restore the page instead of reloading it, as WebKit does. `android.settings` applies after this.
|
|
177
153
|
// Not without NavigationListener: WebView 133 sends onPageStarted for a restored page but no onPageFinished.
|
|
178
154
|
if (hasNavigationListener && WebViewFeature.isFeatureSupported(WebViewFeature.BACK_FORWARD_CACHE)) {
|
|
179
155
|
WebSettingsCompat.setBackForwardCacheEnabled(settings, true)
|
|
180
156
|
}
|
|
181
|
-
// No favicon requests: nothing shows them.
|
|
182
157
|
if (WebViewFeature.isFeatureSupported(WebViewFeature.DOWNLOAD_FAVICONS_ENABLED)) {
|
|
183
158
|
WebSettingsCompat.setDownloadFaviconsEnabled(settings, false)
|
|
184
159
|
}
|
|
185
160
|
}
|
|
186
161
|
|
|
187
|
-
/** Called once per prop batch. `source` loads last so everything else is ready first. */
|
|
188
162
|
internal fun applyProps() {
|
|
189
|
-
// The dead WebView after a crash takes nothing: recover() applies the props to the new one.
|
|
190
163
|
if (recovering) return
|
|
191
|
-
// The first prop batch sets up the first WebView: the hooks get their options with it.
|
|
192
164
|
if (hasWebView) {
|
|
193
165
|
applySettings()
|
|
194
166
|
scripts.update()
|
|
195
167
|
} else {
|
|
196
168
|
webView = try {
|
|
197
|
-
createWebView()
|
|
169
|
+
createWebView(first = true)
|
|
198
170
|
} catch (e: AndroidRuntimeException) { // no WebView provider: missing, disabled or updating
|
|
199
171
|
return reportError("network", source?.uri.orEmpty(), WebViewClient.ERROR_UNKNOWN, "Android System WebView is unavailable: ${e.message}")
|
|
200
172
|
}
|
|
@@ -203,16 +175,13 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
203
175
|
loadNewSource()
|
|
204
176
|
}
|
|
205
177
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
if (next == loadedSource) return false
|
|
178
|
+
private fun loadNewSource() {
|
|
179
|
+
val next = source ?: return
|
|
180
|
+
if (next == loadedSource) return
|
|
210
181
|
loadedSource = next
|
|
211
182
|
load(next)
|
|
212
|
-
return true
|
|
213
183
|
}
|
|
214
184
|
|
|
215
|
-
/** Readies a new WebView, the first or the one after a crash, in one order: settings, layout, hooks, scripts. */
|
|
216
185
|
private fun setUp() {
|
|
217
186
|
applySettings(force = true)
|
|
218
187
|
// addView(view) alone would give the view WRAP_CONTENT, and Chromium then resolves CSS vh to 0.
|
|
@@ -227,7 +196,7 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
227
196
|
if (source.uri == null) {
|
|
228
197
|
webView.loadDataWithBaseURL(source.baseUrl, source.html ?: "", "text/html", "utf-8", null)
|
|
229
198
|
} else if (source.body != null) {
|
|
230
|
-
webView.postUrl(source.uri, source.body.toByteArray())
|
|
199
|
+
webView.postUrl(source.uri, source.body.toByteArray())
|
|
231
200
|
} else {
|
|
232
201
|
webView.loadUrl(source.uri, source.headers.orEmpty())
|
|
233
202
|
}
|
|
@@ -239,15 +208,11 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
239
208
|
if (recovering) pendingLoad = url else webView.loadUrl(url)
|
|
240
209
|
}
|
|
241
210
|
|
|
242
|
-
/** Loads the page on screen again: the `source` when that is the page (its headers, html or POST), else its URL. */
|
|
243
211
|
private fun loadAgain() {
|
|
244
212
|
if (showsSource(source, url)) source?.let(::load) else if (url.isNotEmpty()) webView.loadUrl(url)
|
|
245
213
|
}
|
|
246
214
|
|
|
247
|
-
/**
|
|
248
|
-
* The ref's reload: the app's retry starts the crash-loop guard over. Only this and [load] clear it: the guard counts
|
|
249
|
-
* the process's renderer deaths, so another view's new source or pull-to-refresh mustn't end a crash loop's count.
|
|
250
|
-
*/
|
|
215
|
+
/** The ref's reload: the app's retry starts the crash-loop guard over. */
|
|
251
216
|
internal fun retry() {
|
|
252
217
|
crashTimes.clear()
|
|
253
218
|
reload()
|
|
@@ -255,12 +220,9 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
255
220
|
|
|
256
221
|
internal fun reload() {
|
|
257
222
|
when {
|
|
258
|
-
|
|
259
|
-
recovering -> recover(reload = true)
|
|
260
|
-
// The first WebView couldn't be made (onError said so): try again.
|
|
223
|
+
recovering -> recover(forceLoad = true)
|
|
261
224
|
!hasWebView -> applyProps()
|
|
262
|
-
//
|
|
263
|
-
// WebView's reload declines its form resubmission.
|
|
225
|
+
// A POST source: WebView's reload declines its form resubmission.
|
|
264
226
|
webView.url == null || showingPost -> loadAgain()
|
|
265
227
|
else -> webView.reload()
|
|
266
228
|
}
|
|
@@ -268,18 +230,12 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
268
230
|
|
|
269
231
|
private val showingPost get() = source?.body != null && showsSource(source, url)
|
|
270
232
|
|
|
271
|
-
/**
|
|
272
|
-
* The `android` settings, applied from each prop batch (only the keys that changed) and all again to a new WebView
|
|
273
|
-
* after a crash. A key that can't be applied is reported to JS, which warns.
|
|
274
|
-
*/
|
|
275
233
|
private fun applySettings(force: Boolean = false) {
|
|
276
|
-
platformSettings.apply(settings, force).forEach { warn("android.${it
|
|
234
|
+
platformSettings.apply(settings, force).forEach { warn("android.${it.key}: ${it.reason}") }
|
|
277
235
|
}
|
|
278
236
|
|
|
279
|
-
/** A warning for the app's developer, which JS logs with console.warn. */
|
|
280
237
|
internal fun warn(message: String) = emit("core", "warning", mapOf("message" to message))
|
|
281
238
|
|
|
282
|
-
/** Tells each attached hook whose value changed, and attaches a hook without `configure` or `webViewFactory` whose key turns on. */
|
|
283
239
|
private fun optionsChanged(old: Map<String, Any?>) {
|
|
284
240
|
for (key in inRegistrationOrder(old.keys + hookOptions.keys, MinimalWebViewHooks.hooks.map { it.key })) {
|
|
285
241
|
val value = hookOptions[key]
|
|
@@ -287,7 +243,7 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
287
243
|
val context = hookContexts.find { it.key == key }
|
|
288
244
|
if (context != null) {
|
|
289
245
|
context.optionsChanged(value)
|
|
290
|
-
} else if (key !in
|
|
246
|
+
} else if (key !in keysToAttach && old[key] == null) {
|
|
291
247
|
attachLate(key)
|
|
292
248
|
}
|
|
293
249
|
}
|
|
@@ -296,13 +252,8 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
296
252
|
private fun attachLate(key: String) {
|
|
297
253
|
val hook = MinimalWebViewHooks.hooks.find { it.key == key }
|
|
298
254
|
?: return warn("extension $key: no hook is registered under this key")
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
val why = if (hook.configure != null) "configure" else "a webViewFactory"
|
|
302
|
-
return warn("extension $key: its hook has $why, so it attaches only when the WebView is created")
|
|
303
|
-
}
|
|
304
|
-
hookKeys = hookKeys + key
|
|
305
|
-
// The WebView the recovery builds attaches it with the others.
|
|
255
|
+
hook.whyNotLate?.let { return warn("extension $key: its hook has $it, so it attaches only when the WebView is created") }
|
|
256
|
+
keysToAttach = keysToAttach + key
|
|
306
257
|
if (recovering) return
|
|
307
258
|
val context = HookContext(hook, this)
|
|
308
259
|
hookContexts = hookContexts + context
|
|
@@ -315,21 +266,18 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
315
266
|
fun emit(key: String, name: String, payload: Map<String, Any?>) =
|
|
316
267
|
onHookEvent(mapOf("key" to key, "name" to name, "payload" to payload))
|
|
317
268
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
asks.documentEnded(reason)
|
|
331
|
-
hookContexts.forEach { it.documentEnded(reason) }
|
|
332
|
-
}
|
|
269
|
+
internal val documents = Documents(
|
|
270
|
+
core = { reason ->
|
|
271
|
+
fileUpload.cancel()
|
|
272
|
+
navigation.documentEnded()
|
|
273
|
+
scripts.documentEnded(reason)
|
|
274
|
+
fullscreen.documentEnded(reason)
|
|
275
|
+
},
|
|
276
|
+
asksAndHooks = { reason ->
|
|
277
|
+
asks.documentEnded(reason)
|
|
278
|
+
hookContexts.forEach { it.documentEnded(reason) }
|
|
279
|
+
}
|
|
280
|
+
)
|
|
333
281
|
|
|
334
282
|
internal fun loadStarted() {
|
|
335
283
|
loading = true
|
|
@@ -337,7 +285,10 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
337
285
|
stateChanged()
|
|
338
286
|
}
|
|
339
287
|
|
|
340
|
-
|
|
288
|
+
internal fun settleOutcome(value: LoadOutcome) {
|
|
289
|
+
outcome = outcome ?: value
|
|
290
|
+
}
|
|
291
|
+
|
|
341
292
|
internal fun loadEnded() {
|
|
342
293
|
loading = false
|
|
343
294
|
refresher.pageFinished()
|
|
@@ -347,10 +298,6 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
347
298
|
internal fun evaluate(script: String, onResult: (Result<String>) -> Unit) =
|
|
348
299
|
if (recovering) onResult(Result.failure(EvaluateError("unavailable", RECOVERING))) else scripts.evaluate(script, onResult)
|
|
349
300
|
|
|
350
|
-
/**
|
|
351
|
-
* The renderer died. The WebView can't be reused: it goes now, after saving its history. The new one is built
|
|
352
|
-
* at once if the view can be seen, else once it can (Android's guidance: not in the background).
|
|
353
|
-
*/
|
|
354
301
|
internal fun processGone(cause: String, at: Long) {
|
|
355
302
|
if (destroyed) return
|
|
356
303
|
val reload = crashAllowsReload(crashTimes, at) && !showingPost
|
|
@@ -359,19 +306,17 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
359
306
|
val history = Bundle().takeIf { reload && webView.saveState(it) != null }
|
|
360
307
|
tearDown(DocumentEnd.CRASHED)
|
|
361
308
|
pendingRecovery = Recovery(reload, history, restorable)
|
|
362
|
-
|
|
363
|
-
if (loading) outcome = "failed"
|
|
309
|
+
if (loading) outcome = LoadOutcome.FAILED
|
|
364
310
|
loading = false
|
|
365
311
|
pendingUrl = null
|
|
366
312
|
progress = 0.0
|
|
367
313
|
refresher.pageFinished()
|
|
368
314
|
onProcessGone(mapOf("cause" to cause, "recovered" to reload))
|
|
369
|
-
// A detached view's window reports GONE
|
|
315
|
+
// A detached view's window reports GONE.
|
|
370
316
|
if (windowVisibility == View.VISIBLE) recover()
|
|
371
|
-
stateChanged()
|
|
317
|
+
stateChanged()
|
|
372
318
|
}
|
|
373
319
|
|
|
374
|
-
/** The renderer died and the new WebView isn't built yet: the one in [webView] is destroyed. */
|
|
375
320
|
internal val recovering get() = pendingRecovery != null
|
|
376
321
|
|
|
377
322
|
// Also on attach: dispatchAttachedToWindow calls this unless the window is GONE.
|
|
@@ -380,23 +325,18 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
380
325
|
if (visibility == View.VISIBLE) recover()
|
|
381
326
|
}
|
|
382
327
|
|
|
383
|
-
|
|
384
|
-
* Builds the WebView that replaces the dead one and brings its history back, or else loads its page again. With
|
|
385
|
-
* [reload] (the app's retry) it loads also when the crash-loop guard or a POST source held the reload back.
|
|
386
|
-
*/
|
|
387
|
-
private fun recover(reload: Boolean = false) {
|
|
328
|
+
private fun recover(forceLoad: Boolean = false) {
|
|
388
329
|
val recovery = pendingRecovery ?: return
|
|
389
330
|
pendingRecovery = null
|
|
390
|
-
webView = createWebView()
|
|
331
|
+
webView = createWebView(first = false)
|
|
391
332
|
setUp()
|
|
392
|
-
// The history first: a source the app set or a `load` it asked for while this waited loads on top of it, as on iOS.
|
|
393
333
|
val replaced = pendingLoad != null || source != null && source != loadedSource
|
|
394
334
|
val restored = (replaced || recovery.restorable) && recovery.history?.let { webView.restoreState(it) } != null
|
|
395
335
|
if (replaced) {
|
|
396
336
|
loadNewSource()
|
|
397
337
|
pendingLoad?.let(webView::loadUrl)
|
|
398
338
|
pendingLoad = null
|
|
399
|
-
} else if (!restored && (recovery.reload ||
|
|
339
|
+
} else if (!restored && (recovery.reload || forceLoad)) {
|
|
400
340
|
loadAgain()
|
|
401
341
|
}
|
|
402
342
|
stateChanged()
|
|
@@ -405,18 +345,16 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
405
345
|
internal fun destroy() {
|
|
406
346
|
destroyed = true
|
|
407
347
|
downloads.detach()
|
|
408
|
-
// A crash tore the WebView down already, and detached the hooks; with none, there is nothing to tear down.
|
|
409
348
|
if (!hasWebView || recovering) {
|
|
410
349
|
pendingRecovery = null
|
|
411
|
-
return
|
|
350
|
+
return documents.end(DocumentEnd.CLOSING)
|
|
412
351
|
}
|
|
413
352
|
webView.stopLoading()
|
|
414
353
|
tearDown(DocumentEnd.CLOSING)
|
|
415
354
|
}
|
|
416
355
|
|
|
417
|
-
/** Ends the Document with [reason] and destroys the WebView. The detached hooks' onDestroyed handlers run after it is destroyed. */
|
|
418
356
|
private fun tearDown(reason: DocumentEnd) {
|
|
419
|
-
|
|
357
|
+
documents.end(reason)
|
|
420
358
|
val detached = hookContexts
|
|
421
359
|
detached.forEach { it.detach() }
|
|
422
360
|
hookContexts = emptyList()
|
|
@@ -426,18 +364,15 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
426
364
|
}
|
|
427
365
|
|
|
428
366
|
internal fun reportError(kind: String, url: String, code: Int, description: String) {
|
|
429
|
-
|
|
430
|
-
if (loading) outcome = "failed"
|
|
367
|
+
if (loading) outcome = LoadOutcome.FAILED
|
|
431
368
|
onError(mapOf("kind" to kind, "url" to url, "code" to code, "description" to description))
|
|
432
369
|
}
|
|
433
370
|
|
|
434
371
|
internal fun reportHttpError(url: String, statusCode: Int) =
|
|
435
372
|
onError(mapOf("kind" to "http", "url" to url, "statusCode" to statusCode))
|
|
436
373
|
|
|
437
|
-
// Changes within one frame are combined into one event, except `loading` flips: a load that starts and fails
|
|
438
|
-
// within one frame must still reach JS as loading true -> false (onLoadStart / onLoadEnd).
|
|
439
374
|
internal fun stateChanged() {
|
|
440
|
-
if (
|
|
375
|
+
if (loadingFlipped) return emitState()
|
|
441
376
|
if (emitScheduled) return
|
|
442
377
|
emitScheduled = true
|
|
443
378
|
mainHandler.postDelayed({
|
|
@@ -446,9 +381,10 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
446
381
|
}, FRAME_MS)
|
|
447
382
|
}
|
|
448
383
|
|
|
384
|
+
private val loadingFlipped get() = loading != lastEmitted?.get("loading")
|
|
385
|
+
|
|
449
386
|
private fun emitState() {
|
|
450
387
|
if (destroyed) return
|
|
451
|
-
// While a recovery waits, `webView` is the destroyed one: the history stays as JS saw it last.
|
|
452
388
|
val (back, forward) = if (recovering) {
|
|
453
389
|
(lastEmitted?.get("canGoBack") ?: false) to (lastEmitted?.get("canGoForward") ?: false)
|
|
454
390
|
} else {
|
|
@@ -462,7 +398,7 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
462
398
|
"progress" to progress,
|
|
463
399
|
"canGoBack" to back,
|
|
464
400
|
"canGoForward" to forward,
|
|
465
|
-
"outcome" to if (loading) null else outcome
|
|
401
|
+
"outcome" to if (loading) null else outcome?.jsName
|
|
466
402
|
)
|
|
467
403
|
if (state == lastEmitted) return
|
|
468
404
|
lastEmitted = state
|
|
@@ -477,14 +413,15 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
477
413
|
|
|
478
414
|
internal const val TAG = "MinimalWebView"
|
|
479
415
|
|
|
416
|
+
internal enum class LoadOutcome(val jsName: String) { FINISHED("finished"), FAILED("failed"), CANCELLED("cancelled") }
|
|
417
|
+
|
|
480
418
|
/**
|
|
481
|
-
*
|
|
482
|
-
*
|
|
419
|
+
* Not View.post: a detached view (unmounted, or a covered screen) holds that until it is attached again. Lazy: the JVM
|
|
420
|
+
* tests load this file, and Handler's constructor throws there.
|
|
483
421
|
*/
|
|
484
422
|
internal val mainHandler by lazy { Handler(Looper.getMainLooper()) }
|
|
485
423
|
internal const val RECOVERING = "the WebView is being rebuilt after its renderer died"
|
|
486
424
|
|
|
487
|
-
/** Whether this process has made a WebView. */
|
|
488
425
|
@Volatile internal var webViewCreated = false
|
|
489
426
|
|
|
490
427
|
/** What `setWebContentsDebuggingEnabled` set; null until it is called. */
|
|
@@ -525,12 +462,23 @@ private fun canonicalUrl(url: String): String {
|
|
|
525
462
|
return "$lower://$userinfo${host.lowercase()}$portPart${if (rest.startsWith('/')) rest else "/$rest"}"
|
|
526
463
|
}
|
|
527
464
|
|
|
528
|
-
// scheme "://" [userinfo "@"] host [":" port] rest
|
|
529
465
|
private val urlAuthority = Regex("^([a-zA-Z][a-zA-Z0-9+.-]*)://([^/?#@]*@)?(\\[[^\\]/?#]*]|[^/?#:]*)(?::(\\d*))?(.*)$")
|
|
530
466
|
|
|
531
|
-
/** The special schemes' default ports (the WHATWG URL Standard's)
|
|
467
|
+
/** The special schemes' default ports (the WHATWG URL Standard's). */
|
|
532
468
|
internal val defaultPorts = mapOf("http" to 80, "https" to 443, "ws" to 80, "wss" to 443, "ftp" to 21)
|
|
533
469
|
|
|
470
|
+
/** The first commit after mount or a crash opens a Document and ends none: only [core] hears it. */
|
|
471
|
+
internal class Documents(private val core: (DocumentEnd) -> Unit, private val asksAndHooks: (DocumentEnd) -> Unit) {
|
|
472
|
+
private var open = false
|
|
473
|
+
|
|
474
|
+
fun end(reason: DocumentEnd) {
|
|
475
|
+
val opening = reason == DocumentEnd.COMMITTED && !open
|
|
476
|
+
open = reason == DocumentEnd.COMMITTED
|
|
477
|
+
core(reason)
|
|
478
|
+
if (!opening) asksAndHooks(reason)
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
534
482
|
/**
|
|
535
483
|
* Records a renderer death at [now] in [times] and says whether to reload after it: not after more than
|
|
536
484
|
* [MAX_CRASHES_PER_WINDOW] in [CRASH_WINDOW_MS]. Every WebView on the renderer hears of its death, so one within
|
|
@@ -542,7 +490,6 @@ internal fun crashAllowsReload(times: ArrayDeque<Long>, now: Long): Boolean {
|
|
|
542
490
|
return times.size <= MAX_CRASHES_PER_WINDOW
|
|
543
491
|
}
|
|
544
492
|
|
|
545
|
-
// shortcut: a fixed crash-loop guard, make it a prop if an app needs a different policy
|
|
546
493
|
private const val MAX_CRASHES_PER_WINDOW = 3
|
|
547
494
|
private const val CRASH_WINDOW_MS = 60_000L
|
|
548
495
|
private const val SAME_CRASH_MS = 100L
|