react-native-minimal-webview 1.1.0 → 1.2.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/README.md +35 -31
- package/android/src/main/java/expo/modules/minimalwebview/Hooks.kt +1 -6
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebView.kt +56 -31
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewClients.kt +3 -2
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewModule.kt +16 -4
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewNavigation.kt +3 -3
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewScripts.kt +5 -7
- package/android/src/main/java/expo/modules/minimalwebview/PlatformSettings.kt +47 -36
- package/android/src/main/java/expo/modules/minimalwebview/PullToRefresh.kt +2 -2
- package/android/src/main/java/expo/modules/minimalwebview/requests/Downloads.kt +7 -8
- package/android/src/main/java/expo/modules/minimalwebview/requests/FullscreenVideo.kt +2 -2
- package/android/src/main/java/expo/modules/minimalwebview/requests/Permissions.kt +4 -5
- package/build/WebView.d.ts +4 -10
- package/build/WebView.d.ts.map +1 -1
- package/build/WebView.js +60 -103
- package/build/WebView.js.map +1 -1
- package/build/bridge/createBridge.d.ts +17 -11
- package/build/bridge/createBridge.d.ts.map +1 -1
- package/build/bridge/createBridge.js +32 -44
- package/build/bridge/createBridge.js.map +1 -1
- package/build/bridge/endpoint.d.ts +1 -3
- package/build/bridge/endpoint.d.ts.map +1 -1
- package/build/bridge/endpoint.js +2 -2
- package/build/bridge/endpoint.js.map +1 -1
- package/build/bridge/origins.d.ts +5 -2
- package/build/bridge/origins.d.ts.map +1 -1
- package/build/bridge/origins.js +15 -4
- 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 +1 -1
- package/build/bridge/pageClient.generated.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -1
- package/build/index.js.map +1 -1
- package/build/{defaultNavigationRules.d.ts → navigationRules.d.ts} +10 -1
- package/build/navigationRules.d.ts.map +1 -0
- package/build/navigationRules.js +93 -0
- package/build/navigationRules.js.map +1 -0
- package/build/refStore.d.ts.map +1 -1
- package/build/refStore.js +13 -17
- package/build/refStore.js.map +1 -1
- package/build/types.d.ts +66 -40
- package/build/types.d.ts.map +1 -1
- package/ios/DelegateProxy.swift +35 -30
- package/ios/Loads.swift +4 -4
- package/ios/MRWInvoke.h +8 -0
- package/ios/MRWInvoke.m +39 -24
- package/ios/MinimalWebView+Dialogs.swift +4 -2
- package/ios/MinimalWebView+Navigation.swift +23 -13
- package/ios/MinimalWebView+Scripts.swift +6 -1
- package/ios/MinimalWebView.swift +78 -29
- package/ios/MinimalWebViewModule.swift +1 -0
- package/ios/NavigationRules.swift +15 -15
- package/ios/PlatformSettings.swift +2 -13
- package/ios/SharedCookies.swift +1 -2
- package/mocks/MinimalWebView.ts +5 -0
- package/package.json +9 -8
- package/src/WebView.tsx +74 -117
- package/src/bridge/createBridge.ts +45 -53
- package/src/bridge/endpoint.ts +3 -5
- package/src/bridge/origins.ts +18 -6
- package/src/bridge/pageClient.generated.ts +1 -1
- package/src/index.ts +1 -1
- package/src/navigationRules.ts +111 -0
- package/src/refStore.ts +15 -19
- package/src/types.ts +66 -40
- package/build/defaultNavigationRules.d.ts.map +0 -1
- package/build/defaultNavigationRules.js +0 -22
- package/build/defaultNavigationRules.js.map +0 -1
- package/ios/MRWExceptions.h +0 -9
- package/ios/MRWExceptions.m +0 -10
- package/src/defaultNavigationRules.ts +0 -24
package/README.md
CHANGED
|
@@ -4,6 +4,7 @@ A React Native WebView that hands you the iOS and Android WebView APIs, instead
|
|
|
4
4
|
|
|
5
5
|
It runs on iOS and Android as an Expo module.
|
|
6
6
|
|
|
7
|
+
[Get started](guides/getting-started.md) ·
|
|
7
8
|
[Install](#install) ·
|
|
8
9
|
[npm](https://www.npmjs.com/package/react-native-minimal-webview) ·
|
|
9
10
|
[API reference](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/) ·
|
|
@@ -37,6 +38,7 @@ import { WebView, type WebViewRef } from 'react-native-minimal-webview';
|
|
|
37
38
|
export function Browser() {
|
|
38
39
|
const ref = useRef<WebViewRef>(null);
|
|
39
40
|
const pageDown = () => {
|
|
41
|
+
// Android's WebView.pageDown(boolean): a method string names one platform's method
|
|
40
42
|
if (Platform.OS === 'android') ref.current?.call('webView.pageDown', [true]);
|
|
41
43
|
};
|
|
42
44
|
return (
|
|
@@ -54,7 +56,8 @@ export function Browser() {
|
|
|
54
56
|
}
|
|
55
57
|
```
|
|
56
58
|
|
|
57
|
-
A setting
|
|
59
|
+
A setting a newer SDK adds goes under its root's `other` key, and `call` takes any public method, so neither waits for a
|
|
60
|
+
release of this package: see
|
|
58
61
|
[`ios`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#ios),
|
|
59
62
|
[`android`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#android) and
|
|
60
63
|
[`call`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/WebViewRef/#call).
|
|
@@ -63,10 +66,13 @@ A setting or method a newer SDK adds works without a release of this package: se
|
|
|
63
66
|
|
|
64
67
|
[How it works](guides/how-it-works.md) explains the parts. The core adds only what the platform APIs lack:
|
|
65
68
|
|
|
66
|
-
- `navigationRules
|
|
67
|
-
|
|
68
|
-
-
|
|
69
|
-
|
|
69
|
+
- [`navigationRules`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#navigationrules): navigations decided
|
|
70
|
+
natively, with a JS round trip only where a prop asks for one.
|
|
71
|
+
- A typed promise bridge ([`createBridge`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/functions/createBridge/)): `window.nativeBridge` in
|
|
72
|
+
the page, open only to the bridge's [`allowedOrigins`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/BridgeOptions/#allowedorigins).
|
|
73
|
+
- [`useWebViewState(ref)`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/functions/useWebViewState/): the navigation state, kept natively.
|
|
74
|
+
- [`onPermissionRequest`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#onpermissionrequest): your app answers the
|
|
75
|
+
page's permission requests (camera, microphone, location).
|
|
70
76
|
|
|
71
77
|
The [migration guide](guides/migrating-from-react-native-webview.md) covers each react-native-webview prop that works differently here.
|
|
72
78
|
|
|
@@ -78,13 +84,34 @@ The [migration guide](guides/migrating-from-react-native-webview.md) covers each
|
|
|
78
84
|
|
|
79
85
|
## Install
|
|
80
86
|
|
|
87
|
+
[Get started](guides/getting-started.md) walks you through a new Expo app with a WebView on screen.
|
|
88
|
+
|
|
89
|
+
In an Expo app, install the package and expo-build-properties:
|
|
90
|
+
|
|
81
91
|
```sh
|
|
82
|
-
npx expo install react-native-minimal-webview
|
|
92
|
+
npx expo install react-native-minimal-webview expo-build-properties
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Set the iOS and Android [minimums](#requirements) in `app.json`, before you rebuild: below iOS 17.0, the build leaves
|
|
96
|
+
the package's native module out, and `WebView` throws as it renders.
|
|
97
|
+
|
|
98
|
+
```json
|
|
99
|
+
{
|
|
100
|
+
"expo": {
|
|
101
|
+
"plugins": [
|
|
102
|
+
[
|
|
103
|
+
"expo-build-properties",
|
|
104
|
+
{ "ios": { "deploymentTarget": "17.0" }, "android": { "minSdkVersion": 29 } }
|
|
105
|
+
]
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
}
|
|
83
109
|
```
|
|
84
110
|
|
|
85
111
|
Then rebuild the app (`npx expo run:ios`, `npx expo run:android`).
|
|
86
112
|
|
|
87
|
-
A bare React Native app adds Expo modules first, then installs with its package manager
|
|
113
|
+
A bare React Native app adds Expo modules first, then installs with its package manager. It sets the minimums in
|
|
114
|
+
`ios/Podfile` (`platform :ios, '17.0'`) and `android/build.gradle` (`minSdkVersion = 29`) before `pod-install`:
|
|
88
115
|
|
|
89
116
|
```sh
|
|
90
117
|
npx install-expo-modules@latest
|
|
@@ -101,34 +128,11 @@ npx pod-install
|
|
|
101
128
|
| Expo SDK | 53 (React Native 0.79, React 19) |
|
|
102
129
|
| Architecture | [New architecture](https://reactnative.dev/architecture/landing-page) only |
|
|
103
130
|
|
|
104
|
-
An Expo app sets the two minimums with expo-build-properties (`npx expo install expo-build-properties`) in `app.json`:
|
|
105
|
-
|
|
106
|
-
```json
|
|
107
|
-
{
|
|
108
|
-
"expo": {
|
|
109
|
-
"plugins": [
|
|
110
|
-
[
|
|
111
|
-
"expo-build-properties",
|
|
112
|
-
{ "ios": { "deploymentTarget": "17.0" }, "android": { "minSdkVersion": 29 } }
|
|
113
|
-
]
|
|
114
|
-
]
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
```
|
|
118
|
-
|
|
119
131
|
Some features need a newer Android System WebView: [which ones](guides/android-system-webview.md).
|
|
120
132
|
|
|
121
133
|
## Usage
|
|
122
134
|
|
|
123
|
-
|
|
124
|
-
import { WebView } from 'react-native-minimal-webview';
|
|
125
|
-
|
|
126
|
-
export function Browser() {
|
|
127
|
-
return <WebView source={{ uri: 'https://example.com' }} style={{ flex: 1 }} />;
|
|
128
|
-
}
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
Then pick what you need:
|
|
135
|
+
Pick what you need:
|
|
132
136
|
|
|
133
137
|
| To | See |
|
|
134
138
|
| --------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
package expo.modules.minimalwebview
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
|
-
import android.os.Handler
|
|
5
4
|
import android.os.Looper
|
|
6
5
|
import android.util.Log
|
|
7
6
|
import android.view.View
|
|
@@ -422,11 +421,7 @@ internal class HookContext(val hook: MinimalWebViewHooks.Hook, view: MinimalWebV
|
|
|
422
421
|
|
|
423
422
|
// A hook's shouldInterceptRequest runs on Chromium's IO thread; the view's events and asks are main-thread state.
|
|
424
423
|
private fun onMain(block: () -> Unit) {
|
|
425
|
-
if (Looper.myLooper() == Looper.getMainLooper()) block() else
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
private companion object {
|
|
429
|
-
val main = Handler(Looper.getMainLooper())
|
|
424
|
+
if (Looper.myLooper() == Looper.getMainLooper()) block() else mainHandler.post { block() }
|
|
430
425
|
}
|
|
431
426
|
}
|
|
432
427
|
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
package expo.modules.minimalwebview
|
|
2
2
|
|
|
3
3
|
import android.annotation.SuppressLint
|
|
4
|
-
import android.app.Activity
|
|
5
4
|
import android.content.Context
|
|
6
5
|
import android.content.pm.ApplicationInfo
|
|
7
6
|
import android.os.Bundle
|
|
8
7
|
import android.os.Handler
|
|
9
8
|
import android.os.Looper
|
|
9
|
+
import android.util.AndroidRuntimeException
|
|
10
10
|
import android.util.Log
|
|
11
11
|
import android.view.MotionEvent
|
|
12
12
|
import android.view.View
|
|
@@ -37,9 +37,10 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
37
37
|
private val onProcessGone by EventDispatcher<Map<String, Any?>>()
|
|
38
38
|
private val onNavigation by EventDispatcher<Map<String, Any?>>()
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
// Lazy: it loads the WebView provider, which throws without one, and createWebView() reports that.
|
|
41
|
+
internal val hasNavigationListener by lazy { WebViewFeature.isFeatureSupported(WebViewFeature.NAVIGATION_LISTENER) }
|
|
41
42
|
private val navigation = MinimalWebViewNavigation(this) { onNavigation(it) }
|
|
42
|
-
|
|
43
|
+
internal val scripts = MinimalWebViewScripts(this) { onBridge(it) }
|
|
43
44
|
|
|
44
45
|
/** Set by the first prop batch and replaced after a renderer crash; see [recovering]. */
|
|
45
46
|
internal lateinit var webView: WebView
|
|
@@ -61,9 +62,13 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
61
62
|
internal var injectedScripts: List<InjectedScript>? = null
|
|
62
63
|
/** The `android` prop: WebSettings and WebView setters by name, applied by [PlatformSettings]. */
|
|
63
64
|
internal var settings: Map<String, Any?>? = null
|
|
64
|
-
internal val platformSettings = PlatformSettings { PlatformSettings.targets(webView, it) }
|
|
65
|
-
internal var pullToRefresh = false
|
|
65
|
+
internal val platformSettings = PlatformSettings({ hasNavigationListener }) { PlatformSettings.targets(webView, it) }
|
|
66
66
|
private val refresher = PullToRefresh(this)
|
|
67
|
+
internal var pullToRefresh = false
|
|
68
|
+
set(value) {
|
|
69
|
+
field = value
|
|
70
|
+
refresher.layout.isEnabled = value
|
|
71
|
+
}
|
|
67
72
|
/**
|
|
68
73
|
* The page requests JS has a prop for, by prop name (`onFileRequest`, `onDownload`, ...), and `onNavigation`, read when a
|
|
69
74
|
* request comes or a decision is reported.
|
|
@@ -74,7 +79,6 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
74
79
|
// The hooks' lists: each context owns its own, and the core routes those of the hooks that are on, in attach order.
|
|
75
80
|
val clients: List<WebViewClient> get() = routed(hookContexts) { it.clients }
|
|
76
81
|
val chromeClients: List<WebChromeClient> get() = routed(hookContexts) { it.chromeClients }
|
|
77
|
-
val activity: Activity? get() = appContext.currentActivity
|
|
78
82
|
internal val fileUpload = FileUpload(this)
|
|
79
83
|
internal val fullscreen = FullscreenVideo(this)
|
|
80
84
|
internal val permissions = Permissions(this)
|
|
@@ -98,15 +102,16 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
98
102
|
private var lastEmitted: Map<String, Any?>? = null
|
|
99
103
|
private var emitScheduled = false
|
|
100
104
|
|
|
101
|
-
/**
|
|
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
|
+
*/
|
|
102
109
|
private var pendingRecovery: Recovery? = null
|
|
103
|
-
private class Recovery(val reload: Boolean, val history: Bundle
|
|
110
|
+
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
|
+
private var pendingLoad: String? = null
|
|
104
113
|
private var destroyed = false
|
|
105
114
|
|
|
106
|
-
// The core's deferred work. Not View.post: a detached view (unmounted, or a covered screen) holds that until it is
|
|
107
|
-
// attached again.
|
|
108
|
-
internal val mainHandler = Handler(Looper.getMainLooper())
|
|
109
|
-
|
|
110
115
|
init {
|
|
111
116
|
// The WebView goes into the layout in setUp(); the prop only turns the pull on and off.
|
|
112
117
|
addView(refresher.layout, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
|
|
@@ -188,10 +193,13 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
188
193
|
applySettings()
|
|
189
194
|
scripts.update()
|
|
190
195
|
} else {
|
|
191
|
-
webView =
|
|
196
|
+
webView = try {
|
|
197
|
+
createWebView()
|
|
198
|
+
} catch (e: AndroidRuntimeException) { // no WebView provider: missing, disabled or updating
|
|
199
|
+
return reportError("network", source?.uri.orEmpty(), WebViewClient.ERROR_UNKNOWN, "Android System WebView is unavailable: ${e.message}")
|
|
200
|
+
}
|
|
192
201
|
setUp()
|
|
193
202
|
}
|
|
194
|
-
refresher.layout.isEnabled = pullToRefresh
|
|
195
203
|
loadNewSource()
|
|
196
204
|
}
|
|
197
205
|
|
|
@@ -225,14 +233,20 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
225
233
|
}
|
|
226
234
|
}
|
|
227
235
|
|
|
236
|
+
/** The ref's `load`: like [retry], the app's own action starts the crash-loop guard over. */
|
|
237
|
+
internal fun load(url: String) {
|
|
238
|
+
crashTimes.clear()
|
|
239
|
+
if (recovering) pendingLoad = url else webView.loadUrl(url)
|
|
240
|
+
}
|
|
241
|
+
|
|
228
242
|
/** Loads the page on screen again: the `source` when that is the page (its headers, html or POST), else its URL. */
|
|
229
243
|
private fun loadAgain() {
|
|
230
244
|
if (showsSource(source, url)) source?.let(::load) else if (url.isNotEmpty()) webView.loadUrl(url)
|
|
231
245
|
}
|
|
232
246
|
|
|
233
247
|
/**
|
|
234
|
-
* The ref's reload: the app's retry starts the crash-loop guard over. Only this
|
|
235
|
-
* process's renderer deaths, so another view's new source or pull-to-refresh mustn't end a crash loop's count.
|
|
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.
|
|
236
250
|
*/
|
|
237
251
|
internal fun retry() {
|
|
238
252
|
crashTimes.clear()
|
|
@@ -243,14 +257,17 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
243
257
|
when {
|
|
244
258
|
// The renderer died and the new WebView isn't built yet: build it now, and load.
|
|
245
259
|
recovering -> recover(reload = true)
|
|
246
|
-
//
|
|
247
|
-
|
|
248
|
-
//
|
|
249
|
-
|
|
260
|
+
// The first WebView couldn't be made (onError said so): try again.
|
|
261
|
+
!hasWebView -> applyProps()
|
|
262
|
+
// Nothing loaded: the crash-loop guard gave up (processGone), and this is the app's retry. Or a POST source:
|
|
263
|
+
// WebView's reload declines its form resubmission.
|
|
264
|
+
webView.url == null || showingPost -> loadAgain()
|
|
250
265
|
else -> webView.reload()
|
|
251
266
|
}
|
|
252
267
|
}
|
|
253
268
|
|
|
269
|
+
private val showingPost get() = source?.body != null && showsSource(source, url)
|
|
270
|
+
|
|
254
271
|
/**
|
|
255
272
|
* The `android` settings, applied from each prop batch (only the keys that changed) and all again to a new WebView
|
|
256
273
|
* after a crash. A key that can't be applied is reported to JS, which warns.
|
|
@@ -327,8 +344,6 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
327
344
|
stateChanged()
|
|
328
345
|
}
|
|
329
346
|
|
|
330
|
-
internal fun bridgePost(message: String) = scripts.post(message)
|
|
331
|
-
|
|
332
347
|
internal fun evaluate(script: String, onResult: (Result<String>) -> Unit) =
|
|
333
348
|
if (recovering) onResult(Result.failure(EvaluateError("unavailable", RECOVERING))) else scripts.evaluate(script, onResult)
|
|
334
349
|
|
|
@@ -338,12 +353,12 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
338
353
|
*/
|
|
339
354
|
internal fun processGone(cause: String, at: Long) {
|
|
340
355
|
if (destroyed) return
|
|
341
|
-
val reload = crashAllowsReload(crashTimes, at) && !
|
|
356
|
+
val reload = crashAllowsReload(crashTimes, at) && !showingPost
|
|
342
357
|
// The source page reloads as the source: a restored entry carries neither source.headers nor an html source.
|
|
343
358
|
val restorable = !showsSource(source, url)
|
|
344
|
-
val history = Bundle().takeIf { reload &&
|
|
359
|
+
val history = Bundle().takeIf { reload && webView.saveState(it) != null }
|
|
345
360
|
tearDown(DocumentEnd.CRASHED)
|
|
346
|
-
pendingRecovery = Recovery(reload, history)
|
|
361
|
+
pendingRecovery = Recovery(reload, history, restorable)
|
|
347
362
|
// Nothing will end the load the crash cut off. `url` stays: loadAgain() loads it.
|
|
348
363
|
if (loading) outcome = "failed"
|
|
349
364
|
loading = false
|
|
@@ -374,11 +389,15 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
374
389
|
pendingRecovery = null
|
|
375
390
|
webView = createWebView()
|
|
376
391
|
setUp()
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
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
|
+
val replaced = pendingLoad != null || source != null && source != loadedSource
|
|
394
|
+
val restored = (replaced || recovery.restorable) && recovery.history?.let { webView.restoreState(it) } != null
|
|
395
|
+
if (replaced) {
|
|
396
|
+
loadNewSource()
|
|
397
|
+
pendingLoad?.let(webView::loadUrl)
|
|
398
|
+
pendingLoad = null
|
|
399
|
+
} else if (!restored && (recovery.reload || reload)) {
|
|
400
|
+
loadAgain()
|
|
382
401
|
}
|
|
383
402
|
stateChanged()
|
|
384
403
|
}
|
|
@@ -457,6 +476,12 @@ internal class MinimalWebView(context: Context, appContext: AppContext) : ExpoVi
|
|
|
457
476
|
}
|
|
458
477
|
|
|
459
478
|
internal const val TAG = "MinimalWebView"
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* The core's deferred work. Not View.post: a detached view (unmounted, or a covered screen) holds that until it is
|
|
482
|
+
* attached again. Lazy: the JVM tests load this file, and Handler's constructor throws there.
|
|
483
|
+
*/
|
|
484
|
+
internal val mainHandler by lazy { Handler(Looper.getMainLooper()) }
|
|
460
485
|
internal const val RECOVERING = "the WebView is being rebuilt after its renderer died"
|
|
461
486
|
|
|
462
487
|
/** Whether this process has made a WebView. */
|
|
@@ -504,7 +529,7 @@ private fun canonicalUrl(url: String): String {
|
|
|
504
529
|
private val urlAuthority = Regex("^([a-zA-Z][a-zA-Z0-9+.-]*)://([^/?#@]*@)?(\\[[^\\]/?#]*]|[^/?#:]*)(?::(\\d*))?(.*)$")
|
|
505
530
|
|
|
506
531
|
/** The special schemes' default ports (the WHATWG URL Standard's), as on iOS (Loads.swift). */
|
|
507
|
-
|
|
532
|
+
internal val defaultPorts = mapOf("http" to 80, "https" to 443, "ws" to 80, "wss" to 443, "ftp" to 21)
|
|
508
533
|
|
|
509
534
|
/**
|
|
510
535
|
* Records a renderer death at [now] in [times] and says whether to reload after it: not after more than
|
|
@@ -75,7 +75,8 @@ internal class Listener(private val owner: MinimalWebView) : NavigationListener
|
|
|
75
75
|
|
|
76
76
|
override fun onPageLoadEvent(page: Page) {
|
|
77
77
|
loadedPages += page
|
|
78
|
-
|
|
78
|
+
// The page being left still fires its load event while the next one is pending: that load isn't over.
|
|
79
|
+
if (owner.pendingUrl == null) owner.loadEnded()
|
|
79
80
|
}
|
|
80
81
|
}
|
|
81
82
|
|
|
@@ -212,7 +213,7 @@ internal class Client(private val owner: MinimalWebView, private val navigation:
|
|
|
212
213
|
// Returning true keeps the app alive; a stale WebView (already replaced) needs nothing more.
|
|
213
214
|
// Timed here: every WebView on the renderer hears of its death back to back, before any recovery runs.
|
|
214
215
|
val at = SystemClock.elapsedRealtime()
|
|
215
|
-
if (view === owner.webView)
|
|
216
|
+
if (view === owner.webView) mainHandler.post { owner.processGone(if (detail.didCrash()) "crash" else "memory", at) }
|
|
216
217
|
observing("onRenderProcessGone", WEB_VIEW, RenderProcessGoneDetail::class.java) { it.onRenderProcessGone(view, detail) }
|
|
217
218
|
return true
|
|
218
219
|
}
|
|
@@ -3,6 +3,7 @@ package expo.modules.minimalwebview
|
|
|
3
3
|
import android.content.Context
|
|
4
4
|
import android.content.Intent
|
|
5
5
|
import android.net.Uri
|
|
6
|
+
import android.util.AndroidRuntimeException
|
|
6
7
|
import android.util.Log
|
|
7
8
|
import android.webkit.CookieManager
|
|
8
9
|
import android.webkit.WebChromeClient.FileChooserParams
|
|
@@ -106,6 +107,7 @@ class MinimalWebViewModule : Module() {
|
|
|
106
107
|
|
|
107
108
|
AsyncFunction("goBack") { view: MinimalWebView -> live(view) { it.goBack() } }.runOnQueue(Queues.MAIN)
|
|
108
109
|
AsyncFunction("goForward") { view: MinimalWebView -> live(view) { it.goForward() } }.runOnQueue(Queues.MAIN)
|
|
110
|
+
AsyncFunction("load") { view: MinimalWebView, url: String -> view.load(url) }.runOnQueue(Queues.MAIN)
|
|
109
111
|
AsyncFunction("reload") { view: MinimalWebView -> view.retry() }.runOnQueue(Queues.MAIN)
|
|
110
112
|
AsyncFunction("stopLoading") { view: MinimalWebView -> live(view) { it.stopLoading() } }.runOnQueue(Queues.MAIN)
|
|
111
113
|
AsyncFunction("get") { view: MinimalWebView, path: String, promise: Promise ->
|
|
@@ -118,7 +120,7 @@ class MinimalWebViewModule : Module() {
|
|
|
118
120
|
// A call can change what the state shows (clearHistory, canGoBack); an unchanged state isn't sent.
|
|
119
121
|
view.stateChanged()
|
|
120
122
|
}.runOnQueue(Queues.MAIN)
|
|
121
|
-
AsyncFunction("bridgePost") { view: MinimalWebView, message: String -> view.
|
|
123
|
+
AsyncFunction("bridgePost") { view: MinimalWebView, message: String -> view.scripts.post(message) }.runOnQueue(Queues.MAIN)
|
|
122
124
|
AsyncFunction("answer") { view: MinimalWebView, askId: Int, json: String ->
|
|
123
125
|
view.asks.answer(askId, fromJson(JSONTokener(json).nextValue()))
|
|
124
126
|
}.runOnQueue(Queues.MAIN)
|
|
@@ -161,14 +163,24 @@ private fun setWebContentsDebugging(enabled: Boolean) {
|
|
|
161
163
|
}
|
|
162
164
|
|
|
163
165
|
private fun warmUp(context: Context) {
|
|
164
|
-
val
|
|
165
|
-
WebViewCompat.startUpWebView(context, config, object : WebViewOutcomeReceiver<WebViewStartUpResult, WebViewStartupException> {
|
|
166
|
+
val outcome = object : WebViewOutcomeReceiver<WebViewStartUpResult, WebViewStartupException> {
|
|
166
167
|
override fun onResult(result: WebViewStartUpResult) = Unit
|
|
167
168
|
|
|
168
169
|
override fun onError(error: WebViewStartupException) {
|
|
169
170
|
Log.w(TAG, "WebView warm-up failed", error)
|
|
170
171
|
}
|
|
171
|
-
}
|
|
172
|
+
}
|
|
173
|
+
// startUpWebView doesn't catch its task's throw, which on this thread would crash the app without a WebView provider.
|
|
174
|
+
val config = WebViewStartUpConfig.Builder { task ->
|
|
175
|
+
Thread({
|
|
176
|
+
try {
|
|
177
|
+
task.run()
|
|
178
|
+
} catch (e: AndroidRuntimeException) {
|
|
179
|
+
outcome.onError(WebViewStartupException("no WebView provider").apply { initCause(e) })
|
|
180
|
+
}
|
|
181
|
+
}, "WebViewWarmUp").start()
|
|
182
|
+
}.build()
|
|
183
|
+
WebViewCompat.startUpWebView(context, config, outcome)
|
|
172
184
|
}
|
|
173
185
|
|
|
174
186
|
/** Runs [action] on the WebView, unless it's recovering: `webView` is then the destroyed one, and only reload acts, by building the new one. */
|
|
@@ -79,7 +79,7 @@ internal class MinimalWebViewNavigation(
|
|
|
79
79
|
if (action == "external") openExternal(request, rule) else notifyNavigation(request, action, rule)
|
|
80
80
|
}
|
|
81
81
|
// Unanswered after the timeout: block (the answer callback runs with null). A late answer finds nothing.
|
|
82
|
-
|
|
82
|
+
mainHandler.postDelayed({ owner.asks.cancel(id) }, ASK_TIMEOUT_MS)
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
// WebView's request headers on a navigation are empty, so the page's URL goes as the Referer, trimmed by WebView's
|
|
@@ -171,7 +171,7 @@ internal class MinimalWebViewNavigation(
|
|
|
171
171
|
// Destroyed later: this may run inside the child's own callback.
|
|
172
172
|
fun finish(): Boolean {
|
|
173
173
|
val wasPending = children.remove(child)
|
|
174
|
-
if (wasPending)
|
|
174
|
+
if (wasPending) mainHandler.post { child.destroy() }
|
|
175
175
|
return wasPending
|
|
176
176
|
}
|
|
177
177
|
child.webViewClient = object : WebViewClient() {
|
|
@@ -188,7 +188,7 @@ internal class MinimalWebViewNavigation(
|
|
|
188
188
|
}
|
|
189
189
|
}
|
|
190
190
|
// window.open('') or about:blank never navigates: give up on it.
|
|
191
|
-
|
|
191
|
+
mainHandler.postDelayed({ finish() }, NEW_WINDOW_TIMEOUT_MS)
|
|
192
192
|
(resultMsg.obj as WebView.WebViewTransport).webView = child
|
|
193
193
|
resultMsg.sendToTarget()
|
|
194
194
|
return true
|
|
@@ -27,12 +27,9 @@ internal class MinimalWebViewScripts(private val owner: MinimalWebView, private
|
|
|
27
27
|
scriptHandlers.clear()
|
|
28
28
|
installEvaluate()
|
|
29
29
|
installBridge()
|
|
30
|
-
|
|
30
|
+
update(force = true)
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
/** A later prop batch: only `injectedScripts` can change. */
|
|
34
|
-
fun update() = installScripts()
|
|
35
|
-
|
|
36
33
|
private fun installBridge() {
|
|
37
34
|
val script = owner.bridgeScript ?: return
|
|
38
35
|
val bridgeFeatures = listOf(WebViewFeature.WEB_MESSAGE_LISTENER, WebViewFeature.DOCUMENT_START_SCRIPT)
|
|
@@ -60,8 +57,8 @@ internal class MinimalWebViewScripts(private val owner: MinimalWebView, private
|
|
|
60
57
|
}
|
|
61
58
|
}
|
|
62
59
|
|
|
63
|
-
/**
|
|
64
|
-
|
|
60
|
+
/** Installs `injectedScripts` if they changed, or with [force] always. They take effect from the next navigation. */
|
|
61
|
+
fun update(force: Boolean = false) {
|
|
65
62
|
val scripts = owner.injectedScripts.orEmpty()
|
|
66
63
|
if (!force && scripts == installedScripts) return
|
|
67
64
|
installedScripts = scripts
|
|
@@ -90,7 +87,8 @@ internal class MinimalWebViewScripts(private val owner: MinimalWebView, private
|
|
|
90
87
|
// At a commit this runs one main-thread hop after Chromium's (the NavigationListener's executor posts it), and
|
|
91
88
|
// relies on landing before the new page's first message sets replyProxy (the bridge suite's head-script call).
|
|
92
89
|
replyProxy = null
|
|
93
|
-
if (reason
|
|
90
|
+
if (reason == DocumentEnd.COMMITTED && owner.bridgeScript != null) onBridge(mapOf("navigated" to true))
|
|
91
|
+
if (reason == DocumentEnd.CRASHED && owner.bridgeScript != null) onBridge(mapOf("crashed" to true))
|
|
94
92
|
}
|
|
95
93
|
|
|
96
94
|
/**
|
|
@@ -19,7 +19,10 @@ import kotlin.math.truncate
|
|
|
19
19
|
* (`getMethods()`), so a setter a newer SDK or androidx.webkit adds needs no code here. The roots and their order:
|
|
20
20
|
* [Companion.targets].
|
|
21
21
|
*/
|
|
22
|
-
internal class PlatformSettings(
|
|
22
|
+
internal class PlatformSettings(
|
|
23
|
+
private val hasNavigationListener: () -> Boolean = { true },
|
|
24
|
+
private val targets: (root: String) -> List<Target>?
|
|
25
|
+
) {
|
|
23
26
|
/** `root.key` -> the last value applied or rejected, so an unchanged value is neither applied nor reported again. */
|
|
24
27
|
private val applied = mutableMapOf<String, Any?>()
|
|
25
28
|
|
|
@@ -65,6 +68,7 @@ internal class PlatformSettings(private val targets: (root: String) -> List<Targ
|
|
|
65
68
|
|
|
66
69
|
/** [set], keeping the value it replaces the first time under [id]; returns the problem, or null when it was set. */
|
|
67
70
|
private fun write(targets: List<Target>, id: String, key: String, value: Any?): String? {
|
|
71
|
+
refusal(setterOf(key), listOf(value))?.let { return it }
|
|
68
72
|
if (id in originals) return set(targets, key, value)
|
|
69
73
|
return set(targets, key, value) {
|
|
70
74
|
originals[id] = runCatching { read(targets, key) }.getOrDefault(NONE)
|
|
@@ -109,12 +113,18 @@ internal class PlatformSettings(private val targets: (root: String) -> List<Targ
|
|
|
109
113
|
fun call(path: String, args: List<Any?>, reply: (() -> String) -> Unit) {
|
|
110
114
|
val (targets, name) = resolve(path)
|
|
111
115
|
if (name in OWNED) throw problem("$name is ${owned(name)}")
|
|
112
|
-
|
|
113
|
-
|
|
116
|
+
refusal(name, args)?.let { throw PlatformException("unavailable", it) }
|
|
117
|
+
val direct = find(targets, args) { it.name == name }
|
|
118
|
+
if (direct != null) {
|
|
119
|
+
val result = direct()
|
|
120
|
+
return reply { toJson(result) }
|
|
121
|
+
}
|
|
114
122
|
// The callback runs later, outside the caller's try: the receiver converts, so it can reject instead of crashing.
|
|
115
123
|
val callback = ValueCallback<Any?> { value -> reply { toJson(value) } }
|
|
116
|
-
val awaiting =
|
|
117
|
-
|
|
124
|
+
val awaiting = find(targets, args + callback) {
|
|
125
|
+
it.name == name && it.parameterTypes.lastOrNull() == ValueCallback::class.java
|
|
126
|
+
} ?: throw notFound("no public method $path takes these arguments")
|
|
127
|
+
awaiting()
|
|
118
128
|
}
|
|
119
129
|
|
|
120
130
|
/** The targets of `root.key` and the key; throws for a malformed path or an unknown root. */
|
|
@@ -124,9 +134,20 @@ internal class PlatformSettings(private val targets: (root: String) -> List<Targ
|
|
|
124
134
|
return (targets(parts[0]) ?: throw notFound("unknown root: use settings, webView, cookieManager or webStorage")) to parts[1]
|
|
125
135
|
}
|
|
126
136
|
|
|
137
|
+
/** Why [method] can't take [args] on this WebView, or null. */
|
|
138
|
+
private fun refusal(method: String, args: List<Any?>) =
|
|
139
|
+
// Without NavigationListener the clients end a load at onPageFinished, which WebView 133 skips for a restored page.
|
|
140
|
+
if (method == "setBackForwardCacheEnabled" && args == listOf(true) && !hasNavigationListener()) {
|
|
141
|
+
"not supported by this WebView: below Android System WebView 146 a page restored from the cache reports no load end"
|
|
142
|
+
} else {
|
|
143
|
+
null
|
|
144
|
+
}
|
|
145
|
+
|
|
127
146
|
internal companion object {
|
|
128
147
|
private val NONE = Any()
|
|
129
148
|
|
|
149
|
+
private fun setterOf(key: String) = "set" + key.replaceFirstChar { it.uppercaseChar() }
|
|
150
|
+
|
|
130
151
|
/** The targets of [root] on [webView], or null for an unknown root. */
|
|
131
152
|
fun targets(webView: WebView, root: String): List<Target>? = when (root) {
|
|
132
153
|
"settings" -> listOf(
|
|
@@ -156,16 +177,16 @@ internal class PlatformSettings(private val targets: (root: String) -> List<Targ
|
|
|
156
177
|
"setWebContentsDebuggingEnabled", "setOnTouchListener", "setOnScrollChangeListener", "addNavigationListener"
|
|
157
178
|
)
|
|
158
179
|
|
|
159
|
-
/** The
|
|
160
|
-
private fun
|
|
180
|
+
/** The first method of [targets], target by target, that [accepts] and takes [values], bound to them; or null. */
|
|
181
|
+
private fun find(targets: List<Target>, values: List<Any?>, accepts: (Method) -> Boolean): (() -> Any?)? {
|
|
161
182
|
for (target in targets) {
|
|
162
183
|
for (method in target.cls.methods) {
|
|
163
|
-
if (
|
|
164
|
-
val
|
|
165
|
-
return invoke(method, target,
|
|
184
|
+
if (!accepts(method)) continue
|
|
185
|
+
val args = arguments(method, target, values) ?: continue
|
|
186
|
+
return { invoke(method, target, args) }
|
|
166
187
|
}
|
|
167
188
|
}
|
|
168
|
-
return
|
|
189
|
+
return null
|
|
169
190
|
}
|
|
170
191
|
|
|
171
192
|
private fun owned(name: String) =
|
|
@@ -181,38 +202,28 @@ internal class PlatformSettings(private val targets: (root: String) -> List<Targ
|
|
|
181
202
|
*/
|
|
182
203
|
private fun set(targets: List<Target>, key: String, value: Any?, before: () -> Unit = {}): String? {
|
|
183
204
|
if (key.isEmpty() || key.startsWith("_")) return "not a public setter"
|
|
184
|
-
val setter =
|
|
205
|
+
val setter = setterOf(key)
|
|
185
206
|
if (setter in OWNED) return owned(setter)
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
} catch (e: IllegalArgumentException) { // a PlatformException from invoke is one, so its reason is the message
|
|
197
|
-
e.message
|
|
198
|
-
}
|
|
199
|
-
}
|
|
207
|
+
val apply = find(targets, listOf(value)) { it.name == setter } ?: return when {
|
|
208
|
+
targets.none { t -> t.cls.methods.any { it.name == setter } } -> "no public setter"
|
|
209
|
+
else -> "no public setter takes a ${value?.javaClass?.simpleName ?: "null"}"
|
|
210
|
+
}
|
|
211
|
+
return try {
|
|
212
|
+
before()
|
|
213
|
+
apply()
|
|
214
|
+
null
|
|
215
|
+
} catch (e: IllegalArgumentException) { // a PlatformException from invoke is one, so its reason is the message
|
|
216
|
+
e.message
|
|
200
217
|
}
|
|
201
|
-
return if (seen) "no public setter takes a ${value?.javaClass?.simpleName ?: "null"}" else "no public setter"
|
|
202
218
|
}
|
|
203
219
|
|
|
204
220
|
/** The value of [key]'s getter on the first target that has one, or [NONE]. */
|
|
205
221
|
private fun read(targets: List<Target>, key: String): Any? {
|
|
206
222
|
val capital = key.replaceFirstChar { it.uppercaseChar() }
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
val args = arguments(method, target, emptyList()) ?: continue
|
|
212
|
-
return invoke(method, target, args)
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
return NONE
|
|
223
|
+
val getter = find(targets, emptyList()) {
|
|
224
|
+
it.name == "get$capital" || it.name == "is$capital" || (it.name == key && it.returnType != Void.TYPE)
|
|
225
|
+
} ?: return NONE
|
|
226
|
+
return getter()
|
|
216
227
|
}
|
|
217
228
|
|
|
218
229
|
/** The method's arguments for [values], with the target's leading object first, or null when they don't fit. */
|
|
@@ -20,7 +20,7 @@ internal class PullToRefresh(private val owner: MinimalWebView) {
|
|
|
20
20
|
/** Holds the WebView. SwipeRefreshLayout keeps its first child as its target for good, so a crash swaps the WebView here. */
|
|
21
21
|
val content = FrameLayout(owner.context)
|
|
22
22
|
|
|
23
|
-
/** Always around [content]; off until the
|
|
23
|
+
/** Always around [content]; off until the prop sets `isEnabled`. */
|
|
24
24
|
val layout: SwipeRefreshLayout = Layout(owner.context).apply {
|
|
25
25
|
isEnabled = false
|
|
26
26
|
addView(content, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
|
|
@@ -35,7 +35,7 @@ internal class PullToRefresh(private val owner: MinimalWebView) {
|
|
|
35
35
|
if (touch == touches) innerScrolled = result == "true"
|
|
36
36
|
}
|
|
37
37
|
// A busy page answers late: don't hold the pull off for longer than this.
|
|
38
|
-
|
|
38
|
+
mainHandler.postDelayed({ if (touch == touches && innerScrolled == null) innerScrolled = false }, ANSWER_TIMEOUT_MS)
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/** A load ended: the spinner stops, or it would ignore the next pull. */
|