1 /*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bundle_container.h"
17
18 #include <nlohmann/json.hpp>
19
20 #include "hilog_tag_wrapper.h"
21 #include "json_serializer.h"
22 #include "module_profile.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
GetInstance()26 BundleContainer& BundleContainer::GetInstance()
27 {
28 static BundleContainer instance;
29 return instance;
30 }
31
LoadBundleInfos(const std::vector<uint8_t> & buffer)32 void BundleContainer::LoadBundleInfos(const std::vector<uint8_t> &buffer)
33 {
34 bundleInfo_ = std::make_shared<InnerBundleInfo>();
35 if (!bundleInfo_) {
36 TAG_LOGD(AAFwkTag::ABILITY_SIM, "null bundleInfo_");
37 return;
38 }
39
40 bundleInfo_->SetIsNewVersion(true);
41 ModuleProfile moduleProfile;
42 moduleProfile.TransformTo(buffer, *bundleInfo_);
43 }
44
GetApplicationInfo() const45 std::shared_ptr<ApplicationInfo> BundleContainer::GetApplicationInfo() const
46 {
47 if (bundleInfo_ != nullptr) {
48 auto appInfo = std::make_shared<ApplicationInfo>();
49 bundleInfo_->GetApplicationInfo(0, Constants::UNSPECIFIED_USERID, *appInfo);
50 return appInfo;
51 }
52 return nullptr;
53 }
54
GetHapModuleInfo(const std::string & modulePackage) const55 std::shared_ptr<HapModuleInfo> BundleContainer::GetHapModuleInfo(const std::string &modulePackage) const
56 {
57 if (bundleInfo_ != nullptr) {
58 auto uid = Constants::UNSPECIFIED_USERID;
59 TAG_LOGI(AAFwkTag::ABILITY_SIM,
60 "modulePackage:%{public}s", modulePackage.c_str());
61 std::optional<HapModuleInfo> hapMouduleInfo = bundleInfo_->FindHapModuleInfo(modulePackage, uid);
62 if (hapMouduleInfo) {
63 auto hapInfo = std::make_shared<HapModuleInfo>();
64 *hapInfo = *hapMouduleInfo;
65 return hapInfo;
66 }
67 }
68 return nullptr;
69 }
70
GetAbilityInfo(const std::string & moduleName,const std::string & abilityName) const71 std::shared_ptr<AbilityInfo> BundleContainer::GetAbilityInfo(
72 const std::string &moduleName, const std::string &abilityName) const
73 {
74 if (bundleInfo_ != nullptr) {
75 auto uid = Constants::UNSPECIFIED_USERID;
76 std::optional<AbilityInfo> ablilityInfo = bundleInfo_->FindAbilityInfo(moduleName, abilityName, uid);
77 if (ablilityInfo) {
78 auto aInfo = std::make_shared<AbilityInfo>();
79 *aInfo = *ablilityInfo;
80 return aInfo;
81 }
82 }
83 return nullptr;
84 }
85 } // namespace AppExecFwk
86 } // namespace OHOS
87