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_impl.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 { RegisterInputer(std::shared_ptr<IInputer> inputer)35bool PinAuthRegisterImpl::RegisterInputer(std::shared_ptr<IInputer> inputer) 36 { 37 IAM_LOGI("start"); 38 if (inputer == nullptr) { 39 IAM_LOGE("inputer is nullptr"); 40 return false; 41 } 42 auto proxy = GetProxy(); 43 if (proxy == nullptr) { 44 IAM_LOGE("get proxy failed"); 45 return false; 46 } 47 sptr<InputerGetData> callback(new (std::nothrow) InputerGetDataService(inputer)); 48 if (callback == nullptr) { 49 return false; 50 } 51 return proxy->RegisterInputer(callback); 52 } 53 UnRegisterInputer()54void PinAuthRegisterImpl::UnRegisterInputer() 55 { 56 IAM_LOGI("start"); 57 auto proxy = GetProxy(); 58 if (proxy == nullptr) { 59 IAM_LOGE("proxy is nullptr"); 60 return; 61 } 62 proxy->UnRegisterInputer(); 63 } 64 GetProxy()65sptr<PinAuthInterface> PinAuthRegisterImpl::GetProxy() 66 { 67 IAM_LOGI("start"); 68 if (proxy_ != nullptr) { 69 return proxy_; 70 } 71 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 72 if (sam == nullptr) { 73 IAM_LOGE("get system ability manager fail"); 74 return nullptr; 75 } 76 sptr<IRemoteObject> obj = sam->CheckSystemAbility(SUBSYS_USERIAM_SYS_ABILITY_PINAUTH); 77 if (obj == nullptr) { 78 IAM_LOGE("get distributed gallery manager service fail"); 79 return nullptr; 80 } 81 sptr<IRemoteObject::DeathRecipient> dr(new (std::nothrow) PinAuthDeathRecipient()); 82 if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) { 83 IAM_LOGE("add death recipient fail"); 84 return nullptr; 85 } 86 87 proxy_ = iface_cast<PinAuthInterface>(obj); 88 deathRecipient_ = dr; 89 IAM_LOGI("succeed to connect distributed gallery manager service"); 90 return proxy_; 91 } 92 ResetProxy(const wptr<IRemoteObject> & remote)93void PinAuthRegisterImpl::ResetProxy(const wptr<IRemoteObject>& remote) 94 { 95 IAM_LOGI("start"); 96 std::lock_guard<std::mutex> lock(mutex_); 97 auto serviceRemote = proxy_->AsObject(); 98 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) { 99 serviceRemote->RemoveDeathRecipient(deathRecipient_); 100 proxy_ = nullptr; 101 } 102 } 103 Instance()104PinAuthRegisterImpl &PinAuthRegisterImpl::Instance() 105 { 106 static PinAuthRegisterImpl impl; 107 return impl; 108 } 109 GetInstance()110PinAuthRegister &PinAuthRegister::GetInstance() 111 { 112 return PinAuthRegisterImpl::Instance(); 113 } 114 OnRemoteDied(const wptr<IRemoteObject> & remote)115void PinAuthRegisterImpl::PinAuthDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote) 116 { 117 IAM_LOGI("start"); 118 if (remote == nullptr) { 119 IAM_LOGE("remote is nullptr"); 120 return; 121 } 122 PinAuthRegisterImpl::Instance().ResetProxy(remote); 123 IAM_LOGI("recv death notice"); 124 } 125 } // namespace PinAuth 126 } // namespace UserIam 127 } // namespace OHOS