• 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 "bundlemgr/bundle_mgr_interface.h"
26 #include "hilog_wrapper.h"
27 #include "ipc_skeleton.h"
28 #include "permission_verification.h"
29 #include "sa_mgr_client.h"
30 #include "system_ability_definition.h"
31 
32 namespace OHOS {
33 namespace AAFwk {
34 namespace AbilityUtil {
35 constexpr const char* SYSTEM_BASIC = "system_basic";
36 constexpr const char* SYSTEM_CORE = "system_core";
37 constexpr const char* DLP_BUNDLE_NAME = "com.ohos.dlpmanager";
38 constexpr const char* DLP_ABILITY_NAME = "ViewAbility";
39 constexpr const char* DLP_PARAMS_SANDBOX = "ohos.dlp.params.sandbox";
40 constexpr const char* DLP_PARAMS_BUNDLE_NAME = "ohos.dlp.params.bundleName";
41 constexpr const char* DLP_PARAMS_MODULE_NAME = "ohos.dlp.params.moduleName";
42 constexpr const char* DLP_PARAMS_ABILITY_NAME = "ohos.dlp.params.abilityName";
43 const std::string MARKET_BUNDLE_NAME = "com.huawei.hmos.appgallery";
44 const std::string BUNDLE_NAME_SELECTOR_DIALOG = "com.ohos.amsdialog";
45 // dlp White list
46 const std::unordered_set<std::string> WHITE_LIST_DLP_SET = { BUNDLE_NAME_SELECTOR_DIALOG };
47 
48 static constexpr unsigned int CHANGE_CONFIG_ALL_CHANGED = 0xFFFFFFFF;
49 static constexpr unsigned int CHANGE_CONFIG_NONE = 0x00000000;
50 static constexpr unsigned int CHANGE_CONFIG_LOCALE = 0x00000001;
51 static constexpr unsigned int CHANGE_CONFIG_LAYOUT = 0x00000002;
52 static constexpr unsigned int CHANGE_CONFIG_FONTSIZE = 0x00000004;
53 static constexpr unsigned int CHANGE_CONFIG_ORIENTATION = 0x00000008;
54 static constexpr unsigned int CHANGE_CONFIG_DENSITY = 0x00000010;
55 
56 #define CHECK_POINTER_CONTINUE(object)      \
57     if (!object) {                          \
58         HILOG_ERROR("pointer is nullptr."); \
59         continue;                           \
60     }
61 
62 #define CHECK_POINTER_IS_NULLPTR(object)    \
63     if (object == nullptr) {                \
64         HILOG_ERROR("pointer is nullptr."); \
65         return;                             \
66     }
67 
68 #define CHECK_POINTER(object)               \
69     if (!object) {                          \
70         HILOG_ERROR("pointer is nullptr."); \
71         return;                             \
72     }
73 
74 #define CHECK_POINTER_LOG(object, log)   \
75     if (!object) {                       \
76         HILOG_ERROR("%{public}s:", log); \
77         return;                          \
78     }
79 
80 #define CHECK_POINTER_AND_RETURN(object, value) \
81     if (!object) {                              \
82         HILOG_ERROR("pointer is nullptr.");     \
83         return value;                           \
84     }
85 
86 #define CHECK_POINTER_AND_RETURN_LOG(object, value, log) \
87     if (!object) {                                       \
88         HILOG_ERROR("%{public}s:", log);                 \
89         return value;                                    \
90     }
91 
92 #define CHECK_POINTER_RETURN_BOOL(object)   \
93     if (!object) {                          \
94         HILOG_ERROR("pointer is nullptr."); \
95         return false;                       \
96     }
97 
98 #define CHECK_RET_RETURN_RET(object, log)                         \
99     if (object != ERR_OK) {                                       \
100         HILOG_ERROR("%{public}s, ret : %{public}d", log, object); \
101         return object;                                            \
102     }
103 
104 #define CHECK_TRUE_RETURN_RET(object, value, log) \
105     if (object) {                                 \
106         HILOG_WARN("%{public}s", log);            \
107         return value;                             \
108     }
109 
IsSystemDialogAbility(const std::string & bundleName,const std::string & abilityName)110 [[maybe_unused]] static bool IsSystemDialogAbility(const std::string &bundleName, const std::string &abilityName)
111 {
112     if (abilityName == AbilityConfig::SYSTEM_DIALOG_NAME && bundleName == AbilityConfig::SYSTEM_UI_BUNDLE_NAME) {
113         return true;
114     }
115 
116     if (abilityName == AbilityConfig::DEVICE_MANAGER_NAME && bundleName == AbilityConfig::DEVICE_MANAGER_BUNDLE_NAME) {
117         return true;
118     }
119 
120     return false;
121 }
122 
123 [[maybe_unused]] static std::string ConvertBundleNameSingleton(const std::string &bundleName, const std::string &name,
124     const std::string &moduleName, const int32_t appIndex = 0)
125 {
126     std::string strName;
127     if (appIndex == 0) {
128         strName = AbilityConfig::MISSION_NAME_MARK_HEAD + bundleName +
129             AbilityConfig::MISSION_NAME_SEPARATOR + moduleName +
130             AbilityConfig::MISSION_NAME_SEPARATOR + name;
131     } else {
132         strName = AbilityConfig::MISSION_NAME_MARK_HEAD + bundleName +
133             AbilityConfig::MISSION_NAME_SEPARATOR + std::to_string(appIndex) +
134             AbilityConfig::MISSION_NAME_SEPARATOR + moduleName +
135             AbilityConfig::MISSION_NAME_SEPARATOR + name;
136     }
137 
138     return strName;
139 }
140 
141 static constexpr int64_t NANOSECONDS = 1000000000;  // NANOSECONDS mean 10^9 nano second
142 static constexpr int64_t MICROSECONDS = 1000000;    // MICROSECONDS mean 10^6 millias second
SystemTimeMillis()143 [[maybe_unused]] static int64_t SystemTimeMillis()
144 {
145     struct timespec t;
146     t.tv_sec = 0;
147     t.tv_nsec = 0;
148     clock_gettime(CLOCK_MONOTONIC, &t);
149     return (int64_t)((t.tv_sec) * NANOSECONDS + t.tv_nsec) / MICROSECONDS;
150 }
151 
UTCTimeSeconds()152 [[maybe_unused]] static int64_t UTCTimeSeconds()
153 {
154     struct timespec t;
155     t.tv_sec = 0;
156     t.tv_nsec = 0;
157     clock_gettime(CLOCK_REALTIME, &t);
158     return (int64_t)(t.tv_sec);
159 }
160 
IsStartFreeInstall(const Want & want)161 [[maybe_unused]] static bool IsStartFreeInstall(const Want &want)
162 {
163     auto flags = want.GetFlags();
164     if ((flags & Want::FLAG_INSTALL_ON_DEMAND) == Want::FLAG_INSTALL_ON_DEMAND) {
165         return true;
166     }
167     return false;
168 }
169 
GetBundleManager()170 [[maybe_unused]] static sptr<AppExecFwk::IBundleMgr> GetBundleManager()
171 {
172     auto bundleObj =
173         OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
174     if (bundleObj == nullptr) {
175         HILOG_ERROR("failed to get bundle manager service");
176         return nullptr;
177     }
178     return iface_cast<AppExecFwk::IBundleMgr>(bundleObj);
179 }
180 
HandleDlpApp(Want & want)181 [[maybe_unused]] static bool HandleDlpApp(Want &want)
182 {
183     if (WHITE_LIST_DLP_SET.find(want.GetBundle()) != WHITE_LIST_DLP_SET.end()) {
184         HILOG_INFO("%{public}s, enter special app", __func__);
185         return false;
186     }
187 
188     AppExecFwk::ElementName element = want.GetElement();
189     if (want.GetBoolParam(DLP_PARAMS_SANDBOX, false) && !element.GetBundleName().empty()
190         && !element.GetAbilityName().empty()) {
191         want.SetElementName(DLP_BUNDLE_NAME, DLP_ABILITY_NAME);
192         want.SetParam(DLP_PARAMS_BUNDLE_NAME, element.GetBundleName());
193         want.SetParam(DLP_PARAMS_MODULE_NAME, element.GetModuleName());
194         want.SetParam(DLP_PARAMS_ABILITY_NAME, element.GetAbilityName());
195         want.RemoveParam(DLP_PARAMS_SANDBOX);
196         return true;
197     }
198 
199     return false;
200 }
201 
StartAppgallery(const int requestCode,const int32_t userId,const std::string & action)202 inline int StartAppgallery(const int requestCode, const int32_t userId, const std::string &action)
203 {
204     Want want;
205     want.SetElementName(MARKET_BUNDLE_NAME, "");
206     want.SetAction(action);
207     return AbilityManagerClient::GetInstance()->StartAbility(want, userId, requestCode);
208 }
209 }  // namespace AbilityUtil
210 }  // namespace AAFwk
211 }  // namespace OHOS
212 
213 #endif  // OHOS_ABILITY_RUNTIME_ABILITY_UTIL_H
214