diff --git a/flutter_inappwebview_ohos/ohos/build-profile.json5 b/flutter_inappwebview_ohos/ohos/build-profile.json5 index 07ffd85bb358ce17c64bd6e3aaa241ed141bfb7b..01366d5cc4b5db377444fbc0f659db07180b09a6 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 0000000000000000000000000000000000000000..f763aea477b509d37f71c54478322ecd31786824 --- /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 0000000000000000000000000000000000000000..d84983edaaac4cc24b05fd9eaf648bd194cfcb61 --- /dev/null +++ b/flutter_inappwebview_ohos/ohos/src/main/cpp/napi_init.cpp @@ -0,0 +1,51 @@ +/* +* 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 + +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 0000000000000000000000000000000000000000..7cdb0f2a3b6456b26f6003a5eafdafce2783ea80 --- /dev/null +++ b/flutter_inappwebview_ohos/ohos/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +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 new file mode 100644 index 0000000000000000000000000000000000000000..c3dc8a047541add56134ad738478a5ef782b11cb --- /dev/null +++ b/flutter_inappwebview_ohos/ohos/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* +* 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", + "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 a57c9ceb2d0b547465a6c831fe9ccdc753efdd47..daa3a606fb7e468fa08d0b88c967db6d02ceb94f 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 84ce295e9dd8c7bf613054751691dbde77e8c2a1..b5e5079c55bf2a43d97ffeb285f47acba89cdbf3 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 72ed83e5544ccf4f4836f054ddb0d5f3ca370048..7075594ab431cfdd8a4a87e5939d7dcb4c0e3184 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); }