From 484d6290e8c95e1dead8a9df6cf39c90f53c0344 Mon Sep 17 00:00:00 2001 From: fenggechang Date: Tue, 19 Aug 2025 15:41:21 +0800 Subject: [PATCH 1/2] web configuring window orientation Signed-off-by: fenggechang --- ohos_nweb/BUILD.gn | 1 + ohos_nweb/include/nweb_config_helper.h | 1 + ohos_nweb/src/nweb_config_helper.cpp | 59 +++++++++++ .../nweb_config_helper_test.cpp | 99 +++++++++++++++++++ 4 files changed, 160 insertions(+) diff --git a/ohos_nweb/BUILD.gn b/ohos_nweb/BUILD.gn index b89eb40b6..0062b6774 100644 --- a/ohos_nweb/BUILD.gn +++ b/ohos_nweb/BUILD.gn @@ -104,6 +104,7 @@ ohos_shared_library("libnweb") { ] external_deps = [ + "ability_runtime:app_context", "ability_runtime:ability_manager", "ability_runtime:app_context", "ability_runtime:app_manager", diff --git a/ohos_nweb/include/nweb_config_helper.h b/ohos_nweb/include/nweb_config_helper.h index 1b7bf9758..977515013 100644 --- a/ohos_nweb/include/nweb_config_helper.h +++ b/ohos_nweb/include/nweb_config_helper.h @@ -43,6 +43,7 @@ private: void ParseWebConfigXml(const std::string &configFilePath, std::shared_ptr initArgs); void WriteConfigValueToSysPara(const std::string &configName, const std::string &value); void ParseDeleteConfig(const xmlNodePtr &rootPtr, std::shared_ptr initArgs); + void ParseWindowOrientationConfig(xmlNodePtr nodePtr, std::shared_ptr initArgs); std::map perfConfig_; std::map> ltpoConfig_; }; diff --git a/ohos_nweb/src/nweb_config_helper.cpp b/ohos_nweb/src/nweb_config_helper.cpp index e09a5e59b..98ba0d991 100644 --- a/ohos_nweb/src/nweb_config_helper.cpp +++ b/ohos_nweb/src/nweb_config_helper.cpp @@ -23,6 +23,7 @@ #include #include +#include "application_context.h" #include "config_policy_utils.h" #include "nweb_config_helper.h" #include "nweb_log.h" @@ -36,10 +37,14 @@ const std::string PERFORMANCE_CONFIG = "performanceConfig"; // The config used in base/web/webview const std::string BASE_WEB_CONFIG = "baseWebConfig"; const std::string WEB_ANIMATION_DYNAMIC_SETTING_CONFIG = "property_animation_dynamic_settings"; +const std::string WEB_WINDOW_ORIENTATION_CONFIG = "window_orientation_config"; +const std::string WEB_ALL_BUNDLE_NAME = "*"; const auto XML_ATTR_NAME = "name"; const auto XML_ATTR_MIN = "min"; const auto XML_ATTR_MAX = "max"; const auto XML_ATTR_FPS = "preferred_fps"; +const auto XML_BUNDLE_NAME = "bundle_name"; +const auto XML_ENABLE_WINDOW_ORIENTATION = "enable_window_orientation"; const std::unordered_map> configMap = { { "renderConfig/renderProcessCount", [](std::string& contentStr) { return std::string("--renderer-process-limit=") + contentStr; } }, @@ -279,6 +284,12 @@ void NWebConfigHelper::ParseWebConfigXml(const std::string& configFilePath, ParseNWebLTPOConfig(ltpoConfigNodePtr); } } + + xmlNodePtr windowOrientationNodePtr = GetChildrenNode(rootPtr, WEB_WINDOW_ORIENTATION_CONFIG); + if (windowOrientationNodePtr != nullptr) { + WVLOG_D("read config from window orientation node"); + ParseWindowOrientationConfig(windowOrientationNodePtr, initArgs); + } xmlFreeDoc(docPtr); } @@ -410,6 +421,54 @@ void NWebConfigHelper::ParseDeleteConfig(const xmlNodePtr &rootPtr, std::shared_ } } +void NWebConfigHelper::ParseWindowOrientationConfig(xmlNodePtr nodePtr, + std::shared_ptr initArgs) +{ + for (xmlNodePtr curNodePtr = nodePtr->xmlChildrenNode; curNodePtr; curNodePtr = curNodePtr->next) { + if (curNodePtr->name == nullptr || curNodePtr->type == XML_COMMENT_NODE) { + WVLOG_E("invalid node!"); + continue; + } + char* bundleNamePtr = (char *)xmlGetProp(curNodePtr,BAD_CAST(XML_BUNDLE_NAME)); + char* orientationPtr = (char *)xmlGetProp(curNodePtr,BAD_CAST(XML_ENABLE_WINDOW_ORIENTATION)); + if (!bundleNamePtr || !orientationPtr) { + WVLOG_E("invalid bundleNamePtr or orientationPtr!"); + if (bundleNamePtr) { + xmlFree(bundleNamePtr); + } + if (orientationPtr) { + xmlFree(orientationPtr); + } + continue; + } + std::string bundleName(bundleNamePtr); + std::string orientation(orientationPtr); + xmlFree(bundleNamePtr); + xmlFree(orientationPtr); + + std::string curBundleName; + std::shared_ptr ctx = + AbilityRuntime::ApplicationContext::GetApplicationContext(); + if (ctx) { + std::string curBundleName = ctx->GetBundleName(); + } + if (curBundleName.empty()) { + WVLOG_E("invalid curBundleName, no need to continue!"); + return; + } + if (bundleName == curBundleName || bundleName == WEB_ALL_BUNDLE_NAME) { + if (orientation == "true") { + initArgs->AddArg("--enable-blink-features=OrientationEvent"); + } else if (orientation == "false") { + initArgs->AddArg("--disable-blink-features=OrientationEvent"); + } else { + WVLOG_E("invalid orientation value!"); + } + break; + } + } +} + int NWebConfigHelper::safeGetPropAsInt(xmlNode* node, const xmlChar* propName, int defaultValue) { xmlChar* propValue = xmlGetProp(node, propName); diff --git a/test/unittest/nweb_config_helper_test/nweb_config_helper_test.cpp b/test/unittest/nweb_config_helper_test/nweb_config_helper_test.cpp index 38e6f5543..be6698c17 100644 --- a/test/unittest/nweb_config_helper_test/nweb_config_helper_test.cpp +++ b/test/unittest/nweb_config_helper_test/nweb_config_helper_test.cpp @@ -26,6 +26,9 @@ using namespace testing; namespace OHOS { namespace NWebConfig { +const auto XML_BUNDLE_NAME = "bundle_name"; +const auto XML_ENABLE_WINDOW_ORIENTATION = "enable_window_orientation"; + class NWebConfigHelperTest : public ::testing::Test { public: static void SetUpTestCase(void); @@ -55,6 +58,19 @@ HWTEST_F(NWebConfigHelperTest, ParseWebConfigXml_FileNotFound, TestSize.Level0) EXPECT_NO_THROW(NWebConfigHelper::Instance().ParseWebConfigXml(configFilePath, initArgs)); } +/** + * @tc.name: ParseWebConfigXml_ValidOrientationConfig + * @tc.desc: ParseWebConfigXml. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(NWebConfigHelperTest, ParseWebConfigXml_ValidOrientationConfig, TestSize.Level0) +{ + std::string configFilePath = "valid_orientation.xml"; + EXPECT_NE(initArgs, nullptr); + NWebConfigHelper::Instance().ParseWebConfigXml(configFilePath, initArgs); +} + /** * @tc.name: ParseWebConfigXml_InvalidRootElement * @tc.desc: ParseWebConfigXml. @@ -360,6 +376,89 @@ HWTEST_F(NWebConfigHelperTest, ParseDeleteConfig_EmptyParam, TestSize.Level0) NWebConfigHelper::Instance().ParseDeleteConfig(rootPtr, initArgs); } +/** + * @tc.name: ParseWindowOrientationConfig_WhenNodeIsComment + * @tc.desc: ParseDeleteConfig. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(NWebConfigHelperTest, + ParseWindowOrientationConfig_WhenNodeIsComment, TestSize.Level0) { + xmlNodePtr nodePtr = xmlNewNode(nullptr, BAD_CAST "testNode"); + EXPECT_NE(nodePtr, nullptr); + nodePtr->type = xmlElementType::XML_COMMENT_NODE; + std::shared_ptr initArgs = std::make_shared(); + NWebConfigHelper::Instance().ParseWindowOrientationConfig(nodePtr, initArgs); + EXPECT_EQ(initArgs->GetArgsToAdd().size(), 0); +} + +/** + * @tc.name: ParseWindowOrientationConfig_WhenBundleNameOrOrientationIsMissing + * @tc.desc: ParseWindowOrientationConfig. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(NWebConfigHelperTest, + ParseWindowOrientationConfig_WhenBundleNameOrOrientationIsMissing, TestSize.Level0) { + xmlNodePtr nodePtr = xmlNewNode(nullptr, BAD_CAST "testNode"); + EXPECT_NE(nodePtr, nullptr); + nodePtr->properties = nullptr; + std::shared_ptr initArgs = std::make_shared(); + NWebConfigHelper::Instance().ParseWindowOrientationConfig(nodePtr, initArgs); + EXPECT_EQ(initArgs->GetArgsToAdd().size(), 0); +} + +/** + * @tc.name: ParseWindowOrientationConfig_WhenBundleNameMatchesAndOrientationIsTure + * @tc.desc: ParseWindowOrientationConfig. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(NWebConfigHelperTest, + ParseWindowOrientationConfig_WhenBundleNameMatchesAndOrientationIsTure, TestSize.Level0) { + xmlNodePtr nodePtr = xmlNewNode(nullptr, BAD_CAST "testNode"); + EXPECT_NE(nodePtr, nullptr); + xmlNewProp(nodePtr, BAD_CAST(XML_BUNDLE_NAME), BAD_CAST("testBundleName")); + xmlNewProp(nodePtr, BAD_CAST(XML_ENABLE_WINDOW_ORIENTATION), BAD_CAST("true")); + std::shared_ptr initArgs = std::make_shared(); + NWebConfigHelper::Instance().ParseWindowOrientationConfig(nodePtr, initArgs); + EXPECT_EQ(initArgs->GetArgsToAdd().size(), 0); +} + +/** + * @tc.name: ParseWindowOrientationConfig_WhenBundleNameMatchesAndOrientationIsFalse + * @tc.desc: ParseWindowOrientationConfig. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(NWebConfigHelperTest, + ParseWindowOrientationConfig_WhenBundleNameMatchesAndOrientationIsFalse, TestSize.Level0) { + xmlNodePtr nodePtr = xmlNewNode(nullptr, BAD_CAST "testNode"); + EXPECT_NE(nodePtr, nullptr); + xmlNewProp(nodePtr, BAD_CAST(XML_BUNDLE_NAME), BAD_CAST "testBundleName"); + xmlNewProp(nodePtr, BAD_CAST(XML_ENABLE_WINDOW_ORIENTATION), BAD_CAST("false")); + std::shared_ptr initArgs = std::make_shared(); + NWebConfigHelper::Instance().ParseWindowOrientationConfig(nodePtr, initArgs); + EXPECT_EQ(initArgs->GetArgsToAdd().size(), 0); +} + +/** + * @tc.name: ParseWindowOrientationConfig_WhenBundleNameMatchesAndOrientationIsInvalid + * @tc.desc: ParseWindowOrientationConfig. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(NWebConfigHelperTest, + ParseWindowOrientationConfig_WhenBundleNameMatchesAndOrientationIsInvalid, TestSize.Level0) { + xmlNodePtr nodePtr = xmlNewNode(nullptr, BAD_CAST "testNode"); + EXPECT_NE(nodePtr, nullptr); + xmlNewProp(nodePtr, BAD_CAST(XML_BUNDLE_NAME), BAD_CAST("testBundleName")); + xmlNewProp(nodePtr, BAD_CAST(XML_ENABLE_WINDOW_ORIENTATION), BAD_CAST("invalidValue")); + std::shared_ptr initArgs = std::make_shared(); + NWebConfigHelper::Instance().ParseWindowOrientationConfig(nodePtr, initArgs); + EXPECT_EQ(initArgs->GetArgsToAdd().size(), 0); +} + /** * @tc.name : safeGetPropAsInt_ShouldReturnDefaultValue_WhenPropNotExist * @tc.number: OHOS_NWEB_001 -- Gitee From 23ac9c59636762a0e610d8d9cb9986fde80bb4a7 Mon Sep 17 00:00:00 2001 From: fenggechang Date: Wed, 20 Aug 2025 03:55:43 +0000 Subject: [PATCH 2/2] update ohos_nweb/BUILD.gn. Signed-off-by: fenggechang --- ohos_nweb/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ohos_nweb/BUILD.gn b/ohos_nweb/BUILD.gn index 0062b6774..02817f95e 100644 --- a/ohos_nweb/BUILD.gn +++ b/ohos_nweb/BUILD.gn @@ -63,6 +63,7 @@ ohos_shared_library("web_configs") { deps = [ ":web_config" ] external_deps = [ + "ability_runtime:app_context", "config_policy:configpolicy_util", "hilog:libhilog", "init:libbegetutil", @@ -104,7 +105,6 @@ ohos_shared_library("libnweb") { ] external_deps = [ - "ability_runtime:app_context", "ability_runtime:ability_manager", "ability_runtime:app_context", "ability_runtime:app_manager", -- Gitee