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 "pinauth_register.h" 17 18 #include <if_system_ability_manager.h> 19 #include <iservice_registry.h> 20 #include <system_ability_definition.h> 21 22 #include "refbase.h" 23 #include "iremote_broker.h" 24 #include "iremote_object.h" 25 26 #include "inputer_get_data_service.h" 27 #include "inputer_get_data.h" 28 #include "iam_logger.h" 29 30 #define LOG_LABEL OHOS::UserIam::Common::LABEL_PIN_AUTH_SDK 31 32 namespace OHOS { 33 namespace UserIam { 34 namespace PinAuth { 35 PinAuthRegister::PinAuthRegister() = default; 36 PinAuthRegister::~PinAuthRegister() = default; 37 RegisterInputer(std::shared_ptr<IInputer> inputer)38bool PinAuthRegister::RegisterInputer(std::shared_ptr<IInputer> inputer) 39 { 40 IAM_LOGI("start"); 41 if (inputer == nullptr) { 42 IAM_LOGE("inputer is nullptr"); 43 return false; 44 } 45 auto proxy = GetProxy(); 46 if (proxy == nullptr) { 47 IAM_LOGE("get proxy failed"); 48 return false; 49 } 50 sptr<InputerGetData> callback = new (std::nothrow) InputerGetDataService(inputer); 51 if (callback == nullptr) { 52 return false; 53 } 54 return proxy->RegisterInputer(callback); 55 } 56 UnRegisterInputer()57void PinAuthRegister::UnRegisterInputer() 58 { 59 IAM_LOGI("start"); 60 auto proxy = GetProxy(); 61 if (proxy == nullptr) { 62 IAM_LOGE("proxy is nullptr"); 63 return; 64 } 65 proxy->UnRegisterInputer(); 66 } 67 GetProxy()68sptr<PinAuthInterface> PinAuthRegister::GetProxy() 69 { 70 IAM_LOGI("start"); 71 if (proxy_ != nullptr) { 72 return proxy_; 73 } 74 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 75 if (sam == nullptr) { 76 IAM_LOGE("get system ability manager fail"); 77 return nullptr; 78 } 79 sptr<IRemoteObject> obj = sam->CheckSystemAbility(SUBSYS_USERIAM_SYS_ABILITY_PINAUTH); 80 if (obj == nullptr) { 81 IAM_LOGE("get distributed gallery manager service fail"); 82 return nullptr; 83 } 84 sptr<IRemoteObject::DeathRecipient> dr = new PinAuthDeathRecipient(); 85 if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) { 86 IAM_LOGE("add death recipient fail"); 87 return nullptr; 88 } 89 90 proxy_ = iface_cast<PinAuthInterface>(obj); 91 deathRecipient_ = dr; 92 IAM_LOGI("succeed to connect distributed gallery manager service"); 93 return proxy_; 94 } 95 ResetProxy(const wptr<IRemoteObject> & remote)96void PinAuthRegister::ResetProxy(const wptr<IRemoteObject>& remote) 97 { 98 IAM_LOGI("start"); 99 std::lock_guard<std::mutex> lock(mutex_); 100 auto serviceRemote = proxy_->AsObject(); 101 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) { 102 serviceRemote->RemoveDeathRecipient(deathRecipient_); 103 proxy_ = nullptr; 104 } 105 } 106 OnRemoteDied(const wptr<IRemoteObject> & remote)107void PinAuthRegister::PinAuthDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote) 108 { 109 IAM_LOGI("start"); 110 if (remote == nullptr) { 111 IAM_LOGE("remote is nullptr"); 112 return; 113 } 114 PinAuthRegister::GetInstance().ResetProxy(remote); 115 IAM_LOGI("recv death notice"); 116 } 117 } // namespace PinAuth 118 } // namespace UserIam 119 } // namespace OHOS