From 0d067817fc181ad2160ac97684d4df05357db4f5 Mon Sep 17 00:00:00 2001 From: chenguanhao Date: Sat, 13 Sep 2025 16:05:07 +0800 Subject: [PATCH] add arkts1.2 interfaces Signed-off-by: chenguanhao --- interfaces/kits/ani/webview/BUILD.gn | 1 + .../ani/webview/ets/@ohos.web.webview.ets | 7 + .../webviewcontroller/webview_controller.cpp | 29 ++++ .../webviewcontroller/webview_controller.h | 6 + .../ani_webview_controller.cpp | 126 +++++++++++++++++- 5 files changed, 168 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/ani/webview/BUILD.gn b/interfaces/kits/ani/webview/BUILD.gn index 6bf09842c..b8734e326 100644 --- a/interfaces/kits/ani/webview/BUILD.gn +++ b/interfaces/kits/ani/webview/BUILD.gn @@ -121,6 +121,7 @@ ohos_shared_library("webview_ani") { "../../../../ohos_nweb:libnweb", "../../../native:ohweb", "../../napi/protos:proto_gen", + "//base/web/webview/arkweb_utils:libarkweb_utils", ] external_deps = [ "ability_base:extractortool", diff --git a/interfaces/kits/ani/webview/ets/@ohos.web.webview.ets b/interfaces/kits/ani/webview/ets/@ohos.web.webview.ets index 291a41bdd..e838b78e3 100644 --- a/interfaces/kits/ani/webview/ets/@ohos.web.webview.ets +++ b/interfaces/kits/ani/webview/ets/@ohos.web.webview.ets @@ -772,6 +772,13 @@ export default namespace webview { } native getCertificateSync(): Array; + native getProgress(): int; + native static setAppCustomUserAgent(userAgent: string): void; + native static setUserAgentForHosts(userAgent: string, hosts: Array): void; + native static enablePrivateNetworkAccess(enable: boolean): void; + native static isPrivateNetworkAccessEnabled(): boolean; + native setErrorPageEnabled(enable: boolean): void; + native getErrorPageEnabled(): boolean; } export class WebSchemeHandlerResponse { diff --git a/interfaces/kits/ani/webview/native/webviewcontroller/webview_controller.cpp b/interfaces/kits/ani/webview/native/webviewcontroller/webview_controller.cpp index c823fdeb0..a4ea4569f 100644 --- a/interfaces/kits/ani/webview/native/webviewcontroller/webview_controller.cpp +++ b/interfaces/kits/ani/webview/native/webviewcontroller/webview_controller.cpp @@ -2049,5 +2049,34 @@ int32_t WebviewController::GetNWebId() return nwebId_; } +int32_t WebviewController::GetProgress() +{ + int32_t progress = 0; + auto nweb_ptr = NWebHelper::Instance().GetNWeb(nwebId_); + if (nweb_ptr) { + progress = nweb_ptr->PageLoadProgress(); + } + return progress; +} + +ErrCode WebviewController::SetErrorPageEnabled(bool enable) +{ + auto nweb_ptr = NWebHelper::Instance().GetNWeb(nwebId_); + if (!nweb_ptr) { + return INIT_ERROR; + } + nweb_ptr->SetErrorPageEnabled(enable); + return NWebError::NO_ERROR; +} + +bool WebviewController::GetErrorPageEnabled() +{ + auto nweb_ptr = NWebHelper::Instance().GetNWeb(nwebId_); + if (!nweb_ptr) { + return false; + } + return nweb_ptr->GetErrorPageEnabled(); +} + } // namespace NWeb } // namespace OHOS diff --git a/interfaces/kits/ani/webview/native/webviewcontroller/webview_controller.h b/interfaces/kits/ani/webview/native/webviewcontroller/webview_controller.h index f7b146824..22778e987 100644 --- a/interfaces/kits/ani/webview/native/webviewcontroller/webview_controller.h +++ b/interfaces/kits/ani/webview/native/webviewcontroller/webview_controller.h @@ -393,6 +393,12 @@ public: int32_t GetNWebId(); + int32_t GetProgress(); + + ErrCode SetErrorPageEnabled(bool enable); + + bool GetErrorPageEnabled(); + private: int ConverToWebHitTestType(int hitType); diff --git a/interfaces/kits/ani/webview/src/webviewcontroller/ani_webview_controller.cpp b/interfaces/kits/ani/webview/src/webviewcontroller/ani_webview_controller.cpp index 5513b8cd5..99e74978c 100644 --- a/interfaces/kits/ani/webview/src/webviewcontroller/ani_webview_controller.cpp +++ b/interfaces/kits/ani/webview/src/webviewcontroller/ani_webview_controller.cpp @@ -76,6 +76,7 @@ #include "ani_web_scheme_handler_request.h" #include "web_scheme_handler_request.h" #include "arkweb_scheme_handler.h" +#include "arkweb_utils.h" namespace OHOS { namespace NWeb { @@ -2162,8 +2163,9 @@ static void ClearWebSchemeHandler(ani_env *env, ani_object object) if (controller->ClearWebSchemeHandler() != 0) { WVLOG_E("AniWebviewController::ClearWebSchemeHandler failed"); + } else { + WVLOG_I("AniWebviewController::ClearWebSchemeHandler successful"); } - WVLOG_D("AniWebviewController::ClearWebSchemeHandler successful"); } static ani_ref GetItemAtIndex(ani_env *env, ani_object object, ani_int aniIndex) @@ -5224,6 +5226,121 @@ static ani_object GetCertificateSync(ani_env* env, ani_object object) return certificateObj; } +static ani_int GetProgress(ani_env* env, ani_object object) +{ + ani_int progress = 0; + if (env == nullptr) { + WVLOG_E("env is nullptr"); + return progress; + } + + auto* controller = reinterpret_cast(AniParseUtils::Unwrap(env, object)); + if (!controller || !controller->IsInit()) { + WVLOG_E("GetProgress: get controller failed"); + return progress; + } + return static_cast(controller->GetProgress()); +} + +static void SetAppCustomUserAgent(ani_env* env, ani_object object, ani_object aniUA) +{ + if (env == nullptr) { + WVLOG_E("env is nullptr"); + return; + } + + std::string userAgent; + if (!AniParseUtils::ParseString(env, aniUA, userAgent)) { + AniBusinessError::ThrowError(env, NWebError::PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "userAgent", "string")); + return; + } + NWebHelper::Instance().SetAppCustomUserAgent(userAgent); +} + +static void SetUserAgentForHosts(ani_env* env, ani_object object, ani_object aniUA, ani_object aniHosts) +{ + if (env == nullptr) { + WVLOG_E("env is nullptr"); + return; + } + + std::string userAgent; + if (!AniParseUtils::ParseString(env, aniUA, userAgent)) { + AniBusinessError::ThrowError(env, NWebError::PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "userAgent", "string")); + return; + } + std::vector hosts; + if (!AniParseUtils::ParseStringArray(env, aniHosts, hosts)) { + AniBusinessError::ThrowError(env, NWebError::PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "hosts", "array")); + return; + } + NWebHelper::Instance().SetUserAgentForHosts(userAgent, hosts); +} + +static void EnablePrivateNetworkAccess(ani_env* env, ani_object object, ani_boolean aniEnable) +{ + if (IS_CALLING_FROM_M114()) { + WVLOG_E("EnablePrivateNetworkAccess unsupported engine version: M114"); + return; + } + + WVLOG_D("EnablePrivateNetworkAccess start"); + bool enable = static_cast(aniEnable); + NWebHelper::Instance().EnablePrivateNetworkAccess(enable); +} + +static ani_boolean IsPrivateNetworkAccessEnabled(ani_env* env, ani_object object) +{ + if (IS_CALLING_FROM_M114()) { + WVLOG_E("IsPrivateNetworkAccessEnabled unsupported engine version: M114"); + return ANI_FALSE; + } + + WVLOG_D("IsPrivateNetworkAccessEnabled start"); + return static_case(NWebHelper::Instance().IsPrivateNetworkAccessEnabled()); +} + +static void SetErrorPageEnabled(ani_env* env, ani_object object, ani_boolean aniEnable) +{ + if (IS_CALLING_FROM_M114()) { + WVLOG_E("SetErrorPageEnabled unsupported engine version: M114"); + return; + } + + WVLOG_D("SetErrorPageEnabled start"); + auto* controller = reinterpret_cast(AniParseUtils::Unwrap(env, object)); + if (!controller || !controller->IsInit()) { + AniBusinessError::ThrowErrorByErrCode(env, INIT_ERROR); + return; + } + + bool enable = static_cast(aniEnable); + ErrCode ret = controller->SetErrorPageEnabled(enable); + if (ret != NO_ERROR) { + AniBusinessError::ThrowErrorByErrCode(env, ret); + } +} + +static ani_boolean GetErrorPageEnabled(ani_env* env, ani_object object) +{ + if (IS_CALLING_FROM_M114()) { + WVLOG_E("GetErrorPageEnabled unsupported engine version: M114"); + return ANI_FALSE; + } + + WVLOG_D("GetErrorPageEnabled start"); + auto* controller = reinterpret_cast(AniParseUtils::Unwrap(env, object)); + if (!controller || !controller->IsInit()) { + AniBusinessError::ThrowErrorByErrCode(env, INIT_ERROR); + return; + } + + return static_case(controller->GetErrorPageEnabled()); +} + ani_status StsWebviewControllerInit(ani_env *env) { WVLOG_D("[DOWNLOAD] StsWebviewControllerInit"); @@ -5368,6 +5485,13 @@ ani_status StsWebviewControllerInit(ani_env *env) ani_native_function { "hasImageCallback", nullptr, reinterpret_cast(HasImageCallback) }, ani_native_function { "hasImagePromise", nullptr, reinterpret_cast(HasImagePromise) }, ani_native_function { "getCertificateSync", nullptr, reinterpret_cast(GetCertificateSync) }, + ani_native_function { "getProgress", nullptr, reinterpret_cast(GetProgress) }, + ani_native_function { "setAppCustomUserAgent", nullptr, reinterpret_cast(SetAppCustomUserAgent) }, + ani_native_function { "setUserAgentForHosts", nullptr, reinterpret_cast(SetUserAgentForHosts) }, + ani_native_function { "enablePrivateNetworkAccess", nullptr, reinterpret_cast(EnablePrivateNetworkAccess) }, + ani_native_function { "isPrivateNetworkAccessEnabled", nullptr, reinterpret_cast(IsPrivateNetworkAccessEnabled) }, + ani_native_function { "setErrorPageEnabled", nullptr, reinterpret_cast(SetErrorPageEnabled) }, + ani_native_function { "getErrorPageEnabled", nullptr, reinterpret_cast(GetErrorPageEnabled) }, }; status = env->Class_BindNativeMethods(webviewControllerCls, controllerMethods.data(), controllerMethods.size()); -- Gitee