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 "pin_auth_manager.h"
17 #include "iam_logger.h"
18
19 #define LOG_LABEL UserIam::Common::LABEL_PIN_AUTH_SA
20
21 namespace OHOS {
22 namespace UserIam {
23 namespace PinAuth {
24 PinAuthManager::PinAuthManager() = default;
25 PinAuthManager::~PinAuthManager() = default;
RegisterInputer(uint32_t tokenId,const sptr<InputerGetData> & inputer)26 bool PinAuthManager::RegisterInputer(uint32_t tokenId, const sptr<InputerGetData> &inputer)
27 {
28 std::lock_guard<std::mutex> guard(mutex_);
29 IAM_LOGI("start, tokenId = %{public}u", tokenId);
30 if (pinAuthInputerMap_.find(tokenId) != pinAuthInputerMap_.end()) {
31 IAM_LOGE("inputer is already register, do not repeat");
32 return false;
33 }
34 if (inputer == nullptr) {
35 IAM_LOGE("inputer is nullptr");
36 return false;
37 }
38 pinAuthInputerMap_.emplace(tokenId, inputer);
39 sptr<IRemoteObject::DeathRecipient> dr(new (std::nothrow) ResPinauthInputerDeathRecipient(tokenId));
40 if (dr == nullptr || inputer->AsObject() == nullptr) {
41 IAM_LOGE("dr or inputer's object is nullptr");
42 } else {
43 pinAuthDeathMap_.emplace(tokenId, dr);
44 if (!inputer->AsObject()->AddDeathRecipient(dr)) {
45 IAM_LOGE("add death recipient fail");
46 }
47 }
48 IAM_LOGI("end");
49 return true;
50 }
51
UnRegisterInputer(uint32_t tokenId)52 void PinAuthManager::UnRegisterInputer(uint32_t tokenId)
53 {
54 std::lock_guard<std::mutex> guard(mutex_);
55 IAM_LOGI("start, tokenId = %{public}u", tokenId);
56 if (pinAuthInputerMap_.find(tokenId) != pinAuthInputerMap_.end()) {
57 if (pinAuthDeathMap_.find(tokenId) != pinAuthDeathMap_.end()) {
58 auto inputer = pinAuthInputerMap_[tokenId];
59 if (inputer == nullptr || inputer->AsObject() == nullptr) {
60 IAM_LOGE("inputer or inputer's object is nullptr");
61 } else if (!inputer->AsObject()->RemoveDeathRecipient(pinAuthDeathMap_[tokenId])) {
62 IAM_LOGE("remove death recipient fail");
63 }
64 pinAuthDeathMap_.erase(tokenId);
65 }
66 pinAuthInputerMap_.erase(tokenId);
67 IAM_LOGE("pinAuthInputerMap_ erase success");
68 }
69 IAM_LOGI("end");
70 }
71
GetInputerLock(uint32_t tokenId)72 sptr<InputerGetData> PinAuthManager::GetInputerLock(uint32_t tokenId)
73 {
74 std::lock_guard<std::mutex> guard(mutex_);
75 IAM_LOGI("start");
76 auto pinAuthInputer = pinAuthInputerMap_.find(tokenId);
77 if (pinAuthInputer != pinAuthInputerMap_.end()) {
78 IAM_LOGI("find pinAuthInputer");
79 return pinAuthInputer->second;
80 } else {
81 IAM_LOGE("pinAuthInputer is not found");
82 }
83 return nullptr;
84 }
85
ResPinauthInputerDeathRecipient(uint32_t tokenId)86 PinAuthManager::ResPinauthInputerDeathRecipient::ResPinauthInputerDeathRecipient(uint32_t tokenId)
87 : tokenId_(tokenId) {}
88
OnRemoteDied(const wptr<IRemoteObject> & remote)89 void PinAuthManager::ResPinauthInputerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
90 {
91 IAM_LOGI("start");
92 if (remote == nullptr) {
93 IAM_LOGE("remote is nullptr");
94 return;
95 }
96 PinAuthManager::GetInstance().UnRegisterInputer(tokenId_);
97 }
98 } // namespace PinAuth
99 } // namespace UserIam
100 } // namespace OHOS
101