1 /*
2 * Copyright (c) 2021-2024 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 #include "context_deal.h"
17
18 #include <regex>
19
20 #include "ability_manager_client.h"
21 #include "ability_manager_interface.h"
22 #include "app_context.h"
23 #include "bundle_mgr_helper.h"
24 #include "constants.h"
25 #include "directory_ex.h"
26 #include "file_ex.h"
27 #include "hilog_tag_wrapper.h"
28 #include "iservice_registry.h"
29 #include "os_account_manager_wrapper.h"
30 #include "sys_mgr_client.h"
31 #include "system_ability_definition.h"
32
33 #define MODE 0771
34 namespace OHOS {
35 namespace AppExecFwk {
36 using namespace OHOS::AbilityBase::Constants;
37
38 const std::string ContextDeal::CONTEXT_DEAL_FILE_SEPARATOR("/");
39 const std::string ContextDeal::CONTEXT_DEAL_Files("files");
40 const int64_t ContextDeal::CONTEXT_CREATE_BY_SYSTEM_APP(0x00000001);
41 const std::string ContextDeal::CONTEXT_DATA_STORAGE("/data/storage/");
42 const std::string ContextDeal::CONTEXT_DEAL_DATA_APP("/data/app/");
43 const std::string ContextDeal::CONTEXT_DEAL_BASE("base");
44 const std::string ContextDeal::CONTEXT_DEAL_DATABASE("database");
45 const std::string ContextDeal::CONTEXT_DEAL_PREFERENCES("preferences");
46 const std::string ContextDeal::CONTEXT_DEAL_DATA("data");
47
ContextDeal(bool isCreateBySystemApp)48 ContextDeal::ContextDeal(bool isCreateBySystemApp) : isCreateBySystemApp_(isCreateBySystemApp)
49 {}
50
GetApplicationInfo() const51 std::shared_ptr<ApplicationInfo> ContextDeal::GetApplicationInfo() const
52 {
53 return applicationInfo_;
54 }
55
SetApplicationInfo(const std::shared_ptr<ApplicationInfo> & info)56 void ContextDeal::SetApplicationInfo(const std::shared_ptr<ApplicationInfo> &info)
57 {
58 if (info == nullptr) {
59 TAG_LOGE(AAFwkTag::APPKIT, "info is empty");
60 return;
61 }
62 applicationInfo_ = info;
63 }
64
GetApplicationContext() const65 std::shared_ptr<Context> ContextDeal::GetApplicationContext() const
66 {
67 return appContext_;
68 }
69
SetApplicationContext(const std::shared_ptr<Context> & context)70 void ContextDeal::SetApplicationContext(const std::shared_ptr<Context> &context)
71 {
72 if (context == nullptr) {
73 TAG_LOGE(AAFwkTag::APPKIT, "context is empty");
74 return;
75 }
76 appContext_ = context;
77 }
78
GetBundleCodePath()79 std::string ContextDeal::GetBundleCodePath()
80 {
81 if (applicationInfo_ == nullptr) {
82 return "";
83 }
84
85 std::string dir;
86 if (isCreateBySystemApp_) {
87 dir = std::regex_replace(applicationInfo_->codePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
88 } else {
89 dir = LOCAL_CODE_PATH;
90 }
91
92 return dir;
93 }
94
SetBundleCodePath(std::string & path)95 void ContextDeal::SetBundleCodePath(std::string &path)
96 {
97 path_ = path;
98 }
99
GetAbilityInfo()100 const std::shared_ptr<AbilityInfo> ContextDeal::GetAbilityInfo()
101 {
102 return abilityInfo_;
103 }
104
SetAbilityInfo(const std::shared_ptr<AbilityInfo> & info)105 void ContextDeal::SetAbilityInfo(const std::shared_ptr<AbilityInfo> &info)
106 {
107 if (info == nullptr) {
108 TAG_LOGE(AAFwkTag::APPKIT, "info is empty");
109 return;
110 }
111 abilityInfo_ = info;
112 }
113
GetContext()114 std::shared_ptr<Context> ContextDeal::GetContext()
115 {
116 return abilityContext_;
117 }
118
SetContext(const std::shared_ptr<Context> & context)119 void ContextDeal::SetContext(const std::shared_ptr<Context> &context)
120 {
121 if (context == nullptr) {
122 TAG_LOGE(AAFwkTag::APPKIT, "context is empty.");
123 return;
124 }
125 abilityContext_ = context;
126 }
127
GetBundleManager() const128 std::shared_ptr<BundleMgrHelper> ContextDeal::GetBundleManager() const
129 {
130 auto bundleMgrHelper = DelayedSingleton<BundleMgrHelper>::GetInstance();
131 if (bundleMgrHelper == nullptr) {
132 TAG_LOGE(AAFwkTag::APPKIT, "Failed to get bundle manager service.");
133 return nullptr;
134 }
135 return bundleMgrHelper;
136 }
137
GetResourceManager() const138 std::shared_ptr<Global::Resource::ResourceManager> ContextDeal::GetResourceManager() const
139 {
140 return resourceManager_;
141 }
142
GetDatabaseDir()143 std::string ContextDeal::GetDatabaseDir()
144 {
145 std::string dir;
146 if (IsCreateBySystemApp()) {
147 dir = CONTEXT_DEAL_DATA_APP + currArea_ + CONTEXT_DEAL_FILE_SEPARATOR + std::to_string(GetCurrentAccountId())
148 + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_DATABASE + CONTEXT_DEAL_FILE_SEPARATOR + GetBundleName();
149 } else {
150 dir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_DATABASE;
151 }
152 CreateDirIfNotExist(dir);
153 TAG_LOGD(AAFwkTag::APPKIT, "GetDatabaseDir:%{public}s", dir.c_str());
154 return dir;
155 }
156
GetDataDir()157 std::string ContextDeal::GetDataDir()
158 {
159 std::string dir = GetBaseDir() + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_DATA;
160 CreateDirIfNotExist(dir);
161 TAG_LOGD(AAFwkTag::APPKIT, "GetDataDir dir = %{public}s", dir.c_str());
162 return dir;
163 }
164
GetDir(const std::string & name,int mode)165 std::string ContextDeal::GetDir(const std::string &name, int mode)
166 {
167 if (applicationInfo_ == nullptr) {
168 TAG_LOGE(AAFwkTag::APPKIT, "applicationInfo_ == nullptr");
169 return "";
170 }
171 std::string dir = applicationInfo_->dataDir + CONTEXT_DEAL_FILE_SEPARATOR + name;
172 if (!OHOS::FileExists(dir)) {
173 TAG_LOGI(AAFwkTag::APPKIT, "GetDir File is not exits");
174 OHOS::ForceCreateDirectory(dir);
175 OHOS::ChangeModeDirectory(dir, mode);
176 }
177 return dir;
178 }
179
GetFilesDir()180 std::string ContextDeal::GetFilesDir()
181 {
182 std::string dir = GetBaseDir() + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_Files;
183 CreateDirIfNotExist(dir);
184 TAG_LOGD(AAFwkTag::APPKIT, "GetFilesDir dir = %{public}s", dir.c_str());
185 return dir;
186 }
187
GetBundleName() const188 std::string ContextDeal::GetBundleName() const
189 {
190 return (applicationInfo_ != nullptr) ? applicationInfo_->bundleName : "";
191 }
192
GetBundleResourcePath()193 std::string ContextDeal::GetBundleResourcePath()
194 {
195 if (abilityInfo_ == nullptr) {
196 return "";
197 }
198
199 std::string dir;
200 if (isCreateBySystemApp_) {
201 dir = std::regex_replace(abilityInfo_->resourcePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
202 } else {
203 std::regex pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + abilityInfo_->bundleName);
204 dir = std::regex_replace(abilityInfo_->resourcePath, pattern, LOCAL_CODE_PATH);
205 }
206 return dir;
207 }
208
GetAbilityManager()209 sptr<AAFwk::IAbilityManager> ContextDeal::GetAbilityManager()
210 {
211 auto remoteObject = OHOS::DelayedSingleton<SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
212 if (remoteObject == nullptr) {
213 TAG_LOGE(AAFwkTag::APPKIT, "Failed to get ability manager service.");
214 return nullptr;
215 }
216 sptr<AAFwk::IAbilityManager> ams = iface_cast<AAFwk::IAbilityManager>(remoteObject);
217 return ams;
218 }
219
GetAppType()220 std::string ContextDeal::GetAppType()
221 {
222 auto ptr = GetBundleManager();
223 if (ptr == nullptr) {
224 TAG_LOGE(AAFwkTag::APPKIT, "GetAppType failed to get bundle manager service");
225 return "";
226 }
227 std::string retString = ptr->GetAppType(applicationInfo_->bundleName);
228 return retString;
229 }
230
IsCreateBySystemApp() const231 bool ContextDeal::IsCreateBySystemApp() const
232 {
233 return (static_cast<uint64_t>(flags_) & static_cast<uint64_t>(CONTEXT_CREATE_BY_SYSTEM_APP)) == 1;
234 }
235
GetCurrentAccountId() const236 int ContextDeal::GetCurrentAccountId() const
237 {
238 int userId = 0;
239 DelayedSingleton<OsAccountManagerWrapper>::GetInstance()->GetOsAccountLocalIdFromProcess(userId);
240 return userId;
241 }
242
CreateDirIfNotExist(const std::string & dirPath) const243 void ContextDeal::CreateDirIfNotExist(const std::string &dirPath) const
244 {
245 if (!OHOS::FileExists(dirPath)) {
246 TAG_LOGD(AAFwkTag::APPKIT, "CreateDirIfNotExist File is not exits");
247 bool createDir = OHOS::ForceCreateDirectory(dirPath);
248 if (!createDir) {
249 TAG_LOGI(AAFwkTag::APPKIT, "CreateDirIfNotExist: create dir %{public}s failed.", dirPath.c_str());
250 return;
251 }
252 }
253 }
254
SetPattern(int patternId)255 void ContextDeal::SetPattern(int patternId)
256 {
257 if (resourceManager_ != nullptr) {
258 if (!pattern_.empty()) {
259 pattern_.clear();
260 }
261 OHOS::Global::Resource::RState errval = resourceManager_->GetPatternById(patternId, pattern_);
262 if (errval != OHOS::Global::Resource::RState::SUCCESS) {
263 TAG_LOGE(AAFwkTag::APPKIT, "SetPattern GetPatternById(patternId:%d) retval is %u", patternId, errval);
264 }
265 } else {
266 TAG_LOGE(AAFwkTag::APPKIT, "SetPattern resourceManager_ is nullptr");
267 }
268 }
269
GetHapModuleInfo()270 std::shared_ptr<HapModuleInfo> ContextDeal::GetHapModuleInfo()
271 {
272 // fix set HapModuleInfoLocal data failed, request only once
273 if (hapModuleInfoLocal_ == nullptr) {
274 HapModuleInfoRequestInit();
275 if (hapModuleInfoLocal_ == nullptr) {
276 TAG_LOGE(AAFwkTag::APPKIT, "hapModuleInfoLocal_ is nullptr");
277 return nullptr;
278 }
279 }
280 return hapModuleInfoLocal_;
281 }
282
initResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)283 void ContextDeal::initResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
284 {
285 resourceManager_ = resourceManager;
286 }
287
GetString(int resId)288 std::string ContextDeal::GetString(int resId)
289 {
290 if (resourceManager_ == nullptr) {
291 TAG_LOGE(AAFwkTag::APPKIT, "GetString resourceManager_ is nullptr");
292 return "";
293 }
294
295 std::string ret;
296 OHOS::Global::Resource::RState errval = resourceManager_->GetStringById(resId, ret);
297 if (errval == OHOS::Global::Resource::RState::SUCCESS) {
298 return ret;
299 } else {
300 TAG_LOGE(AAFwkTag::APPKIT, "GetString GetStringById(resId:%d) retval is %u", resId, errval);
301 return "";
302 }
303 }
304
GetStringArray(int resId)305 std::vector<std::string> ContextDeal::GetStringArray(int resId)
306 {
307 if (resourceManager_ == nullptr) {
308 TAG_LOGE(AAFwkTag::APPKIT, "resourceManager_ is nullptr");
309 return std::vector<std::string>();
310 }
311
312 std::vector<std::string> retv;
313 OHOS::Global::Resource::RState errval = resourceManager_->GetStringArrayById(resId, retv);
314 if (errval == OHOS::Global::Resource::RState::SUCCESS) {
315 return retv;
316 } else {
317 TAG_LOGE(AAFwkTag::APPKIT, "GetStringArrayById(resId:%d) retval is %u", resId, errval);
318 return std::vector<std::string>();
319 }
320 }
321
GetIntArray(int resId)322 std::vector<int> ContextDeal::GetIntArray(int resId)
323 {
324 if (resourceManager_ == nullptr) {
325 TAG_LOGE(AAFwkTag::APPKIT, "GetIntArray resourceManager_ is nullptr");
326 return std::vector<int>();
327 }
328
329 std::vector<int> retv;
330 OHOS::Global::Resource::RState errval = resourceManager_->GetIntArrayById(resId, retv);
331 if (errval == OHOS::Global::Resource::RState::SUCCESS) {
332 return retv;
333 } else {
334 TAG_LOGE(AAFwkTag::APPKIT, "GetIntArray GetIntArrayById(resId:%d) retval is %u", resId, errval);
335 return std::vector<int>();
336 }
337 }
338
GetTheme()339 std::map<std::string, std::string> ContextDeal::GetTheme()
340 {
341 if (theme_.empty()) {
342 SetTheme(GetThemeId());
343 }
344 return theme_;
345 }
346
SetTheme(int themeId)347 void ContextDeal::SetTheme(int themeId)
348 {
349 if (resourceManager_ == nullptr) {
350 TAG_LOGE(AAFwkTag::APPKIT, "resourceManager_ is nullptr");
351 return;
352 }
353
354 auto hapModInfo = GetHapModuleInfo();
355 if (hapModInfo == nullptr) {
356 TAG_LOGE(AAFwkTag::APPKIT, "hapModInfo is nullptr");
357 return;
358 }
359
360 if (!theme_.empty()) {
361 theme_.clear();
362 }
363 OHOS::Global::Resource::RState errval = resourceManager_->GetThemeById(themeId, theme_);
364 if (errval != OHOS::Global::Resource::RState::SUCCESS) {
365 TAG_LOGE(AAFwkTag::APPKIT, "SetTheme GetThemeById(themeId:%d) retval is %u", themeId, errval);
366 }
367 }
368
GetPattern()369 std::map<std::string, std::string> ContextDeal::GetPattern()
370 {
371 if (!pattern_.empty()) {
372 return pattern_;
373 } else {
374 TAG_LOGE(AAFwkTag::APPKIT, "GetPattern pattern_ is empty");
375 return std::map<std::string, std::string>();
376 }
377 }
378
GetColor(int resId)379 int ContextDeal::GetColor(int resId)
380 {
381 if (resourceManager_ == nullptr) {
382 TAG_LOGE(AAFwkTag::APPKIT, "GetColor resourceManager_ is nullptr");
383 return INVALID_RESOURCE_VALUE;
384 }
385
386 uint32_t ret = INVALID_RESOURCE_VALUE;
387 OHOS::Global::Resource::RState errval = resourceManager_->GetColorById(resId, ret);
388 if (errval == OHOS::Global::Resource::RState::SUCCESS) {
389 return ret;
390 } else {
391 TAG_LOGE(AAFwkTag::APPKIT, "GetColor GetColorById(resId:%d) retval is %u", resId, errval);
392 return INVALID_RESOURCE_VALUE;
393 }
394 }
395
GetThemeId()396 int ContextDeal::GetThemeId()
397 {
398 auto hapModInfo = GetHapModuleInfo();
399 if (hapModInfo != nullptr) {
400 return -1;
401 } else {
402 TAG_LOGE(AAFwkTag::APPKIT, "GetThemeId hapModInfo is nullptr");
403 return -1;
404 }
405 }
406
GetDisplayOrientation()407 int ContextDeal::GetDisplayOrientation()
408 {
409 if (abilityInfo_ != nullptr) {
410 return static_cast<int>(abilityInfo_->orientation);
411 } else {
412 TAG_LOGE(AAFwkTag::APPKIT, "abilityInfo_ is nullptr");
413 return static_cast<int>(DisplayOrientation::UNSPECIFIED);
414 }
415 }
416
GetPreferencesDir()417 std::string ContextDeal::GetPreferencesDir()
418 {
419 std::string dir = GetBaseDir() + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_PREFERENCES;
420 CreateDirIfNotExist(dir);
421 TAG_LOGD(AAFwkTag::APPKIT, "GetPreferencesDir:%{public}s", dir.c_str());
422 return dir;
423 }
424
SetColorMode(int mode)425 void ContextDeal::SetColorMode(int mode)
426 {
427 auto hapModInfo = GetHapModuleInfo();
428 if (hapModInfo == nullptr) {
429 TAG_LOGE(AAFwkTag::APPKIT, "SetColorMode hapModInfo is nullptr");
430 return;
431 }
432
433 if (mode == static_cast<int>(ModuleColorMode::DARK)) {
434 hapModInfo->colorMode = ModuleColorMode::DARK;
435 } else if (mode == static_cast<int>(ModuleColorMode::LIGHT)) {
436 hapModInfo->colorMode = ModuleColorMode::LIGHT;
437 } else { // default use AUTO
438 hapModInfo->colorMode = ModuleColorMode::AUTO;
439 }
440 }
441
GetColorMode()442 int ContextDeal::GetColorMode()
443 {
444 auto hapModInfo = GetHapModuleInfo();
445 if (hapModInfo == nullptr) {
446 TAG_LOGE(AAFwkTag::APPKIT, "GetColorMode hapModInfo is nullptr");
447 return -1;
448 }
449 return static_cast<int>(hapModInfo->colorMode);
450 }
451
452
HapModuleInfoRequestInit()453 bool ContextDeal::HapModuleInfoRequestInit()
454 {
455 auto ptr = GetBundleManager();
456 if (ptr == nullptr) {
457 TAG_LOGE(AAFwkTag::APPKIT, "Failed to get bundle manager service.");
458 return false;
459 }
460
461 if (abilityInfo_ == nullptr) {
462 TAG_LOGE(AAFwkTag::APPKIT, "The abilityInfo_ is nullptr.");
463 return false;
464 }
465
466 hapModuleInfoLocal_ = std::make_shared<HapModuleInfo>();
467 if (!ptr->GetHapModuleInfo(*abilityInfo_.get(), *hapModuleInfoLocal_)) {
468 TAG_LOGE(AAFwkTag::APPKIT, "Failed, will retval false value.");
469 return false;
470 }
471 return true;
472 }
473
GetBaseDir() const474 std::string ContextDeal::GetBaseDir() const
475 {
476 std::string baseDir;
477 if (IsCreateBySystemApp()) {
478 baseDir = CONTEXT_DEAL_DATA_APP + currArea_ + CONTEXT_DEAL_FILE_SEPARATOR +
479 std::to_string(GetCurrentAccountId()) + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_BASE +
480 CONTEXT_DEAL_FILE_SEPARATOR + GetBundleName();
481 } else {
482 baseDir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_BASE;
483 }
484
485 TAG_LOGD(AAFwkTag::APPKIT, "GetBaseDir:%{public}s", baseDir.c_str());
486 return baseDir;
487 }
488 } // namespace AppExecFwk
489 } // namespace OHOS
490