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