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