1 /*
2 * Copyright (c) 2021-2022 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_impl.h"
17
18 #include <cerrno>
19 #include <regex>
20
21 #include "ability_constants.h"
22 #include "app_mgr_client.h"
23 #include "bundle_mgr_proxy.h"
24 #include "configuration_convertor.h"
25 #include "directory_ex.h"
26 #include "file_ex.h"
27 #include "hilog_wrapper.h"
28 #include "ipc_singleton.h"
29 #include "js_runtime_utils.h"
30 #ifdef SUPPORT_GRAPHICS
31 #include "locale_config.h"
32 #endif
33 #include "os_account_manager_wrapper.h"
34 #include "parameters.h"
35 #include "running_process_info.h"
36 #include "sys_mgr_client.h"
37 #include "system_ability_definition.h"
38
39 namespace OHOS {
40 namespace AbilityRuntime {
41 using namespace OHOS::AbilityRuntime::Constants;
42
43 const size_t Context::CONTEXT_TYPE_ID(std::hash<const char*> {} ("Context"));
44 const int64_t ContextImpl::CONTEXT_CREATE_BY_SYSTEM_APP(0x00000001);
45 const mode_t MODE = 0770;
46 const std::string ContextImpl::CONTEXT_DATA_APP("/data/app/");
47 const std::string ContextImpl::CONTEXT_BUNDLE("/bundle/");
48 const std::string ContextImpl::CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE("/mnt/hmdfs/");
49 const std::string ContextImpl::CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE("/device_view/local/data/");
50 const std::string ContextImpl::CONTEXT_DISTRIBUTEDFILES("distributedfiles");
51 const std::string ContextImpl::CONTEXT_FILE_SEPARATOR("/");
52 const std::string ContextImpl::CONTEXT_DATA("/data/");
53 const std::string ContextImpl::CONTEXT_DATA_STORAGE("/data/storage/");
54 const std::string ContextImpl::CONTEXT_BASE("base");
55 const std::string ContextImpl::CONTEXT_CACHE("cache");
56 const std::string ContextImpl::CONTEXT_PREFERENCES("preferences");
57 const std::string ContextImpl::CONTEXT_DATABASE("database");
58 const std::string ContextImpl::CONTEXT_TEMP("/temp");
59 const std::string ContextImpl::CONTEXT_FILES("/files");
60 const std::string ContextImpl::CONTEXT_HAPS("/haps");
61 const std::string ContextImpl::CONTEXT_ELS[] = {"el1", "el2"};
62 Global::Resource::DeviceType ContextImpl::deviceType_ = Global::Resource::DeviceType::DEVICE_NOT_SET;
63
GetBundleName() const64 std::string ContextImpl::GetBundleName() const
65 {
66 if (parentContext_ != nullptr) {
67 return parentContext_->GetBundleName();
68 }
69 return (applicationInfo_ != nullptr) ? applicationInfo_->bundleName : "";
70 }
71
GetBundleCodeDir()72 std::string ContextImpl::GetBundleCodeDir()
73 {
74 auto appInfo = GetApplicationInfo();
75 if (appInfo == nullptr) {
76 return "";
77 }
78
79 std::string dir;
80 if (IsCreateBySystemApp()) {
81 dir = std::regex_replace(appInfo->codePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
82 } else {
83 dir = LOCAL_CODE_PATH;
84 }
85 CreateDirIfNotExist(dir, MODE);
86 HILOG_DEBUG("ContextImpl::GetBundleCodeDir:%{public}s", dir.c_str());
87 return dir;
88 }
89
GetCacheDir()90 std::string ContextImpl::GetCacheDir()
91 {
92 std::string dir = GetBaseDir() + CONTEXT_FILE_SEPARATOR + CONTEXT_CACHE;
93 CreateDirIfNotExist(dir, MODE);
94 HILOG_DEBUG("ContextImpl::GetCacheDir:%{public}s", dir.c_str());
95 return dir;
96 }
97
IsUpdatingConfigurations()98 bool ContextImpl::IsUpdatingConfigurations()
99 {
100 return false;
101 }
102
PrintDrawnCompleted()103 bool ContextImpl::PrintDrawnCompleted()
104 {
105 return false;
106 }
107
GetDatabaseDir()108 std::string ContextImpl::GetDatabaseDir()
109 {
110 std::string dir;
111 if (IsCreateBySystemApp()) {
112 dir = CONTEXT_DATA_APP + currArea_ + CONTEXT_FILE_SEPARATOR + std::to_string(GetCurrentAccountId())
113 + CONTEXT_FILE_SEPARATOR + CONTEXT_DATABASE + CONTEXT_FILE_SEPARATOR + GetBundleName();
114 } else {
115 dir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_DATABASE;
116 }
117 if (parentContext_ != nullptr) {
118 dir = dir + CONTEXT_FILE_SEPARATOR + ((GetHapModuleInfo() == nullptr) ? "" : GetHapModuleInfo()->moduleName);
119 }
120 CreateDirIfNotExist(dir, 0);
121 HILOG_DEBUG("ContextImpl::GetDatabaseDir:%{public}s", dir.c_str());
122 return dir;
123 }
124
GetPreferencesDir()125 std::string ContextImpl::GetPreferencesDir()
126 {
127 std::string dir = GetBaseDir() + CONTEXT_FILE_SEPARATOR + CONTEXT_PREFERENCES;
128 CreateDirIfNotExist(dir, MODE);
129 HILOG_DEBUG("ContextImpl::GetPreferencesDir:%{public}s", dir.c_str());
130 return dir;
131 }
132
GetTempDir()133 std::string ContextImpl::GetTempDir()
134 {
135 std::string dir = GetBaseDir() + CONTEXT_TEMP;
136 CreateDirIfNotExist(dir, MODE);
137 HILOG_DEBUG("ContextImpl::GetTempDir:%{public}s", dir.c_str());
138 return dir;
139 }
140
GetFilesDir()141 std::string ContextImpl::GetFilesDir()
142 {
143 std::string dir = GetBaseDir() + CONTEXT_FILES;
144 CreateDirIfNotExist(dir, MODE);
145 HILOG_DEBUG("ContextImpl::GetFilesDir:%{public}s", dir.c_str());
146 return dir;
147 }
148
GetDistributedFilesDir()149 std::string ContextImpl::GetDistributedFilesDir()
150 {
151 HILOG_DEBUG("ContextImpl::GetDistributedFilesDir");
152 std::string dir;
153 if (IsCreateBySystemApp()) {
154 dir = CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE + std::to_string(GetCurrentAccountId()) +
155 CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE + GetBundleName();
156 } else {
157 dir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_DISTRIBUTEDFILES;
158 }
159 CreateDirIfNotExist(dir, 0);
160 HILOG_DEBUG("ContextImpl::GetDistributedFilesDir:%{public}s", dir.c_str());
161 return dir;
162 }
163
SwitchArea(int mode)164 void ContextImpl::SwitchArea(int mode)
165 {
166 HILOG_DEBUG("ContextImpl::SwitchArea, mode:%{public}d.", mode);
167 if (mode < 0 || mode >= (int)(sizeof(CONTEXT_ELS) / sizeof(CONTEXT_ELS[0]))) {
168 HILOG_ERROR("ContextImpl::SwitchArea, mode is invalid.");
169 return;
170 }
171 currArea_ = CONTEXT_ELS[mode];
172 HILOG_DEBUG("ContextImpl::SwitchArea end, currArea:%{public}s.", currArea_.c_str());
173 }
174
CreateModuleContext(const std::string & moduleName)175 std::shared_ptr<Context> ContextImpl::CreateModuleContext(const std::string &moduleName)
176 {
177 return CreateModuleContext(GetBundleName(), moduleName);
178 }
179
CreateModuleContext(const std::string & bundleName,const std::string & moduleName)180 std::shared_ptr<Context> ContextImpl::CreateModuleContext(const std::string &bundleName, const std::string &moduleName)
181 {
182 if (bundleName.empty()) {
183 HILOG_ERROR("ContextImpl::CreateModuleContext bundleName is empty");
184 return nullptr;
185 }
186
187 if (moduleName.empty()) {
188 HILOG_ERROR("ContextImpl::CreateModuleContext moduleName is empty");
189 return nullptr;
190 }
191
192 sptr<AppExecFwk::IBundleMgr> bundleMgr = GetBundleManager();
193 if (bundleMgr == nullptr) {
194 HILOG_ERROR("ContextImpl::CreateModuleContext GetBundleManager is nullptr");
195 return nullptr;
196 }
197
198 HILOG_DEBUG("ContextImpl::CreateModuleContext length: %{public}zu, bundleName: %{public}s",
199 (size_t)bundleName.length(), bundleName.c_str());
200
201 int accountId = GetCurrentAccountId();
202 if (accountId == 0) {
203 accountId = GetCurrentActiveAccountId();
204 }
205
206 AppExecFwk::BundleInfo bundleInfo;
207 if (bundleName == GetBundleName()) {
208 bundleMgr->GetBundleInfoForSelf(
209 (static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
210 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY) +
211 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) +
212 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE) +
213 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO) +
214 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY) +
215 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA)), bundleInfo);
216 } else {
217 bundleMgr->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, accountId);
218 }
219
220 if (bundleInfo.name.empty() || bundleInfo.applicationInfo.name.empty()) {
221 HILOG_ERROR("ContextImpl::CreateModuleContext GetBundleInfo is error");
222 return nullptr;
223 }
224
225 auto info = std::find_if(bundleInfo.hapModuleInfos.begin(), bundleInfo.hapModuleInfos.end(),
226 [&moduleName](const AppExecFwk::HapModuleInfo &hapModuleInfo) {
227 return hapModuleInfo.moduleName == moduleName;
228 });
229 if (info == bundleInfo.hapModuleInfos.end()) {
230 HILOG_ERROR("ContextImpl::CreateModuleContext moduleName is error.");
231 return nullptr;
232 }
233 std::shared_ptr<ContextImpl> appContext = std::make_shared<ContextImpl>();
234 appContext->InitHapModuleInfo(*info);
235 appContext->SetConfiguration(config_);
236 InitResourceManager(bundleInfo, appContext, GetBundleName() == bundleName, moduleName);
237 appContext->SetApplicationInfo(GetApplicationInfo());
238 return appContext;
239 }
240
GetArea()241 int ContextImpl::GetArea()
242 {
243 HILOG_DEBUG("ContextImpl::GetArea begin");
244 int mode = -1;
245 for (int i = 0; i < (int)(sizeof(CONTEXT_ELS) / sizeof(CONTEXT_ELS[0])); i++) {
246 if (currArea_ == CONTEXT_ELS[i]) {
247 mode = i;
248 break;
249 }
250 }
251 if (mode == -1) {
252 HILOG_ERROR("ContextImpl::GetArea not find mode.");
253 return EL_DEFAULT;
254 }
255 HILOG_DEBUG("ContextImpl::GetArea end");
256 return mode;
257 }
258
GetBaseDir() const259 std::string ContextImpl::GetBaseDir() const
260 {
261 std::string baseDir;
262 if (IsCreateBySystemApp()) {
263 baseDir = CONTEXT_DATA_APP + currArea_ + CONTEXT_FILE_SEPARATOR + std::to_string(GetCurrentAccountId()) +
264 CONTEXT_FILE_SEPARATOR + CONTEXT_BASE + CONTEXT_FILE_SEPARATOR + GetBundleName();
265 } else {
266 baseDir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_BASE;
267 }
268 if (parentContext_ != nullptr) {
269 baseDir = baseDir + CONTEXT_HAPS + CONTEXT_FILE_SEPARATOR +
270 ((GetHapModuleInfo() == nullptr) ? "" : GetHapModuleInfo()->moduleName);
271 }
272
273 HILOG_DEBUG("ContextImpl::GetBaseDir:%{public}s", baseDir.c_str());
274 return baseDir;
275 }
276
GetCurrentAccountId() const277 int ContextImpl::GetCurrentAccountId() const
278 {
279 int userId = 0;
280 auto instance = DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance();
281 if (instance == nullptr) {
282 HILOG_ERROR("Failed to get OsAccountManager instance.");
283 return userId;
284 }
285 instance->GetOsAccountLocalIdFromProcess(userId);
286 return userId;
287 }
288
GetCurrentActiveAccountId() const289 int ContextImpl::GetCurrentActiveAccountId() const
290 {
291 std::vector<int> accountIds;
292 auto instance = DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance();
293 if (instance == nullptr) {
294 HILOG_ERROR("Failed to get OsAccountManager instance.");
295 return 0;
296 }
297 ErrCode ret = instance->QueryActiveOsAccountIds(accountIds);
298 if (ret != ERR_OK) {
299 HILOG_ERROR("ContextImpl::GetCurrentActiveAccountId error.");
300 return 0;
301 }
302
303 if (accountIds.size() == 0) {
304 HILOG_ERROR("ContextImpl::GetCurrentActiveAccountId error, no accounts.");
305 return 0;
306 }
307
308 if (accountIds.size() > 1) {
309 HILOG_ERROR("ContextImpl::GetCurrentActiveAccountId error, no current now.");
310 return 0;
311 }
312
313 return accountIds[0];
314 }
315
CreateBundleContext(const std::string & bundleName)316 std::shared_ptr<Context> ContextImpl::CreateBundleContext(const std::string &bundleName)
317 {
318 if (parentContext_ != nullptr) {
319 return parentContext_->CreateBundleContext(bundleName);
320 }
321
322 if (bundleName.empty()) {
323 HILOG_ERROR("ContextImpl::CreateBundleContext bundleName is empty");
324 return nullptr;
325 }
326
327 sptr<AppExecFwk::IBundleMgr> bundleMgr = GetBundleManager();
328 if (bundleMgr == nullptr) {
329 HILOG_ERROR("ContextImpl::CreateBundleContext GetBundleManager is nullptr");
330 return nullptr;
331 }
332
333 AppExecFwk::BundleInfo bundleInfo;
334 int accountId = GetCurrentAccountId();
335 if (accountId == 0) {
336 accountId = GetCurrentActiveAccountId();
337 }
338 HILOG_DEBUG("ContextImpl::CreateBundleContext length: %{public}zu, bundleName: %{public}s",
339 (size_t)bundleName.length(), bundleName.c_str());
340 bundleMgr->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, accountId);
341
342 if (bundleInfo.name.empty() || bundleInfo.applicationInfo.name.empty()) {
343 HILOG_ERROR("ContextImpl::CreateBundleContext GetBundleInfo is error");
344 return nullptr;
345 }
346
347 std::shared_ptr<ContextImpl> appContext = std::make_shared<ContextImpl>();
348 appContext->SetFlags(CONTEXT_CREATE_BY_SYSTEM_APP);
349 appContext->SetConfiguration(config_);
350
351 // init resourceManager.
352 InitResourceManager(bundleInfo, appContext);
353 appContext->SetApplicationInfo(GetApplicationInfo());
354 return appContext;
355 }
356
InitResourceManager(const AppExecFwk::BundleInfo & bundleInfo,const std::shared_ptr<ContextImpl> & appContext,bool currentBundle,const std::string & moduleName) const357 void ContextImpl::InitResourceManager(const AppExecFwk::BundleInfo &bundleInfo,
358 const std::shared_ptr<ContextImpl> &appContext, bool currentBundle, const std::string& moduleName) const
359 {
360 std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
361 if (appContext == nullptr || resourceManager == nullptr) {
362 HILOG_ERROR("InitResourceManager create resourceManager failed");
363 return;
364 }
365 if (!moduleName.empty() || !bundleInfo.applicationInfo.multiProjects) {
366 HILOG_DEBUG("InitResourceManager hapModuleInfos count: %{public}zu", bundleInfo.hapModuleInfos.size());
367 std::regex inner_pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + GetBundleName());
368 std::regex outer_pattern(ABS_CODE_PATH);
369 for (auto hapModuleInfo : bundleInfo.hapModuleInfos) {
370 if (!moduleName.empty() && hapModuleInfo.moduleName != moduleName) {
371 continue;
372 }
373 std::string loadPath = (system::GetBoolParameter(COMPRESS_PROPERTY, false) &&
374 !hapModuleInfo.hapPath.empty()) ? hapModuleInfo.hapPath : hapModuleInfo.resourcePath;
375 if (loadPath.empty()) {
376 continue;
377 }
378 if (currentBundle) {
379 loadPath = std::regex_replace(loadPath, inner_pattern, LOCAL_CODE_PATH);
380 } else {
381 loadPath = std::regex_replace(loadPath, outer_pattern, LOCAL_BUNDLES);
382 }
383
384 HILOG_DEBUG("ContextImpl::InitResourceManager loadPath: %{public}s", loadPath.c_str());
385 if (!resourceManager->AddResource(loadPath.c_str())) {
386 HILOG_ERROR("InitResourceManager AddResource fail, moduleResPath: %{public}s", loadPath.c_str());
387 }
388 }
389 }
390
391 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
392 if (resConfig == nullptr) {
393 HILOG_ERROR("ContextImpl::InitResourceManager create ResConfig failed");
394 return;
395 }
396 #ifdef SUPPORT_GRAPHICS
397 UErrorCode status = U_ZERO_ERROR;
398 icu::Locale locale = icu::Locale::forLanguageTag(Global::I18n::LocaleConfig::GetSystemLanguage(), status);
399 resConfig->SetLocaleInfo(locale);
400 if (resConfig->GetLocaleInfo() != nullptr) {
401 HILOG_DEBUG("ContextImpl::InitResourceManager language: %{public}s, script: %{public}s, region: %{public}s,",
402 resConfig->GetLocaleInfo()->getLanguage(), resConfig->GetLocaleInfo()->getScript(),
403 resConfig->GetLocaleInfo()->getCountry());
404 } else {
405 HILOG_ERROR("ContextImpl::InitResourceManager language: GetLocaleInfo is null.");
406 }
407 #endif
408 resConfig->SetDeviceType(GetDeviceType());
409 resourceManager->UpdateResConfig(*resConfig);
410 appContext->SetResourceManager(resourceManager);
411 }
412
GetBundleManager() const413 sptr<AppExecFwk::IBundleMgr> ContextImpl::GetBundleManager() const
414 {
415 HILOG_DEBUG("ContextImpl::GetBundleManager");
416 auto instance = OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance();
417 if (instance == nullptr) {
418 HILOG_ERROR("failed to get SysMrgClient instance");
419 return nullptr;
420 }
421 auto bundleObj = instance->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
422 if (bundleObj == nullptr) {
423 HILOG_ERROR("failed to get bundle manager service");
424 return nullptr;
425 }
426 sptr<AppExecFwk::IBundleMgr> bms = iface_cast<AppExecFwk::IBundleMgr>(bundleObj);
427 return bms;
428 }
429
SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> & info)430 void ContextImpl::SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info)
431 {
432 if (info == nullptr) {
433 HILOG_ERROR("ContextImpl::SetApplicationInfo failed, info is empty");
434 return;
435 }
436 applicationInfo_ = info;
437 }
438
SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)439 void ContextImpl::SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
440 {
441 HILOG_DEBUG("ContextImpl::initResourceManager. Start.");
442 resourceManager_ = resourceManager;
443 HILOG_DEBUG("ContextImpl::initResourceManager. End.");
444 }
445
GetResourceManager() const446 std::shared_ptr<Global::Resource::ResourceManager> ContextImpl::GetResourceManager() const
447 {
448 if (parentContext_ != nullptr) {
449 return parentContext_->GetResourceManager();
450 }
451
452 return resourceManager_;
453 }
454
GetApplicationInfo() const455 std::shared_ptr<AppExecFwk::ApplicationInfo> ContextImpl::GetApplicationInfo() const
456 {
457 if (parentContext_ != nullptr) {
458 return parentContext_->GetApplicationInfo();
459 }
460
461 return applicationInfo_;
462 }
463
SetParentContext(const std::shared_ptr<Context> & context)464 void ContextImpl::SetParentContext(const std::shared_ptr<Context> &context)
465 {
466 parentContext_ = context;
467 }
468
GetBundleCodePath() const469 std::string ContextImpl::GetBundleCodePath() const
470 {
471 if (parentContext_ != nullptr) {
472 return parentContext_->GetBundleCodePath();
473 }
474 return (applicationInfo_ != nullptr) ? applicationInfo_->codePath : "";
475 }
476
InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> & abilityInfo)477 void ContextImpl::InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo)
478 {
479 if (hapModuleInfo_ != nullptr || abilityInfo == nullptr) {
480 return;
481 }
482 sptr<AppExecFwk::IBundleMgr> ptr = GetBundleManager();
483 if (ptr == nullptr) {
484 HILOG_ERROR("InitHapModuleInfo: failed to get bundle manager service");
485 return;
486 }
487
488 hapModuleInfo_ = std::make_shared<AppExecFwk::HapModuleInfo>();
489 if (!ptr->GetHapModuleInfo(*abilityInfo.get(), *hapModuleInfo_)) {
490 HILOG_ERROR("InitHapModuleInfo: GetHapModuleInfo failed, will retval false value");
491 }
492 }
493
InitHapModuleInfo(const AppExecFwk::HapModuleInfo & hapModuleInfo)494 void ContextImpl::InitHapModuleInfo(const AppExecFwk::HapModuleInfo &hapModuleInfo)
495 {
496 hapModuleInfo_ = std::make_shared<AppExecFwk::HapModuleInfo>(hapModuleInfo);
497 }
498
GetHapModuleInfo() const499 std::shared_ptr<AppExecFwk::HapModuleInfo> ContextImpl::GetHapModuleInfo() const
500 {
501 if (hapModuleInfo_ == nullptr) {
502 HILOG_DEBUG("ContextImpl::GetHapModuleInfo, hapModuleInfo is empty");
503 }
504 return hapModuleInfo_;
505 }
506
SetFlags(int64_t flags)507 void ContextImpl::SetFlags(int64_t flags)
508 {
509 flags_ = static_cast<uint64_t>(flags_) | static_cast<uint64_t>(CONTEXT_CREATE_BY_SYSTEM_APP);
510 }
511
IsCreateBySystemApp() const512 bool ContextImpl::IsCreateBySystemApp() const
513 {
514 return (static_cast<uint64_t>(flags_) & static_cast<uint64_t>(CONTEXT_CREATE_BY_SYSTEM_APP)) == 1;
515 }
516
517 std::shared_ptr<ApplicationContext> Context::applicationContext_ = nullptr;
518 std::mutex Context::contextMutex_;
519
GetApplicationContext()520 std::shared_ptr<ApplicationContext> Context::GetApplicationContext()
521 {
522 std::lock_guard<std::mutex> lock(contextMutex_);
523 return applicationContext_;
524 }
525
SetToken(const sptr<IRemoteObject> & token)526 void ContextImpl::SetToken(const sptr<IRemoteObject> &token)
527 {
528 if (token == nullptr) {
529 HILOG_DEBUG("ContextImpl::SetToken failed, application is nullptr");
530 return;
531 }
532 token_ = token;
533 }
534
GetToken()535 sptr<IRemoteObject> ContextImpl::GetToken()
536 {
537 return token_;
538 }
539
CreateDirIfNotExist(const std::string & dirPath,const mode_t & mode) const540 void ContextImpl::CreateDirIfNotExist(const std::string& dirPath, const mode_t& mode) const
541 {
542 HILOG_DEBUG("createDir: create directory if not exists.");
543 if (!OHOS::FileExists(dirPath)) {
544 bool createDir = OHOS::ForceCreateDirectory(dirPath);
545 if (!createDir) {
546 HILOG_ERROR("createDir: create dir %{public}s failed, errno is %{public}d.", dirPath.c_str(), errno);
547 return;
548 }
549 if (mode != 0) {
550 chmod(dirPath.c_str(), mode);
551 }
552 }
553 }
554
SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & config)555 void ContextImpl::SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config)
556 {
557 config_ = config;
558 }
559
KillProcessBySelf()560 void ContextImpl::KillProcessBySelf()
561 {
562 auto appMgrClient = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance();
563 appMgrClient->KillApplicationSelf();
564 }
565
GetProcessRunningInformation(AppExecFwk::RunningProcessInfo & info)566 int32_t ContextImpl::GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info)
567 {
568 auto appMgrClient = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance();
569 auto result = appMgrClient->GetProcessRunningInformation(info);
570 HILOG_DEBUG("result is %{public}d.", result);
571 return result;
572 }
573
GetConfiguration() const574 std::shared_ptr<AppExecFwk::Configuration> ContextImpl::GetConfiguration() const
575 {
576 return config_;
577 }
578
GetDeviceType() const579 Global::Resource::DeviceType ContextImpl::GetDeviceType() const
580 {
581 if (deviceType_ != Global::Resource::DeviceType::DEVICE_NOT_SET) {
582 return deviceType_;
583 }
584
585 auto config = GetConfiguration();
586 if (config != nullptr) {
587 auto deviceType = config->GetItem(AAFwk::GlobalConfigurationKey::DEVICE_TYPE);
588 HILOG_INFO("deviceType is %{public}s.", deviceType.c_str());
589 deviceType_ = AppExecFwk::ConvertDeviceType(deviceType);
590 }
591
592 if (deviceType_ == Global::Resource::DeviceType::DEVICE_NOT_SET) {
593 deviceType_ = Global::Resource::DeviceType::DEVICE_PHONE;
594 }
595 HILOG_DEBUG("deviceType is %{public}d.", deviceType_);
596 return deviceType_;
597 }
598 } // namespace AbilityRuntime
599 } // namespace OHOS
600