1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "window_info_helper.h"
16
17 #include <vector>
18 #include "sec_comp_log.h"
19 #include "window_manager.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace SecurityComponent {
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "WindowInfoHelper"};
26 constexpr int32_t INVALID_WINDOW_LAYER = -1;
27 constexpr uint32_t UI_EXTENSION_MASK = 0x40000000;
28 }
29
GetWindowScale(int32_t windowId)30 float WindowInfoHelper::GetWindowScale(int32_t windowId)
31 {
32 float scale = FULL_SCREEN_SCALE;
33 std::vector<sptr<Rosen::AccessibilityWindowInfo>> infos;
34 if (Rosen::WindowManager::GetInstance().GetAccessibilityWindowInfo(infos) != Rosen::WMError::WM_OK) {
35 SC_LOG_ERROR(LABEL, "Get AccessibilityWindowInfo failed");
36 return scale;
37 }
38 auto iter = std::find_if(infos.begin(), infos.end(), [windowId](const sptr<Rosen::AccessibilityWindowInfo> info) {
39 return windowId == info->wid_;
40 });
41 if (iter == infos.end()) {
42 return scale;
43 }
44 scale = (*iter)->scaleVal_;
45 SC_LOG_INFO(LABEL, "Get scale %{public}f", scale);
46 return scale;
47 }
48
IsRectInWindRect(const Rosen::Rect & windRect,const SecCompRect & secRect)49 static bool IsRectInWindRect(const Rosen::Rect& windRect, const SecCompRect& secRect)
50 {
51 // left or right
52 if ((secRect.x_ + secRect.width_ <= windRect.posX_) || (secRect.x_ >= windRect.posX_ + windRect.width_)) {
53 return false;
54 }
55 // top or bottom
56 if ((secRect.y_ + secRect.height_ <= windRect.posY_) || (secRect.y_ >= windRect.posY_ + windRect.height_)) {
57 return false;
58 }
59
60 return true;
61 }
62
CheckOtherWindowCoverComp(int32_t compWinId,const SecCompRect & secRect)63 bool WindowInfoHelper::CheckOtherWindowCoverComp(int32_t compWinId, const SecCompRect& secRect)
64 {
65 if ((static_cast<uint32_t>(compWinId) & UI_EXTENSION_MASK) == UI_EXTENSION_MASK) {
66 SC_LOG_INFO(LABEL, "UI extension can not check");
67 return true;
68 }
69 std::vector<sptr<Rosen::AccessibilityWindowInfo>> infos;
70 if (Rosen::WindowManager::GetInstance().GetAccessibilityWindowInfo(infos) != Rosen::WMError::WM_OK) {
71 SC_LOG_ERROR(LABEL, "Get AccessibilityWindowInfo failed");
72 return false;
73 }
74
75 int32_t compLayer = INVALID_WINDOW_LAYER;
76 std::vector<int32_t> layerList;
77 for (auto& info : infos) {
78 if (info == nullptr) {
79 continue;
80 }
81
82 if (info->wid_ == compWinId) {
83 compLayer = info->layer_;
84 continue;
85 }
86 if (info->scaleVal_ != 0.0) {
87 info->windowRect_.width_ *= info->scaleVal_;
88 info->windowRect_.height_ *= info->scaleVal_;
89 }
90 if (IsRectInWindRect(info->windowRect_, secRect)) {
91 layerList.emplace_back(info->layer_);
92 }
93 }
94
95 if (compLayer == INVALID_WINDOW_LAYER) {
96 SC_LOG_ERROR(LABEL, "windId %{public}d component layer is wrong", compWinId);
97 return false;
98 }
99
100 if (layerList.size() == static_cast<size_t>(0)) {
101 return true;
102 }
103
104 auto iter = std::find_if(layerList.begin(), layerList.end(), [compLayer](const int layer) {
105 return layer >= compLayer;
106 });
107 if (iter != layerList.end()) {
108 SC_LOG_ERROR(LABEL, "component window %{public}d is covered, click check failed", compWinId);
109 return false;
110 }
111 return true;
112 }
113 } // namespace SecurityComponent
114 } // namespace Security
115 } // namespace OHOS
116