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