From d543f11652dbf0064dec519e3a8b42628ccc697e Mon Sep 17 00:00:00 2001 From: wangtian Date: Tue, 11 Feb 2025 19:48:29 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=A7=A3=E5=86=B3javaScriptBridgeInterface?= =?UTF-8?q?=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangtian --- .../plugin/webview/in_app_webview/InAppWebView.ets | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/InAppWebView.ets b/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/InAppWebView.ets index c533bee1..62a107fb 100644 --- a/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/InAppWebView.ets +++ b/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/InAppWebView.ets @@ -695,6 +695,12 @@ export default class InAppWebView implements InAppWebViewInterface { this.webViewAssetLoaderExt.dispose(); this.webViewAssetLoaderExt = null; } + if(this.javaScriptBridgeInterface != null){ + this.controller.deleteJavaScriptRegister(JavaScriptBridgeJS.JAVASCRIPT_BRIDGE_NAME); + this.controller.refresh(); + this.javaScriptBridgeInterface.dispose(); + this.javaScriptBridgeInterface = null; + } } getUrl(): string { -- Gitee From 20e913c0a614c17df7adfb13a21772c26a9eaa98 Mon Sep 17 00:00:00 2001 From: wangtian Date: Fri, 14 Feb 2025 15:57:13 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=AE=9E=E7=8E=B0shouldInterceptRequest?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=E8=AF=B7=E6=B1=82=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangtian --- .../ohos/build-profile.json5 | 5 +++ .../ohos/src/main/cpp/CMakeLists.txt | 17 +++++++++ .../ohos/src/main/cpp/napi_init.cpp | 35 +++++++++++++++++++ .../src/main/cpp/types/libentry/Index.d.ts | 1 + .../main/cpp/types/libentry/oh-package.json5 | 6 ++++ .../types/SyncBaseCallbackResultImpl.ets | 18 +++------- .../plugin/webview/WebViewChannelDelegate.ets | 4 +-- .../in_app_webview/InAppWebViewClient.ets | 32 +++++++++-------- 8 files changed, 88 insertions(+), 30 deletions(-) create mode 100644 flutter_inappwebview_ohos/ohos/src/main/cpp/CMakeLists.txt create mode 100644 flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp create mode 100644 flutter_inappwebview_ohos/ohos/src/main/cpp/types/libentry/Index.d.ts create mode 100644 flutter_inappwebview_ohos/ohos/src/main/cpp/types/libentry/oh-package.json5 diff --git a/flutter_inappwebview_ohos/ohos/build-profile.json5 b/flutter_inappwebview_ohos/ohos/build-profile.json5 index 07ffd85b..01366d5c 100644 --- a/flutter_inappwebview_ohos/ohos/build-profile.json5 +++ b/flutter_inappwebview_ohos/ohos/build-profile.json5 @@ -16,6 +16,11 @@ { "apiType": "stageMode", "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } }, "targets": [ { diff --git a/flutter_inappwebview_ohos/ohos/src/main/cpp/CMakeLists.txt b/flutter_inappwebview_ohos/ohos/src/main/cpp/CMakeLists.txt new file mode 100644 index 00000000..f763aea4 --- /dev/null +++ b/flutter_inappwebview_ohos/ohos/src/main/cpp/CMakeLists.txt @@ -0,0 +1,17 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(MyApplication2) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(entry SHARED napi_init.cpp) +target_link_libraries(entry PUBLIC libace_napi.z.so + libuv.so +) \ No newline at end of file diff --git a/flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp b/flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp new file mode 100644 index 00000000..9b678eb1 --- /dev/null +++ b/flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp @@ -0,0 +1,35 @@ +#include "napi/native_api.h" +#include + +static napi_value UvRunNowait(napi_env env, napi_callback_info info) +{ + uv_loop_s* loop = nullptr; + napi_get_uv_event_loop(env, &loop); + uv_run(loop, UV_RUN_NOWAIT); + return nullptr; +} + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) { + napi_property_descriptor desc[] = { + {"uvRunNowait", nullptr, UvRunNowait, nullptr, nullptr, nullptr, napi_default, nullptr}, + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "entry", + .nm_priv = ((void*)0), + .reserved = { 0 }, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) +{ + napi_module_register(&demoModule); +} diff --git a/flutter_inappwebview_ohos/ohos/src/main/cpp/types/libentry/Index.d.ts b/flutter_inappwebview_ohos/ohos/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 00000000..f8c96b4f --- /dev/null +++ b/flutter_inappwebview_ohos/ohos/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1 @@ +export const uvRunNowait: () => void; \ No newline at end of file diff --git a/flutter_inappwebview_ohos/ohos/src/main/cpp/types/libentry/oh-package.json5 b/flutter_inappwebview_ohos/ohos/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 00000000..ea410725 --- /dev/null +++ b/flutter_inappwebview_ohos/ohos/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,6 @@ +{ + "name": "libentry.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/types/SyncBaseCallbackResultImpl.ets b/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/types/SyncBaseCallbackResultImpl.ets index a57c9ceb..daa3a606 100644 --- a/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/types/SyncBaseCallbackResultImpl.ets +++ b/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/types/SyncBaseCallbackResultImpl.ets @@ -15,6 +15,7 @@ import { Any } from '@ohos/flutter_ohos'; import BaseCallbackResultImpl from './BaseCallbackResultImpl'; +import napi from 'libentry.so'; export default class SyncBaseCallbackResultImpl extends BaseCallbackResultImpl { public result: T | null = null; @@ -40,19 +41,10 @@ export default class SyncBaseCallbackResultImpl extends BaseCallbackResultImp } } - waitResponse(): Promise { - return new Promise((resolve, reject) => { - try { - let intervalId = setInterval(() => { - if (this.responsed) { - clearInterval(intervalId) - resolve() - } - }, 20) - } catch (err) { - reject() - } - }) + waitResponse(): void { + while (!this.responsed) { + napi.uvRunNowait(); + } } error(errorCode: string, errorMessage: string, errorDetails: Any) { diff --git a/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/WebViewChannelDelegate.ets b/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/WebViewChannelDelegate.ets index 84ce295e..b5e5079c 100644 --- a/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/WebViewChannelDelegate.ets +++ b/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/WebViewChannelDelegate.ets @@ -966,12 +966,12 @@ export default class WebViewChannelDelegate extends ChannelDelegateImpl { channel.invokeMethod("onReceivedClientCertRequest", await challenge.toMap(), callback); } - async shouldInterceptRequest(request: WebResourceRequestExt) { + shouldInterceptRequest(request: WebResourceRequestExt){ let channel = this.getChannel(); if (channel == null) return null; let callback = new SyncShouldInterceptRequestCallback(); channel.invokeMethod("shouldInterceptRequest", request.toMap(), callback); - await callback.waitResponse() + callback.waitResponse() return callback.result; } diff --git a/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/InAppWebViewClient.ets b/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/InAppWebViewClient.ets index 72ed83e5..7075594a 100644 --- a/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/InAppWebViewClient.ets +++ b/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/InAppWebViewClient.ets @@ -73,7 +73,7 @@ import ClientCertResponse from '../../types/ClientCertResponse'; import CreateWindowAction from '../../types/CreateWindowAction'; import SafeBrowsingResponse from '../../types/SafeBrowsingResponse'; import { cert } from '@kit.DeviceCertificateKit' -import { List } from '@kit.ArkTS'; +import { List, util } from '@kit.ArkTS'; import { common } from '@kit.AbilityKit'; const TAG = "InAppWebViewClient"; @@ -319,20 +319,22 @@ export default class InAppWebViewClient { if (this.inAppWebView.customSettings.useShouldInterceptRequest) { if (this.inAppWebView.channelDelegate != null) { try { - this.inAppWebView.channelDelegate.shouldInterceptRequest(request).then(response => { - if (response != null) { - let webResourceResponse = new WebResourceResponse(); - webResourceResponse.setResponseData(response.getData()) - webResourceResponse.setResponseMimeType(response.getContentType()) - webResourceResponse.setResponseEncoding(response.getContentEncoding()) - webResourceResponse.setResponseCode(response.getStatusCode()) - webResourceResponse.setReasonMessage(response.getReasonPhrase()) - webResourceResponse.setResponseHeader(response.getHeaderArray()) - webResourceResponse.setResponseIsReady(true) - return webResourceResponse - } - return null; - }) + let response = this.inAppWebView.channelDelegate.shouldInterceptRequest(request); + if (response != null) { + const uint8 = new Uint8Array(response.getData()); + let data = util.TextDecoder.create().decodeToString(uint8); + let webResourceResponse = new WebResourceResponse(); + webResourceResponse.setResponseData(data) + webResourceResponse.setResponseMimeType(response.getContentType()) + webResourceResponse.setResponseEncoding(response.getContentEncoding()) + webResourceResponse.setResponseCode(response.getStatusCode()) + webResourceResponse.setReasonMessage(response.getReasonPhrase()) + webResourceResponse.setResponseHeader(response.getHeaderArray()) + webResourceResponse.setResponseIsReady(true) + return webResourceResponse + } + Log.i(TAG, 'return null '); + return null; } catch (e) { Log.e(TAG, "", e); } -- Gitee From bcf9e2e3ef84b4034f33357aa592919d2597d380 Mon Sep 17 00:00:00 2001 From: wangtian Date: Fri, 14 Feb 2025 17:04:27 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=B3=84=E9=9C=B2OOM=E9=97=AE=E9=A2=98=EF=BC=8C=E9=87=8A?= =?UTF-8?q?=E6=94=BEInAppWebView=E7=9B=B8=E5=85=B3=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangtian --- .../webview/in_app_webview/InAppWebView.ets | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/InAppWebView.ets b/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/InAppWebView.ets index 62a107fb..da25652e 100644 --- a/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/InAppWebView.ets +++ b/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/InAppWebView.ets @@ -695,12 +695,30 @@ export default class InAppWebView implements InAppWebViewInterface { this.webViewAssetLoaderExt.dispose(); this.webViewAssetLoaderExt = null; } + if(this.controller){ + try { + this.controller.deleteJavaScriptRegister(JavaScriptBridgeJS.JAVASCRIPT_BRIDGE_NAME); + this.controller.refresh(); + } catch (error) { + Log.e(TAG, `ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); + } + } if(this.javaScriptBridgeInterface != null){ - this.controller.deleteJavaScriptRegister(JavaScriptBridgeJS.JAVASCRIPT_BRIDGE_NAME); - this.controller.refresh(); this.javaScriptBridgeInterface.dispose(); this.javaScriptBridgeInterface = null; } + if(this.findInteractionController != null){ + this.findInteractionController.dispose(); + this.findInteractionController = null; + } + if(this.pullToRefreshLayout != null){ + this.pullToRefreshLayout.dispose(); + this.pullToRefreshLayout = null; + } + if(this.channelDelegate != null){ + this.channelDelegate.dispose(); + this.channelDelegate = null; + } } getUrl(): string { -- Gitee