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_impl.h"
17
18 #include <cerrno>
19 #include <regex>
20
21 #include "ability_manager_client.h"
22 #include "app_mgr_client.h"
23 #include "application_context.h"
24 #include "bundle_mgr_helper.h"
25 #include "bundle_mgr_proxy.h"
26 #include "common_event_manager.h"
27 #include "configuration_convertor.h"
28 #include "constants.h"
29 #include "directory_ex.h"
30 #include "file_ex.h"
31 #include "hilog_tag_wrapper.h"
32 #include "hitrace_meter.h"
33 #include "ipc_object_proxy.h"
34 #include "ipc_singleton.h"
35 #include "js_runtime_utils.h"
36 #ifdef SUPPORT_GRAPHICS
37 #include "locale_config.h"
38 #endif
39 #include "os_account_manager_wrapper.h"
40 #include "overlay_event_subscriber.h"
41 #include "overlay_module_info.h"
42 #include "parameters.h"
43 #include "running_process_info.h"
44 #include "sys_mgr_client.h"
45 #include "system_ability_definition.h"
46
47 namespace OHOS {
48 namespace AbilityRuntime {
49 #ifdef SUPPORT_GRAPHICS
50 std::mutex ContextImpl::getDisplayConfigCallbackMutex_;
51 GetDisplayConfigCallback ContextImpl::getDisplayConfigCallback_ = nullptr;
52 #endif
53 using namespace OHOS::AbilityBase::Constants;
54
55 const std::string PATTERN_VERSION = std::string(FILE_SEPARATOR) + "v\\d+" + FILE_SEPARATOR;
56
57 const size_t Context::CONTEXT_TYPE_ID(std::hash<const char*> {} ("Context"));
58 const int64_t ContextImpl::CONTEXT_CREATE_BY_SYSTEM_APP(0x00000001);
59 const mode_t MODE = 0770;
60 const mode_t GROUP_MODE = 02770;
61 const std::string ContextImpl::CONTEXT_DATA_APP("/data/app/");
62 const std::string ContextImpl::CONTEXT_BUNDLE("/bundle/");
63 const std::string ContextImpl::CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE("/mnt/hmdfs/");
64 const std::string ContextImpl::CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE("/device_view/local/data/");
65 const std::string ContextImpl::CONTEXT_DISTRIBUTEDFILES("distributedfiles");
66 const std::string ContextImpl::CONTEXT_CLOUDFILE("cloud");
67 const std::string ContextImpl::CONTEXT_FILE_SEPARATOR("/");
68 const std::string ContextImpl::CONTEXT_DATA("/data/");
69 const std::string ContextImpl::CONTEXT_DATA_STORAGE("/data/storage/");
70 const std::string ContextImpl::CONTEXT_BASE("base");
71 const std::string ContextImpl::CONTEXT_CACHE("cache");
72 const std::string ContextImpl::CONTEXT_PREFERENCES("preferences");
73 const std::string ContextImpl::CONTEXT_GROUP("group");
74 const std::string ContextImpl::CONTEXT_DATABASE("database");
75 const std::string ContextImpl::CONTEXT_TEMP("/temp");
76 const std::string ContextImpl::CONTEXT_FILES("/files");
77 const std::string ContextImpl::CONTEXT_HAPS("/haps");
78 const std::string ContextImpl::CONTEXT_ELS[] = {"el1", "el2", "el3", "el4", "el5"};
79 const std::string ContextImpl::CONTEXT_RESOURCE_END = "/resources/resfile";
80 Global::Resource::DeviceType ContextImpl::deviceType_ = Global::Resource::DeviceType::DEVICE_NOT_SET;
81 const std::string OVERLAY_STATE_CHANGED = "usual.event.OVERLAY_STATE_CHANGED";
82 const int32_t TYPE_RESERVE = 1;
83 const int32_t TYPE_OTHERS = 2;
84 const int32_t API11 = 11;
85 const int32_t API_VERSION_MOD = 100;
86 const int32_t ERR_ABILITY_RUNTIME_EXTERNAL_NOT_SYSTEM_HSP = 16400001;
87 const int AREA2 = 2;
88 const int AREA3 = 3;
89 const int AREA4 = 4;
90
~ContextImpl()91 ContextImpl::~ContextImpl()
92 {
93 UnsubscribeToOverlayEvents();
94 }
95
GetBundleName() const96 std::string ContextImpl::GetBundleName() const
97 {
98 if (parentContext_ != nullptr) {
99 return parentContext_->GetBundleName();
100 }
101 return (applicationInfo_ != nullptr) ? applicationInfo_->bundleName : "";
102 }
103
GetBundleNameWithContext(std::shared_ptr<Context> inputContext) const104 std::string ContextImpl::GetBundleNameWithContext(std::shared_ptr<Context> inputContext) const
105 {
106 if (inputContext) {
107 return inputContext->GetBundleName();
108 }
109 return GetBundleName();
110 }
111
GetBundleCodeDir()112 std::string ContextImpl::GetBundleCodeDir()
113 {
114 auto appInfo = GetApplicationInfo();
115 if (appInfo == nullptr) {
116 return "";
117 }
118
119 std::string dir;
120 if (IsCreateBySystemApp()) {
121 dir = std::regex_replace(appInfo->codePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
122 } else {
123 dir = LOCAL_CODE_PATH;
124 }
125 CreateDirIfNotExist(dir, MODE);
126 TAG_LOGD(AAFwkTag::APPKIT, "dir:%{public}s", dir.c_str());
127 return dir;
128 }
129
GetCacheDir()130 std::string ContextImpl::GetCacheDir()
131 {
132 std::string dir = GetBaseDir() + CONTEXT_FILE_SEPARATOR + CONTEXT_CACHE;
133 CreateDirIfNotExist(dir, MODE);
134 TAG_LOGD(AAFwkTag::APPKIT, "dir:%{public}s", dir.c_str());
135 return dir;
136 }
137
IsUpdatingConfigurations()138 bool ContextImpl::IsUpdatingConfigurations()
139 {
140 return false;
141 }
142
PrintDrawnCompleted()143 bool ContextImpl::PrintDrawnCompleted()
144 {
145 return false;
146 }
147
CreateDirIfNotExistWithCheck(const std::string & dirPath,const mode_t & mode,bool checkExist)148 void ContextImpl::CreateDirIfNotExistWithCheck(const std::string &dirPath, const mode_t &mode, bool checkExist)
149 {
150 if (checkExist) {
151 CreateDirIfNotExist(dirPath, mode);
152 return;
153 }
154 // Check if the dirPath exists on the first call
155 std::lock_guard<std::mutex> lock(checkedDirSetLock_);
156 if (checkedDirSet_.find(dirPath) != checkedDirSet_.end()) {
157 return;
158 }
159 checkedDirSet_.emplace(dirPath);
160 CreateDirIfNotExist(dirPath, mode);
161 }
162
GetDatabaseDirWithCheck(bool checkExist,std::string & databaseDir)163 int32_t ContextImpl::GetDatabaseDirWithCheck(bool checkExist, std::string &databaseDir)
164 {
165 if (IsCreateBySystemApp()) {
166 databaseDir = CONTEXT_DATA_APP + currArea_ + CONTEXT_FILE_SEPARATOR + std::to_string(GetCurrentAccountId())
167 + CONTEXT_FILE_SEPARATOR + CONTEXT_DATABASE + CONTEXT_FILE_SEPARATOR + GetBundleName();
168 } else {
169 databaseDir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_DATABASE;
170 }
171 if (parentContext_ != nullptr) {
172 databaseDir = databaseDir + CONTEXT_FILE_SEPARATOR +
173 ((GetHapModuleInfo() == nullptr) ? "" : GetHapModuleInfo()->moduleName);
174 }
175 CreateDirIfNotExistWithCheck(databaseDir, 0, checkExist);
176 return ERR_OK;
177 }
178
GetGroupDatabaseDirWithCheck(const std::string & groupId,bool checkExist,std::string & databaseDir)179 int32_t ContextImpl::GetGroupDatabaseDirWithCheck(const std::string &groupId, bool checkExist, std::string &databaseDir)
180 {
181 int32_t ret = GetGroupDirWithCheck(groupId, checkExist, databaseDir);
182 if (ret != ERR_OK) {
183 return ret;
184 }
185 databaseDir = databaseDir + CONTEXT_FILE_SEPARATOR + CONTEXT_DATABASE;
186 CreateDirIfNotExistWithCheck(databaseDir, GROUP_MODE, checkExist);
187 return ERR_OK;
188 }
189
GetSystemDatabaseDir(const std::string & groupId,bool checkExist,std::string & databaseDir)190 int32_t ContextImpl::GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir)
191 {
192 int32_t ret;
193 if (groupId.empty()) {
194 ret = GetDatabaseDirWithCheck(checkExist, databaseDir);
195 } else {
196 ret = GetGroupDatabaseDirWithCheck(groupId, checkExist, databaseDir);
197 }
198 TAG_LOGD(AAFwkTag::APPKIT, "databaseDir: %{public}s", databaseDir.c_str());
199 return ret;
200 }
201
GetDatabaseDir()202 std::string ContextImpl::GetDatabaseDir()
203 {
204 std::string dir;
205 GetDatabaseDirWithCheck(true, dir);
206 TAG_LOGD(AAFwkTag::APPKIT, "databaseDir: %{public}s", dir.c_str());
207 return dir;
208 }
209
GetPreferencesDirWithCheck(bool checkExist,std::string & preferencesDir)210 int32_t ContextImpl::GetPreferencesDirWithCheck(bool checkExist, std::string &preferencesDir)
211 {
212 preferencesDir = GetBaseDir() + CONTEXT_FILE_SEPARATOR + CONTEXT_PREFERENCES;
213 CreateDirIfNotExistWithCheck(preferencesDir, MODE, checkExist);
214 return ERR_OK;
215 }
216
GetGroupPreferencesDirWithCheck(const std::string & groupId,bool checkExist,std::string & preferencesDir)217 int32_t ContextImpl::GetGroupPreferencesDirWithCheck(const std::string &groupId, bool checkExist,
218 std::string &preferencesDir)
219 {
220 int32_t ret = GetGroupDirWithCheck(groupId, checkExist, preferencesDir);
221 if (ret != ERR_OK) {
222 return ret;
223 }
224 preferencesDir = preferencesDir + CONTEXT_FILE_SEPARATOR + CONTEXT_PREFERENCES;
225 CreateDirIfNotExistWithCheck(preferencesDir, GROUP_MODE, checkExist);
226 return ERR_OK;
227 }
228
GetSystemPreferencesDir(const std::string & groupId,bool checkExist,std::string & preferencesDir)229 int32_t ContextImpl::GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir)
230 {
231 int32_t ret;
232 if (groupId.empty()) {
233 ret = GetPreferencesDirWithCheck(checkExist, preferencesDir);
234 } else {
235 ret = GetGroupPreferencesDirWithCheck(groupId, checkExist, preferencesDir);
236 }
237 TAG_LOGD(AAFwkTag::APPKIT, "preferencesDir: %{public}s", preferencesDir.c_str());
238 return ret;
239 }
240
GetPreferencesDir()241 std::string ContextImpl::GetPreferencesDir()
242 {
243 std::string dir;
244 GetPreferencesDirWithCheck(true, dir);
245 TAG_LOGD(AAFwkTag::APPKIT, "preferencesDir: %{public}s", dir.c_str());
246 return dir;
247 }
248
GetGroupDirWithCheck(const std::string & groupId,bool checkExist,std::string & groupDir)249 int32_t ContextImpl::GetGroupDirWithCheck(const std::string &groupId, bool checkExist, std::string &groupDir)
250 {
251 if (currArea_ == CONTEXT_ELS[0]) {
252 TAG_LOGE(AAFwkTag::APPKIT, "GroupDir currently can't supports the el1 level");
253 return ERR_INVALID_VALUE;
254 }
255 int errCode = GetBundleManager();
256 if (errCode != ERR_OK) {
257 TAG_LOGE(AAFwkTag::APPKIT, "failed, errCode: %{public}d", errCode);
258 return errCode;
259 }
260 std::string groupDirGet;
261 bool ret = bundleMgr_->GetGroupDir(groupId, groupDirGet);
262 if (!ret || groupDirGet.empty()) {
263 TAG_LOGE(AAFwkTag::APPKIT, "GetGroupDir failed or groupDirGet is empty");
264 return ERR_INVALID_VALUE;
265 }
266 std::string uuid = groupDirGet.substr(groupDirGet.rfind('/'));
267 groupDir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_GROUP + uuid;
268 CreateDirIfNotExistWithCheck(groupDir, MODE, true);
269 return ERR_OK;
270 }
271
GetGroupDir(std::string groupId)272 std::string ContextImpl::GetGroupDir(std::string groupId)
273 {
274 std::string dir;
275 GetGroupDirWithCheck(groupId, true, dir);
276 TAG_LOGD(AAFwkTag::APPKIT, "GroupDir:%{public}s", dir.c_str());
277 return dir;
278 }
279
GetTempDir()280 std::string ContextImpl::GetTempDir()
281 {
282 std::string dir = GetBaseDir() + CONTEXT_TEMP;
283 CreateDirIfNotExist(dir, MODE);
284 TAG_LOGD(AAFwkTag::APPKIT, "dir:%{public}s", dir.c_str());
285 return dir;
286 }
287
GetResourceDir()288 std::string ContextImpl::GetResourceDir()
289 {
290 std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfoPtr = GetHapModuleInfo();
291 if (hapModuleInfoPtr == nullptr || hapModuleInfoPtr->moduleName.empty()) {
292 return "";
293 }
294 std::string dir = std::string(LOCAL_CODE_PATH) + CONTEXT_FILE_SEPARATOR +
295 hapModuleInfoPtr->moduleName + CONTEXT_RESOURCE_END;
296 if (OHOS::FileExists(dir)) {
297 return dir;
298 }
299 return "";
300 }
301
GetAllTempDir(std::vector<std::string> & tempPaths)302 void ContextImpl::GetAllTempDir(std::vector<std::string> &tempPaths)
303 {
304 // Application temp dir
305 auto appTemp = GetTempDir();
306 if (OHOS::FileExists(appTemp)) {
307 tempPaths.push_back(appTemp);
308 }
309 // Module dir
310 if (applicationInfo_ == nullptr) {
311 TAG_LOGE(AAFwkTag::APPKIT, "The application info is empty");
312 return;
313 }
314
315 std::string baseDir;
316 if (IsCreateBySystemApp()) {
317 baseDir = CONTEXT_DATA_APP + currArea_ + CONTEXT_FILE_SEPARATOR + std::to_string(GetCurrentAccountId()) +
318 CONTEXT_FILE_SEPARATOR + CONTEXT_BASE + CONTEXT_FILE_SEPARATOR + GetBundleName();
319 } else {
320 baseDir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_BASE;
321 }
322 for (const auto &moudleItem: applicationInfo_->moduleInfos) {
323 auto moudleTemp = baseDir + CONTEXT_HAPS + CONTEXT_FILE_SEPARATOR + moudleItem.moduleName + CONTEXT_TEMP;
324 if (!OHOS::FileExists(moudleTemp)) {
325 TAG_LOGW(AAFwkTag::APPKIT, "moudle[%{public}s] temp path not exist,path: %{public}s",
326 moudleItem.moduleName.c_str(), moudleTemp.c_str());
327 continue;
328 }
329 tempPaths.push_back(moudleTemp);
330 }
331 }
332
GetFilesDir()333 std::string ContextImpl::GetFilesDir()
334 {
335 std::string dir = GetBaseDir() + CONTEXT_FILES;
336 CreateDirIfNotExist(dir, MODE);
337 TAG_LOGD(AAFwkTag::APPKIT, "dir:%{public}s", dir.c_str());
338 return dir;
339 }
340
GetDistributedFilesDir()341 std::string ContextImpl::GetDistributedFilesDir()
342 {
343 TAG_LOGD(AAFwkTag::APPKIT, "called");
344 std::string dir;
345 if (IsCreateBySystemApp()) {
346 dir = CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE + std::to_string(GetCurrentAccountId()) +
347 CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE + GetBundleName();
348 } else {
349 if (currArea_ == CONTEXT_ELS[1] || currArea_ == CONTEXT_ELS[AREA2] || currArea_ == CONTEXT_ELS[AREA3] ||
350 currArea_ == CONTEXT_ELS[AREA4]) {
351 // when areamode swith to el3/el4/el5, the distributedfiles dir should be always el2's
352 // distributedfilesdir dir
353 dir = CONTEXT_DATA_STORAGE + CONTEXT_ELS[1] + CONTEXT_FILE_SEPARATOR + CONTEXT_DISTRIBUTEDFILES;
354 } else {
355 dir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_DISTRIBUTEDFILES;
356 }
357 }
358 CreateDirIfNotExist(dir, 0);
359 TAG_LOGD(AAFwkTag::APPKIT, "dir:%{public}s", dir.c_str());
360 return dir;
361 }
362
GetCloudFileDir()363 std::string ContextImpl::GetCloudFileDir()
364 {
365 std::string dir = CONTEXT_DATA_STORAGE + CONTEXT_ELS[1] + CONTEXT_FILE_SEPARATOR + CONTEXT_CLOUDFILE;
366 CreateDirIfNotExist(dir, MODE);
367 return dir;
368 }
369
SwitchArea(int mode)370 void ContextImpl::SwitchArea(int mode)
371 {
372 TAG_LOGD(AAFwkTag::APPKIT, "mode:%{public}d", mode);
373 if (mode < 0 || mode >= (int)(sizeof(CONTEXT_ELS) / sizeof(CONTEXT_ELS[0]))) {
374 TAG_LOGE(AAFwkTag::APPKIT, "ContextImpl::SwitchArea, mode is invalid.");
375 return;
376 }
377 currArea_ = CONTEXT_ELS[mode];
378 TAG_LOGD(AAFwkTag::APPKIT, "currArea:%{public}s", currArea_.c_str());
379 }
380
SetMcc(std::string mcc)381 void ContextImpl::SetMcc(std::string mcc)
382 {
383 TAG_LOGD(AAFwkTag::APPKIT, "mcc:%{public}s", mcc.c_str());
384 if (config_) {
385 config_->AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_MCC, mcc);
386 }
387 }
388
SetMnc(std::string mnc)389 void ContextImpl::SetMnc(std::string mnc)
390 {
391 TAG_LOGD(AAFwkTag::APPKIT, "mnc:%{public}s", mnc.c_str());
392 if (config_) {
393 config_->AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_MNC, mnc);
394 }
395 }
396
CreateModuleContext(const std::string & moduleName,std::shared_ptr<Context> inputContext)397 std::shared_ptr<Context> ContextImpl::CreateModuleContext(const std::string &moduleName,
398 std::shared_ptr<Context> inputContext)
399 {
400 return CreateModuleContext(GetBundleNameWithContext(inputContext), moduleName, inputContext);
401 }
402
CreateModuleContext(const std::string & bundleName,const std::string & moduleName,std::shared_ptr<Context> inputContext)403 std::shared_ptr<Context> ContextImpl::CreateModuleContext(const std::string &bundleName, const std::string &moduleName,
404 std::shared_ptr<Context> inputContext)
405 {
406 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
407 TAG_LOGD(AAFwkTag::APPKIT, "begin");
408 if (bundleName.empty() || moduleName.empty()) {
409 return nullptr;
410 }
411
412 TAG_LOGD(AAFwkTag::APPKIT, "length: %{public}zu, bundleName: %{public}s",
413 (size_t)bundleName.length(), bundleName.c_str());
414
415 int accountId = GetCurrentAccountId();
416 if (accountId == 0) {
417 accountId = GetCurrentActiveAccountId();
418 }
419
420 AppExecFwk::BundleInfo bundleInfo;
421 GetBundleInfo(bundleName, bundleInfo, accountId, inputContext);
422 if (bundleInfo.name.empty() || bundleInfo.applicationInfo.name.empty()) {
423 TAG_LOGE(AAFwkTag::APPKIT, "GetBundleInfo is error");
424 ErrCode ret = bundleMgr_->GetDependentBundleInfo(bundleName, bundleInfo,
425 AppExecFwk::GetDependentBundleInfoFlag::GET_ALL_DEPENDENT_BUNDLE_INFO);
426 if (ret != ERR_OK) {
427 TAG_LOGE(AAFwkTag::APPKIT, "GetDependentBundleInfo failed:%{public}d", ret);
428 return nullptr;
429 }
430 }
431
432 std::shared_ptr<ContextImpl> appContext = std::make_shared<ContextImpl>();
433 if (bundleInfo.applicationInfo.codePath != std::to_string(TYPE_RESERVE) &&
434 bundleInfo.applicationInfo.codePath != std::to_string(TYPE_OTHERS)) {
435 TAG_LOGD(AAFwkTag::APPKIT, "modulename: %{public}s, bundleName: %{public}s",
436 moduleName.c_str(), bundleName.c_str());
437 auto info = std::find_if(bundleInfo.hapModuleInfos.begin(), bundleInfo.hapModuleInfos.end(),
438 [&moduleName](const AppExecFwk::HapModuleInfo &hapModuleInfo) {
439 return hapModuleInfo.moduleName == moduleName;
440 });
441 if (info == bundleInfo.hapModuleInfos.end()) {
442 TAG_LOGE(AAFwkTag::APPKIT, "moduleName is error");
443 return nullptr;
444 }
445 appContext->InitHapModuleInfo(*info);
446 }
447
448 appContext->SetConfiguration(config_);
449 bool self = false;
450 if (inputContext) {
451 self = (bundleName == inputContext->GetBundleName());
452 } else {
453 self = bundleName == GetBundleName();
454 }
455 InitResourceManager(bundleInfo, appContext, self, moduleName, inputContext);
456 appContext->SetApplicationInfo(std::make_shared<AppExecFwk::ApplicationInfo>(bundleInfo.applicationInfo));
457 return appContext;
458 }
459
CreateModuleContext(const std::string & moduleName)460 std::shared_ptr<Context> ContextImpl::CreateModuleContext(const std::string &moduleName)
461 {
462 return CreateModuleContext(GetBundleName(), moduleName, nullptr);
463 }
464
CreateModuleContext(const std::string & bundleName,const std::string & moduleName)465 std::shared_ptr<Context> ContextImpl::CreateModuleContext(const std::string &bundleName, const std::string &moduleName)
466 {
467 return CreateModuleContext(bundleName, moduleName, nullptr);
468 }
469
CreateModuleResourceManager(const std::string & bundleName,const std::string & moduleName)470 std::shared_ptr<Global::Resource::ResourceManager> ContextImpl::CreateModuleResourceManager(
471 const std::string &bundleName, const std::string &moduleName)
472 {
473 TAG_LOGD(AAFwkTag::APPKIT, "begin, bundleName: %{public}s, moduleName: %{public}s",
474 bundleName.c_str(), moduleName.c_str());
475 if (bundleName.empty() || moduleName.empty()) {
476 TAG_LOGE(AAFwkTag::APPKIT, "bundleName is %{public}s, moduleName is %{public}s",
477 bundleName.c_str(), moduleName.c_str());
478 return nullptr;
479 }
480
481 AppExecFwk::BundleInfo bundleInfo;
482 bool currentBundle = false;
483 if (GetBundleInfo(bundleName, bundleInfo, currentBundle) != ERR_OK) {
484 TAG_LOGE(AAFwkTag::APPKIT, "Failed to GetBundleInfo, bundleName: %{public}s", bundleName.c_str());
485 return nullptr;
486 }
487
488 if (bundleInfo.applicationInfo.codePath == std::to_string(TYPE_RESERVE) ||
489 bundleInfo.applicationInfo.codePath == std::to_string(TYPE_OTHERS)) {
490 std::shared_ptr<Global::Resource::ResourceManager> resourceManager = InitOthersResourceManagerInner(
491 bundleInfo, currentBundle, moduleName);
492 if (resourceManager == nullptr) {
493 TAG_LOGE(AAFwkTag::APPKIT, "InitOthersResourceManagerInner create resourceManager failed");
494 }
495 return resourceManager;
496 }
497
498 std::shared_ptr<Global::Resource::ResourceManager> resourceManager = InitResourceManagerInner(
499 bundleInfo, currentBundle, moduleName);
500 if (resourceManager == nullptr) {
501 TAG_LOGE(AAFwkTag::APPKIT, "InitResourceManagerInner create resourceManager failed");
502 return nullptr;
503 }
504 UpdateResConfig(GetResourceManager(), resourceManager);
505 return resourceManager;
506 }
507
CreateSystemHspModuleResourceManager(const std::string & bundleName,const std::string & moduleName,std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)508 int32_t ContextImpl::CreateSystemHspModuleResourceManager(const std::string &bundleName,
509 const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
510 {
511 TAG_LOGD(AAFwkTag::APPKIT, "begin, bundleName: %{public}s, moduleName: %{public}s",
512 bundleName.c_str(), moduleName.c_str());
513 if (bundleName.empty() || moduleName.empty()) {
514 TAG_LOGE(AAFwkTag::APPKIT, "bundleName is %{public}s, moduleName is %{public}s",
515 bundleName.c_str(), moduleName.c_str());
516 return ERR_INVALID_VALUE;
517 }
518
519 int accountId = GetCurrentAccountId();
520 if (accountId == 0) {
521 accountId = GetCurrentActiveAccountId();
522 }
523 AppExecFwk::BundleInfo bundleInfo;
524 GetBundleInfo(bundleName, bundleInfo, accountId);
525 if (bundleInfo.name.empty() || bundleInfo.applicationInfo.name.empty()) {
526 TAG_LOGW(AAFwkTag::APPKIT, "GetBundleInfo is error");
527 ErrCode ret = bundleMgr_->GetDependentBundleInfo(bundleName, bundleInfo,
528 AppExecFwk::GetDependentBundleInfoFlag::GET_ALL_DEPENDENT_BUNDLE_INFO);
529 if (ret != ERR_OK) {
530 TAG_LOGE(AAFwkTag::APPKIT, "GetDependentBundleInfo failed:%{public}d", ret);
531 return ERR_INVALID_VALUE;
532 }
533 }
534
535 if (bundleInfo.applicationInfo.bundleType != AppExecFwk::BundleType::APP_SERVICE_FWK) {
536 TAG_LOGE(AAFwkTag::APPKIT, "input bundleName:%{public}s is not system hsp", bundleName.c_str());
537 return ERR_ABILITY_RUNTIME_EXTERNAL_NOT_SYSTEM_HSP;
538 }
539
540 std::string selfBundleName = GetBundleName();
541 if (bundleInfo.applicationInfo.codePath == std::to_string(TYPE_RESERVE) ||
542 bundleInfo.applicationInfo.codePath == std::to_string(TYPE_OTHERS)) {
543 resourceManager = InitOthersResourceManagerInner(bundleInfo, selfBundleName == bundleName, moduleName);
544 if (resourceManager == nullptr) {
545 TAG_LOGE(AAFwkTag::APPKIT, "InitOthersResourceManagerInner create resourceManager failed");
546 }
547 return ERR_INVALID_VALUE;
548 }
549
550 resourceManager = InitResourceManagerInner(bundleInfo, selfBundleName == bundleName, moduleName);
551 if (resourceManager == nullptr) {
552 TAG_LOGE(AAFwkTag::APPKIT, "InitResourceManagerInner create resourceManager failed");
553 return ERR_INVALID_VALUE;
554 }
555 UpdateResConfig(GetResourceManager(), resourceManager);
556 return ERR_OK;
557 }
558
GetBundleInfo(const std::string & bundleName,AppExecFwk::BundleInfo & bundleInfo,bool & currentBundle)559 int32_t ContextImpl::GetBundleInfo(const std::string &bundleName, AppExecFwk::BundleInfo &bundleInfo,
560 bool ¤tBundle)
561 {
562 std::string currentBundleName;
563 auto appContext = ApplicationContext::GetInstance();
564 if (appContext != nullptr) {
565 currentBundleName = appContext->GetBundleName();
566 }
567 currentBundle = bundleName == currentBundleName;
568
569 int errCode = GetBundleManager();
570 if (errCode != ERR_OK) {
571 TAG_LOGE(AAFwkTag::APPKIT, "errCode: %{public}d", errCode);
572 return errCode;
573 }
574
575 if (currentBundle) {
576 bundleMgr_->GetBundleInfoForSelf((
577 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
578 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY) +
579 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) +
580 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE) +
581 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO) +
582 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY) +
583 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA)), bundleInfo);
584 } else {
585 int accountId = GetCurrentAccountId();
586 if (accountId == 0) {
587 accountId = GetCurrentActiveAccountId();
588 }
589 bundleMgr_->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, accountId);
590 }
591
592 if (bundleInfo.name.empty() || bundleInfo.applicationInfo.name.empty()) {
593 TAG_LOGW(AAFwkTag::APPKIT, "bundleInfo is empty");
594 ErrCode ret = bundleMgr_->GetUninstalledBundleInfo(bundleName, bundleInfo);
595 if (ret != ERR_OK) {
596 TAG_LOGE(AAFwkTag::APPKIT, "GetUninstalledBundleInfo failed:%{public}d", ret);
597 return ret;
598 }
599 }
600 return ERR_OK;
601 }
602
GetBundleInfo(const std::string & bundleName,AppExecFwk::BundleInfo & bundleInfo,const int & accountId,std::shared_ptr<Context> inputContext)603 void ContextImpl::GetBundleInfo(const std::string &bundleName, AppExecFwk::BundleInfo &bundleInfo,
604 const int &accountId, std::shared_ptr<Context> inputContext)
605 {
606 TAG_LOGD(AAFwkTag::APPKIT, "begin");
607 if (bundleMgr_ == nullptr) {
608 int errCode = GetBundleManager();
609 if (errCode != ERR_OK) {
610 TAG_LOGE(AAFwkTag::APPKIT, "failed, errCode: %{public}d", errCode);
611 return;
612 }
613 }
614
615 if (bundleName == GetBundleNameWithContext(inputContext)) {
616 bundleMgr_->GetBundleInfoForSelf(
617 (static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
618 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY) +
619 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) +
620 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE) +
621 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO) +
622 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY) +
623 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA)), bundleInfo);
624 } else {
625 bundleMgr_->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, accountId);
626 }
627 }
628
GetArea()629 int ContextImpl::GetArea()
630 {
631 TAG_LOGD(AAFwkTag::APPKIT, "begin");
632 int mode = -1;
633 for (int i = 0; i < (int)(sizeof(CONTEXT_ELS) / sizeof(CONTEXT_ELS[0])); i++) {
634 if (currArea_ == CONTEXT_ELS[i]) {
635 mode = i;
636 break;
637 }
638 }
639 if (mode == -1) {
640 TAG_LOGE(AAFwkTag::APPKIT, "not find mode");
641 return EL_DEFAULT;
642 }
643 TAG_LOGD(AAFwkTag::APPKIT, "end");
644 return mode;
645 }
646
GetBaseDir() const647 std::string ContextImpl::GetBaseDir() const
648 {
649 std::string baseDir;
650 if (IsCreateBySystemApp()) {
651 baseDir = CONTEXT_DATA_APP + currArea_ + CONTEXT_FILE_SEPARATOR + std::to_string(GetCurrentAccountId()) +
652 CONTEXT_FILE_SEPARATOR + CONTEXT_BASE + CONTEXT_FILE_SEPARATOR + GetBundleName();
653 } else {
654 baseDir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_BASE;
655 }
656 if (parentContext_ != nullptr) {
657 baseDir = baseDir + CONTEXT_HAPS + CONTEXT_FILE_SEPARATOR +
658 ((GetHapModuleInfo() == nullptr) ? "" : GetHapModuleInfo()->moduleName);
659 }
660
661 TAG_LOGD(AAFwkTag::APPKIT, "Dir:%{public}s", baseDir.c_str());
662 return baseDir;
663 }
664
GetCurrentAccountId() const665 int ContextImpl::GetCurrentAccountId() const
666 {
667 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
668 int userId = 0;
669 auto instance = DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance();
670 if (instance == nullptr) {
671 TAG_LOGE(AAFwkTag::APPKIT, "Failed to get OsAccountManager instance");
672 return userId;
673 }
674 instance->GetOsAccountLocalIdFromProcess(userId);
675 return userId;
676 }
677
GetCurrentActiveAccountId() const678 int ContextImpl::GetCurrentActiveAccountId() const
679 {
680 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
681 std::vector<int> accountIds;
682 auto instance = DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance();
683 if (instance == nullptr) {
684 TAG_LOGE(AAFwkTag::APPKIT, "Failed to get OsAccountManager instance");
685 return 0;
686 }
687 ErrCode ret = instance->QueryActiveOsAccountIds(accountIds);
688 if (ret != ERR_OK) {
689 TAG_LOGE(AAFwkTag::APPKIT, "ContextImpl::GetCurrentActiveAccountId error");
690 return 0;
691 }
692
693 if (accountIds.size() == 0) {
694 TAG_LOGE(AAFwkTag::APPKIT, "no accounts");
695 return 0;
696 }
697 return accountIds[0];
698 }
699
CreateBundleContext(std::shared_ptr<Context> & context,const std::string & bundleName,std::shared_ptr<Context> inputContext)700 int32_t ContextImpl::CreateBundleContext(std::shared_ptr<Context> &context, const std::string &bundleName,
701 std::shared_ptr<Context> inputContext)
702 {
703 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
704
705 if (bundleName.empty()) {
706 TAG_LOGE(AAFwkTag::APPKIT, "bundleName is empty");
707 return ERR_INVALID_VALUE;
708 }
709
710 int errCode = GetBundleManager();
711 if (errCode != ERR_OK) {
712 TAG_LOGE(AAFwkTag::APPKIT, "failed, errCode: %{public}d", errCode);
713 return ERR_INVALID_VALUE;
714 }
715
716 AppExecFwk::BundleInfo bundleInfo;
717 int accountId = GetCurrentAccountId();
718 if (accountId == 0) {
719 accountId = GetCurrentActiveAccountId();
720 }
721
722 TAG_LOGD(AAFwkTag::APPKIT, "length: %{public}zu, bundleName: %{public}s",
723 (size_t)bundleName.length(), bundleName.c_str());
724 bundleMgr_->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, accountId);
725
726 if (bundleInfo.name.empty() || bundleInfo.applicationInfo.name.empty()) {
727 TAG_LOGE(AAFwkTag::APPKIT, "bundleInfo is empty");
728 return ERR_INVALID_VALUE;
729 }
730
731 std::shared_ptr<ContextImpl> appContext = std::make_shared<ContextImpl>();
732 appContext->SetFlags(CONTEXT_CREATE_BY_SYSTEM_APP);
733 appContext->SetConfiguration(config_);
734
735 // init resourceManager.
736 InitResourceManager(bundleInfo, appContext, false, "", inputContext);
737
738 appContext->SetApplicationInfo(std::make_shared<AppExecFwk::ApplicationInfo>(bundleInfo.applicationInfo));
739 context = appContext;
740 return ERR_OK;
741 }
742
CreateBundleContext(const std::string & bundleName)743 std::shared_ptr<Context> ContextImpl::CreateBundleContext(const std::string &bundleName)
744 {
745 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
746 TAG_LOGD(AAFwkTag::APPKIT, "begin");
747 if (parentContext_ != nullptr) {
748 return parentContext_->CreateBundleContext(bundleName);
749 }
750
751 if (bundleName.empty()) {
752 TAG_LOGE(AAFwkTag::APPKIT, "bundleName is empty");
753 return nullptr;
754 }
755
756 int errCode = GetBundleManager();
757 if (errCode != ERR_OK) {
758 TAG_LOGE(AAFwkTag::APPKIT, "failed, errCode: %{public}d", errCode);
759 return nullptr;
760 }
761
762 AppExecFwk::BundleInfo bundleInfo;
763 int accountId = GetCurrentAccountId();
764 if (accountId == 0) {
765 accountId = GetCurrentActiveAccountId();
766 }
767 TAG_LOGD(AAFwkTag::APPKIT, "length: %{public}zu, bundleName: %{public}s",
768 (size_t)bundleName.length(), bundleName.c_str());
769 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "bundleMgr_->GetBundleInfo");
770 bundleMgr_->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, accountId);
771
772 if (bundleInfo.name.empty() || bundleInfo.applicationInfo.name.empty()) {
773 TAG_LOGE(AAFwkTag::APPKIT, "bundleInfo is empty");
774 return nullptr;
775 }
776
777 std::shared_ptr<ContextImpl> appContext = std::make_shared<ContextImpl>();
778 appContext->SetFlags(CONTEXT_CREATE_BY_SYSTEM_APP);
779 appContext->SetConfiguration(config_);
780
781 // init resourceManager.
782 InitResourceManager(bundleInfo, appContext);
783 appContext->SetApplicationInfo(std::make_shared<AppExecFwk::ApplicationInfo>(bundleInfo.applicationInfo));
784 return appContext;
785 }
786
InitResourceManager(const AppExecFwk::BundleInfo & bundleInfo,const std::shared_ptr<ContextImpl> & appContext,bool currentBundle,const std::string & moduleName,std::shared_ptr<Context> inputContext)787 void ContextImpl::InitResourceManager(const AppExecFwk::BundleInfo &bundleInfo,
788 const std::shared_ptr<ContextImpl> &appContext, bool currentBundle, const std::string& moduleName,
789 std::shared_ptr<Context> inputContext)
790 {
791 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
792 TAG_LOGD(AAFwkTag::APPKIT, "begin, bundleName:%{public}s, moduleName:%{public}s",
793 bundleInfo.name.c_str(), moduleName.c_str());
794
795 if (appContext == nullptr) {
796 TAG_LOGE(AAFwkTag::APPKIT, "InitResourceManager appContext is nullptr");
797 return;
798 }
799 if (bundleInfo.applicationInfo.codePath == std::to_string(TYPE_RESERVE) ||
800 bundleInfo.applicationInfo.codePath == std::to_string(TYPE_OTHERS)) {
801 std::shared_ptr<Global::Resource::ResourceManager> resourceManager = InitOthersResourceManagerInner(
802 bundleInfo, currentBundle, moduleName);
803 if (resourceManager == nullptr) {
804 TAG_LOGE(AAFwkTag::APPKIT, "create resourceManager failed");
805 return;
806 }
807 appContext->SetResourceManager(resourceManager);
808 return;
809 }
810
811 std::shared_ptr<Global::Resource::ResourceManager> resourceManager = InitResourceManagerInner(
812 bundleInfo, currentBundle, moduleName, inputContext);
813 if (resourceManager == nullptr) {
814 return;
815 }
816 std::shared_ptr<Global::Resource::ResourceManager> src = nullptr;
817 if (inputContext) {
818 src = inputContext->GetResourceManager();
819 }
820 UpdateResConfig(src, resourceManager);
821 appContext->SetResourceManager(resourceManager);
822 }
823
InitOthersResourceManagerInner(const AppExecFwk::BundleInfo & bundleInfo,bool currentBundle,const std::string & moduleName)824 std::shared_ptr<Global::Resource::ResourceManager> ContextImpl::InitOthersResourceManagerInner(
825 const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName)
826 {
827 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
828 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
829 std::string hapPath;
830 std::vector<std::string> overlayPaths;
831 int32_t appType;
832 if (bundleInfo.applicationInfo.codePath == std::to_string(TYPE_RESERVE)) {
833 appType = TYPE_RESERVE;
834 } else if (bundleInfo.applicationInfo.codePath == std::to_string(TYPE_OTHERS)) {
835 appType = TYPE_OTHERS;
836 } else {
837 appType = 0;
838 }
839 std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager(
840 bundleInfo.name, moduleName, hapPath, overlayPaths, *resConfig, appType));
841 return resourceManager;
842 }
843
InitResourceManagerInner(const AppExecFwk::BundleInfo & bundleInfo,bool currentBundle,const std::string & moduleName,std::shared_ptr<Context> inputContext)844 std::shared_ptr<Global::Resource::ResourceManager> ContextImpl::InitResourceManagerInner(
845 const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName,
846 std::shared_ptr<Context> inputContext)
847 {
848 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
849 std::shared_ptr<Global::Resource::ResourceManager> resourceManager = InitOthersResourceManagerInner(
850 bundleInfo, currentBundle, moduleName);
851 if (resourceManager == nullptr) {
852 TAG_LOGE(AAFwkTag::APPKIT, "Create resourceManager failed");
853 return resourceManager;
854 }
855 if (!moduleName.empty() || !bundleInfo.applicationInfo.multiProjects) {
856 TAG_LOGD(AAFwkTag::APPKIT, "hapModuleInfos count: %{public}zu", bundleInfo.hapModuleInfos.size());
857 std::regex inner_pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR)
858 + GetBundleNameWithContext(inputContext));
859 std::regex outer_pattern(ABS_CODE_PATH);
860 std::regex hsp_pattern(std::string(ABS_CODE_PATH) + FILE_SEPARATOR + bundleInfo.name + PATTERN_VERSION);
861 std::string hsp_sandbox = std::string(LOCAL_CODE_PATH) + FILE_SEPARATOR + bundleInfo.name + FILE_SEPARATOR;
862 {
863 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "for (auto hapModuleInfo : bundleInfo.hapModuleInfos)");
864 for (auto hapModuleInfo : bundleInfo.hapModuleInfos) {
865 TAG_LOGD(AAFwkTag::APPKIT, "hapModuleInfo abilityInfo size: %{public}zu",
866 hapModuleInfo.abilityInfos.size());
867 if (!moduleName.empty() && hapModuleInfo.moduleName != moduleName) {
868 continue;
869 }
870 std::string loadPath =
871 hapModuleInfo.hapPath.empty() ? hapModuleInfo.resourcePath : hapModuleInfo.hapPath;
872 if (loadPath.empty()) {
873 TAG_LOGD(AAFwkTag::APPKIT, "loadPath is empty");
874 continue;
875 }
876 if (currentBundle) {
877 loadPath = std::regex_replace(loadPath, inner_pattern, LOCAL_CODE_PATH);
878 } else if (bundleInfo.applicationInfo.bundleType == AppExecFwk::BundleType::SHARED) {
879 loadPath = std::regex_replace(loadPath, hsp_pattern, hsp_sandbox);
880 } else if (bundleInfo.applicationInfo.bundleType == AppExecFwk::BundleType::APP_SERVICE_FWK) {
881 TAG_LOGD(AAFwkTag::APPKIT, "System hsp path, not need translate");
882 } else {
883 loadPath = std::regex_replace(loadPath, outer_pattern, LOCAL_BUNDLES);
884 }
885
886 TAG_LOGD(AAFwkTag::APPKIT, "loadPath: %{private}s", loadPath.c_str());
887 GetOverlayPath(resourceManager, bundleInfo.name, hapModuleInfo.moduleName, loadPath, currentBundle,
888 inputContext);
889 }
890 }
891 }
892 return resourceManager;
893 }
894
GetOverlayPath(std::shared_ptr<Global::Resource::ResourceManager> & resourceManager,const std::string & bundleName,const std::string & moduleName,std::string & loadPath,bool currentBundle,std::shared_ptr<Context> inputContext)895 void ContextImpl::GetOverlayPath(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager,
896 const std::string &bundleName, const std::string &moduleName, std::string &loadPath, bool currentBundle,
897 std::shared_ptr<Context> inputContext)
898 {
899 // getOverlayPath
900 std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos;
901 auto res = GetOverlayModuleInfos(bundleName, moduleName, overlayModuleInfos);
902 if (res != ERR_OK) {
903 TAG_LOGD(AAFwkTag::APPKIT, "Get overlay paths from bms failed.");
904 }
905 if (overlayModuleInfos.size() == 0) {
906 if (!resourceManager->AddResource(loadPath.c_str())) {
907 TAG_LOGE(AAFwkTag::APPKIT, "AddResource fail, moduleResPath: %{private}s", loadPath.c_str());
908 }
909 } else {
910 std::vector<std::string> overlayPaths;
911 for (auto it : overlayModuleInfos) {
912 if (std::regex_search(it.hapPath, std::regex(GetBundleNameWithContext(inputContext)))) {
913 it.hapPath = std::regex_replace(it.hapPath, std::regex(std::string(ABS_CODE_PATH) +
914 std::string(FILE_SEPARATOR) + GetBundleNameWithContext(inputContext)), LOCAL_CODE_PATH);
915 } else {
916 it.hapPath = std::regex_replace(it.hapPath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
917 }
918 if (it.state == AppExecFwk::OverlayState::OVERLAY_ENABLE) {
919 TAG_LOGD(AAFwkTag::APPKIT, "hapPath: %{private}s", it.hapPath.c_str());
920 overlayPaths.emplace_back(it.hapPath);
921 }
922 }
923 TAG_LOGD(AAFwkTag::APPKIT, "OverlayPaths size:%{public}zu.", overlayPaths.size());
924 if (!resourceManager->AddResource(loadPath, overlayPaths)) {
925 TAG_LOGE(AAFwkTag::APPKIT, "AddResource failed");
926 }
927
928 if (currentBundle) {
929 SubscribeToOverlayEvents(resourceManager, bundleName, moduleName, loadPath, overlayModuleInfos);
930 }
931 }
932 }
933
SubscribeToOverlayEvents(std::shared_ptr<Global::Resource::ResourceManager> & resourceManager,const std::string & name,const std::string & hapModuleName,std::string & loadPath,std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos)934 void ContextImpl::SubscribeToOverlayEvents(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager,
935 const std::string &name, const std::string &hapModuleName, std::string &loadPath,
936 std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos)
937 {
938 if (overlaySubscriber_ != nullptr) {
939 return;
940 }
941 // add listen overlay change
942 overlayModuleInfos_ = overlayModuleInfos;
943 EventFwk::MatchingSkills matchingSkills;
944 matchingSkills.AddEvent(OVERLAY_STATE_CHANGED);
945 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
946 subscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
947 auto callback = [this, resourceManager, bundleName = name, moduleName =
948 hapModuleName, loadPath](const EventFwk::CommonEventData &data) {
949 TAG_LOGI(AAFwkTag::APPKIT, "On overlay changed.");
950 this->OnOverlayChanged(data, resourceManager, bundleName, moduleName, loadPath);
951 };
952 overlaySubscriber_ = std::make_shared<AppExecFwk::OverlayEventSubscriber>(subscribeInfo, callback);
953 bool subResult = EventFwk::CommonEventManager::SubscribeCommonEvent(overlaySubscriber_);
954 TAG_LOGI(AAFwkTag::APPKIT, "Overlay event subscriber register result is %{public}d", subResult);
955 }
956
UnsubscribeToOverlayEvents()957 void ContextImpl::UnsubscribeToOverlayEvents()
958 {
959 if (overlaySubscriber_ != nullptr) {
960 EventFwk::CommonEventManager::UnSubscribeCommonEvent(overlaySubscriber_);
961 overlaySubscriber_ = nullptr;
962 }
963 }
964
UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)965 void ContextImpl::UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
966 {
967 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
968 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
969 if (resConfig == nullptr) {
970 TAG_LOGE(AAFwkTag::APPKIT, "create ResConfig failed");
971 return;
972 }
973
974 if (GetHapModuleInfo() != nullptr && GetApplicationInfo() != nullptr) {
975 std::vector<AppExecFwk::Metadata> metadata = GetHapModuleInfo()->metadata;
976 bool load = std::any_of(metadata.begin(), metadata.end(), [](const auto &metadataItem) {
977 return metadataItem.name == "ContextResourceConfigLoadFromParentTemp" && metadataItem.value == "true";
978 });
979 if (load && GetApplicationInfo()->apiTargetVersion % API_VERSION_MOD >= API11) {
980 std::shared_ptr<Global::Resource::ResourceManager> currentResMgr = GetResourceManager();
981 if (currentResMgr != nullptr) {
982 TAG_LOGD(AAFwkTag::APPKIT, "apiVersion: %{public}d, load parent config",
983 GetApplicationInfo()->apiTargetVersion);
984 currentResMgr->GetResConfig(*resConfig);
985 }
986 }
987 }
988 #ifdef SUPPORT_GRAPHICS
989 UErrorCode status = U_ZERO_ERROR;
990 icu::Locale locale = icu::Locale::forLanguageTag(Global::I18n::LocaleConfig::GetSystemLocale(), status);
991 resConfig->SetLocaleInfo(locale);
992 if (resConfig->GetLocaleInfo() != nullptr) {
993 TAG_LOGD(AAFwkTag::APPKIT,
994 "ContextImpl::InitResourceManager language: %{public}s, script: %{public}s, region: %{public}s,",
995 resConfig->GetLocaleInfo()->getLanguage(), resConfig->GetLocaleInfo()->getScript(),
996 resConfig->GetLocaleInfo()->getCountry());
997 } else {
998 TAG_LOGE(AAFwkTag::APPKIT, "LocaleInfo is null");
999 }
1000 #endif
1001 resConfig->SetDeviceType(GetDeviceType());
1002 if (config_) {
1003 std::string mcc = config_->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_MCC);
1004 std::string mnc = config_->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_MNC);
1005 try {
1006 resConfig->SetMcc(static_cast<uint32_t>(std::stoi(mcc)));
1007 resConfig->SetMnc(static_cast<uint32_t>(std::stoi(mnc)));
1008 } catch (...) {
1009 TAG_LOGD(AAFwkTag::APPKIT, "Set mcc,mnc failed mcc:%{public}s mnc:%{public}s", mcc.c_str(), mnc.c_str());
1010 }
1011 }
1012 resourceManager->UpdateResConfig(*resConfig);
1013 }
1014
UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> src,std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)1015 void ContextImpl::UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> src,
1016 std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
1017 {
1018 if (src == nullptr) {
1019 UpdateResConfig(resourceManager);
1020 return;
1021 }
1022 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1023 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
1024 if (resConfig == nullptr) {
1025 TAG_LOGE(AAFwkTag::APPKIT, "create ResConfig failed");
1026 return;
1027 }
1028 src->GetResConfig(*resConfig);
1029 resourceManager->UpdateResConfig(*resConfig);
1030 }
1031
GetBundleManager()1032 ErrCode ContextImpl::GetBundleManager()
1033 {
1034 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1035 std::lock_guard<std::mutex> lock(bundleManagerMutex_);
1036 if (bundleMgr_ != nullptr && !resetFlag_) {
1037 return ERR_OK;
1038 }
1039
1040 bundleMgr_ = DelayedSingleton<AppExecFwk::BundleMgrHelper>::GetInstance();
1041 if (bundleMgr_ == nullptr) {
1042 TAG_LOGE(AAFwkTag::APPKIT, "The bundleMgr_ is nullptr");
1043 return ERR_NULL_OBJECT;
1044 }
1045
1046 TAG_LOGD(AAFwkTag::APPKIT, "Success");
1047 return ERR_OK;
1048 }
1049
SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> & info)1050 void ContextImpl::SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info)
1051 {
1052 if (info == nullptr) {
1053 TAG_LOGE(AAFwkTag::APPKIT, "info is empty");
1054 return;
1055 }
1056 applicationInfo_ = info;
1057 }
1058
SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)1059 void ContextImpl::SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
1060 {
1061 resourceManager_ = resourceManager;
1062 }
1063
GetResourceManager() const1064 std::shared_ptr<Global::Resource::ResourceManager> ContextImpl::GetResourceManager() const
1065 {
1066 if (resourceManager_) {
1067 return resourceManager_;
1068 }
1069
1070 return parentContext_ != nullptr ? parentContext_->GetResourceManager() : nullptr;
1071 }
1072
GetApplicationInfo() const1073 std::shared_ptr<AppExecFwk::ApplicationInfo> ContextImpl::GetApplicationInfo() const
1074 {
1075 if (parentContext_ != nullptr) {
1076 return parentContext_->GetApplicationInfo();
1077 }
1078
1079 return applicationInfo_;
1080 }
1081
SetParentContext(const std::shared_ptr<Context> & context)1082 void ContextImpl::SetParentContext(const std::shared_ptr<Context> &context)
1083 {
1084 parentContext_ = context;
1085 }
1086
GetBundleCodePath() const1087 std::string ContextImpl::GetBundleCodePath() const
1088 {
1089 if (parentContext_ != nullptr) {
1090 return parentContext_->GetBundleCodePath();
1091 }
1092 return (applicationInfo_ != nullptr) ? applicationInfo_->codePath : "";
1093 }
1094
InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> & abilityInfo)1095 void ContextImpl::InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo)
1096 {
1097 if (hapModuleInfo_ != nullptr || abilityInfo == nullptr) {
1098 return;
1099 }
1100 int errCode = GetBundleManager();
1101 if (errCode != ERR_OK) {
1102 TAG_LOGE(AAFwkTag::APPKIT, "failed, errCode: %{public}d", errCode);
1103 return ;
1104 }
1105
1106 hapModuleInfo_ = std::make_shared<AppExecFwk::HapModuleInfo>();
1107 if (!bundleMgr_->GetHapModuleInfo(*abilityInfo.get(), *hapModuleInfo_)) {
1108 TAG_LOGE(AAFwkTag::APPKIT, "GetHapModuleInfo failed, will retval false value");
1109 }
1110 }
1111
InitHapModuleInfo(const AppExecFwk::HapModuleInfo & hapModuleInfo)1112 void ContextImpl::InitHapModuleInfo(const AppExecFwk::HapModuleInfo &hapModuleInfo)
1113 {
1114 hapModuleInfo_ = std::make_shared<AppExecFwk::HapModuleInfo>(hapModuleInfo);
1115 }
1116
GetHapModuleInfo() const1117 std::shared_ptr<AppExecFwk::HapModuleInfo> ContextImpl::GetHapModuleInfo() const
1118 {
1119 if (hapModuleInfo_ == nullptr) {
1120 TAG_LOGD(AAFwkTag::APPKIT, "hapModuleInfo is empty");
1121 }
1122 return hapModuleInfo_;
1123 }
1124
GetHapModuleInfoWithContext(std::shared_ptr<Context> inputContext) const1125 std::shared_ptr<AppExecFwk::HapModuleInfo> ContextImpl::GetHapModuleInfoWithContext(
1126 std::shared_ptr<Context> inputContext) const
1127 {
1128 if (inputContext) {
1129 return inputContext->GetHapModuleInfo();
1130 }
1131 return GetHapModuleInfo();
1132 }
1133
SetFlags(int64_t flags)1134 void ContextImpl::SetFlags(int64_t flags)
1135 {
1136 flags_ = static_cast<uint64_t>(flags_) | static_cast<uint64_t>(CONTEXT_CREATE_BY_SYSTEM_APP);
1137 }
1138
IsCreateBySystemApp() const1139 bool ContextImpl::IsCreateBySystemApp() const
1140 {
1141 return (static_cast<uint64_t>(flags_) & static_cast<uint64_t>(CONTEXT_CREATE_BY_SYSTEM_APP)) == 1;
1142 }
1143
1144 std::shared_ptr<ApplicationContext> Context::applicationContext_ = nullptr;
1145 std::mutex Context::contextMutex_;
1146
GetApplicationContext()1147 std::shared_ptr<ApplicationContext> Context::GetApplicationContext()
1148 {
1149 std::lock_guard<std::mutex> lock(contextMutex_);
1150 return applicationContext_;
1151 }
1152
SetToken(const sptr<IRemoteObject> & token)1153 void ContextImpl::SetToken(const sptr<IRemoteObject> &token)
1154 {
1155 if (token == nullptr) {
1156 TAG_LOGD(AAFwkTag::APPKIT, "application is nullptr");
1157 return;
1158 }
1159 token_ = token;
1160 if (GetBundleName() == "com.ohos.callui") {
1161 PrintTokenInfo();
1162 }
1163 }
1164
GetToken()1165 sptr<IRemoteObject> ContextImpl::GetToken()
1166 {
1167 return token_;
1168 }
1169
CreateDirIfNotExist(const std::string & dirPath,const mode_t & mode) const1170 void ContextImpl::CreateDirIfNotExist(const std::string& dirPath, const mode_t& mode) const
1171 {
1172 if (!OHOS::FileExists(dirPath)) {
1173 TAG_LOGD(AAFwkTag::APPKIT, "ForceCreateDirectory, dir: %{public}s", dirPath.c_str());
1174 bool createDir = OHOS::ForceCreateDirectory(dirPath);
1175 if (!createDir) {
1176 TAG_LOGE(AAFwkTag::APPKIT, "create dir %{public}s failed, errno is %{public}d", dirPath.c_str(), errno);
1177 return;
1178 }
1179 if (mode != 0) {
1180 chmod(dirPath.c_str(), mode);
1181 }
1182 }
1183 }
1184
SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & config)1185 void ContextImpl::SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config)
1186 {
1187 config_ = config;
1188 }
1189
AppHasDarkRes(bool & darkRes)1190 void ContextImpl::AppHasDarkRes(bool &darkRes)
1191 {
1192 std::shared_ptr<Global::Resource::ResourceManager> currentResMgr = GetResourceManager();
1193 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
1194 if (currentResMgr == nullptr || resConfig == nullptr) {
1195 TAG_LOGE(AAFwkTag::APPKIT, "resMgr or resConfig is null");
1196 return;
1197 }
1198 currentResMgr->GetResConfig(*resConfig);
1199 darkRes = resConfig->GetAppDarkRes();
1200 TAG_LOGD(AAFwkTag::APPKIT, "darkRes %{public}d", darkRes);
1201 }
1202
KillProcessBySelf(const bool clearPageStack)1203 void ContextImpl::KillProcessBySelf(const bool clearPageStack)
1204 {
1205 TAG_LOGD(AAFwkTag::APPKIT, "killProcessBySelf called clearPageStack is %{public}d", clearPageStack);
1206 auto appMgrClient = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance();
1207 appMgrClient->KillApplicationSelf(clearPageStack);
1208 }
1209
GetProcessRunningInformation(AppExecFwk::RunningProcessInfo & info)1210 int32_t ContextImpl::GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info)
1211 {
1212 auto appMgrClient = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance();
1213 auto result = appMgrClient->GetProcessRunningInformation(info);
1214 TAG_LOGD(AAFwkTag::APPKIT, "result is %{public}d", result);
1215 return result;
1216 }
1217
GetAllRunningInstanceKeys(std::vector<std::string> & instanceKeys)1218 int32_t ContextImpl::GetAllRunningInstanceKeys(std::vector<std::string> &instanceKeys)
1219 {
1220 auto appMgrClient = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance();
1221 auto result = appMgrClient->GetAllRunningInstanceKeysBySelf(instanceKeys);
1222 TAG_LOGD(AAFwkTag::APPKIT, "result is %{public}d", result);
1223 return result;
1224 }
1225
RestartApp(const AAFwk::Want & want)1226 int32_t ContextImpl::RestartApp(const AAFwk::Want& want)
1227 {
1228 auto result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->RestartApp(want);
1229 TAG_LOGD(AAFwkTag::APPKIT, "result is %{public}d", result);
1230 return result;
1231 }
1232
GetConfiguration() const1233 std::shared_ptr<AppExecFwk::Configuration> ContextImpl::GetConfiguration() const
1234 {
1235 return config_;
1236 }
1237
GetDeviceType() const1238 Global::Resource::DeviceType ContextImpl::GetDeviceType() const
1239 {
1240 if (deviceType_ != Global::Resource::DeviceType::DEVICE_NOT_SET) {
1241 return deviceType_;
1242 }
1243
1244 auto config = GetConfiguration();
1245 if (config != nullptr) {
1246 auto deviceType = config->GetItem(AAFwk::GlobalConfigurationKey::DEVICE_TYPE);
1247 TAG_LOGD(AAFwkTag::APPKIT, "deviceType is %{public}s", deviceType.c_str());
1248 deviceType_ = AppExecFwk::ConvertDeviceType(deviceType);
1249 }
1250
1251 if (deviceType_ == Global::Resource::DeviceType::DEVICE_NOT_SET) {
1252 deviceType_ = Global::Resource::DeviceType::DEVICE_PHONE;
1253 }
1254 TAG_LOGD(AAFwkTag::APPKIT, "deviceType is %{public}d", deviceType_);
1255 return deviceType_;
1256 }
1257
GetOverlayMgrProxy()1258 ErrCode ContextImpl::GetOverlayMgrProxy()
1259 {
1260 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1261 int errCode = GetBundleManager();
1262 if (errCode != ERR_OK) {
1263 TAG_LOGE(AAFwkTag::APPKIT, "failed, errCode: %{public}d", errCode);
1264 return errCode;
1265 }
1266
1267 std::lock_guard<std::mutex> lock(overlayMgrProxyMutex_);
1268 if (overlayMgrProxy_ != nullptr) {
1269 return ERR_OK;
1270 }
1271
1272 overlayMgrProxy_ = bundleMgr_->GetOverlayManagerProxy();
1273 if (overlayMgrProxy_ == nullptr) {
1274 TAG_LOGE(AAFwkTag::APPKIT, "GetOverlayManagerProxy failed");
1275 return ERR_NULL_OBJECT;
1276 }
1277
1278 TAG_LOGD(AAFwkTag::APPKIT, "Success.");
1279 return ERR_OK;
1280 }
1281
GetOverlayModuleInfos(const std::string & bundleName,const std::string & moduleName,std::vector<AppExecFwk::OverlayModuleInfo> & overlayModuleInfos)1282 int ContextImpl::GetOverlayModuleInfos(const std::string &bundleName, const std::string &moduleName,
1283 std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos)
1284 {
1285 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1286 int errCode = GetOverlayMgrProxy();
1287 if (errCode != ERR_OK) {
1288 TAG_LOGE(AAFwkTag::APPKIT, "failed, errCode: %{public}d", errCode);
1289 return errCode;
1290 }
1291 {
1292 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "overlayMgrProxy_->GetTargetOverlayModuleInfo");
1293 auto ret = overlayMgrProxy_->GetTargetOverlayModuleInfo(moduleName, overlayModuleInfos);
1294 if (ret != ERR_OK) {
1295 TAG_LOGD(AAFwkTag::APPKIT, "GetOverlayModuleInfo form bms failed");
1296 return ret;
1297 }
1298 }
1299 std::sort(overlayModuleInfos.begin(), overlayModuleInfos.end(),
1300 [](const AppExecFwk::OverlayModuleInfo& lhs, const AppExecFwk::OverlayModuleInfo& rhs) -> bool {
1301 return lhs.priority > rhs.priority;
1302 });
1303 TAG_LOGD(AAFwkTag::APPKIT, "the size of overlay is: %{public}zu", overlayModuleInfos.size());
1304 return ERR_OK;
1305 }
1306
GetAddOverlayPaths(const std::vector<AppExecFwk::OverlayModuleInfo> & overlayModuleInfos)1307 std::vector<std::string> ContextImpl::GetAddOverlayPaths(
1308 const std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos)
1309 {
1310 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1311 std::vector<std::string> addPaths;
1312 for (auto it : overlayModuleInfos) {
1313 auto iter = std::find_if(
1314 overlayModuleInfos_.begin(), overlayModuleInfos_.end(), [it](AppExecFwk::OverlayModuleInfo item) {
1315 return it.moduleName == item.moduleName;
1316 });
1317 if ((iter != overlayModuleInfos_.end()) && (it.state == AppExecFwk::OverlayState::OVERLAY_ENABLE)) {
1318 iter->state = it.state;
1319 ChangeToLocalPath(iter->bundleName, iter->hapPath, iter->hapPath);
1320 TAG_LOGD(AAFwkTag::APPKIT, "add path:%{private}s", iter->hapPath.c_str());
1321 addPaths.emplace_back(iter->hapPath);
1322 }
1323 }
1324
1325 return addPaths;
1326 }
1327
GetRemoveOverlayPaths(const std::vector<AppExecFwk::OverlayModuleInfo> & overlayModuleInfos)1328 std::vector<std::string> ContextImpl::GetRemoveOverlayPaths(
1329 const std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos)
1330 {
1331 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1332 std::vector<std::string> removePaths;
1333 for (auto it : overlayModuleInfos) {
1334 auto iter = std::find_if(
1335 overlayModuleInfos_.begin(), overlayModuleInfos_.end(), [it](AppExecFwk::OverlayModuleInfo item) {
1336 return it.moduleName == item.moduleName;
1337 });
1338 if ((iter != overlayModuleInfos_.end()) && (it.state != AppExecFwk::OverlayState::OVERLAY_ENABLE)) {
1339 iter->state = it.state;
1340 ChangeToLocalPath(iter->bundleName, iter->hapPath, iter->hapPath);
1341 TAG_LOGD(AAFwkTag::APPKIT, "remove path:%{private}s", iter->hapPath.c_str());
1342 removePaths.emplace_back(iter->hapPath);
1343 }
1344 }
1345
1346 return removePaths;
1347 }
1348
OnOverlayChanged(const EventFwk::CommonEventData & data,const std::shared_ptr<Global::Resource::ResourceManager> & resourceManager,const std::string & bundleName,const std::string & moduleName,const std::string & loadPath)1349 void ContextImpl::OnOverlayChanged(const EventFwk::CommonEventData &data,
1350 const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, const std::string &bundleName,
1351 const std::string &moduleName, const std::string &loadPath)
1352 {
1353 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1354 TAG_LOGD(AAFwkTag::APPKIT, "begin");
1355 auto want = data.GetWant();
1356 std::string action = want.GetAction();
1357 if (action != OVERLAY_STATE_CHANGED) {
1358 TAG_LOGD(AAFwkTag::APPKIT, "Not this subscribe, action: %{public}s", action.c_str());
1359 return;
1360 }
1361 if (GetBundleName() != bundleName) {
1362 TAG_LOGD(AAFwkTag::APPKIT, "Not this app, bundleName: %{public}s", bundleName.c_str());
1363 return;
1364 }
1365 bool isEnable = data.GetWant().GetBoolParam(AppExecFwk::Constants::OVERLAY_STATE, false);
1366 // 1.get overlay hapPath
1367 if (resourceManager == nullptr) {
1368 TAG_LOGE(AAFwkTag::APPKIT, "resourceManager is nullptr");
1369 return;
1370 }
1371 if (overlayModuleInfos_.size() == 0) {
1372 TAG_LOGE(AAFwkTag::APPKIT, "overlayModuleInfos is empty");
1373 return;
1374 }
1375 std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos;
1376 auto res = GetOverlayModuleInfos(bundleName, moduleName, overlayModuleInfos);
1377 if (res != ERR_OK) {
1378 return;
1379 }
1380
1381 // 2.add/remove overlay hapPath
1382 if (loadPath.empty() || overlayModuleInfos.size() == 0) {
1383 TAG_LOGW(AAFwkTag::APPKIT, "There is not any hapPath in overlayModuleInfo");
1384 } else {
1385 if (isEnable) {
1386 std::vector<std::string> overlayPaths = GetAddOverlayPaths(overlayModuleInfos);
1387 if (!resourceManager->AddResource(loadPath, overlayPaths)) {
1388 TAG_LOGE(AAFwkTag::APPKIT, "AddResource error");
1389 }
1390 } else {
1391 std::vector<std::string> overlayPaths = GetRemoveOverlayPaths(overlayModuleInfos);
1392 if (!resourceManager->RemoveResource(loadPath, overlayPaths)) {
1393 TAG_LOGE(AAFwkTag::APPKIT, "RemoveResource error");
1394 }
1395 }
1396 }
1397 }
1398
ChangeToLocalPath(const std::string & bundleName,const std::string & sourceDir,std::string & localPath)1399 void ContextImpl::ChangeToLocalPath(const std::string& bundleName, const std::string& sourceDir, std::string &localPath)
1400 {
1401 std::regex pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + bundleName);
1402 if (sourceDir.empty()) {
1403 return;
1404 }
1405 bool isExist = false;
1406 try {
1407 isExist = std::regex_search(localPath, std::regex(bundleName));
1408 } catch (...) {
1409 TAG_LOGE(AAFwkTag::APPKIT, "ChangeToLocalPath error localPath:%{public}s bundleName:%{public}s",
1410 localPath.c_str(), bundleName.c_str());
1411 }
1412 if (isExist) {
1413 localPath = std::regex_replace(localPath, pattern, std::string(LOCAL_CODE_PATH));
1414 } else {
1415 localPath = std::regex_replace(localPath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
1416 }
1417 }
1418
ClearUpApplicationData()1419 void ContextImpl::ClearUpApplicationData()
1420 {
1421 int errCode = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->ClearUpApplicationDataBySelf();
1422 if (errCode != ERR_OK) {
1423 TAG_LOGE(AAFwkTag::APPKIT, "Delete bundle side user data by self is fail.");
1424 return;
1425 }
1426 }
1427
SetSupportedProcessCacheSelf(bool isSupport)1428 int32_t ContextImpl::SetSupportedProcessCacheSelf(bool isSupport)
1429 {
1430 TAG_LOGD(AAFwkTag::APPKIT, "Called");
1431 auto appMgrClient = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance();
1432 if (appMgrClient == nullptr) {
1433 TAG_LOGE(AAFwkTag::APPKIT, "appMgrClient is nullptr");
1434 return ERR_INVALID_VALUE;
1435 }
1436 return appMgrClient->SetSupportedProcessCacheSelf(isSupport);
1437 }
1438
PrintTokenInfo() const1439 void ContextImpl::PrintTokenInfo() const
1440 {
1441 if (token_ == nullptr) {
1442 TAG_LOGI(AAFwkTag::EXT, "null token");
1443 return;
1444 }
1445 if (!token_->IsProxyObject()) {
1446 TAG_LOGI(AAFwkTag::EXT, "token not proxy");
1447 return;
1448 }
1449 IPCObjectProxy *tokenProxyObject = reinterpret_cast<IPCObjectProxy *>(token_.GetRefPtr());
1450 if (tokenProxyObject != nullptr) {
1451 std::string remoteDescriptor = Str16ToStr8(tokenProxyObject->GetInterfaceDescriptor());
1452 TAG_LOGI(AAFwkTag::EXT, "handle: %{public}d, descriptor: %{public}s",
1453 tokenProxyObject->GetHandle(), remoteDescriptor.c_str());
1454 }
1455 }
1456
ShallowCopySelf(std::shared_ptr<ContextImpl> & contextImpl)1457 void ContextImpl::ShallowCopySelf(std::shared_ptr<ContextImpl> &contextImpl)
1458 {
1459 contextImpl->token_ = token_;
1460 contextImpl->applicationInfo_ = applicationInfo_;
1461 contextImpl->parentContext_ = parentContext_;
1462 contextImpl->resourceManager_ = resourceManager_;
1463 contextImpl->hapModuleInfo_ = hapModuleInfo_;
1464 contextImpl->config_ = config_;
1465 contextImpl->currArea_ = currArea_;
1466 contextImpl->overlayModuleInfos_ = overlayModuleInfos_;
1467 {
1468 std::lock_guard<std::mutex> lock(checkedDirSetLock_);
1469 contextImpl->checkedDirSet_ = checkedDirSet_;
1470 }
1471 {
1472 std::lock_guard<std::mutex> lock(bundleManagerMutex_);
1473 contextImpl->bundleMgr_ = bundleMgr_;
1474 }
1475 {
1476 std::lock_guard<std::mutex> lock(overlayMgrProxyMutex_);
1477 contextImpl->overlayMgrProxy_ = overlayMgrProxy_;
1478 }
1479 contextImpl->resetFlag_ = resetFlag_;
1480 contextImpl->processName_ = processName_;
1481 }
1482
UpdateDisplayConfiguration(std::shared_ptr<ContextImpl> & contextImpl,uint64_t displayId,float density,std::string direction)1483 bool ContextImpl::UpdateDisplayConfiguration(std::shared_ptr<ContextImpl> &contextImpl, uint64_t displayId,
1484 float density, std::string direction)
1485 {
1486 if (contextImpl == nullptr) {
1487 TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
1488 return false;
1489 }
1490
1491 std::shared_ptr<AppExecFwk::Configuration> newConfiguration = nullptr;
1492 if (config_ == nullptr) {
1493 newConfiguration = std::make_shared<AppExecFwk::Configuration>();
1494 } else {
1495 newConfiguration = std::make_shared<AppExecFwk::Configuration>(*config_);
1496 }
1497 if (newConfiguration == nullptr) {
1498 TAG_LOGE(AAFwkTag::APPKIT, "failed to create configuration");
1499 return false;
1500 }
1501 newConfiguration->AddItem(AppExecFwk::ConfigurationInner::APPLICATION_DISPLAYID, std::to_string(displayId));
1502 newConfiguration->AddItem(displayId, AppExecFwk::ConfigurationInner::APPLICATION_DENSITYDPI,
1503 AppExecFwk::GetDensityStr(density));
1504 newConfiguration->AddItem(displayId, AppExecFwk::ConfigurationInner::APPLICATION_DIRECTION, direction);
1505 contextImpl->config_ = newConfiguration;
1506
1507 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
1508 if (resConfig == nullptr) {
1509 TAG_LOGE(AAFwkTag::APPKIT, "null resConfig");
1510 return false;
1511 }
1512 auto resourceManager = GetResourceManager();
1513 if (resourceManager == nullptr) {
1514 TAG_LOGE(AAFwkTag::APPKIT, "null resource manager");
1515 return false;
1516 }
1517 resourceManager->GetResConfig(*resConfig);
1518 resConfig->SetScreenDensity(density);
1519 resConfig->SetDirection(AppExecFwk::ConvertDirection(direction));
1520 if (contextImpl->resourceManager_ == nullptr) {
1521 TAG_LOGE(AAFwkTag::APPKIT, "null resource manager");
1522 return false;
1523 }
1524 contextImpl->resourceManager_->UpdateResConfig(*resConfig);
1525 return true;
1526 }
1527
1528 #ifdef SUPPORT_GRAPHICS
CreateDisplayContext(uint64_t displayId)1529 std::shared_ptr<Context> ContextImpl::CreateDisplayContext(uint64_t displayId)
1530 {
1531 float density;
1532 std::string direction;
1533 if (!GetDisplayConfig(displayId, density, direction)) {
1534 TAG_LOGE(AAFwkTag::APPKIT, "failed to get display config");
1535 return nullptr;
1536 }
1537 std::shared_ptr<ContextImpl> contextImpl = std::make_shared<ContextImpl>();
1538 if (contextImpl == nullptr) {
1539 TAG_LOGE(AAFwkTag::APPKIT, "failed to create contextImpl");
1540 return nullptr;
1541 }
1542 ShallowCopySelf(contextImpl);
1543
1544 auto appContext = ApplicationContext::GetInstance();
1545 if (appContext == nullptr) {
1546 TAG_LOGE(AAFwkTag::APPKIT, "failed to get application context");
1547 return nullptr;
1548 }
1549 std::string currentBundleName = appContext->GetBundleName();
1550 AppExecFwk::BundleInfo bundleInfo;
1551 bool currentBundle = true;
1552 auto result = GetBundleInfo(currentBundleName, bundleInfo, currentBundle);
1553 if (result != ERR_OK) {
1554 TAG_LOGE(AAFwkTag::APPKIT, "failed to get bundle info");
1555 return nullptr;
1556 }
1557 InitResourceManager(bundleInfo, contextImpl, true);
1558 if (contextImpl->resourceManager_ == nullptr) {
1559 TAG_LOGE(AAFwkTag::APPKIT, "failed to init resource manager");
1560 return nullptr;
1561 }
1562 if (!UpdateDisplayConfiguration(contextImpl, displayId, density, direction)) {
1563 TAG_LOGE(AAFwkTag::APPKIT, "failed to Update display configuration");
1564 return nullptr;
1565 }
1566 return contextImpl;
1567 }
1568
RegisterGetDisplayConfig(GetDisplayConfigCallback getDisplayConfigCallback)1569 void ContextImpl::RegisterGetDisplayConfig(GetDisplayConfigCallback getDisplayConfigCallback)
1570 {
1571 std::lock_guard<std::mutex> lock(getDisplayConfigCallbackMutex_);
1572 getDisplayConfigCallback_ = getDisplayConfigCallback;
1573 }
1574
GetDisplayConfig(uint64_t displayId,float & density,std::string & directionStr)1575 bool ContextImpl::GetDisplayConfig(uint64_t displayId, float &density, std::string &directionStr)
1576 {
1577 std::lock_guard<std::mutex> lock(getDisplayConfigCallbackMutex_);
1578 if (getDisplayConfigCallback_ == nullptr) {
1579 TAG_LOGE(AAFwkTag::APPKIT, "null ability GetDisplayConfig callback");
1580 return false;
1581 }
1582 return getDisplayConfigCallback_(displayId, density, directionStr);
1583 }
1584 #endif
1585 } // namespace AbilityRuntime
1586 } // namespace OHOS
1587