1 /*
2 * Copyright (c) 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 #define LOG_TAG "BundleMgrProxy"
16 #include "bundle_mgr_proxy.h"
17
18 #include "account/account_delegate.h"
19 #include "datashare_errno.h"
20 #include "datashare_radar_reporter.h"
21 #include "if_system_ability_manager.h"
22 #include "iservice_registry.h"
23 #include "log_print.h"
24 #include "system_ability_definition.h"
25 #include "uri_utils.h"
26
27 namespace OHOS::DataShare {
GetBundleMgrProxy()28 sptr<AppExecFwk::BundleMgrProxy> BundleMgrProxy::GetBundleMgrProxy()
29 {
30 std::lock_guard<std::mutex> lock(mutex_);
31 if (proxy_ != nullptr) {
32 return iface_cast<AppExecFwk::BundleMgrProxy>(proxy_);
33 }
34 proxy_ = CheckBMS();
35 if (proxy_ == nullptr) {
36 ZLOGE("BMS service not ready to complete.");
37 return nullptr;
38 }
39 deathRecipient_ = new (std::nothrow)BundleMgrProxy::ServiceDeathRecipient(weak_from_this());
40 if (deathRecipient_ == nullptr) {
41 ZLOGE("deathRecipient alloc failed.");
42 return nullptr;
43 }
44 if (!proxy_->AddDeathRecipient(deathRecipient_)) {
45 ZLOGE("add death recipient failed.");
46 proxy_ = nullptr;
47 deathRecipient_ = nullptr;
48 return nullptr;
49 }
50 return iface_cast<AppExecFwk::BundleMgrProxy>(proxy_);
51 }
52
CheckBMS()53 sptr<IRemoteObject> BundleMgrProxy::CheckBMS()
54 {
55 sptr<ISystemAbilityManager> systemAbilityManager =
56 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
57 if (systemAbilityManager == nullptr) {
58 ZLOGE("Failed to get system ability mgr.");
59 return nullptr;
60 }
61 return systemAbilityManager->CheckSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
62 }
63
GetBundleInfoFromBMS(const std::string & bundleName,int32_t userId,BundleConfig & bundleConfig)64 int BundleMgrProxy::GetBundleInfoFromBMS(
65 const std::string &bundleName, int32_t userId, BundleConfig &bundleConfig)
66 {
67 auto bundleKey = bundleName + std::to_string(userId);
68 auto it = bundleCache_.Find(bundleKey);
69 if (it.first) {
70 bundleConfig = it.second;
71 return E_OK;
72 }
73 auto bmsClient = GetBundleMgrProxy();
74 if (bmsClient == nullptr) {
75 RADAR_REPORT(__FUNCTION__, RadarReporter::SILENT_ACCESS, RadarReporter::GET_BMS,
76 RadarReporter::FAILED, RadarReporter::ERROR_CODE, RadarReporter::GET_BMS_FAILED);
77 ZLOGE("GetBundleMgrProxy is nullptr!");
78 return E_BMS_NOT_READY;
79 }
80 AppExecFwk::BundleInfo bundleInfo;
81 bool ret = bmsClient->GetBundleInfo(
82 bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, userId);
83 if (!ret) {
84 RADAR_REPORT(__FUNCTION__, RadarReporter::SILENT_ACCESS, RadarReporter::GET_BMS,
85 RadarReporter::FAILED, RadarReporter::ERROR_CODE, RadarReporter::GET_BUNDLE_INFP_FAILED);
86 ZLOGE("GetBundleInfo failed!bundleName is %{public}s, userId is %{public}d", bundleName.c_str(), userId);
87 return E_BUNDLE_NAME_NOT_EXIST;
88 }
89 auto [errCode, bundle] = ConvertToDataShareBundle(bundleInfo);
90 if (errCode != E_OK) {
91 ZLOGE("Profile Unmarshall failed! bundleName:%{public}s", URIUtils::Anonymous(bundle.name).c_str());
92 return errCode;
93 }
94 bundleConfig = bundle;
95 bundleCache_.Insert(bundleKey, bundle);
96 return E_OK;
97 }
98
OnProxyDied()99 void BundleMgrProxy::OnProxyDied()
100 {
101 std::lock_guard<std::mutex> lock(mutex_);
102 ZLOGE("bundleMgr died, proxy=null ? %{public}s.", proxy_ == nullptr ? "true" : "false");
103 if (proxy_ != nullptr) {
104 proxy_->RemoveDeathRecipient(deathRecipient_);
105 }
106 proxy_ = nullptr;
107 deathRecipient_ = nullptr;
108 }
109
~BundleMgrProxy()110 BundleMgrProxy::~BundleMgrProxy()
111 {
112 std::lock_guard<std::mutex> lock(mutex_);
113 if (proxy_ != nullptr) {
114 proxy_->RemoveDeathRecipient(deathRecipient_);
115 }
116 }
117
Delete(const std::string & bundleName,int32_t userId)118 void BundleMgrProxy::Delete(const std::string &bundleName, int32_t userId)
119 {
120 bundleCache_.Erase(bundleName + std::to_string(userId));
121 return;
122 }
123
GetInstance()124 std::shared_ptr<BundleMgrProxy> BundleMgrProxy::GetInstance()
125 {
126 static std::shared_ptr<BundleMgrProxy> proxy(new BundleMgrProxy());
127 return proxy;
128 }
129
ConvertToDataShareBundle(AppExecFwk::BundleInfo & bundleInfo)130 std::pair<int, BundleConfig> BundleMgrProxy::ConvertToDataShareBundle(AppExecFwk::BundleInfo &bundleInfo)
131 {
132 BundleConfig bundleConfig;
133 bundleConfig.name = std::move(bundleInfo.name);
134 bundleConfig.singleton = std::move(bundleInfo.singleton);
135 auto [errCode, hapModuleInfos] = ConvertHapModuleInfo(bundleInfo);
136 if (errCode != E_OK) {
137 return std::make_pair(errCode, bundleConfig);
138 }
139 bundleConfig.hapModuleInfos = hapModuleInfos;
140
141 auto [err, extensionInfos] = ConvertExtensionAbility(bundleInfo);
142 if (err != E_OK) {
143 return std::make_pair(err, bundleConfig);
144 }
145 bundleConfig.extensionInfos = extensionInfos;
146 return std::make_pair(E_OK, bundleConfig);
147 }
148
ConvertExtensionAbility(AppExecFwk::BundleInfo & bundleInfo)149 std::pair<int, std::vector<ExtensionAbilityInfo>> BundleMgrProxy::ConvertExtensionAbility(
150 AppExecFwk::BundleInfo &bundleInfo)
151 {
152 std::vector<ExtensionAbilityInfo> extensionInfos;
153 for (auto &item : bundleInfo.extensionInfos) {
154 if (item.type != AppExecFwk::ExtensionAbilityType::DATASHARE) {
155 continue;
156 }
157 ExtensionAbilityInfo extensionInfo;
158 extensionInfo.type = std::move(item.type);
159 extensionInfo.readPermission = std::move(item.readPermission);
160 extensionInfo.writePermission = std::move(item.writePermission);
161 extensionInfo.uri = std::move(item.uri);
162 extensionInfo.resourcePath = std::move(item.resourcePath);
163 extensionInfo.hapPath = std::move(item.hapPath);
164 ProfileConfig profileConfig;
165 auto [ret, profileInfo] = DataShareProfileConfig::GetDataProperties(
166 item.metadata, extensionInfo.resourcePath, extensionInfo.hapPath, DATA_SHARE_EXTENSION_META);
167 if (ret == NOT_FOUND) {
168 profileConfig.resultCode = NOT_FOUND;
169 }
170 if (ret == ERROR) {
171 profileConfig.resultCode = ERROR;
172 ZLOGE("Profile unmarshall error.uri: %{public}s", URIUtils::Anonymous(extensionInfo.uri).c_str());
173 return std::make_pair(E_ERROR, extensionInfos);
174 }
175 profileConfig.profile = profileInfo;
176 extensionInfo.profileInfo = profileConfig;
177 extensionInfos.emplace_back(extensionInfo);
178 }
179 return std::make_pair(E_OK, extensionInfos);
180 }
181
ConvertHapModuleInfo(AppExecFwk::BundleInfo & bundleInfo)182 std::pair<int, std::vector<HapModuleInfo>> BundleMgrProxy::ConvertHapModuleInfo(AppExecFwk::BundleInfo &bundleInfo)
183 {
184 std::vector<HapModuleInfo> hapModuleInfos;
185 for (auto &item : bundleInfo.hapModuleInfos) {
186 HapModuleInfo hapModuleInfo;
187 hapModuleInfo.resourcePath = std::move(item.resourcePath);
188 hapModuleInfo.hapPath = std::move(item.hapPath);
189 hapModuleInfo.moduleName = std::move(item.moduleName);
190 std::vector<ProxyData> proxyDatas;
191 for (auto &proxyData : item.proxyDatas) {
192 ProxyData data;
193 data.uri = std::move(proxyData.uri);
194 data.requiredReadPermission = std::move(proxyData.requiredReadPermission);
195 data.requiredWritePermission = std::move(proxyData.requiredWritePermission);
196 ProfileConfig profileConfig;
197 auto [ret, profileInfo] = DataShareProfileConfig::GetDataProperties(
198 std::vector<AppExecFwk::Metadata>{proxyData.metadata}, hapModuleInfo.resourcePath,
199 hapModuleInfo.hapPath, DATA_SHARE_PROPERTIES_META);
200 if (ret == NOT_FOUND) {
201 profileConfig.resultCode = NOT_FOUND;
202 }
203 if (ret == ERROR) {
204 profileConfig.resultCode = ERROR;
205 ZLOGE("Profile unmarshall error.uri: %{public}s", URIUtils::Anonymous(data.uri).c_str());
206 return std::make_pair(E_ERROR, hapModuleInfos);
207 }
208 profileConfig.profile = profileInfo;
209 data.profileInfo = profileConfig;
210 proxyDatas.emplace_back(data);
211 }
212 hapModuleInfo.proxyDatas = proxyDatas;
213 hapModuleInfos.emplace_back(hapModuleInfo);
214 }
215 return std::make_pair(E_OK, hapModuleInfos);
216 }
217 } // namespace OHOS::DataShare