@react-native-ohos/react-native-webview 13.10.6-beta.3 → 13.10.6

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/LICENSE +21 -21
  3. package/README.md +385 -393
  4. package/harmony/rn_webview/.gitignore +6 -0
  5. package/harmony/rn_webview/BuildProfile.ets +1 -1
  6. package/harmony/rn_webview/LICENSE +21 -21
  7. package/harmony/rn_webview/NOTICE +33 -33
  8. package/harmony/rn_webview/OAT.xml +44 -44
  9. package/harmony/rn_webview/README.OpenSource +10 -10
  10. package/harmony/rn_webview/build-profile.json5 +28 -28
  11. package/harmony/rn_webview/index.ets +33 -33
  12. package/harmony/rn_webview/obfuscation-rules.txt +17 -17
  13. package/harmony/rn_webview/oh-package-lock.json5 +18 -18
  14. package/harmony/rn_webview/oh-package.json5 +13 -13
  15. package/harmony/rn_webview/src/main/cpp/CMakeLists.txt +9 -9
  16. package/harmony/rn_webview/src/main/cpp/WebViewPackage.h +36 -36
  17. package/harmony/rn_webview/src/main/cpp/generated/RNOH/generated/BaseReactNativeWebviewPackage.h +94 -94
  18. package/harmony/rn_webview/src/main/cpp/generated/RNOH/generated/components/RNCWebViewJSIBinder.h +129 -128
  19. package/harmony/rn_webview/src/main/cpp/generated/RNOH/generated/turbo_modules/RNCWebView.cpp +20 -20
  20. package/harmony/rn_webview/src/main/cpp/generated/RNOH/generated/turbo_modules/RNCWebView.h +16 -16
  21. package/harmony/rn_webview/src/main/cpp/generated/RNOH/generated/turbo_modules/RNCWebViewModule.cpp +19 -19
  22. package/harmony/rn_webview/src/main/cpp/generated/RNOH/generated/turbo_modules/RNCWebViewModule.h +16 -16
  23. package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/ComponentDescriptors.h +22 -22
  24. package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/EventEmitters.cpp +240 -240
  25. package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/EventEmitters.h +285 -285
  26. package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/Props.cpp +156 -155
  27. package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/Props.h +340 -339
  28. package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/ShadowNodes.cpp +19 -19
  29. package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/ShadowNodes.h +34 -34
  30. package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/States.cpp +16 -16
  31. package/harmony/rn_webview/src/main/cpp/generated/react/renderer/components/react_native_webview/States.h +19 -19
  32. package/harmony/rn_webview/src/main/ets/CutomReference.ts +49 -49
  33. package/harmony/rn_webview/src/main/ets/Logger.ts +63 -63
  34. package/harmony/rn_webview/src/main/ets/Magic.ets +223 -221
  35. package/harmony/rn_webview/src/main/ets/RNCWebView.ets +1098 -903
  36. package/harmony/rn_webview/src/main/ets/RNCWebViewPackage.ets +65 -65
  37. package/harmony/rn_webview/src/main/ets/ShouldRequestUrl.ts +67 -67
  38. package/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets +490 -490
  39. package/harmony/rn_webview/src/main/ets/WebViewTurboModule.ets +81 -81
  40. package/harmony/rn_webview/src/main/ets/generated/components/RNCWebView.ts +516 -511
  41. package/harmony/rn_webview/src/main/ets/generated/components/ts.ts +5 -5
  42. package/harmony/rn_webview/src/main/ets/generated/index.ets +5 -5
  43. package/harmony/rn_webview/src/main/ets/generated/ts.ts +6 -6
  44. package/harmony/rn_webview/src/main/ets/generated/turboModules/RNCWebView.ts +18 -18
  45. package/harmony/rn_webview/src/main/ets/generated/turboModules/RNCWebViewModule.ts +16 -16
  46. package/harmony/rn_webview/src/main/ets/generated/turboModules/ts.ts +6 -6
  47. package/harmony/rn_webview/src/main/module.json5 +10 -10
  48. package/harmony/rn_webview/src/main/resources/base/element/string.json +39 -39
  49. package/harmony/rn_webview/src/main/resources/en_US/element/string.json +39 -39
  50. package/harmony/rn_webview/src/main/resources/zh_CN/element/string.json +39 -39
  51. package/harmony/rn_webview/src/test/List.test.ets +4 -4
  52. package/harmony/rn_webview/src/test/LocalUnit.test.ets +32 -32
  53. package/harmony/rn_webview/ts.ets +28 -28
  54. package/harmony/rn_webview.har +0 -0
  55. package/package.json +105 -104
  56. package/src/NativeRNCWebView.ts +15 -15
  57. package/src/NativeRNCWebViewModule.ts +17 -17
  58. package/src/RNCWebViewNativeComponent.ts +289 -288
  59. package/src/WebView.harmony.tsx +331 -331
  60. package/src/WebView.tsx +4 -4
  61. package/src/codegenUtils.ts +8 -8
  62. package/src/index.ts +4 -4
