• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #include "context_container.h"
16 
17 #include <regex>
18 
19 #include "constants.h"
20 #include "ability_manager_client.h"
21 #include "ability_manager_errors.h"
22 #include "app_context.h"
23 #include "bundle_constants.h"
24 #include "hilog_wrapper.h"
25 #include "parameters.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 // for api7 demo special
30 constexpr int CURRENT_ACCOUNT_ID = 100;
31 const int32_t TYPE_RESERVE = 1;
32 const int32_t TYPE_OTHERS = 2;
33 
AttachBaseContext(const std::shared_ptr<ContextDeal> & base)34 void ContextContainer::AttachBaseContext(const std::shared_ptr<ContextDeal> &base)
35 {
36     if (base == nullptr) {
37         HILOG_ERROR("ContextDeal::AttachBaseContext failed, base is nullptr");
38         return;
39     }
40     baseContext_ = base;
41 }
42 
DetachBaseContext()43 void ContextContainer::DetachBaseContext()
44 {
45     if (baseContext_ != nullptr) {
46         baseContext_.reset();
47     }
48     baseContext_ = nullptr;
49 }
50 
GetProcessInfo() const51 std::shared_ptr<ProcessInfo> ContextContainer::GetProcessInfo() const
52 {
53     return processInfo_;
54 }
55 
SetProcessInfo(const std::shared_ptr<ProcessInfo> & info)56 void ContextContainer::SetProcessInfo(const std::shared_ptr<ProcessInfo> &info)
57 {
58     if (info == nullptr) {
59         HILOG_ERROR("SetProcessInfo failed, info is empty");
60         return;
61     }
62     processInfo_ = info;
63 }
64 
GetApplicationInfo() const65 std::shared_ptr<ApplicationInfo> ContextContainer::GetApplicationInfo() const
66 {
67     if (baseContext_ != nullptr) {
68         return baseContext_->GetApplicationInfo();
69     } else {
70         HILOG_ERROR("ContextContainer::GetApplicationInfo baseContext_ is nullptr");
71         return nullptr;
72     }
73 }
74 
GetApplicationContext() const75 std::shared_ptr<Context> ContextContainer::GetApplicationContext() const
76 {
77     if (baseContext_ != nullptr) {
78         return baseContext_->GetApplicationContext();
79     } else {
80         HILOG_ERROR("ContextContainer::GetApplicationContext baseContext_ is nullptr");
81         return nullptr;
82     }
83 }
84 
GetBundleCodePath()85 std::string ContextContainer::GetBundleCodePath()
86 {
87     if (baseContext_ != nullptr) {
88         return baseContext_->GetBundleCodePath();
89     } else {
90         HILOG_ERROR("ContextContainer::GetBundleCodePath baseContext_ is nullptr");
91         return "";
92     }
93 }
94 
GetAbilityInfo()95 const std::shared_ptr<AbilityInfo> ContextContainer::GetAbilityInfo()
96 {
97     if (baseContext_ != nullptr) {
98         return baseContext_->GetAbilityInfo();
99     } else {
100         HILOG_ERROR("ContextContainer::GetAbilityInfo baseContext_ is nullptr");
101         return nullptr;
102     }
103 }
104 
GetContext()105 std::shared_ptr<Context> ContextContainer::GetContext()
106 {
107     if (baseContext_ != nullptr) {
108         return baseContext_->GetContext();
109     } else {
110         HILOG_ERROR("ContextContainer::GetContext baseContext_ is nullptr");
111         return nullptr;
112     }
113 }
114 
GetBundleManager() const115 sptr<IBundleMgr> ContextContainer::GetBundleManager() const
116 {
117     if (baseContext_ != nullptr) {
118         return baseContext_->GetBundleManager();
119     } else {
120         HILOG_ERROR("ContextContainer::GetBundleManager baseContext_ is nullptr");
121         return nullptr;
122     }
123 }
124 
GetResourceManager() const125 std::shared_ptr<Global::Resource::ResourceManager> ContextContainer::GetResourceManager() const
126 {
127     if (baseContext_ != nullptr) {
128         return baseContext_->GetResourceManager();
129     } else {
130         HILOG_ERROR("ContextContainer::GetResourceManager baseContext_ is nullptr");
131         return nullptr;
132     }
133 }
134 
GetDatabaseDir()135 std::string ContextContainer::GetDatabaseDir()
136 {
137     if (baseContext_ != nullptr) {
138         return baseContext_->GetDatabaseDir();
139     } else {
140         HILOG_ERROR("ContextContainer::GetDatabaseDir baseContext_ is nullptr");
141         return "";
142     }
143 }
144 
GetDataDir()145 std::string ContextContainer::GetDataDir()
146 {
147     if (baseContext_ != nullptr) {
148         return baseContext_->GetDataDir();
149     } else {
150         HILOG_ERROR("ContextContainer::GetDataDir baseContext_ is nullptr");
151         return "";
152     }
153 }
154 
GetDir(const std::string & name,int mode)155 std::string ContextContainer::GetDir(const std::string &name, int mode)
156 {
157     if (baseContext_ != nullptr) {
158         return baseContext_->GetDir(name, mode);
159     } else {
160         HILOG_ERROR("ContextContainer::GetDir baseContext_ is nullptr");
161         return "";
162     }
163 }
164 
GetFilesDir()165 std::string ContextContainer::GetFilesDir()
166 {
167     if (baseContext_ != nullptr) {
168         return baseContext_->GetFilesDir();
169     } else {
170         HILOG_ERROR("ContextContainer::GetFilesDir baseContext_ is nullptr");
171         return "";
172     }
173 }
174 
GetBundleName() const175 std::string ContextContainer::GetBundleName() const
176 {
177     if (baseContext_ != nullptr) {
178         return baseContext_->GetBundleName();
179     } else {
180         HILOG_ERROR("ContextContainer::GetBundleName baseContext_ is nullptr");
181         return "";
182     }
183 }
184 
GetBundleResourcePath()185 std::string ContextContainer::GetBundleResourcePath()
186 {
187     if (baseContext_ != nullptr) {
188         return baseContext_->GetBundleResourcePath();
189     } else {
190         HILOG_ERROR("ContextContainer::GetBundleResourcePath baseContext_ is nullptr");
191         return "";
192     }
193 }
194 
GetAbilityManager()195 sptr<AAFwk::IAbilityManager> ContextContainer::GetAbilityManager()
196 {
197     if (baseContext_ != nullptr) {
198         return baseContext_->GetAbilityManager();
199     } else {
200         HILOG_ERROR("ContextContainer::GetAbilityManager baseContext_ is nullptr");
201         return nullptr;
202     }
203 }
204 
GetAppType()205 std::string ContextContainer::GetAppType()
206 {
207     if (baseContext_ != nullptr) {
208         return baseContext_->GetAppType();
209     } else {
210         HILOG_ERROR("ContextContainer::GetAppType baseContext_ is nullptr");
211         return "";
212     }
213 }
214 
SetPattern(int patternId)215 void ContextContainer::SetPattern(int patternId)
216 {
217     if (baseContext_ != nullptr) {
218         baseContext_->SetPattern(patternId);
219     } else {
220         HILOG_ERROR("ContextContainer::SetPattern baseContext_ is nullptr");
221     }
222 }
223 
GetHapModuleInfo()224 std::shared_ptr<HapModuleInfo> ContextContainer::GetHapModuleInfo()
225 {
226     if (baseContext_ != nullptr) {
227         return baseContext_->GetHapModuleInfo();
228     } else {
229         HILOG_ERROR("ContextContainer::GetHapModuleInfo baseContext_ is nullptr");
230         return nullptr;
231     }
232 }
233 
GetProcessName()234 std::string ContextContainer::GetProcessName()
235 {
236     return (processInfo_ != nullptr) ? processInfo_->GetProcessName() : "";
237 }
238 
CreateBundleContext(std::string bundleName,int flag,int accountId)239 std::shared_ptr<Context> ContextContainer::CreateBundleContext(std::string bundleName, int flag, int accountId)
240 {
241     if (bundleName.empty()) {
242         HILOG_ERROR("ContextContainer::CreateBundleContext bundleName is empty");
243         return nullptr;
244     }
245 
246     if (strcmp(bundleName.c_str(), GetBundleName().c_str()) == 0) {
247         return GetApplicationContext();
248     }
249 
250     sptr<IBundleMgr> bundleMgr = GetBundleManager();
251     if (bundleMgr == nullptr) {
252         HILOG_ERROR("ContextContainer::CreateBundleContext GetBundleManager is nullptr");
253         return nullptr;
254     }
255 
256     BundleInfo bundleInfo;
257     HILOG_INFO("CreateBundleContext length: %{public}zu, bundleName: %{public}s, accountId is %{public}d",
258         bundleName.length(),
259         bundleName.c_str(),
260         accountId);
261     int realAccountId = CURRENT_ACCOUNT_ID;
262     if (accountId != DEFAULT_ACCOUNT_ID) {
263         realAccountId = accountId;
264     }
265     bundleMgr->GetBundleInfo(bundleName, BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, realAccountId);
266 
267     if (bundleInfo.name.empty() || bundleInfo.applicationInfo.name.empty()) {
268         HILOG_ERROR("ContextContainer::CreateBundleContext GetBundleInfo is error");
269         return nullptr;
270     }
271 
272     std::shared_ptr<AppContext> appContext = std::make_shared<AppContext>();
273     std::shared_ptr<ContextDeal> deal = std::make_shared<ContextDeal>(true);
274 
275     // init resourceManager.
276     InitResourceManager(bundleInfo, deal);
277 
278     deal->SetApplicationInfo(std::make_shared<ApplicationInfo>(bundleInfo.applicationInfo));
279     appContext->AttachBaseContext(deal);
280     return appContext;
281 }
282 
InitResourceManager(BundleInfo & bundleInfo,std::shared_ptr<ContextDeal> & deal)283 void ContextContainer::InitResourceManager(BundleInfo &bundleInfo, std::shared_ptr<ContextDeal> &deal)
284 {
285     HILOG_DEBUG("InitResourceManager begin, bundleName:%{public}s, codePath:%{public}s",
286         bundleInfo.name.c_str(), bundleInfo.applicationInfo.codePath.c_str());
287     if (deal == nullptr) {
288         HILOG_ERROR("InitResourceManager deal is nullptr");
289         return;
290     }
291     if (bundleInfo.applicationInfo.codePath == std::to_string(TYPE_RESERVE) ||
292         bundleInfo.applicationInfo.codePath == std::to_string(TYPE_OTHERS)) {
293         std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
294         std::string moduleName;
295         std::string hapPath;
296         std::vector<std::string> overlayPaths;
297         int32_t appType;
298         if (bundleInfo.applicationInfo.codePath == std::to_string(TYPE_RESERVE)) {
299             appType = TYPE_RESERVE;
300         } else if (bundleInfo.applicationInfo.codePath == std::to_string(TYPE_OTHERS)) {
301             appType = TYPE_OTHERS;
302         } else {
303             appType = 0;
304         }
305         std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager(
306             bundleInfo.name, moduleName, hapPath, overlayPaths, *resConfig, appType));
307         if (resourceManager == nullptr) {
308             HILOG_ERROR("ContextImpl::InitResourceManager create resourceManager failed");
309             return;
310         }
311         deal->initResourceManager(resourceManager);
312         return;
313     }
314 
315     std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
316     if (resourceManager == nullptr) {
317         HILOG_ERROR("ContextContainer::InitResourceManager create resourceManager failed");
318         return;
319     }
320 
321     HILOG_DEBUG(
322         "ContextContainer::InitResourceManager hapModuleInfos count: %{public}zu", bundleInfo.hapModuleInfos.size());
323     std::regex pattern(AbilityBase::Constants::ABS_CODE_PATH);
324     for (auto hapModuleInfo : bundleInfo.hapModuleInfos) {
325         std::string loadPath;
326         if (!hapModuleInfo.hapPath.empty()) {
327             loadPath = hapModuleInfo.hapPath;
328         } else {
329             loadPath = hapModuleInfo.resourcePath;
330         }
331         if (loadPath.empty()) {
332             continue;
333         }
334         loadPath = std::regex_replace(loadPath, pattern, AbilityBase::Constants::LOCAL_BUNDLES);
335         HILOG_DEBUG("ContextContainer::InitResourceManager loadPath: %{public}s", loadPath.c_str());
336         if (!resourceManager->AddResource(loadPath.c_str())) {
337             HILOG_ERROR("ContextContainer::InitResourceManager AddResource failed");
338         }
339     }
340 
341     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
342     resConfig->SetLocaleInfo("zh", "Hans", "CN");
343 #ifdef SUPPORT_GRAPHICS
344     if (resConfig->GetLocaleInfo() != nullptr) {
345         HILOG_INFO(
346             "ContextContainer::InitResourceManager language: %{public}s, script: %{public}s, region: %{public}s,",
347             resConfig->GetLocaleInfo()->getLanguage(),
348             resConfig->GetLocaleInfo()->getScript(),
349             resConfig->GetLocaleInfo()->getCountry());
350     } else {
351         HILOG_INFO("ContextContainer::InitResourceManager language: GetLocaleInfo is null.");
352     }
353 #endif
354     resourceManager->UpdateResConfig(*resConfig);
355     deal->initResourceManager(resourceManager);
356 }
357 
GetCaller()358 Uri ContextContainer::GetCaller()
359 {
360     Uri uri(uriString_);
361     return uri;
362 }
363 
SetUriString(const std::string & uri)364 void ContextContainer::SetUriString(const std::string &uri)
365 {
366     uriString_ = uri;
367 }
368 
GetString(int resId)369 std::string ContextContainer::GetString(int resId)
370 {
371     if (baseContext_ != nullptr) {
372         std::string ret = baseContext_->GetString(resId);
373         return ret;
374     } else {
375         HILOG_ERROR("ContextContainer::GetString baseContext_ is nullptr");
376         return "";
377     }
378 }
379 
GetStringArray(int resId)380 std::vector<std::string> ContextContainer::GetStringArray(int resId)
381 {
382     if (baseContext_ != nullptr) {
383         return baseContext_->GetStringArray(resId);
384     } else {
385         HILOG_ERROR("ContextContainer::GetStringArray baseContext_ is nullptr");
386         return std::vector<std::string>();
387     }
388 }
389 
GetIntArray(int resId)390 std::vector<int> ContextContainer::GetIntArray(int resId)
391 {
392     if (baseContext_ != nullptr) {
393         return baseContext_->GetIntArray(resId);
394     } else {
395         HILOG_ERROR("ContextContainer::GetIntArray baseContext_ is nullptr");
396         return std::vector<int>();
397     }
398 }
399 
GetTheme()400 std::map<std::string, std::string> ContextContainer::GetTheme()
401 {
402     if (baseContext_ != nullptr) {
403         return baseContext_->GetTheme();
404     } else {
405         HILOG_ERROR("ContextContainer::GetTheme baseContext_ is nullptr");
406         return std::map<std::string, std::string>();
407     }
408 }
409 
SetTheme(int themeId)410 void ContextContainer::SetTheme(int themeId)
411 {
412     if (baseContext_ != nullptr) {
413         baseContext_->SetTheme(themeId);
414     } else {
415         HILOG_ERROR("ContextContainer::SetTheme baseContext_ is nullptr");
416     }
417 }
418 
GetPattern()419 std::map<std::string, std::string> ContextContainer::GetPattern()
420 {
421     if (baseContext_ != nullptr) {
422         return baseContext_->GetPattern();
423     } else {
424         HILOG_ERROR("ContextContainer::GetPattern baseContext_ is nullptr");
425         return std::map<std::string, std::string>();
426     }
427 }
428 
GetColor(int resId)429 int ContextContainer::GetColor(int resId)
430 {
431     if (baseContext_ != nullptr) {
432         return baseContext_->GetColor(resId);
433     } else {
434         HILOG_ERROR("ContextContainer::GetColor baseContext_ is nullptr");
435         return INVALID_RESOURCE_VALUE;
436     }
437 }
438 
GetThemeId()439 int ContextContainer::GetThemeId()
440 {
441     if (baseContext_ != nullptr) {
442         return baseContext_->GetThemeId();
443     } else {
444         HILOG_ERROR("ContextContainer::GetThemeId baseContext_ is nullptr");
445         return -1;
446     }
447 }
448 
GetDisplayOrientation()449 int ContextContainer::GetDisplayOrientation()
450 {
451     if (baseContext_ != nullptr) {
452         return baseContext_->GetDisplayOrientation();
453     } else {
454         HILOG_ERROR("ContextContainer::GetDisplayOrientation baseContext_ is nullptr");
455         return static_cast<int>(DisplayOrientation::UNSPECIFIED);
456     }
457 }
458 
GetPreferencesDir()459 std::string ContextContainer::GetPreferencesDir()
460 {
461     if (baseContext_ != nullptr) {
462         return baseContext_->GetPreferencesDir();
463     } else {
464         HILOG_ERROR("ContextContainer::GetPreferencesDir baseContext_ is nullptr");
465         return "";
466     }
467 }
468 
SetColorMode(int mode)469 void ContextContainer::SetColorMode(int mode)
470 {
471     if (baseContext_ == nullptr) {
472         HILOG_ERROR("ContextContainer::SetColorMode baseContext_ is nullptr");
473         return;
474     }
475 
476     baseContext_->SetColorMode(mode);
477 }
478 
GetColorMode()479 int ContextContainer::GetColorMode()
480 {
481     if (baseContext_ == nullptr) {
482         HILOG_ERROR("ContextContainer::GetColorMode baseContext_ is nullptr");
483         return -1;
484     }
485 
486     return baseContext_->GetColorMode();
487 }
488 
GetMissionId()489 int ContextContainer::GetMissionId()
490 {
491     return lifeCycleStateInfo_.missionId;
492 }
493 
SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo & info)494 void ContextContainer::SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo &info)
495 {
496     lifeCycleStateInfo_ = info;
497 }
498 
GetLifeCycleStateInfo() const499 AAFwk::LifeCycleStateInfo ContextContainer::GetLifeCycleStateInfo() const
500 {
501     return lifeCycleStateInfo_;
502 }
503 }  // namespace AppExecFwk
504 }  // namespace OHOS
505