• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #include "bundle_resource_helper.h"
16 
17 #include "iservice_registry.h"
18 #include "system_ability_definition.h"
19 
20 namespace OHOS {
21 namespace Notification {
22 
BundleResourceHelper()23 BundleResourceHelper::BundleResourceHelper()
24 {
25     deathRecipient_ = new (std::nothrow)
26         BundleDeathRecipient(std::bind(&BundleResourceHelper::OnRemoteDied, this, std::placeholders::_1));
27     if (deathRecipient_ == nullptr) {
28         ANS_LOGE("Failed to create BundleDeathRecipient instance");
29     }
30 }
31 
~BundleResourceHelper()32 BundleResourceHelper::~BundleResourceHelper()
33 {
34     std::lock_guard<std::mutex> lock(connectionMutex_);
35     Disconnect();
36 }
37 
Connect()38 void BundleResourceHelper::Connect()
39 {
40     if (bundleMgr_ != nullptr) {
41         return;
42     }
43 
44     sptr<ISystemAbilityManager> systemAbilityManager =
45         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
46     if (systemAbilityManager == nullptr) {
47         return;
48     }
49 
50     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
51     if (remoteObject == nullptr) {
52         return;
53     }
54 
55     bundleMgr_ = OHOS::iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
56     if (bundleMgr_ != nullptr) {
57         bundleMgr_->AsObject()->AddDeathRecipient(deathRecipient_);
58     }
59 }
60 
Disconnect()61 void BundleResourceHelper::Disconnect()
62 {
63     if (bundleMgr_ != nullptr) {
64         bundleMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_);
65         bundleMgr_ = nullptr;
66     }
67 }
68 
OnRemoteDied(const wptr<IRemoteObject> & object)69 void BundleResourceHelper::OnRemoteDied(const wptr<IRemoteObject> &object)
70 {
71     std::lock_guard<std::mutex> lock(connectionMutex_);
72     Disconnect();
73 }
74 
GetBundleInfo(const std::string & bundleName,AppExecFwk::BundleResourceInfo & bundleResourceInfo,const int32_t appIndex)75 ErrCode BundleResourceHelper::GetBundleInfo(const std::string &bundleName,
76     AppExecFwk::BundleResourceInfo &bundleResourceInfo, const int32_t appIndex)
77 {
78     ErrCode result = 0;
79     std::lock_guard<std::mutex> lock(connectionMutex_);
80     Connect();
81     if (bundleMgr_ == nullptr) {
82         ANS_LOGE("GetBundleInfo bundle proxy failed.");
83         return -1;
84     }
85     sptr<AppExecFwk::IBundleResource> bundleResourceProxy = bundleMgr_->GetBundleResourceProxy();
86     if (!bundleResourceProxy) {
87         ANS_LOGE("GetBundleInfo, get bundle resource proxy failed.");
88         return -1;
89     }
90 
91     std::string identity = IPCSkeleton::ResetCallingIdentity();
92     int32_t flag = static_cast<int32_t>(AppExecFwk::ResourceFlag::GET_RESOURCE_INFO_WITH_ICON);
93     result = bundleResourceProxy->GetBundleResourceInfo(bundleName, flag, bundleResourceInfo, appIndex);
94     IPCSkeleton::SetCallingIdentity(identity);
95     return result;
96 }
97 }
98 }
99