@@ -1,490 +1,490 @@
1
- /**
2
- * MIT License
3
- *
4
- * Copyright (C) 2025 Huawei Device Co., Ltd.
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
-
25
- import { RNC } from './generated/ts'
26
- import { webview } from '@kit.ArkWeb'
27
- import { CACHE_MODE, COMMAND_NAME, ONE_HUNDRED, WebViewEventParams } from './Magic';
28
- import Logger from './Logger';
29
- import { common, ConfigurationConstant } from '@kit.AbilityKit';
30
-
31
- export const TAG = "WebView"
32
-
33
-
34
- interface ProgressInterface {
35
- progress: number;
36
- }
37
-
38
- interface CreateWebViewEventInterface {
39
- type: string;
40
- progress: number;
41
- }
42
-
43
- export class BaseOperate {
44
- private static LOCK_IDENTIFIER = 0
45
- private eventEmitter: RNC.RNCWebView.EventEmitter
46
- private controller: webview.WebviewController
47
- private lockIdentifier: number = 0
48
-
49
- constructor(eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController) {
50
- this.eventEmitter = eventEmitter
51
- this.controller = controller
52
- this.lockIdentifier = BaseOperate.generateLockIdentifier()
53
- }
54
-
55
- static setThirdPartyCookiesEnabled(status: boolean) {
56
- try {
57
- webview.WebCookieManager.putAcceptThirdPartyCookieEnabled(status);
58
- } catch (error) {
59
- Logger.error(TAG, `[RNOH] setThirdPartyCookiesEnabled Errorcode: ${error.code}`);
60
- }
61
- }
62
-
63
- static getColorMode(context: common.UIAbilityContext, forceDarkOn: boolean): WebDarkMode {
64
- try {
65
- return ((context.config.colorMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK) && forceDarkOn) ?
66
- WebDarkMode.On : WebDarkMode.Off
67
- } catch (error) {
68
- Logger.error(TAG, `[RNOH] getColorMode Errorcode: ${error.code}`);
69
- return WebDarkMode.Off
70
- }
71
- }
72
-
73
- static generateLockIdentifier(): number {
74
- BaseOperate.LOCK_IDENTIFIER = BaseOperate.LOCK_IDENTIFIER + 1
75
- return BaseOperate.LOCK_IDENTIFIER
76
- }
77
-
78
- getLockIdentifier():number {
79
- return this.lockIdentifier
80
- }
81
-
82
- setLockIdentifier(lockIdentifier: number) {
83
- this.lockIdentifier = lockIdentifier
84
- }
85
-
86
- emitProgressChange(params: ProgressInterface) {
87
- try {
88
- this.eventEmitter!.emit('loadingProgress', {
89
- url: this.controller.getUrl(),
90
- loading: params.progress != ONE_HUNDRED,
91
- title: this.controller.getTitle(),
92
- canGoBack: this.controller.accessBackward(),
93
- canGoForward: this.controller.accessForward(),
94
- lockIdentifier: this.lockIdentifier,
95
- progress: params.progress / ONE_HUNDRED
96
- })
97
- } catch (error) {
98
- Logger.error(TAG, `[RNOH] emitProgressChange Errorcode: ${error.code}`);
99
- }
100
- }
101
-
102
- emitLoadingStart(params: ProgressInterface) {
103
- try {
104
- this.eventEmitter!.emit('loadingStart', {
105
- url: this.controller.getUrl(),
106
- loading: params.progress != ONE_HUNDRED,
107
- title: this.controller.getTitle(),
108
- canGoBack: this.controller.accessBackward(),
109
- canGoForward: this.controller.accessForward(),
110
- lockIdentifier: this.lockIdentifier,
111
- navigationType: "other",
112
- mainDocumentURL: ""
113
- })
114
- } catch (error) {
115
- Logger.error(TAG, `[RNOH] emitLoadingStart Errorcode: ${error.code}`);
116
- }
117
- }
118
-
119
- emitLoadingFinish(params: ProgressInterface) {
120
- try {
121
- this.eventEmitter!.emit('loadingFinish', {
122
- url: this.controller.getUrl(),
123
- loading: params.progress != ONE_HUNDRED,
124
- title: this.controller.getTitle(),
125
- canGoBack: this.controller.accessBackward(),
126
- canGoForward: this.controller.accessForward(),
127
- lockIdentifier: this.lockIdentifier,
128
- navigationType: "other",
129
- mainDocumentURL: ""
130
- })
131
- } catch (error) {
132
- Logger.error(TAG, `[RNOH] emitLoadingFinish Errorcode: ${error.code}`);
133
- }
134
- }
135
-
136
- emitLoadingError(event: OnErrorReceiveEvent) {
137
- try {
138
- if (!event.request.isMainFrame()) {
139
- Logger.debug(TAG, "[RNOH] Not Main Frame Error");
140
- return;
141
- }
142
- this.eventEmitter!.emit('loadingError', {
143
- url: this.controller.getUrl(),
144
- loading: false,
145
- title: this.controller.getTitle(),
146
- canGoBack: this.controller.accessBackward(),
147
- canGoForward: this.controller.accessForward(),
148
- lockIdentifier: this.lockIdentifier,
149
- domain: "",
150
- code: event.error.getErrorCode(),
151
- description: event.error.getErrorInfo()
152
- })
153
- } catch (error) {
154
- Logger.error(TAG, `[RNOH] emitLoadingError Errorcode: ${error.code}`);
155
- }
156
- }
157
-
158
- emitHttpError(event: OnHttpErrorReceiveEvent) {
159
- try {
160
- this.eventEmitter!.emit('httpError', {
161
- url: this.controller.getUrl(),
162
- loading: false,
163
- title: this.controller.getTitle(),
164
- canGoBack: this.controller.accessBackward(),
165
- canGoForward: this.controller.accessForward(),
166
- lockIdentifier: this.lockIdentifier,
167
- description: event.response.getResponseData(),
168
- statusCode: event.response.getResponseCode()
169
- })
170
- } catch (error) {
171
- Logger.error(TAG, `[RNOH] emitHttpError Errorcode: ${error.code}`);
172
- }
173
- }
174
-
175
- emitScroll(event: OnScrollEvent) {
176
- try {
177
- this.eventEmitter!.emit('scroll', {
178
- contentInset: {
179
- bottom: 0,
180
- left: 0,
181
- right: 0,
182
- top: 0
183
- },
184
- contentOffset: { y: event.yOffset, x: event.xOffset },
185
- contentSize: { height: 0, width: 0 },
186
- layoutMeasurement: { height: 0, width: 0 }
187
- })
188
- } catch (error) {
189
- Logger.error(TAG, `[RNOH] emitScroll Errorcode: ${error.code}`);
190
- }
191
- }
192
-
193
- verifyURLFormat(url: string, html: string | undefined, baseUrl: string | undefined) {
194
- const dataUrlPattern = /^data:text\/html;/;
195
- if (dataUrlPattern.test(url)){
196
- if (html != undefined && html != "") {
197
- if (baseUrl != undefined && baseUrl != "") {
198
- url = baseUrl.toLowerCase();
199
- } else {
200
- url = 'about:blank';
201
- }
202
- }
203
- }
204
- return url;
205
- }
206
-
207
- emitShouldStartLoadWithRequest(event: OnLoadInterceptEvent, html: string | undefined, baseUrl: string | undefined) {
208
- try {
209
- this.eventEmitter!.emit('shouldStartLoadWithRequest', {
210
- url: this.verifyURLFormat(event.data.getRequestUrl(), html, baseUrl),
211
- loading: false,
212
- title: this.controller.getTitle(),
213
- canGoBack: this.controller.accessBackward(),
214
- canGoForward: this.controller.accessForward(),
215
- lockIdentifier: this.lockIdentifier,
216
- navigationType: "other",
217
- mainDocumentURL: "",
218
- isTopFrame: false
219
- })
220
- } catch (error) {
221
- Logger.error(TAG, `[RNOH] emitShouldStartLoadWithRequest Errorcode: ${error.code}`);
222
- }
223
- }
224
-
225
- emitShouldStartLoadWithRequestOverrideUrlLoading(event: WebResourceRequest) {
226
- try {
227
- this.eventEmitter!.emit('shouldStartLoadWithRequest', {
228
- url: event.getRequestUrl(),
229
- loading: false,
230
- title: this.controller.getTitle(),
231
- canGoBack: this.controller.accessBackward(),
232
- canGoForward: this.controller.accessForward(),
233
- lockIdentifier: this.lockIdentifier,
234
- navigationType: "other",
235
- mainDocumentURL: "",
236
- isTopFrame: false
237
- })
238
- } catch (error) {
239
- Logger.error(TAG, `[RNOH] emitShouldStartLoadWithRequest Errorcode: ${error.code}`);
240
- }
241
- }
242
-
243
- transCacheMode(cacheMode: CACHE_MODE): CacheMode {
244
- let mode = CacheMode.Default
245
- switch (cacheMode) {
246
- case CACHE_MODE.LOAD_DEFAULT:
247
- mode = CacheMode.Default
248
- break;
249
- case CACHE_MODE.LOAD_CACHE_ELSE_NETWORK:
250
- mode = CacheMode.None
251
- break;
252
- case CACHE_MODE.LOAD_NO_CACHE:
253
- mode = CacheMode.Online
254
- break;
255
- case CACHE_MODE.LOAD_CACHE_ONLY:
256
- mode = CacheMode.Only
257
- break;
258
- default:
259
- break;
260
- }
261
- return mode
262
- }
263
-
264
- createWebViewEvent(param: CreateWebViewEventInterface): WebViewEventParams {
265
- let result: WebViewEventParams = new WebViewEventParams(param.type);
266
- result.loading = param.progress != ONE_HUNDRED
267
- try {
268
- result.url = this.controller.getUrl();
269
- result.title = this.controller.getTitle();
270
- result.canGoBack = this.controller.accessBackward();
271
- result.canGoForward = this.controller.accessForward();
272
- } catch (error) {
273
- result.url = "";
274
- result.title = "";
275
- result.canGoBack = false;
276
- result.canGoForward = false;
277
- }
278
- return result;
279
- }
280
-
281
- // 与OS web同事 对齐 默认false 是取消静音
282
- ignoreSilentHardwareSwitchMethods(ignoreSilentHardwareSwitch: boolean) {
283
- this.controller.setAudioMuted(!ignoreSilentHardwareSwitch)
284
- }
285
-
286
- setCustomUserAgent(customUserAgent?: string, applicationNameForUserAgent?: string): void {
287
- try {
288
- if (customUserAgent) {
289
- let userAgent: string = customUserAgent;
290
- this.controller.setCustomUserAgent(userAgent);
291
- }
292
- if (applicationNameForUserAgent) {
293
- this.controller.setCustomUserAgent(`${this.controller.getUserAgent()}${applicationNameForUserAgent}`);
294
- }
295
- } catch (error) {
296
- Logger.debug(TAG,
297
- `[RNOH] setCustomUserAgent ErrorCode: ${error.code}, userAgent: ${customUserAgent}`);
298
- }
299
- }
300
-
301
- injectJavaScript(args: string[]) {
302
- try {
303
- this.controller.runJavaScript(args[0])
304
- } catch (error) {
305
- Logger.error(TAG, `[RNOH] injectJavaScript Errorcode: ${error.code}`);
306
- }
307
- }
308
-
309
- postMessage(args: string[]) {
310
- let data = JSON.stringify({ data: args[0] })
311
- let result: string = "(function () {" +
312
- "var event;" +
313
- "var data = " + data.toString() + ";" +
314
- "try {" +
315
- "event = new MessageEvent('message', data);" +
316
- "} catch (e) {" +
317
- "event = document.createEvent('MessageEvent');" +
318
- "event.initMessageEvent('message', true, true, data.data, data.origin, data.lastEventId, data.source);" +
319
- "}" +
320
- "document.dispatchEvent(event);" +
321
- "})();"
322
- try {
323
- this.controller.runJavaScript(result)
324
- } catch (error) {
325
- Logger.error(TAG, `[RNOH] postMessage Errorcode: ${error.code}`);
326
- }
327
- }
328
-
329
- reload() {
330
- try {
331
- this.controller.refresh();
332
- } catch (error) {
333
- Logger.error(TAG, `[RNOH] reload Errorcode: ${error.code}`);
334
- }
335
- }
336
-
337
- goBack() {
338
- try {
339
- this.controller.backward();
340
- } catch (error) {
341
- Logger.error(TAG, `[RNOH] goBack Errorcode: ${error.code}`);
342
- }
343
- }
344
-
345
- goForward() {
346
- try {
347
- this.controller.forward();
348
- } catch (error) {
349
- Logger.error(TAG, `[RNOH] goForward Errorcode: ${error.code}`);
350
- }
351
- }
352
-
353
- requestFocus() {
354
- try {
355
- this.controller.requestFocus();
356
- } catch (error) {
357
- Logger.error(TAG, `[RNOH] requestFocus Errorcode: ${error.code}`);
358
- }
359
- }
360
-
361
- clearCache(args: string[]) {
362
- try {
363
- const removeFlag = !!args[0] === true;
364
- this.controller.removeCache(removeFlag);
365
- } catch (error) {
366
- Logger.error(TAG, `[RNOH] clearCache Errorcode: ${error.code}`);
367
- }
368
- }
369
-
370
- clearHistory() {
371
- try {
372
- this.controller.clearHistory();
373
- } catch (error) {
374
- Logger.error(TAG, `[RNOH] clearHistory Errorcode: ${error.code}`);
375
- }
376
- }
377
-
378
- stopLoading() {
379
- try {
380
- this.controller.stop();
381
- } catch (error) {
382
- Logger.error(TAG, `[RNOH] stopLoading Errorcode: ${error.code}`);
383
- }
384
- }
385
-
386
- loadUrl(args: string[]) {
387
- try {
388
- this.controller.loadUrl(args[0]);
389
- } catch (error) {
390
- Logger.error(TAG, `[RNOH] loadUrl Errorcode: ${error.code}`);
391
- }
392
- }
393
-
394
- registerCommandCallback(commandName: COMMAND_NAME, args: string[]) {
395
- switch (commandName) {
396
- case COMMAND_NAME.INJECTJAVASCRIPT:
397
- this.injectJavaScript(args)
398
- break
399
- case COMMAND_NAME.POSTMESSAGE:
400
- this.postMessage(args)
401
- break
402
- case COMMAND_NAME.RELOAD:
403
- this.reload()
404
- break
405
- case COMMAND_NAME.GOBACK:
406
- this.goBack()
407
- break
408
- case COMMAND_NAME.GOFORWARD:
409
- this.goForward()
410
- break
411
- case COMMAND_NAME.REQUESTFOCUS:
412
- this.requestFocus()
413
- case COMMAND_NAME.CLEARCACHE:
414
- this.clearCache(args)
415
- case COMMAND_NAME.CLEARHISTORY:
416
- this.clearHistory()
417
- break
418
- case COMMAND_NAME.STOPLOADING:
419
- this.stopLoading()
420
- break
421
- case COMMAND_NAME.LOADURL:
422
- this.loadUrl(args)
423
- break
424
- default:
425
- break
426
- }
427
- }
428
-
429
- setOriginWhitelist(uris: string) {
430
- try {
431
- this.controller.setUrlTrustList(uris);
432
- } catch (error) {
433
- Logger.error(TAG, `[RNOH] setOriginWhitelist Errorcode: ${error.code}`);
434
- }
435
- }
436
-
437
- setIncognito() {
438
- this.controller.clearHistory()
439
- webview.WebCookieManager.clearAllCookiesSync(true);
440
- webview.WebCookieManager.clearSessionCookieSync()
441
- webview.WebStorage.deleteAllData();
442
- }
443
-
444
- setFraudulentWebsiteWarningEnabled(status: boolean) {
445
- try {
446
- this.controller.enableSafeBrowsing(status);
447
- } catch (error) {
448
- Logger.error(TAG, `[RNOH] setFraudulentWebsiteWarningEnabled Errorcode: ${error.code}`);
449
- }
450
- }
451
-
452
- onDownloadStart(event: OnDownloadStartEvent) {
453
- try {
454
- this.eventEmitter!.emit('fileDownload', {
455
- downloadUrl: event.url
456
- })
457
- } catch (error) {
458
- Logger.error(TAG, `[RNOH] onDownloadStart Errorcode: ${error.code}`);
459
- }
460
- }
461
-
462
- onRenderExited(event: OnRenderExitedEvent) {
463
- try {
464
-
465
- } catch (error) {
466
- Logger.error(TAG, `[RNOH] onRenderExited Errorcode: ${error.code}`);
467
- }
468
- }
469
-
470
- onTitleReceive(event: OnTitleReceiveEvent) {
471
- try {
472
-
473
- } catch (error) {
474
- Logger.error(TAG, `[RNOH] onTitleReceive Errorcode: ${error.code}`);
475
- }
476
- }
477
-
478
- customMenuSelection(menuItem: TextMenuItem, selectionText: string) {
479
- try {
480
- selectionText = JSON.parse(selectionText).selection;
481
- this.eventEmitter!.emit('customMenuSelection', {
482
- label: menuItem.content.toString(),
483
- key: JSON.parse(JSON.stringify(menuItem.id)).id_,
484
- selectedText: selectionText
485
- })
486
- } catch (error) {
487
- Logger.error(TAG, `[RNOH] customMenuSelection Errorcode: ${error.code}`);
488
- }
489
- }
490
- }
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (C) 2025 Huawei Device Co., Ltd.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { RNC } from './generated/ts'
26
+ import { webview } from '@kit.ArkWeb'
27
+ import { CACHE_MODE, COMMAND_NAME, ONE_HUNDRED, WebViewEventParams } from './Magic';
28
+ import Logger from './Logger';
29
+ import { common, ConfigurationConstant } from '@kit.AbilityKit';
30
+
31
+ export const TAG = "WebView"
32
+
33
+
34
+ interface ProgressInterface {
35
+ progress: number;
36
+ }
37
+
38
+ interface CreateWebViewEventInterface {
39
+ type: string;
40
+ progress: number;
41
+ }
42
+
43
+ export class BaseOperate {
44
+ private static LOCK_IDENTIFIER = 0
45
+ private eventEmitter: RNC.RNCWebView.EventEmitter
46
+ private controller: webview.WebviewController
47
+ private lockIdentifier: number = 0
48
+
49
+ constructor(eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController) {
50
+ this.eventEmitter = eventEmitter
51
+ this.controller = controller
52
+ this.lockIdentifier = BaseOperate.generateLockIdentifier()
53
+ }
54
+
55
+ static setThirdPartyCookiesEnabled(status: boolean) {
56
+ try {
57
+ webview.WebCookieManager.putAcceptThirdPartyCookieEnabled(status);
58
+ } catch (error) {
59
+ Logger.error(TAG, `[RNOH] setThirdPartyCookiesEnabled Errorcode: ${error.code}`);
60
+ }
61
+ }
62
+
63
+ static getColorMode(context: common.UIAbilityContext, forceDarkOn: boolean): WebDarkMode {
64
+ try {
65
+ return ((context.config.colorMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK) && forceDarkOn) ?
66
+ WebDarkMode.On : WebDarkMode.Off
67
+ } catch (error) {
68
+ Logger.error(TAG, `[RNOH] getColorMode Errorcode: ${error.code}`);
69
+ return WebDarkMode.Off
70
+ }
71
+ }
72
+
73
+ static generateLockIdentifier(): number {
74
+ BaseOperate.LOCK_IDENTIFIER = BaseOperate.LOCK_IDENTIFIER + 1
75
+ return BaseOperate.LOCK_IDENTIFIER
76
+ }
77
+
78
+ getLockIdentifier():number {
79
+ return this.lockIdentifier
80
+ }
81
+
82
+ setLockIdentifier(lockIdentifier: number) {
83
+ this.lockIdentifier = lockIdentifier
84
+ }
85
+
86
+ emitProgressChange(params: ProgressInterface) {
87
+ try {
88
+ this.eventEmitter!.emit('loadingProgress', {
89
+ url: this.controller.getUrl(),
90
+ loading: params.progress != ONE_HUNDRED,
91
+ title: this.controller.getTitle(),
92
+ canGoBack: this.controller.accessBackward(),
93
+ canGoForward: this.controller.accessForward(),
94
+ lockIdentifier: this.lockIdentifier,
95
+ progress: params.progress / ONE_HUNDRED
96
+ })
97
+ } catch (error) {
98
+ Logger.error(TAG, `[RNOH] emitProgressChange Errorcode: ${error.code}`);
99
+ }
100
+ }
101
+
102
+ emitLoadingStart(params: ProgressInterface) {
103
+ try {
104
+ this.eventEmitter!.emit('loadingStart', {
105
+ url: this.controller.getUrl(),
106
+ loading: params.progress != ONE_HUNDRED,
107
+ title: this.controller.getTitle(),
108
+ canGoBack: this.controller.accessBackward(),
109
+ canGoForward: this.controller.accessForward(),
110
+ lockIdentifier: this.lockIdentifier,
111
+ navigationType: "other",
112
+ mainDocumentURL: ""
113
+ })
114
+ } catch (error) {
115
+ Logger.error(TAG, `[RNOH] emitLoadingStart Errorcode: ${error.code}`);
116
+ }
117
+ }
118
+
119
+ emitLoadingFinish(params: ProgressInterface) {
120
+ try {
121
+ this.eventEmitter!.emit('loadingFinish', {
122
+ url: this.controller.getUrl(),
123
+ loading: params.progress != ONE_HUNDRED,
124
+ title: this.controller.getTitle(),
125
+ canGoBack: this.controller.accessBackward(),
126
+ canGoForward: this.controller.accessForward(),
127
+ lockIdentifier: this.lockIdentifier,
128
+ navigationType: "other",
129
+ mainDocumentURL: ""
130
+ })
131
+ } catch (error) {
132
+ Logger.error(TAG, `[RNOH] emitLoadingFinish Errorcode: ${error.code}`);
133
+ }
134
+ }
135
+
136
+ emitLoadingError(event: OnErrorReceiveEvent) {
137
+ try {
138
+ if (!event.request.isMainFrame()) {
139
+ Logger.debug(TAG, "[RNOH] Not Main Frame Error");
140
+ return;
141
+ }
142
+ this.eventEmitter!.emit('loadingError', {
143
+ url: this.controller.getUrl(),
144
+ loading: false,
145
+ title: this.controller.getTitle(),
146
+ canGoBack: this.controller.accessBackward(),
147
+ canGoForward: this.controller.accessForward(),
148
+ lockIdentifier: this.lockIdentifier,
149
+ domain: "",
150
+ code: event.error.getErrorCode(),
151
+ description: event.error.getErrorInfo()
152
+ })
153
+ } catch (error) {
154
+ Logger.error(TAG, `[RNOH] emitLoadingError Errorcode: ${error.code}`);
155
+ }
156
+ }
157
+
158
+ emitHttpError(event: OnHttpErrorReceiveEvent) {
159
+ try {
160
+ this.eventEmitter!.emit('httpError', {
161
+ url: this.controller.getUrl(),
162
+ loading: false,
163
+ title: this.controller.getTitle(),
164
+ canGoBack: this.controller.accessBackward(),
165
+ canGoForward: this.controller.accessForward(),
166
+ lockIdentifier: this.lockIdentifier,
167
+ description: event.response.getResponseData(),
168
+ statusCode: event.response.getResponseCode()
169
+ })
170
+ } catch (error) {
171
+ Logger.error(TAG, `[RNOH] emitHttpError Errorcode: ${error.code}`);
172
+ }
173
+ }
174
+
175
+ emitScroll(event: OnScrollEvent) {
176
+ try {
177
+ this.eventEmitter!.emit('scroll', {
178
+ contentInset: {
179
+ bottom: 0,
180
+ left: 0,
181
+ right: 0,
182
+ top: 0
183
+ },
184
+ contentOffset: { y: event.yOffset, x: event.xOffset },
185
+ contentSize: { height: 0, width: 0 },
186
+ layoutMeasurement: { height: 0, width: 0 }
187
+ })
188
+ } catch (error) {
189
+ Logger.error(TAG, `[RNOH] emitScroll Errorcode: ${error.code}`);
190
+ }
191
+ }
192
+
193
+ verifyURLFormat(url: string, html: string | undefined, baseUrl: string | undefined) {
194
+ const dataUrlPattern = /^data:text\/html;/;
195
+ if (dataUrlPattern.test(url)){
196
+ if (html != undefined && html != "") {
197
+ if (baseUrl != undefined && baseUrl != "") {
198
+ url = baseUrl.toLowerCase();
199
+ } else {
200
+ url = 'about:blank';
201
+ }
202
+ }
203
+ }
204
+ return url;
205
+ }
206
+
207
+ emitShouldStartLoadWithRequest(event: OnLoadInterceptEvent, html: string | undefined, baseUrl: string | undefined) {
208
+ try {
209
+ this.eventEmitter!.emit('shouldStartLoadWithRequest', {
210
+ url: this.verifyURLFormat(event.data.getRequestUrl(), html, baseUrl),
211
+ loading: false,
212
+ title: this.controller.getTitle(),
213
+ canGoBack: this.controller.accessBackward(),
214
+ canGoForward: this.controller.accessForward(),
215
+ lockIdentifier: this.lockIdentifier,
216
+ navigationType: "other",
217
+ mainDocumentURL: "",
218
+ isTopFrame: false
219
+ })
220
+ } catch (error) {
221
+ Logger.error(TAG, `[RNOH] emitShouldStartLoadWithRequest Errorcode: ${error.code}`);
222
+ }
223
+ }
224
+
225
+ emitShouldStartLoadWithRequestOverrideUrlLoading(event: WebResourceRequest) {
226
+ try {
227
+ this.eventEmitter!.emit('shouldStartLoadWithRequest', {
228
+ url: event.getRequestUrl(),
229
+ loading: false,
230
+ title: this.controller.getTitle(),
231
+ canGoBack: this.controller.accessBackward(),
232
+ canGoForward: this.controller.accessForward(),
233
+ lockIdentifier: this.lockIdentifier,
234
+ navigationType: "other",
235
+ mainDocumentURL: "",
236
+ isTopFrame: false
237
+ })
238
+ } catch (error) {
239
+ Logger.error(TAG, `[RNOH] emitShouldStartLoadWithRequest Errorcode: ${error.code}`);
240
+ }
241
+ }
242
+
243
+ transCacheMode(cacheMode: CACHE_MODE): CacheMode {
244
+ let mode = CacheMode.Default
245
+ switch (cacheMode) {
246
+ case CACHE_MODE.LOAD_DEFAULT:
247
+ mode = CacheMode.Default
248
+ break;
249
+ case CACHE_MODE.LOAD_CACHE_ELSE_NETWORK:
250
+ mode = CacheMode.None
251
+ break;
252
+ case CACHE_MODE.LOAD_NO_CACHE:
253
+ mode = CacheMode.Online
254
+ break;
255
+ case CACHE_MODE.LOAD_CACHE_ONLY:
256
+ mode = CacheMode.Only
257
+ break;
258
+ default:
259
+ break;
260
+ }
261
+ return mode
262
+ }
263
+
264
+ createWebViewEvent(param: CreateWebViewEventInterface): WebViewEventParams {
265
+ let result: WebViewEventParams = new WebViewEventParams(param.type);
266
+ result.loading = param.progress != ONE_HUNDRED
267
+ try {
268
+ result.url = this.controller.getUrl();
269
+ result.title = this.controller.getTitle();
270
+ result.canGoBack = this.controller.accessBackward();
271
+ result.canGoForward = this.controller.accessForward();
272
+ } catch (error) {
273
+ result.url = "";
274
+ result.title = "";
275
+ result.canGoBack = false;
276
+ result.canGoForward = false;
277
+ }
278
+ return result;
279
+ }
280
+
281
+ // 与OS web同事 对齐 默认false 是取消静音
282
+ ignoreSilentHardwareSwitchMethods(ignoreSilentHardwareSwitch: boolean) {
283
+ this.controller.setAudioMuted(!ignoreSilentHardwareSwitch)
284
+ }
285
+
286
+ setCustomUserAgent(customUserAgent?: string, applicationNameForUserAgent?: string): void {
287
+ try {
288
+ if (customUserAgent) {
289
+ let userAgent: string = customUserAgent;
290
+ this.controller.setCustomUserAgent(userAgent);
291
+ }
292
+ if (applicationNameForUserAgent) {
293
+ this.controller.setCustomUserAgent(`${this.controller.getUserAgent()}${applicationNameForUserAgent}`);
294
+ }
295
+ } catch (error) {
296
+ Logger.debug(TAG,
297
+ `[RNOH] setCustomUserAgent ErrorCode: ${error.code}, userAgent: ${customUserAgent}`);
298
+ }
299
+ }
300
+
301
+ injectJavaScript(args: string[]) {
302
+ try {
303
+ this.controller.runJavaScript(args[0])
304
+ } catch (error) {
305
+ Logger.error(TAG, `[RNOH] injectJavaScript Errorcode: ${error.code}`);
306
+ }
307
+ }
308
+
309
+ postMessage(args: string[]) {
310
+ let data = JSON.stringify({ data: args[0] })
311
+ let result: string = "(function () {" +
312
+ "var event;" +
313
+ "var data = " + data.toString() + ";" +
314
+ "try {" +
315
+ "event = new MessageEvent('message', data);" +
316
+ "} catch (e) {" +
317
+ "event = document.createEvent('MessageEvent');" +
318
+ "event.initMessageEvent('message', true, true, data.data, data.origin, data.lastEventId, data.source);" +
319
+ "}" +
320
+ "document.dispatchEvent(event);" +
321
+ "})();"
322
+ try {
323
+ this.controller.runJavaScript(result)
324
+ } catch (error) {
325
+ Logger.error(TAG, `[RNOH] postMessage Errorcode: ${error.code}`);
326
+ }
327
+ }
328
+
329
+ reload() {
330
+ try {
331
+ this.controller.refresh();
332
+ } catch (error) {
333
+ Logger.error(TAG, `[RNOH] reload Errorcode: ${error.code}`);
334
+ }
335
+ }
336
+
337
+ goBack() {
338
+ try {
339
+ this.controller.backward();
340
+ } catch (error) {
341
+ Logger.error(TAG, `[RNOH] goBack Errorcode: ${error.code}`);
342
+ }
343
+ }
344
+
345
+ goForward() {
346
+ try {
347
+ this.controller.forward();
348
+ } catch (error) {
349
+ Logger.error(TAG, `[RNOH] goForward Errorcode: ${error.code}`);
350
+ }
351
+ }
352
+
353
+ requestFocus() {
354
+ try {
355
+ this.controller.requestFocus();
356
+ } catch (error) {
357
+ Logger.error(TAG, `[RNOH] requestFocus Errorcode: ${error.code}`);
358
+ }
359
+ }
360
+
361
+ clearCache(args: string[]) {
362
+ try {
363
+ const removeFlag = !!args[0] === true;
364
+ this.controller.removeCache(removeFlag);
365
+ } catch (error) {
366
+ Logger.error(TAG, `[RNOH] clearCache Errorcode: ${error.code}`);
367
+ }
368
+ }
369
+
370
+ clearHistory() {
371
+ try {
372
+ this.controller.clearHistory();
373
+ } catch (error) {
374
+ Logger.error(TAG, `[RNOH] clearHistory Errorcode: ${error.code}`);
375
+ }
376
+ }
377
+
378
+ stopLoading() {
379
+ try {
380
+ this.controller.stop();
381
+ } catch (error) {
382
+ Logger.error(TAG, `[RNOH] stopLoading Errorcode: ${error.code}`);
383
+ }
384
+ }
385
+
386
+ loadUrl(args: string[]) {
387
+ try {
388
+ this.controller.loadUrl(args[0]);
389
+ } catch (error) {
390
+ Logger.error(TAG, `[RNOH] loadUrl Errorcode: ${error.code}`);
391
+ }
392
+ }
393
+
394
+ registerCommandCallback(commandName: COMMAND_NAME, args: string[]) {
395
+ switch (commandName) {
396
+ case COMMAND_NAME.INJECTJAVASCRIPT:
397
+ this.injectJavaScript(args)
398
+ break
399
+ case COMMAND_NAME.POSTMESSAGE:
400
+ this.postMessage(args)
401
+ break
402
+ case COMMAND_NAME.RELOAD:
403
+ this.reload()
404
+ break
405
+ case COMMAND_NAME.GOBACK:
406
+ this.goBack()
407
+ break
408
+ case COMMAND_NAME.GOFORWARD:
409
+ this.goForward()
410
+ break
411
+ case COMMAND_NAME.REQUESTFOCUS:
412
+ this.requestFocus()
413
+ case COMMAND_NAME.CLEARCACHE:
414
+ this.clearCache(args)
415
+ case COMMAND_NAME.CLEARHISTORY:
416
+ this.clearHistory()
417
+ break
418
+ case COMMAND_NAME.STOPLOADING:
419
+ this.stopLoading()
420
+ break
421
+ case COMMAND_NAME.LOADURL:
422
+ this.loadUrl(args)
423
+ break
424
+ default:
425
+ break
426
+ }
427
+ }
428
+
429
+ setOriginWhitelist(uris: string) {
430
+ try {
431
+ this.controller.setUrlTrustList(uris);
432
+ } catch (error) {
433
+ Logger.error(TAG, `[RNOH] setOriginWhitelist Errorcode: ${error.code}`);
434
+ }
435
+ }
436
+
437
+ setIncognito() {
438
+ this.controller.clearHistory()
439
+ webview.WebCookieManager.clearAllCookiesSync(true);
440
+ webview.WebCookieManager.clearSessionCookieSync()
441
+ webview.WebStorage.deleteAllData();
442
+ }
443
+
444
+ setFraudulentWebsiteWarningEnabled(status: boolean) {
445
+ try {
446
+ this.controller.enableSafeBrowsing(status);
447
+ } catch (error) {
448
+ Logger.error(TAG, `[RNOH] setFraudulentWebsiteWarningEnabled Errorcode: ${error.code}`);
449
+ }
450
+ }
451
+
452
+ onDownloadStart(event: OnDownloadStartEvent) {
453
+ try {
454
+ this.eventEmitter!.emit('fileDownload', {
455
+ downloadUrl: event.url
456
+ })
457
+ } catch (error) {
458
+ Logger.error(TAG, `[RNOH] onDownloadStart Errorcode: ${error.code}`);
459
+ }
460
+ }
461
+
462
+ onRenderExited(event: OnRenderExitedEvent) {
463
+ try {
464
+
465
+ } catch (error) {
466
+ Logger.error(TAG, `[RNOH] onRenderExited Errorcode: ${error.code}`);
467
+ }
468
+ }
469
+
470
+ onTitleReceive(event: OnTitleReceiveEvent) {
471
+ try {
472
+
473
+ } catch (error) {
474
+ Logger.error(TAG, `[RNOH] onTitleReceive Errorcode: ${error.code}`);
475
+ }
476
+ }
477
+
478
+ customMenuSelection(menuItem: TextMenuItem, selectionText: string) {
479
+ try {
480
+ selectionText = JSON.parse(selectionText).selection;
481
+ this.eventEmitter!.emit('customMenuSelection', {
482
+ label: menuItem.content.toString(),
483
+ key: JSON.parse(JSON.stringify(menuItem.id)).id_,
484
+ selectedText: selectionText
485
+ })
486
+ } catch (error) {
487
+ Logger.error(TAG, `[RNOH] customMenuSelection Errorcode: ${error.code}`);
488
+ }
489
+ }
490
+ }