1 /*
2  * Copyright (c) 2021-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 
16 #include "print_bms_helper.h"
17 #include "bundle_constants.h"
18 #include "ipc_skeleton.h"
19 #include "print_log.h"
20 
21 namespace OHOS::Print {
22 using namespace OHOS::AppExecFwk::Constants;
23 
PrintBMSHelper()24 PrintBMSHelper::PrintBMSHelper() : sptrBundleMgr_(nullptr), printBMSDeath_(nullptr), helper_(nullptr)
25 {}
26 
~PrintBMSHelper()27 PrintBMSHelper::~PrintBMSHelper()
28 {}
29 
SetHelper(std::shared_ptr<PrintServiceHelper> & helper)30 void PrintBMSHelper::SetHelper(std::shared_ptr<PrintServiceHelper> &helper)
31 {
32     helper_ = helper;
33     sptrBundleMgr_ = nullptr;
34 }
35 
QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)36 bool PrintBMSHelper::QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
37 {
38     if (!hasBms) {
39         PRINT_HILOGE("Don't have BMS.");
40         return false;
41     }
42     std::lock_guard<std::mutex> lock(mutex_);
43     if (helper_ == nullptr || !GetProxy()) {
44         return false;
45     }
46     std::vector<int> osAccountIds;
47     if (!helper_->QueryAccounts(osAccountIds)) {
48         return false;
49     }
50 
51     for (auto userId : osAccountIds) {
52         PRINT_HILOGI("active userId = %{public}d", userId);
53         helper_->QueryExtension(sptrBundleMgr_, userId, extensionInfos);
54     }
55     return true;
56 }
57 
QueryCallerBundleName()58 std::string PrintBMSHelper::QueryCallerBundleName()
59 {
60     std::lock_guard<std::mutex> lock(mutex_);
61     if (helper_ == nullptr || !GetProxy()) {
62         return "";
63     }
64     int32_t callerUid = IPCSkeleton::GetCallingUid();
65     std::string bundleName = "";
66     helper_->QueryNameForUid(sptrBundleMgr_, callerUid, bundleName);
67     PRINT_HILOGD("callerUid = %{public}d, bundleName = %{public}s", callerUid, bundleName.c_str());
68     return bundleName;
69 }
70 
GetProxy()71 bool PrintBMSHelper::GetProxy()
72 {
73     if (sptrBundleMgr_ == nullptr) {
74         if (helper_ == nullptr) {
75             PRINT_HILOGE("Invalid printer helper.");
76             return false;
77         }
78         auto remoteObject = helper_->GetBundleMgr();
79         if (remoteObject == nullptr) {
80             PRINT_HILOGE("Failed to get bundle manager service.");
81             return false;
82         }
83 
84         sptrBundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
85         if (sptrBundleMgr_ == nullptr) {
86             PRINT_HILOGE("Failed to get system bundle manager services ability");
87             return false;
88         }
89 
90         printBMSDeath_ = new (std::nothrow) PrintBMSDeathRecipient();
91         if (printBMSDeath_ == nullptr) {
92             PRINT_HILOGE("Failed to create death Recipient ptr BMSDeathRecipient");
93             return false;
94         }
95         remoteObject->AddDeathRecipient(printBMSDeath_);
96     }
97     return true;
98 }
99 
ResetProxy(const wptr<IRemoteObject> & remote)100 void PrintBMSHelper::ResetProxy(const wptr<IRemoteObject> &remote)
101 {
102     if (remote == nullptr) {
103         PRINT_HILOGE("remote is nullptr");
104         return;
105     }
106 
107     std::lock_guard<std::mutex> lock(mutex_);
108     if (sptrBundleMgr_ == nullptr) {
109         PRINT_HILOGE("sptrBundleMgr_ is null");
110         return;
111     }
112 
113     auto serviceRemote = sptrBundleMgr_->AsObject();
114     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
115         PRINT_HILOGD("need reset");
116         serviceRemote->RemoveDeathRecipient(printBMSDeath_);
117         sptrBundleMgr_ = nullptr;
118         delete printBMSDeath_;
119         printBMSDeath_ = nullptr;
120     }
121 }
122 }  // namespace OHOS
123