• 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 "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 iface_cast<AppExecFwk::BundleMgrProxy>(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     proxy_ = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
39     if (proxy_ == nullptr) {
40         ZLOGE("Failed to get bundle manager proxy.");
41         return nullptr;
42     }
43     deathRecipient_ = new (std::nothrow)BundleMgrProxy::ServiceDeathRecipient(weak_from_this());
44     if (deathRecipient_ == nullptr) {
45         ZLOGE("deathRecipient alloc failed.");
46         return nullptr;
47     }
48     proxy_->AddDeathRecipient(deathRecipient_);
49     return iface_cast<AppExecFwk::BundleMgrProxy>(proxy_);
50 }
51 
GetBundleInfoFromBMS(const std::string & bundleName,int32_t userId,AppExecFwk::BundleInfo & bundleInfo)52 bool BundleMgrProxy::GetBundleInfoFromBMS(
53     const std::string &bundleName, int32_t userId, AppExecFwk::BundleInfo &bundleInfo)
54 {
55     auto bmsClient = GetBundleMgrProxy();
56     if (bmsClient == nullptr) {
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!bundleName is %{public}s, userId is %{public}d", bundleName.c_str(), userId);
64         return false;
65     }
66     return true;
67 }
68 
OnProxyDied()69 void BundleMgrProxy::OnProxyDied()
70 {
71     std::lock_guard<std::mutex> lock(mutex_);
72     if (proxy_ != nullptr) {
73         proxy_->RemoveDeathRecipient(deathRecipient_);
74     }
75     proxy_ = nullptr;
76 }
77 
~BundleMgrProxy()78 BundleMgrProxy::~BundleMgrProxy()
79 {
80     std::lock_guard<std::mutex> lock(mutex_);
81     if (proxy_ != nullptr) {
82         proxy_->RemoveDeathRecipient(deathRecipient_);
83     }
84 }
85 
GetInstance()86 std::shared_ptr<BundleMgrProxy> BundleMgrProxy::GetInstance()
87 {
88     static std::shared_ptr<BundleMgrProxy> proxy(new BundleMgrProxy());
89     return proxy;
90 }
91 } // namespace OHOS::DataShare