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