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