1 /*
2 * Copyright (c) 2024 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 #include "context_death_recipient.h"
16
17 #include <sstream>
18
19 #include "context_pool.h"
20 #include "iam_check.h"
21 #include "iam_logger.h"
22 #include "iam_para2str.h"
23
24 #define LOG_TAG "USER_AUTH_SA"
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
AddDeathRecipient(std::shared_ptr<ContextCallback> & callback,uint64_t contextId)28 void ContextDeathRecipientManager::AddDeathRecipient(std::shared_ptr<ContextCallback> &callback, uint64_t contextId)
29 {
30 IAM_LOGI("start");
31 IF_FALSE_LOGE_AND_RETURN(callback != nullptr);
32
33 const sptr<IamCallbackInterface> iamCallback = callback->GetIamCallback();
34 if (iamCallback == nullptr) {
35 IAM_LOGE("callback_ is nullptr");
36 return;
37 }
38 auto obj = iamCallback->AsObject();
39 if (obj == nullptr) {
40 IAM_LOGE("remote object is nullptr");
41 return;
42 }
43
44 sptr<IRemoteObject::DeathRecipient> dr(new (std::nothrow) ContextDeathRecipient(contextId));
45 if ((dr == nullptr) || (!obj->AddDeathRecipient(dr))) {
46 IAM_LOGE("AddDeathRecipient failed");
47 return;
48 }
49
50 deathRecipient_ = dr;
51 IAM_LOGI("AddDeathRecipient success, contextId:****%{public}hx", static_cast<uint16_t>(contextId));
52 return;
53 }
54
RemoveDeathRecipient(std::shared_ptr<ContextCallback> & callback)55 void ContextDeathRecipientManager::RemoveDeathRecipient(std::shared_ptr<ContextCallback> &callback)
56 {
57 IAM_LOGI("start");
58 IF_FALSE_LOGE_AND_RETURN(callback != nullptr);
59
60 if (deathRecipient_ == nullptr) {
61 IAM_LOGE("deathRecipient_ is nullptr");
62 return;
63 }
64
65 const sptr<IamCallbackInterface> iamCallback = callback->GetIamCallback();
66 if (iamCallback == nullptr) {
67 IAM_LOGE("callback_ is nullptr");
68 return;
69 }
70
71 auto obj = iamCallback->AsObject();
72 if (obj == nullptr) {
73 IAM_LOGE("remote object is nullptr");
74 return;
75 }
76
77 obj->RemoveDeathRecipient(deathRecipient_);
78 deathRecipient_ = nullptr;
79 IAM_LOGI("RemoveDeathRecipient success");
80 return;
81 }
82
ContextDeathRecipient(uint64_t contextId)83 ContextDeathRecipient::ContextDeathRecipient(uint64_t contextId)
84 : contextId_(contextId)
85 {
86 IAM_LOGI("start");
87 }
88
OnRemoteDied(const wptr<IRemoteObject> & remote)89 void ContextDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
90 {
91 IAM_LOGI("start, contextId: ****%{public}hx", static_cast<uint16_t>(contextId_));
92 if (remote == nullptr) {
93 IAM_LOGE("remote is nullptr");
94 return;
95 }
96
97 auto context = ContextPool::Instance().Select(contextId_).lock();
98 if (context == nullptr) {
99 IAM_LOGE("context is nullptr");
100 return;
101 }
102
103 if (!context->Stop()) {
104 IAM_LOGE("failed to cancel enroll or auth");
105 return;
106 }
107 return;
108 }
109 } // namespace UserAuth
110 } // namespace UserIam
111 } // namespace OHOS
112