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