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