• 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_ABILITY_RUNTIME_ABILITY_UTIL_H
17 #define OHOS_ABILITY_RUNTIME_ABILITY_UTIL_H
18 
19 #include <string>
20 #include <unordered_set>
21 
22 #include "ability_config.h"
23 #include "ability_manager_errors.h"
24 #include "ability_manager_client.h"
25 #include "app_jump_control_rule.h"
26 #include "bundlemgr/bundle_mgr_interface.h"
27 #ifndef SUPPORT_ERMS
28 #include "erms_mgr_interface.h"
29 #endif
30 #include "hilog_wrapper.h"
31 #include "in_process_call_wrapper.h"
32 #include "ipc_skeleton.h"
33 #include "permission_verification.h"
34 #include "sa_mgr_client.h"
35 #include "system_ability_definition.h"
36 
37 namespace OHOS {
38 namespace AAFwk {
39 namespace AbilityUtil {
40 constexpr const char* SYSTEM_BASIC = "system_basic";
41 constexpr const char* SYSTEM_CORE = "system_core";
42 constexpr const char* DLP_BUNDLE_NAME = "com.ohos.dlpmanager";
43 constexpr const char* DLP_ABILITY_NAME = "ViewAbility";
44 constexpr const char* DLP_PARAMS_SANDBOX = "ohos.dlp.params.sandbox";
45 constexpr const char* DLP_PARAMS_BUNDLE_NAME = "ohos.dlp.params.bundleName";
46 constexpr const char* DLP_PARAMS_MODULE_NAME = "ohos.dlp.params.moduleName";
47 constexpr const char* DLP_PARAMS_ABILITY_NAME = "ohos.dlp.params.abilityName";
48 const std::string MARKET_BUNDLE_NAME = "com.huawei.hmos.appgallery";
49 const std::string BUNDLE_NAME_SELECTOR_DIALOG = "com.ohos.amsdialog";
50 const std::string JUMP_INTERCEPTOR_DIALOG_CALLER_PKG = "interceptor_callerPkg";
51 // dlp White list
52 const std::unordered_set<std::string> WHITE_LIST_DLP_SET = { BUNDLE_NAME_SELECTOR_DIALOG };
53 
54 #define CHECK_POINTER_CONTINUE(object)      \
55     if (!object) {                          \
56         HILOG_ERROR("pointer is nullptr."); \
57         continue;                           \
58     }
59 
60 #define CHECK_POINTER_IS_NULLPTR(object)    \
61     if (object == nullptr) {                \
62         HILOG_ERROR("pointer is nullptr."); \
63         return;                             \
64     }
65 
66 #define CHECK_POINTER(object)               \
67     if (!object) {                          \
68         HILOG_ERROR("pointer is nullptr."); \
69         return;                             \
70     }
71 
72 #define CHECK_POINTER_LOG(object, log)   \
73     if (!object) {                       \
74         HILOG_ERROR("%{public}s:", log); \
75         return;                          \
76     }
77 
78 #define CHECK_POINTER_AND_RETURN(object, value) \
79     if (!object) {                              \
80         HILOG_ERROR("pointer is nullptr.");     \
81         return value;                           \
82     }
83 
84 #define CHECK_POINTER_AND_RETURN_LOG(object, value, log) \
85     if (!object) {                                       \
86         HILOG_ERROR("%{public}s:", log);                 \
87         return value;                                    \
88     }
89 
90 #define CHECK_POINTER_RETURN_BOOL(object)   \
91     if (!object) {                          \
92         HILOG_ERROR("pointer is nullptr."); \
93         return false;                       \
94     }
95 
96 #define CHECK_RET_RETURN_RET(object, log)                         \
97     if (object != ERR_OK) {                                       \
98         HILOG_ERROR("%{public}s, ret : %{public}d", log, object); \
99         return object;                                            \
100     }
101 
102 #define CHECK_TRUE_RETURN_RET(object, value, log) \
103     if (object) {                                 \
104         HILOG_WARN("%{public}s", log);            \
105         return value;                             \
106     }
107 
IsSystemDialogAbility(const std::string & bundleName,const std::string & abilityName)108 [[maybe_unused]] static bool IsSystemDialogAbility(const std::string &bundleName, const std::string &abilityName)
109 {
110     if (abilityName == AbilityConfig::SYSTEM_DIALOG_NAME && bundleName == AbilityConfig::SYSTEM_UI_BUNDLE_NAME) {
111         return true;
112     }
113 
114     if (abilityName == AbilityConfig::DEVICE_MANAGER_NAME && bundleName == AbilityConfig::DEVICE_MANAGER_BUNDLE_NAME) {
115         return true;
116     }
117 
118     return false;
119 }
120 
121 [[maybe_unused]] static std::string ConvertBundleNameSingleton(const std::string &bundleName, const std::string &name,
122     const std::string &moduleName, const int32_t appIndex = 0)
123 {
124     std::string strName;
125     if (appIndex == 0) {
126         strName = AbilityConfig::MISSION_NAME_MARK_HEAD + bundleName +
127             AbilityConfig::MISSION_NAME_SEPARATOR + moduleName +
128             AbilityConfig::MISSION_NAME_SEPARATOR + name;
129     } else {
130         strName = AbilityConfig::MISSION_NAME_MARK_HEAD + bundleName +
131             AbilityConfig::MISSION_NAME_SEPARATOR + std::to_string(appIndex) +
132             AbilityConfig::MISSION_NAME_SEPARATOR + moduleName +
133             AbilityConfig::MISSION_NAME_SEPARATOR + name;
134     }
135 
136     return strName;
137 }
138 
139 static constexpr int64_t NANOSECONDS = 1000000000;  // NANOSECONDS mean 10^9 nano second
140 static constexpr int64_t MICROSECONDS = 1000000;    // MICROSECONDS mean 10^6 millias second
SystemTimeMillis()141 [[maybe_unused]] static int64_t SystemTimeMillis()
142 {
143     struct timespec t;
144     t.tv_sec = 0;
145     t.tv_nsec = 0;
146     clock_gettime(CLOCK_MONOTONIC, &t);
147     return (int64_t)((t.tv_sec) * NANOSECONDS + t.tv_nsec) / MICROSECONDS;
148 }
149 
UTCTimeSeconds()150 [[maybe_unused]] static int64_t UTCTimeSeconds()
151 {
152     struct timespec t;
153     t.tv_sec = 0;
154     t.tv_nsec = 0;
155     clock_gettime(CLOCK_REALTIME, &t);
156     return (int64_t)(t.tv_sec);
157 }
158 
IsStartFreeInstall(const Want & want)159 [[maybe_unused]] static bool IsStartFreeInstall(const Want &want)
160 {
161     auto flags = want.GetFlags();
162     if ((flags & Want::FLAG_INSTALL_ON_DEMAND) == Want::FLAG_INSTALL_ON_DEMAND) {
163         return true;
164     }
165     return false;
166 }
167 
GetBundleManager()168 [[maybe_unused]] static sptr<AppExecFwk::IBundleMgr> GetBundleManager()
169 {
170     auto bundleObj =
171         OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
172     if (bundleObj == nullptr) {
173         HILOG_ERROR("failed to get bundle manager service");
174         return nullptr;
175     }
176     return iface_cast<AppExecFwk::IBundleMgr>(bundleObj);
177 }
178 
179 #ifndef SUPPORT_ERMS
CheckEcologicalRuleMgr()180 [[maybe_unused]] static sptr<AppExecFwk::IEcologicalRuleManager> CheckEcologicalRuleMgr()
181 {
182     // should remove when AG SA online
183     int32_t ECOLOGICAL_RULE_SA_ID = 9999;
184     auto remoteObject =
185             OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->CheckSystemAbility(ECOLOGICAL_RULE_SA_ID);
186     if (remoteObject == nullptr) {
187         HILOG_ERROR("%{public}s error, failed to check ecological rule manager service.", __func__);
188         return nullptr;
189     }
190 
191     return iface_cast<AppExecFwk::IEcologicalRuleManager>(remoteObject);
192 }
193 #endif
194 
ParseJumpInterceptorWant(Want & targetWant,const std::string callerPkg)195 [[maybe_unused]] static bool ParseJumpInterceptorWant(Want &targetWant, const std::string callerPkg)
196 {
197     if (callerPkg.empty()) {
198         HILOG_ERROR("%{public}s error, get empty callerPkg.", __func__);
199         return false;
200     }
201     targetWant.SetParam(JUMP_INTERCEPTOR_DIALOG_CALLER_PKG, callerPkg);
202     return true;
203 }
204 
CheckJumpInterceptorWant(const Want & targetWant,std::string & callerPkg,std::string & targetPkg)205 [[maybe_unused]] static bool CheckJumpInterceptorWant(const Want &targetWant, std::string &callerPkg,
206     std::string &targetPkg)
207 {
208     if (!targetWant.HasParameter(JUMP_INTERCEPTOR_DIALOG_CALLER_PKG)) {
209         HILOG_ERROR("%{public}s error, the interceptor parameter invalid.", __func__);
210         return false;
211     }
212     callerPkg = targetWant.GetStringParam(JUMP_INTERCEPTOR_DIALOG_CALLER_PKG);
213     targetPkg = targetWant.GetElement().GetBundleName();
214     return !callerPkg.empty() && !targetPkg.empty();
215 }
216 
AddAbilityJumpRuleToBms(const std::string & callerPkg,const std::string & targetPkg,int32_t userId)217 [[maybe_unused]] static bool AddAbilityJumpRuleToBms(const std::string &callerPkg, const std::string &targetPkg,
218     int32_t userId)
219 {
220     if (callerPkg.empty() || targetPkg.empty()) {
221         HILOG_ERROR("get invalid inputs");
222         return false;
223     }
224     auto bms = AbilityUtil::GetBundleManager();
225     if (!bms) {
226         HILOG_ERROR("GetBundleManager failed");
227         return false;
228     }
229     auto appControlMgr = bms->GetAppControlProxy();
230     if (appControlMgr == nullptr) {
231         HILOG_ERROR("Get appControlMgr failed");
232         return false;
233     }
234     int ret = IN_PROCESS_CALL(appControlMgr->ConfirmAppJumpControlRule(callerPkg, targetPkg, userId));
235     return ret == ERR_OK;
236 }
237 
HandleDlpApp(Want & want)238 [[maybe_unused]] static bool HandleDlpApp(Want &want)
239 {
240     if (WHITE_LIST_DLP_SET.find(want.GetBundle()) != WHITE_LIST_DLP_SET.end()) {
241         HILOG_INFO("%{public}s, enter special app", __func__);
242         return false;
243     }
244 
245     AppExecFwk::ElementName element = want.GetElement();
246     if (want.GetBoolParam(DLP_PARAMS_SANDBOX, false) && !element.GetBundleName().empty() &&
247         !element.GetAbilityName().empty()) {
248         want.SetElementName(DLP_BUNDLE_NAME, DLP_ABILITY_NAME);
249         want.SetParam(DLP_PARAMS_BUNDLE_NAME, element.GetBundleName());
250         want.SetParam(DLP_PARAMS_MODULE_NAME, element.GetModuleName());
251         want.SetParam(DLP_PARAMS_ABILITY_NAME, element.GetAbilityName());
252         want.RemoveParam(DLP_PARAMS_SANDBOX);
253         return true;
254     }
255 
256     return false;
257 }
258 
IsStartIncludeAtomicService(const Want & want,const int32_t userId)259 [[maybe_unused]] static bool IsStartIncludeAtomicService(const Want &want, const int32_t userId)
260 {
261     auto bms = GetBundleManager();
262     if (!bms) {
263         HILOG_ERROR("GetBundleManager failed");
264         return false;
265     }
266 
267     std::string targetBundleName = want.GetBundle();
268     AppExecFwk::ApplicationInfo targetAppInfo;
269     bool getTargetResult = IN_PROCESS_CALL(bms->GetApplicationInfo(targetBundleName,
270         AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, targetAppInfo));
271     if (!getTargetResult) {
272         HILOG_ERROR("Get targetAppInfo failed in check atomic service.");
273         return false;
274     }
275     if (targetAppInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE) {
276         HILOG_INFO("the target is atomic service");
277         return true;
278     }
279 
280     int callerUid = IPCSkeleton::GetCallingUid();
281     std::string callerBundleName;
282     ErrCode err = IN_PROCESS_CALL(bms->GetNameForUid(callerUid, callerBundleName));
283     if (err != ERR_OK) {
284         HILOG_ERROR("Get bms failed in check atomic service.");
285         return false;
286     }
287     AppExecFwk::ApplicationInfo callerAppInfo;
288     bool getCallerResult = IN_PROCESS_CALL(bms->GetApplicationInfo(callerBundleName,
289         AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, callerAppInfo));
290     if (!getCallerResult) {
291         HILOG_ERROR("Get callerAppInfo failed in check atomic service.");
292         return false;
293     }
294     if (callerAppInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE) {
295         HILOG_INFO("the caller is atomic service");
296         return true;
297     }
298     return false;
299 }
300 
StartAppgallery(const int requestCode,const int32_t userId,const std::string & action)301 inline int StartAppgallery(const int requestCode, const int32_t userId, const std::string &action)
302 {
303     Want want;
304     want.SetElementName(MARKET_BUNDLE_NAME, "");
305     want.SetAction(action);
306     return AbilityManagerClient::GetInstance()->StartAbility(want, userId, requestCode);
307 }
308 }  // namespace AbilityUtil
309 }  // namespace AAFwk
310 }  // namespace OHOS
311 
312 #endif  // OHOS_ABILITY_RUNTIME_ABILITY_UTIL_H
313