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