From 52146fe4f9f33865a906d7220ae2ddcd69b557b2 Mon Sep 17 00:00:00 2001 From: Lin Jiacheng Date: Fri, 8 Nov 2024 10:32:52 +0800 Subject: [PATCH 1/2] feat: implement onShouldStartLoadWithRequest and webView turboModule --- .../rn_webview/src/main/ets/RNCWebView.ets | 45 +++++++++++++++++-- .../src/main/ets/RNCWebViewPackage.ts | 23 +++++++++- .../src/main/ets/WebViewBaseOperate.ets | 19 ++++---- .../src/main/ets/WebViewTurboModule.ts | 27 +++++++++++ src/NativeRNCWebViewModule.ts | 13 ++++++ src/WebView.harmony.tsx | 11 ++--- 6 files changed, 116 insertions(+), 22 deletions(-) create mode 100644 harmony/rn_webview/src/main/ets/WebViewTurboModule.ts create mode 100644 src/NativeRNCWebViewModule.ts diff --git a/harmony/rn_webview/src/main/ets/RNCWebView.ets b/harmony/rn_webview/src/main/ets/RNCWebView.ets index 17b4156fb..87c542dcc 100644 --- a/harmony/rn_webview/src/main/ets/RNCWebView.ets +++ b/harmony/rn_webview/src/main/ets/RNCWebView.ets @@ -25,7 +25,7 @@ import { RNComponentContext } from '@rnoh/react-native-openharmony'; import webview from '@ohos.web.webview'; import { url as OSUrl } from '@kit.ArkTS'; -import { RNC } from '@rnoh/react-native-openharmony/generated'; +import { RNC, TM } from '@rnoh/react-native-openharmony/generated'; import Logger from './Logger'; import { BaseOperate } from './WebViewBaseOperate'; import { @@ -45,6 +45,7 @@ import { REFRESH_OFFSET, MINHEIGHT, } from './Magic'; +import { WebViewTurboModule } from './WebViewTurboModule'; import { bundleManager } from '@kit.AbilityKit'; export const TAG = "WebView" @@ -97,6 +98,8 @@ export struct RNCWebView { private cleanUpCallbacks: (() => void)[] = [] private descriptorWrapper: WebViewDescriptor = Object() as WebViewDescriptor private webViewBaseOperate: BaseOperate | null = null + private shouldInterceptLoad: boolean = true + static readonly SHOULD_OVERRIDE_URL_LOADING_TIMEOUT: number = 250 aboutToAppear() { try { @@ -261,7 +264,7 @@ export struct RNCWebView { controllerAttachedInit(): void { this.controllerAttached = true; this.eventEmitter = new RNC.RNCWebView.EventEmitter(this.ctx.rnInstance, this.tag) - this.webViewBaseOperate = new BaseOperate(this.eventEmitter, this.controller) + this.webViewBaseOperate = new BaseOperate(this.tag, this.eventEmitter, this.controller) this.webViewBaseOperate.setCustomUserAgent(this.descriptorWrapper.rawProps.userAgent, this.descriptorWrapper.rawProps.applicationNameForUserAgent) this.webViewBaseOperate.setFraudulentWebsiteWarningEnabled(this.descriptorWrapper.rawProps.fraudulentWebsiteWarningEnabled) @@ -269,6 +272,7 @@ export struct RNCWebView { let uri = this.source.uri if (this.source.html != undefined && this.source.html != "") { try { + this.shouldInterceptLoad = true this.controller.loadData( this.source.html, "text/html", @@ -280,8 +284,10 @@ export struct RNCWebView { Logger.error(TAG, "error:" + error) } } else if (uri != undefined && uri != "") { + this.shouldInterceptLoad = true this.controller.loadUrl(uri, this.headers); } else { + this.shouldInterceptLoad = true this.controller.loadUrl(uri, this.headers); } if (!this.hasRegisterJavaScriptProxy) { @@ -352,8 +358,38 @@ export struct RNCWebView { } onLoadIntercept(event: OnLoadInterceptEvent): boolean { + if (!this.shouldInterceptLoad) { + return false + } + + let webViewTurboModule = this.ctx.rnInstance.getUITurboModule(TM.RNCWebViewModule.NAME) + let timer = setTimeout(()=>{ + webViewTurboModule.callLoadFunction(this.tag) + }, RNCWebView.SHOULD_OVERRIDE_URL_LOADING_TIMEOUT) + + webViewTurboModule.setLoadCallback(this.tag, ()=>{ + clearTimeout(timer) + this.shouldInterceptLoad = false + Logger.debug(TAG, "call setLoadCallback") + if (this.html != "") { + try { + this.controller.loadData( + this.source.html, + "text/html", + "UTF-8", + this.source.baseUrl, + " " + ); + } catch (error) { + Logger.error(TAG, "error: " + error) + } + } else if (this.source.uri != "") { + Logger.debug(TAG, `[RNOH] newDescriptor props update uri: ` + this.source.uri); + this.controller.loadUrl(this.descriptorWrapper.rawProps.newSource.uri, this.headers) + } + }) this.webViewBaseOperate?.emitShouldStartLoadWithRequest(event) - return false + return true } onOverrideUrlLoading(event: WebResourceRequest) { @@ -444,6 +480,7 @@ export struct RNCWebView { this.html = this.source.html if (this.controllerAttached) { try { + this.shouldInterceptLoad = true this.controller.loadData( this.source.html, "text/html", @@ -459,6 +496,7 @@ export struct RNCWebView { Logger.debug(TAG, `[RNOH] newDescriptor props update uri: ` + this.source.uri); this.url = this.source.uri as string; if (this.controllerAttached) { + this.shouldInterceptLoad = true this.controller.loadUrl(this.descriptorWrapper.rawProps.newSource.uri, this.headers) } } @@ -536,6 +574,7 @@ export struct RNCWebView { } }; this.controller.registerJavaScriptProxy(bridge, JAVASCRIPT_INTERFACE, ["postMessage"]) + this.shouldInterceptLoad = true this.source.uri ? this.controller.loadUrl(this.source.uri, this.headers) : this.controller.refresh() this.hasRegisterJavaScriptProxy = true diff --git a/harmony/rn_webview/src/main/ets/RNCWebViewPackage.ts b/harmony/rn_webview/src/main/ets/RNCWebViewPackage.ts index ee5135ecc..9a320ead4 100644 --- a/harmony/rn_webview/src/main/ets/RNCWebViewPackage.ts +++ b/harmony/rn_webview/src/main/ets/RNCWebViewPackage.ts @@ -26,8 +26,10 @@ import type { DescriptorWrapperFactoryByDescriptorType, DescriptorWrapperFactoryByDescriptorTypeCtx, } from '@rnoh/react-native-openharmony/ts'; -import { RNPackage } from '@rnoh/react-native-openharmony/ts'; -import { RNC } from '@rnoh/react-native-openharmony/generated/ts'; +import { RNPackage, UITurboModuleFactory, UITurboModule } from '@rnoh/react-native-openharmony/ts'; +import { RNC, TM } from '@rnoh/react-native-openharmony/generated/ts'; +import { WebViewTurboModule } from './WebViewTurboModule' +import { UITurboModuleContext } from '@rnoh/react-native-openharmony/src/main/ets/RNOH/RNOHContext'; export class RNCWebViewPackage extends RNPackage { createDescriptorWrapperFactoryByDescriptorType(ctx: DescriptorWrapperFactoryByDescriptorTypeCtx): DescriptorWrapperFactoryByDescriptorType { @@ -35,4 +37,21 @@ export class RNCWebViewPackage extends RNPackage { [RNC.RNCWebView.NAME]: (ctx) => new RNC.RNCWebView.DescriptorWrapper(ctx.descriptor) } } + + createUITurboModuleFactory(ctx: UITurboModuleContext): UITurboModuleFactory { + return new WebViewTurboModulesFactory(ctx); + } +} + +class WebViewTurboModulesFactory extends UITurboModuleFactory { + createTurboModule(name: string): UITurboModule | null { + if (name === TM.RNCWebViewModule.NAME) { + return new WebViewTurboModule(this.ctx); + } + return null; + } + + hasTurboModule(name: string): boolean { + return name === TM.RNCWebViewModule.NAME; + } } \ No newline at end of file diff --git a/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets b/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets index cf86012e0..5b01b14b0 100644 --- a/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets +++ b/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets @@ -19,9 +19,10 @@ interface CreateWebViewEventInterface { export class BaseOperate { private eventEmitter: RNC.RNCWebView.EventEmitter private controller: webview.WebviewController - private title: string = '' + private tag: number - constructor(eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController) { + constructor(tag: number, eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController) { + this.tag = tag this.eventEmitter = eventEmitter this.controller = controller } @@ -52,7 +53,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: 0, + lockIdentifier: this.tag, progress: params.progress / ONE_HUNDRED }) } catch (error) { @@ -68,7 +69,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: 0, + lockIdentifier: this.tag, navigationType: "other", mainDocumentURL: "" }) @@ -85,7 +86,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: 0, + lockIdentifier: this.tag, navigationType: "other", mainDocumentURL: "" }) @@ -106,7 +107,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: 0, + lockIdentifier: this.tag, domain: "", code: event.error.getErrorCode(), description: event.error.getErrorInfo() @@ -124,7 +125,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: 0, + lockIdentifier: this.tag, description: event.response.getResponseData(), statusCode: event.response.getResponseCode() }) @@ -159,7 +160,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: 0, + lockIdentifier: this.tag, navigationType: "other", mainDocumentURL: "", isTopFrame: false @@ -177,7 +178,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: 0, + lockIdentifier: this.tag, navigationType: "other", mainDocumentURL: "", isTopFrame: false diff --git a/harmony/rn_webview/src/main/ets/WebViewTurboModule.ts b/harmony/rn_webview/src/main/ets/WebViewTurboModule.ts new file mode 100644 index 000000000..ace4f4244 --- /dev/null +++ b/harmony/rn_webview/src/main/ets/WebViewTurboModule.ts @@ -0,0 +1,27 @@ +import { UITurboModule } from '@rnoh/react-native-openharmony/ts'; +import { TM } from '@rnoh/react-native-openharmony/generated/ts'; + +export class WebViewTurboModule extends UITurboModule implements TM.RNCWebViewModule.Spec { + private loadCallbackMap : Map void> = new Map(); + + isFileUploadSupported(): Promise { + return Promise.resolve(true) + } + + shouldStartLoadWithLockIdentifier(shouldStart: boolean, lockIdentifier: number): void { + if (shouldStart) { + this.callLoadFunction(lockIdentifier) + }else { + this.loadCallbackMap.delete(lockIdentifier) + } + } + + setLoadCallback(tag:number, cb: ()=> void) { + this.loadCallbackMap.set(tag,cb) + } + + callLoadFunction(tag:number){ + this.loadCallbackMap.get(tag)?.() + this.loadCallbackMap.delete(tag) + } +} \ No newline at end of file diff --git a/src/NativeRNCWebViewModule.ts b/src/NativeRNCWebViewModule.ts new file mode 100644 index 000000000..220d2390f --- /dev/null +++ b/src/NativeRNCWebViewModule.ts @@ -0,0 +1,13 @@ +import type { TurboModule } from 'react-native'; +import { TurboModuleRegistry } from 'react-native'; +import { Double } from 'react-native/Libraries/Types/CodegenTypes'; + +export interface Spec extends TurboModule { + isFileUploadSupported(): Promise; + shouldStartLoadWithLockIdentifier( + shouldStart: boolean, + lockIdentifier: Double + ): void; +} + +export default TurboModuleRegistry.getEnforcing('RNCWebViewModule'); diff --git a/src/WebView.harmony.tsx b/src/WebView.harmony.tsx index 0aa109195..cc91720ee 100644 --- a/src/WebView.harmony.tsx +++ b/src/WebView.harmony.tsx @@ -5,10 +5,10 @@ import React, { useRef } from 'react'; import { Image, View, ImageSourcePropType, HostComponent } from 'react-native'; -import { Double } from 'react-native/Libraries/Types/CodegenTypes'; import invariant from 'invariant'; import RNCWebView, { Commands, NativeProps } from './RNCWebViewNativeComponent'; +import RNCWebViewModule from './NativeRNCWebViewModule'; import { defaultOriginWhitelist, @@ -46,11 +46,6 @@ const useWarnIfChanges = (value: T, name: string) => { } }; -const shouldStartLoadWithLockIdentifier: ( - shouldStart: boolean, - lockIdentifier: Double -) => void = () => { } - const WebViewComponent = forwardRef<{}, IOSWebViewProps & { scalesPageToFit: boolean, minimumFontSize: number, thirdPartyCookiesEnabled: boolean, geolocationEnabled: boolean }>( ( { @@ -113,7 +108,7 @@ const WebViewComponent = forwardRef<{}, IOSWebViewProps & { scalesPageToFit: boo const onShouldStartLoadWithRequestCallback = useCallback( (shouldStart: boolean, _url: string, lockIdentifier = 0) => { - shouldStartLoadWithLockIdentifier( + RNCWebViewModule.shouldStartLoadWithLockIdentifier( shouldStart, lockIdentifier ); @@ -328,4 +323,4 @@ const isFileUploadSupported: () => Promise = async () => true; const WebView = Object.assign(WebViewComponent, { isFileUploadSupported }); -export default WebView; +export default WebView; \ No newline at end of file From d1ead818b43cc34a44295afd385d0d1c3ef4e437 Mon Sep 17 00:00:00 2001 From: Lin Jiacheng Date: Fri, 8 Nov 2024 17:02:32 +0800 Subject: [PATCH 2/2] fix: resolve the issue of onShouldStartLoadWithRequest exception caused by multiple loading of webview --- .../rn_webview/src/main/ets/RNCWebView.ets | 42 ++++++++++++------- .../src/main/ets/WebViewBaseOperate.ets | 34 ++++++++++----- .../src/main/ets/WebViewTurboModule.ts | 22 +++++++--- 3 files changed, 67 insertions(+), 31 deletions(-) diff --git a/harmony/rn_webview/src/main/ets/RNCWebView.ets b/harmony/rn_webview/src/main/ets/RNCWebView.ets index 87c542dcc..ae79933f0 100644 --- a/harmony/rn_webview/src/main/ets/RNCWebView.ets +++ b/harmony/rn_webview/src/main/ets/RNCWebView.ets @@ -74,7 +74,6 @@ export struct RNCWebView { overScrollMode: OverScrollMode = OverScrollMode.NEVER; progress: number = ZERO; cacheMode: number = CacheMode.Default; - lockIdentifier: string = ""; requestUrl: string = ""; bundleName: string = ""; messagingEnabled: boolean = false; @@ -99,6 +98,7 @@ export struct RNCWebView { private descriptorWrapper: WebViewDescriptor = Object() as WebViewDescriptor private webViewBaseOperate: BaseOperate | null = null private shouldInterceptLoad: boolean = true + private callLoadTimer: number = -1 static readonly SHOULD_OVERRIDE_URL_LOADING_TIMEOUT: number = 250 aboutToAppear() { @@ -264,7 +264,7 @@ export struct RNCWebView { controllerAttachedInit(): void { this.controllerAttached = true; this.eventEmitter = new RNC.RNCWebView.EventEmitter(this.ctx.rnInstance, this.tag) - this.webViewBaseOperate = new BaseOperate(this.tag, this.eventEmitter, this.controller) + this.webViewBaseOperate = new BaseOperate(this.eventEmitter, this.controller) this.webViewBaseOperate.setCustomUserAgent(this.descriptorWrapper.rawProps.userAgent, this.descriptorWrapper.rawProps.applicationNameForUserAgent) this.webViewBaseOperate.setFraudulentWebsiteWarningEnabled(this.descriptorWrapper.rawProps.fraudulentWebsiteWarningEnabled) @@ -272,7 +272,7 @@ export struct RNCWebView { let uri = this.source.uri if (this.source.html != undefined && this.source.html != "") { try { - this.shouldInterceptLoad = true + this.resetIntercept() this.controller.loadData( this.source.html, "text/html", @@ -284,10 +284,10 @@ export struct RNCWebView { Logger.error(TAG, "error:" + error) } } else if (uri != undefined && uri != "") { - this.shouldInterceptLoad = true + this.resetIntercept() this.controller.loadUrl(uri, this.headers); } else { - this.shouldInterceptLoad = true + this.resetIntercept() this.controller.loadUrl(uri, this.headers); } if (!this.hasRegisterJavaScriptProxy) { @@ -358,19 +358,22 @@ export struct RNCWebView { } onLoadIntercept(event: OnLoadInterceptEvent): boolean { + Logger.debug(TAG, `onLoadIntercept request url:${event.data.getRequestUrl()} ,shouldInterceptLoad:${this.shouldInterceptLoad}`) if (!this.shouldInterceptLoad) { return false } + let lockIdentifier = this.webViewBaseOperate?.getLockIdentifier() let webViewTurboModule = this.ctx.rnInstance.getUITurboModule(TM.RNCWebViewModule.NAME) - let timer = setTimeout(()=>{ - webViewTurboModule.callLoadFunction(this.tag) + this.callLoadTimer = setTimeout(()=>{ + Logger.debug(TAG, "call setTimeout") + webViewTurboModule.callLoadFunction(lockIdentifier) }, RNCWebView.SHOULD_OVERRIDE_URL_LOADING_TIMEOUT) - webViewTurboModule.setLoadCallback(this.tag, ()=>{ - clearTimeout(timer) - this.shouldInterceptLoad = false + webViewTurboModule.setLoadCallback(lockIdentifier, ()=>{ Logger.debug(TAG, "call setLoadCallback") + clearTimeout(this.callLoadTimer) + this.shouldInterceptLoad = false if (this.html != "") { try { this.controller.loadData( @@ -384,7 +387,7 @@ export struct RNCWebView { Logger.error(TAG, "error: " + error) } } else if (this.source.uri != "") { - Logger.debug(TAG, `[RNOH] newDescriptor props update uri: ` + this.source.uri); + Logger.debug(TAG, `uri: ` + this.source.uri); this.controller.loadUrl(this.descriptorWrapper.rawProps.newSource.uri, this.headers) } }) @@ -397,6 +400,15 @@ export struct RNCWebView { return false } + resetIntercept() { + Logger.debug(TAG,"resetIntercept") + this.ctx.rnInstance.getUITurboModule(TM.RNCWebViewModule.NAME) + .clearLoadFunction(this.webViewBaseOperate?.getLockIdentifier()) + clearTimeout(this.callLoadTimer) + this.shouldInterceptLoad = true + this.webViewBaseOperate?.setLockIdentifier(BaseOperate.generateLockIdentifier()) + } + build() { Stack() { Web({ src: "", controller: this.controller, renderMode: this.renderMode }) @@ -480,7 +492,7 @@ export struct RNCWebView { this.html = this.source.html if (this.controllerAttached) { try { - this.shouldInterceptLoad = true + this.resetIntercept() this.controller.loadData( this.source.html, "text/html", @@ -496,7 +508,7 @@ export struct RNCWebView { Logger.debug(TAG, `[RNOH] newDescriptor props update uri: ` + this.source.uri); this.url = this.source.uri as string; if (this.controllerAttached) { - this.shouldInterceptLoad = true + this.resetIntercept() this.controller.loadUrl(this.descriptorWrapper.rawProps.newSource.uri, this.headers) } } @@ -567,14 +579,14 @@ export struct RNCWebView { let result: WebViewEventParams = this.createWebViewEvent("onMessage") if (result) { result.data = data.toString() - result.lockIdentifier = ZERO + result.lockIdentifier = this.webViewBaseOperate?.getLockIdentifier() this.eventEmitter!.emit("message", result as ResultType); } } } }; this.controller.registerJavaScriptProxy(bridge, JAVASCRIPT_INTERFACE, ["postMessage"]) - this.shouldInterceptLoad = true + this.resetIntercept() this.source.uri ? this.controller.loadUrl(this.source.uri, this.headers) : this.controller.refresh() this.hasRegisterJavaScriptProxy = true diff --git a/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets b/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets index 5b01b14b0..09c91d481 100644 --- a/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets +++ b/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets @@ -17,14 +17,15 @@ interface CreateWebViewEventInterface { } export class BaseOperate { + private static LOCK_IDENTIFIER = 0 private eventEmitter: RNC.RNCWebView.EventEmitter private controller: webview.WebviewController - private tag: number + private lockIdentifier: number = 0 - constructor(tag: number, eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController) { - this.tag = tag + constructor(eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController) { this.eventEmitter = eventEmitter this.controller = controller + this.lockIdentifier = BaseOperate.generateLockIdentifier() } static setThirdPartyCookiesEnabled(status: boolean) { @@ -45,6 +46,19 @@ export class BaseOperate { } } + static generateLockIdentifier(): number { + BaseOperate.LOCK_IDENTIFIER = BaseOperate.LOCK_IDENTIFIER + 1 + return BaseOperate.LOCK_IDENTIFIER + } + + getLockIdentifier():number { + return this.lockIdentifier + } + + setLockIdentifier(lockIdentifier: number) { + this.lockIdentifier = lockIdentifier + } + emitProgressChange(params: ProgressInterface) { try { this.eventEmitter!.emit('loadingProgress', { @@ -53,7 +67,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: this.tag, + lockIdentifier: this.lockIdentifier, progress: params.progress / ONE_HUNDRED }) } catch (error) { @@ -69,7 +83,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: this.tag, + lockIdentifier: this.lockIdentifier, navigationType: "other", mainDocumentURL: "" }) @@ -86,7 +100,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: this.tag, + lockIdentifier: this.lockIdentifier, navigationType: "other", mainDocumentURL: "" }) @@ -107,7 +121,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: this.tag, + lockIdentifier: this.lockIdentifier, domain: "", code: event.error.getErrorCode(), description: event.error.getErrorInfo() @@ -125,7 +139,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: this.tag, + lockIdentifier: this.lockIdentifier, description: event.response.getResponseData(), statusCode: event.response.getResponseCode() }) @@ -160,7 +174,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: this.tag, + lockIdentifier: this.lockIdentifier, navigationType: "other", mainDocumentURL: "", isTopFrame: false @@ -178,7 +192,7 @@ export class BaseOperate { title: this.controller.getTitle(), canGoBack: this.controller.accessBackward(), canGoForward: this.controller.accessForward(), - lockIdentifier: this.tag, + lockIdentifier: this.lockIdentifier, navigationType: "other", mainDocumentURL: "", isTopFrame: false diff --git a/harmony/rn_webview/src/main/ets/WebViewTurboModule.ts b/harmony/rn_webview/src/main/ets/WebViewTurboModule.ts index ace4f4244..043fd00f0 100644 --- a/harmony/rn_webview/src/main/ets/WebViewTurboModule.ts +++ b/harmony/rn_webview/src/main/ets/WebViewTurboModule.ts @@ -1,5 +1,8 @@ import { UITurboModule } from '@rnoh/react-native-openharmony/ts'; import { TM } from '@rnoh/react-native-openharmony/generated/ts'; +import Logger from './Logger'; + +const TAG = "WebViewTurboModule" export class WebViewTurboModule extends UITurboModule implements TM.RNCWebViewModule.Spec { private loadCallbackMap : Map void> = new Map(); @@ -9,19 +12,26 @@ export class WebViewTurboModule extends UITurboModule implements TM.RNCWebViewMo } shouldStartLoadWithLockIdentifier(shouldStart: boolean, lockIdentifier: number): void { + Logger.debug(TAG,`shouldStartLoadWithLockIdentifier shouldStart:${shouldStart},lockIdentifier:${lockIdentifier}`) if (shouldStart) { this.callLoadFunction(lockIdentifier) }else { - this.loadCallbackMap.delete(lockIdentifier) + this.clearLoadFunction(lockIdentifier) } } - setLoadCallback(tag:number, cb: ()=> void) { - this.loadCallbackMap.set(tag,cb) + setLoadCallback(lockIdentifier:number, cb: ()=> void) { + this.loadCallbackMap.set(lockIdentifier,cb) + } + + callLoadFunction(lockIdentifier:number){ + Logger.debug(TAG,`callLoadFunction function:${JSON.stringify(this.loadCallbackMap.get(lockIdentifier))}`) + this.loadCallbackMap.get(lockIdentifier)?.() + this.clearLoadFunction(lockIdentifier) } - callLoadFunction(tag:number){ - this.loadCallbackMap.get(tag)?.() - this.loadCallbackMap.delete(tag) + clearLoadFunction(lockIdentifier:number){ + Logger.debug(TAG,`clearLoadFunction lockIdentifier:${lockIdentifier} `) + this.loadCallbackMap.delete(lockIdentifier) } } \ No newline at end of file