From 1ad3ddd28dd9756f8a7b97eb7675b38005fc2923 Mon Sep 17 00:00:00 2001 From: gaojianhao1 Date: Wed, 26 Jun 2024 11:23:02 +0800 Subject: [PATCH 01/32] Add BFCache interface. Signed-off-by: gaojianhao1 --- .../kits/cj/include/webview_controller_impl.h | 2 + .../kits/cj/src/webview_controller_impl.cpp | 10 ++ .../napi_webview_controller.cpp | 107 ++++++++++++++++++ .../napi_webview_controller.h | 14 +++ .../webviewcontroller/webview_controller.cpp | 10 ++ .../webviewcontroller/webview_controller.h | 2 + ohos_interface/include/ohos_nweb/nweb.h | 6 + .../include/ohos_nweb/nweb_engine.h | 2 + .../bridge/webcore/ark_web_engine_impl.cpp | 5 + .../bridge/webcore/ark_web_engine_impl.h | 2 + .../bridge/webcore/ark_web_nweb_impl.cpp | 5 + .../bridge/webcore/ark_web_nweb_impl.h | 6 + .../bridge/webview/ark_web_engine_wrapper.cpp | 4 + .../bridge/webview/ark_web_engine_wrapper.h | 2 + .../bridge/webview/ark_web_nweb_wrapper.cpp | 5 + .../bridge/webview/ark_web_nweb_wrapper.h | 6 + .../ohos_nweb/include/ark_web_engine.h | 3 + .../ohos_nweb/include/ark_web_nweb.h | 6 + ohos_nweb/include/nweb_helper.h | 2 + ohos_nweb/src/nweb_helper.cpp | 10 ++ 20 files changed, 209 insertions(+) diff --git a/interfaces/kits/cj/include/webview_controller_impl.h b/interfaces/kits/cj/include/webview_controller_impl.h index ddcb4a51d..db30340ee 100644 --- a/interfaces/kits/cj/include/webview_controller_impl.h +++ b/interfaces/kits/cj/include/webview_controller_impl.h @@ -153,6 +153,8 @@ namespace OHOS::Webview { void Stop(); + void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive); + public: static std::string customeSchemeCmdLine_; static bool existNweb_; diff --git a/interfaces/kits/cj/src/webview_controller_impl.cpp b/interfaces/kits/cj/src/webview_controller_impl.cpp index 03b8e08a9..34e53145d 100644 --- a/interfaces/kits/cj/src/webview_controller_impl.cpp +++ b/interfaces/kits/cj/src/webview_controller_impl.cpp @@ -609,4 +609,14 @@ namespace OHOS::Webview { } return; } + + void WebviewControllerImpl::SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) + { + auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_); + if (!nweb_ptr) { + WEBVIEWLOGE("WebviewControllerImpl::void SetBackForwardCacheOptions nweb_ptr is null"); + return; + } + nweb_ptr->SetBackForwardCacheOptions(size, timeToLive); + } } \ No newline at end of file diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp index 38f761bfb..9a92099bd 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp @@ -490,6 +490,8 @@ napi_value NapiWebviewController::Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("webPageSnapshot", NapiWebviewController::WebPageSnapshot), DECLARE_NAPI_FUNCTION("setPathAllowingUniversalAccess", NapiWebviewController::SetPathAllowingUniversalAccess), + DECLARE_NAPI_STATIC_FUNCTION("enableBackForwardCache", NapiWebviewController::EnableBackForwardCache), + DECLARE_NAPI_FUNCTION("setBackForwardCacheOptions", NapiWebviewController::SetBackForwardCacheOptions), }; napi_value constructor = nullptr; napi_define_class(env, WEBVIEW_CONTROLLER_CLASS_NAME.c_str(), WEBVIEW_CONTROLLER_CLASS_NAME.length(), @@ -5439,6 +5441,111 @@ napi_value NapiWebviewController::PrecompileJavaScript(napi_env env, napi_callba return promise; } +bool ParseBackForwardCacheSupportedFeature(napi_env env, napi_value obj, BackForwardCacheSupportedFeature& feature) +{ + std::map> featuresBooleanProperties = { + {"nativeEmbed", [](const BackForwardCacheSupportedFeature& feature) { return feature.nativeEmbed; }}, + {"mediaIntercept", [](const BackForwardCacheSupportedFeature& feature) { return feature.mediaIntercept; }}, + }; + + for (const auto& property : featuresBooleanProperties) { + napi_value propertyObj = nullptr; + napi_get_named_property(env, obj, property.first.c_str(), &propertyObj); + bool featureProperty = property.second(feature); + if (!NapiParseUtils::ParseBoolean(env, propertyObj, featureProperty)) { + return false; + } + } + + return true; +} + +napi_value NapiWebviewController::EnableBackForwardCache(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + napi_value result = nullptr; + size_t argc = INTEGER_ONE; + napi_value argv[INTEGER_ONE] = { 0 }; + napi_get_undefined(env, &result); + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + if (argc != INTEGER_ONE) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); + return result; + } + + napi_value obj = argv[0]; + BackForwardCacheSupportedFeature feature; + if (!ParseBackForwardCacheSupportedFeature(env, obj, feature)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + "BusinessError: 401. Parameter error. Wrong param of features."); + return result; + } + + WVLOG_I("The value of supported ativeEmbed is: %{public}d", feature.nativeEmbed); + WVLOG_I("The value of supported mediaIntercept is: %{public}d", feature.mediaIntercept); + + NWebHelper::Instance().EnableBackForwardCache(feature.nativeEmbed, feature.mediaIntercept); + NAPI_CALL(env, napi_get_undefined(env, &result)); + return result; +} + +bool ParseBackForwardCacheOptions(napi_env env, napi_value obj, BackForwardCacheOptions& option) +{ + std::map> optionsBooleanProperties = { + {"size", [](const BackForwardCacheOptions& option) { return option.size; }}, + {"timeToLive", [](const BackForwardCacheOptions& option) { return option.timeToLive; }}, + }; + + for (const auto& property : optionsBooleanProperties) { + napi_value propertyObj = nullptr; + napi_get_named_property(env, obj, property.first.c_str(), &propertyObj); + bool optionProperty = property.second(option); + if (!NapiParseUtils::ParseBoolean(env, propertyObj, optionProperty)) { + return false; + } + } + + return true; +} + +napi_value NapiWebviewController::SetBackForwardCacheOptions(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + napi_value result = nullptr; + size_t argc = INTEGER_ONE; + napi_value argv[INTEGER_ONE] = { 0 }; + napi_get_undefined(env, &result); + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + if (argc != INTEGER_ONE) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); + return result; + } + + WebviewController* webviewController = GetWebviewController(env, info); + if (!webviewController) { + WVLOG_E("InjectOfflineResource: init webview controller error."); + BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); + return result; + } + + napi_value obj = argv[0]; + BackForwardCacheOptions option; + if (!ParseBackForwardCacheOptions(env, obj, option)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + "BusinessError: 401. Parameter error. Wrong param of options."); + return result; + } + + WVLOG_I("The value of backforward cache option size is: %{public}d", option.size); + WVLOG_I("The value of backforward cache option timeToLive is: %{public}d", option.timeToLive); + + webviewController->SetBackForwardCacheOptions(option.size, option.timeToLive); + NAPI_CALL(env, napi_get_undefined(env, &result)); + return result; +} + napi_value NapiWebviewController::WarmupServiceWorker(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h index bab044a4e..33962ca35 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h @@ -61,6 +61,16 @@ struct OfflineResourceValue { napi_value type; }; +struct BackForwardCacheSupportedFeature { + bool nativeEmbed = true; + bool mediaIntercept = true; +}; + +struct BackForwardCacheOptions { + int32_t size = 1; + int32_t timeToLive = 600; +}; + class NapiWebviewController { public: NapiWebviewController() {} @@ -348,6 +358,10 @@ private: static napi_value SetPathAllowingUniversalAccess(napi_env env, napi_callback_info info); + static napi_value EnableBackForwardCache(napi_env env, napi_callback_info info); + + static napi_value SetBackForwardCacheOptions(napi_env env, napi_callback_info info); + static int32_t maxFdNum_; static std::atomic usedFd_; }; diff --git a/interfaces/kits/napi/webviewcontroller/webview_controller.cpp b/interfaces/kits/napi/webviewcontroller/webview_controller.cpp index 354fa4bf2..52a53a716 100644 --- a/interfaces/kits/napi/webviewcontroller/webview_controller.cpp +++ b/interfaces/kits/napi/webviewcontroller/webview_controller.cpp @@ -1942,5 +1942,15 @@ void WebviewController::SetPathAllowingUniversalAccess( } nweb_ptr->SetPathAllowingUniversalAccess(pathList, moduleName_, errorPath); } + +void WebviewController::SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) +{ + auto nweb_ptr = NWebHelper::Instance().GetNWeb(nwebId_); + if (!nweb_ptr) { + return; + } + + nweb_ptr->SetBackForwardCacheOptions(size, timeToLive); +} } // namespace NWeb } // namespace OHOS diff --git a/interfaces/kits/napi/webviewcontroller/webview_controller.h b/interfaces/kits/napi/webviewcontroller/webview_controller.h index e7623fc3a..743e89ff5 100644 --- a/interfaces/kits/napi/webviewcontroller/webview_controller.h +++ b/interfaces/kits/napi/webviewcontroller/webview_controller.h @@ -372,6 +372,8 @@ public: void SetPathAllowingUniversalAccess(const std::vector& pathList, std::string& errorPath); + void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive); + private: int ConverToWebHitTestType(int hitType); diff --git a/ohos_interface/include/ohos_nweb/nweb.h b/ohos_interface/include/ohos_nweb/nweb.h index 18bdc317e..feac8f12f 100644 --- a/ohos_interface/include/ohos_nweb/nweb.h +++ b/ohos_interface/include/ohos_nweb/nweb.h @@ -1356,6 +1356,12 @@ public: */ virtual void PerformAction(int64_t accessibilityId, uint32_t action, const std::map& actionArguments) {} + + /** + * @brief Set backforward cache options. + */ + /*--ark web()--*/ + virtual void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) { return; } }; } // namespace OHOS::NWeb diff --git a/ohos_interface/include/ohos_nweb/nweb_engine.h b/ohos_interface/include/ohos_nweb/nweb_engine.h index 41853edc0..351e52247 100644 --- a/ohos_interface/include/ohos_nweb/nweb_engine.h +++ b/ohos_interface/include/ohos_nweb/nweb_engine.h @@ -82,6 +82,8 @@ public: virtual std::shared_ptr GetAdsBlockManager() { return nullptr; }; + + virtual void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) {}; }; } // namespace OHOS::NWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp index 5b55bcb92..d94b0c6bc 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp @@ -212,4 +212,9 @@ ArkWebRefPtr ArkWebEngineImpl::GetAdsBlockManager() } return new ArkWebAdsBlockManagerImpl(nweb_adsBlock_manager); } + +void ArkWebEngineImpl::EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) +{ + nweb_engine_->EnableBackForwardCache(enableNativeEmbed, enableMediaIntercept); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h index 28236ab74..63243ef51 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h @@ -79,6 +79,8 @@ public: ArkWebRefPtr GetAdsBlockManager() override; + void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) override; + private: std::shared_ptr nweb_engine_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.cpp index cbca90c1e..86cd96a88 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.cpp @@ -1055,4 +1055,9 @@ void ArkWebNWebImpl::PerformAction(int64_t accessibility_id, uint32_t action, { nweb_nweb_->PerformAction(accessibility_id, action, ArkWebStringMapStructToClass(actionArguments)); } + +void ArkWebNWebImpl::SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) +{ + nweb_nweb_->SetBackForwardCacheOptions(size, timeToLive); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.h index a464c2d0b..625478b79 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.h @@ -1189,6 +1189,12 @@ public: /*--ark web()--*/ void PerformAction(int64_t accessibility_id, uint32_t action, const ArkWebStringMap& actionArguments) override; + + /** + * @brief set backforward cache options. + */ + /*--ark web()--*/ + void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) override; private: std::shared_ptr nweb_nweb_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp index c08317fa5..b384e5bf5 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp @@ -240,4 +240,8 @@ ArkWebEngineWrapper::GetAdsBlockManager() { return std::make_shared(ark_web_adsblock_manager); } + +void ArkWebEngineWrapper::EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) { + ark_web_engine_->EnableBackForwardCache(enableNativeEmbed, enableMediaIntercept); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h index 6400bfcdd..865d41b8e 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h @@ -76,6 +76,8 @@ public: void EnableWholeWebPageDrawing() override; std::shared_ptr GetAdsBlockManager() override; + + void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) override; private: ArkWebRefPtr ark_web_engine_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.cpp index 8c12aaf14..77dc35c8b 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.cpp @@ -1233,4 +1233,9 @@ void ArkWebNWebWrapper::PerformAction(int64_t accessibility_id, uint32_t action, ArkWebStringMapStructRelease(stArguments); } + +void ArkWebNWebWrapper::SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) +{ + ark_web_nweb_->SetBackForwardCacheOptions(size, timeToLive); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h index 23940f2d3..86da31a16 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h @@ -1199,6 +1199,12 @@ public: /*--ark web()--*/ void PerformAction(int64_t accessibilityId, uint32_t action, const std::map& actionArguments) override; + + /** + * @brief set backforward cache options. + */ + /*--ark web()--*/ + void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) override; private: ArkWebRefPtr ark_web_nweb_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h index ab6194dcf..30021b2cb 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h +++ b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h @@ -108,6 +108,9 @@ public: /*--ark web()--*/ virtual ArkWebRefPtr GetAdsBlockManager() = 0; + + /*--ark web()--*/ + virtual void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) = 0; }; } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_nweb.h b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_nweb.h index 480beb616..5fde82e3d 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_nweb.h +++ b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_nweb.h @@ -1351,6 +1351,12 @@ public: /*--ark web()--*/ virtual void PerformAction(int64_t accessibilityId, uint32_t action, const ArkWebStringMap& actionArguments) = 0; + + /** + * @brief set backforward cache options. + */ + /*--ark web()--*/ + virtual void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) = 0; }; } // namespace OHOS::ArkWeb diff --git a/ohos_nweb/include/nweb_helper.h b/ohos_nweb/include/nweb_helper.h index a6fa18a67..bc9536149 100644 --- a/ohos_nweb/include/nweb_helper.h +++ b/ohos_nweb/include/nweb_helper.h @@ -86,6 +86,8 @@ public: void EnableWholeWebPageDrawing(); std::shared_ptr GetAdsBlockManager(); + void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept); + private: NWebHelper() = default; bool LoadLib(bool from_ark); diff --git a/ohos_nweb/src/nweb_helper.cpp b/ohos_nweb/src/nweb_helper.cpp index d29a0d003..3a546cbd2 100644 --- a/ohos_nweb/src/nweb_helper.cpp +++ b/ohos_nweb/src/nweb_helper.cpp @@ -1092,6 +1092,16 @@ void NWebHelper::ClearHostIP(const std::string& hostName) nwebEngine_->ClearHostIP(hostName); } +void NWebHelper::EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) +{ + if (nwebEngine_ == nullptr) { + WVLOG_E("nweb engine is nullptr"); + return; + } + + nwebEngine_->EnableBackForwardCache(enableNativeEmbed, enableMediaIntercept); +} + void NWebHelper::EnableWholeWebPageDrawing() { if (nwebEngine_ == nullptr) { -- Gitee From e076f9b3dbf6bc33d973a7a19b1b0409bdbd9e10 Mon Sep 17 00:00:00 2001 From: gaojianhao1 Date: Sat, 29 Jun 2024 16:54:35 +0800 Subject: [PATCH 02/32] Add BFCache interface. Signed-off-by: gaojianhao1 --- .../webviewcontroller/back_forward_cache_options.h | 4 ++-- .../webviewcontroller/napi_webview_controller.cpp | 12 ++++++------ .../napi/webviewcontroller/napi_webview_controller.h | 10 ---------- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h index 6c8cf0817..16ef7d2a6 100644 --- a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h +++ b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h @@ -38,7 +38,7 @@ private: napi_env env_; int32_t size_; int32_t timeToLive_; -} +}; class BackForwardCacheSupportFeatures { public: @@ -53,7 +53,7 @@ private: napi_env env_; bool nativeEmbed_; bool mediaIntercept_; -} +}; } } diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp index 0a39d7c30..890a084e8 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp @@ -5488,10 +5488,10 @@ napi_value NapiWebviewController::EnableBackForwardCache(napi_env env, napi_call napi_unwrap(env, obj, (void**)&features); napi_create_reference(env, obj, 1, &features->delegate_); - WVLOG_I("The value of supported ativeEmbed is: %{public}d", feature.IsEnableNativeEmbed()); - WVLOG_I("The value of supported mediaIntercept is: %{public}d", feature.IsEnableMediaIntercept()); + WVLOG_I("The value of supported ativeEmbed is: %{public}d", features->IsEnableNativeEmbed()); + WVLOG_I("The value of supported mediaIntercept is: %{public}d", features->IsEnableMediaIntercept()); - NWebHelper::Instance().EnableBackForwardCache(feature.IsEnableNativeEmbed(), feature.IsEnableMediaIntercept()); + NWebHelper::Instance().EnableBackForwardCache(feature->IsEnableNativeEmbed(), feature->IsEnableMediaIntercept()); NAPI_CALL(env, napi_get_undefined(env, &result)); return result; } @@ -5522,10 +5522,10 @@ napi_value NapiWebviewController::SetBackForwardCacheOptions(napi_env env, napi_ napi_unwrap(env, obj, (void**)&options); napi_create_reference(env, obj, 1, &options->delegate_); - WVLOG_I("The value of backforward cache option size is: %{public}d", options.GetSize()); - WVLOG_I("The value of backforward cache option timeToLive is: %{public}d", options.GetTimeToLive()); + WVLOG_I("The value of backforward cache option size is: %{public}d", options->GetSize()); + WVLOG_I("The value of backforward cache option timeToLive is: %{public}d", options->GetTimeToLive()); - webviewController->SetBackForwardCacheOptions(option.size, option.timeToLive); + webviewController->SetBackForwardCacheOptions(options->GetSize(), options->GetTimeToLive()); NAPI_CALL(env, napi_get_undefined(env, &result)); return result; } diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h index 33962ca35..5877ee94e 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h @@ -61,16 +61,6 @@ struct OfflineResourceValue { napi_value type; }; -struct BackForwardCacheSupportedFeature { - bool nativeEmbed = true; - bool mediaIntercept = true; -}; - -struct BackForwardCacheOptions { - int32_t size = 1; - int32_t timeToLive = 600; -}; - class NapiWebviewController { public: NapiWebviewController() {} -- Gitee From 3ebea90ef3b5291e91e76e009f57680d34dfa063 Mon Sep 17 00:00:00 2001 From: gaojianhao1 Date: Sat, 29 Jun 2024 17:56:42 +0800 Subject: [PATCH 03/32] Add BFCache interface. Signed-off-by: gaojianhao1 --- interfaces/kits/napi/BUILD.gn | 2 + .../back_forward_cache_options.cpp | 27 +--- .../back_forward_cache_options.h | 6 +- .../napi_back_forward_cache_options.cpp | 130 ++++++++++++++++++ .../napi_back_forward_cache_options.h | 52 +++++++ .../napi_webview_controller.cpp | 2 +- 6 files changed, 193 insertions(+), 26 deletions(-) create mode 100644 interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp create mode 100644 interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index 38175046f..964b8e492 100644 --- a/interfaces/kits/napi/BUILD.gn +++ b/interfaces/kits/napi/BUILD.gn @@ -53,6 +53,8 @@ ohos_shared_library("webview_napi") { "webstorage/napi_web_storage.cpp", "webviewcontroller/back_forward_cache_options.cpp", "webviewcontroller/back_forward_cache_options.h", + "webviewcontroller/napi_back_forward_cache_options.cpp", + "webviewcontroller/napi_back_forward_cache_options.h", "webviewcontroller/napi_native_media_player.cpp", "webviewcontroller/napi_native_media_player.h", "webviewcontroller/napi_web_download_delegate.cpp", diff --git a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp index e4167ce13..f3fe444fc 100644 --- a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp +++ b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp @@ -17,22 +17,14 @@ #include -#include "back_forward_cache_options.h" #include "napi_parse_utils.h" #include "nweb_log.h" #include "business_error.h" #include "web_errors.h" namespace OHOS::NWeb { -namespace { -BackForwardCacheOptions::BackForwardCacheOptions(napi_env env) - : env_(env) -{ - WVLOG_D("Created a BackForwardCacheOptions class."); -} - -BackForwardCacheOptions::BackForwardCacheOptions(napi_env env, int32_t size, int32_t timeToLive) - : env_(env), size_(size), timeToLive_(timeToLive) +BackForwardCacheOptions::BackForwardCacheOptions(int32_t size, int32_t timeToLive) + : size_(size), timeToLive_(timeToLive) { WVLOG_D("Created a BackForwardCacheOptions class. Value size: %{public}d timeToLive: %{public}d.", size_, timeToLive_); } @@ -47,27 +39,20 @@ int32_t BackForwardCacheOptions::GetTimeToLive() return timeToLive_; } -BackForwardCacheSupportFeatures::BackForwardCacheSupportedFeature(napi_env env) - : env_(env) -{ - WVLOG_D("Created a BackForwardCacheSupportedFeature class."); -} - -BackForwardCacheSupportFeatures::BackForwardCacheSupportFeatures(napi_env env, bool nativeEmbed, bool mediaIntercept) - : env_(env), nativeEmbed_(nativeEmbed), mediaIntercept_(mediaIntercept) +BackForwardCacheSupportFeatures::BackForwardCacheSupportFeatures(bool nativeEmbed, bool mediaIntercept) + : nativeEmbed_(nativeEmbed), mediaIntercept_(mediaIntercept) { WVLOG_D("Created a BackForwardCacheSupportFeatures class. Value nativeEmbed: %{public}d mediaIntercept: %{public}d.", nativeEmbed_, mediaIntercept_); } -bool BackForwardCacheSupportedFeature::IsEnableNativeEmbed() +bool BackForwardCacheSupportFeatures::IsEnableNativeEmbed() { return nativeEmbed_; } -bool BackForwardCacheSupportedFeature::IsEnableMediaIntercept() +bool BackForwardCacheSupportFeatures::IsEnableMediaIntercept() { return mediaIntercept_; } -} } \ No newline at end of file diff --git a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h index 16ef7d2a6..ed28ed1b8 100644 --- a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h +++ b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h @@ -27,7 +27,7 @@ namespace OHOS { namespace NWeb { class BackForwardCacheOptions { public: - explicit BackForwardCacheOptions(napi_env env); + BackForwardCacheOptions(napi_env env); BackForwardCacheOptions(napi_env env, int32_t size, int32_t timeToLive); int32_t GetSize(); int32_t GetTimeToLive(); @@ -35,14 +35,13 @@ public: napi_ref delegate_ = nullptr; private: - napi_env env_; int32_t size_; int32_t timeToLive_; }; class BackForwardCacheSupportFeatures { public: - explicit BackForwardCacheSupportFeatures(napi_env env); + BackForwardCacheSupportFeatures(napi_env env); BackForwardCacheSupportFeatures(napi_env env, bool nativeEmbed, bool mediaIntercept); bool IsEnableNativeEmbed(); bool IsEnableMediaIntercept(); @@ -50,7 +49,6 @@ public: napi_ref delegate_ = nullptr; private: - napi_env env_; bool nativeEmbed_; bool mediaIntercept_; }; diff --git a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp new file mode 100644 index 000000000..e4687d617 --- /dev/null +++ b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2024 Huawei Device 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_back_forward_cache_options.h" + +#include +#include +#include +#include +#include + +#include "business_error.h" +#include "nweb_log.h" +#include "napi_parse_utils.h" +#include "web_errors.h" + +using namespace OHOS::NWebError; + +namespace OHOS { +namespace NWeb { +napi_value NapiBackForwardCacheOptions::JS_Constructor(napi_env env, napi_callback_info info) +{ + WVLOG_I("NapiBackForwardCacheOptions::JS_Constructor is called"); + napi_value thisVar = nullptr; + void *data = nullptr; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data); + + BackForwardCacheOptions *options = new BackForwardCacheOptions(env); + + napi_wrap( + env, thisVar, options, + [](napi_env /* env */, void *data, void * /* hint */) { + BackForwardCacheOptions *options = (BackForwardCacheOptions *)data; + delete options; + }, + nullptr, nullptr); + + return thisVar; +} + +napi_value NapiBackForwardCacheSupportFeatures::JS_Constructor(napi_env env, napi_callback_info info) +{ + WVLOG_I("NapiBackForwardCacheSupportFeatures::JS_Constructor is called"); + napi_value thisVar = nullptr; + void *data = nullptr; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data); + + BackForwardCacheSupportFeatures *features = new BackForwardCacheSupportFeatures(env); + + napi_wrap( + env, thisVar, features, + [](napi_env /* env */, void *data, void * /* hint */) { + BackForwardCacheSupportFeatures *features = (BackForwardCacheSupportFeatures *)data; + delete features; + }, + nullptr, nullptr); + + return thisVar; +} + +napi_value NapiBackForwardCacheOptions::JS_GetSize(napi_env env, napi_callback_info info) +{ + WVLOG_D("NapiBackForwardCacheOptions::JS_GetSize"); + return nullptr; +} + +napi_value NapiBackForwardCacheOptions::JS_GetTimeToLive(napi_env env, napi_callback_info info) +{ + WVLOG_D("NapiBackForwardCacheOptions::JS_GetTimeToLive"); + return nullptr; +} + +napi_value NapiBackForwardCacheSupportFeatures::JS_IsEnableNativeEmbed(napi_env env, napi_callback_info info) +{ + WVLOG_D("NapiBackForwardCacheSupportFeatures::JS_IsEnableNativeEmbed"); + return nullptr; +} + +napi_value NapiBackForwardCacheSupportFeatures::JS_IsEnableMediaIntercept(napi_env env, napi_callback_info info) +{ + WVLOG_D("NapiBackForwardCacheSupportFeatures::JS_IsEnableMediaIntercept"); + return nullptr; +} + +napi_value NapiBackForwardCacheOptions::Init(napi_env env, napi_value exports) +{ + WVLOG_D("NapiBackForwardCacheOptions::Init"); + napi_property_descriptor properties[] = { + DECLARE_NAPI_FUNCTION("getSize", JS_GetSize), + DECLARE_NAPI_FUNCTION("getTimeToLive", JS_GetTimeToLive), + }; + napi_value backForwardCacheOptions = nullptr; + napi_define_class(env, BACK_FORWARD_CACHE_OPTIONS.c_str(), BACK_FORWARD_CACHE_OPTIONS.length(), + JS_Constructor, nullptr, + sizeof(properties) / sizeof(properties[0]), properties, &backForwardCacheOption); + napi_set_named_property(env, exports, BACK_FORWARD_CACHE_OPTIONS.c_str(), + backForwardCacheOption); + return exports; +} + +napi_value NapiBackForwardCacheSupportFeatures::Init(napi_env env, napi_value exports) +{ + WVLOG_D("NapiBackForwardCacheSupportFeatures::Init"); + napi_property_descriptor properties[] = { + DECLARE_NAPI_FUNCTION("isEnableNativeEmbed", JS_IsEnableNativeEmbed), + DECLARE_NAPI_FUNCTION("isEnableMediaIntercept", JS_IsEnableMediaIntercept), + }; + napi_value backForwardCacheSupportFeature = nullptr; + napi_define_class(env, BACK_FORWARD_CACHE_SUPPORT_FEATURES.c_str(), BACK_FORWARD_CACHE_SUPPORT_FEATURES.length(), + JS_Constructor, nullptr, + sizeof(properties) / sizeof(properties[0]), properties, &backForwardCacheSupportFeature); + napi_set_named_property(env, exports, BACK_FORWARD_CACHE_SUPPORT_FEATURES.c_str(), + backForwardCacheSupportFeature); + return exports; +} + +} +} \ No newline at end of file diff --git a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h new file mode 100644 index 000000000..1d0c8f713 --- /dev/null +++ b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2024 Huawei Device 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. + */ + +#ifndef NWEB_NAPI_BACK_FORWARD_CACHE_OPTIONS_H +#define NWEB_NAPI_BACK_FORWARD_CACHE_OPTIONS_H + +#include "napi/native_api.h" +#include "napi/native_common.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace NWeb { + +const std::string BACK_FORWARD_CACHE_OPTIONS = "BackForwardCacheOptions"; +const std::string BACK_FORWARD_CACHE_SUPPORT_FEATURES = "BackForwardCacheSupportFeatures"; + +class NapiBackForwardCacheOptions { +public: + BackForwardCacheOptions() = default; + ~BackForwardCacheOptions() = default; + + static napi_value Init(napi_env env, napi_value exports); + static napi_value JS_Constructor(napi_env env, napi_callback_info info); + static napi_value JS_GetSize(napi_env env, napi_callback_info info); + static napi_value JS_GetTimeToLive(napi_env env, napi_callback_info info); +}; + +class NapiBackForwardCacheSupportFeatures { +public: + BackForwardCacheSupportFeatures() = default; + ~BackForwardCacheSupportFeatures() = default; + + static napi_value Init(napi_env env, napi_value exports); + static napi_value JS_Constructor(napi_env env, napi_callback_info info); + static napi_value JS_IsEnableNativeEmbed(napi_env env, napi_callback_info info); + static napi_value JS_IsEnableMediaIntercept(napi_env env, napi_callback_info info); +}; + +} +} \ No newline at end of file diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp index 890a084e8..8e8a4269d 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp @@ -5491,7 +5491,7 @@ napi_value NapiWebviewController::EnableBackForwardCache(napi_env env, napi_call WVLOG_I("The value of supported ativeEmbed is: %{public}d", features->IsEnableNativeEmbed()); WVLOG_I("The value of supported mediaIntercept is: %{public}d", features->IsEnableMediaIntercept()); - NWebHelper::Instance().EnableBackForwardCache(feature->IsEnableNativeEmbed(), feature->IsEnableMediaIntercept()); + NWebHelper::Instance().EnableBackForwardCache(features->IsEnableNativeEmbed(), features->IsEnableMediaIntercept()); NAPI_CALL(env, napi_get_undefined(env, &result)); return result; } -- Gitee From 011f6a0152f43d111c36b66c72fa8d1bdd44c932 Mon Sep 17 00:00:00 2001 From: gaojianhao1 Date: Mon, 1 Jul 2024 14:06:39 +0800 Subject: [PATCH 04/32] Add class constructor. Signed-off-by: gaojianhao1 --- .../common/napi_webview_native_module.cpp | 3 + .../back_forward_cache_options.cpp | 14 +++- .../back_forward_cache_options.h | 16 ++-- .../napi_back_forward_cache_options.cpp | 80 ++++++++++++++++--- .../napi_back_forward_cache_options.h | 12 +-- .../napi_webview_controller.cpp | 16 ++-- 6 files changed, 109 insertions(+), 32 deletions(-) diff --git a/interfaces/kits/napi/common/napi_webview_native_module.cpp b/interfaces/kits/napi/common/napi_webview_native_module.cpp index d09ed21fa..00ad3d24a 100644 --- a/interfaces/kits/napi/common/napi_webview_native_module.cpp +++ b/interfaces/kits/napi/common/napi_webview_native_module.cpp @@ -30,6 +30,7 @@ #include "napi_web_download_item.h" #include "napi_web_download_delegate.h" #include "napi_web_scheme_handler_request.h" +#include "napi_back_forward_cache_options.h" namespace OHOS { namespace NWeb { @@ -51,6 +52,8 @@ static napi_value WebViewExport(napi_env env, napi_value exports) NapiWebAdsBlockManager::Init(env, exports); WebFunctionInit(env, exports); NapiNativeMediaPlayerHandler::Init(env, exports); + NapiBackForwardCacheOptions::Init(env, exports); + NapiBackForwardCacheSupportFeatures::Init(env, exports); return exports; } EXTERN_C_END diff --git a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp index f3fe444fc..caac1d118 100644 --- a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp +++ b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp @@ -23,8 +23,13 @@ #include "web_errors.h" namespace OHOS::NWeb { +BackForwardCacheOptions::BackForwardCacheOptions() +{ + WVLOG_D("Created a BackForwardCacheOptions class."); +} + BackForwardCacheOptions::BackForwardCacheOptions(int32_t size, int32_t timeToLive) - : size_(size), timeToLive_(timeToLive) + :size_(size), timeToLive_(timeToLive) { WVLOG_D("Created a BackForwardCacheOptions class. Value size: %{public}d timeToLive: %{public}d.", size_, timeToLive_); } @@ -39,8 +44,13 @@ int32_t BackForwardCacheOptions::GetTimeToLive() return timeToLive_; } +BackForwardCacheSupportFeatures::BackForwardCacheSupportFeatures() +{ + WVLOG_D("Created a BackForwardCacheSupportFeatures class."); +} + BackForwardCacheSupportFeatures::BackForwardCacheSupportFeatures(bool nativeEmbed, bool mediaIntercept) - : nativeEmbed_(nativeEmbed), mediaIntercept_(mediaIntercept) + :nativeEmbed_(nativeEmbed), mediaIntercept_(mediaIntercept) { WVLOG_D("Created a BackForwardCacheSupportFeatures class. Value nativeEmbed: %{public}d mediaIntercept: %{public}d.", nativeEmbed_, mediaIntercept_); } diff --git a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h index ed28ed1b8..e58e675b9 100644 --- a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h +++ b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h @@ -27,30 +27,30 @@ namespace OHOS { namespace NWeb { class BackForwardCacheOptions { public: - BackForwardCacheOptions(napi_env env); - BackForwardCacheOptions(napi_env env, int32_t size, int32_t timeToLive); + BackForwardCacheOptions(); + BackForwardCacheOptions(int32_t size, int32_t timeToLive); int32_t GetSize(); int32_t GetTimeToLive(); napi_ref delegate_ = nullptr; private: - int32_t size_; - int32_t timeToLive_; + int32_t size_ = 1; + int32_t timeToLive_ = 600; }; class BackForwardCacheSupportFeatures { public: - BackForwardCacheSupportFeatures(napi_env env); - BackForwardCacheSupportFeatures(napi_env env, bool nativeEmbed, bool mediaIntercept); + BackForwardCacheSupportFeatures(); + BackForwardCacheSupportFeatures(bool nativeEmbed, bool mediaIntercept); bool IsEnableNativeEmbed(); bool IsEnableMediaIntercept(); napi_ref delegate_ = nullptr; private: - bool nativeEmbed_; - bool mediaIntercept_; + bool nativeEmbed_ = true; + bool mediaIntercept_ = true; }; } diff --git a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp index e4687d617..875b1290b 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp @@ -21,9 +21,11 @@ #include #include +#include "back_forward_cache_options.h" #include "business_error.h" #include "nweb_log.h" #include "napi_parse_utils.h" +#include "napi/native_node_api.h" #include "web_errors.h" using namespace OHOS::NWebError; @@ -35,15 +37,43 @@ napi_value NapiBackForwardCacheOptions::JS_Constructor(napi_env env, napi_callba WVLOG_I("NapiBackForwardCacheOptions::JS_Constructor is called"); napi_value thisVar = nullptr; void *data = nullptr; - napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data); - - BackForwardCacheOptions *options = new BackForwardCacheOptions(env); + size_t argc = 2; + napi_value argv[2] = {0}; + napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + + BackForwardCacheOptions *options = nullptr; + if (argc == 0) + options = new BackForwardCacheOptions(); + else if (argc == 2) { + int32_t size = 0; + if (!NapiParseUtils::ParseInt32(env, argv[0], size) || (size <= 0 || size > 50)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + "BusinessError: 401. Parameter error. The type of param 'size' must be integer and value between 1 and 50."); + return thisVar; + } + + int32_t timeToLive = 0; + if (!NapiParseUtils::ParseInt32(env, argv[1], timeToLive)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + "BusinessError: 401. Parameter error. The type of param 'timeToLive' must be integer."); + return thisVar; + } + + options = new BackForwardCacheOptions(size, timeToLive); + napi_set_named_property(env, thisVar, "size_", argv[0]); + napi_set_named_property(env, thisVar, "timeToLive_", argv[1]); + } else { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "none", "two")); + return thisVar; + } napi_wrap( env, thisVar, options, [](napi_env /* env */, void *data, void * /* hint */) { BackForwardCacheOptions *options = (BackForwardCacheOptions *)data; delete options; + options = nullptr; }, nullptr, nullptr); @@ -55,15 +85,43 @@ napi_value NapiBackForwardCacheSupportFeatures::JS_Constructor(napi_env env, nap WVLOG_I("NapiBackForwardCacheSupportFeatures::JS_Constructor is called"); napi_value thisVar = nullptr; void *data = nullptr; - napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data); - - BackForwardCacheSupportFeatures *features = new BackForwardCacheSupportFeatures(env); + size_t argc = 2; + napi_value argv[2] = {0}; + napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + + BackForwardCacheSupportFeatures *features = nullptr; + if (argc == 0) + features = new BackForwardCacheSupportFeatures(); + else if (argc == 2) { + bool nativeEmbed = true; + if (!NapiParseUtils::ParseBoolean(env, argv[0], nativeEmbed)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "nativeEmbed", "boolean")); + return thisVar; + } + + bool mediaIntercept = true; + if (!NapiParseUtils::ParseBoolean(env, argv[1], mediaIntercept)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "mediaIntercept", "boolean")); + return thisVar; + } + + features = new BackForwardCacheSupportFeatures(nativeEmbed, mediaIntercept); + napi_set_named_property(env, thisVar, "nativeEmbed_", argv[0]); + napi_set_named_property(env, thisVar, "mediaIntercept_", argv[1]); + } else { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "none", "two")); + return thisVar; + } napi_wrap( env, thisVar, features, [](napi_env /* env */, void *data, void * /* hint */) { BackForwardCacheSupportFeatures *features = (BackForwardCacheSupportFeatures *)data; delete features; + features = nullptr; }, nullptr, nullptr); @@ -104,9 +162,9 @@ napi_value NapiBackForwardCacheOptions::Init(napi_env env, napi_value exports) napi_value backForwardCacheOptions = nullptr; napi_define_class(env, BACK_FORWARD_CACHE_OPTIONS.c_str(), BACK_FORWARD_CACHE_OPTIONS.length(), JS_Constructor, nullptr, - sizeof(properties) / sizeof(properties[0]), properties, &backForwardCacheOption); + sizeof(properties) / sizeof(properties[0]), properties, &backForwardCacheOptions); napi_set_named_property(env, exports, BACK_FORWARD_CACHE_OPTIONS.c_str(), - backForwardCacheOption); + backForwardCacheOptions); return exports; } @@ -117,12 +175,12 @@ napi_value NapiBackForwardCacheSupportFeatures::Init(napi_env env, napi_value ex DECLARE_NAPI_FUNCTION("isEnableNativeEmbed", JS_IsEnableNativeEmbed), DECLARE_NAPI_FUNCTION("isEnableMediaIntercept", JS_IsEnableMediaIntercept), }; - napi_value backForwardCacheSupportFeature = nullptr; + napi_value backForwardCacheSupportFeatures = nullptr; napi_define_class(env, BACK_FORWARD_CACHE_SUPPORT_FEATURES.c_str(), BACK_FORWARD_CACHE_SUPPORT_FEATURES.length(), JS_Constructor, nullptr, - sizeof(properties) / sizeof(properties[0]), properties, &backForwardCacheSupportFeature); + sizeof(properties) / sizeof(properties[0]), properties, &backForwardCacheSupportFeatures); napi_set_named_property(env, exports, BACK_FORWARD_CACHE_SUPPORT_FEATURES.c_str(), - backForwardCacheSupportFeature); + backForwardCacheSupportFeatures); return exports; } diff --git a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h index 1d0c8f713..41e3d497b 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h +++ b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h @@ -28,8 +28,8 @@ const std::string BACK_FORWARD_CACHE_SUPPORT_FEATURES = "BackForwardCacheSupport class NapiBackForwardCacheOptions { public: - BackForwardCacheOptions() = default; - ~BackForwardCacheOptions() = default; + NapiBackForwardCacheOptions() = default; + ~NapiBackForwardCacheOptions() = default; static napi_value Init(napi_env env, napi_value exports); static napi_value JS_Constructor(napi_env env, napi_callback_info info); @@ -39,8 +39,8 @@ public: class NapiBackForwardCacheSupportFeatures { public: - BackForwardCacheSupportFeatures() = default; - ~BackForwardCacheSupportFeatures() = default; + NapiBackForwardCacheSupportFeatures() = default; + ~NapiBackForwardCacheSupportFeatures() = default; static napi_value Init(napi_env env, napi_value exports); static napi_value JS_Constructor(napi_env env, napi_callback_info info); @@ -49,4 +49,6 @@ public: }; } -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp index 8e8a4269d..07b4907af 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp @@ -5484,9 +5484,11 @@ napi_value NapiWebviewController::EnableBackForwardCache(napi_env env, napi_call } BackForwardCacheSupportFeatures* features = nullptr; - napi_value obj = argv[0]; - napi_unwrap(env, obj, (void**)&features); - napi_create_reference(env, obj, 1, &features->delegate_); + napi_status status = napi_unwrap(env, argv[INTEGER_ZERO], (void**)&features) + if (status != napi_ok || features == nullptr) { + WVLOG_E("BACKFORWARDCACHE:: unwrap features failed."); + return result; + } WVLOG_I("The value of supported ativeEmbed is: %{public}d", features->IsEnableNativeEmbed()); WVLOG_I("The value of supported mediaIntercept is: %{public}d", features->IsEnableMediaIntercept()); @@ -5518,9 +5520,11 @@ napi_value NapiWebviewController::SetBackForwardCacheOptions(napi_env env, napi_ } BackForwardCacheOptions* options = nullptr; - napi_value obj = argv[0]; - napi_unwrap(env, obj, (void**)&options); - napi_create_reference(env, obj, 1, &options->delegate_); + napi_status status = napi_unwrap(env, argv[INTEGER_ZERO], (void**)&options) + if (status != napi_ok || options == nullptr) { + WVLOG_E("BACKFORWARDCACHE:: unwrap options failed."); + return result; + } WVLOG_I("The value of backforward cache option size is: %{public}d", options->GetSize()); WVLOG_I("The value of backforward cache option timeToLive is: %{public}d", options->GetTimeToLive()); -- Gitee From a9639f2eb1eed5b8e55721aab19dc7fb84303648 Mon Sep 17 00:00:00 2001 From: gaojianhao1 Date: Wed, 3 Jul 2024 13:32:11 +0800 Subject: [PATCH 05/32] Add interface Signed-off-by: gaojianhao1 --- .../napi_webview_controller.cpp | 72 +++++++++---------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp index 0f6d9ec6c..65fb6b5fc 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp @@ -5503,30 +5503,26 @@ napi_value NapiWebviewController::EnableBackForwardCache(napi_env env, napi_call bool mediaTakeOver = false; napi_value embedObj = nullptr; napi_value mediaObj = nullptr; - if (napi_has_named_property(env, argv[INTEGER_ZERO], "nativeEmbed", &embedObj) == napi_ok) { - if (napi_get_named_property(env, argv[INTEGER_ZERO], "nativeEmbed", &embedObj) != napi_ok) { - WVLOG_E("Failed to get BackForwardCacheOptions nativeEmbed value."); - return result; - } + if (napi_get_named_property(env, argv[INTEGER_ZERO], "nativeEmbed", &embedObj) != napi_ok) { + WVLOG_E("Failed to get BackForwardCacheOptions nativeEmbed value."); + return result; + } - if (!NapiParseUtils::ParseBoolean(env, mediaObj, nativeEmbed)) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR, "nativeEmbed", "bool")); - return result; - } + if (!NapiParseUtils::ParseBoolean(env, embedObj, nativeEmbed)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR, "nativeEmbed", "bool")); + return result; } - if (napi_has_named_property(env, argv[INTEGER_ZERO], "mediaTakeOver", &mediaObj) == napi_ok) { - if (napi_get_named_property(env, argv[INTEGER_ZERO], "mediaTakeOver", &mediaObj) != napi_ok) { - WVLOG_E("Failed to get BackForwardCacheOptions mediaTakeOver value."); - return result; - } + if (napi_get_named_property(env, argv[INTEGER_ZERO], "mediaTakeOver", &mediaObj) != napi_ok) { + WVLOG_E("Failed to get BackForwardCacheOptions mediaTakeOver value."); + return result; + } - if (!NapiParseUtils::ParseBoolean(env, mediaObj, mediaTakeOver)) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR, "mediaTakeOver", "bool")); - return result; - } + if (!NapiParseUtils::ParseBoolean(env, mediaObj, mediaTakeOver)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR, "mediaTakeOver", "bool")); + return result; } WVLOG_I("The value of supported ativeEmbed is: %{public}d", nativeEmbed); @@ -5555,30 +5551,26 @@ napi_value NapiWebviewController::SetBackForwardCacheOptions(napi_env env, napi_ int32_t timeToLive = 600; napi_value sizeObj = nullptr; napi_value timeToLiveObj = nullptr; - if (napi_has_named_property(env, argv[INTEGER_ZERO], "size", &sizeObj) == napi_ok) { - if (napi_get_named_property(env, argv[INTEGER_ZERO], "size", &sizeObj) != napi_ok) { - WVLOG_E("Failed to get BackForwardCacheOptions size value."); - return result; - } + if (napi_get_named_property(env, argv[INTEGER_ZERO], "size", &sizeObj) != napi_ok) { + WVLOG_E("Failed to get BackForwardCacheOptions size value."); + return result; + } - if (!NapiParseUtils::ParseInt32(env, sizeObj, size)) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR, "size", "int")); - return result; - } + if (!NapiParseUtils::ParseInt32(env, sizeObj, size)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR, "size", "int")); + return result; } - if (napi_has_named_property(env, argv[INTEGER_ZERO], "timeToLive", &timeToLiveObj) == napi_ok) { - if (napi_get_named_property(env, argv[INTEGER_ZERO], "timeToLive", &timeToLiveObj) != napi_ok) { - WVLOG_E("Failed to get BackForwardCacheOptions timeToLive value."); - return result; - } + if (napi_get_named_property(env, argv[INTEGER_ZERO], "timeToLive", &timeToLiveObj) != napi_ok) { + WVLOG_E("Failed to get BackForwardCacheOptions timeToLive value."); + return result; + } - if (!NapiParseUtils::ParseInt32(env, timeToLiveObj, timeToLive)) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR, "timeToLive", "int")); - return result; - } + if (!NapiParseUtils::ParseInt32(env, timeToLiveObj, timeToLive)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR, "timeToLive", "int")); + return result; } WebviewController* webviewController = GetWebviewController(env, info); -- Gitee From 92fc5e7e26dd8d197a68a39b38ef56e4bbf455d3 Mon Sep 17 00:00:00 2001 From: xiongjun_gitee Date: Mon, 1 Jul 2024 10:59:09 +0800 Subject: [PATCH 06/32] add ResizeVisibleViewport Signed-off-by: xiongjun_gitee --- ohos_interface/include/ohos_nweb/nweb.h | 9 +++++++++ .../ohos_nweb/bridge/webcore/ark_web_nweb_impl.cpp | 6 +++++- .../ohos_nweb/bridge/webcore/ark_web_nweb_impl.h | 11 +++++++++++ .../ohos_nweb/bridge/webview/ark_web_nweb_wrapper.cpp | 5 +++++ .../ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h | 11 +++++++++++ .../ohos_glue/ohos_nweb/include/ark_web_nweb.h | 10 ++++++++++ 6 files changed, 51 insertions(+), 1 deletion(-) diff --git a/ohos_interface/include/ohos_nweb/nweb.h b/ohos_interface/include/ohos_nweb/nweb.h index 14a9fe83f..a3019b9d5 100644 --- a/ohos_interface/include/ohos_nweb/nweb.h +++ b/ohos_interface/include/ohos_nweb/nweb.h @@ -1430,6 +1430,15 @@ public: */ virtual void RegisterArkJSfunction(const std::string& object_name, const std::vector& method_list, const std::vector& async_method_list, const int32_t object_id, const std::string& permission) {} + + /** + * @brief resize visual viewport. + * + * @param width width. + * @param height height. + * @param iskeyboard from keybord. + */ + virtual void ResizeVisibleViewport(uint32_t width, uint32_t height, bool isKeyboard) {} }; } // namespace OHOS::NWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.cpp index 6276b5fd9..1ccff32bd 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.cpp @@ -1095,7 +1095,6 @@ void ArkWebNWebImpl::WebSendTouchpadFlingEvent(double x, ArkWebBasicVectorStructToClass(pressedCodes)); } - void ArkWebNWebImpl::SendAccessibilityHoverEvent(int32_t x, int32_t y) { nweb_nweb_->SendAccessibilityHoverEvent(x, y); @@ -1108,4 +1107,9 @@ void ArkWebNWebImpl::RegisterArkJSfunction(const ArkWebString& object_name, cons ArkWebStringVectorStructToClass(method_list), ArkWebStringVectorStructToClass(async_method_list), object_id, ArkWebStringStructToClass(permission)); } + +void ArkWebNWebImpl::ResizeVisibleViewport(uint32_t width, uint32_t height, bool isKeyboard) +{ + nweb_nweb_->ResizeVisibleViewport(width, height, isKeyboard); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.h index db22e8e3f..fff3e1f1b 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.h @@ -1237,6 +1237,7 @@ public: double vx, double vy, const ArkWebInt32Vector& pressedCodes) override; + /** * @brief Set url trust list with error message. * @@ -1266,6 +1267,16 @@ public: */ void RegisterArkJSfunction(const ArkWebString& object_name, const ArkWebStringVector& method_list, const ArkWebStringVector& async_method_list, const int32_t object_id, const ArkWebString& permission) override; + + /** + * @brief resize visual viewport. + * + * @param width width. + * @param height height. + * @param iskeyboard from keybord. + */ + /*--ark web()--*/ + void ResizeVisibleViewport(uint32_t width, uint32_t height, bool isKeyboard) override; private: std::shared_ptr nweb_nweb_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.cpp index db4de86e5..79fe1cc44 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.cpp @@ -1304,4 +1304,9 @@ void ArkWebNWebWrapper::RegisterArkJSfunction(const std::string& object_name, ArkWebStringVectorStructRelease(stAsyncMethods); ArkWebStringStructRelease(stPermission); } + +void ArkWebNWebWrapper::ResizeVisibleViewport(uint32_t width, uint32_t height, bool isKeyboard) +{ + ark_web_nweb_->ResizeVisibleViewport(width, height, isKeyboard); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h index 454294bb0..c2adcd0e7 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h @@ -1245,6 +1245,7 @@ public: double vx, double vy, const std::vector& pressedCodes) override; + /** * @brief Set url trust list with error message. */ @@ -1275,6 +1276,16 @@ public: const std::vector& async_method_list, const int32_t object_id, const std::string& permission) override; + + /** + * @brief resize visual viewport. + * + * @param width width. + * @param height height. + * @param iskeyboard from keybord. + */ + /*--ark web()--*/ + void ResizeVisibleViewport(uint32_t width, uint32_t height, bool isKeyboard) override; private: ArkWebRefPtr ark_web_nweb_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_nweb.h b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_nweb.h index 0bba50118..55dd314b5 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_nweb.h +++ b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_nweb.h @@ -1426,6 +1426,16 @@ public: /*--ark web()--*/ virtual void RegisterArkJSfunction(const ArkWebString& object_name, const ArkWebStringVector& method_list, const ArkWebStringVector& async_method_list, const int32_t object_id, const ArkWebString& permission) = 0; + + /** + * @brief resize visual viewport. + * + * @param width width. + * @param height height. + * @param iskeyboard from keybord. + */ + /*--ark web()--*/ + virtual void ResizeVisibleViewport(uint32_t width, uint32_t height, bool isKeyboard) = 0; }; } // namespace OHOS::ArkWeb -- Gitee From 68a3e02b86951a367b5eab5732690721969e3a3a Mon Sep 17 00:00:00 2001 From: zhushengle Date: Wed, 3 Jul 2024 19:25:26 +0800 Subject: [PATCH 07/32] =?UTF-8?q?feat:=20WebCore=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=85=AC=E7=89=88sysroot=EF=BC=8C=E4=BD=BF=E7=94=A8=E8=83=B6?= =?UTF-8?q?=E6=B0=B4=E5=B1=82=E9=9A=94=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhushengle Change-Id: I933070816ce9f6bcbced405f04cf985644d932f5 --- ohos_interface/ohos_glue/BUILD_webcore.gn | 5 ++++- .../ohos_glue/base/bridge/ark_web_bridge_helper.cpp | 2 ++ ohos_interface/ohos_glue/base/bridge/ark_web_bridge_helper.h | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ohos_interface/ohos_glue/BUILD_webcore.gn b/ohos_interface/ohos_glue/BUILD_webcore.gn index 9e56406b8..34cf21e43 100644 --- a/ohos_interface/ohos_glue/BUILD_webcore.gn +++ b/ohos_interface/ohos_glue/BUILD_webcore.gn @@ -17,7 +17,10 @@ import("//build/config/ohos/config.gni") ################################################# config("ohos_glue_config") { - defines = [ "OHOS_NWEB" ] + defines = [ + "OHOS_NWEB", + "OHOS_WEBCORE_GLUE", + ] include_dirs = [ "//ohos_glue" ] diff --git a/ohos_interface/ohos_glue/base/bridge/ark_web_bridge_helper.cpp b/ohos_interface/ohos_glue/base/bridge/ark_web_bridge_helper.cpp index df9dbaaf7..9b835fbb2 100644 --- a/ohos_interface/ohos_glue/base/bridge/ark_web_bridge_helper.cpp +++ b/ohos_interface/ohos_glue/base/bridge/ark_web_bridge_helper.cpp @@ -43,6 +43,7 @@ bool ArkWebBridgeHelper::LoadLibFile(int mode, const std::string& libFilePath, b return true; } +#if !defined(OHOS_WEBCORE_GLUE) bool ArkWebBridgeHelper::LoadLibFile(int mode, const std::string& libNsName, const std::string& libDirPath, const std::string& libFileName, bool isPrintLog) { @@ -69,6 +70,7 @@ bool ArkWebBridgeHelper::LoadLibFile(int mode, const std::string& libNsName, con libFileHandler_ = libFileHandler; return true; } +#endif void ArkWebBridgeHelper::UnloadLibFile() { diff --git a/ohos_interface/ohos_glue/base/bridge/ark_web_bridge_helper.h b/ohos_interface/ohos_glue/base/bridge/ark_web_bridge_helper.h index 71dbd3abe..75f0818a0 100644 --- a/ohos_interface/ohos_glue/base/bridge/ark_web_bridge_helper.h +++ b/ohos_interface/ohos_glue/base/bridge/ark_web_bridge_helper.h @@ -33,8 +33,10 @@ protected: bool LoadLibFile(int mode, const std::string& libFilePath, bool isPrintLog = true); +#if !defined(OHOS_WEBCORE_GLUE) bool LoadLibFile(int mode, const std::string& libNsName, const std::string& libDirPath, const std::string& libFileName, bool isPrintLog = true); +#endif private: void UnloadLibFile(); -- Gitee From bac9c8178ca72c80c7b635ae7401fdb955687eda Mon Sep 17 00:00:00 2001 From: gaojianhao1 Date: Wed, 3 Jul 2024 20:59:46 +0800 Subject: [PATCH 08/32] Add interface of bfcache. Signed-off-by: gaojianhao1 --- .../back_forward_cache_options.cpp | 12 ---- .../back_forward_cache_options.h | 6 -- .../napi_back_forward_cache_options.cpp | 56 +------------------ .../napi_webview_controller.cpp | 14 ++--- .../webviewcontroller/webview_controller.h | 1 + ohos_interface/include/ohos_nweb/nweb.h | 10 ++-- .../include/ohos_nweb/nweb_engine.h | 2 - .../bridge/webcore/ark_web_engine_impl.cpp | 5 -- .../bridge/webcore/ark_web_nweb_impl.h | 12 ++-- .../bridge/webview/ark_web_engine_wrapper.cpp | 4 -- .../bridge/webview/ark_web_engine_wrapper.h | 2 - .../bridge/webview/ark_web_nweb_wrapper.h | 12 ++-- .../ohos_nweb/include/ark_web_engine.h | 3 - .../ohos_nweb/include/ark_web_nweb.h | 12 ++-- ohos_nweb/include/nweb_helper.h | 2 - ohos_nweb/src/nweb_helper.cpp | 5 +- .../nweb_helper_test/nweb_helper_test.cpp | 3 + 17 files changed, 35 insertions(+), 126 deletions(-) diff --git a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp index caac1d118..d8b61a597 100644 --- a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp +++ b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp @@ -28,12 +28,6 @@ BackForwardCacheOptions::BackForwardCacheOptions() WVLOG_D("Created a BackForwardCacheOptions class."); } -BackForwardCacheOptions::BackForwardCacheOptions(int32_t size, int32_t timeToLive) - :size_(size), timeToLive_(timeToLive) -{ - WVLOG_D("Created a BackForwardCacheOptions class. Value size: %{public}d timeToLive: %{public}d.", size_, timeToLive_); -} - int32_t BackForwardCacheOptions::GetSize() { return size_; @@ -49,12 +43,6 @@ BackForwardCacheSupportFeatures::BackForwardCacheSupportFeatures() WVLOG_D("Created a BackForwardCacheSupportFeatures class."); } -BackForwardCacheSupportFeatures::BackForwardCacheSupportFeatures(bool nativeEmbed, bool mediaIntercept) - :nativeEmbed_(nativeEmbed), mediaIntercept_(mediaIntercept) -{ - WVLOG_D("Created a BackForwardCacheSupportFeatures class. Value nativeEmbed: %{public}d mediaIntercept: %{public}d.", nativeEmbed_, mediaIntercept_); -} - bool BackForwardCacheSupportFeatures::IsEnableNativeEmbed() { return nativeEmbed_; diff --git a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h index e58e675b9..e9eca1d8f 100644 --- a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h +++ b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h @@ -28,12 +28,9 @@ namespace NWeb { class BackForwardCacheOptions { public: BackForwardCacheOptions(); - BackForwardCacheOptions(int32_t size, int32_t timeToLive); int32_t GetSize(); int32_t GetTimeToLive(); - napi_ref delegate_ = nullptr; - private: int32_t size_ = 1; int32_t timeToLive_ = 600; @@ -42,12 +39,9 @@ private: class BackForwardCacheSupportFeatures { public: BackForwardCacheSupportFeatures(); - BackForwardCacheSupportFeatures(bool nativeEmbed, bool mediaIntercept); bool IsEnableNativeEmbed(); bool IsEnableMediaIntercept(); - napi_ref delegate_ = nullptr; - private: bool nativeEmbed_ = true; bool mediaIntercept_ = true; diff --git a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp index 875b1290b..2751af4c9 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp @@ -41,32 +41,7 @@ napi_value NapiBackForwardCacheOptions::JS_Constructor(napi_env env, napi_callba napi_value argv[2] = {0}; napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - BackForwardCacheOptions *options = nullptr; - if (argc == 0) - options = new BackForwardCacheOptions(); - else if (argc == 2) { - int32_t size = 0; - if (!NapiParseUtils::ParseInt32(env, argv[0], size) || (size <= 0 || size > 50)) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - "BusinessError: 401. Parameter error. The type of param 'size' must be integer and value between 1 and 50."); - return thisVar; - } - - int32_t timeToLive = 0; - if (!NapiParseUtils::ParseInt32(env, argv[1], timeToLive)) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - "BusinessError: 401. Parameter error. The type of param 'timeToLive' must be integer."); - return thisVar; - } - - options = new BackForwardCacheOptions(size, timeToLive); - napi_set_named_property(env, thisVar, "size_", argv[0]); - napi_set_named_property(env, thisVar, "timeToLive_", argv[1]); - } else { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "none", "two")); - return thisVar; - } + BackForwardCacheOptions *options = new BackForwardCacheOptions(); napi_wrap( env, thisVar, options, @@ -89,32 +64,7 @@ napi_value NapiBackForwardCacheSupportFeatures::JS_Constructor(napi_env env, nap napi_value argv[2] = {0}; napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - BackForwardCacheSupportFeatures *features = nullptr; - if (argc == 0) - features = new BackForwardCacheSupportFeatures(); - else if (argc == 2) { - bool nativeEmbed = true; - if (!NapiParseUtils::ParseBoolean(env, argv[0], nativeEmbed)) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "nativeEmbed", "boolean")); - return thisVar; - } - - bool mediaIntercept = true; - if (!NapiParseUtils::ParseBoolean(env, argv[1], mediaIntercept)) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "mediaIntercept", "boolean")); - return thisVar; - } - - features = new BackForwardCacheSupportFeatures(nativeEmbed, mediaIntercept); - napi_set_named_property(env, thisVar, "nativeEmbed_", argv[0]); - napi_set_named_property(env, thisVar, "mediaIntercept_", argv[1]); - } else { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "none", "two")); - return thisVar; - } + BackForwardCacheSupportFeatures *features = new BackForwardCacheSupportFeatures(); napi_wrap( env, thisVar, features, @@ -173,7 +123,7 @@ napi_value NapiBackForwardCacheSupportFeatures::Init(napi_env env, napi_value ex WVLOG_D("NapiBackForwardCacheSupportFeatures::Init"); napi_property_descriptor properties[] = { DECLARE_NAPI_FUNCTION("isEnableNativeEmbed", JS_IsEnableNativeEmbed), - DECLARE_NAPI_FUNCTION("isEnableMediaIntercept", JS_IsEnableMediaIntercept), + DECLARE_NAPI_FUNCTION("isEnableMediaTakeOver", JS_IsEnableMediaTakeOver), }; napi_value backForwardCacheSupportFeatures = nullptr; napi_define_class(env, BACK_FORWARD_CACHE_SUPPORT_FEATURES.c_str(), BACK_FORWARD_CACHE_SUPPORT_FEATURES.length(), diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp index 65fb6b5fc..22d271159 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp @@ -5510,7 +5510,7 @@ napi_value NapiWebviewController::EnableBackForwardCache(napi_env env, napi_call if (!NapiParseUtils::ParseBoolean(env, embedObj, nativeEmbed)) { BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR, "nativeEmbed", "bool")); + NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "nativeEmbed", "bool")); return result; } @@ -5521,13 +5521,10 @@ napi_value NapiWebviewController::EnableBackForwardCache(napi_env env, napi_call if (!NapiParseUtils::ParseBoolean(env, mediaObj, mediaTakeOver)) { BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR, "mediaTakeOver", "bool")); + NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "mediaTakeOver", "bool")); return result; } - WVLOG_I("The value of supported ativeEmbed is: %{public}d", nativeEmbed); - WVLOG_I("The value of supported mediaIntercept is: %{public}d", mediaTakeOver); - NWebHelper::Instance().EnableBackForwardCache(nativeEmbed, mediaTakeOver); NAPI_CALL(env, napi_get_undefined(env, &result)); return result; @@ -5558,7 +5555,7 @@ napi_value NapiWebviewController::SetBackForwardCacheOptions(napi_env env, napi_ if (!NapiParseUtils::ParseInt32(env, sizeObj, size)) { BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR, "size", "int")); + NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "size", "int")); return result; } @@ -5569,7 +5566,7 @@ napi_value NapiWebviewController::SetBackForwardCacheOptions(napi_env env, napi_ if (!NapiParseUtils::ParseInt32(env, timeToLiveObj, timeToLive)) { BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR, "timeToLive", "int")); + NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "timeToLive", "int")); return result; } @@ -5580,9 +5577,6 @@ napi_value NapiWebviewController::SetBackForwardCacheOptions(napi_env env, napi_ return result; } - WVLOG_I("The value of backforward cache option size is: %{public}d", size); - WVLOG_I("The value of backforward cache option timeToLive is: %{public}d", timeToLive); - webviewController->SetBackForwardCacheOptions(size, timeToLive); NAPI_CALL(env, napi_get_undefined(env, &result)); return result; diff --git a/interfaces/kits/napi/webviewcontroller/webview_controller.h b/interfaces/kits/napi/webviewcontroller/webview_controller.h index 687962b8c..508d8d160 100644 --- a/interfaces/kits/napi/webviewcontroller/webview_controller.h +++ b/interfaces/kits/napi/webviewcontroller/webview_controller.h @@ -370,6 +370,7 @@ public: void SetPathAllowingUniversalAccess(const std::vector& pathList, std::string& errorPath); + void ScrollToWithAnime(float x, float y, int32_t duration) ; void ScrollByWithAnime(float deltaX, float deltaY, int32_t duration) ; diff --git a/ohos_interface/include/ohos_nweb/nweb.h b/ohos_interface/include/ohos_nweb/nweb.h index 1c9bef3aa..4702f902a 100644 --- a/ohos_interface/include/ohos_nweb/nweb.h +++ b/ohos_interface/include/ohos_nweb/nweb.h @@ -1377,11 +1377,6 @@ public: */ virtual void ScrollByWithAnime(float delta_x, float delta_y, int32_t duration) {} - /** - * @brief Set backforward cache options. - */ - virtual void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) { return; } - /** * @brief Send mouse wheel event. */ @@ -1435,6 +1430,11 @@ public: */ virtual void RegisterArkJSfunction(const std::string& object_name, const std::vector& method_list, const std::vector& async_method_list, const int32_t object_id, const std::string& permission) {} + + /** + * @brief Set backforward cache options. + */ + virtual void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) { return; } }; } // namespace OHOS::NWeb diff --git a/ohos_interface/include/ohos_nweb/nweb_engine.h b/ohos_interface/include/ohos_nweb/nweb_engine.h index 351e52247..41853edc0 100644 --- a/ohos_interface/include/ohos_nweb/nweb_engine.h +++ b/ohos_interface/include/ohos_nweb/nweb_engine.h @@ -82,8 +82,6 @@ public: virtual std::shared_ptr GetAdsBlockManager() { return nullptr; }; - - virtual void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) {}; }; } // namespace OHOS::NWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp index d94b0c6bc..5b55bcb92 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp @@ -212,9 +212,4 @@ ArkWebRefPtr ArkWebEngineImpl::GetAdsBlockManager() } return new ArkWebAdsBlockManagerImpl(nweb_adsBlock_manager); } - -void ArkWebEngineImpl::EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) -{ - nweb_engine_->EnableBackForwardCache(enableNativeEmbed, enableMediaIntercept); -} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.h index ac1b9431d..51b5992d8 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_nweb_impl.h @@ -1212,12 +1212,6 @@ public: /*--ark web()--*/ void ScrollByWithAnime(float delta_x, float delta_y, int32_t duration) override; - /** - * @brief set backforward cache options. - */ - /*--ark web()--*/ - void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) override; - /** * @brief Send mouse wheel event. */ @@ -1272,6 +1266,12 @@ public: */ void RegisterArkJSfunction(const ArkWebString& object_name, const ArkWebStringVector& method_list, const ArkWebStringVector& async_method_list, const int32_t object_id, const ArkWebString& permission) override; + + /** + * @brief set backforward cache options. + */ + /*--ark web()--*/ + void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) override; private: std::shared_ptr nweb_nweb_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp index b384e5bf5..c08317fa5 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp @@ -240,8 +240,4 @@ ArkWebEngineWrapper::GetAdsBlockManager() { return std::make_shared(ark_web_adsblock_manager); } - -void ArkWebEngineWrapper::EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) { - ark_web_engine_->EnableBackForwardCache(enableNativeEmbed, enableMediaIntercept); -} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h index 865d41b8e..6400bfcdd 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h @@ -76,8 +76,6 @@ public: void EnableWholeWebPageDrawing() override; std::shared_ptr GetAdsBlockManager() override; - - void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) override; private: ArkWebRefPtr ark_web_engine_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h index 084049c79..4b6748ce5 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_nweb_wrapper.h @@ -1220,12 +1220,6 @@ public: /*--ark web()--*/ void ScrollByWithAnime(float delta_x, float delta_y, int32_t duration) override; - /** - * @brief set backforward cache options. - */ - /*--ark web()--*/ - void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) override; - /** * @brief Send mouse wheel event. */ @@ -1281,6 +1275,12 @@ public: const std::vector& async_method_list, const int32_t object_id, const std::string& permission) override; + + /** + * @brief set backforward cache options. + */ + /*--ark web()--*/ + void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) override; private: ArkWebRefPtr ark_web_nweb_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h index 30021b2cb..ab6194dcf 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h +++ b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h @@ -108,9 +108,6 @@ public: /*--ark web()--*/ virtual ArkWebRefPtr GetAdsBlockManager() = 0; - - /*--ark web()--*/ - virtual void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) = 0; }; } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_nweb.h b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_nweb.h index d7a135721..76b150336 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_nweb.h +++ b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_nweb.h @@ -1372,12 +1372,6 @@ public: /*--ark web()--*/ virtual void ScrollByWithAnime(float delta_x, float delta_y, int32_t duration) = 0; - /** - * @brief set backforward cache options. - */ - /*--ark web()--*/ - virtual void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) = 0; - /** * @brief Send mouse wheel event. */ @@ -1432,6 +1426,12 @@ public: /*--ark web()--*/ virtual void RegisterArkJSfunction(const ArkWebString& object_name, const ArkWebStringVector& method_list, const ArkWebStringVector& async_method_list, const int32_t object_id, const ArkWebString& permission) = 0; + + /** + * @brief set backforward cache options. + */ + /*--ark web()--*/ + virtual void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) = 0; }; } // namespace OHOS::ArkWeb diff --git a/ohos_nweb/include/nweb_helper.h b/ohos_nweb/include/nweb_helper.h index e5c7f0f45..b65860d10 100644 --- a/ohos_nweb/include/nweb_helper.h +++ b/ohos_nweb/include/nweb_helper.h @@ -86,8 +86,6 @@ public: void EnableWholeWebPageDrawing(); std::shared_ptr GetAdsBlockManager(); - void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept); - private: NWebHelper() = default; bool LoadLib(bool from_ark); diff --git a/ohos_nweb/src/nweb_helper.cpp b/ohos_nweb/src/nweb_helper.cpp index 74acf8087..d51b45202 100644 --- a/ohos_nweb/src/nweb_helper.cpp +++ b/ohos_nweb/src/nweb_helper.cpp @@ -814,7 +814,7 @@ bool NWebHelper::InitAndRun(bool from_ark) if (backForwardCacheCmdLine_.size() != 0) { for (auto backForwardCacheCmdLine : backForwardCacheCmdLine_) { initArgs->AddArg(backForwardCacheCmdLine); - WVLOG_I("Add command line: %{public}s", backForwardCacheCmdLine.c_str()); + WVLOG_I("Add command line when init web engine: %{public}s", backForwardCacheCmdLine.c_str()); } } @@ -1087,15 +1087,12 @@ void NWebHelper::ClearHostIP(const std::string& hostName) void NWebHelper::EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) { this->backForwardCacheCmdLine_.emplace_back("--enable-bfcache"); - WVLOG_I("The value of backForwardCacheCmdLin_ size is %{public}d", this->backForwardCacheCmdLine_.size()); if (enableNativeEmbed) { this->backForwardCacheCmdLine_.emplace_back("--enable-cache-native-embed"); - WVLOG_I("The value of backForwardCacheCmdLin_ size is %{public}d", this->backForwardCacheCmdLine_.size()); } if (enableNativeEmbed) { this->backForwardCacheCmdLine_.emplace_back("--enable-cache-media-intercept"); - WVLOG_I("The value of backForwardCacheCmdLin_ size is %{public}d", this->backForwardCacheCmdLine_.size()); } } diff --git a/test/unittest/nweb_helper_test/nweb_helper_test.cpp b/test/unittest/nweb_helper_test/nweb_helper_test.cpp index 33fcc2acc..2ebb7da75 100644 --- a/test/unittest/nweb_helper_test/nweb_helper_test.cpp +++ b/test/unittest/nweb_helper_test/nweb_helper_test.cpp @@ -143,6 +143,8 @@ public: { return nullptr; } + + void EnableBackForwardCache(bool nativeEmbed, bool mediaTakeOver); }; void NwebHelperTest::SetUpTestCase(void) @@ -192,6 +194,7 @@ HWTEST_F(NwebHelperTest, NWebHelper_SetBundlePath_001, TestSize.Level1) NWebHelper::Instance().PrefetchResource(nullptr, {}, "web_test", 0); NWebHelper::Instance().ClearPrefetchedResource({"web_test"}); NWebAdapterHelper::Instance().ReadConfigIfNeeded(); + NWebHelper::Instance().EnableBackForwardCache(true, true); result = NWebHelper::Instance().InitAndRun(false); EXPECT_FALSE(result); ApplicationContextMock *contextMock = new ApplicationContextMock(); -- Gitee From f1be1859473dbb902120f98779fdb6a712f40f57 Mon Sep 17 00:00:00 2001 From: tengfan3 Date: Thu, 4 Jul 2024 14:32:37 +0800 Subject: [PATCH 09/32] =?UTF-8?q?=E4=BC=A0=E6=84=9F=E5=99=A8=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=8F=8A=E6=9D=83=E9=99=90=E5=AF=B9=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tengfan3 --- bundle.json | 1 + config.gni | 5 + ohos_adapter/BUILD.gn | 7 + .../include/ohos_adapter_helper_impl.h | 2 + .../src/ohos_adapter_helper_impl.cpp | 12 + .../include/sensor_adapter_impl.h | 58 ++++ .../src/sensor_adapter_impl.cpp | 303 ++++++++++++++++++ ohos_glue/BUILD.gn | 8 + .../ohos_adapter/ohos_adapter_helper.h | 3 + .../include/ohos_adapter/sensor_adapter.h | 65 ++++ .../include/ohos_nweb/nweb_access_request.h | 3 +- ohos_interface/ohos_glue/BUILD_webcore.gn | 8 + .../base/include/ark_web_bridge_types.h | 2 + .../ark_ohos_adapter_helper_wrapper.cpp | 11 + .../webcore/ark_ohos_adapter_helper_wrapper.h | 2 + .../webcore/ark_sensor_adapter_wrapper.cpp | 73 +++++ .../webcore/ark_sensor_adapter_wrapper.h | 45 +++ .../ark_sensor_callback_adapter_impl.cpp | 30 ++ .../ark_sensor_callback_adapter_impl.h | 40 +++ .../webview/ark_ohos_adapter_helper_impl.cpp | 8 +- .../webview/ark_ohos_adapter_helper_impl.h | 2 + .../webview/ark_sensor_adapter_impl.cpp | 73 +++++ .../bridge/webview/ark_sensor_adapter_impl.h | 54 ++++ .../ark_sensor_callback_adapter_wrapper.cpp | 31 ++ .../ark_sensor_callback_adapter_wrapper.h | 38 +++ .../include/ark_ohos_adapter_helper.h | 4 + .../ohos_adapter/include/ark_sensor_adapter.h | 64 ++++ 27 files changed, 950 insertions(+), 2 deletions(-) create mode 100644 ohos_adapter/sensor_adapter/include/sensor_adapter_impl.h create mode 100644 ohos_adapter/sensor_adapter/src/sensor_adapter_impl.cpp create mode 100644 ohos_interface/include/ohos_adapter/sensor_adapter.h create mode 100644 ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_adapter_wrapper.cpp create mode 100644 ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_adapter_wrapper.h create mode 100644 ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_callback_adapter_impl.cpp create mode 100644 ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_callback_adapter_impl.h create mode 100644 ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_adapter_impl.cpp create mode 100644 ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_adapter_impl.h create mode 100644 ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_callback_adapter_wrapper.cpp create mode 100644 ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_callback_adapter_wrapper.h create mode 100644 ohos_interface/ohos_glue/ohos_adapter/include/ark_sensor_adapter.h diff --git a/bundle.json b/bundle.json index 068d0402b..c7c93faf5 100644 --- a/bundle.json +++ b/bundle.json @@ -71,6 +71,7 @@ "relational_store", "resource_schedule_service", "samgr", + "sensor", "soc_perf", "time_service", "window_manager", diff --git a/config.gni b/config.gni index cd08c7309..84868daee 100644 --- a/config.gni +++ b/config.gni @@ -24,6 +24,7 @@ declare_args() { webview_print_enable = true webview_enterprise_device_manager_enable = true webview_media_avsession_enable = true + webview_sensors_sensor_enable = true if (defined(global_parts_info) && !defined(global_parts_info.resourceschedule_soc_perf)) { @@ -75,4 +76,8 @@ declare_args() { !defined(global_parts_info.multimedia_av_session)) { webview_media_avsession_enable = false } + if (defined(global_parts_info) && + !defined(global_parts_info.sensors_sensor)) { + webview_sensors_sensor_enable = false + } } diff --git a/ohos_adapter/BUILD.gn b/ohos_adapter/BUILD.gn index 22071d4da..7cdb4fc0c 100644 --- a/ohos_adapter/BUILD.gn +++ b/ohos_adapter/BUILD.gn @@ -102,6 +102,7 @@ ohos_shared_library("nweb_ohos_adapter") { "screen_capture_adapter/include", "soc_perf_adapter/include", "system_properties_adapter/include", + "sensor_adapter/include", ] public_configs = [ ":ohos_adapter_public_interface" ] @@ -286,6 +287,12 @@ ohos_shared_library("nweb_ohos_adapter") { defines += [ "NWEB_MEDIA_AVSESSION_ENABLE" ] } + if (webview_sensors_sensor_enable) { + sources += [ "sensor_adapter/src/sensor_adapter_impl.cpp" ] + external_deps += [ "sensor:sensor_interface_native" ] + defines += [ "NWEB_SENSORS_SENSOR_ENABLE" ] + } + innerapi_tags = [ "platformsdk" ] part_name = "webview" subsystem_name = "web" diff --git a/ohos_adapter/ohos_adapter_helper/include/ohos_adapter_helper_impl.h b/ohos_adapter/ohos_adapter_helper/include/ohos_adapter_helper_impl.h index 7ca517ce1..7bd751b8c 100644 --- a/ohos_adapter/ohos_adapter_helper/include/ohos_adapter_helper_impl.h +++ b/ohos_adapter/ohos_adapter_helper/include/ohos_adapter_helper_impl.h @@ -110,6 +110,8 @@ public: std::unique_ptr CreateMediaAVSessionAdapter() override; std::unique_ptr CreateOhosImageDecoderAdapter() override; + + std::unique_ptr CreateSensorAdapter() override; }; } // namespace OHOS::NWeb diff --git a/ohos_adapter/ohos_adapter_helper/src/ohos_adapter_helper_impl.cpp b/ohos_adapter/ohos_adapter_helper/src/ohos_adapter_helper_impl.cpp index 66b6a8f38..8c429eacb 100644 --- a/ohos_adapter/ohos_adapter_helper/src/ohos_adapter_helper_impl.cpp +++ b/ohos_adapter/ohos_adapter_helper/src/ohos_adapter_helper_impl.cpp @@ -70,6 +70,9 @@ #include "system_properties_adapter_impl.h" #include "vsync_adapter_impl.h" #include "window_adapter_impl.h" +#if defined(NWEB_SENSORS_SENSOR_ENABLE) +#include "sensor_adapter_impl.h" +#endif namespace OHOS::NWeb { // static @@ -323,4 +326,13 @@ std::unique_ptr OhosAdapterHelperImpl::CreateOhosImageD { return std::make_unique(); } + +std::unique_ptr OhosAdapterHelperImpl::CreateSensorAdapter() +{ +#if defined(NWEB_SENSORS_SENSOR_ENABLE) + return std::make_unique(); +#else + return nullptr; +#endif +} } // namespace OHOS::NWeb diff --git a/ohos_adapter/sensor_adapter/include/sensor_adapter_impl.h b/ohos_adapter/sensor_adapter/include/sensor_adapter_impl.h new file mode 100644 index 000000000..8d3c78b2b --- /dev/null +++ b/ohos_adapter/sensor_adapter/include/sensor_adapter_impl.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2024 Huawei Device 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. + */ + +#ifndef SENSOR_ADAPTER_IMPL_H +#define SENSOR_ADAPTER_IMPL_H + +#include "sensor_adapter.h" +#include "sensor_agent.h" + +namespace OHOS::NWeb { + +class SensorCallbackImpl { +public: + SensorCallbackImpl(std::shared_ptr callbackAdapter); + ~SensorCallbackImpl() = default; + + void UpdateOhosSensorData(double timestamp, + double value1, double value2, double value3, double value4); +private: + std::shared_ptr callbackAdapter_; +}; + +class SensorAdapterImpl : public SensorAdapter { +public: + SensorAdapterImpl() = default; + ~SensorAdapterImpl() = default; + + int32_t IsOhosSensorSupported(int32_t sensorTypeId) override; + int32_t GetOhosSensorReportingMode(int32_t sensorTypeId) override; + double GetOhosSensorDefaultSupportedFrequency(int32_t sensorTypeId) override; + double GetOhosSensorMinSupportedFrequency(int32_t sensorTypeId) override; + double GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeId) override; + int32_t SubscribeOhosSensor(int32_t sensorTypeId, int64_t samplingInterval) override; + int32_t RegistOhosSensorCallback(int32_t sensorTypeId, + std::shared_ptr callbackAdapter) override; + int32_t UnsubscribeOhosSensor(int32_t sensorTypeId) override; + +private: + static void OhosSensorCallback(SensorEvent* event); + static std::unordered_map> sensorCallbackMap; + + SensorUser mSensorUser{}; +}; + +} +#endif // SENSOR_ADAPTER_IMPL_H \ No newline at end of file diff --git a/ohos_adapter/sensor_adapter/src/sensor_adapter_impl.cpp b/ohos_adapter/sensor_adapter/src/sensor_adapter_impl.cpp new file mode 100644 index 000000000..d6f6328f4 --- /dev/null +++ b/ohos_adapter/sensor_adapter/src/sensor_adapter_impl.cpp @@ -0,0 +1,303 @@ +/* + * Copyright (c) 2024 Huawei Device 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 "sensor_adapter_impl.h" + +#include +#include + +#include "nweb_log.h" + +namespace OHOS::NWeb { + +std::unordered_map> SensorAdapterImpl::sensorCallbackMap; +constexpr double NANOSECONDS_IN_SECOND = 1000000000.0; +constexpr double DEFAULT_SAMPLE_PERIOD = 200000000.0; + +SensorTypeId SensorTypeToOhosSensorType(int sensorTypeId) { + SensorTypeId ohosSensorTypeId = SENSOR_TYPE_ID_NONE; + const static std::map TO_OHOS_SENSOR_TYPE_MAP = { + {2 /* ACCELEROMETER */, SENSOR_TYPE_ID_ACCELEROMETER }, + {3 /* LINEAR_ACCELERATION */, SENSOR_TYPE_ID_LINEAR_ACCELERATION }, + {4 /* GRAVITY */, SENSOR_TYPE_ID_GRAVITY }, + {5 /* GYROSCOPE */, SENSOR_TYPE_ID_GYROSCOPE }, + {8 /* ABSOLUTE_ORIENTATION_EULER_ANGLES}*/, SENSOR_TYPE_ID_ORIENTATION } + }; + auto checkIter = TO_OHOS_SENSOR_TYPE_MAP.find(sensorTypeId); + if (checkIter != TO_OHOS_SENSOR_TYPE_MAP.end()) { + ohosSensorTypeId = checkIter->second; + } + return ohosSensorTypeId; +} + +std::string SensorTypeToSensorUserName(int sensorTypeId) { + const static std::map TO_OHOS_SENSOR_USER_NAME_MAP = { + {2 /* ACCELEROMETER */, "OhosAccelerometerService" }, + {3 /* LINEAR_ACCELERATION */, "OhosLinearAccelerometerService" }, + {4 /* GRAVITY */, "OhosGravityService" }, + {5 /* GYROSCOPE */, "OhosCyroscopeService" }, + {8 /* ABSOLUTE_ORIENTATION_EULER_ANGLES}*/, "OhosOrientationService" } + }; + std::string userName = "OhosSensorService"; + auto checkIter = TO_OHOS_SENSOR_USER_NAME_MAP.find(sensorTypeId); + if (checkIter != TO_OHOS_SENSOR_USER_NAME_MAP.end()) { + userName = checkIter->second; + } + return userName; +} + +SensorCallbackImpl::SensorCallbackImpl( + std::shared_ptr callbackAdapter) + : callbackAdapter_(callbackAdapter) { +} + +void SensorCallbackImpl::UpdateOhosSensorData(double timestamp, + double value1, double value2, double value3, double value4) { + if (callbackAdapter_) { + callbackAdapter_->UpdateOhosSensorData(timestamp, value1, value2, value3, value4); + } +} + +int32_t SensorAdapterImpl::IsOhosSensorSupported(int32_t sensorTypeId) { + int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); + if (ohosSensorTypeId != SENSOR_TYPE_ID_NONE) { + SensorInfo* sensorInfo = nullptr; + int32_t count; + int ret = GetAllSensors(&sensorInfo, &count); + if (ret != SENSOR_SUCCESS || sensorInfo == nullptr || count < 0) { + WVLOG_E("IsOhosSensorSupported Error, ret = %{public}d, count = %{public}d.", ret, count); + return SENSOR_ERROR; + } + + for (int i = 0; i < count; i++) { + if (sensorInfo[i].sensorId == ohosSensorTypeId) { + WVLOG_I("IsOhosSensorSupported SUCCESS, sensorTypeId = %{public}d.", sensorTypeId); + return SENSOR_SUCCESS; + } + } + } + WVLOG_E("IsOhosSensorSupported Error, sensorTypeId = %{public}d is invalid.", sensorTypeId); + return SENSOR_ERROR; +} + +int32_t SensorAdapterImpl::GetOhosSensorReportingMode(int32_t sensorTypeId) { + int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); + int32_t reportingMode = -1; + switch (ohosSensorTypeId) { + case SENSOR_TYPE_ID_ACCELEROMETER: + case SENSOR_TYPE_ID_GRAVITY: + case SENSOR_TYPE_ID_LINEAR_ACCELERATION: + case SENSOR_TYPE_ID_GYROSCOPE: + case SENSOR_TYPE_ID_ORIENTATION: + reportingMode = SENSOR_DATA_REPORT_CONTINUOUS; + break; + default: + break; + } + return reportingMode; +} + +double SensorAdapterImpl::GetOhosSensorDefaultSupportedFrequency(int32_t sensorTypeId) { + int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); + double defaultFrequency = 0.0; + if (ohosSensorTypeId != SENSOR_TYPE_ID_NONE) { + defaultFrequency = NANOSECONDS_IN_SECOND / DEFAULT_SAMPLE_PERIOD; + } + WVLOG_I("GetOhosSensorDefaultSupportedFrequency sensorTypeId: %{public}d, defaultFrequency: %{public}f", + sensorTypeId, defaultFrequency); + return defaultFrequency; +} + +double SensorAdapterImpl::GetOhosSensorMinSupportedFrequency(int32_t sensorTypeId) { + int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); + double minFrequency = 0.0; + if (ohosSensorTypeId == SENSOR_TYPE_ID_NONE) { + WVLOG_E("GetOhosSensorMinSupportedFrequency Error, sensorTypeId = %{public}d is invalid.", sensorTypeId); + return minFrequency; + } + SensorInfo* sensorInfo = nullptr; + int32_t count; + int ret = GetAllSensors(&sensorInfo, &count); + if (ret != SENSOR_SUCCESS || sensorInfo == nullptr || count < 0) { + WVLOG_E("GetOhosSensorMinSupportedFrequency Error, ret = %{public}d, count = %{public}d.", ret, count); + return minFrequency; + } + for (int i = 0; i < count; i++) { + if (sensorInfo[i].sensorId == ohosSensorTypeId) { + int64_t maxSamplePeriod = sensorInfo[i].maxSamplePeriod; + if (maxSamplePeriod > 0) { + minFrequency = NANOSECONDS_IN_SECOND / static_cast(maxSamplePeriod); + } + break; + } + } + WVLOG_I("GetOhosSensorMinSupportedFrequency sensorTypeId: %{public}d, minFrequency: %{public}f", + sensorTypeId, minFrequency); + return minFrequency; +} + +double SensorAdapterImpl::GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeId) { + int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); + double maxFrequency = 0.0; + if (ohosSensorTypeId == SENSOR_TYPE_ID_NONE) { + WVLOG_E("GetOhosSensorMaxSupportedFrequency Error, sensorTypeId = %{public}d is invalid.", sensorTypeId); + return maxFrequency; + } + SensorInfo* sensorInfo = nullptr; + int32_t count; + int ret = GetAllSensors(&sensorInfo, &count); + if (ret != SENSOR_SUCCESS || sensorInfo == nullptr || count < 0) { + WVLOG_E("GetOhosSensorMaxSupportedFrequency Error, ret = %{public}d, count = %{public}d.", ret, count); + return maxFrequency; + } + for (int i = 0; i < count; i++) { + if (sensorInfo[i].sensorId == ohosSensorTypeId) { + int64_t minSamplePeriod = sensorInfo[i].minSamplePeriod; + if (minSamplePeriod > 0) { + maxFrequency = NANOSECONDS_IN_SECOND / static_cast(minSamplePeriod); + } + break; + } + } + WVLOG_I("GetOhosSensorMaxSupportedFrequency sensorTypeId: %{public}d, maxFrequency: %{public}f", + sensorTypeId, maxFrequency); + return maxFrequency; +} + +void SensorAdapterImpl::OhosSensorCallback(SensorEvent* event) { + std::shared_ptr callback = nullptr; + auto findIter = sensorCallbackMap.find(event->sensorTypeId); + if (findIter != sensorCallbackMap.end()) { + callback = findIter->second; + } + if ((event == nullptr) || (callback == nullptr)) { + WVLOG_E("OhosSensorCallback Error."); + return; + } + switch (event->sensorTypeId) { + case SENSOR_TYPE_ID_ACCELEROMETER: { + AccelData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); + } + break; + } + case SENSOR_TYPE_ID_GRAVITY: { + GravityData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); + } + break; + } + case SENSOR_TYPE_ID_LINEAR_ACCELERATION: { + LinearAccelData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); + } + break; + } + case SENSOR_TYPE_ID_GYROSCOPE: { + GyroscopeData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); + } + break; + } + case SENSOR_TYPE_ID_ORIENTATION: { + OrientationData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->beta, data->gamma, data->alpha, 0.0f); + } + break; + } + default: + break; + } +} + +int32_t SensorAdapterImpl::SubscribeOhosSensor(int32_t sensorTypeId, int64_t samplingInterval) { + WVLOG_I("SubscribeOhosSensor sensorTypeId: %{public}d, samplingInterval: %{public}lld", + sensorTypeId, samplingInterval); + if (samplingInterval <= 0) { + WVLOG_E("SubscribeOhosSensor error, samplingInterval is invalid."); + return SENSOR_PARAMETER_ERROR; + } + int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); + if (ohosSensorTypeId == SENSOR_TYPE_ID_NONE) { + WVLOG_E("SubscribeOhosSensor error, sensorTypeId is invalid."); + return SENSOR_PARAMETER_ERROR; + } + + std::string userName = SensorTypeToSensorUserName(sensorTypeId); + (void)strcpy_s(mSensorUser.name, sizeof(mSensorUser.name), userName.c_str()); + mSensorUser.userData = nullptr; + mSensorUser.callback = OhosSensorCallback; + int32_t ret = SENSOR_SUCCESS; + ret = SubscribeSensor(ohosSensorTypeId, &mSensorUser); + if (ret != SENSOR_SUCCESS) { + WVLOG_E("SubscribeOhosSensor error, call SubscribeSensor ret = %{public}d.", ret); + return ret; + } + ret = SetBatch(ohosSensorTypeId, &mSensorUser, samplingInterval, samplingInterval); + if (ret != SENSOR_SUCCESS) { + WVLOG_E("SubscribeOhosSensor error, call SetBatch ret = %{public}d.", ret); + return ret; + } + ret = ActivateSensor(ohosSensorTypeId, &mSensorUser); + if (ret != SENSOR_SUCCESS) { + WVLOG_E("SubscribeOhosSensor error, call ActivateSensor ret = %{public}d.", ret); + return ret; + } + ret = SetMode(ohosSensorTypeId, &mSensorUser, SENSOR_REALTIME_MODE); + if (ret != SENSOR_SUCCESS) { + WVLOG_E("SubscribeOhosSensor error, call SetMode ret = %{public}d.", ret); + return ret; + } + return SENSOR_SUCCESS; +} + +int32_t SensorAdapterImpl::RegistOhosSensorCallback(int32_t sensorTypeId, + std::shared_ptr callbackAdapter) { + int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); + if (ohosSensorTypeId != SENSOR_TYPE_ID_NONE) { + auto callback = std::make_shared(callbackAdapter); + sensorCallbackMap[ohosSensorTypeId] = callback; + return SENSOR_SUCCESS; + } + WVLOG_E("RegistOhosSensorCallback error, sensorTypeId is invalid."); + return SENSOR_PARAMETER_ERROR; +} + +int32_t SensorAdapterImpl::UnsubscribeOhosSensor(int32_t sensorTypeId) { + WVLOG_I("UnsubscribeOhosSensor sensorTypeId: %{public}d.", sensorTypeId); + int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); + if (ohosSensorTypeId != SENSOR_TYPE_ID_NONE) { + sensorCallbackMap.erase(ohosSensorTypeId); + int32_t ret = DeactivateSensor(ohosSensorTypeId, &mSensorUser); + if (ret != SENSOR_SUCCESS) { + WVLOG_E("UnsubscribeOhosSensor error, call DeactivateSensor ret = %{public}d.", ret); + return ret; + } + ret = UnsubscribeSensor(ohosSensorTypeId, &mSensorUser); + if (ret != SENSOR_SUCCESS) { + WVLOG_E("UnsubscribeOhosSensor error, call UnsubscribeSensor ret = %{public}d.", ret); + return ret; + } + return SENSOR_SUCCESS; + } + WVLOG_E("UnsubscribeOhosSensor error, sensorTypeId is invalid."); + return SENSOR_PARAMETER_ERROR; +} +} // namespace OHOS::NWeb \ No newline at end of file diff --git a/ohos_glue/BUILD.gn b/ohos_glue/BUILD.gn index e4386d88b..15f54a184 100644 --- a/ohos_glue/BUILD.gn +++ b/ohos_glue/BUILD.gn @@ -655,6 +655,10 @@ action("ohos_glue_adapter_prepare") { "${glue_build_gen_dir}/ohos_adapter/bridge/ark_screen_capture_callback_adapter_wrapper.h", "${glue_build_gen_dir}/ohos_adapter/bridge/ark_screen_capture_config_adapter_wrapper.cpp", "${glue_build_gen_dir}/ohos_adapter/bridge/ark_screen_capture_config_adapter_wrapper.h", + "${glue_build_gen_dir}/ohos_adapter/bridge/ark_sensor_adapter_impl.cpp", + "${glue_build_gen_dir}/ohos_adapter/bridge/ark_sensor_adapter_impl.h", + "${glue_build_gen_dir}/ohos_adapter/bridge/ark_sensor_callback_adapter_wrapper.cpp", + "${glue_build_gen_dir}/ohos_adapter/bridge/ark_sensor_callback_adapter_wrapper.h", "${glue_build_gen_dir}/ohos_adapter/bridge/ark_soc_perf_client_adapter_impl.cpp", "${glue_build_gen_dir}/ohos_adapter/bridge/ark_soc_perf_client_adapter_impl.h", "${glue_build_gen_dir}/ohos_adapter/bridge/ark_surface_buffer_adapter_impl.cpp", @@ -817,6 +821,8 @@ action("ohos_glue_adapter_prepare") { "${glue_build_gen_dir}/ohos_adapter/cpptoc/ark_running_lock_adapter_cpptoc.h", "${glue_build_gen_dir}/ohos_adapter/cpptoc/ark_screen_capture_adapter_cpptoc.cpp", "${glue_build_gen_dir}/ohos_adapter/cpptoc/ark_screen_capture_adapter_cpptoc.h", + "${glue_build_gen_dir}/ohos_adapter/cpptoc/ark_sensor_adapter_cpptoc.cpp", + "${glue_build_gen_dir}/ohos_adapter/cpptoc/ark_sensor_adapter_cpptoc.h", "${glue_build_gen_dir}/ohos_adapter/cpptoc/ark_soc_perf_client_adapter_cpptoc.cpp", "${glue_build_gen_dir}/ohos_adapter/cpptoc/ark_soc_perf_client_adapter_cpptoc.h", "${glue_build_gen_dir}/ohos_adapter/cpptoc/ark_surface_buffer_adapter_cpptoc.cpp", @@ -935,6 +941,8 @@ action("ohos_glue_adapter_prepare") { "${glue_build_gen_dir}/ohos_adapter/ctocpp/ark_screen_capture_callback_adapter_ctocpp.h", "${glue_build_gen_dir}/ohos_adapter/ctocpp/ark_screen_capture_config_adapter_ctocpp.cpp", "${glue_build_gen_dir}/ohos_adapter/ctocpp/ark_screen_capture_config_adapter_ctocpp.h", + "${glue_build_gen_dir}/ohos_adapter/ctocpp/ark_sensor_callback_adapter_ctocpp.cpp", + "${glue_build_gen_dir}/ohos_adapter/ctocpp/ark_sensor_callback_adapter_ctocpp.h", "${glue_build_gen_dir}/ohos_adapter/ctocpp/ark_timezone_event_callback_adapter_ctocpp.cpp", "${glue_build_gen_dir}/ohos_adapter/ctocpp/ark_timezone_event_callback_adapter_ctocpp.h", "${glue_build_gen_dir}/ohos_adapter/ctocpp/ark_video_capture_info_adapter_ctocpp.cpp", diff --git a/ohos_interface/include/ohos_adapter/ohos_adapter_helper.h b/ohos_interface/include/ohos_adapter/ohos_adapter_helper.h index 6a82adfef..0b89991c9 100644 --- a/ohos_interface/include/ohos_adapter/ohos_adapter_helper.h +++ b/ohos_interface/include/ohos_adapter/ohos_adapter_helper.h @@ -54,6 +54,7 @@ #include "screen_capture_adapter.h" #include "soc_perf_client_adapter.h" #include "system_properties_adapter.h" +#include "sensor_adapter.h" namespace OHOS::NWeb { class OhosAdapterHelper { @@ -143,6 +144,8 @@ public: virtual std::unique_ptr CreateMediaAVSessionAdapter() = 0; virtual std::unique_ptr CreateOhosImageDecoderAdapter() = 0; + + virtual std::unique_ptr CreateSensorAdapter() = 0; }; } // namespace OHOS::NWeb diff --git a/ohos_interface/include/ohos_adapter/sensor_adapter.h b/ohos_interface/include/ohos_adapter/sensor_adapter.h new file mode 100644 index 000000000..c3253b0e0 --- /dev/null +++ b/ohos_interface/include/ohos_adapter/sensor_adapter.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2024 Huawei Device 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. + */ + +#ifndef SENSOR_ADAPTER_H +#define SENSOR_ADAPTER_H + +#include + + +namespace OHOS::NWeb { + +enum { + SENSOR_ERROR = -1, + SENSOR_SUCCESS = 0, + SENSOR_PERMISSION_DENIED = 201, // Use this error code when permission is denied. + SENSOR_PARAMETER_ERROR = 401, // Use this error code when the input parameter type or range does not match. + SENSOR_SERVICE_EXCEPTION = 14500101, // Use this error code when the service is exception. + SENSOR_NO_SUPPORT = 14500102, // Use this error code when the sensor is not supported by the device. + SENSOR_NON_SYSTEM_API = 202 // Permission check failed. A non-system application uses the system API. +}; + +enum { + SENSOR_DATA_REPORT_ON_CHANGE = 0, + SENSOR_DATA_REPORT_CONTINUOUS = 1 +}; + +class SensorCallbackAdapter { +public: + virtual ~SensorCallbackAdapter() = default; + + virtual void UpdateOhosSensorData(double timestamp, + double value1, double value2, double value3, double value4) = 0; +}; + +class SensorAdapter { +public: + SensorAdapter() = default; + virtual ~SensorAdapter() = default; + + virtual int32_t IsOhosSensorSupported(int32_t sensorTypeId) = 0; + virtual int32_t GetOhosSensorReportingMode(int32_t sensorTypeId) = 0; + virtual double GetOhosSensorDefaultSupportedFrequency(int32_t sensorTypeId) = 0; + virtual double GetOhosSensorMinSupportedFrequency(int32_t sensorTypeId) = 0; + virtual double GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeId) = 0; + virtual int32_t SubscribeOhosSensor(int32_t sensorTypeId, int64_t samplingInterval) = 0; + virtual int32_t RegistOhosSensorCallback(int32_t sensorTypeId, + std::shared_ptr callbackAdapter) = 0; + virtual int32_t UnsubscribeOhosSensor(int32_t sensorTypeId) = 0; +}; + +} + +#endif // SENSOR_ADAPTER_H \ No newline at end of file diff --git a/ohos_interface/include/ohos_nweb/nweb_access_request.h b/ohos_interface/include/ohos_nweb/nweb_access_request.h index 703f989f3..a41853f46 100644 --- a/ohos_interface/include/ohos_nweb/nweb_access_request.h +++ b/ohos_interface/include/ohos_nweb/nweb_access_request.h @@ -35,7 +35,8 @@ public: PROTECTED_MEDIA_ID = 1 << 3, MIDI_SYSEX = 1 << 4, CLIPBOARD_READ_WRITE = 1 << 5, - CLIPBOARD_SANITIZED_WRITE = 1 << 6, + CLIPBOARD_SANITIZED_WRITE = 1 << 6, + SENSORS = 1 << 7, }; /** diff --git a/ohos_interface/ohos_glue/BUILD_webcore.gn b/ohos_interface/ohos_glue/BUILD_webcore.gn index 9e56406b8..c8201e332 100644 --- a/ohos_interface/ohos_glue/BUILD_webcore.gn +++ b/ohos_interface/ohos_glue/BUILD_webcore.gn @@ -647,6 +647,10 @@ component("ohos_adapter_glue_source") { "ohos_adapter/bridge/ark_screen_capture_callback_adapter_impl.h", "ohos_adapter/bridge/ark_screen_capture_config_adapter_impl.cpp", "ohos_adapter/bridge/ark_screen_capture_config_adapter_impl.h", + "ohos_adapter/bridge/ark_sensor_adapter_wrapper.cpp", + "ohos_adapter/bridge/ark_sensor_adapter_wrapper.h", + "ohos_adapter/bridge/ark_sensor_callback_adapter_impl.cpp", + "ohos_adapter/bridge/ark_sensor_callback_adapter_impl.h", "ohos_adapter/bridge/ark_soc_perf_client_adapter_wrapper.cpp", "ohos_adapter/bridge/ark_soc_perf_client_adapter_wrapper.h", "ohos_adapter/bridge/ark_surface_buffer_adapter_wrapper.cpp", @@ -775,6 +779,8 @@ component("ohos_adapter_glue_source") { "ohos_adapter/cpptoc/ark_screen_capture_callback_adapter_cpptoc.h", "ohos_adapter/cpptoc/ark_screen_capture_config_adapter_cpptoc.cpp", "ohos_adapter/cpptoc/ark_screen_capture_config_adapter_cpptoc.h", + "ohos_adapter/cpptoc/ark_sensor_callback_adapter_cpptoc.cpp", + "ohos_adapter/cpptoc/ark_sensor_callback_adapter_cpptoc.h", "ohos_adapter/cpptoc/ark_timezone_event_callback_adapter_cpptoc.cpp", "ohos_adapter/cpptoc/ark_timezone_event_callback_adapter_cpptoc.h", "ohos_adapter/cpptoc/ark_video_capture_info_adapter_cpptoc.cpp", @@ -919,6 +925,8 @@ component("ohos_adapter_glue_source") { "ohos_adapter/ctocpp/ark_running_lock_adapter_ctocpp.h", "ohos_adapter/ctocpp/ark_screen_capture_adapter_ctocpp.cpp", "ohos_adapter/ctocpp/ark_screen_capture_adapter_ctocpp.h", + "ohos_adapter/ctocpp/ark_sensor_adapter_ctocpp.cpp", + "ohos_adapter/ctocpp/ark_sensor_adapter_ctocpp.h", "ohos_adapter/ctocpp/ark_soc_perf_client_adapter_ctocpp.cpp", "ohos_adapter/ctocpp/ark_soc_perf_client_adapter_ctocpp.h", "ohos_adapter/ctocpp/ark_surface_buffer_adapter_ctocpp.cpp", diff --git a/ohos_interface/ohos_glue/base/include/ark_web_bridge_types.h b/ohos_interface/ohos_glue/base/include/ark_web_bridge_types.h index 7507c246d..66af6e053 100644 --- a/ohos_interface/ohos_glue/base/include/ark_web_bridge_types.h +++ b/ohos_interface/ohos_glue/base/include/ark_web_bridge_types.h @@ -243,6 +243,8 @@ enum ArkWebBridgeType { ARK_AAFWK_BROWSER_CLIENT_ADAPTER = 10127, ARK_AAFWK_BROWSER_HOST_ADAPTER = 10128, ARK_AUDIO_OUTPUT_CHANGE_CALLBACK_ADAPTER = 10129, + ARK_SENSOR_ADAPTER = 10130, + ARK_SENSOR_CALLBACK_ADAPTER = 10131, /*Note: Only add an enum value before ARK_WEB_BRIDGE_BUTT*/ ARK_WEB_BRIDGE_BUTT diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_ohos_adapter_helper_wrapper.cpp b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_ohos_adapter_helper_wrapper.cpp index 45706409c..6d4ae1703 100644 --- a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_ohos_adapter_helper_wrapper.cpp +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_ohos_adapter_helper_wrapper.cpp @@ -57,6 +57,7 @@ #include "ohos_adapter/bridge/ark_print_manager_adapter_wrapper.h" #include "ohos_adapter/bridge/ark_running_lock_adapter_wrapper.h" #include "ohos_adapter/bridge/ark_screen_capture_adapter_wrapper.h" +#include "ohos_adapter/bridge/ark_sensor_adapter_wrapper.h" #include "ohos_adapter/bridge/ark_soc_perf_client_adapter_wrapper.h" #include "ohos_adapter/bridge/ark_surface_buffer_adapter_wrapper.h" #include "ohos_adapter/bridge/ark_system_properties_adapter_wrapper.h" @@ -448,4 +449,14 @@ std::unique_ptr ArkOhosAdapterHelperWrapper::Crea return std::make_unique(adapter); } +std::unique_ptr ArkOhosAdapterHelperWrapper::CreateSensorAdapter() +{ + ArkWebRefPtr adapter = ctocpp_->CreateSensorAdapter(); + + if (CHECK_REF_PTR_IS_NULL(adapter)) { + return nullptr; + } + + return std::make_unique(adapter); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_ohos_adapter_helper_wrapper.h b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_ohos_adapter_helper_wrapper.h index 85af3fee6..398b8b285 100644 --- a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_ohos_adapter_helper_wrapper.h +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_ohos_adapter_helper_wrapper.h @@ -108,6 +108,8 @@ public: std::unique_ptr CreateOhosImageDecoderAdapter() override; + std::unique_ptr CreateSensorAdapter() override; + private: ArkWebRefPtr ctocpp_; }; diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_adapter_wrapper.cpp b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_adapter_wrapper.cpp new file mode 100644 index 000000000..0f1765b9b --- /dev/null +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_adapter_wrapper.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 Huawei Device 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 "ohos_adapter/bridge/ark_sensor_adapter_wrapper.h" + +#include "ohos_adapter/bridge/ark_sensor_callback_adapter_impl.h" + +#include "base/bridge/ark_web_bridge_macros.h" + +namespace OHOS::ArkWeb { + +ArkSensorAdapterWrapper::ArkSensorAdapterWrapper(ArkWebRefPtr ref) + : ctocpp_(ref) +{} + +int32_t ArkSensorAdapterWrapper::IsOhosSensorSupported(int32_t sensorTypeId) +{ + return ctocpp_->IsOhosSensorSupported(sensorTypeId); +} + +int32_t ArkSensorAdapterWrapper::GetOhosSensorReportingMode(int32_t sensorTypeId) +{ + return ctocpp_->GetOhosSensorReportingMode(sensorTypeId); +} + +double ArkSensorAdapterWrapper::GetOhosSensorDefaultSupportedFrequency(int32_t sensorTypeId) +{ + return ctocpp_->GetOhosSensorDefaultSupportedFrequency(sensorTypeId); +} + +double ArkSensorAdapterWrapper::GetOhosSensorMinSupportedFrequency(int32_t sensorTypeId) +{ + return ctocpp_->GetOhosSensorMinSupportedFrequency(sensorTypeId); +} + +double ArkSensorAdapterWrapper::GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeId) +{ + return ctocpp_->GetOhosSensorMaxSupportedFrequency(sensorTypeId); +} + +int32_t ArkSensorAdapterWrapper::SubscribeOhosSensor(int32_t sensorTypeId, int64_t samplingInterval) +{ + return ctocpp_->SubscribeOhosSensor(sensorTypeId, samplingInterval); +} + +int32_t ArkSensorAdapterWrapper::UnsubscribeOhosSensor(int32_t sensorTypeId) +{ + return ctocpp_->UnsubscribeOhosSensor(sensorTypeId); +} + +int32_t ArkSensorAdapterWrapper::RegistOhosSensorCallback(int32_t sensorTypeId, + std::shared_ptr callbackAdapter) +{ + if (CHECK_SHARED_PTR_IS_NULL(callbackAdapter)) { + return ctocpp_->RegistOhosSensorCallback(-1, nullptr); + } + + return ctocpp_->RegistOhosSensorCallback(sensorTypeId, + new ArkSensorCallbackAdapterImpl(callbackAdapter)); +} +} // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_adapter_wrapper.h b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_adapter_wrapper.h new file mode 100644 index 000000000..8db4f15eb --- /dev/null +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_adapter_wrapper.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2024 Huawei Device 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. + */ + +#ifndef ARK_SENSOR_ADAPTER_WRAPPER_H +#define ARK_SENSOR_ADAPTER_WRAPPER_H +#pragma once + +#include "sensor_adapter.h" +#include "ohos_adapter/include/ark_sensor_adapter.h" + +namespace OHOS::ArkWeb { + +class ArkSensorAdapterWrapper : public OHOS::NWeb::SensorAdapter { +public: + ArkSensorAdapterWrapper(ArkWebRefPtr); + + int32_t IsOhosSensorSupported(int32_t sensorTypeId) override; + int32_t GetOhosSensorReportingMode(int32_t sensorTypeId) override; + double GetOhosSensorDefaultSupportedFrequency(int32_t sensorTypeId) override; + double GetOhosSensorMinSupportedFrequency(int32_t sensorTypeId) override; + double GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeId) override; + int32_t SubscribeOhosSensor(int32_t sensorTypeId, int64_t samplingInterval) override; + int32_t RegistOhosSensorCallback(int32_t sensorTypeId, + std::shared_ptr callbackAdapter) override; + int32_t UnsubscribeOhosSensor(int32_t sensorTypeId) override; + +private: + ArkWebRefPtr ctocpp_; +}; + +} // namespace OHOS::ArkWeb + +#endif // ARK_SENSOR_ADAPTER_WRAPPER_H diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_callback_adapter_impl.cpp b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_callback_adapter_impl.cpp new file mode 100644 index 000000000..1714e840e --- /dev/null +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_callback_adapter_impl.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2024 Huawei Device 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 "ohos_adapter/bridge/ark_sensor_callback_adapter_impl.h" + +namespace OHOS::ArkWeb { + +ArkSensorCallbackAdapterImpl::ArkSensorCallbackAdapterImpl( + std::shared_ptr ref) + : real_(ref) +{} + +void ArkSensorCallbackAdapterImpl::UpdateOhosSensorData(double timestamp, + double value1, double value2, double value3, double value4) +{ + real_->UpdateOhosSensorData(timestamp, value1, value2, value3, value4); +} +} // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_callback_adapter_impl.h b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_callback_adapter_impl.h new file mode 100644 index 000000000..0bca27d8c --- /dev/null +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_sensor_callback_adapter_impl.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 Huawei Device 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. + */ + +#ifndef ARK_SENSOR_CALLBACK_ADAPTER_IMPL_H +#define ARK_SENSOR_CALLBACK_ADAPTER_IMPL_H +#pragma once + +#include "sensor_adapter.h" +#include "ohos_adapter/include/ark_sensor_adapter.h" + +namespace OHOS::ArkWeb { + +class ArkSensorCallbackAdapterImpl : public ArkSensorCallbackAdapter { +public: + ArkSensorCallbackAdapterImpl(std::shared_ptr); + + void UpdateOhosSensorData(double timestamp, + double value1, double value2, double value3, double value4) override; + +private: + std::shared_ptr real_; + + IMPLEMENT_REFCOUNTING(ArkSensorCallbackAdapterImpl); +}; + +} // namespace OHOS::ArkWeb + +#endif // ARK_SENSOR_CALLBACK_ADAPTER_IMPL_H diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_ohos_adapter_helper_impl.cpp b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_ohos_adapter_helper_impl.cpp index fb8d79ff5..e52ff0a71 100644 --- a/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_ohos_adapter_helper_impl.cpp +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_ohos_adapter_helper_impl.cpp @@ -68,7 +68,7 @@ #include "ohos_adapter/bridge/ark_vsync_adapter_impl.h" #include "ohos_adapter/bridge/ark_web_date_timezone_info_impl.h" #include "ohos_adapter/bridge/ark_window_adapter_impl.h" - +#include "ohos_adapter/bridge/ark_sensor_adapter_impl.h" #include "base/bridge/ark_web_bridge_macros.h" namespace OHOS::ArkWeb { @@ -371,4 +371,10 @@ ArkWebRefPtr ArkOhosAdapterHelperImpl::CreateOhosIma return new ArkOhosImageDecoderAdapterImpl(shared); } +ArkWebRefPtr ArkOhosAdapterHelperImpl::CreateSensorAdapter() +{ + std::unique_ptr adapter = real_.CreateSensorAdapter(); + std::shared_ptr shared = std::move(adapter); + return new ArkSensorAdapterImpl(shared); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_ohos_adapter_helper_impl.h b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_ohos_adapter_helper_impl.h index 2ab29309d..a29b699d2 100644 --- a/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_ohos_adapter_helper_impl.h +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_ohos_adapter_helper_impl.h @@ -108,6 +108,8 @@ public: ArkWebRefPtr CreateOhosImageDecoderAdapter() override; + ArkWebRefPtr CreateSensorAdapter() override; + private: NWeb::OhosAdapterHelper& real_; diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_adapter_impl.cpp b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_adapter_impl.cpp new file mode 100644 index 000000000..472ef827e --- /dev/null +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_adapter_impl.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 Huawei Device 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 "ohos_adapter/bridge/ark_sensor_adapter_impl.h" + +#include "ohos_adapter/bridge/ark_sensor_callback_adapter_wrapper.h" + +#include "base/bridge/ark_web_bridge_macros.h" + +namespace OHOS::ArkWeb { + +ArkSensorAdapterImpl::ArkSensorAdapterImpl(std::shared_ptr ref) + : real_(ref) +{} + +int32_t ArkSensorAdapterImpl::IsOhosSensorSupported(int32_t type) +{ + return real_->IsOhosSensorSupported(type); +} + +int32_t ArkSensorAdapterImpl::GetOhosSensorReportingMode(int32_t type) +{ + return real_->GetOhosSensorReportingMode(type); +} + +double ArkSensorAdapterImpl::GetOhosSensorDefaultSupportedFrequency(int32_t type) +{ + return real_->GetOhosSensorDefaultSupportedFrequency(type); +} + +double ArkSensorAdapterImpl::GetOhosSensorMinSupportedFrequency(int32_t type) +{ + return real_->GetOhosSensorMinSupportedFrequency(type); +} + +double ArkSensorAdapterImpl::GetOhosSensorMaxSupportedFrequency(int32_t type) +{ + return real_->GetOhosSensorMaxSupportedFrequency(type); +} + +int32_t ArkSensorAdapterImpl::SubscribeOhosSensor(int32_t type, int64_t samplingInterval) +{ + return real_->SubscribeOhosSensor(type, samplingInterval); +} + +int32_t ArkSensorAdapterImpl::RegistOhosSensorCallback(int32_t sensorTypeId, + ArkWebRefPtr callbackAdapter) +{ + if (!(CHECK_REF_PTR_IS_NULL(callbackAdapter))) { + return real_->RegistOhosSensorCallback(sensorTypeId, + std::make_shared(callbackAdapter)); + } + return false; +} + +int32_t ArkSensorAdapterImpl::UnsubscribeOhosSensor(int32_t type) +{ + return real_->UnsubscribeOhosSensor(type); +} + +} // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_adapter_impl.h b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_adapter_impl.h new file mode 100644 index 000000000..f11880317 --- /dev/null +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_adapter_impl.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2024 Huawei Device 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. + */ + +#ifndef ARK_SENSOR_ADAPTER_IMPL_H +#define ARK_SENSOR_ADAPTER_IMPL_H +#pragma once + +#include "sensor_adapter.h" +#include "ohos_adapter/include/ark_sensor_adapter.h" + +namespace OHOS::ArkWeb { + +class ArkSensorAdapterImpl : public ArkSensorAdapter { +public: + ArkSensorAdapterImpl(std::shared_ptr); + + int32_t IsOhosSensorSupported(int32_t sensorTypeId) override; + + int32_t GetOhosSensorReportingMode(int32_t type) override; + + double GetOhosSensorDefaultSupportedFrequency(int32_t sensorTypeId) override; + + double GetOhosSensorMinSupportedFrequency(int32_t sensorTypeId) override; + + double GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeId) override; + + int32_t SubscribeOhosSensor(int32_t sensorTypeId, int64_t samplingInterval) override; + + int32_t RegistOhosSensorCallback(int32_t sensorTypeId, + ArkWebRefPtr callbackAdapter) override; + + int32_t UnsubscribeOhosSensor(int32_t sensorTypeId) override; + +private: + std::shared_ptr real_; + + IMPLEMENT_REFCOUNTING(ArkSensorAdapterImpl); +}; + +} // namespace OHOS::ArkWeb + +#endif // ARK_SENSOR_ADAPTER_IMPL_H diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_callback_adapter_wrapper.cpp b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_callback_adapter_wrapper.cpp new file mode 100644 index 000000000..4d917c672 --- /dev/null +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_callback_adapter_wrapper.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 Huawei Device 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 "ohos_adapter/bridge/ark_sensor_callback_adapter_wrapper.h" + +#include "base/bridge/ark_web_bridge_macros.h" + +namespace OHOS::ArkWeb { +ArkSensorCallbackAdapterWrapper::ArkSensorCallbackAdapterWrapper( + ArkWebRefPtr ref) + : ctocpp_(ref) +{} + +void ArkSensorCallbackAdapterWrapper::UpdateOhosSensorData(double timestamp, + double value1, double value2, double value3, double value4) +{ + ctocpp_->UpdateOhosSensorData(timestamp, value1, value2, value3, value4); +} +} // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_callback_adapter_wrapper.h b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_callback_adapter_wrapper.h new file mode 100644 index 000000000..a30160c2a --- /dev/null +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_sensor_callback_adapter_wrapper.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2024 Huawei Device 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. + */ + +#ifndef ARK_SENSOR_CALLBACK_ADAPTER_WRAPPER_H +#define ARK_SENSOR_CALLBACK_ADAPTER_WRAPPER_H +#pragma once + +#include "sensor_adapter.h" +#include "ohos_adapter/include/ark_sensor_adapter.h" + +namespace OHOS::ArkWeb { + +class ArkSensorCallbackAdapterWrapper : public OHOS::NWeb::SensorCallbackAdapter { +public: + ArkSensorCallbackAdapterWrapper(ArkWebRefPtr); + + void UpdateOhosSensorData(double timestamp, + double value1, double value2, double value3, double value4) override; + +private: + ArkWebRefPtr ctocpp_; +}; + +} // namespace OHOS::ArkWeb + +#endif // ARK_SENSOR_CALLBACK_ADAPTER_WRAPPER_H diff --git a/ohos_interface/ohos_glue/ohos_adapter/include/ark_ohos_adapter_helper.h b/ohos_interface/ohos_glue/ohos_adapter/include/ark_ohos_adapter_helper.h index edd90eff7..61b9e1343 100644 --- a/ohos_interface/ohos_glue/ohos_adapter/include/ark_ohos_adapter_helper.h +++ b/ohos_interface/ohos_glue/ohos_adapter/include/ark_ohos_adapter_helper.h @@ -53,6 +53,7 @@ #include "ohos_adapter/include/ark_screen_capture_adapter.h" #include "ohos_adapter/include/ark_soc_perf_client_adapter.h" #include "ohos_adapter/include/ark_system_properties_adapter.h" +#include "ohos_adapter/include/ark_sensor_adapter.h" namespace OHOS::ArkWeb { @@ -184,6 +185,9 @@ public: /*--ark web()--*/ virtual ArkWebRefPtr CreateOhosImageDecoderAdapter() = 0; + + /*--ark web()--*/ + virtual ArkWebRefPtr CreateSensorAdapter() = 0; }; } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_adapter/include/ark_sensor_adapter.h b/ohos_interface/ohos_glue/ohos_adapter/include/ark_sensor_adapter.h new file mode 100644 index 000000000..32a83406e --- /dev/null +++ b/ohos_interface/ohos_glue/ohos_adapter/include/ark_sensor_adapter.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2024 Huawei Device 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. + */ + +#ifndef ARK_SENSOR_ADAPTER_H +#define ARK_SENSOR_ADAPTER_H +#pragma once + +#include "base/include/ark_web_base_ref_counted.h" +#include "base/include/ark_web_types.h" + +namespace OHOS::ArkWeb { + +/*--ark web(source=webcore)--*/ +class ArkSensorCallbackAdapter : public virtual ArkWebBaseRefCounted { +public: + /*--ark web()--*/ + virtual void UpdateOhosSensorData(double timestamp, + double value1, double value2, double value3, double value4) = 0; +}; + +/*--ark web(source=webview)--*/ +class ArkSensorAdapter : public virtual ArkWebBaseRefCounted { +public: + /*--ark web()--*/ + virtual int32_t IsOhosSensorSupported(int32_t sensorTypeId) = 0; + + /*--ark web()--*/ + virtual int32_t GetOhosSensorReportingMode(int32_t type) = 0; + + /*--ark web()--*/ + virtual double GetOhosSensorDefaultSupportedFrequency(int32_t sensorTypeId) = 0; + + /*--ark web()--*/ + virtual double GetOhosSensorMinSupportedFrequency(int32_t sensorTypeId) = 0; + + /*--ark web()--*/ + virtual double GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeId) = 0; + + /*--ark web()--*/ + virtual int32_t SubscribeOhosSensor(int32_t sensorTypeId, int64_t samplingInterval) = 0; + + /*--ark web()--*/ + virtual int32_t RegistOhosSensorCallback(int32_t sensorTypeId, + ArkWebRefPtr callbackAdapter) = 0; + + /*--ark web()--*/ + virtual int32_t UnsubscribeOhosSensor(int32_t sensorTypeId) = 0; +}; + +} // namespace OHOS::ArkWeb + +#endif // ARK_SENSOR_ADAPTER_H -- Gitee From cf16a5094a26a17e79adfad3c05dc25341cbf9b6 Mon Sep 17 00:00:00 2001 From: ZhaoPengfei Date: Thu, 4 Jul 2024 15:33:59 +0800 Subject: [PATCH 10/32] =?UTF-8?q?=E5=8F=96=E6=B6=88webview=E9=BB=98?= =?UTF-8?q?=E8=AE=A4120fps=E5=B8=A7=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ZhaoPengfei --- ohos_adapter/graphic_adapter/src/vsync_adapter_impl.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ohos_adapter/graphic_adapter/src/vsync_adapter_impl.cpp b/ohos_adapter/graphic_adapter/src/vsync_adapter_impl.cpp index 4b79df9c1..0a7c3f8ed 100644 --- a/ohos_adapter/graphic_adapter/src/vsync_adapter_impl.cpp +++ b/ohos_adapter/graphic_adapter/src/vsync_adapter_impl.cpp @@ -170,12 +170,11 @@ void VSyncAdapterImpl::SetFrameRateLinkerEnable(bool enabled) return; } - Rosen::FrameRateRange range = {0, RANGE_MAX_REFRESHRATE, 120, WEBVIEW_FRAME_RATE_TYPE}; if (frameRateLinker_) { if (!enabled) { - range = {0, RANGE_MAX_REFRESHRATE, 0, WEBVIEW_FRAME_RATE_TYPE}; + Rosen::FrameRateRange range = {0, RANGE_MAX_REFRESHRATE, 0, WEBVIEW_FRAME_RATE_TYPE}; + frameRateLinker_->UpdateFrameRateRangeImme(range); } - frameRateLinker_->UpdateFrameRateRangeImme(range); frameRateLinker_->SetEnable(enabled); frameRateLinkerEnable_ = enabled; } -- Gitee From cc50784be17cff7c63013b9c8a2b4f513146aaa6 Mon Sep 17 00:00:00 2001 From: zhangyanchuan Date: Tue, 2 Jul 2024 20:47:35 +0800 Subject: [PATCH 11/32] =?UTF-8?q?=E8=A7=86=E9=A2=91=E6=89=98=E7=AE=A1?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=BF=9BBFCache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangyanchuan --- .../webviewcontroller/napi_native_media_player.cpp | 10 ++++++---- .../webviewcontroller/native_media_player_impl.cpp | 2 +- .../include/ohos_nweb/nweb_native_media_player.h | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/interfaces/kits/napi/webviewcontroller/napi_native_media_player.cpp b/interfaces/kits/napi/webviewcontroller/napi_native_media_player.cpp index a49293da2..9251d7932 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_native_media_player.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_native_media_player.cpp @@ -40,6 +40,8 @@ void NapiNativeMediaPlayerHandler::Init(napi_env env, napi_value value) NAPI_CALL_RETURN_VOID(env, ExportEnumReadyState(env, &value)); + NAPI_CALL_RETURN_VOID(env, ExportEnumSuspendType(env, &value)); + NAPI_CALL_RETURN_VOID(env, ExportEnumNetworkState(env, &value)); NAPI_CALL_RETURN_VOID(env, ExportEnumPlaybackStatus(env, &value)); @@ -172,16 +174,16 @@ napi_status NapiNativeMediaPlayerHandler::ExportEnumReadyState(napi_env env, nap napi_status NapiNativeMediaPlayerHandler::ExportEnumSuspendType(napi_env env, napi_value* value) { - WVLOG_D("begin to export enum suspend type state"); + WVLOG_D("begin to export enum suspend type"); const std::string NPI_SUSPEND_TYPE_ENUM_NAME = "SuspendType"; napi_property_descriptor properties[] = { - DECLARE_NAPI_STATIC_PROPERTY( - "EnterBFCache", NapiParseUtils::ToInt32Value(env, static_cast(SuspendType::EnterBFCache))), + DECLARE_NAPI_STATIC_PROPERTY("EnterBackForwardCache", + NapiParseUtils::ToInt32Value(env, static_cast(SuspendType::EnterBackForwardCache))), DECLARE_NAPI_STATIC_PROPERTY( "EnterBackground", NapiParseUtils::ToInt32Value(env, static_cast(SuspendType::EnterBackground))), DECLARE_NAPI_STATIC_PROPERTY( - "PausedOverTime", NapiParseUtils::ToInt32Value(env, static_cast(SuspendType::PausedOverTime))), + "AutoCleanup", NapiParseUtils::ToInt32Value(env, static_cast(SuspendType::AutoCleanup))), }; napi_value enumValue = nullptr; diff --git a/interfaces/kits/napi/webviewcontroller/native_media_player_impl.cpp b/interfaces/kits/napi/webviewcontroller/native_media_player_impl.cpp index ae5aa70a4..a0bbf6d43 100644 --- a/interfaces/kits/napi/webviewcontroller/native_media_player_impl.cpp +++ b/interfaces/kits/napi/webviewcontroller/native_media_player_impl.cpp @@ -163,7 +163,7 @@ void NWebNativeMediaPlayerBridgeImpl::SuspendMediaPlayer(SuspendType type) napi_value argv[INTEGER_ONE] = { nullptr }; NAPI_CALL_RETURN_VOID(env_, napi_create_int32(env_, static_cast(type), &argv[INTEGER_ZERO])); - NAPI_CALL_RETURN_VOID(env_, napi_call_function(env_, value_, callback, INTEGER_ZERO, nullptr, nullptr)); + NAPI_CALL_RETURN_VOID(env_, napi_call_function(env_, value_, callback, INTEGER_ONE, argv, nullptr)); } NapiNativeMediaPlayerHandlerImpl::NapiNativeMediaPlayerHandlerImpl( diff --git a/ohos_interface/include/ohos_nweb/nweb_native_media_player.h b/ohos_interface/include/ohos_nweb/nweb_native_media_player.h index 4aa810c1b..dffc7edad 100644 --- a/ohos_interface/include/ohos_nweb/nweb_native_media_player.h +++ b/ohos_interface/include/ohos_nweb/nweb_native_media_player.h @@ -31,7 +31,7 @@ enum class MediaError { NETWORK_ERROR = 1, FORMAT_ERROR, DECODE_ERROR }; enum class ReadyState { HAVE_NOTHING = 0, HAVE_METADATA, HAVE_CURRENT_DATA, HAVE_FUTURE_DATA, HAVE_ENOUGH_DATA }; -enum class SuspendType { EnterBFCache = 0, EnterBackground, PausedOverTime }; +enum class SuspendType { EnterBackForwardCache = 0, EnterBackground, AutoCleanup }; enum class NetworkState { EMPTY = 0, IDLE, LOADING, NETWORK_ERROR }; -- Gitee From 07123b2e07797a446dd59903f63bbec91eaeea83 Mon Sep 17 00:00:00 2001 From: lie Date: Thu, 4 Jul 2024 20:01:49 +0800 Subject: [PATCH 12/32] add bfcache status for samelayer Signed-off-by: lie Change-Id: I53e7174efa8622f9fe2ee197e8dbbbb3e89529fb --- ohos_interface/include/ohos_nweb/nweb_handler.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ohos_interface/include/ohos_nweb/nweb_handler.h b/ohos_interface/include/ohos_nweb/nweb_handler.h index f27bea9b1..459752ea2 100644 --- a/ohos_interface/include/ohos_nweb/nweb_handler.h +++ b/ohos_interface/include/ohos_nweb/nweb_handler.h @@ -213,6 +213,8 @@ enum class NativeEmbedStatus { CREATE, UPDATE, DESTROY, + ENTER_BFCACHE, + LEAVE_BFCACHE, }; enum class NWebFocusSource { -- Gitee From b314ef36ee11ee3c145bc673208e4e31dde9dbc5 Mon Sep 17 00:00:00 2001 From: kirby Date: Fri, 5 Jul 2024 10:35:52 +0800 Subject: [PATCH 13/32] ffi fix codecheck Signed-off-by: kirby --- .../webview_javascript_result_callback.h | 2 +- .../webview_javascript_result_callback.cpp | 64 +++++++++---------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/interfaces/kits/cj/include/webview_javascript_result_callback.h b/interfaces/kits/cj/include/webview_javascript_result_callback.h index bc4c56379..1a313945d 100644 --- a/interfaces/kits/cj/include/webview_javascript_result_callback.h +++ b/interfaces/kits/cj/include/webview_javascript_result_callback.h @@ -170,7 +170,7 @@ private: bool ConstructArgv(void* ashmem, std::vector> args, std::vector& argv, std::shared_ptr jsObj, int32_t routingId); - char* FlowbufStrAtIndex(void* mem, int flowbuf_index, int* arg_index, int* str_len); + char* FlowbufStrAtIndex(void* mem, int flowbufIndex, int* argIndex, int* strLen); std::shared_ptr GetJavaScriptResultSelfHelper(std::shared_ptr jsObj, const std::string& method, int32_t routingId, std::vector argv); diff --git a/interfaces/kits/cj/src/webview_javascript_result_callback.cpp b/interfaces/kits/cj/src/webview_javascript_result_callback.cpp index f3ca8d1d0..358cbc759 100644 --- a/interfaces/kits/cj/src/webview_javascript_result_callback.cpp +++ b/interfaces/kits/cj/src/webview_javascript_result_callback.cpp @@ -205,37 +205,37 @@ std::shared_ptr WebviewJavaScriptResultCallBackImpl::GetJavaScriptRes } char* WebviewJavaScriptResultCallBackImpl::FlowbufStrAtIndex( - void* mem, int flowbuf_index, int* arg_index, int* str_len) + void* mem, int flowbufIndex, int* argIndex, int* strLen) { int* header = static_cast(mem); // Cast the memory block to int* for easier access int offset = 0; - if (arg_index == nullptr) { + if (argIndex == nullptr) { return nullptr; } - if (flowbuf_index >= MAX_ENTRIES) { - *arg_index = -1; + if (flowbufIndex >= MAX_ENTRIES) { + *argIndex = -1; return nullptr; } - int* entry = header + (flowbuf_index * INDEX_SIZE); + int* entry = header + (flowbufIndex * INDEX_SIZE); if (entry == nullptr) { return nullptr; } if (*(entry + 1) == 0) { // Check if length is 0, indicating unused entry - *arg_index = -1; + *argIndex = -1; return nullptr; } int i = 0; - for (i = 0; i < flowbuf_index; i++) { + for (i = 0; i < flowbufIndex; i++) { offset += *(header + (i * INDEX_SIZE) + 1); } - if (str_len == nullptr) { + if (strLen == nullptr) { return nullptr; } - *str_len = *(header + (i * INDEX_SIZE) + 1) - 1; + *strLen = *(header + (i * INDEX_SIZE) + 1) - 1; - *arg_index = *entry; + *argIndex = *entry; char* dataSegment = static_cast(mem) + HEADER_SIZE; char* currentString = dataSegment + offset; @@ -248,35 +248,35 @@ bool WebviewJavaScriptResultCallBackImpl::ConstructArgv(void* ashmem, std::shared_ptr jsObj, int32_t routingId) { - int arg_index = -1; - int curr_index = 0; - int flowbuf_index = 0; - int str_len = 0; - char* flowbuf_str = FlowbufStrAtIndex(ashmem, flowbuf_index, &arg_index, &str_len); - flowbuf_index++; - while (arg_index == curr_index) { - argv.push_back(std::string(flowbuf_str)); - curr_index ++; - flowbuf_str = FlowbufStrAtIndex(ashmem, flowbuf_index, &arg_index, &str_len); - flowbuf_index++; + int argIndex = -1; + int currIndex = 0; + int flowbufIndex = 0; + int strLen = 0; + char* flowbufStr = FlowbufStrAtIndex(ashmem, flowbufIndex, &argIndex, &strLen); + flowbufIndex++; + while (argIndex == currIndex) { + argv.push_back(std::string(flowbufStr)); + currIndex ++; + flowbufStr = FlowbufStrAtIndex(ashmem, flowbufIndex, &argIndex, &strLen); + flowbufIndex++; } for (std::shared_ptr input : args) { - while (arg_index == curr_index) { - argv.push_back(std::string(flowbuf_str)); - curr_index ++; - flowbuf_str = FlowbufStrAtIndex(ashmem, flowbuf_index, &arg_index, &str_len); - flowbuf_index++; + while (argIndex == currIndex) { + argv.push_back(std::string(flowbufStr)); + currIndex ++; + flowbufStr = FlowbufStrAtIndex(ashmem, flowbufIndex, &argIndex, &strLen); + flowbufIndex++; } argv.push_back(input->GetString()); - curr_index++; + currIndex++; } - while (arg_index == curr_index) { - argv.push_back(std::string(flowbuf_str)); - curr_index ++; - flowbuf_str = FlowbufStrAtIndex(ashmem, flowbuf_index, &arg_index, &str_len); - flowbuf_index++; + while (argIndex == currIndex) { + argv.push_back(std::string(flowbufStr)); + currIndex ++; + flowbufStr = FlowbufStrAtIndex(ashmem, flowbufIndex, &argIndex, &strLen); + flowbufIndex++; } return true; } -- Gitee From 96c0160fce97f2048ee974fb40efb79fa82bcad9 Mon Sep 17 00:00:00 2001 From: hwchenhongyu Date: Fri, 5 Jul 2024 02:29:37 +0000 Subject: [PATCH 14/32] change tags to uint Signed-off-by: hwchenhongyu --- ohos_adapter/hiviewdfx_adapter/src/hitrace_adapter_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ohos_adapter/hiviewdfx_adapter/src/hitrace_adapter_impl.cpp b/ohos_adapter/hiviewdfx_adapter/src/hitrace_adapter_impl.cpp index 27ef05c3c..8b9a6b36f 100644 --- a/ohos_adapter/hiviewdfx_adapter/src/hitrace_adapter_impl.cpp +++ b/ohos_adapter/hiviewdfx_adapter/src/hitrace_adapter_impl.cpp @@ -62,7 +62,7 @@ bool HiTraceAdapterImpl::IsHiTraceEnable() static CachedHandle g_Handle = CachedParameterCreate("debug.hitrace.tags.enableflags", "0"); int changed = 0; const char *enable = CachedParameterGetChanged(g_Handle, &changed); - auto tags = ConvertToInt(enable, 0); + uint64_t tags = static_cast(ConvertToInt(enable, 0)); firstAceEnable_ = tags & HITRACE_TAG_ACE; return (tags & HITRACE_TAG_NWEB); } -- Gitee From feabfcc6f0feb0d3c572759ed10f4436d9f9aef5 Mon Sep 17 00:00:00 2001 From: zhengenhao0 Date: Fri, 5 Jul 2024 14:17:42 +0800 Subject: [PATCH 15/32] update NWeb.hap in master to 0705 Signed-off-by: zhengenhao0 --- ohos_nweb/prebuilts/arm/NWeb.hap | 4 ++-- ohos_nweb/prebuilts/arm64/NWeb.hap | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ohos_nweb/prebuilts/arm/NWeb.hap b/ohos_nweb/prebuilts/arm/NWeb.hap index ac7f3c56c..2916f731b 100644 --- a/ohos_nweb/prebuilts/arm/NWeb.hap +++ b/ohos_nweb/prebuilts/arm/NWeb.hap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:fd59e132e126e3df85c2b1f6123c46e32c8d60f4093451172fb9d19cef01c7b5 -size 79363768 +oid sha256:98f54394d9240f9a37c4fef8f799fe876aac74a86d70a74eba262ca59931dbda +size 79497971 diff --git a/ohos_nweb/prebuilts/arm64/NWeb.hap b/ohos_nweb/prebuilts/arm64/NWeb.hap index 3ea02155b..da5358b4d 100644 --- a/ohos_nweb/prebuilts/arm64/NWeb.hap +++ b/ohos_nweb/prebuilts/arm64/NWeb.hap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e4ed361a5946db6afb8e9d6b894c7e1e368d483b5f7b083da80469e668788589 -size 90156696 +oid sha256:bb1c4bf27de592f9096fcc03d95ac5213ab388d144783ee326df17ac586f59bb +size 90255633 -- Gitee From 2eb396e916e9b5b368f8f65dbf5bb1fd2edcca07 Mon Sep 17 00:00:00 2001 From: gaojianhao1 Date: Fri, 5 Jul 2024 15:50:20 +0800 Subject: [PATCH 16/32] Add interface enableBackForwardCache and setBackForwardCacheOptions. Signed-off-by: gaojianhao1 --- .../back_forward_cache_options.cpp | 14 +++- .../back_forward_cache_options.h | 10 ++- .../napi_back_forward_cache_options.cpp | 10 +-- .../napi_back_forward_cache_options.h | 2 +- .../napi_webview_controller.cpp | 73 +++++++------------ .../bridge/webcore/ark_web_engine_impl.h | 2 +- ohos_nweb/include/nweb_helper.h | 2 + ohos_nweb/src/nweb_helper.cpp | 12 ++- 8 files changed, 60 insertions(+), 65 deletions(-) diff --git a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp index d8b61a597..7945a393e 100644 --- a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp +++ b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp @@ -38,6 +38,16 @@ int32_t BackForwardCacheOptions::GetTimeToLive() return timeToLive_; } +static int32_t BackForwardCacheOptions::GetDefaultSize() +{ + return default_size_; +} + +static int32_t BackForwardCacheOptions::GetDefaultTimeToLive() +{ + return default_time_to_live_; +} + BackForwardCacheSupportFeatures::BackForwardCacheSupportFeatures() { WVLOG_D("Created a BackForwardCacheSupportFeatures class."); @@ -48,9 +58,9 @@ bool BackForwardCacheSupportFeatures::IsEnableNativeEmbed() return nativeEmbed_; } -bool BackForwardCacheSupportFeatures::IsEnableMediaIntercept() +bool BackForwardCacheSupportFeatures::isEnableMediaTakeOver() { - return mediaIntercept_; + return mediaTakeOver_; } } \ No newline at end of file diff --git a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h index e9eca1d8f..0c54d3215 100644 --- a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h +++ b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h @@ -30,21 +30,25 @@ public: BackForwardCacheOptions(); int32_t GetSize(); int32_t GetTimeToLive(); + static int32_t GetDefaultSize(); + static int32_t GetDefaultTimeToLive(); private: int32_t size_ = 1; int32_t timeToLive_ = 600; + int32_t default_size_ = 1; + int32_t default_time_to_live_ = 600; }; class BackForwardCacheSupportFeatures { public: BackForwardCacheSupportFeatures(); bool IsEnableNativeEmbed(); - bool IsEnableMediaIntercept(); + bool isEnableMediaTakeOver(); private: - bool nativeEmbed_ = true; - bool mediaIntercept_ = true; + bool nativeEmbed_ = false; + bool mediaTakeOver_ = false; }; } diff --git a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp index 2751af4c9..c43df7d4e 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp @@ -80,25 +80,25 @@ napi_value NapiBackForwardCacheSupportFeatures::JS_Constructor(napi_env env, nap napi_value NapiBackForwardCacheOptions::JS_GetSize(napi_env env, napi_callback_info info) { - WVLOG_D("NapiBackForwardCacheOptions::JS_GetSize"); + WVLOG_D("NapiBackForwardCacheOptions::JS_GetSize."); return nullptr; } napi_value NapiBackForwardCacheOptions::JS_GetTimeToLive(napi_env env, napi_callback_info info) { - WVLOG_D("NapiBackForwardCacheOptions::JS_GetTimeToLive"); + WVLOG_D("NapiBackForwardCacheOptions::JS_GetTimeToLive."); return nullptr; } napi_value NapiBackForwardCacheSupportFeatures::JS_IsEnableNativeEmbed(napi_env env, napi_callback_info info) { - WVLOG_D("NapiBackForwardCacheSupportFeatures::JS_IsEnableNativeEmbed"); + WVLOG_D("NapiBackForwardCacheSupportFeatures::JS_IsEnableNativeEmbed."); return nullptr; } -napi_value NapiBackForwardCacheSupportFeatures::JS_IsEnableMediaIntercept(napi_env env, napi_callback_info info) +napi_value NapiBackForwardCacheSupportFeatures::JS_IsEnableMediaTakeOver(napi_env env, napi_callback_info info) { - WVLOG_D("NapiBackForwardCacheSupportFeatures::JS_IsEnableMediaIntercept"); + WVLOG_D("NapiBackForwardCacheSupportFeatures::JS_IsEnableMediaTakeOver."); return nullptr; } diff --git a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h index 41e3d497b..68e3eaa59 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h +++ b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h @@ -45,7 +45,7 @@ public: static napi_value Init(napi_env env, napi_value exports); static napi_value JS_Constructor(napi_env env, napi_callback_info info); static napi_value JS_IsEnableNativeEmbed(napi_env env, napi_callback_info info); - static napi_value JS_IsEnableMediaIntercept(napi_env env, napi_callback_info info); + static napi_value JS_IsEnableMediaTakeOver(napi_env env, napi_callback_info info); }; } diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp index e676a922f..66a2ee972 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp @@ -5494,8 +5494,8 @@ napi_value NapiWebviewController::EnableBackForwardCache(napi_env env, napi_call napi_get_undefined(env, &result); napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != INTEGER_ONE) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); + NWebHelper::Instance().EnableBackForwardCache(false, false); + NAPI_CALL(env, napi_get_undefined(env, &result)); return result; } @@ -5503,26 +5503,16 @@ napi_value NapiWebviewController::EnableBackForwardCache(napi_env env, napi_call bool mediaTakeOver = false; napi_value embedObj = nullptr; napi_value mediaObj = nullptr; - if (napi_get_named_property(env, argv[INTEGER_ZERO], "nativeEmbed", &embedObj) != napi_ok) { - WVLOG_E("Failed to get BackForwardCacheOptions nativeEmbed value."); - return result; - } - - if (!NapiParseUtils::ParseBoolean(env, embedObj, nativeEmbed)) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "nativeEmbed", "bool")); - return result; + if (napi_get_named_property(env, argv[INTEGER_ZERO], "nativeEmbed", &embedObj) == napi_ok) { + if (!NapiParseUtils::ParseBoolean(env, embedObj, nativeEmbed)) { + nativeEmbed = false; + } } if (napi_get_named_property(env, argv[INTEGER_ZERO], "mediaTakeOver", &mediaObj) != napi_ok) { - WVLOG_E("Failed to get BackForwardCacheOptions mediaTakeOver value."); - return result; - } - - if (!NapiParseUtils::ParseBoolean(env, mediaObj, mediaTakeOver)) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "mediaTakeOver", "bool")); - return result; + if (!NapiParseUtils::ParseBoolean(env, mediaObj, mediaTakeOver)) { + mediaTakeOver = false; + } } NWebHelper::Instance().EnableBackForwardCache(nativeEmbed, mediaTakeOver); @@ -5538,9 +5528,17 @@ napi_value NapiWebviewController::SetBackForwardCacheOptions(napi_env env, napi_ napi_value argv[INTEGER_ONE] = { 0 }; napi_get_undefined(env, &result); napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + WebviewController* webviewController = GetWebviewController(env, info); + if (!webviewController) { + WVLOG_E("InjectOfflineResource: init webview controller error."); + BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); + return result; + } + if (argc != INTEGER_ONE) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); + webviewController->SetBackForwardCacheOptions( + BackForwardCacheOptions::GetDefaultSize(), BackForwardCacheOptions::GetDefaultTimeToLive()); + NAPI_CALL(env, napi_get_undefined(env, &result)); return result; } @@ -5548,33 +5546,16 @@ napi_value NapiWebviewController::SetBackForwardCacheOptions(napi_env env, napi_ int32_t timeToLive = 600; napi_value sizeObj = nullptr; napi_value timeToLiveObj = nullptr; - if (napi_get_named_property(env, argv[INTEGER_ZERO], "size", &sizeObj) != napi_ok) { - WVLOG_E("Failed to get BackForwardCacheOptions size value."); - return result; - } - - if (!NapiParseUtils::ParseInt32(env, sizeObj, size)) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "size", "int")); - return result; + if (napi_get_named_property(env, argv[INTEGER_ZERO], "size", &sizeObj) == napi_ok) { + if (!NapiParseUtils::ParseInt32(env, sizeObj, size)) { + size = BackForwardCacheOptions::GetDefaultSize(); + } } - if (napi_get_named_property(env, argv[INTEGER_ZERO], "timeToLive", &timeToLiveObj) != napi_ok) { - WVLOG_E("Failed to get BackForwardCacheOptions timeToLive value."); - return result; - } - - if (!NapiParseUtils::ParseInt32(env, timeToLiveObj, timeToLive)) { - BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, - NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "timeToLive", "int")); - return result; - } - - WebviewController* webviewController = GetWebviewController(env, info); - if (!webviewController) { - WVLOG_E("InjectOfflineResource: init webview controller error."); - BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); - return result; + if (napi_get_named_property(env, argv[INTEGER_ZERO], "timeToLive", &timeToLiveObj) == napi_ok) { + if (!NapiParseUtils::ParseInt32(env, timeToLiveObj, timeToLive)) { + timeToLive = BackForwardCacheOptions::GetDefaultTimeToLive(); + } } webviewController->SetBackForwardCacheOptions(size, timeToLive); diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h index 63243ef51..7271bdbb3 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h @@ -79,7 +79,7 @@ public: ArkWebRefPtr GetAdsBlockManager() override; - void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) override; + void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaTakeOver) override; private: std::shared_ptr nweb_engine_; diff --git a/ohos_nweb/include/nweb_helper.h b/ohos_nweb/include/nweb_helper.h index b65860d10..5654b55d0 100644 --- a/ohos_nweb/include/nweb_helper.h +++ b/ohos_nweb/include/nweb_helper.h @@ -86,6 +86,8 @@ public: void EnableWholeWebPageDrawing(); std::shared_ptr GetAdsBlockManager(); + void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaTakeOver); + private: NWebHelper() = default; bool LoadLib(bool from_ark); diff --git a/ohos_nweb/src/nweb_helper.cpp b/ohos_nweb/src/nweb_helper.cpp index d51b45202..ef78f8f2b 100644 --- a/ohos_nweb/src/nweb_helper.cpp +++ b/ohos_nweb/src/nweb_helper.cpp @@ -811,11 +811,9 @@ bool NWebHelper::InitAndRun(bool from_ark) std::string simplifiedLocale = baseLanguage + "-" + systemRegion; initArgs->AddArg(std::string("--lang=").append(simplifiedLocale)); - if (backForwardCacheCmdLine_.size() != 0) { - for (auto backForwardCacheCmdLine : backForwardCacheCmdLine_) { - initArgs->AddArg(backForwardCacheCmdLine); - WVLOG_I("Add command line when init web engine: %{public}s", backForwardCacheCmdLine.c_str()); - } + for (auto backForwardCacheCmdLine : backForwardCacheCmdLine_) { + initArgs->AddArg(backForwardCacheCmdLine); + WVLOG_I("Add command line when init web engine: %{public}s", backForwardCacheCmdLine.c_str()); } nwebEngine_->InitializeWebEngine(initArgs); @@ -1084,7 +1082,7 @@ void NWebHelper::ClearHostIP(const std::string& hostName) nwebEngine_->ClearHostIP(hostName); } -void NWebHelper::EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaIntercept) +void NWebHelper::EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaTakeOver) { this->backForwardCacheCmdLine_.emplace_back("--enable-bfcache"); if (enableNativeEmbed) { @@ -1092,7 +1090,7 @@ void NWebHelper::EnableBackForwardCache(bool enableNativeEmbed, bool enableMedia } if (enableNativeEmbed) { - this->backForwardCacheCmdLine_.emplace_back("--enable-cache-media-intercept"); + this->backForwardCacheCmdLine_.emplace_back("--enable-cache-media-take-over"); } } -- Gitee From b11d75f6166ff9a9d310bb6ebbd875124c896a53 Mon Sep 17 00:00:00 2001 From: gaojianhao1 Date: Fri, 5 Jul 2024 17:18:23 +0800 Subject: [PATCH 17/32] Add interface. Signed-off-by: gaojianhao1 --- .../common/napi_webview_native_module.cpp | 2 +- .../back_forward_cache_options.cpp | 18 ++---- .../back_forward_cache_options.h | 13 ++-- .../napi_back_forward_cache_options.cpp | 62 +++++-------------- .../napi_back_forward_cache_options.h | 12 ++-- .../napi_webview_controller.cpp | 10 +-- .../bridge/webcore/ark_web_engine_impl.h | 2 - .../nweb_helper_test/nweb_helper_test.cpp | 3 +- 8 files changed, 38 insertions(+), 84 deletions(-) diff --git a/interfaces/kits/napi/common/napi_webview_native_module.cpp b/interfaces/kits/napi/common/napi_webview_native_module.cpp index 00ad3d24a..404784205 100644 --- a/interfaces/kits/napi/common/napi_webview_native_module.cpp +++ b/interfaces/kits/napi/common/napi_webview_native_module.cpp @@ -53,7 +53,7 @@ static napi_value WebViewExport(napi_env env, napi_value exports) WebFunctionInit(env, exports); NapiNativeMediaPlayerHandler::Init(env, exports); NapiBackForwardCacheOptions::Init(env, exports); - NapiBackForwardCacheSupportFeatures::Init(env, exports); + NapiBackForwardCacheSupportedFeatures::Init(env, exports); return exports; } EXTERN_C_END diff --git a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp index 7945a393e..f6fcbb15d 100644 --- a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp +++ b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.cpp @@ -38,27 +38,17 @@ int32_t BackForwardCacheOptions::GetTimeToLive() return timeToLive_; } -static int32_t BackForwardCacheOptions::GetDefaultSize() +BackForwardCacheSupportedFeatures::BackForwardCacheSupportedFeatures() { - return default_size_; + WVLOG_D("Created a BackForwardCacheSupportedFeatures class."); } -static int32_t BackForwardCacheOptions::GetDefaultTimeToLive() -{ - return default_time_to_live_; -} - -BackForwardCacheSupportFeatures::BackForwardCacheSupportFeatures() -{ - WVLOG_D("Created a BackForwardCacheSupportFeatures class."); -} - -bool BackForwardCacheSupportFeatures::IsEnableNativeEmbed() +bool BackForwardCacheSupportedFeatures::IsEnableNativeEmbed() { return nativeEmbed_; } -bool BackForwardCacheSupportFeatures::isEnableMediaTakeOver() +bool BackForwardCacheSupportedFeatures::IsEnableMediaTakeOver() { return mediaTakeOver_; } diff --git a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h index 0c54d3215..e11da84d4 100644 --- a/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h +++ b/interfaces/kits/napi/webviewcontroller/back_forward_cache_options.h @@ -23,6 +23,9 @@ #include "napi/native_common.h" #include "napi/native_node_api.h" +#define BFCACHE_DEFAULT_SIZE 1 +#define BFCACHE_DEFAULT_TIMETOLIVE 600 + namespace OHOS { namespace NWeb { class BackForwardCacheOptions { @@ -30,21 +33,17 @@ public: BackForwardCacheOptions(); int32_t GetSize(); int32_t GetTimeToLive(); - static int32_t GetDefaultSize(); - static int32_t GetDefaultTimeToLive(); private: int32_t size_ = 1; int32_t timeToLive_ = 600; - int32_t default_size_ = 1; - int32_t default_time_to_live_ = 600; }; -class BackForwardCacheSupportFeatures { +class BackForwardCacheSupportedFeatures { public: - BackForwardCacheSupportFeatures(); + BackForwardCacheSupportedFeatures(); bool IsEnableNativeEmbed(); - bool isEnableMediaTakeOver(); + bool IsEnableMediaTakeOver(); private: bool nativeEmbed_ = false; diff --git a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp index c43df7d4e..f149c6007 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.cpp @@ -55,21 +55,21 @@ napi_value NapiBackForwardCacheOptions::JS_Constructor(napi_env env, napi_callba return thisVar; } -napi_value NapiBackForwardCacheSupportFeatures::JS_Constructor(napi_env env, napi_callback_info info) +napi_value NapiBackForwardCacheSupportedFeatures::JS_Constructor(napi_env env, napi_callback_info info) { - WVLOG_I("NapiBackForwardCacheSupportFeatures::JS_Constructor is called"); + WVLOG_I("NapiBackForwardCacheSupportedFeatures::JS_Constructor is called"); napi_value thisVar = nullptr; void *data = nullptr; size_t argc = 2; napi_value argv[2] = {0}; napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - BackForwardCacheSupportFeatures *features = new BackForwardCacheSupportFeatures(); + BackForwardCacheSupportedFeatures *features = new BackForwardCacheSupportedFeatures(); napi_wrap( env, thisVar, features, [](napi_env /* env */, void *data, void * /* hint */) { - BackForwardCacheSupportFeatures *features = (BackForwardCacheSupportFeatures *)data; + BackForwardCacheSupportedFeatures *features = (BackForwardCacheSupportedFeatures *)data; delete features; features = nullptr; }, @@ -78,59 +78,27 @@ napi_value NapiBackForwardCacheSupportFeatures::JS_Constructor(napi_env env, nap return thisVar; } -napi_value NapiBackForwardCacheOptions::JS_GetSize(napi_env env, napi_callback_info info) -{ - WVLOG_D("NapiBackForwardCacheOptions::JS_GetSize."); - return nullptr; -} - -napi_value NapiBackForwardCacheOptions::JS_GetTimeToLive(napi_env env, napi_callback_info info) -{ - WVLOG_D("NapiBackForwardCacheOptions::JS_GetTimeToLive."); - return nullptr; -} - -napi_value NapiBackForwardCacheSupportFeatures::JS_IsEnableNativeEmbed(napi_env env, napi_callback_info info) -{ - WVLOG_D("NapiBackForwardCacheSupportFeatures::JS_IsEnableNativeEmbed."); - return nullptr; -} - -napi_value NapiBackForwardCacheSupportFeatures::JS_IsEnableMediaTakeOver(napi_env env, napi_callback_info info) -{ - WVLOG_D("NapiBackForwardCacheSupportFeatures::JS_IsEnableMediaTakeOver."); - return nullptr; -} - napi_value NapiBackForwardCacheOptions::Init(napi_env env, napi_value exports) { WVLOG_D("NapiBackForwardCacheOptions::Init"); - napi_property_descriptor properties[] = { - DECLARE_NAPI_FUNCTION("getSize", JS_GetSize), - DECLARE_NAPI_FUNCTION("getTimeToLive", JS_GetTimeToLive), - }; napi_value backForwardCacheOptions = nullptr; - napi_define_class(env, BACK_FORWARD_CACHE_OPTIONS.c_str(), BACK_FORWARD_CACHE_OPTIONS.length(), - JS_Constructor, nullptr, - sizeof(properties) / sizeof(properties[0]), properties, &backForwardCacheOptions); + napi_define_class(env, BACK_FORWARD_CACHE_OPTIONS.c_str(), + BACK_FORWARD_CACHE_OPTIONS.length(), + JS_Constructor, nullptr, 0, nullptr, &backForwardCacheOptions); napi_set_named_property(env, exports, BACK_FORWARD_CACHE_OPTIONS.c_str(), backForwardCacheOptions); return exports; } -napi_value NapiBackForwardCacheSupportFeatures::Init(napi_env env, napi_value exports) +napi_value NapiBackForwardCacheSupportedFeatures::Init(napi_env env, napi_value exports) { - WVLOG_D("NapiBackForwardCacheSupportFeatures::Init"); - napi_property_descriptor properties[] = { - DECLARE_NAPI_FUNCTION("isEnableNativeEmbed", JS_IsEnableNativeEmbed), - DECLARE_NAPI_FUNCTION("isEnableMediaTakeOver", JS_IsEnableMediaTakeOver), - }; - napi_value backForwardCacheSupportFeatures = nullptr; - napi_define_class(env, BACK_FORWARD_CACHE_SUPPORT_FEATURES.c_str(), BACK_FORWARD_CACHE_SUPPORT_FEATURES.length(), - JS_Constructor, nullptr, - sizeof(properties) / sizeof(properties[0]), properties, &backForwardCacheSupportFeatures); - napi_set_named_property(env, exports, BACK_FORWARD_CACHE_SUPPORT_FEATURES.c_str(), - backForwardCacheSupportFeatures); + WVLOG_D("NapiBackForwardCacheSupportedFeatures::Init"); + napi_value backForwardCacheSupportedFeatures = nullptr; + napi_define_class(env, BACK_FORWARD_CACHE_SUPPORTED_FEATURES.c_str(), + BACK_FORWARD_CACHE_SUPPORTED_FEATURES.length(), + JS_Constructor, nullptr, 0, nullptr, &backForwardCacheSupportedFeatures); + napi_set_named_property(env, exports, BACK_FORWARD_CACHE_SUPPORTED_FEATURES.c_str(), + backForwardCacheSupportedFeatures); return exports; } diff --git a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h index 68e3eaa59..2d04145c9 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h +++ b/interfaces/kits/napi/webviewcontroller/napi_back_forward_cache_options.h @@ -24,7 +24,7 @@ namespace OHOS { namespace NWeb { const std::string BACK_FORWARD_CACHE_OPTIONS = "BackForwardCacheOptions"; -const std::string BACK_FORWARD_CACHE_SUPPORT_FEATURES = "BackForwardCacheSupportFeatures"; +const std::string BACK_FORWARD_CACHE_SUPPORTED_FEATURES = "BackForwardCacheSupportedFeatures"; class NapiBackForwardCacheOptions { public: @@ -33,19 +33,15 @@ public: static napi_value Init(napi_env env, napi_value exports); static napi_value JS_Constructor(napi_env env, napi_callback_info info); - static napi_value JS_GetSize(napi_env env, napi_callback_info info); - static napi_value JS_GetTimeToLive(napi_env env, napi_callback_info info); }; -class NapiBackForwardCacheSupportFeatures { +class NapiBackForwardCacheSupportedFeatures { public: - NapiBackForwardCacheSupportFeatures() = default; - ~NapiBackForwardCacheSupportFeatures() = default; + NapiBackForwardCacheSupportedFeatures() = default; + ~NapiBackForwardCacheSupportedFeatures() = default; static napi_value Init(napi_env env, napi_value exports); static napi_value JS_Constructor(napi_env env, napi_callback_info info); - static napi_value JS_IsEnableNativeEmbed(napi_env env, napi_callback_info info); - static napi_value JS_IsEnableMediaTakeOver(napi_env env, napi_callback_info info); }; } diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp index 66a2ee972..87ec385a1 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp @@ -5494,6 +5494,7 @@ napi_value NapiWebviewController::EnableBackForwardCache(napi_env env, napi_call napi_get_undefined(env, &result); napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != INTEGER_ONE) { + WVLOG_E("SetBackForwardCacheOptions: wrong number of params."); NWebHelper::Instance().EnableBackForwardCache(false, false); NAPI_CALL(env, napi_get_undefined(env, &result)); return result; @@ -5530,14 +5531,15 @@ napi_value NapiWebviewController::SetBackForwardCacheOptions(napi_env env, napi_ napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); WebviewController* webviewController = GetWebviewController(env, info); if (!webviewController) { - WVLOG_E("InjectOfflineResource: init webview controller error."); + WVLOG_E("SetBackForwardCacheOptions: Init webview controller error."); BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); return result; } if (argc != INTEGER_ONE) { + WVLOG_E("SetBackForwardCacheOptions: wrong number of params."); webviewController->SetBackForwardCacheOptions( - BackForwardCacheOptions::GetDefaultSize(), BackForwardCacheOptions::GetDefaultTimeToLive()); + BFCACHE_DEFAULT_SIZE, BFCACHE_DEFAULT_TIMETOLIVE); NAPI_CALL(env, napi_get_undefined(env, &result)); return result; } @@ -5548,13 +5550,13 @@ napi_value NapiWebviewController::SetBackForwardCacheOptions(napi_env env, napi_ napi_value timeToLiveObj = nullptr; if (napi_get_named_property(env, argv[INTEGER_ZERO], "size", &sizeObj) == napi_ok) { if (!NapiParseUtils::ParseInt32(env, sizeObj, size)) { - size = BackForwardCacheOptions::GetDefaultSize(); + size = BFCACHE_DEFAULT_SIZE; } } if (napi_get_named_property(env, argv[INTEGER_ZERO], "timeToLive", &timeToLiveObj) == napi_ok) { if (!NapiParseUtils::ParseInt32(env, timeToLiveObj, timeToLive)) { - timeToLive = BackForwardCacheOptions::GetDefaultTimeToLive(); + timeToLive = BFCACHE_DEFAULT_TIMETOLIVE; } } diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h index 7271bdbb3..28236ab74 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h @@ -79,8 +79,6 @@ public: ArkWebRefPtr GetAdsBlockManager() override; - void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaTakeOver) override; - private: std::shared_ptr nweb_engine_; }; diff --git a/test/unittest/nweb_helper_test/nweb_helper_test.cpp b/test/unittest/nweb_helper_test/nweb_helper_test.cpp index 2ebb7da75..51d27e0f4 100644 --- a/test/unittest/nweb_helper_test/nweb_helper_test.cpp +++ b/test/unittest/nweb_helper_test/nweb_helper_test.cpp @@ -144,7 +144,7 @@ public: return nullptr; } - void EnableBackForwardCache(bool nativeEmbed, bool mediaTakeOver); + void EnableBackForwardCache(bool nativeEmbed, bool mediaTakeOver) {} }; void NwebHelperTest::SetUpTestCase(void) @@ -328,6 +328,7 @@ HWTEST_F(NwebHelperTest, NWebHelper_GetConfigPath_005, TestSize.Level1) NWebHelper::Instance().PrefetchResource(nullptr, {}, "web_test", 0); NWebHelper::Instance().ClearPrefetchedResource({"web_test"}); NWebHelper::Instance().bundlePath_.clear(); + NWebHelper::Instance().EnableBackForwardCache(true, true); bool result = NWebHelper::Instance().InitAndRun(false); EXPECT_FALSE(result); NWebHelper::Instance().SetConnectionTimeout(1); -- Gitee From edf7fa1c12aea31b522070fd464b4f6727fbc80a Mon Sep 17 00:00:00 2001 From: zhangyanchuan Date: Mon, 8 Jul 2024 10:59:20 +0800 Subject: [PATCH 18/32] =?UTF-8?q?=E4=BF=AE=E6=94=B9so=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E4=B8=BA=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangyanchuan --- .../bridge/webcore/ark_web_adapter_bridge_helper.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_web_adapter_bridge_helper.cpp b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_web_adapter_bridge_helper.cpp index bec20dfb8..1180d736d 100644 --- a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_web_adapter_bridge_helper.cpp +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_web_adapter_bridge_helper.cpp @@ -22,12 +22,6 @@ namespace OHOS::ArkWeb { const std::string LIB_FILE_NAME = "libohos_adapter_glue_source.z.so"; -#if defined(__aarch64__) || defined(__x86_64__) -const std::string LIB_DIR_PATH = "/system/lib64"; -#else -const std::string LIB_DIR_PATH = "/system/lib"; -#endif - ArkWebAdapterBridgeHelper& ArkWebAdapterBridgeHelper::GetInstance(bool isPrintLog) { static ArkWebAdapterBridgeHelper helper; @@ -38,7 +32,7 @@ ArkWebAdapterBridgeHelper& ArkWebAdapterBridgeHelper::GetInstance(bool isPrintLo bool ArkWebAdapterBridgeHelper::Init(bool isPrintLog) { - return LoadLibFile(RTLD_LAZY, LIB_DIR_PATH + "/" + LIB_FILE_NAME, isPrintLog); + return LoadLibFile(RTLD_LAZY, LIB_FILE_NAME, isPrintLog); } } // namespace OHOS::ArkWeb -- Gitee From fa1e5414384f61ba2a7dee101a9ec71d1d1ae0c7 Mon Sep 17 00:00:00 2001 From: zhanggefei Date: Mon, 8 Jul 2024 11:21:13 +0800 Subject: [PATCH 19/32] add web database API to webview module Change-Id: If77a15f0174fdb5d9105260937ca15855f4fd945 --- interfaces/kits/cj/BUILD.gn | 1 + interfaces/kits/cj/include/web_data_base.h | 40 ++++++++++ interfaces/kits/cj/include/web_errors.h | 2 + interfaces/kits/cj/include/webview_ffi.h | 6 ++ interfaces/kits/cj/src/web_data_base.cpp | 93 ++++++++++++++++++++++ interfaces/kits/cj/src/web_errors.cpp | 2 + interfaces/kits/cj/src/webview_ffi.cpp | 43 ++++++++++ 7 files changed, 187 insertions(+) create mode 100644 interfaces/kits/cj/include/web_data_base.h create mode 100644 interfaces/kits/cj/src/web_data_base.cpp diff --git a/interfaces/kits/cj/BUILD.gn b/interfaces/kits/cj/BUILD.gn index 3603933fb..e53b6f2b9 100644 --- a/interfaces/kits/cj/BUILD.gn +++ b/interfaces/kits/cj/BUILD.gn @@ -39,6 +39,7 @@ ohos_shared_library("cj_webview_ffi") { "src/webview_javascript_execute_callback.cpp", "src/webview_javascript_result_callback.cpp", "src/webview_utils.cpp", + "src/web_data_base.cpp", ] deps = [ diff --git a/interfaces/kits/cj/include/web_data_base.h b/interfaces/kits/cj/include/web_data_base.h new file mode 100644 index 000000000..dc7dcfb42 --- /dev/null +++ b/interfaces/kits/cj/include/web_data_base.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 Huawei Device 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. + */ + +#ifndef WEB_DATA_BASE_H +#define WEB_DATA_BASE_H + +#include +#include + +namespace OHOS { +namespace NWeb { + +const int MAX_STRING_LENGTH = 40960; +const int MAX_PWD_LENGTH = 256; +class WebDataBase { +public: + WebDataBase() {} + ~WebDataBase() = default; + + static CArrString CJGetHttpAuthCredentials(const std::string &host, const std::string &realm); + static void CJSaveHttpAuthCredentials(const std::string &host, const std::string &realm, const std::string &username, const std::string &password); + static bool CJExistHttpAuthCredentials(); + static void CJDeleteHttpAuthCredentials(); +}; +} +} + +#endif \ No newline at end of file diff --git a/interfaces/kits/cj/include/web_errors.h b/interfaces/kits/cj/include/web_errors.h index a71d3776e..c17111404 100644 --- a/interfaces/kits/cj/include/web_errors.h +++ b/interfaces/kits/cj/include/web_errors.h @@ -47,6 +47,8 @@ constexpr ErrCode REGISTER_CUSTOM_SCHEME_FAILED = 17100020; constexpr ErrCode RESOURCE_HANDLER_INVALID = 17100021; constexpr ErrCode HTTP_BODY_STREAN_INIT_FAILED = 17100022; +constexpr ErrCode HTTP_AUTH_NOT_FOUND = 17100023; + std::string GetErrMsgByErrCode(ErrCode code); } } diff --git a/interfaces/kits/cj/include/webview_ffi.h b/interfaces/kits/cj/include/webview_ffi.h index af456fc2c..829605d9d 100644 --- a/interfaces/kits/cj/include/webview_ffi.h +++ b/interfaces/kits/cj/include/webview_ffi.h @@ -85,6 +85,12 @@ extern "C" { FFI_EXPORT bool FfiOHOSCookieMgrExistCookie(bool incognitoMode); FFI_EXPORT void FfiOHOSCookieMgrClearAllCookiesSync(bool incognitoMode); FFI_EXPORT void FfiOHOSCookieMgrClearSessionCookieSync(); + + // data_base + FFI_EXPORT RetDataCArrString FfiOHOSDBGetHttpAuthCredentials(const char *host, const char *realm); + FFI_EXPORT void FfiOHOSDBSaveHttpAuthCredentials(const char *host, const char *realm, const char *username, const char *password); + FFI_EXPORT bool FfiOHOSDBExistHttpAuthCredentials(); + FFI_EXPORT void FfiOHOSDBDeleteHttpAuthCredentials(); } #endif // WEBVIEW_FFI_H \ No newline at end of file diff --git a/interfaces/kits/cj/src/web_data_base.cpp b/interfaces/kits/cj/src/web_data_base.cpp new file mode 100644 index 000000000..0d04b1bde --- /dev/null +++ b/interfaces/kits/cj/src/web_data_base.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2024 Huawei Device 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 "web_data_base.h" +#include "nweb_data_base.h" +#include "nweb_helper.h" +#include "web_errors.h" +#include + + +namespace OHOS { +namespace NWeb { + +CArrString WebDataBase::CJGetHttpAuthCredentials(const std::string &host, const std::string &realm) +{ + // 1 create information for auth name and password; + struct CArrString ret; + std::string username_s; + char password[MAX_PWD_LENGTH + 1] = {0}; + + // get web database instance; + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); + if (database != nullptr) { + database->GetHttpAuthCredentials(host, realm, username_s, password, MAX_PWD_LENGTH + 1); + } + + // 2 params check about username and password; + // if get no information for auth name or password, it will return result with nullptr; + if (username_s.empty() || strlen(password) == 0) { + ret.head = nullptr; + ret.size = 0; + return ret; + } + + // 3 make result; + char** result; + result = (char**)malloc(sizeof(char*) * 2); + + result[0] = (char*)malloc(sizeof(char) * MAX_STRING_LENGTH); + result[1] = (char*)malloc(sizeof(char) * MAX_PWD_LENGTH); + + strcpy(result[0], username_s.c_str()); + strcpy(result[1], password); + + ret.head = result; + ret.size = 2; + + // 4 return; + return ret; +} + +void WebDataBase::CJSaveHttpAuthCredentials(const std::string &host, const std::string &realm, const std::string &username, const std::string &password) +{ + // get web database instance; + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); + if (database != nullptr) { + database->SaveHttpAuthCredentials(host, realm, username, password.c_str()); + } +} + +bool WebDataBase::CJExistHttpAuthCredentials() +{ + bool isExist = false; + // get web database instance; + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); + if (database != nullptr) { + isExist = database->ExistHttpAuthCredentials(); + } + return isExist; +} + +void WebDataBase::CJDeleteHttpAuthCredentials() +{ + // get web database instance; + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); + if (database != nullptr) { + database->DeleteHttpAuthCredentials(); + } +} +} +} \ No newline at end of file diff --git a/interfaces/kits/cj/src/web_errors.cpp b/interfaces/kits/cj/src/web_errors.cpp index 93295db9a..9ec956ead 100644 --- a/interfaces/kits/cj/src/web_errors.cpp +++ b/interfaces/kits/cj/src/web_errors.cpp @@ -41,6 +41,7 @@ const std::string DOWNLOAD_NOT_START_MSG = "The download task is not started yet const std::string REGISTER_CUSTOM_SCHEME_FAILED_MSG = "Failed to register custom schemes."; const std::string HTTP_BODY_STREAN_INIT_FAILED_MSG = "Failed to initialize the HTTP body stream."; const std::string RESOURCE_HANDLER_INVALID_MSG = "The resource handler is invalid."; +const std::string HTTP_AUTH_NOT_FOUND_MSG = "Failed to find http auth credential in web database."; } namespace OHOS { @@ -67,6 +68,7 @@ std::unordered_map g_errCodeMsgMap = { {REGISTER_CUSTOM_SCHEME_FAILED, REGISTER_CUSTOM_SCHEME_FAILED_MSG}, {HTTP_BODY_STREAN_INIT_FAILED, HTTP_BODY_STREAN_INIT_FAILED_MSG}, {RESOURCE_HANDLER_INVALID, RESOURCE_HANDLER_INVALID_MSG}, + {HTTP_AUTH_NOT_FOUND, HTTP_AUTH_NOT_FOUND_MSG}, }; std::string GetErrMsgByErrCode(ErrCode code) diff --git a/interfaces/kits/cj/src/webview_ffi.cpp b/interfaces/kits/cj/src/webview_ffi.cpp index 62028c2aa..f8bfc8e77 100644 --- a/interfaces/kits/cj/src/webview_ffi.cpp +++ b/interfaces/kits/cj/src/webview_ffi.cpp @@ -22,6 +22,7 @@ #include "webview_log.h" #include "parameters.h" #include "web_cookie_manager.h" +#include "web_data_base.h" #include "pixel_map.h" #include "cj_lambda.h" #include "pixel_map_impl.h" @@ -762,6 +763,48 @@ extern "C" { *errCode = NWebError::NO_ERROR; return ret; } + + // web data base; + RetDataCArrString FfiOHOSDBGetHttpAuthCredentials(const char *host, const char *realm) + { + std::string host_s = std::string(host); + std::string realm_s = std::string(realm); + + struct CArrString result = OHOS::NWeb::WebDataBase::CJGetHttpAuthCredentials(host_s, realm_s); + struct RetDataCArrString ret; + + // judge the return result size; + // if size = 0, return null string array; + if (result.size == 0) { + ret.code = NWebError::HTTP_AUTH_NOT_FOUND; + } + else { + ret.code = NWebError::NO_ERROR; + } + + ret.data = result; + return ret; + } + + void FfiOHOSDBSaveHttpAuthCredentials(const char *host, const char *realm, const char *username, const char *password) + { + std::string host_s = std::string(host); + std::string realm_s = std::string(realm); + std::string username_s = std::string(username); + std::string password_s = std::string(password); + + return OHOS::NWeb::WebDataBase::CJSaveHttpAuthCredentials(host_s, realm_s, username_s, password_s); + } + + bool FfiOHOSDBExistHttpAuthCredentials() + { + return OHOS::NWeb::WebDataBase::CJExistHttpAuthCredentials(); + } + + void FfiOHOSDBDeleteHttpAuthCredentials() + { + return OHOS::NWeb::WebDataBase::CJDeleteHttpAuthCredentials(); + } } } } -- Gitee From bd34455d8285635881f520d64ce8db4babe1cbe3 Mon Sep 17 00:00:00 2001 From: zhanggefei Date: Mon, 8 Jul 2024 11:21:13 +0800 Subject: [PATCH 20/32] add web database API to webview module Change-Id: If77a15f0174fdb5d9105260937ca15855f4fd945 Signed-off-by: zhanggefei --- interfaces/kits/cj/BUILD.gn | 1 + interfaces/kits/cj/include/web_data_base.h | 40 ++++++++++ interfaces/kits/cj/include/web_errors.h | 2 + interfaces/kits/cj/include/webview_ffi.h | 6 ++ interfaces/kits/cj/src/web_data_base.cpp | 93 ++++++++++++++++++++++ interfaces/kits/cj/src/web_errors.cpp | 2 + interfaces/kits/cj/src/webview_ffi.cpp | 43 ++++++++++ 7 files changed, 187 insertions(+) create mode 100644 interfaces/kits/cj/include/web_data_base.h create mode 100644 interfaces/kits/cj/src/web_data_base.cpp diff --git a/interfaces/kits/cj/BUILD.gn b/interfaces/kits/cj/BUILD.gn index 3603933fb..e53b6f2b9 100644 --- a/interfaces/kits/cj/BUILD.gn +++ b/interfaces/kits/cj/BUILD.gn @@ -39,6 +39,7 @@ ohos_shared_library("cj_webview_ffi") { "src/webview_javascript_execute_callback.cpp", "src/webview_javascript_result_callback.cpp", "src/webview_utils.cpp", + "src/web_data_base.cpp", ] deps = [ diff --git a/interfaces/kits/cj/include/web_data_base.h b/interfaces/kits/cj/include/web_data_base.h new file mode 100644 index 000000000..dc7dcfb42 --- /dev/null +++ b/interfaces/kits/cj/include/web_data_base.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 Huawei Device 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. + */ + +#ifndef WEB_DATA_BASE_H +#define WEB_DATA_BASE_H + +#include +#include + +namespace OHOS { +namespace NWeb { + +const int MAX_STRING_LENGTH = 40960; +const int MAX_PWD_LENGTH = 256; +class WebDataBase { +public: + WebDataBase() {} + ~WebDataBase() = default; + + static CArrString CJGetHttpAuthCredentials(const std::string &host, const std::string &realm); + static void CJSaveHttpAuthCredentials(const std::string &host, const std::string &realm, const std::string &username, const std::string &password); + static bool CJExistHttpAuthCredentials(); + static void CJDeleteHttpAuthCredentials(); +}; +} +} + +#endif \ No newline at end of file diff --git a/interfaces/kits/cj/include/web_errors.h b/interfaces/kits/cj/include/web_errors.h index a71d3776e..c17111404 100644 --- a/interfaces/kits/cj/include/web_errors.h +++ b/interfaces/kits/cj/include/web_errors.h @@ -47,6 +47,8 @@ constexpr ErrCode REGISTER_CUSTOM_SCHEME_FAILED = 17100020; constexpr ErrCode RESOURCE_HANDLER_INVALID = 17100021; constexpr ErrCode HTTP_BODY_STREAN_INIT_FAILED = 17100022; +constexpr ErrCode HTTP_AUTH_NOT_FOUND = 17100023; + std::string GetErrMsgByErrCode(ErrCode code); } } diff --git a/interfaces/kits/cj/include/webview_ffi.h b/interfaces/kits/cj/include/webview_ffi.h index af456fc2c..829605d9d 100644 --- a/interfaces/kits/cj/include/webview_ffi.h +++ b/interfaces/kits/cj/include/webview_ffi.h @@ -85,6 +85,12 @@ extern "C" { FFI_EXPORT bool FfiOHOSCookieMgrExistCookie(bool incognitoMode); FFI_EXPORT void FfiOHOSCookieMgrClearAllCookiesSync(bool incognitoMode); FFI_EXPORT void FfiOHOSCookieMgrClearSessionCookieSync(); + + // data_base + FFI_EXPORT RetDataCArrString FfiOHOSDBGetHttpAuthCredentials(const char *host, const char *realm); + FFI_EXPORT void FfiOHOSDBSaveHttpAuthCredentials(const char *host, const char *realm, const char *username, const char *password); + FFI_EXPORT bool FfiOHOSDBExistHttpAuthCredentials(); + FFI_EXPORT void FfiOHOSDBDeleteHttpAuthCredentials(); } #endif // WEBVIEW_FFI_H \ No newline at end of file diff --git a/interfaces/kits/cj/src/web_data_base.cpp b/interfaces/kits/cj/src/web_data_base.cpp new file mode 100644 index 000000000..0d04b1bde --- /dev/null +++ b/interfaces/kits/cj/src/web_data_base.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2024 Huawei Device 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 "web_data_base.h" +#include "nweb_data_base.h" +#include "nweb_helper.h" +#include "web_errors.h" +#include + + +namespace OHOS { +namespace NWeb { + +CArrString WebDataBase::CJGetHttpAuthCredentials(const std::string &host, const std::string &realm) +{ + // 1 create information for auth name and password; + struct CArrString ret; + std::string username_s; + char password[MAX_PWD_LENGTH + 1] = {0}; + + // get web database instance; + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); + if (database != nullptr) { + database->GetHttpAuthCredentials(host, realm, username_s, password, MAX_PWD_LENGTH + 1); + } + + // 2 params check about username and password; + // if get no information for auth name or password, it will return result with nullptr; + if (username_s.empty() || strlen(password) == 0) { + ret.head = nullptr; + ret.size = 0; + return ret; + } + + // 3 make result; + char** result; + result = (char**)malloc(sizeof(char*) * 2); + + result[0] = (char*)malloc(sizeof(char) * MAX_STRING_LENGTH); + result[1] = (char*)malloc(sizeof(char) * MAX_PWD_LENGTH); + + strcpy(result[0], username_s.c_str()); + strcpy(result[1], password); + + ret.head = result; + ret.size = 2; + + // 4 return; + return ret; +} + +void WebDataBase::CJSaveHttpAuthCredentials(const std::string &host, const std::string &realm, const std::string &username, const std::string &password) +{ + // get web database instance; + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); + if (database != nullptr) { + database->SaveHttpAuthCredentials(host, realm, username, password.c_str()); + } +} + +bool WebDataBase::CJExistHttpAuthCredentials() +{ + bool isExist = false; + // get web database instance; + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); + if (database != nullptr) { + isExist = database->ExistHttpAuthCredentials(); + } + return isExist; +} + +void WebDataBase::CJDeleteHttpAuthCredentials() +{ + // get web database instance; + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); + if (database != nullptr) { + database->DeleteHttpAuthCredentials(); + } +} +} +} \ No newline at end of file diff --git a/interfaces/kits/cj/src/web_errors.cpp b/interfaces/kits/cj/src/web_errors.cpp index 93295db9a..9ec956ead 100644 --- a/interfaces/kits/cj/src/web_errors.cpp +++ b/interfaces/kits/cj/src/web_errors.cpp @@ -41,6 +41,7 @@ const std::string DOWNLOAD_NOT_START_MSG = "The download task is not started yet const std::string REGISTER_CUSTOM_SCHEME_FAILED_MSG = "Failed to register custom schemes."; const std::string HTTP_BODY_STREAN_INIT_FAILED_MSG = "Failed to initialize the HTTP body stream."; const std::string RESOURCE_HANDLER_INVALID_MSG = "The resource handler is invalid."; +const std::string HTTP_AUTH_NOT_FOUND_MSG = "Failed to find http auth credential in web database."; } namespace OHOS { @@ -67,6 +68,7 @@ std::unordered_map g_errCodeMsgMap = { {REGISTER_CUSTOM_SCHEME_FAILED, REGISTER_CUSTOM_SCHEME_FAILED_MSG}, {HTTP_BODY_STREAN_INIT_FAILED, HTTP_BODY_STREAN_INIT_FAILED_MSG}, {RESOURCE_HANDLER_INVALID, RESOURCE_HANDLER_INVALID_MSG}, + {HTTP_AUTH_NOT_FOUND, HTTP_AUTH_NOT_FOUND_MSG}, }; std::string GetErrMsgByErrCode(ErrCode code) diff --git a/interfaces/kits/cj/src/webview_ffi.cpp b/interfaces/kits/cj/src/webview_ffi.cpp index 62028c2aa..f8bfc8e77 100644 --- a/interfaces/kits/cj/src/webview_ffi.cpp +++ b/interfaces/kits/cj/src/webview_ffi.cpp @@ -22,6 +22,7 @@ #include "webview_log.h" #include "parameters.h" #include "web_cookie_manager.h" +#include "web_data_base.h" #include "pixel_map.h" #include "cj_lambda.h" #include "pixel_map_impl.h" @@ -762,6 +763,48 @@ extern "C" { *errCode = NWebError::NO_ERROR; return ret; } + + // web data base; + RetDataCArrString FfiOHOSDBGetHttpAuthCredentials(const char *host, const char *realm) + { + std::string host_s = std::string(host); + std::string realm_s = std::string(realm); + + struct CArrString result = OHOS::NWeb::WebDataBase::CJGetHttpAuthCredentials(host_s, realm_s); + struct RetDataCArrString ret; + + // judge the return result size; + // if size = 0, return null string array; + if (result.size == 0) { + ret.code = NWebError::HTTP_AUTH_NOT_FOUND; + } + else { + ret.code = NWebError::NO_ERROR; + } + + ret.data = result; + return ret; + } + + void FfiOHOSDBSaveHttpAuthCredentials(const char *host, const char *realm, const char *username, const char *password) + { + std::string host_s = std::string(host); + std::string realm_s = std::string(realm); + std::string username_s = std::string(username); + std::string password_s = std::string(password); + + return OHOS::NWeb::WebDataBase::CJSaveHttpAuthCredentials(host_s, realm_s, username_s, password_s); + } + + bool FfiOHOSDBExistHttpAuthCredentials() + { + return OHOS::NWeb::WebDataBase::CJExistHttpAuthCredentials(); + } + + void FfiOHOSDBDeleteHttpAuthCredentials() + { + return OHOS::NWeb::WebDataBase::CJDeleteHttpAuthCredentials(); + } } } } -- Gitee From d12862a617d45ffef3950ad2d61452ca63de7880 Mon Sep 17 00:00:00 2001 From: zgit2021 Date: Fri, 5 Jul 2024 17:22:08 +0800 Subject: [PATCH 21/32] adapt to customDialog's keyboard avoid Signed-off-by: zgit2021 Change-Id: I8f44ebf6a49be0159480064cb4c127ecfd0adacd --- .../src/imf_adapter_impl.cpp | 10 +++++++--- ohos_interface/include/ohos_adapter/imf_adapter.h | 10 ++++++++++ ohos_interface/include/ohos_nweb/nweb_handler.h | 8 ++++++++ .../webcore/ark_imf_text_config_adapter_impl.cpp | 9 +++++++++ .../bridge/webcore/ark_imf_text_config_adapter_impl.h | 4 ++++ .../webview/ark_imftext_config_adapter_wrapper.cpp | 9 +++++++++ .../webview/ark_imftext_config_adapter_wrapper.h | 4 ++++ .../ohos_glue/ohos_adapter/include/ark_imf_adapter.h | 6 ++++++ .../bridge/webcore/ark_web_handler_wrapper.cpp | 5 +++++ .../ohos_nweb/bridge/webcore/ark_web_handler_wrapper.h | 3 +++ .../ohos_nweb/bridge/webview/ark_web_handler_impl.cpp | 5 +++++ .../ohos_nweb/bridge/webview/ark_web_handler_impl.h | 3 +++ .../ohos_glue/ohos_nweb/include/ark_web_handler.h | 9 +++++++++ 13 files changed, 82 insertions(+), 3 deletions(-) diff --git a/ohos_adapter/inputmethodframework_adapter/src/imf_adapter_impl.cpp b/ohos_adapter/inputmethodframework_adapter/src/imf_adapter_impl.cpp index e2b5bc8f3..4d832a195 100644 --- a/ohos_adapter/inputmethodframework_adapter/src/imf_adapter_impl.cpp +++ b/ohos_adapter/inputmethodframework_adapter/src/imf_adapter_impl.cpp @@ -300,9 +300,13 @@ bool IMFAdapterImpl::Attach(std::shared_ptr listener, bo .width = config->GetCursorInfo()->GetWidth(), .height = config->GetCursorInfo()->GetHeight() }; - MiscServices::TextConfig textConfig = { - .inputAttribute = inputAttribute, .cursorInfo = imfInfo, .windowId = config->GetWindowId() - }; + MiscServices::TextConfig textConfig = { .inputAttribute = inputAttribute, + .cursorInfo = imfInfo, + .windowId = config->GetWindowId(), + .positionY = config->GetPositionY(), + .height = config->GetHeight() }; + WVLOG_I("web inputmethod attach, isShowKeyboard=%{public}d, textConfig=%{public}s", isShowKeyboard, + textConfig.ToString().c_str()); int32_t ret = MiscServices::InputMethodController::GetInstance()->Attach(textListener_, isShowKeyboard, textConfig); if (ret != 0) { WVLOG_E("inputmethod attach failed, errcode=%{public}d", ret); diff --git a/ohos_interface/include/ohos_adapter/imf_adapter.h b/ohos_interface/include/ohos_adapter/imf_adapter.h index 5aa58b694..fca876848 100644 --- a/ohos_interface/include/ohos_adapter/imf_adapter.h +++ b/ohos_interface/include/ohos_adapter/imf_adapter.h @@ -106,6 +106,16 @@ public: virtual std::shared_ptr GetSelectionRange() = 0; virtual uint32_t GetWindowId() = 0; + + virtual double GetPositionY() + { + return 0.0; + } + + virtual double GetHeight() + { + return 0.0; + } }; enum class IMFAdapterKeyboardStatus : int32_t { NONE = 0, HIDE, SHOW }; diff --git a/ohos_interface/include/ohos_nweb/nweb_handler.h b/ohos_interface/include/ohos_nweb/nweb_handler.h index 459752ea2..212146715 100644 --- a/ohos_interface/include/ohos_nweb/nweb_handler.h +++ b/ohos_interface/include/ohos_nweb/nweb_handler.h @@ -966,6 +966,14 @@ public: * */ virtual void OnAdsBlocked(const std::string& url, const std::vector& adsBlocked) {} + + /** + * @brief called when the cursor info is updated. + * + * @param x, y relative coordinates within web components of the cursor + * @param width, height width and height of the cursor + */ + virtual void OnCursorUpdate(double x, double y, double width, double height) {} }; } // namespace OHOS::NWeb diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_imf_text_config_adapter_impl.cpp b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_imf_text_config_adapter_impl.cpp index b1fbadd3b..54d23bc9b 100644 --- a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_imf_text_config_adapter_impl.cpp +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_imf_text_config_adapter_impl.cpp @@ -59,4 +59,13 @@ uint32_t ArkIMFTextConfigAdapterImpl::GetWindowId() return real_->GetWindowId(); } +double ArkIMFTextConfigAdapterImpl::GetPositionY() +{ + return real_->GetPositionY(); +} + +double ArkIMFTextConfigAdapterImpl::GetHeight() +{ + return real_->GetHeight(); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_imf_text_config_adapter_impl.h b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_imf_text_config_adapter_impl.h index 2fa221425..1d6762f00 100644 --- a/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_imf_text_config_adapter_impl.h +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webcore/ark_imf_text_config_adapter_impl.h @@ -34,6 +34,10 @@ public: uint32_t GetWindowId() override; + double GetPositionY() override; + + double GetHeight() override; + private: std::shared_ptr real_; diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_imftext_config_adapter_wrapper.cpp b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_imftext_config_adapter_wrapper.cpp index 61ef624f8..af7aa2310 100644 --- a/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_imftext_config_adapter_wrapper.cpp +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_imftext_config_adapter_wrapper.cpp @@ -58,4 +58,13 @@ uint32_t ArkIMFTextConfigAdapterWrapper::GetWindowId() return ctocpp_->GetWindowId(); } +double ArkIMFTextConfigAdapterWrapper::GetPositionY() +{ + return ctocpp_->GetPositionY(); +} + +double ArkIMFTextConfigAdapterWrapper::GetHeight() +{ + return ctocpp_->GetHeight(); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_imftext_config_adapter_wrapper.h b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_imftext_config_adapter_wrapper.h index 6f03508c3..b7121b0df 100644 --- a/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_imftext_config_adapter_wrapper.h +++ b/ohos_interface/ohos_glue/ohos_adapter/bridge/webview/ark_imftext_config_adapter_wrapper.h @@ -34,6 +34,10 @@ public: uint32_t GetWindowId() override; + double GetPositionY() override; + + double GetHeight() override; + private: ArkWebRefPtr ctocpp_; }; diff --git a/ohos_interface/ohos_glue/ohos_adapter/include/ark_imf_adapter.h b/ohos_interface/ohos_glue/ohos_adapter/include/ark_imf_adapter.h index 0bd546403..415dd5a25 100644 --- a/ohos_interface/ohos_glue/ohos_adapter/include/ark_imf_adapter.h +++ b/ohos_interface/ohos_glue/ohos_adapter/include/ark_imf_adapter.h @@ -72,6 +72,12 @@ public: /*--ark web()--*/ virtual uint32_t GetWindowId() = 0; + + /*--ark web()--*/ + virtual double GetPositionY() = 0; + + /*--ark web()--*/ + virtual double GetHeight() = 0; }; /*--ark web(source=webview)--*/ diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_handler_wrapper.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_handler_wrapper.cpp index 6e6c298ae..7cd66db54 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_handler_wrapper.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_handler_wrapper.cpp @@ -977,4 +977,9 @@ void ArkWebHandlerWrapper::OnAdsBlocked( ArkWebStringVectorStructRelease(stAdsBlocked); ArkWebStringStructRelease(stUrl); } + +void ArkWebHandlerWrapper::OnCursorUpdate(double x, double y, double width, double height) +{ + ark_web_handler_->OnCursorUpdate(x, y, width, height); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_handler_wrapper.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_handler_wrapper.h index 7e5ad0c6a..9ac672927 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_handler_wrapper.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_handler_wrapper.h @@ -594,6 +594,9 @@ public: void OnCustomKeyboardClose() override; void OnAdsBlocked(const std::string &url, const std::vector &adsBlocked) override; + + void OnCursorUpdate(double x, double y, double width, double height) override; + private: ArkWebRefPtr ark_web_handler_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_handler_impl.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_handler_impl.cpp index 580d80a55..14693cd48 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_handler_impl.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_handler_impl.cpp @@ -867,4 +867,9 @@ void ArkWebHandlerImpl::OnAdsBlocked(const ArkWebString &url, const ArkWebString nweb_handler_->OnAdsBlocked(ArkWebStringStructToClass(url), ArkWebStringVectorStructToClass(adsBlocked)); } + +void ArkWebHandlerImpl::OnCursorUpdate(double x, double y, double width, double height) +{ + nweb_handler_->OnCursorUpdate(x, y, width, height); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_handler_impl.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_handler_impl.h index c9a0edae0..42707294c 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_handler_impl.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_handler_impl.h @@ -557,6 +557,9 @@ public: void OnCustomKeyboardClose() override; void OnAdsBlocked(const ArkWebString &url, const ArkWebStringVector &adsBlocked) override; + + void OnCursorUpdate(double x, double y, double width, double height) override; + private: std::shared_ptr nweb_handler_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_handler.h b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_handler.h index b17fc6b55..b9352634a 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_handler.h +++ b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_handler.h @@ -744,6 +744,15 @@ public: */ /*--ark web()--*/ virtual void OnAdsBlocked(const ArkWebString &url, const ArkWebStringVector &adsBlocked) = 0; + + /** + * @brief called when the cursor info is updated. + * + * @param x, y relative coordinates within web components of the cursor + * @param width, height width and height of the cursor + */ + /*--ark web()--*/ + virtual void OnCursorUpdate(double x, double y, double width, double height) = 0; }; } // namespace OHOS::ArkWeb -- Gitee From 883ae653895f519e8af98382cda3975de2012009 Mon Sep 17 00:00:00 2001 From: Wanpucheng Date: Mon, 8 Jul 2024 19:10:54 +0800 Subject: [PATCH 22/32] =?UTF-8?q?=E8=A7=A3=E5=86=B3ArkWeb=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=BC=A9=E8=BF=9B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Wanpucheng --- ohos_nweb/include/nweb_init_params.h | 306 +++++++++++++++------------ 1 file changed, 170 insertions(+), 136 deletions(-) diff --git a/ohos_nweb/include/nweb_init_params.h b/ohos_nweb/include/nweb_init_params.h index 8cfe711d2..9f1b23742 100644 --- a/ohos_nweb/include/nweb_init_params.h +++ b/ohos_nweb/include/nweb_init_params.h @@ -22,201 +22,235 @@ namespace OHOS::NWeb { class NWebDOHConfigImpl : public NWebDOHConfig { public: - NWebDOHConfigImpl() = default; - ~NWebDOHConfigImpl() = default; + NWebDOHConfigImpl() = default; + ~NWebDOHConfigImpl() = default; - void SetMode(int mode) { - mode_ = mode; - } + void SetMode(int mode) + { + mode_ = mode; + } - int GetMode() override { - return mode_; - } + int GetMode() override + { + return mode_; + } - void SetConfig(const std::string &config) { - config_ = config; - } + void SetConfig(const std::string& config) + { + config_ = config; + } - std::string GetConfig() override { - return config_; - } + std::string GetConfig() override + { + return config_; + } private: - int mode_ = -1; - std::string config_; + int mode_ = -1; + std::string config_; }; class NWebCreateInfoImpl : public NWebCreateInfo { public: - NWebCreateInfoImpl() = default; - ~NWebCreateInfoImpl() = default; + NWebCreateInfoImpl() = default; + ~NWebCreateInfoImpl() = default; - void SetWidth(uint32_t width) { - width_ = width; - } + void SetWidth(uint32_t width) + { + width_ = width; + } - uint32_t GetWidth() override { - return width_; - } + uint32_t GetWidth() override + { + return width_; + } - void SetHeight(uint32_t height) { - height_ = height; - } + void SetHeight(uint32_t height) + { + height_ = height; + } - uint32_t GetHeight() override { - return height_; - } + uint32_t GetHeight() override + { + return height_; + } - void SetIsIncognitoMode(bool isIncognitoMode) { - isIncognitoMode_ = isIncognitoMode; - } + void SetIsIncognitoMode(bool isIncognitoMode) + { + isIncognitoMode_ = isIncognitoMode; + } - bool GetIsIncognitoMode() override { - return isIncognitoMode_; - } + bool GetIsIncognitoMode() override + { + return isIncognitoMode_; + } - void SetProducerSurface(void *producerSurface) { - producerSurface_ = producerSurface; - } + void SetProducerSurface(void* producerSurface) + { + producerSurface_ = producerSurface; + } - void *GetProducerSurface() override { - return producerSurface_; - } + void* GetProducerSurface() override + { + return producerSurface_; + } - void SetEnhanceSurfaceInfo(void *enhanceSurfaceInfo) { - enhanceSurfaceInfo_ = enhanceSurfaceInfo; - } + void SetEnhanceSurfaceInfo(void* enhanceSurfaceInfo) + { + enhanceSurfaceInfo_ = enhanceSurfaceInfo; + } - void *GetEnhanceSurfaceInfo() override { - return enhanceSurfaceInfo_; - } + void* GetEnhanceSurfaceInfo() override + { + return enhanceSurfaceInfo_; + } - void SetEngineInitArgs(std::shared_ptr initArgs) { - initArgs_ = initArgs; - } + void SetEngineInitArgs(std::shared_ptr initArgs) + { + initArgs_ = initArgs; + } - std::shared_ptr GetEngineInitArgs() override { - return initArgs_; - } + std::shared_ptr GetEngineInitArgs() override + { + return initArgs_; + } - void SetOutputFrameCallback( - std::shared_ptr outputFrameCallback) { - outputFrameCallback_ = outputFrameCallback; - } + void SetOutputFrameCallback(std::shared_ptr outputFrameCallback) + { + outputFrameCallback_ = outputFrameCallback; + } - std::shared_ptr GetOutputFrameCallback() override { - return outputFrameCallback_; - } + std::shared_ptr GetOutputFrameCallback() override + { + return outputFrameCallback_; + } private: - uint32_t width_ = 0; - uint32_t height_ = 0; + uint32_t width_ = 0; + uint32_t height_ = 0; - bool isIncognitoMode_ = false; + bool isIncognitoMode_ = false; - void *producerSurface_ = nullptr; - void *enhanceSurfaceInfo_ = nullptr; + void* producerSurface_ = nullptr; + void* enhanceSurfaceInfo_ = nullptr; - std::shared_ptr initArgs_ = nullptr; - std::shared_ptr outputFrameCallback_ = nullptr; + std::shared_ptr initArgs_ = nullptr; + std::shared_ptr outputFrameCallback_ = nullptr; }; class NWebEngineInitArgsImpl : public NWebEngineInitArgs { public: - NWebEngineInitArgsImpl() = default; - ~NWebEngineInitArgsImpl() = default; + NWebEngineInitArgsImpl() = default; + ~NWebEngineInitArgsImpl() = default; - void AddArg(const std::string &arg) { - argsToAdd_.emplace_back(arg); - } + void AddArg(const std::string& arg) + { + argsToAdd_.emplace_back(arg); + } - void AddDeleteArg(const std::string &arg) { - argsToDelete_.emplace_back(arg); - } + void AddDeleteArg(const std::string& arg) + { + argsToDelete_.emplace_back(arg); + } - void SetDumpPath(const std::string &dumpPath) { - dumpPath_ = dumpPath; - } + void SetDumpPath(const std::string& dumpPath) + { + dumpPath_ = dumpPath; + } - std::string GetDumpPath() override { - return dumpPath_; - } + std::string GetDumpPath() override + { + return dumpPath_; + } - void SetIsPopup(bool isPopup) { - isPopup_ = isPopup; - } + void SetIsPopup(bool isPopup) + { + isPopup_ = isPopup; + } - bool GetIsPopup() override { - return isPopup_; - } + bool GetIsPopup() override + { + return isPopup_; + } - void SetIsFrameInfoDump(bool isFrameInfoDump) { - isFrameInfoDump_ = isFrameInfoDump; - } + void SetIsFrameInfoDump(bool isFrameInfoDump) + { + isFrameInfoDump_ = isFrameInfoDump; + } - bool GetIsFrameInfoDump() override { - return isFrameInfoDump_; - } + bool GetIsFrameInfoDump() override + { + return isFrameInfoDump_; + } - void SetIsEnhanceSurface(bool isEnhanceSurface) { - isEnhanceSurface_ = isEnhanceSurface; - } + void SetIsEnhanceSurface(bool isEnhanceSurface) + { + isEnhanceSurface_ = isEnhanceSurface; + } - bool GetIsEnhanceSurface() override { - return isEnhanceSurface_; - } + bool GetIsEnhanceSurface() override + { + return isEnhanceSurface_; + } - void SetIsMultiRendererProcess(bool isMultiRendererProcess) { - isMultiRendererProcess_ = isMultiRendererProcess; - } + void SetIsMultiRendererProcess(bool isMultiRendererProcess) + { + isMultiRendererProcess_ = isMultiRendererProcess; + } - bool GetIsMultiRendererProcess() override { - return isMultiRendererProcess_; - } + bool GetIsMultiRendererProcess() override + { + return isMultiRendererProcess_; + } - void SetArgsToAdd(const std::list &argsToAdd) { - argsToAdd_ = argsToAdd; - } + void SetArgsToAdd(const std::list& argsToAdd) + { + argsToAdd_ = argsToAdd; + } - std::list GetArgsToAdd() override { - return argsToAdd_; - } + std::list GetArgsToAdd() override + { + return argsToAdd_; + } - void SetArgsToDelete(const std::list &argsToDelete) { - argsToDelete_ = argsToDelete; - } + void SetArgsToDelete(const std::list& argsToDelete) + { + argsToDelete_ = argsToDelete; + } - std::list GetArgsToDelete() override { - return argsToDelete_; - } + std::list GetArgsToDelete() override + { + return argsToDelete_; + } - void SetSharedRenderProcessToken(const std::string& sharedRenderProcessToken) - { - sharedRenderProcessToken_ = sharedRenderProcessToken; - } + void SetSharedRenderProcessToken(const std::string& sharedRenderProcessToken) + { + sharedRenderProcessToken_ = sharedRenderProcessToken; + } - std::string GetSharedRenderProcessToken() override - { - return sharedRenderProcessToken_; - } + std::string GetSharedRenderProcessToken() override + { + return sharedRenderProcessToken_; + } private: - std::string dumpPath_; + std::string dumpPath_; - bool isPopup_ = false; - bool isFrameInfoDump_ = false; - bool isEnhanceSurface_ = false; - bool isMultiRendererProcess_ = false; + bool isPopup_ = false; + bool isFrameInfoDump_ = false; + bool isEnhanceSurface_ = false; + bool isMultiRendererProcess_ = false; - std::list argsToAdd_; - std::list argsToDelete_; - std::string sharedRenderProcessToken_; + std::list argsToAdd_; + std::list argsToDelete_; + std::string sharedRenderProcessToken_; }; class NWebEnginePrefetchArgsImpl : public NWebEnginePrefetchArgs { public: - NWebEnginePrefetchArgsImpl(const std::string &url, const std::string &method, const std::string &formData) - : url_(url), method_(method), form_data_(formData) {} + NWebEnginePrefetchArgsImpl(const std::string& url, const std::string& method, const std::string& formData) + : url_(url), method_(method), form_data_(formData) + {} ~NWebEnginePrefetchArgsImpl() = default; -- Gitee From d2a67d87e60f8cc81d5071bea5a5e835f782c198 Mon Sep 17 00:00:00 2001 From: zhangyanchuan Date: Tue, 9 Jul 2024 14:40:54 +0800 Subject: [PATCH 23/32] =?UTF-8?q?=E8=A7=86=E9=A2=91=E6=89=98=E7=AE=A1?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=BF=9BBFCache(=E5=90=8C=E6=AD=A5=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=8E=A5=E5=8F=A3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangyanchuan --- .../napi/webviewcontroller/napi_native_media_player.cpp | 8 ++++---- .../include/ohos_nweb/nweb_native_media_player.h | 9 ++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/interfaces/kits/napi/webviewcontroller/napi_native_media_player.cpp b/interfaces/kits/napi/webviewcontroller/napi_native_media_player.cpp index 9251d7932..17ca2f7c4 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_native_media_player.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_native_media_player.cpp @@ -178,12 +178,12 @@ napi_status NapiNativeMediaPlayerHandler::ExportEnumSuspendType(napi_env env, na const std::string NPI_SUSPEND_TYPE_ENUM_NAME = "SuspendType"; napi_property_descriptor properties[] = { - DECLARE_NAPI_STATIC_PROPERTY("EnterBackForwardCache", - NapiParseUtils::ToInt32Value(env, static_cast(SuspendType::EnterBackForwardCache))), + DECLARE_NAPI_STATIC_PROPERTY("ENTER_BACK_FORWARD_CACHE", + NapiParseUtils::ToInt32Value(env, static_cast(SuspendType::ENTER_BACK_FORWARD_CACHE))), DECLARE_NAPI_STATIC_PROPERTY( - "EnterBackground", NapiParseUtils::ToInt32Value(env, static_cast(SuspendType::EnterBackground))), + "ENTER_BACKGROUND", NapiParseUtils::ToInt32Value(env, static_cast(SuspendType::ENTER_BACKGROUND))), DECLARE_NAPI_STATIC_PROPERTY( - "AutoCleanup", NapiParseUtils::ToInt32Value(env, static_cast(SuspendType::AutoCleanup))), + "AUTO_CLEANUP", NapiParseUtils::ToInt32Value(env, static_cast(SuspendType::AUTO_CLEANUP))), }; napi_value enumValue = nullptr; diff --git a/ohos_interface/include/ohos_nweb/nweb_native_media_player.h b/ohos_interface/include/ohos_nweb/nweb_native_media_player.h index dffc7edad..c3bd00758 100644 --- a/ohos_interface/include/ohos_nweb/nweb_native_media_player.h +++ b/ohos_interface/include/ohos_nweb/nweb_native_media_player.h @@ -31,7 +31,14 @@ enum class MediaError { NETWORK_ERROR = 1, FORMAT_ERROR, DECODE_ERROR }; enum class ReadyState { HAVE_NOTHING = 0, HAVE_METADATA, HAVE_CURRENT_DATA, HAVE_FUTURE_DATA, HAVE_ENOUGH_DATA }; -enum class SuspendType { EnterBackForwardCache = 0, EnterBackground, AutoCleanup }; +enum class SuspendType { + ENTER_BACK_FORWARD_CACHE = 0, + ENTER_BACKGROUND, + AUTO_CLEANUP, + EnterBackForwardCache = 0, + EnterBackground, + AutoCleanup +}; enum class NetworkState { EMPTY = 0, IDLE, LOADING, NETWORK_ERROR }; -- Gitee From b797bae1c5a72547bf15c43a23aca5d38ae53cdd Mon Sep 17 00:00:00 2001 From: zhangyanchuan Date: Tue, 9 Jul 2024 16:11:08 +0800 Subject: [PATCH 24/32] =?UTF-8?q?=E8=A7=86=E9=A2=91=E6=89=98=E7=AE=A1?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=BF=9BBFCache(=E5=90=8C=E6=AD=A5=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=8E=A5=E5=8F=A3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangyanchuan --- .../include/ohos_nweb/nweb_native_media_player.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/ohos_interface/include/ohos_nweb/nweb_native_media_player.h b/ohos_interface/include/ohos_nweb/nweb_native_media_player.h index c3bd00758..609d9a6bb 100644 --- a/ohos_interface/include/ohos_nweb/nweb_native_media_player.h +++ b/ohos_interface/include/ohos_nweb/nweb_native_media_player.h @@ -31,14 +31,7 @@ enum class MediaError { NETWORK_ERROR = 1, FORMAT_ERROR, DECODE_ERROR }; enum class ReadyState { HAVE_NOTHING = 0, HAVE_METADATA, HAVE_CURRENT_DATA, HAVE_FUTURE_DATA, HAVE_ENOUGH_DATA }; -enum class SuspendType { - ENTER_BACK_FORWARD_CACHE = 0, - ENTER_BACKGROUND, - AUTO_CLEANUP, - EnterBackForwardCache = 0, - EnterBackground, - AutoCleanup -}; +enum class SuspendType { ENTER_BACK_FORWARD_CACHE = 0, ENTER_BACKGROUND, AUTO_CLEANUP }; enum class NetworkState { EMPTY = 0, IDLE, LOADING, NETWORK_ERROR }; -- Gitee From a1973f14752f073a8a79f3d2a2d7c27acc6f71eb Mon Sep 17 00:00:00 2001 From: tengfan3 Date: Tue, 9 Jul 2024 23:29:24 +0800 Subject: [PATCH 25/32] =?UTF-8?q?=E4=BC=A0=E6=84=9F=E5=99=A8=E5=AF=B9?= =?UTF-8?q?=E6=8E=A5=EF=BC=8C=E8=BF=BD=E5=8A=A0=E4=BC=A0=E6=84=9F=E5=99=A8?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tengfan3 --- .../include/sensor_adapter_impl.h | 17 ++ .../src/sensor_adapter_impl.cpp | 213 ++++++++++++++---- 2 files changed, 180 insertions(+), 50 deletions(-) diff --git a/ohos_adapter/sensor_adapter/include/sensor_adapter_impl.h b/ohos_adapter/sensor_adapter/include/sensor_adapter_impl.h index 8d3c78b2b..319e3cb9d 100644 --- a/ohos_adapter/sensor_adapter/include/sensor_adapter_impl.h +++ b/ohos_adapter/sensor_adapter/include/sensor_adapter_impl.h @@ -51,6 +51,23 @@ private: static void OhosSensorCallback(SensorEvent* event); static std::unordered_map> sensorCallbackMap; + static void handleAccelerometerData(std::shared_ptr callback, + SensorEvent* event); + static void handleLinearAccelerometerData(std::shared_ptr callback, + SensorEvent* event); + static void handleGravityData(std::shared_ptr callback, + SensorEvent* event); + static void handleCyroscopeData(std::shared_ptr callback, + SensorEvent* event); + static void handleMagnetometerData(std::shared_ptr callback, + SensorEvent* event); + static void handleOrientationData(std::shared_ptr callback, + SensorEvent* event); + static void handleRotationVectorData(std::shared_ptr callback, + SensorEvent* event); + static void handleGameRotationVectorData(std::shared_ptr callback, + SensorEvent* event); + SensorUser mSensorUser{}; }; diff --git a/ohos_adapter/sensor_adapter/src/sensor_adapter_impl.cpp b/ohos_adapter/sensor_adapter/src/sensor_adapter_impl.cpp index d6f6328f4..e244a5770 100644 --- a/ohos_adapter/sensor_adapter/src/sensor_adapter_impl.cpp +++ b/ohos_adapter/sensor_adapter/src/sensor_adapter_impl.cpp @@ -26,14 +26,18 @@ std::unordered_map> SensorAdapterIm constexpr double NANOSECONDS_IN_SECOND = 1000000000.0; constexpr double DEFAULT_SAMPLE_PERIOD = 200000000.0; -SensorTypeId SensorTypeToOhosSensorType(int sensorTypeId) { +SensorTypeId SensorTypeToOhosSensorType(int sensorTypeId) +{ SensorTypeId ohosSensorTypeId = SENSOR_TYPE_ID_NONE; const static std::map TO_OHOS_SENSOR_TYPE_MAP = { {2 /* ACCELEROMETER */, SENSOR_TYPE_ID_ACCELEROMETER }, {3 /* LINEAR_ACCELERATION */, SENSOR_TYPE_ID_LINEAR_ACCELERATION }, {4 /* GRAVITY */, SENSOR_TYPE_ID_GRAVITY }, {5 /* GYROSCOPE */, SENSOR_TYPE_ID_GYROSCOPE }, - {8 /* ABSOLUTE_ORIENTATION_EULER_ANGLES}*/, SENSOR_TYPE_ID_ORIENTATION } + {6 /* MAGNETOMETER */, SENSOR_TYPE_ID_MAGNETIC_FIELD }, + {8 /* ABSOLUTE_ORIENTATION_EULER_ANGLES}*/, SENSOR_TYPE_ID_ORIENTATION }, + {9 /* ABSOLUTE_ORIENTATION_QUATERNION} */, SENSOR_TYPE_ID_ROTATION_VECTOR }, + {11 /* RELATIVE_ORIENTATION_QUATERNION} */, SENSOR_TYPE_ID_GAME_ROTATION_VECTOR } }; auto checkIter = TO_OHOS_SENSOR_TYPE_MAP.find(sensorTypeId); if (checkIter != TO_OHOS_SENSOR_TYPE_MAP.end()) { @@ -42,13 +46,17 @@ SensorTypeId SensorTypeToOhosSensorType(int sensorTypeId) { return ohosSensorTypeId; } -std::string SensorTypeToSensorUserName(int sensorTypeId) { +std::string SensorTypeToSensorUserName(int sensorTypeId) +{ const static std::map TO_OHOS_SENSOR_USER_NAME_MAP = { {2 /* ACCELEROMETER */, "OhosAccelerometerService" }, {3 /* LINEAR_ACCELERATION */, "OhosLinearAccelerometerService" }, {4 /* GRAVITY */, "OhosGravityService" }, {5 /* GYROSCOPE */, "OhosCyroscopeService" }, - {8 /* ABSOLUTE_ORIENTATION_EULER_ANGLES}*/, "OhosOrientationService" } + {6 /* MAGNETOMETER */, "OhosMagnetometerService" }, + {8 /* ABSOLUTE_ORIENTATION_EULER_ANGLES}*/, "OhosOrientationService" }, + {9 /* ABSOLUTE_ORIENTATION_QUATERNION} */, "OhosRotationVectorService"}, + {11 /* RELATIVE_ORIENTATION_QUATERNION} */, "OhosGameRotationVectorService" } }; std::string userName = "OhosSensorService"; auto checkIter = TO_OHOS_SENSOR_USER_NAME_MAP.find(sensorTypeId); @@ -60,17 +68,19 @@ std::string SensorTypeToSensorUserName(int sensorTypeId) { SensorCallbackImpl::SensorCallbackImpl( std::shared_ptr callbackAdapter) - : callbackAdapter_(callbackAdapter) { -} + : callbackAdapter_(callbackAdapter) +{} void SensorCallbackImpl::UpdateOhosSensorData(double timestamp, - double value1, double value2, double value3, double value4) { + double value1, double value2, double value3, double value4) +{ if (callbackAdapter_) { callbackAdapter_->UpdateOhosSensorData(timestamp, value1, value2, value3, value4); - } + } } -int32_t SensorAdapterImpl::IsOhosSensorSupported(int32_t sensorTypeId) { +int32_t SensorAdapterImpl::IsOhosSensorSupported(int32_t sensorTypeId) +{ int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); if (ohosSensorTypeId != SENSOR_TYPE_ID_NONE) { SensorInfo* sensorInfo = nullptr; @@ -92,7 +102,8 @@ int32_t SensorAdapterImpl::IsOhosSensorSupported(int32_t sensorTypeId) { return SENSOR_ERROR; } -int32_t SensorAdapterImpl::GetOhosSensorReportingMode(int32_t sensorTypeId) { +int32_t SensorAdapterImpl::GetOhosSensorReportingMode(int32_t sensorTypeId) +{ int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); int32_t reportingMode = -1; switch (ohosSensorTypeId) { @@ -100,7 +111,10 @@ int32_t SensorAdapterImpl::GetOhosSensorReportingMode(int32_t sensorTypeId) { case SENSOR_TYPE_ID_GRAVITY: case SENSOR_TYPE_ID_LINEAR_ACCELERATION: case SENSOR_TYPE_ID_GYROSCOPE: + case SENSOR_TYPE_ID_MAGNETIC_FIELD: case SENSOR_TYPE_ID_ORIENTATION: + case SENSOR_TYPE_ID_ROTATION_VECTOR: + case SENSOR_TYPE_ID_GAME_ROTATION_VECTOR: reportingMode = SENSOR_DATA_REPORT_CONTINUOUS; break; default: @@ -109,7 +123,8 @@ int32_t SensorAdapterImpl::GetOhosSensorReportingMode(int32_t sensorTypeId) { return reportingMode; } -double SensorAdapterImpl::GetOhosSensorDefaultSupportedFrequency(int32_t sensorTypeId) { +double SensorAdapterImpl::GetOhosSensorDefaultSupportedFrequency(int32_t sensorTypeId) +{ int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); double defaultFrequency = 0.0; if (ohosSensorTypeId != SENSOR_TYPE_ID_NONE) { @@ -120,7 +135,8 @@ double SensorAdapterImpl::GetOhosSensorDefaultSupportedFrequency(int32_t sensorT return defaultFrequency; } -double SensorAdapterImpl::GetOhosSensorMinSupportedFrequency(int32_t sensorTypeId) { +double SensorAdapterImpl::GetOhosSensorMinSupportedFrequency(int32_t sensorTypeId) +{ int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); double minFrequency = 0.0; if (ohosSensorTypeId == SENSOR_TYPE_ID_NONE) { @@ -148,7 +164,8 @@ double SensorAdapterImpl::GetOhosSensorMinSupportedFrequency(int32_t sensorTypeI return minFrequency; } -double SensorAdapterImpl::GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeId) { +double SensorAdapterImpl::GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeId) +{ int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); double maxFrequency = 0.0; if (ohosSensorTypeId == SENSOR_TYPE_ID_NONE) { @@ -176,60 +193,154 @@ double SensorAdapterImpl::GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeI return maxFrequency; } -void SensorAdapterImpl::OhosSensorCallback(SensorEvent* event) { +void SensorAdapterImpl::handleAccelerometerData(std::shared_ptr callback, + SensorEvent* event) +{ + if ((event == nullptr) || (callback == nullptr)) { + WVLOG_E("handleAccelerometerData Error."); + return; + } + AccelData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); + } +} + +void SensorAdapterImpl::handleLinearAccelerometerData(std::shared_ptr callback, + SensorEvent* event) +{ + if ((event == nullptr) || (callback == nullptr)) { + WVLOG_E("handleLinearAccelerometerData Error."); + return; + } + LinearAccelData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); + } +} + +void SensorAdapterImpl::handleGravityData(std::shared_ptr callback, + SensorEvent* event) +{ + if ((event == nullptr) || (callback == nullptr)) { + WVLOG_E("handleGravityData Error."); + return; + } + GravityData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); + } +} + +void SensorAdapterImpl::handleCyroscopeData(std::shared_ptr callback, + SensorEvent* event) +{ + if ((event == nullptr) || (callback == nullptr)) { + WVLOG_E("handleCyroscopeData Error."); + return; + } + GyroscopeData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); + } +} + +void SensorAdapterImpl::handleMagnetometerData(std::shared_ptr callback, + SensorEvent* event) +{ + if ((event == nullptr) || (callback == nullptr)) { + WVLOG_E("handleMagnetometerData Error."); + return; + } + MagneticFieldData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); + } +} + +void SensorAdapterImpl::handleOrientationData(std::shared_ptr callback, + SensorEvent* event) +{ + if ((event == nullptr) || (callback == nullptr)) { + WVLOG_E("handleOrientationData Error."); + return; + } + OrientationData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->beta, data->gamma, data->alpha, 0.0f); + } +} + +void SensorAdapterImpl::handleRotationVectorData(std::shared_ptr callback, + SensorEvent* event) +{ + if ((event == nullptr) || (callback == nullptr)) { + WVLOG_E("handleRotationVectorData Error."); + return; + } + RotationVectorData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, data->w); + } +} + +void SensorAdapterImpl::handleGameRotationVectorData(std::shared_ptr callback, + SensorEvent* event) +{ + if ((event == nullptr) || (callback == nullptr)) { + WVLOG_E("handleGameRotationVectorData Error."); + return; + } + GameRotationVectorData* data = reinterpret_cast(event->data); + if (data != nullptr) { + callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, data->w); + } +} + +void SensorAdapterImpl::OhosSensorCallback(SensorEvent* event) +{ std::shared_ptr callback = nullptr; auto findIter = sensorCallbackMap.find(event->sensorTypeId); if (findIter != sensorCallbackMap.end()) { - callback = findIter->second; + callback = findIter->second; } if ((event == nullptr) || (callback == nullptr)) { WVLOG_E("OhosSensorCallback Error."); return; } switch (event->sensorTypeId) { - case SENSOR_TYPE_ID_ACCELEROMETER: { - AccelData* data = reinterpret_cast(event->data); - if (data != nullptr) { - callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); - } + case SENSOR_TYPE_ID_ACCELEROMETER: + handleAccelerometerData(callback, event); break; - } - case SENSOR_TYPE_ID_GRAVITY: { - GravityData* data = reinterpret_cast(event->data); - if (data != nullptr) { - callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); - } + case SENSOR_TYPE_ID_GRAVITY: + handleGravityData(callback, event); break; - } - case SENSOR_TYPE_ID_LINEAR_ACCELERATION: { - LinearAccelData* data = reinterpret_cast(event->data); - if (data != nullptr) { - callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); - } + case SENSOR_TYPE_ID_LINEAR_ACCELERATION: + handleLinearAccelerometerData(callback, event); break; - } - case SENSOR_TYPE_ID_GYROSCOPE: { - GyroscopeData* data = reinterpret_cast(event->data); - if (data != nullptr) { - callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f); - } + case SENSOR_TYPE_ID_GYROSCOPE: + handleCyroscopeData(callback, event); break; - } - case SENSOR_TYPE_ID_ORIENTATION: { - OrientationData* data = reinterpret_cast(event->data); - if (data != nullptr) { - callback->UpdateOhosSensorData(event->timestamp, data->beta, data->gamma, data->alpha, 0.0f); - } + case SENSOR_TYPE_ID_MAGNETIC_FIELD: + handleMagnetometerData(callback, event); + break; + case SENSOR_TYPE_ID_ORIENTATION: + handleOrientationData(callback, event); + break; + case SENSOR_TYPE_ID_ROTATION_VECTOR: + handleRotationVectorData(callback, event); + break; + case SENSOR_TYPE_ID_GAME_ROTATION_VECTOR: + handleGameRotationVectorData(callback, event); break; - } default: break; } } -int32_t SensorAdapterImpl::SubscribeOhosSensor(int32_t sensorTypeId, int64_t samplingInterval) { - WVLOG_I("SubscribeOhosSensor sensorTypeId: %{public}d, samplingInterval: %{public}lld", - sensorTypeId, samplingInterval); +int32_t SensorAdapterImpl::SubscribeOhosSensor(int32_t sensorTypeId, int64_t samplingInterval) +{ + WVLOG_I("SubscribeOhosSensor sensorTypeId: %{public}d", sensorTypeId); if (samplingInterval <= 0) { WVLOG_E("SubscribeOhosSensor error, samplingInterval is invalid."); return SENSOR_PARAMETER_ERROR; @@ -269,7 +380,8 @@ int32_t SensorAdapterImpl::SubscribeOhosSensor(int32_t sensorTypeId, int64_t sam } int32_t SensorAdapterImpl::RegistOhosSensorCallback(int32_t sensorTypeId, - std::shared_ptr callbackAdapter) { + std::shared_ptr callbackAdapter) +{ int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); if (ohosSensorTypeId != SENSOR_TYPE_ID_NONE) { auto callback = std::make_shared(callbackAdapter); @@ -280,7 +392,8 @@ int32_t SensorAdapterImpl::RegistOhosSensorCallback(int32_t sensorTypeId, return SENSOR_PARAMETER_ERROR; } -int32_t SensorAdapterImpl::UnsubscribeOhosSensor(int32_t sensorTypeId) { +int32_t SensorAdapterImpl::UnsubscribeOhosSensor(int32_t sensorTypeId) +{ WVLOG_I("UnsubscribeOhosSensor sensorTypeId: %{public}d.", sensorTypeId); int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId); if (ohosSensorTypeId != SENSOR_TYPE_ID_NONE) { -- Gitee From b377185863f98c1d5387aca8f89b64053246fbeb Mon Sep 17 00:00:00 2001 From: tengfan Date: Tue, 9 Jul 2024 21:45:47 +0800 Subject: [PATCH 26/32] trim memory by pressure level Signed-off-by: tengfan --- .../napi_webview_controller.cpp | 43 +++++++++++++++++++ .../napi_webview_controller.h | 3 ++ .../webviewcontroller/webview_controller.h | 5 +++ .../include/ohos_nweb/nweb_engine.h | 2 + .../bridge/webcore/ark_web_engine_impl.cpp | 5 +++ .../bridge/webcore/ark_web_engine_impl.h | 2 + .../bridge/webview/ark_web_engine_wrapper.cpp | 5 +++ .../bridge/webview/ark_web_engine_wrapper.h | 2 + .../ohos_nweb/include/ark_web_engine.h | 3 ++ ohos_nweb/include/nweb_helper.h | 2 + ohos_nweb/src/nweb_helper.cpp | 10 +++++ 11 files changed, 82 insertions(+) diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp index 87ec385a1..c2de4111f 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp @@ -529,6 +529,8 @@ napi_value NapiWebviewController::Init(napi_env env, napi_value exports) NapiWebviewController::SetPathAllowingUniversalAccess), DECLARE_NAPI_STATIC_FUNCTION("enableBackForwardCache", NapiWebviewController::EnableBackForwardCache), DECLARE_NAPI_FUNCTION("setBackForwardCacheOptions", NapiWebviewController::SetBackForwardCacheOptions), + DECLARE_NAPI_STATIC_FUNCTION("trimMemoryByPressureLevel", + NapiWebviewController::TrimMemoryByPressureLevel), }; napi_value constructor = nullptr; napi_define_class(env, WEBVIEW_CONTROLLER_CLASS_NAME.c_str(), WEBVIEW_CONTROLLER_CLASS_NAME.length(), @@ -712,6 +714,18 @@ napi_value NapiWebviewController::Init(napi_env env, napi_value exports) sizeof(offlineResourceTypeProperties[0]), offlineResourceTypeProperties, &offlineResourceTypeEnum); napi_set_named_property(env, exports, OFFLINE_RESOURCE_TYPE_ENUM_NAME.c_str(), offlineResourceTypeEnum); + napi_value pressureLevelEnum = nullptr; + napi_property_descriptor pressureLevelProperties[] = { + DECLARE_NAPI_STATIC_PROPERTY("MEMORY_PRESSURE_LEVEL_MODERATE", NapiParseUtils::ToInt32Value(env, + static_cast(PressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE))), + DECLARE_NAPI_STATIC_PROPERTY("MEMORY_PRESSURE_LEVEL_CRITICAL", NapiParseUtils::ToInt32Value(env, + static_cast(PressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL))), + }; + napi_define_class(env, WEB_PRESSURE_LEVEL_ENUM_NAME.c_str(), WEB_PRESSURE_LEVEL_ENUM_NAME.length(), + NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(pressureLevelProperties) / + sizeof(pressureLevelProperties[0]), pressureLevelProperties, &pressureLevelEnum); + napi_set_named_property(env, exports, WEB_PRESSURE_LEVEL_ENUM_NAME.c_str(), pressureLevelEnum); + WebviewJavaScriptExecuteCallback::InitJSExcute(env, exports); return exports; } @@ -6172,5 +6186,34 @@ napi_value NapiWebviewController::SetPathAllowingUniversalAccess( } return result; } + +napi_value NapiWebviewController::TrimMemoryByPressureLevel(napi_env env, + napi_callback_info info) +{ + napi_value thisVar = nullptr; + napi_value result = nullptr; + size_t argc = INTEGER_ONE; + napi_value argv[INTEGER_ONE] = { 0 }; + int32_t memoryLevel; + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + if (argc != INTEGER_ONE) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString( + ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); + return result; + } + + if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], memoryLevel)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, + "PressureLevel", "number")); + return result; + } + + memoryLevel = memoryLevel == 1 ? 0 : memoryLevel; + NWebHelper::Instance().TrimMemoryByPressureLevel(memoryLevel); + NAPI_CALL(env, napi_get_undefined(env, &result)); + return result; +} } // namespace NWeb } // namespace OHOS diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h index 5877ee94e..5a0c1ae91 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h @@ -40,6 +40,7 @@ const std::string WEB_PRINT_DOCUMENT_CLASS_NAME = "WebPrintDocument"; const std::string WEB_SECURITY_LEVEL_ENUM_NAME = "SecurityLevel"; const std::string WEB_RENDER_PROCESS_MODE_ENUM_NAME = "RenderProcessMode"; const std::string OFFLINE_RESOURCE_TYPE_ENUM_NAME = "OfflineResourceType"; +const std::string WEB_PRESSURE_LEVEL_ENUM_NAME = "PressureLevel"; struct Scheme { std::string name; @@ -352,6 +353,8 @@ private: static napi_value SetBackForwardCacheOptions(napi_env env, napi_callback_info info); + static napi_value TrimMemoryByPressureLevel(napi_env env, napi_callback_info info); + static int32_t maxFdNum_; static std::atomic usedFd_; }; diff --git a/interfaces/kits/napi/webviewcontroller/webview_controller.h b/interfaces/kits/napi/webviewcontroller/webview_controller.h index 508d8d160..587649950 100644 --- a/interfaces/kits/napi/webviewcontroller/webview_controller.h +++ b/interfaces/kits/napi/webviewcontroller/webview_controller.h @@ -117,6 +117,11 @@ enum class UrlListSetResult : int { SET_OK = 0, }; +enum class PressureLevel : int { + MEMORY_PRESSURE_LEVEL_MODERATE = 1, + MEMORY_PRESSURE_LEVEL_CRITICAL = 2, +}; + class WebPrintDocument; class WebviewController { public: diff --git a/ohos_interface/include/ohos_nweb/nweb_engine.h b/ohos_interface/include/ohos_nweb/nweb_engine.h index 41853edc0..616b166fc 100644 --- a/ohos_interface/include/ohos_nweb/nweb_engine.h +++ b/ohos_interface/include/ohos_nweb/nweb_engine.h @@ -82,6 +82,8 @@ public: virtual std::shared_ptr GetAdsBlockManager() { return nullptr; }; + + virtual void TrimMemoryByPressureLevel(int32_t memoryLevel) {}; }; } // namespace OHOS::NWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp index 5b55bcb92..ecc4dba41 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp @@ -212,4 +212,9 @@ ArkWebRefPtr ArkWebEngineImpl::GetAdsBlockManager() } return new ArkWebAdsBlockManagerImpl(nweb_adsBlock_manager); } + +void ArkWebEngineImpl::TrimMemoryByPressureLevel(int32_t memoryLevel) +{ + nweb_engine_->TrimMemoryByPressureLevel(memoryLevel); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h index 28236ab74..b883c6994 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h @@ -79,6 +79,8 @@ public: ArkWebRefPtr GetAdsBlockManager() override; + void TrimMemoryByPressureLevel(int32_t memoryLevel) override; + private: std::shared_ptr nweb_engine_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp index c08317fa5..5c26e0755 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp @@ -240,4 +240,9 @@ ArkWebEngineWrapper::GetAdsBlockManager() { return std::make_shared(ark_web_adsblock_manager); } + +void ArkWebEngineWrapper::TrimMemoryByPressureLevel(int32_t memoryLevel) +{ + ark_web_engine_->TrimMemoryByPressureLevel(memoryLevel); +} } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h index 6400bfcdd..ec6ea672a 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h @@ -76,6 +76,8 @@ public: void EnableWholeWebPageDrawing() override; std::shared_ptr GetAdsBlockManager() override; + + void TrimMemoryByPressureLevel(int32_t memoryLevel) override; private: ArkWebRefPtr ark_web_engine_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h index ab6194dcf..3208fffdc 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h +++ b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h @@ -108,6 +108,9 @@ public: /*--ark web()--*/ virtual ArkWebRefPtr GetAdsBlockManager() = 0; + + /*--ark web()--*/ + virtual void TrimMemoryByPressureLevel(int32_t memoryLevel) = 0; }; } // namespace OHOS::ArkWeb diff --git a/ohos_nweb/include/nweb_helper.h b/ohos_nweb/include/nweb_helper.h index 5654b55d0..d09266bd3 100644 --- a/ohos_nweb/include/nweb_helper.h +++ b/ohos_nweb/include/nweb_helper.h @@ -88,6 +88,8 @@ public: void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaTakeOver); + void TrimMemoryByPressureLevel(int32_t memoryLevel); + private: NWebHelper() = default; bool LoadLib(bool from_ark); diff --git a/ohos_nweb/src/nweb_helper.cpp b/ohos_nweb/src/nweb_helper.cpp index ef78f8f2b..d2d633f03 100644 --- a/ohos_nweb/src/nweb_helper.cpp +++ b/ohos_nweb/src/nweb_helper.cpp @@ -1133,6 +1133,16 @@ std::shared_ptr NWebHelper::GetAdsBlockManager() return nwebEngine_->GetAdsBlockManager(); } +void NWebHelper::TrimMemoryByPressureLevel(int32_t memoryLevel) +{ + if (nwebEngine_ == nullptr) { + WVLOG_E("nweb engine is nullptr"); + return; + } + + nwebEngine_->TrimMemoryByPressureLevel(memoryLevel); +} + NWebAdapterHelper &NWebAdapterHelper::Instance() { static NWebAdapterHelper helper; -- Gitee From 6c37b14fa234169db8215f9b964426fe3490fec9 Mon Sep 17 00:00:00 2001 From: echoorchid Date: Mon, 8 Jul 2024 15:30:30 +0800 Subject: [PATCH 27/32] add log for OnActive and OnInactive Signed-off-by: echoorchid --- .../kits/napi/webviewcontroller/napi_webview_controller.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp index 87ec385a1..cdb2288e8 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp @@ -1152,11 +1152,12 @@ napi_value NapiWebviewController::OnActive(napi_env env, napi_callback_info info napi_value result = nullptr; WebviewController *webviewController = GetWebviewController(env, info); if (!webviewController) { + WVLOG_E("NapiWebviewController::OnActive get controller failed"); return nullptr; } webviewController->OnActive(); - WVLOG_D("The web component has been successfully activated"); + WVLOG_I("The web component has been successfully activated"); NAPI_CALL(env, napi_get_undefined(env, &result)); return result; } @@ -1166,11 +1167,12 @@ napi_value NapiWebviewController::OnInactive(napi_env env, napi_callback_info in napi_value result = nullptr; WebviewController *webviewController = GetWebviewController(env, info); if (!webviewController) { + WVLOG_E("NapiWebviewController::OnInactive get controller failed"); return nullptr; } webviewController->OnInactive(); - WVLOG_D("The web component has been successfully inactivated"); + WVLOG_I("The web component has been successfully inactivated"); NAPI_CALL(env, napi_get_undefined(env, &result)); return result; } -- Gitee From ad66eb63044fae2dd98fe89e3a0074d4fe732639 Mon Sep 17 00:00:00 2001 From: kirby Date: Wed, 10 Jul 2024 14:08:46 +0800 Subject: [PATCH 28/32] fix null ptr err Signed-off-by: kirby --- interfaces/kits/cj/src/web_cookie_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/cj/src/web_cookie_manager.cpp b/interfaces/kits/cj/src/web_cookie_manager.cpp index 5810abfcc..8956d29de 100644 --- a/interfaces/kits/cj/src/web_cookie_manager.cpp +++ b/interfaces/kits/cj/src/web_cookie_manager.cpp @@ -31,7 +31,7 @@ std::string WebCookieManager::CjGetCookie(const std::string &url, bool incognito } if (cookieContent == "" && !isValid) { errCode = NWebError::INVALID_URL; - return nullptr; + return ""; } errCode = NWebError::NO_ERROR; return cookieContent; -- Gitee From 532daa7fbb4cf4053985cb305be4e8a22343417f Mon Sep 17 00:00:00 2001 From: zhengenhao0 Date: Wed, 10 Jul 2024 15:05:59 +0800 Subject: [PATCH 29/32] update NWeb.hap in master to 0710 Signed-off-by: zhengenhao0 --- ohos_nweb/prebuilts/arm/NWeb.hap | 4 ++-- ohos_nweb/prebuilts/arm64/NWeb.hap | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ohos_nweb/prebuilts/arm/NWeb.hap b/ohos_nweb/prebuilts/arm/NWeb.hap index 2916f731b..e1f086df3 100644 --- a/ohos_nweb/prebuilts/arm/NWeb.hap +++ b/ohos_nweb/prebuilts/arm/NWeb.hap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:98f54394d9240f9a37c4fef8f799fe876aac74a86d70a74eba262ca59931dbda -size 79497971 +oid sha256:683b1427ef008ab1046561128138933a52d3188709659fbe34b60c7e921156f2 +size 122977436 diff --git a/ohos_nweb/prebuilts/arm64/NWeb.hap b/ohos_nweb/prebuilts/arm64/NWeb.hap index da5358b4d..e82ba550b 100644 --- a/ohos_nweb/prebuilts/arm64/NWeb.hap +++ b/ohos_nweb/prebuilts/arm64/NWeb.hap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:bb1c4bf27de592f9096fcc03d95ac5213ab388d144783ee326df17ac586f59bb -size 90255633 +oid sha256:f7063bd3158146f488df3794da6148cf24a9c74deb56293a31499c664e03d512 +size 190665927 -- Gitee From d7c73bc2f970706f555928d239a0c8df9a721a3b Mon Sep 17 00:00:00 2001 From: LXinJie Date: Wed, 10 Jul 2024 17:45:15 +0800 Subject: [PATCH 30/32] liuxinjie6@huawei.com Signed-off-by: LXinJie --- .../src/aafwk_render_scheduler_impl.cpp | 2 +- .../include/hitrace_adapter_impl.h | 7 ---- .../src/hitrace_adapter_impl.cpp | 32 ++++++++----------- .../nweb_aafwk_adapter_test.cpp | 2 ++ .../hiviewdfx_adapter_test/BUILD.gn | 1 + .../hiviewdfx_adapter_test.cpp | 12 +++---- 6 files changed, 24 insertions(+), 32 deletions(-) diff --git a/ohos_adapter/aafwk_adapter/src/aafwk_render_scheduler_impl.cpp b/ohos_adapter/aafwk_adapter/src/aafwk_render_scheduler_impl.cpp index c4785d8e5..d95ea28d0 100644 --- a/ohos_adapter/aafwk_adapter/src/aafwk_render_scheduler_impl.cpp +++ b/ohos_adapter/aafwk_adapter/src/aafwk_render_scheduler_impl.cpp @@ -34,7 +34,7 @@ void AafwkRenderSchedulerImpl::NotifyBrowserFd( return; } if (browser == nullptr) { - WVLOG_E("browser is nullptr!"); + WVLOG_D("NotifyBrowserFd for render process."); renderSchedulerHostAdapter_->NotifyBrowser(ipcFd, sharedFd, crashFd, nullptr); } else { sptr browserHost = iface_cast(browser); diff --git a/ohos_adapter/hiviewdfx_adapter/include/hitrace_adapter_impl.h b/ohos_adapter/hiviewdfx_adapter/include/hitrace_adapter_impl.h index 227957c6d..97cde921f 100644 --- a/ohos_adapter/hiviewdfx_adapter/include/hitrace_adapter_impl.h +++ b/ohos_adapter/hiviewdfx_adapter/include/hitrace_adapter_impl.h @@ -43,14 +43,7 @@ public: void CountOHOSTrace(const std::string& name, int64_t count) override; - void UpdateOHOSTraceTag(const char* value); - bool IsACETraceEnable() override; - -private: - bool firstAceEnable_ = false; - bool isNWEBTraceEnable_ = false; - bool isOHOSTraceEnable_ = false; }; } // namespace OHOS::NWeb diff --git a/ohos_adapter/hiviewdfx_adapter/src/hitrace_adapter_impl.cpp b/ohos_adapter/hiviewdfx_adapter/src/hitrace_adapter_impl.cpp index 8b9a6b36f..62faf1a01 100644 --- a/ohos_adapter/hiviewdfx_adapter/src/hitrace_adapter_impl.cpp +++ b/ohos_adapter/hiviewdfx_adapter/src/hitrace_adapter_impl.cpp @@ -63,46 +63,42 @@ bool HiTraceAdapterImpl::IsHiTraceEnable() int changed = 0; const char *enable = CachedParameterGetChanged(g_Handle, &changed); uint64_t tags = static_cast(ConvertToInt(enable, 0)); - firstAceEnable_ = tags & HITRACE_TAG_ACE; return (tags & HITRACE_TAG_NWEB); } void HiTraceAdapterImpl::StartOHOSTrace(const std::string& value, float limit) { - if (isOHOSTraceEnable_) { - ::StartTrace(HITRACE_TAG_ACE, value, limit); - } else if (isNWEBTraceEnable_) { + if (IsHiTraceEnable()) { ::StartTrace(HITRACE_TAG_NWEB, value, limit); + } else if (IsACETraceEnable()) { + ::StartTrace(HITRACE_TAG_ACE, value, limit); } } void HiTraceAdapterImpl::FinishOHOSTrace() { - if (isOHOSTraceEnable_) { - ::FinishTrace(HITRACE_TAG_ACE); - } else if (isNWEBTraceEnable_) { + if (IsHiTraceEnable()) { ::FinishTrace(HITRACE_TAG_NWEB); + } else if (IsACETraceEnable()) { + ::FinishTrace(HITRACE_TAG_ACE); } } void HiTraceAdapterImpl::CountOHOSTrace(const std::string& name, int64_t count) { - if (isOHOSTraceEnable_) { - ::CountTrace(HITRACE_TAG_ACE, name, count); - } else if (isNWEBTraceEnable_) { + if (IsHiTraceEnable()) { ::CountTrace(HITRACE_TAG_NWEB, name, count); + } else if (IsACETraceEnable()) { + ::CountTrace(HITRACE_TAG_ACE, name, count); } } -void HiTraceAdapterImpl::UpdateOHOSTraceTag(const char* value) -{ - auto status = std::stoul(value); - isNWEBTraceEnable_ = status & HITRACE_TAG_NWEB; - isOHOSTraceEnable_ = status & HITRACE_TAG_ACE; -} - bool HiTraceAdapterImpl::IsACETraceEnable() { - return firstAceEnable_; + static CachedHandle g_Handle = CachedParameterCreate("debug.hitrace.tags.enableflags", "0"); + int changed = 0; + const char *enable = CachedParameterGetChanged(g_Handle, &changed); + uint64_t tags = static_cast(ConvertToInt(enable, 0)); + return (tags & HITRACE_TAG_ACE); } } // namespace OHOS::NWeb diff --git a/test/unittest/aafwk_app_client_adapter/nweb_aafwk_adapter_test.cpp b/test/unittest/aafwk_app_client_adapter/nweb_aafwk_adapter_test.cpp index 6cc55f1dc..abddfdb86 100644 --- a/test/unittest/aafwk_app_client_adapter/nweb_aafwk_adapter_test.cpp +++ b/test/unittest/aafwk_app_client_adapter/nweb_aafwk_adapter_test.cpp @@ -490,6 +490,8 @@ HWTEST_F(NWebAafwkAdapterTest, NWebAafwkAdapter_browserHost_020, TestSize.Level1 MessageParcel reply; MessageOption option; host->OnRemoteRequest(code, data, reply, option); + code = 100; + host->OnRemoteRequest(code, data, reply, option); } /** diff --git a/test/unittest/ohos_adapter/hiviewdfx_adapter_test/BUILD.gn b/test/unittest/ohos_adapter/hiviewdfx_adapter_test/BUILD.gn index e36c4bced..68f70db25 100644 --- a/test/unittest/ohos_adapter/hiviewdfx_adapter_test/BUILD.gn +++ b/test/unittest/ohos_adapter/hiviewdfx_adapter_test/BUILD.gn @@ -39,6 +39,7 @@ ohos_unittest("nweb_hiviewdfx_adapter_test") { "hilog:libhilog", "hisysevent:libhisysevent", "hitrace:hitrace_meter", + "init:libbegetutil", ] } diff --git a/test/unittest/ohos_adapter/hiviewdfx_adapter_test/hiviewdfx_adapter_test.cpp b/test/unittest/ohos_adapter/hiviewdfx_adapter_test/hiviewdfx_adapter_test.cpp index 8b27bc9c6..08bfbc64f 100644 --- a/test/unittest/ohos_adapter/hiviewdfx_adapter_test/hiviewdfx_adapter_test.cpp +++ b/test/unittest/ohos_adapter/hiviewdfx_adapter_test/hiviewdfx_adapter_test.cpp @@ -18,6 +18,9 @@ #include "ohos_adapter_helper.h" #include "hitrace_adapter_impl.h" +#include "parameters.h" +using namespace OHOS; + using testing::ext::TestSize; namespace OHOS::NWeb { @@ -88,15 +91,12 @@ HWTEST(HiViewDFXAdapterTest, NormalScene, TestSize.Level1) */ HWTEST(HiViewDFXAdapterTest, NormalScene_01, TestSize.Level1) { - uint64_t val = (1ULL << 24); - std::string value = std::to_string(val); - HiTraceAdapterImpl::GetInstance().UpdateOHOSTraceTag(value.c_str()); OhosAdapterHelper::GetInstance().GetHiTraceAdapterInstance().StartOHOSTrace("test"); OhosAdapterHelper::GetInstance().GetHiTraceAdapterInstance().CountOHOSTrace("test", 1); OhosAdapterHelper::GetInstance().GetHiTraceAdapterInstance().FinishOHOSTrace(); - val = (1ULL << 39); - value = std::to_string(val); - HiTraceAdapterImpl::GetInstance().UpdateOHOSTraceTag(value.c_str()); + uint64_t val = (1ULL << 24); + std::string value = std::to_string(val); + OHOS::system::SetParameter("debug.hitrace.tags.enableflags", value); OhosAdapterHelper::GetInstance().GetHiTraceAdapterInstance().StartOHOSTrace("test"); OhosAdapterHelper::GetInstance().GetHiTraceAdapterInstance().CountOHOSTrace("test", 1); OhosAdapterHelper::GetInstance().GetHiTraceAdapterInstance().FinishOHOSTrace(); -- Gitee From d83d31b4604192692d7c79d98df7108e3652a42f Mon Sep 17 00:00:00 2001 From: zhaodan_123 Date: Thu, 11 Jul 2024 10:21:09 +0800 Subject: [PATCH 31/32] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=80=9A=E8=BF=87Guid?= =?UTF-8?q?=E8=8E=B7=E5=8F=96ItemState=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhaodan_123 Change-Id: Ib712ef8df0f475f1ba0ad3e00d6f6b970b80e2a6 Signed-off-by: zhaodan_123 --- .../napi/webviewcontroller/napi_web_download_item.cpp | 9 +++++---- ohos_nweb/include/nweb_c_api.h | 2 ++ ohos_nweb/src/nweb_helper.cpp | 11 ++++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/interfaces/kits/napi/webviewcontroller/napi_web_download_item.cpp b/interfaces/kits/napi/webviewcontroller/napi_web_download_item.cpp index 0c9742d58..a912fbdf9 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_web_download_item.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_web_download_item.cpp @@ -513,8 +513,8 @@ napi_value NapiWebDownloadItem::JS_Pause(napi_env env, napi_callback_info cbinfo WVLOG_E("[DOWNLOAD] unwrap webDownloadItem failed"); return nullptr; } - NWebDownloadItemState state = WebDownload_GetItemState( - webDownloadItem->nwebId, webDownloadItem->webDownloadId); + NWebDownloadItemState state = WebDownload_GetItemStateByGuid(webDownloadItem->guid); + WVLOG_D("[DOWNLOAD] pause state %{public}d", static_cast(state)); if (state != NWebDownloadItemState::IN_PROGRESS && state != NWebDownloadItemState::PENDING) { BusinessError::ThrowErrorByErrcode(env, DOWNLOAD_NOT_START); @@ -545,8 +545,8 @@ napi_value NapiWebDownloadItem::JS_Resume(napi_env env, napi_callback_info cbinf return nullptr; } - NWebDownloadItemState state = WebDownload_GetItemState( - webDownloadItem->nwebId, webDownloadItem->webDownloadId); + NWebDownloadItemState state = WebDownload_GetItemStateByGuid(webDownloadItem->guid); + WVLOG_D("[DOWNLOAD] resume state %{public}d", static_cast(state)); if (state != NWebDownloadItemState::PAUSED) { BusinessError::ThrowErrorByErrcode(env, DOWNLOAD_NOT_PAUSED); return nullptr; @@ -614,6 +614,7 @@ napi_value NapiWebDownloadItem::JS_Start(napi_env env, napi_callback_info cbinfo return nullptr; } webDownloadItem->downloadPath = std::string(stringValue); + WVLOG_D("NapiWebDownloadItem::JS_Start, download_path: %s", webDownloadItem->downloadPath.c_str()); WebDownload_Continue(webDownloadItem->before_download_callback, webDownloadItem->downloadPath.c_str()); return nullptr; } diff --git a/ohos_nweb/include/nweb_c_api.h b/ohos_nweb/include/nweb_c_api.h index 91ddad022..933770ed4 100644 --- a/ohos_nweb/include/nweb_c_api.h +++ b/ohos_nweb/include/nweb_c_api.h @@ -75,6 +75,8 @@ NWEB_EXPORT void WebDownload_Resume(const WebDownloadItemCallbackWrapper *wrappe NWEB_EXPORT NWebDownloadItemState WebDownload_GetItemState(int32_t nwebId, long downloadItemId); +NWEB_EXPORT NWebDownloadItemState WebDownload_GetItemStateByGuid(const std::string& guid); + NWEB_EXPORT void WebDownloadItem_CreateWebDownloadItem(NWebDownloadItem **downloadItem); NWEB_EXPORT void WebDownloadItem_Destroy(NWebDownloadItem *downloadItem); diff --git a/ohos_nweb/src/nweb_helper.cpp b/ohos_nweb/src/nweb_helper.cpp index d2d633f03..432995c87 100644 --- a/ohos_nweb/src/nweb_helper.cpp +++ b/ohos_nweb/src/nweb_helper.cpp @@ -73,6 +73,7 @@ static bool g_isFirstTimeStartUp = false; DO(WebDownload_Pause) \ DO(WebDownload_Resume) \ DO(WebDownload_GetItemState) \ + DO(WebDownload_GetItemStateByGuid) \ DO(WebDownloadItem_Guid) \ DO(WebDownloadItem_GetDownloadItemId) \ DO(WebDownloadItem_GetState) \ @@ -279,12 +280,20 @@ extern "C" void WebDownload_Resume(const WebDownloadItemCallbackWrapper *wrapper extern "C" NWebDownloadItemState WebDownload_GetItemState(int32_t nwebId, long downloadItemId) { - if (!g_nwebCApi->impl_WebDownload_GetItemState) { + if (!g_nwebCApi || !g_nwebCApi->impl_WebDownload_GetItemState) { return NWebDownloadItemState::MAX_DOWNLOAD_STATE; } return g_nwebCApi->impl_WebDownload_GetItemState(nwebId, downloadItemId); } +extern "C" NWebDownloadItemState WebDownload_GetItemStateByGuid(const std::string& guid) +{ + if (!g_nwebCApi || !g_nwebCApi->impl_WebDownload_GetItemStateByGuid) { + return NWebDownloadItemState::MAX_DOWNLOAD_STATE; + } + return g_nwebCApi->impl_WebDownload_GetItemStateByGuid(guid); +} + extern "C" char *WebDownloadItem_Guid(const NWebDownloadItem *downloadItem) { if (!g_nwebCApi->impl_WebDownloadItem_Guid) { -- Gitee From 64302f1d97650aec730511b2e7140aa9989f677c Mon Sep 17 00:00:00 2001 From: zhanggefei Date: Fri, 12 Jul 2024 09:08:20 +0800 Subject: [PATCH 32/32] fix a series of problem about malloc-free Change-Id: I3dbf9ba41a299b9e21ec3fbc959734d1a9ee0c6a --- interfaces/kits/cj/include/web_errors.h | 2 +- interfaces/kits/cj/src/web_data_base.cpp | 59 +++++++++++++++--------- interfaces/kits/cj/src/web_errors.cpp | 4 +- interfaces/kits/cj/src/webview_ffi.cpp | 14 +++--- 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/interfaces/kits/cj/include/web_errors.h b/interfaces/kits/cj/include/web_errors.h index c17111404..77b789254 100644 --- a/interfaces/kits/cj/include/web_errors.h +++ b/interfaces/kits/cj/include/web_errors.h @@ -47,7 +47,7 @@ constexpr ErrCode REGISTER_CUSTOM_SCHEME_FAILED = 17100020; constexpr ErrCode RESOURCE_HANDLER_INVALID = 17100021; constexpr ErrCode HTTP_BODY_STREAN_INIT_FAILED = 17100022; -constexpr ErrCode HTTP_AUTH_NOT_FOUND = 17100023; +constexpr ErrCode HTTP_AUTH_MALLOC_FAILED = 17100023; std::string GetErrMsgByErrCode(ErrCode code); } diff --git a/interfaces/kits/cj/src/web_data_base.cpp b/interfaces/kits/cj/src/web_data_base.cpp index 0d04b1bde..07b29a838 100644 --- a/interfaces/kits/cj/src/web_data_base.cpp +++ b/interfaces/kits/cj/src/web_data_base.cpp @@ -17,47 +17,64 @@ #include "nweb_data_base.h" #include "nweb_helper.h" #include "web_errors.h" -#include - +#include "webview_utils.h" +#include "webview_log.h" +#include "string.h" namespace OHOS { namespace NWeb { +const int DEFAULT_AUTH_LENGTH = 2; +const int HTTP_AUTH_INIT_LENGTH = -1; CArrString WebDataBase::CJGetHttpAuthCredentials(const std::string &host, const std::string &realm) { - // 1 create information for auth name and password; - struct CArrString ret; + CArrString ret = {nullptr, HTTP_AUTH_INIT_LENGTH}; std::string username_s; char password[MAX_PWD_LENGTH + 1] = {0}; - - // get web database instance; + + char** result = static_cast(malloc(sizeof(char*) * DEFAULT_AUTH_LENGTH)); + if (result == nullptr) { + WEBVIEWLOGI("Webdatabase getHttpAuthCredentials malloc result failed!"); + return ret; + } + + result[0] = static_cast(malloc(sizeof(char) * (MAX_STRING_LENGTH + 1))); + if (result[0] == nullptr) { + WEBVIEWLOGI("Webdatabase getHttpAuthCredentials malloc username failed!"); + free(result); + return ret; + } + + result[1] = static_cast(malloc(sizeof(char) * (MAX_PWD_LENGTH + 1))); + if (result[1] == nullptr) { + WEBVIEWLOGI("Webdatabase getHttpAuthCredentials malloc password failed!"); + free(result[0]); + free(result); + return ret; + } + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); if (database != nullptr) { database->GetHttpAuthCredentials(host, realm, username_s, password, MAX_PWD_LENGTH + 1); } - // 2 params check about username and password; - // if get no information for auth name or password, it will return result with nullptr; if (username_s.empty() || strlen(password) == 0) { - ret.head = nullptr; ret.size = 0; return ret; } - // 3 make result; - char** result; - result = (char**)malloc(sizeof(char*) * 2); - - result[0] = (char*)malloc(sizeof(char) * MAX_STRING_LENGTH); - result[1] = (char*)malloc(sizeof(char) * MAX_PWD_LENGTH); + result[0] = OHOS::Webview::MallocCString(username_s); + if (result[0] == nullptr) { + WEBVIEWLOGI("Webdatabase getHttpAuthCredentials transfer username_s failed!"); + for (int i = 0; i < DEFAULT_AUTH_LENGTH; i++) + free(result[i]); + free(result); + return ret; + } - strcpy(result[0], username_s.c_str()); - strcpy(result[1], password); - + result[1] = std::char_traits::copy(result[1], password, MAX_PWD_LENGTH); ret.head = result; - ret.size = 2; - - // 4 return; + ret.size = DEFAULT_AUTH_LENGTH; return ret; } diff --git a/interfaces/kits/cj/src/web_errors.cpp b/interfaces/kits/cj/src/web_errors.cpp index 9ec956ead..2799bfbf3 100644 --- a/interfaces/kits/cj/src/web_errors.cpp +++ b/interfaces/kits/cj/src/web_errors.cpp @@ -41,7 +41,7 @@ const std::string DOWNLOAD_NOT_START_MSG = "The download task is not started yet const std::string REGISTER_CUSTOM_SCHEME_FAILED_MSG = "Failed to register custom schemes."; const std::string HTTP_BODY_STREAN_INIT_FAILED_MSG = "Failed to initialize the HTTP body stream."; const std::string RESOURCE_HANDLER_INVALID_MSG = "The resource handler is invalid."; -const std::string HTTP_AUTH_NOT_FOUND_MSG = "Failed to find http auth credential in web database."; +const std::string HTTP_AUTH_MALLOC_FAILED_MSG = "Failed to malloc string memory to get HttpAuth."; } namespace OHOS { @@ -68,7 +68,7 @@ std::unordered_map g_errCodeMsgMap = { {REGISTER_CUSTOM_SCHEME_FAILED, REGISTER_CUSTOM_SCHEME_FAILED_MSG}, {HTTP_BODY_STREAN_INIT_FAILED, HTTP_BODY_STREAN_INIT_FAILED_MSG}, {RESOURCE_HANDLER_INVALID, RESOURCE_HANDLER_INVALID_MSG}, - {HTTP_AUTH_NOT_FOUND, HTTP_AUTH_NOT_FOUND_MSG}, + {HTTP_AUTH_MALLOC_FAILED, HTTP_AUTH_MALLOC_FAILED_MSG}, }; std::string GetErrMsgByErrCode(ErrCode code) diff --git a/interfaces/kits/cj/src/webview_ffi.cpp b/interfaces/kits/cj/src/webview_ffi.cpp index f8bfc8e77..79266fed4 100644 --- a/interfaces/kits/cj/src/webview_ffi.cpp +++ b/interfaces/kits/cj/src/webview_ffi.cpp @@ -770,13 +770,11 @@ extern "C" { std::string host_s = std::string(host); std::string realm_s = std::string(realm); - struct CArrString result = OHOS::NWeb::WebDataBase::CJGetHttpAuthCredentials(host_s, realm_s); - struct RetDataCArrString ret; + CArrString result = OHOS::NWeb::WebDataBase::CJGetHttpAuthCredentials(host_s, realm_s); + RetDataCArrString ret; - // judge the return result size; - // if size = 0, return null string array; - if (result.size == 0) { - ret.code = NWebError::HTTP_AUTH_NOT_FOUND; + if (result.size == -1) { + ret.code = NWebError::HTTP_AUTH_MALLOC_FAILED; } else { ret.code = NWebError::NO_ERROR; @@ -793,7 +791,7 @@ extern "C" { std::string username_s = std::string(username); std::string password_s = std::string(password); - return OHOS::NWeb::WebDataBase::CJSaveHttpAuthCredentials(host_s, realm_s, username_s, password_s); + OHOS::NWeb::WebDataBase::CJSaveHttpAuthCredentials(host_s, realm_s, username_s, password_s); } bool FfiOHOSDBExistHttpAuthCredentials() @@ -803,7 +801,7 @@ extern "C" { void FfiOHOSDBDeleteHttpAuthCredentials() { - return OHOS::NWeb::WebDataBase::CJDeleteHttpAuthCredentials(); + OHOS::NWeb::WebDataBase::CJDeleteHttpAuthCredentials(); } } } -- Gitee