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