@react-native-ohos/react-native-webview 13.15.1-rc.1 → 13.15.1-rc.4
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/LICENSE +20 -20
- package/README.OpenSource +10 -10
- package/README.md +12 -12
- package/harmony/rn_webview/OAT.xml +44 -44
- package/harmony/rn_webview/build-profile.json5 +28 -28
- package/harmony/rn_webview/index.ets +12 -12
- package/harmony/rn_webview/oh-package.json5 +13 -13
- package/harmony/rn_webview/src/main/cpp/CMakeLists.txt +9 -9
- package/harmony/rn_webview/src/main/cpp/WebViewPackage.h +16 -16
- package/harmony/rn_webview/src/main/cpp/generated/RNOH/generated/BaseReactNativeWebviewPackage.h +95 -95
- package/harmony/rn_webview/src/main/cpp/generated/RNOH/generated/components/RNCWebViewJSIBinder.h +119 -119
- package/harmony/rn_webview/src/main/cpp/generated/RNOH/generated/turbo_modules/RNCWebView.cpp +18 -18
- package/harmony/rn_webview/src/main/cpp/generated/RNOH/generated/turbo_modules/RNCWebView.h +16 -16
- package/harmony/rn_webview/src/main/cpp/generated/RNOH/generated/turbo_modules/RNCWebViewModule.cpp +17 -17
- package/harmony/rn_webview/src/main/cpp/generated/RNOH/generated/turbo_modules/RNCWebViewModule.h +16 -16
- package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/ComponentDescriptors.h +24 -24
- package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/EventEmitters.cpp +241 -241
- package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/EventEmitters.h +263 -263
- package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/Props.cpp +103 -103
- package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/Props.h +509 -509
- package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/ShadowNodes.cpp +17 -17
- package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/ShadowNodes.h +32 -32
- package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/States.cpp +16 -16
- package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/States.h +28 -28
- package/harmony/rn_webview/src/main/ets/CutomReference.ts +29 -29
- package/harmony/rn_webview/src/main/ets/Logger.ts +43 -43
- package/harmony/rn_webview/src/main/ets/Magic.ets +184 -184
- package/harmony/rn_webview/src/main/ets/RNCWebView.ets +645 -570
- package/harmony/rn_webview/src/main/ets/RNCWebViewPackage.ets +37 -37
- package/harmony/rn_webview/src/main/ets/ShouldRequestUrl.ts +47 -47
- package/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets +457 -443
- package/harmony/rn_webview/src/main/ets/WebViewTurboModule.ets +57 -57
- package/harmony/rn_webview/src/main/ets/generated/components/RNCWebView.ts +524 -524
- package/harmony/rn_webview/src/main/ets/generated/components/ts.ts +5 -5
- package/harmony/rn_webview/src/main/ets/generated/index.ets +5 -5
- package/harmony/rn_webview/src/main/ets/generated/ts.ts +6 -6
- package/harmony/rn_webview/src/main/ets/generated/turboModules/RNCWebView.ts +18 -18
- package/harmony/rn_webview/src/main/ets/generated/turboModules/RNCWebViewModule.ts +16 -16
- package/harmony/rn_webview/src/main/ets/generated/turboModules/ts.ts +6 -6
- package/harmony/rn_webview/src/main/module.json5 +10 -10
- package/harmony/rn_webview/src/main/resources/base/element/string.json +39 -39
- package/harmony/rn_webview/src/main/resources/en_US/element/string.json +39 -39
- package/harmony/rn_webview/src/main/resources/zh_CN/element/string.json +39 -39
- package/harmony/rn_webview/src/test/List.test.ets +4 -4
- package/harmony/rn_webview/src/test/LocalUnit.test.ets +32 -32
- package/harmony/rn_webview/ts.ets +8 -8
- package/harmony/rn_webview.har +0 -0
- package/package.json +99 -99
- package/src/NativeRNCWebView.ts +21 -21
- package/src/NativeRNCWebViewModule.ts +19 -19
- package/src/RNCWebViewNativeComponent.ts +354 -354
- package/src/WebView.harmony.tsx +342 -342
- package/src/WebView.tsx +10 -10
- package/src/codegenUtils.ts +10 -10
- package/src/index.ts +10 -10
- package/harmony/rn_webview/BuildProfile.ets +0 -17
package/src/WebView.harmony.tsx
CHANGED
|
@@ -1,343 +1,343 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
-
* Use of this source code is governed by a MIT license that can be
|
|
4
|
-
* found in the LICENSE file.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import React, {
|
|
8
|
-
forwardRef,
|
|
9
|
-
useCallback,
|
|
10
|
-
useImperativeHandle,
|
|
11
|
-
useRef
|
|
12
|
-
} from 'react';
|
|
13
|
-
import { Image, View, ImageSourcePropType, HostComponent } from 'react-native';
|
|
14
|
-
import invariant from 'invariant';
|
|
15
|
-
|
|
16
|
-
import RNCWebView, { Commands, NativeProps } from './RNCWebViewNativeComponent';
|
|
17
|
-
import RNCWebViewModule from './NativeRNCWebViewModule';
|
|
18
|
-
|
|
19
|
-
import {
|
|
20
|
-
defaultOriginWhitelist,
|
|
21
|
-
defaultRenderError,
|
|
22
|
-
defaultRenderLoading,
|
|
23
|
-
useWebViewLogic,
|
|
24
|
-
} from 'react-native-webview/src/WebViewShared';
|
|
25
|
-
import {
|
|
26
|
-
IOSWebViewProps,
|
|
27
|
-
DecelerationRateConstant,
|
|
28
|
-
WebViewSourceUri,
|
|
29
|
-
} from 'react-native-webview/src/WebViewTypes';
|
|
30
|
-
|
|
31
|
-
import styles from 'react-native-webview/src/WebView.styles';
|
|
32
|
-
const { resolveAssetSource } = Image;
|
|
33
|
-
const processDecelerationRate = (
|
|
34
|
-
decelerationRate: DecelerationRateConstant | number | undefined
|
|
35
|
-
) => {
|
|
36
|
-
let newDecelerationRate = decelerationRate;
|
|
37
|
-
if (newDecelerationRate === 'normal') {
|
|
38
|
-
newDecelerationRate = 0.998;
|
|
39
|
-
} else if (newDecelerationRate === 'fast') {
|
|
40
|
-
newDecelerationRate = 0.99;
|
|
41
|
-
}
|
|
42
|
-
return newDecelerationRate;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const useWarnIfChanges = <T extends unknown>(value: T, name: string) => {
|
|
46
|
-
const ref = useRef(value);
|
|
47
|
-
if (ref.current !== value) {
|
|
48
|
-
console.warn(
|
|
49
|
-
`Changes to property ${name} do nothing after the initial render.`
|
|
50
|
-
);
|
|
51
|
-
ref.current = value;
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const WebViewComponent = forwardRef<{}, IOSWebViewProps & { scalesPageToFit: boolean, minimumFontSize: number, thirdPartyCookiesEnabled: boolean, geolocationEnabled: boolean }>(
|
|
56
|
-
(
|
|
57
|
-
{
|
|
58
|
-
fraudulentWebsiteWarningEnabled = true,
|
|
59
|
-
javaScriptEnabled = true,
|
|
60
|
-
cacheEnabled = true,
|
|
61
|
-
originWhitelist = defaultOriginWhitelist,
|
|
62
|
-
applicationNameForUserAgent,
|
|
63
|
-
ignoreSilentHardwareSwitch,
|
|
64
|
-
minimumFontSize,
|
|
65
|
-
useSharedProcessPool = true,
|
|
66
|
-
textInteractionEnabled = true,
|
|
67
|
-
injectedJavaScript,
|
|
68
|
-
injectedJavaScriptBeforeContentLoaded,
|
|
69
|
-
injectedJavaScriptObject,
|
|
70
|
-
menuItems=[],
|
|
71
|
-
injectedJavaScriptForMainFrameOnly = true,
|
|
72
|
-
injectedJavaScriptBeforeContentLoadedForMainFrameOnly = true,
|
|
73
|
-
thirdPartyCookiesEnabled = false,
|
|
74
|
-
geolocationEnabled = false,
|
|
75
|
-
webviewDebuggingEnabled = false,
|
|
76
|
-
paymentRequestEnabled = false,
|
|
77
|
-
startInLoadingState,
|
|
78
|
-
onNavigationStateChange,
|
|
79
|
-
onLoadStart,
|
|
80
|
-
onCustomMenuSelection,
|
|
81
|
-
onError,
|
|
82
|
-
onLoad,
|
|
83
|
-
onLoadEnd,
|
|
84
|
-
onLoadProgress,
|
|
85
|
-
onScroll,
|
|
86
|
-
onContentProcessDidTerminate: onContentProcessDidTerminateProp,
|
|
87
|
-
onFileDownload,
|
|
88
|
-
onHttpError: onHttpErrorProp,
|
|
89
|
-
onMessage: onMessageProp,
|
|
90
|
-
onOpenWindow: onOpenWindowProp,
|
|
91
|
-
renderLoading,
|
|
92
|
-
renderError,
|
|
93
|
-
style,
|
|
94
|
-
containerStyle,
|
|
95
|
-
source,
|
|
96
|
-
nativeConfig,
|
|
97
|
-
allowsInlineMediaPlayback,
|
|
98
|
-
allowsPictureInPictureMediaPlayback,
|
|
99
|
-
allowsAirPlayForMediaPlayback,
|
|
100
|
-
mediaPlaybackRequiresUserAction=true,
|
|
101
|
-
dataDetectorTypes,
|
|
102
|
-
incognito,
|
|
103
|
-
decelerationRate: decelerationRateProp,
|
|
104
|
-
onShouldStartLoadWithRequest: onShouldStartLoadWithRequestProp,
|
|
105
|
-
onStartShouldSetResponder,
|
|
106
|
-
onMoveShouldSetResponderCapture,
|
|
107
|
-
pullToRefreshEnabled=false,
|
|
108
|
-
refreshControlLightMode=false,
|
|
109
|
-
onResponderMove,
|
|
110
|
-
...otherProps
|
|
111
|
-
},
|
|
112
|
-
ref
|
|
113
|
-
) => {
|
|
114
|
-
const webViewRef = useRef<React.ComponentRef<
|
|
115
|
-
HostComponent<NativeProps>
|
|
116
|
-
> | null>(null);
|
|
117
|
-
|
|
118
|
-
const onShouldStartLoadWithRequestCallback = useCallback(
|
|
119
|
-
(shouldStart: boolean, _url: string, lockIdentifier = 0) => {
|
|
120
|
-
RNCWebViewModule.shouldStartLoadWithLockIdentifier(
|
|
121
|
-
shouldStart,
|
|
122
|
-
lockIdentifier
|
|
123
|
-
);
|
|
124
|
-
},
|
|
125
|
-
[]
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
const {
|
|
129
|
-
onLoadingStart,
|
|
130
|
-
onShouldStartLoadWithRequest,
|
|
131
|
-
onMessage,
|
|
132
|
-
viewState,
|
|
133
|
-
setViewState,
|
|
134
|
-
lastErrorEvent,
|
|
135
|
-
onHttpError,
|
|
136
|
-
onLoadingError,
|
|
137
|
-
onLoadingFinish,
|
|
138
|
-
onLoadingProgress,
|
|
139
|
-
onOpenWindow,
|
|
140
|
-
onContentProcessDidTerminate,
|
|
141
|
-
} = useWebViewLogic({
|
|
142
|
-
onNavigationStateChange,
|
|
143
|
-
onLoad,
|
|
144
|
-
onError,
|
|
145
|
-
onHttpErrorProp,
|
|
146
|
-
onLoadEnd,
|
|
147
|
-
onLoadProgress,
|
|
148
|
-
// @ts-expect-error old arch only
|
|
149
|
-
onScroll,
|
|
150
|
-
onLoadStart,
|
|
151
|
-
onMessageProp,
|
|
152
|
-
onOpenWindowProp,
|
|
153
|
-
startInLoadingState,
|
|
154
|
-
originWhitelist,
|
|
155
|
-
ignoreSilentHardwareSwitch,
|
|
156
|
-
allowsPictureInPictureMediaPlayback,
|
|
157
|
-
minimumFontSize,
|
|
158
|
-
onShouldStartLoadWithRequestProp,
|
|
159
|
-
onShouldStartLoadWithRequestCallback,
|
|
160
|
-
onContentProcessDidTerminateProp,
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
useImperativeHandle(
|
|
164
|
-
ref,
|
|
165
|
-
() => ({
|
|
166
|
-
goForward: () =>
|
|
167
|
-
webViewRef.current && Commands.goForward(webViewRef.current),
|
|
168
|
-
goBack: () => webViewRef.current && Commands.goBack(webViewRef.current),
|
|
169
|
-
reload: () => {
|
|
170
|
-
setViewState('LOADING');
|
|
171
|
-
if (webViewRef.current) {
|
|
172
|
-
Commands.reload(webViewRef.current);
|
|
173
|
-
}
|
|
174
|
-
},
|
|
175
|
-
stopLoading: () =>
|
|
176
|
-
webViewRef.current && Commands.stopLoading(webViewRef.current),
|
|
177
|
-
postMessage: (data: string) =>
|
|
178
|
-
webViewRef.current && Commands.postMessage(webViewRef.current, data),
|
|
179
|
-
injectJavaScript: (data: string) =>
|
|
180
|
-
webViewRef.current &&
|
|
181
|
-
Commands.injectJavaScript(webViewRef.current, data),
|
|
182
|
-
requestFocus: () =>
|
|
183
|
-
webViewRef.current && Commands.requestFocus(webViewRef.current),
|
|
184
|
-
clearCache: (includeDiskFiles: boolean) =>
|
|
185
|
-
webViewRef.current &&
|
|
186
|
-
Commands.clearCache(webViewRef.current, includeDiskFiles),
|
|
187
|
-
clearHistory: () => webViewRef.current && Commands.clearHistory(webViewRef.current),
|
|
188
|
-
loadUrl: (data: string) => webViewRef.current && Commands.loadUrl(webViewRef.current, data),
|
|
189
|
-
}),
|
|
190
|
-
[setViewState, webViewRef]
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
useWarnIfChanges(allowsInlineMediaPlayback, 'allowsInlineMediaPlayback');
|
|
194
|
-
useWarnIfChanges(
|
|
195
|
-
allowsPictureInPictureMediaPlayback,
|
|
196
|
-
'allowsPictureInPictureMediaPlayback'
|
|
197
|
-
);
|
|
198
|
-
useWarnIfChanges(
|
|
199
|
-
allowsAirPlayForMediaPlayback,
|
|
200
|
-
'allowsAirPlayForMediaPlayback'
|
|
201
|
-
);
|
|
202
|
-
useWarnIfChanges(incognito, 'incognito');
|
|
203
|
-
useWarnIfChanges(
|
|
204
|
-
mediaPlaybackRequiresUserAction,
|
|
205
|
-
'mediaPlaybackRequiresUserAction'
|
|
206
|
-
);
|
|
207
|
-
useWarnIfChanges(dataDetectorTypes, 'dataDetectorTypes');
|
|
208
|
-
|
|
209
|
-
let otherView = null;
|
|
210
|
-
if (viewState === 'LOADING') {
|
|
211
|
-
otherView = (renderLoading || defaultRenderLoading)();
|
|
212
|
-
} else if (viewState === 'ERROR') {
|
|
213
|
-
invariant(
|
|
214
|
-
lastErrorEvent != null,
|
|
215
|
-
'lastErrorEvent expected to be non-null'
|
|
216
|
-
);
|
|
217
|
-
otherView = (renderError || defaultRenderError)(
|
|
218
|
-
lastErrorEvent?.domain,
|
|
219
|
-
lastErrorEvent?.code ?? 0,
|
|
220
|
-
lastErrorEvent?.description ?? ''
|
|
221
|
-
);
|
|
222
|
-
} else if (viewState !== 'IDLE') {
|
|
223
|
-
console.error(`RNCWebView invalid state encountered: ${viewState}`);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
const webViewStyles = [styles.container, styles.webView, style];
|
|
227
|
-
const webViewContainerStyle = [styles.container, containerStyle];
|
|
228
|
-
|
|
229
|
-
const decelerationRate = processDecelerationRate(decelerationRateProp);
|
|
230
|
-
|
|
231
|
-
const NativeWebView =
|
|
232
|
-
(nativeConfig?.component as typeof RNCWebView | undefined) || RNCWebView;
|
|
233
|
-
|
|
234
|
-
const sourceResolved = resolveAssetSource(source as ImageSourcePropType);
|
|
235
|
-
const newSource =
|
|
236
|
-
typeof sourceResolved === 'object'
|
|
237
|
-
? Object.entries(sourceResolved as WebViewSourceUri).reduce(
|
|
238
|
-
(prev, [currKey, currValue]) => {
|
|
239
|
-
return {
|
|
240
|
-
...prev,
|
|
241
|
-
[currKey]:
|
|
242
|
-
currKey === 'headers' &&
|
|
243
|
-
currValue &&
|
|
244
|
-
typeof currValue === 'object'
|
|
245
|
-
? Object.entries(currValue).map(([key, value]) => {
|
|
246
|
-
return {
|
|
247
|
-
name: key,
|
|
248
|
-
value,
|
|
249
|
-
};
|
|
250
|
-
})
|
|
251
|
-
: currValue,
|
|
252
|
-
};
|
|
253
|
-
},
|
|
254
|
-
{}
|
|
255
|
-
)
|
|
256
|
-
: sourceResolved;
|
|
257
|
-
|
|
258
|
-
const webView = (
|
|
259
|
-
<NativeWebView
|
|
260
|
-
key="webViewKey"
|
|
261
|
-
{...otherProps}
|
|
262
|
-
scrollEnabled={otherProps.scrollEnabled ?? true}
|
|
263
|
-
ignoreSilentHardwareSwitch={ignoreSilentHardwareSwitch}
|
|
264
|
-
minimumFontSize={minimumFontSize}
|
|
265
|
-
scalesPageToFit={otherProps.scalesPageToFit ?? true}
|
|
266
|
-
fraudulentWebsiteWarningEnabled={fraudulentWebsiteWarningEnabled}
|
|
267
|
-
javaScriptEnabled={javaScriptEnabled}
|
|
268
|
-
onCustomMenuSelection={onCustomMenuSelection}
|
|
269
|
-
applicationNameForUserAgent={applicationNameForUserAgent}
|
|
270
|
-
cacheEnabled={cacheEnabled}
|
|
271
|
-
useSharedProcessPool={useSharedProcessPool}
|
|
272
|
-
textInteractionEnabled={textInteractionEnabled}
|
|
273
|
-
geolocationEnabled={geolocationEnabled}
|
|
274
|
-
decelerationRate={decelerationRate}
|
|
275
|
-
messagingEnabled={typeof onMessageProp === 'function'}
|
|
276
|
-
messagingModuleName="" // android ONLY
|
|
277
|
-
onLoadingError={onLoadingError}
|
|
278
|
-
onLoadingFinish={onLoadingFinish}
|
|
279
|
-
onLoadingProgress={onLoadingProgress}
|
|
280
|
-
onScroll={onScroll}
|
|
281
|
-
onFileDownload={onFileDownload}
|
|
282
|
-
onLoadingStart={onLoadingStart}
|
|
283
|
-
onHttpError={onHttpError}
|
|
284
|
-
onMessage={onMessage}
|
|
285
|
-
onOpenWindow={onOpenWindowProp && onOpenWindow}
|
|
286
|
-
hasOnOpenWindowEvent={onOpenWindowProp !== undefined}
|
|
287
|
-
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
|
|
288
|
-
onContentProcessDidTerminate={onContentProcessDidTerminate}
|
|
289
|
-
thirdPartyCookiesEnabled={thirdPartyCookiesEnabled}
|
|
290
|
-
pullToRefreshEnabled={pullToRefreshEnabled}
|
|
291
|
-
refreshControlLightMode={refreshControlLightMode}
|
|
292
|
-
injectedJavaScript={injectedJavaScript}
|
|
293
|
-
injectedJavaScriptBeforeContentLoaded={
|
|
294
|
-
injectedJavaScriptBeforeContentLoaded
|
|
295
|
-
}
|
|
296
|
-
injectedJavaScriptForMainFrameOnly={injectedJavaScriptForMainFrameOnly}
|
|
297
|
-
injectedJavaScriptBeforeContentLoadedForMainFrameOnly={
|
|
298
|
-
injectedJavaScriptBeforeContentLoadedForMainFrameOnly
|
|
299
|
-
}
|
|
300
|
-
injectedJavaScriptObject={JSON.stringify(injectedJavaScriptObject)}
|
|
301
|
-
dataDetectorTypes={
|
|
302
|
-
!dataDetectorTypes || Array.isArray(dataDetectorTypes)
|
|
303
|
-
? dataDetectorTypes
|
|
304
|
-
: [dataDetectorTypes]
|
|
305
|
-
}
|
|
306
|
-
allowsAirPlayForMediaPlayback={allowsAirPlayForMediaPlayback}
|
|
307
|
-
allowsPictureInPictureMediaPlayback={allowsPictureInPictureMediaPlayback}
|
|
308
|
-
allowsInlineMediaPlayback={allowsInlineMediaPlayback}
|
|
309
|
-
incognito={incognito}
|
|
310
|
-
mediaPlaybackRequiresUserAction={mediaPlaybackRequiresUserAction}
|
|
311
|
-
menuItems={menuItems}
|
|
312
|
-
newSource={newSource}
|
|
313
|
-
style={webViewStyles}
|
|
314
|
-
webviewDebuggingEnabled={webviewDebuggingEnabled}
|
|
315
|
-
paymentRequestEnabled={paymentRequestEnabled}
|
|
316
|
-
hasOnFileDownload={!!onFileDownload}
|
|
317
|
-
ref={webViewRef}
|
|
318
|
-
// @ts-expect-error old arch only
|
|
319
|
-
source={sourceResolved}
|
|
320
|
-
{...nativeConfig?.props}
|
|
321
|
-
/>
|
|
322
|
-
);
|
|
323
|
-
|
|
324
|
-
return (
|
|
325
|
-
<View
|
|
326
|
-
style={webViewContainerStyle}
|
|
327
|
-
onStartShouldSetResponder={(e) => onStartShouldSetResponder ? onStartShouldSetResponder(e) : false}
|
|
328
|
-
onMoveShouldSetResponderCapture={(e) => onMoveShouldSetResponderCapture ? onMoveShouldSetResponderCapture(e) : false}
|
|
329
|
-
onResponderMove={(e) => onResponderMove ? onResponderMove(e) : null}
|
|
330
|
-
>
|
|
331
|
-
{webView}
|
|
332
|
-
{otherView}
|
|
333
|
-
</View>
|
|
334
|
-
);
|
|
335
|
-
}
|
|
336
|
-
);
|
|
337
|
-
|
|
338
|
-
// no native implementation for iOS, depends only on permissions
|
|
339
|
-
const isFileUploadSupported: () => Promise<boolean> = async () => true;
|
|
340
|
-
|
|
341
|
-
const WebView = Object.assign(WebViewComponent, { isFileUploadSupported });
|
|
342
|
-
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import React, {
|
|
8
|
+
forwardRef,
|
|
9
|
+
useCallback,
|
|
10
|
+
useImperativeHandle,
|
|
11
|
+
useRef
|
|
12
|
+
} from 'react';
|
|
13
|
+
import { Image, View, ImageSourcePropType, HostComponent } from 'react-native';
|
|
14
|
+
import invariant from 'invariant';
|
|
15
|
+
|
|
16
|
+
import RNCWebView, { Commands, NativeProps } from './RNCWebViewNativeComponent';
|
|
17
|
+
import RNCWebViewModule from './NativeRNCWebViewModule';
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
defaultOriginWhitelist,
|
|
21
|
+
defaultRenderError,
|
|
22
|
+
defaultRenderLoading,
|
|
23
|
+
useWebViewLogic,
|
|
24
|
+
} from 'react-native-webview/src/WebViewShared';
|
|
25
|
+
import {
|
|
26
|
+
IOSWebViewProps,
|
|
27
|
+
DecelerationRateConstant,
|
|
28
|
+
WebViewSourceUri,
|
|
29
|
+
} from 'react-native-webview/src/WebViewTypes';
|
|
30
|
+
|
|
31
|
+
import styles from 'react-native-webview/src/WebView.styles';
|
|
32
|
+
const { resolveAssetSource } = Image;
|
|
33
|
+
const processDecelerationRate = (
|
|
34
|
+
decelerationRate: DecelerationRateConstant | number | undefined
|
|
35
|
+
) => {
|
|
36
|
+
let newDecelerationRate = decelerationRate;
|
|
37
|
+
if (newDecelerationRate === 'normal') {
|
|
38
|
+
newDecelerationRate = 0.998;
|
|
39
|
+
} else if (newDecelerationRate === 'fast') {
|
|
40
|
+
newDecelerationRate = 0.99;
|
|
41
|
+
}
|
|
42
|
+
return newDecelerationRate;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const useWarnIfChanges = <T extends unknown>(value: T, name: string) => {
|
|
46
|
+
const ref = useRef(value);
|
|
47
|
+
if (ref.current !== value) {
|
|
48
|
+
console.warn(
|
|
49
|
+
`Changes to property ${name} do nothing after the initial render.`
|
|
50
|
+
);
|
|
51
|
+
ref.current = value;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const WebViewComponent = forwardRef<{}, IOSWebViewProps & { scalesPageToFit: boolean, minimumFontSize: number, thirdPartyCookiesEnabled: boolean, geolocationEnabled: boolean }>(
|
|
56
|
+
(
|
|
57
|
+
{
|
|
58
|
+
fraudulentWebsiteWarningEnabled = true,
|
|
59
|
+
javaScriptEnabled = true,
|
|
60
|
+
cacheEnabled = true,
|
|
61
|
+
originWhitelist = defaultOriginWhitelist,
|
|
62
|
+
applicationNameForUserAgent,
|
|
63
|
+
ignoreSilentHardwareSwitch,
|
|
64
|
+
minimumFontSize,
|
|
65
|
+
useSharedProcessPool = true,
|
|
66
|
+
textInteractionEnabled = true,
|
|
67
|
+
injectedJavaScript,
|
|
68
|
+
injectedJavaScriptBeforeContentLoaded,
|
|
69
|
+
injectedJavaScriptObject,
|
|
70
|
+
menuItems=[],
|
|
71
|
+
injectedJavaScriptForMainFrameOnly = true,
|
|
72
|
+
injectedJavaScriptBeforeContentLoadedForMainFrameOnly = true,
|
|
73
|
+
thirdPartyCookiesEnabled = false,
|
|
74
|
+
geolocationEnabled = false,
|
|
75
|
+
webviewDebuggingEnabled = false,
|
|
76
|
+
paymentRequestEnabled = false,
|
|
77
|
+
startInLoadingState,
|
|
78
|
+
onNavigationStateChange,
|
|
79
|
+
onLoadStart,
|
|
80
|
+
onCustomMenuSelection,
|
|
81
|
+
onError,
|
|
82
|
+
onLoad,
|
|
83
|
+
onLoadEnd,
|
|
84
|
+
onLoadProgress,
|
|
85
|
+
onScroll,
|
|
86
|
+
onContentProcessDidTerminate: onContentProcessDidTerminateProp,
|
|
87
|
+
onFileDownload,
|
|
88
|
+
onHttpError: onHttpErrorProp,
|
|
89
|
+
onMessage: onMessageProp,
|
|
90
|
+
onOpenWindow: onOpenWindowProp,
|
|
91
|
+
renderLoading,
|
|
92
|
+
renderError,
|
|
93
|
+
style,
|
|
94
|
+
containerStyle,
|
|
95
|
+
source,
|
|
96
|
+
nativeConfig,
|
|
97
|
+
allowsInlineMediaPlayback,
|
|
98
|
+
allowsPictureInPictureMediaPlayback,
|
|
99
|
+
allowsAirPlayForMediaPlayback,
|
|
100
|
+
mediaPlaybackRequiresUserAction=true,
|
|
101
|
+
dataDetectorTypes,
|
|
102
|
+
incognito,
|
|
103
|
+
decelerationRate: decelerationRateProp,
|
|
104
|
+
onShouldStartLoadWithRequest: onShouldStartLoadWithRequestProp,
|
|
105
|
+
onStartShouldSetResponder,
|
|
106
|
+
onMoveShouldSetResponderCapture,
|
|
107
|
+
pullToRefreshEnabled=false,
|
|
108
|
+
refreshControlLightMode=false,
|
|
109
|
+
onResponderMove,
|
|
110
|
+
...otherProps
|
|
111
|
+
},
|
|
112
|
+
ref
|
|
113
|
+
) => {
|
|
114
|
+
const webViewRef = useRef<React.ComponentRef<
|
|
115
|
+
HostComponent<NativeProps>
|
|
116
|
+
> | null>(null);
|
|
117
|
+
|
|
118
|
+
const onShouldStartLoadWithRequestCallback = useCallback(
|
|
119
|
+
(shouldStart: boolean, _url: string, lockIdentifier = 0) => {
|
|
120
|
+
RNCWebViewModule.shouldStartLoadWithLockIdentifier(
|
|
121
|
+
shouldStart,
|
|
122
|
+
lockIdentifier
|
|
123
|
+
);
|
|
124
|
+
},
|
|
125
|
+
[]
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const {
|
|
129
|
+
onLoadingStart,
|
|
130
|
+
onShouldStartLoadWithRequest,
|
|
131
|
+
onMessage,
|
|
132
|
+
viewState,
|
|
133
|
+
setViewState,
|
|
134
|
+
lastErrorEvent,
|
|
135
|
+
onHttpError,
|
|
136
|
+
onLoadingError,
|
|
137
|
+
onLoadingFinish,
|
|
138
|
+
onLoadingProgress,
|
|
139
|
+
onOpenWindow,
|
|
140
|
+
onContentProcessDidTerminate,
|
|
141
|
+
} = useWebViewLogic({
|
|
142
|
+
onNavigationStateChange,
|
|
143
|
+
onLoad,
|
|
144
|
+
onError,
|
|
145
|
+
onHttpErrorProp,
|
|
146
|
+
onLoadEnd,
|
|
147
|
+
onLoadProgress,
|
|
148
|
+
// @ts-expect-error old arch only
|
|
149
|
+
onScroll,
|
|
150
|
+
onLoadStart,
|
|
151
|
+
onMessageProp,
|
|
152
|
+
onOpenWindowProp,
|
|
153
|
+
startInLoadingState,
|
|
154
|
+
originWhitelist,
|
|
155
|
+
ignoreSilentHardwareSwitch,
|
|
156
|
+
allowsPictureInPictureMediaPlayback,
|
|
157
|
+
minimumFontSize,
|
|
158
|
+
onShouldStartLoadWithRequestProp,
|
|
159
|
+
onShouldStartLoadWithRequestCallback,
|
|
160
|
+
onContentProcessDidTerminateProp,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
useImperativeHandle(
|
|
164
|
+
ref,
|
|
165
|
+
() => ({
|
|
166
|
+
goForward: () =>
|
|
167
|
+
webViewRef.current && Commands.goForward(webViewRef.current),
|
|
168
|
+
goBack: () => webViewRef.current && Commands.goBack(webViewRef.current),
|
|
169
|
+
reload: () => {
|
|
170
|
+
setViewState('LOADING');
|
|
171
|
+
if (webViewRef.current) {
|
|
172
|
+
Commands.reload(webViewRef.current);
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
stopLoading: () =>
|
|
176
|
+
webViewRef.current && Commands.stopLoading(webViewRef.current),
|
|
177
|
+
postMessage: (data: string) =>
|
|
178
|
+
webViewRef.current && Commands.postMessage(webViewRef.current, data),
|
|
179
|
+
injectJavaScript: (data: string) =>
|
|
180
|
+
webViewRef.current &&
|
|
181
|
+
Commands.injectJavaScript(webViewRef.current, data),
|
|
182
|
+
requestFocus: () =>
|
|
183
|
+
webViewRef.current && Commands.requestFocus(webViewRef.current),
|
|
184
|
+
clearCache: (includeDiskFiles: boolean) =>
|
|
185
|
+
webViewRef.current &&
|
|
186
|
+
Commands.clearCache(webViewRef.current, includeDiskFiles),
|
|
187
|
+
clearHistory: () => webViewRef.current && Commands.clearHistory(webViewRef.current),
|
|
188
|
+
loadUrl: (data: string) => webViewRef.current && Commands.loadUrl(webViewRef.current, data),
|
|
189
|
+
}),
|
|
190
|
+
[setViewState, webViewRef]
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
useWarnIfChanges(allowsInlineMediaPlayback, 'allowsInlineMediaPlayback');
|
|
194
|
+
useWarnIfChanges(
|
|
195
|
+
allowsPictureInPictureMediaPlayback,
|
|
196
|
+
'allowsPictureInPictureMediaPlayback'
|
|
197
|
+
);
|
|
198
|
+
useWarnIfChanges(
|
|
199
|
+
allowsAirPlayForMediaPlayback,
|
|
200
|
+
'allowsAirPlayForMediaPlayback'
|
|
201
|
+
);
|
|
202
|
+
useWarnIfChanges(incognito, 'incognito');
|
|
203
|
+
useWarnIfChanges(
|
|
204
|
+
mediaPlaybackRequiresUserAction,
|
|
205
|
+
'mediaPlaybackRequiresUserAction'
|
|
206
|
+
);
|
|
207
|
+
useWarnIfChanges(dataDetectorTypes, 'dataDetectorTypes');
|
|
208
|
+
|
|
209
|
+
let otherView = null;
|
|
210
|
+
if (viewState === 'LOADING') {
|
|
211
|
+
otherView = (renderLoading || defaultRenderLoading)();
|
|
212
|
+
} else if (viewState === 'ERROR') {
|
|
213
|
+
invariant(
|
|
214
|
+
lastErrorEvent != null,
|
|
215
|
+
'lastErrorEvent expected to be non-null'
|
|
216
|
+
);
|
|
217
|
+
otherView = (renderError || defaultRenderError)(
|
|
218
|
+
lastErrorEvent?.domain,
|
|
219
|
+
lastErrorEvent?.code ?? 0,
|
|
220
|
+
lastErrorEvent?.description ?? ''
|
|
221
|
+
);
|
|
222
|
+
} else if (viewState !== 'IDLE') {
|
|
223
|
+
console.error(`RNCWebView invalid state encountered: ${viewState}`);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const webViewStyles = [styles.container, styles.webView, style];
|
|
227
|
+
const webViewContainerStyle = [styles.container, containerStyle];
|
|
228
|
+
|
|
229
|
+
const decelerationRate = processDecelerationRate(decelerationRateProp);
|
|
230
|
+
|
|
231
|
+
const NativeWebView =
|
|
232
|
+
(nativeConfig?.component as typeof RNCWebView | undefined) || RNCWebView;
|
|
233
|
+
|
|
234
|
+
const sourceResolved = resolveAssetSource(source as ImageSourcePropType);
|
|
235
|
+
const newSource =
|
|
236
|
+
typeof sourceResolved === 'object'
|
|
237
|
+
? Object.entries(sourceResolved as WebViewSourceUri).reduce(
|
|
238
|
+
(prev, [currKey, currValue]) => {
|
|
239
|
+
return {
|
|
240
|
+
...prev,
|
|
241
|
+
[currKey]:
|
|
242
|
+
currKey === 'headers' &&
|
|
243
|
+
currValue &&
|
|
244
|
+
typeof currValue === 'object'
|
|
245
|
+
? Object.entries(currValue).map(([key, value]) => {
|
|
246
|
+
return {
|
|
247
|
+
name: key,
|
|
248
|
+
value,
|
|
249
|
+
};
|
|
250
|
+
})
|
|
251
|
+
: currValue,
|
|
252
|
+
};
|
|
253
|
+
},
|
|
254
|
+
{}
|
|
255
|
+
)
|
|
256
|
+
: sourceResolved;
|
|
257
|
+
|
|
258
|
+
const webView = (
|
|
259
|
+
<NativeWebView
|
|
260
|
+
key="webViewKey"
|
|
261
|
+
{...otherProps}
|
|
262
|
+
scrollEnabled={otherProps.scrollEnabled ?? true}
|
|
263
|
+
ignoreSilentHardwareSwitch={ignoreSilentHardwareSwitch}
|
|
264
|
+
minimumFontSize={minimumFontSize}
|
|
265
|
+
scalesPageToFit={otherProps.scalesPageToFit ?? true}
|
|
266
|
+
fraudulentWebsiteWarningEnabled={fraudulentWebsiteWarningEnabled}
|
|
267
|
+
javaScriptEnabled={javaScriptEnabled}
|
|
268
|
+
onCustomMenuSelection={onCustomMenuSelection}
|
|
269
|
+
applicationNameForUserAgent={applicationNameForUserAgent}
|
|
270
|
+
cacheEnabled={cacheEnabled}
|
|
271
|
+
useSharedProcessPool={useSharedProcessPool}
|
|
272
|
+
textInteractionEnabled={textInteractionEnabled}
|
|
273
|
+
geolocationEnabled={geolocationEnabled}
|
|
274
|
+
decelerationRate={decelerationRate}
|
|
275
|
+
messagingEnabled={typeof onMessageProp === 'function'}
|
|
276
|
+
messagingModuleName="" // android ONLY
|
|
277
|
+
onLoadingError={onLoadingError}
|
|
278
|
+
onLoadingFinish={onLoadingFinish}
|
|
279
|
+
onLoadingProgress={onLoadingProgress}
|
|
280
|
+
onScroll={onScroll}
|
|
281
|
+
onFileDownload={onFileDownload}
|
|
282
|
+
onLoadingStart={onLoadingStart}
|
|
283
|
+
onHttpError={onHttpError}
|
|
284
|
+
onMessage={onMessage}
|
|
285
|
+
onOpenWindow={onOpenWindowProp && onOpenWindow}
|
|
286
|
+
hasOnOpenWindowEvent={onOpenWindowProp !== undefined}
|
|
287
|
+
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
|
|
288
|
+
onContentProcessDidTerminate={onContentProcessDidTerminate}
|
|
289
|
+
thirdPartyCookiesEnabled={thirdPartyCookiesEnabled}
|
|
290
|
+
pullToRefreshEnabled={pullToRefreshEnabled}
|
|
291
|
+
refreshControlLightMode={refreshControlLightMode}
|
|
292
|
+
injectedJavaScript={injectedJavaScript}
|
|
293
|
+
injectedJavaScriptBeforeContentLoaded={
|
|
294
|
+
injectedJavaScriptBeforeContentLoaded
|
|
295
|
+
}
|
|
296
|
+
injectedJavaScriptForMainFrameOnly={injectedJavaScriptForMainFrameOnly}
|
|
297
|
+
injectedJavaScriptBeforeContentLoadedForMainFrameOnly={
|
|
298
|
+
injectedJavaScriptBeforeContentLoadedForMainFrameOnly
|
|
299
|
+
}
|
|
300
|
+
injectedJavaScriptObject={JSON.stringify(injectedJavaScriptObject)}
|
|
301
|
+
dataDetectorTypes={
|
|
302
|
+
!dataDetectorTypes || Array.isArray(dataDetectorTypes)
|
|
303
|
+
? dataDetectorTypes
|
|
304
|
+
: [dataDetectorTypes]
|
|
305
|
+
}
|
|
306
|
+
allowsAirPlayForMediaPlayback={allowsAirPlayForMediaPlayback}
|
|
307
|
+
allowsPictureInPictureMediaPlayback={allowsPictureInPictureMediaPlayback}
|
|
308
|
+
allowsInlineMediaPlayback={allowsInlineMediaPlayback}
|
|
309
|
+
incognito={incognito}
|
|
310
|
+
mediaPlaybackRequiresUserAction={mediaPlaybackRequiresUserAction}
|
|
311
|
+
menuItems={menuItems}
|
|
312
|
+
newSource={newSource}
|
|
313
|
+
style={webViewStyles}
|
|
314
|
+
webviewDebuggingEnabled={webviewDebuggingEnabled}
|
|
315
|
+
paymentRequestEnabled={paymentRequestEnabled}
|
|
316
|
+
hasOnFileDownload={!!onFileDownload}
|
|
317
|
+
ref={webViewRef}
|
|
318
|
+
// @ts-expect-error old arch only
|
|
319
|
+
source={sourceResolved}
|
|
320
|
+
{...nativeConfig?.props}
|
|
321
|
+
/>
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
return (
|
|
325
|
+
<View
|
|
326
|
+
style={webViewContainerStyle}
|
|
327
|
+
onStartShouldSetResponder={(e) => onStartShouldSetResponder ? onStartShouldSetResponder(e) : false}
|
|
328
|
+
onMoveShouldSetResponderCapture={(e) => onMoveShouldSetResponderCapture ? onMoveShouldSetResponderCapture(e) : false}
|
|
329
|
+
onResponderMove={(e) => onResponderMove ? onResponderMove(e) : null}
|
|
330
|
+
>
|
|
331
|
+
{webView}
|
|
332
|
+
{otherView}
|
|
333
|
+
</View>
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
// no native implementation for iOS, depends only on permissions
|
|
339
|
+
const isFileUploadSupported: () => Promise<boolean> = async () => true;
|
|
340
|
+
|
|
341
|
+
const WebView = Object.assign(WebViewComponent, { isFileUploadSupported });
|
|
342
|
+
|
|
343
343
|
export default WebView;
|