From 8c29884f0a94df93faf94f7e36ef7aa0323859fb Mon Sep 17 00:00:00 2001 From: wangtian Date: Fri, 14 Feb 2025 16:07:28 +0800 Subject: [PATCH 1/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 a196aba3ebec4e9830de8567a3b36f2d7cd6ff5d Mon Sep 17 00:00:00 2001 From: wangtian Date: Wed, 19 Feb 2025 10:56:07 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=89=88=E6=9D=83=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangtian --- .../ohos/src/main/cpp/napi_init.cpp | 18 +++++++++++++++++- .../src/main/cpp/types/libentry/Index.d.ts | 15 +++++++++++++++ .../main/cpp/types/libentry/oh-package.json5 | 15 +++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp b/flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp index 9b678eb1..b8d95cdf 100644 --- a/flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp +++ b/flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp @@ -1,3 +1,18 @@ +/* +* Copyright (c) 2025 Hunan OpenValley Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + #include "napi/native_api.h" #include @@ -10,7 +25,8 @@ static napi_value UvRunNowait(napi_env env, napi_callback_info info) } EXTERN_C_START -static napi_value Init(napi_env env, napi_value exports) { +static napi_value Init(napi_env env, napi_value exports) +{ napi_property_descriptor desc[] = { {"uvRunNowait", nullptr, UvRunNowait, nullptr, nullptr, nullptr, napi_default, nullptr}, }; 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 index f8c96b4f..7cdb0f2a 100644 --- 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 @@ -1 +1,16 @@ +/* +* Copyright (c) 2025 Hunan OpenValley Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + 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 index ea410725..c3dc8a04 100644 --- 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 @@ -1,3 +1,18 @@ +/* +* Copyright (c) 2025 Hunan OpenValley Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + { "name": "libentry.so", "types": "./Index.d.ts", -- Gitee From d2a8477daf755363665c8501a71f57d2445a1dce Mon Sep 17 00:00:00 2001 From: wangtian Date: Wed, 19 Feb 2025 11:04:49 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=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 --- flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp b/flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp index b8d95cdf..d84983ed 100644 --- a/flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp +++ b/flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp @@ -25,7 +25,7 @@ static napi_value UvRunNowait(napi_env env, napi_callback_info info) } EXTERN_C_START -static napi_value Init(napi_env env, napi_value exports) +static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor desc[] = { {"uvRunNowait", nullptr, UvRunNowait, nullptr, nullptr, nullptr, napi_default, nullptr}, -- Gitee