• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #ifndef OHOS_AAFWK_ABILITY_UTIL_H
17 #define OHOS_AAFWK_ABILITY_UTIL_H
18 
19 #include <string>
20 #include "hilog_wrapper.h"
21 #include "ability_config.h"
22 #include "ipc_skeleton.h"
23 #include "sa_mgr_client.h"
24 #include "bundlemgr/bundle_mgr_interface.h"
25 #include "system_ability_definition.h"
26 #include "ability_manager_errors.h"
27 
28 namespace OHOS {
29 namespace AAFwk {
30 namespace AbilityUtil {
31 constexpr int32_t SYSTEM_UID = 1000;
32 constexpr int32_t ROOT_UID = 0;
33 
34 static constexpr unsigned int CHANGE_CONFIG_ALL_CHANGED = 0xFFFFFFFF;
35 static constexpr unsigned int CHANGE_CONFIG_NONE = 0x00000000;
36 static constexpr unsigned int CHANGE_CONFIG_LOCALE = 0x00000001;
37 static constexpr unsigned int CHANGE_CONFIG_LAYOUT = 0x00000002;
38 static constexpr unsigned int CHANGE_CONFIG_FONTSIZE = 0x00000004;
39 static constexpr unsigned int CHANGE_CONFIG_ORIENTATION = 0x00000008;
40 static constexpr unsigned int CHANGE_CONFIG_DENSITY = 0x00000010;
41 
42 #define CHECK_POINTER_CONTINUE(object)      \
43     if (!object) {                          \
44         HILOG_ERROR("pointer is nullptr."); \
45         continue;                           \
46     }
47 
48 #define CHECK_POINTER_IS_NULLPTR(object)    \
49     if (object == nullptr) {                \
50         HILOG_ERROR("pointer is nullptr."); \
51         return;                             \
52     }
53 
54 #define CHECK_POINTER(object)               \
55     if (!object) {                          \
56         HILOG_ERROR("pointer is nullptr."); \
57         return;                             \
58     }
59 
60 #define CHECK_POINTER_LOG(object, log)   \
61     if (!object) {                       \
62         HILOG_ERROR("%{public}s:", log); \
63         return;                          \
64     }
65 
66 #define CHECK_POINTER_AND_RETURN(object, value) \
67     if (!object) {                              \
68         HILOG_ERROR("pointer is nullptr.");     \
69         return value;                           \
70     }
71 
72 #define CHECK_POINTER_AND_RETURN_LOG(object, value, log) \
73     if (!object) {                                       \
74         HILOG_ERROR("%{public}s:", log);                 \
75         return value;                                    \
76     }
77 
78 #define CHECK_POINTER_RETURN_BOOL(object)   \
79     if (!object) {                          \
80         HILOG_ERROR("pointer is nullptr."); \
81         return false;                       \
82     }
83 
84 #define CHECK_RET_RETURN_RET(object, log)                         \
85     if (object != ERR_OK) {                                       \
86         HILOG_ERROR("%{public}s, ret : %{public}d", log, object); \
87         return object;                                            \
88     }
89 
90 #define CHECK_TRUE_RETURN_RET(object, value, log) \
91     if (object) {                                 \
92         HILOG_WARN("%{public}s", log);            \
93         return value;                             \
94     }
95 
IsSystemDialogAbility(const std::string & bundleName,const std::string & abilityName)96 [[maybe_unused]] static bool IsSystemDialogAbility(const std::string &bundleName, const std::string &abilityName)
97 {
98     if (abilityName == AbilityConfig::SYSTEM_DIALOG_NAME && bundleName == AbilityConfig::SYSTEM_UI_BUNDLE_NAME) {
99         return true;
100     }
101 
102     if (abilityName == AbilityConfig::DEVICE_MANAGER_NAME && bundleName == AbilityConfig::DEVICE_MANAGER_BUNDLE_NAME) {
103         return true;
104     }
105 
106     return false;
107 }
108 
ConvertBundleNameSingleton(const std::string & bundleName,const std::string & name)109 [[maybe_unused]] static std::string ConvertBundleNameSingleton(const std::string &bundleName, const std::string &name)
110 {
111     std::string strName =
112         AbilityConfig::MISSION_NAME_MARK_HEAD + bundleName + AbilityConfig::MISSION_NAME_SEPARATOR + name;
113     return strName;
114 }
115 
116 static constexpr int64_t NANOSECONDS = 1000000000;  // NANOSECONDS mean 10^9 nano second
117 static constexpr int64_t MICROSECONDS = 1000000;    // MICROSECONDS mean 10^6 millias second
SystemTimeMillis()118 [[maybe_unused]] static int64_t SystemTimeMillis()
119 {
120     struct timespec t;
121     t.tv_sec = 0;
122     t.tv_nsec = 0;
123     clock_gettime(CLOCK_MONOTONIC, &t);
124     return (int64_t)((t.tv_sec) * NANOSECONDS + t.tv_nsec) / MICROSECONDS;
125 }
126 
UTCTimeSeconds()127 [[maybe_unused]] static int64_t UTCTimeSeconds()
128 {
129     struct timespec t;
130     t.tv_sec = 0;
131     t.tv_nsec = 0;
132     clock_gettime(CLOCK_REALTIME, &t);
133     return (int64_t)(t.tv_sec);
134 }
135 
GetBundleManager()136 static sptr<AppExecFwk::IBundleMgr> GetBundleManager()
137 {
138     auto bundleObj =
139         OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
140     if (bundleObj == nullptr) {
141         HILOG_ERROR("failed to get bundle manager service");
142         return nullptr;
143     }
144     return iface_cast<AppExecFwk::IBundleMgr>(bundleObj);
145 }
146 
147 [[maybe_unused]] static int JudgeAbilityVisibleControl(const AppExecFwk::AbilityInfo &abilityInfo, int callerUid = -1)
148 {
149     HILOG_DEBUG("%{public}s begin", __func__);
150     if (!abilityInfo.visible) {
151         HILOG_ERROR("ability visible is false");
152         if (callerUid == -1) {
153             callerUid = IPCSkeleton::GetCallingUid();
154         }
155         if (ROOT_UID == callerUid) {
156             HILOG_ERROR("uid is root");
157             return ABILITY_VISIBLE_FALSE_DENY_REQUEST;
158         }
159         auto bms = GetBundleManager();
160         CHECK_POINTER_AND_RETURN(bms, GET_ABILITY_SERVICE_FAILED);
161         auto isSystemApp = bms->CheckIsSystemAppByUid(callerUid);
162         if (callerUid != SYSTEM_UID && !isSystemApp) {
163             HILOG_ERROR("caller is not systemAp or system");
164             std::string bundleName;
165             bool result = bms->GetBundleNameForUid(callerUid, bundleName);
166             if (!result) {
167                 HILOG_ERROR("GetBundleNameForUid fail");
168                 return ABILITY_VISIBLE_FALSE_DENY_REQUEST;
169             }
170             if (bundleName != abilityInfo.bundleName) {
171                 HILOG_ERROR("caller ability bundlename not equal abilityInfo.bundleName bundleName: %{public}s "
172                             "abilityInfo.bundleName: %{public}s",
173                     bundleName.c_str(),
174                     abilityInfo.bundleName.c_str());
175                 return ABILITY_VISIBLE_FALSE_DENY_REQUEST;
176             }
177         }
178     }
179     HILOG_DEBUG("%{public}s end", __func__);
180     return ERR_OK;
181 }
182 }  // namespace AbilityUtil
183 }  // namespace AAFwk
184 }  // namespace OHOS
185 
186 #endif  // OHOS_AAFWK_ABILITY_UTIL_H
187