From 17de210abae2a4e715a2f3d6530bdab07ce4e622 Mon Sep 17 00:00:00 2001 From: baoyang Date: Wed, 20 Aug 2025 17:50:38 +0800 Subject: [PATCH] fix seccomp adjust problem Signed-off-by: baoyang Change-Id: I57652684f6aa8125f9fdcabeb5663b62009bb8b1 --- .../include/sec_comp_info.h | 6 +++ .../sec_comp_info_helper.h | 5 +- .../sa/sa_main/sec_comp_info_helper.cpp | 37 +++++++------ .../sa/sa_main/window_info_helper.cpp | 22 +++++--- .../sa/sa_main/window_info_helper.h | 2 +- .../sa/test/mock/include/window_manager.h | 3 ++ .../src/sec_comp_info_helper_test.cpp | 53 +++++++++++++++++-- 7 files changed, 98 insertions(+), 30 deletions(-) diff --git a/interfaces/inner_api/security_component/include/sec_comp_info.h b/interfaces/inner_api/security_component/include/sec_comp_info.h index 699e0c6..b9a1555 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_info.h +++ b/interfaces/inner_api/security_component/include/sec_comp_info.h @@ -52,6 +52,12 @@ struct BorderRadius { DimensionT rightBottom = DEFAULT_DIMENSION; }; +struct Scales { + float scaleX = DEFAULT_DIMENSION; + float scaleY = DEFAULT_DIMENSION; + float floatingScale = DEFAULT_DIMENSION; +}; + enum SecCompType { UNKNOWN_SC_TYPE = 0, LOCATION_COMPONENT, diff --git a/interfaces/inner_api/security_component_common/sec_comp_info_helper.h b/interfaces/inner_api/security_component_common/sec_comp_info_helper.h index 5902c6f..773bf56 100644 --- a/interfaces/inner_api/security_component_common/sec_comp_info_helper.h +++ b/interfaces/inner_api/security_component_common/sec_comp_info_helper.h @@ -50,8 +50,9 @@ struct ScreenInfo { static double GetDistance(DimensionT x1, DimensionT y1, DimensionT x2, DimensionT y2); private: - static float GetWindowScale(int32_t windowId); - static void AdjustSecCompRect(SecCompBase* comp, float scale, bool isCompatScaleMode); + static Scales GetWindowScale(int32_t windowId); + static void AdjustSecCompRect(SecCompBase* comp, const Scales scales, bool isCompatScaleMode, + SecCompRect& windowRect); static bool IsOutOfWatchScreen(const SecCompRect& rect, double radius, std::string& message); static bool IsOutOfScreen(const SecCompRect& rect, double curScreenWidth, double curScreenHeight, std::string& message, bool isWearable); diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index ad68957..5eea866 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -46,27 +46,33 @@ const int NUMBER_TWO = 2; const char HEX_FILL_CHAR = '0'; } -void SecCompInfoHelper::AdjustSecCompRect(SecCompBase* comp, float scale, bool isCompatScaleMode) +void SecCompInfoHelper::AdjustSecCompRect(SecCompBase* comp, const Scales scales, bool isCompatScaleMode, + SecCompRect& windowRect) { - comp->rect_.width_ *= scale; - comp->rect_.height_ *= scale; if (!isCompatScaleMode) { // window scales towards the top-left corner - comp->rect_.x_ = comp->windowRect_.x_ + (comp->rect_.x_ - comp->windowRect_.x_) * scale; - comp->rect_.y_ = comp->windowRect_.y_ + (comp->rect_.y_ - comp->windowRect_.y_) * scale; + comp->rect_.width_ *= scales.floatingScale; + comp->rect_.height_ *= scales.floatingScale; + comp->rect_.x_ = comp->windowRect_.x_ + (comp->rect_.x_ - comp->windowRect_.x_) * scales.floatingScale; + comp->rect_.y_ = comp->windowRect_.y_ + (comp->rect_.y_ - comp->windowRect_.y_) * scales.floatingScale; + comp->windowRect_.width_ *= scales.floatingScale; + comp->windowRect_.height_ *= scales.floatingScale; } else { // window scales towards the center + comp->rect_.width_ *= scales.scaleX; + comp->rect_.height_ *= scales.scaleY; auto disX = comp->rect_.x_ - comp->windowRect_.x_; auto disY = comp->rect_.y_ - comp->windowRect_.y_; - comp->windowRect_.x_ = comp->windowRect_.x_ + (1 - scale) * comp->windowRect_.width_ / NUMBER_TWO; - comp->windowRect_.y_ = comp->windowRect_.y_ + (1 - scale) * comp->windowRect_.height_ / NUMBER_TWO; - comp->rect_.x_ = comp->windowRect_.x_ + scale * disX; - comp->rect_.y_ = comp->windowRect_.y_ + scale * disY; + comp->rect_.x_ = windowRect.x_ + disX * scales.scaleX; + comp->rect_.y_ = windowRect.y_ + disY * scales.scaleY; + + comp->windowRect_.x_ = windowRect.x_; + comp->windowRect_.y_ = windowRect.y_; + comp->windowRect_.width_ = windowRect.width_; + comp->windowRect_.height_ = windowRect.height_; } SC_LOG_DEBUG(LABEL, "After adjust x %{public}f, y %{public}f, width %{public}f, height %{public}f", comp->rect_.x_, comp->rect_.y_, comp->rect_.width_, comp->rect_.height_); - comp->windowRect_.width_ *= scale; - comp->windowRect_.height_ *= scale; } SecCompBase* SecCompInfoHelper::ParseComponent(SecCompType type, const nlohmann::json& jsonComponent, @@ -347,10 +353,11 @@ bool SecCompInfoHelper::CheckComponentValid(SecCompBase* comp, std::string& mess } bool isCompatScaleMode = false; - float scale = WindowInfoHelper::GetWindowScale(comp->windowId_, isCompatScaleMode); - SC_LOG_DEBUG(LABEL, "WindowScale = %{public}f", scale); - if (!IsEqual(scale, WindowInfoHelper::FULL_SCREEN_SCALE) && !IsEqual(scale, 0.0)) { - AdjustSecCompRect(comp, scale, isCompatScaleMode); + SecCompRect scaleRect; + Scales scales = WindowInfoHelper::GetWindowScale(comp->windowId_, isCompatScaleMode, scaleRect); + if ((!IsEqual(scales.floatingScale, WindowInfoHelper::FULL_SCREEN_SCALE) && !IsEqual(scales.floatingScale, 0.0)) || + isCompatScaleMode) { + AdjustSecCompRect(comp, scales, isCompatScaleMode, scaleRect); } if (!CheckSecCompBase(comp, message)) { diff --git a/services/security_component_service/sa/sa_main/window_info_helper.cpp b/services/security_component_service/sa/sa_main/window_info_helper.cpp index 6c791ab..059d583 100644 --- a/services/security_component_service/sa/sa_main/window_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/window_info_helper.cpp @@ -28,25 +28,33 @@ constexpr int32_t INVALID_WINDOW_LAYER = -1; constexpr uint32_t UI_EXTENSION_MASK = 0x40000000; } -float WindowInfoHelper::GetWindowScale(int32_t windowId, bool& isCompatScaleMode) +Scales WindowInfoHelper::GetWindowScale(int32_t windowId, bool& isCompatScaleMode, SecCompRect& scaleRect) { - float scale = FULL_SCREEN_SCALE; + Scales scales; + scales.floatingScale = FULL_SCREEN_SCALE; std::vector> infos; if (Rosen::WindowManager::GetInstance().GetAccessibilityWindowInfo(infos) != Rosen::WMError::WM_OK) { SC_LOG_ERROR(LABEL, "Get AccessibilityWindowInfo failed"); - return scale; + return scales; } auto iter = std::find_if(infos.begin(), infos.end(), [windowId](const sptr info) { return windowId == info->wid_; }); if ((iter == infos.end()) || (*iter == nullptr)) { SC_LOG_WARN(LABEL, "Cannot find AccessibilityWindowInfo, return default scale"); - return scale; + return scales; } isCompatScaleMode = (*iter)->isCompatScaleMode_; - scale = (*iter)->scaleVal_; - SC_LOG_INFO(LABEL, "Get scale = %{public}f, isCompatScaleMode = %{public}d", scale, isCompatScaleMode); - return scale; + scales.floatingScale = (*iter)->scaleVal_; + scales.scaleX = (*iter)->scaleX_; + scales.scaleY = (*iter)->scaleY_; + scaleRect.x_ = (*iter)->scaleRect_.posX_; + scaleRect.y_ = (*iter)->scaleRect_.posY_; + scaleRect.width_ = (*iter)->scaleRect_.width_; + scaleRect.height_ = (*iter)->scaleRect_.height_; + SC_LOG_INFO(LABEL, "Get floatingScale = %{public}f, scaleX = %{public}f, scaleY = %{public}f, \ + isCompatScaleMode = %{public}d", scales.floatingScale, scales.scaleX, scales.scaleY, isCompatScaleMode); + return scales; } std::string GetSecCompWindowMsg(int32_t compWinId, const SecCompRect& secRect, diff --git a/services/security_component_service/sa/sa_main/window_info_helper.h b/services/security_component_service/sa/sa_main/window_info_helper.h index aec5035..272c518 100644 --- a/services/security_component_service/sa/sa_main/window_info_helper.h +++ b/services/security_component_service/sa/sa_main/window_info_helper.h @@ -23,7 +23,7 @@ namespace Security { namespace SecurityComponent { class __attribute__((visibility("default"))) WindowInfoHelper { public: - static float GetWindowScale(int32_t windowId, bool& isCompatScaleMode); + static Scales GetWindowScale(int32_t windowId, bool& isCompatScaleMode, SecCompRect& scaleRect); static bool CheckOtherWindowCoverComp(int32_t compWinId, const SecCompRect& secRect, std::string& message); public: static constexpr float FULL_SCREEN_SCALE = 1.0F; diff --git a/services/security_component_service/sa/test/mock/include/window_manager.h b/services/security_component_service/sa/test/mock/include/window_manager.h index c24499e..c1c2563 100644 --- a/services/security_component_service/sa/test/mock/include/window_manager.h +++ b/services/security_component_service/sa/test/mock/include/window_manager.h @@ -55,6 +55,9 @@ public: WindowMode mode_; WindowType type_; float scaleVal_; + float scaleX_; + float scaleY_; + Rect scaleRect_; bool isCompatScaleMode_ { false }; }; diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index 6350f35..8620191 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -533,8 +533,12 @@ HWTEST_F(SecCompInfoHelperTest, AdjustSecCompRect001, TestSize.Level0) nlohmann::json jsonComponent; ServiceTestCommon::BuildLocationComponentJson(jsonComponent); std::string message; + SecCompRect scaleRect; SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message); - float scale = 0.8f; + Scales scales; + scales.floatingScale = 0.8f; + scales.scaleX = 0.8f; + scales.scaleY = 0.8f; comp->rect_.x_ = 10.0f; comp->rect_.y_ = 5.0f; @@ -545,11 +549,30 @@ HWTEST_F(SecCompInfoHelperTest, AdjustSecCompRect001, TestSize.Level0) comp->windowRect_.width_ = 200.0f; comp->windowRect_.height_ = 100.0f; - SecCompInfoHelper::AdjustSecCompRect(comp, scale, false); + SecCompInfoHelper::AdjustSecCompRect(comp, scales, false, scaleRect); EXPECT_TRUE(IsEqual(comp->rect_.width_, 16)); EXPECT_TRUE(IsEqual(comp->rect_.height_, 8)); EXPECT_TRUE(IsEqual(comp->rect_.x_, 8)); EXPECT_TRUE(IsEqual(comp->rect_.y_, 4)); +} + +/** + * @tc.name: AdjustSecCompRect002 + * @tc.desc: Test AdjustSecCompRect + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, AdjustSecCompRect002, TestSize.Level0) +{ + nlohmann::json jsonComponent; + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); + std::string message; + SecCompRect scaleRect; + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message); + Scales scales; + scales.floatingScale = 0.8f; + scales.scaleX = 0.8f; + scales.scaleY = 0.8f; comp->rect_.x_ = 10.0f; comp->rect_.y_ = 5.0f; @@ -559,12 +582,32 @@ HWTEST_F(SecCompInfoHelperTest, AdjustSecCompRect001, TestSize.Level0) comp->windowRect_.y_ = 0.0f; comp->windowRect_.width_ = 200.0f; comp->windowRect_.height_ = 100.0f; + scaleRect.x_ = 0.0f; + scaleRect.y_ = 0.0f; + scaleRect.width_ = 200.0f; + scaleRect.height_ = 100.0f; - SecCompInfoHelper::AdjustSecCompRect(comp, scale, true); + SecCompInfoHelper::AdjustSecCompRect(comp, scales, true, scaleRect); EXPECT_TRUE(IsEqual(comp->rect_.width_, 16)); EXPECT_TRUE(IsEqual(comp->rect_.height_, 8)); - EXPECT_TRUE(IsEqual(comp->rect_.x_, 28)); - EXPECT_TRUE(IsEqual(comp->rect_.y_, 14)); + EXPECT_TRUE(IsEqual(comp->rect_.x_, 8)); + EXPECT_TRUE(IsEqual(comp->rect_.y_, 4)); + + comp->rect_.x_ = 10.0f; + comp->rect_.y_ = 5.0f; + comp->rect_.width_ = 20.0f; + comp->rect_.height_ = 10.0f; + scaleRect.x_ = 0.0f; + scaleRect.y_ = 0.0f; + scaleRect.width_ = 200.0f; + scaleRect.height_ = 100.0f; + scales.scaleX = 0.8f; + scales.scaleY = 0.6f; + SecCompInfoHelper::AdjustSecCompRect(comp, scales, true, scaleRect); + EXPECT_TRUE(IsEqual(comp->rect_.width_, 16)); + EXPECT_TRUE(IsEqual(comp->rect_.height_, 6)); + EXPECT_TRUE(IsEqual(comp->rect_.x_, 8)); + EXPECT_TRUE(IsEqual(comp->rect_.y_, 3)); } /** -- Gitee