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