• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ipc_skeleton.h"
27 #include "hiview_fault_adapter.h"
28 
29 namespace OHOS::DataShare {
GetBundleMgrProxy()30 sptr<AppExecFwk::BundleMgrProxy> BundleMgrProxy::GetBundleMgrProxy()
31 {
32     std::lock_guard<std::mutex> lock(mutex_);
33     if (proxy_ != nullptr) {
34         return iface_cast<AppExecFwk::BundleMgrProxy>(proxy_);
35     }
36     proxy_ = CheckBMS();
37     if (proxy_ == nullptr) {
38         ZLOGE("BMS service not ready to complete.");
39         return nullptr;
40     }
41     deathRecipient_ = new (std::nothrow)BundleMgrProxy::ServiceDeathRecipient(weak_from_this());
42     if (deathRecipient_ == nullptr) {
43         ZLOGE("deathRecipient alloc failed.");
44         return nullptr;
45     }
46     if (!proxy_->AddDeathRecipient(deathRecipient_)) {
47         ZLOGE("add death recipient failed.");
48         proxy_ = nullptr;
49         deathRecipient_ = nullptr;
50         return nullptr;
51     }
52     return iface_cast<AppExecFwk::BundleMgrProxy>(proxy_);
53 }
54 
CheckBMS()55 sptr<IRemoteObject> BundleMgrProxy::CheckBMS()
56 {
57     sptr<ISystemAbilityManager> systemAbilityManager =
58         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
59     if (systemAbilityManager == nullptr) {
60         ZLOGE("Failed to get system ability mgr.");
61         return nullptr;
62     }
63     return systemAbilityManager->CheckSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
64 }
65 
GetBundleInfoFromBMS(const std::string & bundleName,int32_t userId,BundleConfig & bundleConfig,int32_t appIndex)66 int BundleMgrProxy::GetBundleInfoFromBMS(
67     const std::string &bundleName, int32_t userId, BundleConfig &bundleConfig, int32_t appIndex)
68 {
69     std::string bundleKey = bundleName + std::to_string(userId);
70     if (appIndex != 0) {
71         bundleKey += "appIndex" + std::to_string(appIndex);
72     }
73     auto it = bundleCache_.Find(bundleKey);
74     if (it.first) {
75         bundleConfig = it.second;
76         return E_OK;
77     }
78     auto bmsClient = GetBundleMgrProxy();
79     if (bmsClient == nullptr) {
80         ZLOGE("GetBundleMgrProxy is nullptr!");
81         return E_BMS_NOT_READY;
82     }
83     AppExecFwk::BundleInfo bundleInfo;
84     bool ret;
85     TimeoutReport timeoutReport({bundleName, "", "", __FUNCTION__, 0});
86     if (appIndex == 0) {
87         ret = bmsClient->GetBundleInfo(
88             bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, userId);
89     } else {
90         ret = bmsClient->GetCloneBundleInfo(bundleName,
91             static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY) |
92             static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE),
93             appIndex, bundleInfo, userId);
94         // when there is no error, the former function returns 1 while the new function returns 0
95         ret = !ret;
96         for (auto &item : bundleInfo.hapModuleInfos) {
97             for (auto &item2 : item.extensionInfos) {
98                 bundleInfo.extensionInfos.push_back(item2);
99             }
100         }
101     }
102     timeoutReport.Report(std::to_string(userId), IPCSkeleton::GetCallingPid(), appIndex);
103     if (!ret) {
104         ZLOGE("GetBundleInfo failed!bundleName is %{public}s, userId is %{public}d", bundleName.c_str(), userId);
105         return E_ERROR;
106     }
107     auto [errCode, bundle] = ConvertToDataShareBundle(bundleInfo);
108     if (errCode != E_OK) {
109         ZLOGE("Profile Unmarshall failed! bundleName:%{public}s", URIUtils::Anonymous(bundle.name).c_str());
110         return errCode;
111     }
112     bundleConfig = bundle;
113     bundleCache_.Insert(bundleKey, bundle);
114     return E_OK;
115 }
116 
GetCallerAppIdentifier(const std::string & bundleName,int32_t userId)117 std::pair<int, std::string> BundleMgrProxy::GetCallerAppIdentifier(
118     const std::string &bundleName, int32_t userId)
119 {
120     std::string callerAppIdentifier;
121     std::string callerKey = bundleName + std::to_string(userId);
122     auto it = callerInfoCache_.Find(callerKey);
123     if (it.first) {
124         callerAppIdentifier = it.second;
125         return std::make_pair(E_OK, callerAppIdentifier);
126     }
127     AppExecFwk::BundleInfo bundleInfo;
128     auto bmsClient = GetBundleMgrProxy();
129     if (bmsClient == nullptr) {
130         RADAR_REPORT(__FUNCTION__, RadarReporter::SILENT_ACCESS, RadarReporter::GET_BMS,
131             RadarReporter::FAILED, RadarReporter::ERROR_CODE, RadarReporter::GET_BMS_FAILED);
132         ZLOGE("GetBundleMgrProxy is nullptr!");
133         return std::make_pair(E_BMS_NOT_READY, bundleInfo.signatureInfo.appIdentifier);
134     }
135 
136     int ret = bmsClient->GetBundleInfoV9(bundleName,
137         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO), bundleInfo, userId);
138     if (ret != 0) {
139         RADAR_REPORT(__FUNCTION__, RadarReporter::SILENT_ACCESS, RadarReporter::GET_BMS,
140             RadarReporter::FAILED, RadarReporter::ERROR_CODE, RadarReporter::GET_BUNDLE_INFP_FAILED);
141         ZLOGE("GetBundleInfo failed!bundleName is %{public}s, userId is %{public}d, errcode is %{public}d",
142             bundleName.c_str(), userId, ret);
143         return std::make_pair(E_BUNDLE_NAME_NOT_EXIST, bundleInfo.signatureInfo.appIdentifier);
144     }
145 
146     callerInfoCache_.Insert(callerKey, bundleInfo.signatureInfo.appIdentifier);
147     return std::make_pair(ret, bundleInfo.signatureInfo.appIdentifier);
148 }
149 
OnProxyDied()150 void BundleMgrProxy::OnProxyDied()
151 {
152     std::lock_guard<std::mutex> lock(mutex_);
153     ZLOGE("bundleMgr died, proxy=null ? %{public}s.", proxy_ == nullptr ? "true" : "false");
154     if (proxy_ != nullptr) {
155         proxy_->RemoveDeathRecipient(deathRecipient_);
156     }
157     proxy_ = nullptr;
158     deathRecipient_ = nullptr;
159 }
160 
~BundleMgrProxy()161 BundleMgrProxy::~BundleMgrProxy()
162 {
163     std::lock_guard<std::mutex> lock(mutex_);
164     if (proxy_ != nullptr) {
165         proxy_->RemoveDeathRecipient(deathRecipient_);
166     }
167 }
168 
Delete(const std::string & bundleName,int32_t userId,int32_t appIndex)169 void BundleMgrProxy::Delete(const std::string &bundleName, int32_t userId, int32_t appIndex)
170 {
171     if (appIndex != 0) {
172         bundleCache_.Erase(bundleName + std::to_string(userId) + "appIndex" + std::to_string(appIndex));
173     } else {
174         bundleCache_.Erase(bundleName + std::to_string(userId));
175     }
176     callerInfoCache_.Erase(bundleName + std::to_string(userId));
177     return;
178 }
179 
GetInstance()180 std::shared_ptr<BundleMgrProxy> BundleMgrProxy::GetInstance()
181 {
182     static std::shared_ptr<BundleMgrProxy> proxy(new BundleMgrProxy());
183     return proxy;
184 }
185 
ConvertToDataShareBundle(AppExecFwk::BundleInfo & bundleInfo)186 std::pair<int, BundleConfig> BundleMgrProxy::ConvertToDataShareBundle(AppExecFwk::BundleInfo &bundleInfo)
187 {
188     BundleConfig bundleConfig;
189     bundleConfig.name = std::move(bundleInfo.name);
190     bundleConfig.singleton = std::move(bundleInfo.singleton);
191     auto [errCode, hapModuleInfos] = ConvertHapModuleInfo(bundleInfo);
192     if (errCode != E_OK) {
193         return std::make_pair(errCode, bundleConfig);
194     }
195     bundleConfig.hapModuleInfos = hapModuleInfos;
196 
197     auto [err, extensionInfos] = ConvertExtensionAbility(bundleInfo);
198     if (err != E_OK) {
199         return std::make_pair(err, bundleConfig);
200     }
201     bundleConfig.extensionInfos = extensionInfos;
202     return std::make_pair(E_OK, bundleConfig);
203 }
204 
ConvertExtensionAbility(AppExecFwk::BundleInfo & bundleInfo)205 std::pair<int, std::vector<ExtensionAbilityInfo>> BundleMgrProxy::ConvertExtensionAbility(
206     AppExecFwk::BundleInfo &bundleInfo)
207 {
208     std::vector<ExtensionAbilityInfo> extensionInfos;
209     for (auto &item : bundleInfo.extensionInfos) {
210         if (item.type != AppExecFwk::ExtensionAbilityType::DATASHARE) {
211             continue;
212         }
213         ExtensionAbilityInfo extensionInfo;
214         extensionInfo.type = std::move(item.type);
215         extensionInfo.readPermission = std::move(item.readPermission);
216         extensionInfo.writePermission = std::move(item.writePermission);
217         extensionInfo.uri = std::move(item.uri);
218         extensionInfo.resourcePath = std::move(item.resourcePath);
219         extensionInfo.hapPath = std::move(item.hapPath);
220         ProfileConfig profileConfig;
221         auto [ret, profileInfo] = DataShareProfileConfig::GetDataProperties(
222             item.metadata, extensionInfo.resourcePath, extensionInfo.hapPath, DATA_SHARE_EXTENSION_META);
223         if (ret == NOT_FOUND) {
224             profileConfig.resultCode = NOT_FOUND;
225         }
226         if (ret == ERROR) {
227             profileConfig.resultCode = ERROR;
228             ZLOGE("Profile unmarshall error.uri: %{public}s", URIUtils::Anonymous(extensionInfo.uri).c_str());
229             return std::make_pair(E_ERROR, extensionInfos);
230         }
231         profileConfig.profile = profileInfo;
232         extensionInfo.profileInfo = profileConfig;
233         extensionInfos.emplace_back(extensionInfo);
234     }
235     return std::make_pair(E_OK, extensionInfos);
236 }
237 
ConvertHapModuleInfo(AppExecFwk::BundleInfo & bundleInfo)238 std::pair<int, std::vector<HapModuleInfo>> BundleMgrProxy::ConvertHapModuleInfo(AppExecFwk::BundleInfo &bundleInfo)
239 {
240     std::vector<HapModuleInfo> hapModuleInfos;
241     for (auto &item : bundleInfo.hapModuleInfos) {
242         HapModuleInfo hapModuleInfo;
243         hapModuleInfo.resourcePath = std::move(item.resourcePath);
244         hapModuleInfo.hapPath = std::move(item.hapPath);
245         hapModuleInfo.moduleName = std::move(item.moduleName);
246         std::vector<ProxyData> proxyDatas;
247         for (auto &proxyData : item.proxyDatas) {
248             ProxyData data;
249             data.uri = std::move(proxyData.uri);
250             data.requiredReadPermission = std::move(proxyData.requiredReadPermission);
251             data.requiredWritePermission = std::move(proxyData.requiredWritePermission);
252             ProfileConfig profileConfig;
253             auto [ret, profileInfo] = DataShareProfileConfig::GetDataProperties(
254                 std::vector<AppExecFwk::Metadata>{proxyData.metadata}, hapModuleInfo.resourcePath,
255                 hapModuleInfo.hapPath, DATA_SHARE_PROPERTIES_META);
256             if (ret == NOT_FOUND) {
257                 profileConfig.resultCode = NOT_FOUND;
258             }
259             if (ret == ERROR) {
260                 profileConfig.resultCode = ERROR;
261                 ZLOGE("Profile unmarshall error.uri: %{public}s", URIUtils::Anonymous(data.uri).c_str());
262                 return std::make_pair(E_ERROR, hapModuleInfos);
263             }
264             profileConfig.profile = profileInfo;
265             data.profileInfo = profileConfig;
266             proxyDatas.emplace_back(data);
267         }
268         hapModuleInfo.proxyDatas = proxyDatas;
269         hapModuleInfos.emplace_back(hapModuleInfo);
270     }
271     return std::make_pair(E_OK, hapModuleInfos);
272 }
273 } // namespace OHOS::DataShare