1 /*
2 * Copyright (c) 2022 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 "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "log_print.h"
22 #include "system_ability_definition.h"
23
24 namespace OHOS::DataShare {
GetBundleMgrProxy()25 sptr<AppExecFwk::BundleMgrProxy> BundleMgrProxy::GetBundleMgrProxy()
26 {
27 std::lock_guard<std::mutex> lock(mutex_);
28 if (proxy_ != nullptr) {
29 return proxy_;
30 }
31 sptr<ISystemAbilityManager> systemAbilityManager =
32 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
33 if (systemAbilityManager == nullptr) {
34 ZLOGE("Failed to get system ability mgr.");
35 return nullptr;
36 }
37
38 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
39 if (remoteObject == nullptr) {
40 ZLOGE("Failed to get bundle manager proxy.");
41 return nullptr;
42 }
43 sptr<BundleMgrProxy::ServiceDeathRecipient> deathRecipient = new (std::nothrow)
44 BundleMgrProxy::ServiceDeathRecipient(this);
45 remoteObject->AddDeathRecipient(deathRecipient);
46 proxy_ = iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
47 ZLOGD("Get bundle manager proxy success.");
48 return proxy_;
49 }
50
GetBundleInfoFromBMS(const std::string & bundleName,uint32_t tokenId,AppExecFwk::BundleInfo & bundleInfo)51 bool BundleMgrProxy::GetBundleInfoFromBMS(
52 const std::string &bundleName, uint32_t tokenId, AppExecFwk::BundleInfo &bundleInfo)
53 {
54 auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId);
55 auto bmsClient = GetBundleMgrProxy();
56 if (!bmsClient) {
57 ZLOGE("GetBundleMgrProxy is nullptr!");
58 return false;
59 }
60 bool ret = bmsClient->GetBundleInfo(
61 bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, userId);
62 if (!ret) {
63 ZLOGE("GetBundleInfo failed!");
64 return false;
65 }
66 return true;
67 }
68
OnProxyDied()69 void BundleMgrProxy::OnProxyDied()
70 {
71 std::lock_guard<std::mutex> lock(mutex_);
72 proxy_ = nullptr;
73 ZLOGE("DIED.");
74 }
75 } // namespace OHOS::DataShare