1 /*
2 * Copyright (c) 2023-2024 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 #ifndef I_SECURITY_COMPONENT_INFO_HELPER_H
16 #define I_SECURITY_COMPONENT_INFO_HELPER_H
17
18 #include "accesstoken_kit.h"
19 #include "nlohmann/json.hpp"
20 #include "sec_comp_base.h"
21 #include "sec_comp_info.h"
22
23 namespace OHOS {
24 namespace Security {
25 namespace SecurityComponent {
26 template<typename T>
ConstructComponent(const nlohmann::json & jsonComponent,std::string & message,bool isClicked)27 T* ConstructComponent(const nlohmann::json& jsonComponent, std::string& message, bool isClicked)
28 {
29 T *componentPtr = new (std::nothrow)T();
30 if ((componentPtr != nullptr) && !componentPtr->FromJson(jsonComponent, message, isClicked)) {
31 delete componentPtr;
32 return nullptr;
33 }
34 return componentPtr;
35 }
36
37 class __attribute__((visibility("default"))) SecCompInfoHelper {
38 public:
39 struct ScreenInfo {
40 uint64_t displayId;
41 CrossAxisState crossAxisState;
42 bool isWearable;
43 };
44
45 static SecCompBase* ParseComponent(SecCompType type, const nlohmann::json& jsonComponent,
46 std::string& message, bool isClicked = false);
47 static bool CheckComponentValid(SecCompBase* comp, std::string& message);
48 static bool CheckRectValid(const SecCompRect& rect, const SecCompRect& windowRect, const ScreenInfo& screenInfo,
49 std::string& message);
50 static double GetDistance(DimensionT x1, DimensionT y1, DimensionT x2, DimensionT y2);
51
52 private:
53 static float GetWindowScale(int32_t windowId);
54 static void AdjustSecCompRect(SecCompBase* comp, float scale, bool isCompatScaleMode);
55 static bool IsOutOfWatchScreen(const SecCompRect& rect, double radius, std::string& message);
56 static bool IsOutOfScreen(const SecCompRect& rect, double curScreenWidth, double curScreenHeight,
57 std::string& message, bool isWearable);
58 };
59 } // namespace SecurityComponent
60 } // namespace Security
61 } // namespace OHOS
62 #endif // I_SECURITY_COMPONENT_INFO_HELPER_H
63