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
16 #include "co_auth_client_impl.h"
17
18 #include "system_ability_definition.h"
19
20 #include "callback_manager.h"
21 #include "executor_callback_service.h"
22 #include "iam_logger.h"
23 #include "ipc_client_utils.h"
24
25 #define LOG_TAG "EXECUTOR_MGR_SDK"
26
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
InitIpcExecutorInfo(const ExecutorInfo & info,IpcExecutorRegisterInfo & ipcExecutorRegisterInfo)30 void CoAuthClientImpl::InitIpcExecutorInfo(const ExecutorInfo &info, IpcExecutorRegisterInfo &ipcExecutorRegisterInfo)
31 {
32 ipcExecutorRegisterInfo.authType = static_cast<int32_t>(info.authType);
33 ipcExecutorRegisterInfo.executorRole = static_cast<int32_t>(info.executorRole);
34 ipcExecutorRegisterInfo.executorSensorHint = static_cast<uint32_t>(info.executorSensorHint);
35 ipcExecutorRegisterInfo.executorMatcher = static_cast<uint32_t>(info.executorMatcher);
36 ipcExecutorRegisterInfo.esl = static_cast<int32_t>(info.esl);
37 ipcExecutorRegisterInfo.publicKey = info.publicKey;
38 ipcExecutorRegisterInfo.deviceUdid = info.deviceUdid;
39 ipcExecutorRegisterInfo.signedRemoteExecutorInfo = info.signedRemoteExecutorInfo;
40 ipcExecutorRegisterInfo.maxTemplateAcl = static_cast<uint32_t>(info.maxTemplateAcl);
41 }
42
Register(const ExecutorInfo & info,const std::shared_ptr<ExecutorRegisterCallback> & callback)43 uint64_t CoAuthClientImpl::Register(const ExecutorInfo &info, const std::shared_ptr<ExecutorRegisterCallback> &callback)
44 {
45 IAM_LOGI("start type:%{public}d role:%{public}d", info.authType, info.executorRole);
46 if (!callback) {
47 IAM_LOGE("callback is nullptr");
48 return INVALID_EXECUTOR_INDEX;
49 }
50
51 auto proxy = GetProxy();
52 if (!proxy) {
53 return INVALID_EXECUTOR_INDEX;
54 }
55
56 IpcExecutorRegisterInfo regInfo = {};
57 InitIpcExecutorInfo(info, regInfo);
58 sptr<IExecutorCallback> wrapper(new (std::nothrow) ExecutorCallbackService(callback));
59 if (wrapper == nullptr) {
60 IAM_LOGE("failed to create wrapper");
61 return INVALID_EXECUTOR_INDEX;
62 }
63 uint64_t executorIndex = INVALID_EXECUTOR_INDEX;
64 auto ret = proxy->ExecutorRegister(regInfo, wrapper, executorIndex);
65 if (ret != SUCCESS) {
66 IAM_LOGE("ExecutorRegister fail, ret:%{public}d", ret);
67 return INVALID_EXECUTOR_INDEX;
68 }
69
70 return executorIndex;
71 }
72
Unregister(uint64_t executorIndex)73 void CoAuthClientImpl::Unregister(uint64_t executorIndex)
74 {
75 IAM_LOGI("start");
76
77 auto proxy = GetProxy();
78 if (!proxy) {
79 return;
80 }
81
82 auto ret = proxy->ExecutorUnregister(executorIndex);
83 if (ret != SUCCESS) {
84 IAM_LOGE("ExecutorUnregister fail, ret:%{public}d", ret);
85 return;
86 }
87 }
88
GetProxy()89 sptr<ICoAuth> CoAuthClientImpl::GetProxy()
90 {
91 std::lock_guard<std::mutex> lock(mutex_);
92 if (proxy_ != nullptr) {
93 return proxy_;
94 }
95 sptr<IRemoteObject> obj = IpcClientUtils::GetRemoteObject(SUBSYS_USERIAM_SYS_ABILITY_AUTHEXECUTORMGR);
96 if (obj == nullptr) {
97 IAM_LOGE("remote object is null");
98 return proxy_;
99 }
100 sptr<IRemoteObject::DeathRecipient> dr(new (std::nothrow) CoAuthImplDeathRecipient());
101 if ((dr == nullptr) || (obj->IsProxyObject() && !obj->AddDeathRecipient(dr))) {
102 IAM_LOGE("add death recipient fail");
103 return proxy_;
104 }
105
106 proxy_ = iface_cast<ICoAuth>(obj);
107 deathRecipient_ = dr;
108 return proxy_;
109 }
110
ResetProxy(const wptr<IRemoteObject> & remote)111 void CoAuthClientImpl::ResetProxy(const wptr<IRemoteObject> &remote)
112 {
113 IAM_LOGI("start");
114 std::lock_guard<std::mutex> lock(mutex_);
115 if (proxy_ == nullptr) {
116 IAM_LOGE("proxy_ is null");
117 return;
118 }
119 auto serviceRemote = proxy_->AsObject();
120 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
121 IAM_LOGI("need reset");
122 serviceRemote->RemoveDeathRecipient(deathRecipient_);
123 proxy_ = nullptr;
124 deathRecipient_ = nullptr;
125 }
126 IAM_LOGI("end reset proxy");
127 }
128
OnRemoteDied(const wptr<IRemoteObject> & remote)129 void CoAuthClientImpl::CoAuthImplDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
130 {
131 IAM_LOGI("start");
132 if (remote == nullptr) {
133 IAM_LOGE("remote is nullptr");
134 return;
135 }
136 CallbackManager::GetInstance().OnServiceDeath();
137 CoAuthClientImpl::Instance().ResetProxy(remote);
138 }
139
Instance()140 CoAuthClientImpl &CoAuthClientImpl::Instance()
141 {
142 static CoAuthClientImpl impl;
143 return impl;
144 }
145
GetInstance()146 CoAuthClient &CoAuthClient::GetInstance()
147 {
148 return CoAuthClientImpl::Instance();
149 }
150 } // namespace UserAuth
151 } // namespace UserIam
152 } // namespace OHOS