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
|
@@ -11,10 +11,9 @@ extension MinimalWebView {
|
|
|
11
11
|
// rules, as on Android (shouldOverrideUrlLoading is not called for them).
|
|
12
12
|
guard let targetFrame = action.targetFrame else { return decide(allowInPlace) }
|
|
13
13
|
let mainFrame = targetFrame.isMainFrame
|
|
14
|
-
let own =
|
|
14
|
+
let own = takeOwnLoad(action)
|
|
15
15
|
if own || action.navigationType == .backForward || action.navigationType == .reload
|
|
16
16
|
|| action.request.httpMethod == "POST" {
|
|
17
|
-
if own { programmaticUrl = nil }
|
|
18
17
|
return decide(allowInPlace)
|
|
19
18
|
}
|
|
20
19
|
let request = NavigationRules.Request(
|
|
@@ -34,6 +33,15 @@ extension MinimalWebView {
|
|
|
34
33
|
}
|
|
35
34
|
}
|
|
36
35
|
|
|
36
|
+
/// Counts a main-frame policy check, whoever decides it, and takes the own load (`ownLoads`) it is for: whether it is one.
|
|
37
|
+
func takeOwnLoad(_ action: WKNavigationAction) -> Bool {
|
|
38
|
+
guard action.targetFrame?.isMainFrame == true, let target = action.request.url else { return false }
|
|
39
|
+
mainFrameChecks += 1
|
|
40
|
+
guard let own = ownLoads.firstIndex(where: { Loads.sameURL($0.url, target) }) else { return false }
|
|
41
|
+
ownLoads.remove(at: own)
|
|
42
|
+
return true
|
|
43
|
+
}
|
|
44
|
+
|
|
37
45
|
/// Whether a user gesture started `action`: a link activation right after a tap on this view (`Taps`). WebKit
|
|
38
46
|
/// reports every navigation with a triggering event as `.linkActivated` (NavigationAction.cpp's navigationType),
|
|
39
47
|
/// including a link a script clicks (`element.click()`), and no public WKNavigationAction property tells a real one
|
|
@@ -47,7 +55,7 @@ extension MinimalWebView {
|
|
|
47
55
|
func openDownload(_ url: URL) {
|
|
48
56
|
guard ["http", "https"].contains(url.scheme?.lowercased()) else { return }
|
|
49
57
|
let request = NavigationRules.Request(url: url.absoluteString, mainFrame: true, newWindow: false, userInitiated: false)
|
|
50
|
-
finish(request,
|
|
58
|
+
finish(request, .external, nil) { _ in }
|
|
51
59
|
}
|
|
52
60
|
|
|
53
61
|
/// window.open and target=_blank. Never a second WKWebView: a popup model (presenting and closing a child,
|
|
@@ -88,7 +96,7 @@ extension MinimalWebView {
|
|
|
88
96
|
navigationRules, request, asked: askedAndAllowed[request.mainFrame] ?? [],
|
|
89
97
|
canAsk: requestHandlers.contains("onNavigationRequest")
|
|
90
98
|
)
|
|
91
|
-
guard decision.action ==
|
|
99
|
+
guard decision.action == .ask else {
|
|
92
100
|
finish(request, decision.action, decision.rule, completion)
|
|
93
101
|
return
|
|
94
102
|
}
|
|
@@ -96,7 +104,7 @@ extension MinimalWebView {
|
|
|
96
104
|
// The view can be gone: the decision handler must still be called.
|
|
97
105
|
guard let self else { return completion(false) }
|
|
98
106
|
let action = NavigationRules.answer(answer)
|
|
99
|
-
if action ==
|
|
107
|
+
if action == .allow { self.askedAndAllowed[request.mainFrame, default: []].insert(request.url) }
|
|
100
108
|
self.finish(request, action, decision.rule, completion)
|
|
101
109
|
}
|
|
102
110
|
// Unanswered after the timeout: block.
|
|
@@ -105,25 +113,27 @@ extension MinimalWebView {
|
|
|
105
113
|
}
|
|
106
114
|
}
|
|
107
115
|
|
|
108
|
-
private func finish(
|
|
109
|
-
completion(
|
|
110
|
-
|
|
116
|
+
private func finish(
|
|
117
|
+
_ request: NavigationRules.Request, _ action: NavigationRules.Action, _ rule: Int?, _ completion: (Bool) -> Void
|
|
118
|
+
) {
|
|
119
|
+
completion(action == .allow)
|
|
120
|
+
guard action == .external else { return report(request, action, rule) }
|
|
111
121
|
guard requestHandlers.contains("onOpenExternal") else { return open(request, rule) }
|
|
112
122
|
// A page that moved on first answers nil, which counts as opened. No timeout: nothing waits for the answer.
|
|
113
123
|
asks.ask("core", "onOpenExternal", request.payload) { [weak self] answer in
|
|
114
|
-
if answer as? Bool == false { self?.open(request, rule) } else { self?.report(request,
|
|
124
|
+
if answer as? Bool == false { self?.open(request, rule) } else { self?.report(request, .external, rule) }
|
|
115
125
|
}
|
|
116
126
|
}
|
|
117
127
|
|
|
118
128
|
/// Opens an `external` URL outside the WebView. As on Android, one that nothing opens is reported as blocked.
|
|
119
129
|
private func open(_ request: NavigationRules.Request, _ rule: Int?) {
|
|
120
|
-
guard let url = URL(string: request.url) else { return report(request,
|
|
121
|
-
UIApplication.shared.open(url) { [weak self] opened in self?.report(request, opened ?
|
|
130
|
+
guard let url = URL(string: request.url) else { return report(request, .block, rule) }
|
|
131
|
+
UIApplication.shared.open(url) { [weak self] opened in self?.report(request, opened ? .external : .block, rule) }
|
|
122
132
|
}
|
|
123
133
|
|
|
124
|
-
private func report(_ request: NavigationRules.Request, _ action:
|
|
134
|
+
private func report(_ request: NavigationRules.Request, _ action: NavigationRules.Action, _ rule: Int?) {
|
|
125
135
|
guard requestHandlers.contains("onNavigation") else { return }
|
|
126
|
-
onNavigation(request.payload.merging(["action": action, "rule": rule as Any? ?? NSNull()]) { $1 })
|
|
136
|
+
onNavigation(request.payload.merging(["action": action.rawValue, "rule": rule as Any? ?? NSNull()]) { $1 })
|
|
127
137
|
}
|
|
128
138
|
}
|
|
129
139
|
|
|
@@ -38,6 +38,8 @@ extension MinimalWebView {
|
|
|
38
38
|
case .failure(let error):
|
|
39
39
|
// The thrown error's own text, as on Android; WebKit's localizedDescription is generic.
|
|
40
40
|
let message = (error as NSError).userInfo["WKJavaScriptExceptionMessage"] as? String ?? error.localizedDescription
|
|
41
|
+
// WebKit drops the call of a page being left, and (iOS 27) can say so before didCommit.
|
|
42
|
+
if message == Self.unreachable, self?.webView?.isLoading == true { return promise.reject("navigated", Self.navigated) }
|
|
41
43
|
promise.reject("script_error", message)
|
|
42
44
|
}
|
|
43
45
|
}
|
|
@@ -47,9 +49,12 @@ extension MinimalWebView {
|
|
|
47
49
|
func failEvaluations() {
|
|
48
50
|
let waiting = evaluations.values
|
|
49
51
|
evaluations = [:]
|
|
50
|
-
waiting.forEach { $0.reject("navigated",
|
|
52
|
+
waiting.forEach { $0.reject("navigated", Self.navigated) }
|
|
51
53
|
}
|
|
52
54
|
|
|
55
|
+
private static let navigated = "the page navigated before the script finished"
|
|
56
|
+
private static let unreachable = "Completion handler for function call is no longer reachable"
|
|
57
|
+
|
|
53
58
|
func bridgeReceive(_ message: WKScriptMessage) {
|
|
54
59
|
let origin = OriginRules.origin(of: message.frameInfo.securityOrigin)
|
|
55
60
|
// This view's main frame only: a window.open('') view (WindowCatcher) shares the content controller.
|
package/ios/MinimalWebView.swift
CHANGED
|
@@ -45,12 +45,10 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
45
45
|
// Created once, from the first props: WebKit reads some WKWebViewConfiguration settings only at creation.
|
|
46
46
|
private(set) var webView: WKWebView?
|
|
47
47
|
private var observations: [NSKeyValueObservation] = []
|
|
48
|
-
private
|
|
48
|
+
private lazy var downloads = Downloads(view: self)
|
|
49
49
|
private lazy var proxy = DelegateProxy(core: self)
|
|
50
50
|
/// When the user last tapped the WKWebView: a link activation counts as user-initiated only right after a tap.
|
|
51
51
|
let taps = Taps(target: nil, action: nil)
|
|
52
|
-
/// The decision warnings sent, shared and taken (DelegateProxy): one each per view.
|
|
53
|
-
private var warnedDecisions: Set<String> = []
|
|
54
52
|
|
|
55
53
|
// Navigation state, derived from WebKit in refreshUrls(). Native owns the committed URL; JS is told.
|
|
56
54
|
private(set) var url = ""
|
|
@@ -63,15 +61,23 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
63
61
|
/// while the item that was current at its commit stays current, the html page is what is on screen.
|
|
64
62
|
private var htmlNavigation: WKNavigation?
|
|
65
63
|
private var htmlCommit: (url: String, item: WKBackForwardListItem?)?
|
|
66
|
-
/// The
|
|
67
|
-
|
|
64
|
+
/// The loads this view starts itself (source, `load`, reload after an error, an allowed new window), each until its
|
|
65
|
+
/// main-frame policy check, which skips the rules, with `mainFrameChecks` at its start. A list: a load can start
|
|
66
|
+
/// before the previous one's check, and the page being left can commit after it. A commit drops the loads a later
|
|
67
|
+
/// check passed (cut off before their own), so a page can't later skip the rules by navigating to one.
|
|
68
|
+
var ownLoads: [(url: URL, checks: Int)] = []
|
|
69
|
+
var mainFrameChecks = 0
|
|
68
70
|
private(set) lazy var asks = Asks { [weak self] in self?.onHookEvent($0) }
|
|
69
71
|
/// The attached hooks' contexts in attach order, for the life of the WKWebView: a hook may keep its own.
|
|
70
72
|
var hookContexts: [HookContext] = []
|
|
71
73
|
/// URLs an `ask` allowed in this Document, keyed by mainFrame: a subframe's answer doesn't decide the main frame's.
|
|
72
74
|
var askedAndAllowed: [Bool: Set<String>] = [:]
|
|
73
|
-
/// A `source` load is being prepared (shared cookies): loads wait until it is done.
|
|
75
|
+
/// A `source` or `load` load is being prepared (shared cookies): loads wait until it is done.
|
|
74
76
|
private var preparing = false
|
|
77
|
+
/// The ref's `load` while it is prepared: it loads instead of the `source` being prepared, if any.
|
|
78
|
+
private var queuedUrl: URL?
|
|
79
|
+
/// The last `load` since the last `source`: a retry of it reads, as it did, only within a file's directory.
|
|
80
|
+
private var refLoad: URL?
|
|
75
81
|
/// After a failed main-frame load `url` is the failed URL (as on Android, which commits an error page);
|
|
76
82
|
/// WKWebView itself keeps the previous page, so `url` and reload() use this.
|
|
77
83
|
private(set) var failedUrl: String?
|
|
@@ -80,6 +86,8 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
80
86
|
private var lastEmitted: NSDictionary?
|
|
81
87
|
private var updateScheduled = false
|
|
82
88
|
private var crashTimes: [TimeInterval] = []
|
|
89
|
+
/// A web-process death whose reload waits until the view can be seen; a `load`, reload or new `source` replaces it.
|
|
90
|
+
private var recoveryPending = false
|
|
83
91
|
/// `evaluate` calls waiting for their result, by id, until a new document or a dead web process rejects them.
|
|
84
92
|
var evaluations: [Int: Promise] = [:]
|
|
85
93
|
var nextEvaluation = 0
|
|
@@ -98,6 +106,21 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
98
106
|
clipsToBounds = true
|
|
99
107
|
// Made here, not lazily in the deinit fallback's endDocument(.closing), where its closure can't form a weak self.
|
|
100
108
|
_ = asks
|
|
109
|
+
NotificationCenter.default.addObserver(
|
|
110
|
+
self, selector: #selector(recoverIfVisible), name: UIApplication.didBecomeActiveNotification, object: nil)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
override func didMoveToWindow() {
|
|
114
|
+
super.didMoveToWindow()
|
|
115
|
+
recoverIfVisible()
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/// Crash recovery reloads only in a window of an app not in the background, as on Android: there the reload
|
|
119
|
+
/// could die the same way, unseen.
|
|
120
|
+
@objc private func recoverIfVisible() {
|
|
121
|
+
guard recoveryPending, window != nil, UIApplication.shared.applicationState != .background else { return }
|
|
122
|
+
recoveryPending = false
|
|
123
|
+
loadAgain()
|
|
101
124
|
}
|
|
102
125
|
|
|
103
126
|
private var documentOpen = false
|
|
@@ -109,7 +132,8 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
109
132
|
failEvaluations()
|
|
110
133
|
bridgeOrigin = nil
|
|
111
134
|
// JS queues bridge messages until the next page is ready; at unmount it rejects what is in flight itself.
|
|
112
|
-
if reason
|
|
135
|
+
if reason == .committed, bridgeScript != nil { onBridge(["navigated": true]) }
|
|
136
|
+
if reason == .crashed, bridgeScript != nil { onBridge(["crashed": true]) }
|
|
113
137
|
askedAndAllowed.removeAll()
|
|
114
138
|
dismissDialog()
|
|
115
139
|
// The spinner belongs to a load, and a crash ends the load without a delegate callback.
|
|
@@ -130,11 +154,35 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
130
154
|
loadedSource = next.toDictionary() as NSDictionary
|
|
131
155
|
// New content from the app: the crash-loop guard starts over (recovery reloads don't come through here).
|
|
132
156
|
crashTimes = []
|
|
157
|
+
recoveryPending = false
|
|
158
|
+
prepare(next)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/// The ref's `load`. It is prepared as a `source` load is, and a later call replaces one still waiting.
|
|
162
|
+
func load(url: String) {
|
|
163
|
+
guard let target = URL(string: url) else { return warn("load: \(url) is not a URL") }
|
|
164
|
+
guard webView != nil else { return }
|
|
165
|
+
crashTimes = []
|
|
166
|
+
recoveryPending = false
|
|
167
|
+
queuedUrl = target
|
|
168
|
+
if !preparing { prepare(nil) }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private func prepare(_ source: WebViewSource?) {
|
|
133
172
|
preparing = true
|
|
134
173
|
cookies.prepare { [weak self, weak webView] in
|
|
135
174
|
guard let self, let webView, webView === self.webView else { return }
|
|
136
175
|
preparing = false
|
|
137
|
-
load(
|
|
176
|
+
// A load asked for while the source was prepared replaces it: started first, the source's request (a POST
|
|
177
|
+
// included) would be sent only to be cut off.
|
|
178
|
+
if let queuedUrl {
|
|
179
|
+
self.queuedUrl = nil
|
|
180
|
+
refLoad = queuedUrl
|
|
181
|
+
loadProgrammatic(URLRequest(url: queuedUrl), inSourceRoot: false)
|
|
182
|
+
} else if let source {
|
|
183
|
+
refLoad = nil
|
|
184
|
+
load(source)
|
|
185
|
+
}
|
|
138
186
|
// Props that changed while preparing (a newer source) apply now.
|
|
139
187
|
applyProps()
|
|
140
188
|
}
|
|
@@ -153,7 +201,10 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
153
201
|
}
|
|
154
202
|
loadProgrammatic(request)
|
|
155
203
|
} else {
|
|
156
|
-
|
|
204
|
+
let baseURL = source.baseUrl.flatMap(URL.init)
|
|
205
|
+
// WebKit puts the html load through decidePolicyFor at its base URL, about:blank without one.
|
|
206
|
+
if let own = baseURL ?? URL(string: "about:blank") { ownLoads.append((own, mainFrameChecks)) }
|
|
207
|
+
htmlNavigation = webView?.loadHTMLString(source.html ?? "", baseURL: baseURL)
|
|
157
208
|
}
|
|
158
209
|
}
|
|
159
210
|
|
|
@@ -175,12 +226,12 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
175
226
|
}
|
|
176
227
|
}
|
|
177
228
|
|
|
178
|
-
/// Loads a request the app started (see `
|
|
179
|
-
///
|
|
180
|
-
func loadProgrammatic(_ request: URLRequest) {
|
|
181
|
-
|
|
229
|
+
/// Loads a request the app started (see `ownLoads`). A file URL goes through loadFileURL, so the page's own assets
|
|
230
|
+
/// load (load(_:) can't read local files), with the source's read root or, outside it, the file's directory.
|
|
231
|
+
func loadProgrammatic(_ request: URLRequest, inSourceRoot: Bool = true) {
|
|
232
|
+
if let url = request.url { ownLoads.append((url, mainFrameChecks)) }
|
|
182
233
|
if let url = request.url, url.isFileURL {
|
|
183
|
-
let access = ReadAccess.root(for: url, source?.readAccessRoot)
|
|
234
|
+
let access = ReadAccess.root(for: url, inSourceRoot ? source?.readAccessRoot : nil)
|
|
184
235
|
if let warning = access.warning { warn(warning) }
|
|
185
236
|
webView?.loadFileURL(url, allowingReadAccessTo: access.root)
|
|
186
237
|
} else {
|
|
@@ -196,8 +247,11 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
196
247
|
}
|
|
197
248
|
|
|
198
249
|
func reload() {
|
|
199
|
-
|
|
200
|
-
|
|
250
|
+
recoveryPending = false
|
|
251
|
+
// WebKit's reload redoes no failed load, POST or html source (it fetches the base URL), and loads nothing while `url`
|
|
252
|
+
// is nil (no commit, a dead process).
|
|
253
|
+
let onHtmlSource = onSource && source?.uri == nil
|
|
254
|
+
if let webView, webView.url != nil, failedUrl == nil, !onPostSource, !onHtmlSource { webView.reload() } else { loadAgain() }
|
|
201
255
|
}
|
|
202
256
|
|
|
203
257
|
/// Loads the page on screen again, as crash recovery does. In order:
|
|
@@ -210,7 +264,7 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
210
264
|
if let source, onSource || failedUrl == nil && webView.backForwardList.currentItem == nil {
|
|
211
265
|
load(source)
|
|
212
266
|
} else if let failedUrl, let target = URL(string: failedUrl) {
|
|
213
|
-
loadProgrammatic(URLRequest(url: target))
|
|
267
|
+
loadProgrammatic(URLRequest(url: target), inSourceRoot: !(refLoad.map { Loads.sameURL($0, target) } ?? false))
|
|
214
268
|
} else {
|
|
215
269
|
webView.reload()
|
|
216
270
|
}
|
|
@@ -239,6 +293,7 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
239
293
|
}
|
|
240
294
|
let webView = webViewClass.init(frame: bounds, configuration: configuration)
|
|
241
295
|
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
296
|
+
proxy.hookDecides = { [weak self] in _ = self?.takeOwnLoad($0) }
|
|
242
297
|
webView.navigationDelegate = proxy
|
|
243
298
|
webView.uiDelegate = proxy
|
|
244
299
|
webView.addGestureRecognizer(taps)
|
|
@@ -405,7 +460,7 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
405
460
|
|
|
406
461
|
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
|
|
407
462
|
endDocument(.committed)
|
|
408
|
-
|
|
463
|
+
ownLoads.removeAll { $0.checks < mainFrameChecks }
|
|
409
464
|
failedUrl = nil
|
|
410
465
|
htmlCommit = navigation === htmlNavigation
|
|
411
466
|
? (webView.url?.absoluteString ?? "about:blank", webView.backForwardList.currentItem) : nil
|
|
@@ -440,7 +495,10 @@ class MinimalWebView: ExpoView, WKNavigationDelegate {
|
|
|
440
495
|
let recovered = Recovery.crashAllowsReload(&crashTimes, now: ProcessInfo.processInfo.systemUptime) && !onPostSource
|
|
441
496
|
endDocument(.crashed)
|
|
442
497
|
if lastEmitted?["loading"] as? Bool == true { outcome = "failed" } // the load the crash cut off
|
|
443
|
-
if recovered {
|
|
498
|
+
if recovered {
|
|
499
|
+
recoveryPending = true
|
|
500
|
+
recoverIfVisible()
|
|
501
|
+
}
|
|
444
502
|
onProcessGone(["cause": "unknown", "recovered": recovered])
|
|
445
503
|
forward { $0.webViewWebContentProcessDidTerminate?(webView) }
|
|
446
504
|
}
|
|
@@ -489,16 +547,7 @@ extension MinimalWebView {
|
|
|
489
547
|
webView?.navigationDelegate = proxy
|
|
490
548
|
webView?.uiDelegate = nil
|
|
491
549
|
webView?.uiDelegate = proxy
|
|
492
|
-
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
private func warnDecisions() {
|
|
496
|
-
for name in proxy.sharedDecisions where warnedDecisions.insert(name).inserted {
|
|
497
|
-
warn("\(name): more than one hook delegate implements it; only the first one gets it")
|
|
498
|
-
}
|
|
499
|
-
for (name, lost) in proxy.takenDecisions where warnedDecisions.insert("\(name) taken").inserted {
|
|
500
|
-
warn("\(name): a hook delegate takes it, so \(lost)")
|
|
501
|
-
}
|
|
550
|
+
proxy.newWarnings().forEach(warn)
|
|
502
551
|
}
|
|
503
552
|
|
|
504
553
|
func emit(_ key: String, _ name: String, _ payload: [String: Any]) {
|
|
@@ -29,6 +29,7 @@ public class MinimalWebViewModule: Module {
|
|
|
29
29
|
|
|
30
30
|
AsyncFunction("goBack") { (view: MinimalWebView) in view.webView?.goBack() }
|
|
31
31
|
AsyncFunction("goForward") { (view: MinimalWebView) in view.webView?.goForward() }
|
|
32
|
+
AsyncFunction("load") { (view: MinimalWebView, url: String) in view.load(url: url) }
|
|
32
33
|
AsyncFunction("reload") { (view: MinimalWebView) in view.retry() }
|
|
33
34
|
AsyncFunction("stopLoading") { (view: MinimalWebView) in view.webView?.stopLoading() }
|
|
34
35
|
AsyncFunction("get") { (view: MinimalWebView, path: String, promise: Promise) in
|
|
@@ -14,9 +14,11 @@ enum NavigationRules {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
enum Action: String { case allow, block, external, ask }
|
|
18
|
+
|
|
19
|
+
/// `rule` is the matching rule's index, nil when a default decided.
|
|
18
20
|
struct Decision: Equatable {
|
|
19
|
-
let action:
|
|
21
|
+
let action: Action
|
|
20
22
|
let rule: Int?
|
|
21
23
|
}
|
|
22
24
|
|
|
@@ -33,36 +35,34 @@ enum NavigationRules {
|
|
|
33
35
|
/// Never open as a new window, whatever the rules say: the core loads an allowed new window in the main frame itself,
|
|
34
36
|
/// past the engine's limits on what a page may navigate the top frame to.
|
|
35
37
|
private static let noNewWindowSchemes = internalSchemes.union(["blob", "file"])
|
|
36
|
-
private static let actions: Set<String> = ["allow", "block", "external", "ask"]
|
|
37
|
-
private static let answers: Set<String> = ["allow", "block", "external"]
|
|
38
38
|
|
|
39
39
|
/// The rules' decision, gated for `ask`: blocked when nothing can answer it (`canAsk` false), allowed when its URL is
|
|
40
40
|
/// in `asked` (URLs an `ask` already allowed). An unknown rule action blocks.
|
|
41
41
|
static func decide(_ rules: [[String: Any]]?, _ request: Request, asked: Set<String>, canAsk: Bool) -> Decision {
|
|
42
42
|
let decision = match(rules, request)
|
|
43
|
-
guard decision.action ==
|
|
44
|
-
if !canAsk { return Decision(action:
|
|
45
|
-
return asked.contains(request.url) ? Decision(action:
|
|
43
|
+
guard decision.action == .ask else { return decision }
|
|
44
|
+
if !canAsk { return Decision(action: .block, rule: decision.rule) }
|
|
45
|
+
return asked.contains(request.url) ? Decision(action: .allow, rule: decision.rule) : decision
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
/// An `ask` answer: allow, block or external; anything else (nil on a timeout) blocks.
|
|
49
|
-
static func answer(_ value: Any?) ->
|
|
50
|
-
(value as? String).flatMap
|
|
49
|
+
static func answer(_ value: Any?) -> Action {
|
|
50
|
+
guard let action = (value as? String).flatMap(Action.init), action != .ask else { return .block }
|
|
51
|
+
return action
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
private static func match(_ rules: [[String: Any]]?, _ request: Request) -> Decision {
|
|
54
55
|
let scheme = request.url.firstIndex(of: ":").map { request.url[..<$0].lowercased() } ?? ""
|
|
55
|
-
if request.newWindow, noNewWindowSchemes.contains(scheme) { return Decision(action:
|
|
56
|
-
if internalSchemes.contains(scheme) { return Decision(action:
|
|
56
|
+
if request.newWindow, noNewWindowSchemes.contains(scheme) { return Decision(action: .block, rule: nil) }
|
|
57
|
+
if internalSchemes.contains(scheme) { return Decision(action: .allow, rule: nil) }
|
|
57
58
|
// JS sends defaultNavigationRules when the prop is unset; no rules, nil or empty, match nothing.
|
|
58
59
|
guard let rules, !rules.isEmpty,
|
|
59
60
|
let parts = scheme == "blob" ? blobOrigin(request.url) : urlParts(scheme, request.url)
|
|
60
|
-
else { return Decision(action:
|
|
61
|
+
else { return Decision(action: .block, rule: nil) }
|
|
61
62
|
guard let index = rules.firstIndex(where: { matches($0, parts, request) }) else {
|
|
62
|
-
return Decision(action:
|
|
63
|
+
return Decision(action: .block, rule: nil)
|
|
63
64
|
}
|
|
64
|
-
|
|
65
|
-
return Decision(action: action.flatMap { actions.contains($0) ? $0 : nil } ?? "block", rule: index)
|
|
65
|
+
return Decision(action: (rules[index]["action"] as? String).flatMap(Action.init) ?? .block, rule: index)
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
private static func urlParts(_ scheme: String, _ url: String) -> Parts {
|
|
@@ -186,7 +186,6 @@ final class PlatformSettings {
|
|
|
186
186
|
let isBool = CFGetTypeID(value as CFTypeRef) == CFBooleanGetTypeID()
|
|
187
187
|
let number = isBool ? nil : value as? NSNumber
|
|
188
188
|
let string = value as? String
|
|
189
|
-
let object = value as? [String: Any]
|
|
190
189
|
let isNull = value is NSNull
|
|
191
190
|
switch type {
|
|
192
191
|
case "B", "c":
|
|
@@ -207,18 +206,8 @@ final class PlatformSettings {
|
|
|
207
206
|
case "nonPersistent": return WKWebsiteDataStore.nonPersistent()
|
|
208
207
|
default: return nil
|
|
209
208
|
}
|
|
210
|
-
case "{
|
|
211
|
-
|
|
212
|
-
return NSValue(uiEdgeInsets: UIEdgeInsets(top: t, left: l, bottom: b, right: r))
|
|
213
|
-
case "{CGPoint=dd}":
|
|
214
|
-
guard let o = object, let x = o["x"] as? Double, let y = o["y"] as? Double else { return nil }
|
|
215
|
-
return NSValue(cgPoint: CGPoint(x: x, y: y))
|
|
216
|
-
case "{CGSize=dd}":
|
|
217
|
-
guard let o = object, let w = o["width"] as? Double, let h = o["height"] as? Double else { return nil }
|
|
218
|
-
return NSValue(cgSize: CGSize(width: w, height: h))
|
|
219
|
-
case "{CGRect={CGPoint=dd}{CGSize=dd}}":
|
|
220
|
-
guard let o = object, let x = o["x"] as? Double, let y = o["y"] as? Double, let w = o["width"] as? Double, let h = o["height"] as? Double else { return nil }
|
|
221
|
-
return NSValue(cgRect: CGRect(x: x, y: y, width: w, height: h))
|
|
209
|
+
case _ where type.hasPrefix("{"):
|
|
210
|
+
return __MRWStructValue(type, value)
|
|
222
211
|
default:
|
|
223
212
|
return nil
|
|
224
213
|
}
|
package/ios/SharedCookies.swift
CHANGED
|
@@ -9,10 +9,9 @@ final class SharedCookies: NSObject, WKHTTPCookieStoreObserver {
|
|
|
9
9
|
/// The WebView's cookies at the last copy: one gone from it since is deleted from native too.
|
|
10
10
|
private var copied: [HTTPCookie] = []
|
|
11
11
|
|
|
12
|
-
/// Once
|
|
12
|
+
/// Once, before the first `set`: the view's one WKWebView, which a crash keeps.
|
|
13
13
|
func didCreate(_ webView: WKWebView) {
|
|
14
14
|
store = webView.configuration.websiteDataStore.httpCookieStore
|
|
15
|
-
if enabled { store?.add(self) }
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
func set(_ enabled: Bool) {
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// jest-expo's stand-in for the native module in an app's tests: it loads mocks/<native module name> by itself.
|
|
2
|
+
export const defaultUserAgent = 'Mozilla/5.0';
|
|
3
|
+
export function warmUp() {}
|
|
4
|
+
export async function preconnect(_url: string) {}
|
|
5
|
+
export async function setWebContentsDebuggingEnabled(_enabled: boolean) {}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-minimal-webview",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "A React Native WebView that hands you the iOS and Android WebView APIs, instead of wrapping each one in a prop",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"android/build.gradle",
|
|
22
22
|
"android/consumer-rules.pro",
|
|
23
23
|
"android/src/main",
|
|
24
|
-
"expo-module.config.json"
|
|
24
|
+
"expo-module.config.json",
|
|
25
|
+
"mocks"
|
|
25
26
|
],
|
|
26
27
|
"scripts": {
|
|
27
28
|
"build": "tsc && tsc -p extensions",
|
|
@@ -30,7 +31,7 @@
|
|
|
30
31
|
"test": "jest",
|
|
31
32
|
"lint": "eslint --max-warnings 0 src extensions example .github && prettier --check .",
|
|
32
33
|
"prepare": "npm run clean && npm run build",
|
|
33
|
-
"check": "npm test && npm run lint && knip --config .config/knip.json && tsc --noEmit -p src/__tests__/tsconfig.json && tsc --noEmit -p extensions/src/__tests__/tsconfig.json && (cd example && npx tsc --noEmit) && node .github/scripts/check-doc-samples.mts && npm run prepare && npm run docs:check && tsc --noEmit -p .github/scripts && tsc --noEmit -p example/maestro/scripts",
|
|
34
|
+
"check": "npm test && npm run lint && knip --config .config/knip.json && tsc --noEmit -p src/__tests__/tsconfig.json && tsc --noEmit -p extensions/src/__tests__/tsconfig.json && (cd example && npx tsc --noEmit) && node .github/scripts/check-doc-samples.mts && node .github/scripts/check-expo-set.mts && npm run prepare && npm run docs:check && tsc --noEmit -p .github/scripts && tsc --noEmit -p example/maestro/scripts",
|
|
34
35
|
"mutation": "stryker run .config/stryker.config.json",
|
|
35
36
|
"build:page-client": "node .github/scripts/build-page-client.mts",
|
|
36
37
|
"gen:platform-types": "node .github/scripts/gen-platform-types.mts",
|
|
@@ -57,8 +58,8 @@
|
|
|
57
58
|
"homepage": "https://github.com/RasmusKard/react-native-minimal-webview#readme",
|
|
58
59
|
"devDependencies": {
|
|
59
60
|
"@babel/core": "^7.29.13",
|
|
60
|
-
"@rn-cross-platform-docs/cli": "2.0.
|
|
61
|
-
"@rn-cross-platform-docs/zensical": "2.0.
|
|
61
|
+
"@rn-cross-platform-docs/cli": "2.0.2",
|
|
62
|
+
"@rn-cross-platform-docs/zensical": "2.0.2",
|
|
62
63
|
"@stryker-mutator/core": "10.1.1",
|
|
63
64
|
"@stryker-mutator/jest-runner": "10.1.1",
|
|
64
65
|
"@types/jest": "^30.0.0",
|
|
@@ -67,16 +68,16 @@
|
|
|
67
68
|
"eslint": "~9.39.4",
|
|
68
69
|
"eslint-config-universe": "^16.1.2",
|
|
69
70
|
"eslint-plugin-sonarjs": "4.2.2",
|
|
70
|
-
"expo": "~58.0.
|
|
71
|
+
"expo": "~58.0.26",
|
|
71
72
|
"globals": "^17.14.0",
|
|
72
73
|
"jest": "^30.0.0",
|
|
73
|
-
"jest-expo": "~58.0.
|
|
74
|
+
"jest-expo": "~58.0.15",
|
|
74
75
|
"knip": "6.45.1",
|
|
75
76
|
"prettier": "^3.10.1",
|
|
76
77
|
"react": "19.3.0",
|
|
77
78
|
"react-native": "0.88.1",
|
|
78
79
|
"test-renderer": "1.3.1",
|
|
79
|
-
"tsdoc-defaults": "^1.0.
|
|
80
|
+
"tsdoc-defaults": "^1.0.3",
|
|
80
81
|
"typedoc": "0.28.20",
|
|
81
82
|
"typedoc-plugin-markdown": "4.14.1",
|
|
82
83
|
"typescript": "~6.0.3"
|