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(const std::shared_ptr<PrintServiceHelper> & helper)30 void PrintBMSHelper::SetHelper(const 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 std::lock_guard<std::mutex> lock(mutex_);
39 if (!GetProxy()) {
40 return false;
41 }
42 std::vector<int> osAccountIds;
43 if (!helper_->QueryAccounts(osAccountIds)) {
44 return false;
45 }
46
47 for (auto userId : osAccountIds) {
48 PRINT_HILOGE("active userId = %{public}d", userId);
49 helper_->QueryExtension(sptrBundleMgr_, userId, extensionInfos);
50 }
51 return true;
52 }
53
QueryCallerBundleName()54 std::string PrintBMSHelper::QueryCallerBundleName()
55 {
56 std::lock_guard<std::mutex> lock(mutex_);
57 if (!GetProxy()) {
58 return "";
59 }
60 int32_t callerUid = IPCSkeleton::GetCallingUid();
61 std::string bundleName = "";
62 helper_->QueryNameForUid(sptrBundleMgr_, callerUid, bundleName);
63 PRINT_HILOGD("callerUid = %{public}d, bundleName = %{public}s", callerUid, bundleName.c_str());
64 return bundleName;
65 }
66
GetProxy()67 bool PrintBMSHelper::GetProxy()
68 {
69 if (sptrBundleMgr_ == nullptr) {
70 if (helper_ == nullptr) {
71 PRINT_HILOGE("Invalid printer helper.");
72 return false;
73 }
74 auto remoteObject = helper_->GetBundleMgr();
75 if (remoteObject == nullptr) {
76 PRINT_HILOGE("Failed to get bundle manager service.");
77 return false;
78 }
79
80 sptrBundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
81 if (sptrBundleMgr_ == nullptr) {
82 PRINT_HILOGE("Failed to get system bundle manager services ability");
83 return false;
84 }
85
86 printBMSDeath_ = new (std::nothrow) PrintBMSDeathRecipient();
87 if (printBMSDeath_ == nullptr) {
88 PRINT_HILOGE("Failed to create death Recipient ptr BMSDeathRecipient");
89 return false;
90 }
91 remoteObject->AddDeathRecipient(printBMSDeath_);
92 }
93 return true;
94 }
95
ResetProxy(const wptr<IRemoteObject> & remote)96 void PrintBMSHelper::ResetProxy(const wptr<IRemoteObject> &remote)
97 {
98 if (remote == nullptr) {
99 PRINT_HILOGE("remote is nullptr");
100 return;
101 }
102
103 std::lock_guard<std::mutex> lock(mutex_);
104 if (sptrBundleMgr_ == nullptr) {
105 PRINT_HILOGE("sptrBundleMgr_ is null");
106 return;
107 }
108
109 auto serviceRemote = sptrBundleMgr_->AsObject();
110 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
111 PRINT_HILOGD("need reset");
112 serviceRemote->RemoveDeathRecipient(printBMSDeath_);
113 sptrBundleMgr_ = nullptr;
114 printBMSDeath_ = nullptr;
115 }
116 }
117 } // namespace OHOS
118