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
16 #include "modal_callback_service.h"
17
18 #include "callback_manager.h"
19 #include "iam_logger.h"
20 #include "iam_ptr.h"
21
22 #define LOG_TAG "USER_AUTH_SDK"
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
ModalCallbackService(const std::shared_ptr<UserAuthModalClientCallback> & impl)27 ModalCallbackService::ModalCallbackService(const std::shared_ptr<UserAuthModalClientCallback> &impl)
28 : modalCallback_(impl),
29 iamHitraceHelper_(Common::MakeShared<UserIam::UserAuth::IamHitraceHelper>("UserAuthWidget"))
30 {
31 IAM_LOGI("set modal callback");
32 CallbackManager::CallbackAction action = [impl]() {
33 if (impl != nullptr) {
34 IAM_LOGI("user auth service death, auth widget callback return default result to caller");
35 uint64_t contextId = 0;
36 std::string cmdData = "";
37 impl->SendCommand(contextId, cmdData);
38 }
39 };
40 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
41 }
42
~ModalCallbackService()43 ModalCallbackService::~ModalCallbackService()
44 {
45 IAM_LOGI("~ModalCallbackService clean");
46 iamHitraceHelper_= nullptr;
47 CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
48 }
49
SendCommand(uint64_t contextId,const std::string & cmdData)50 void ModalCallbackService::SendCommand(uint64_t contextId, const std::string &cmdData)
51 {
52 IAM_LOGI("SendCommand start");
53 if (modalCallback_ == nullptr) {
54 IAM_LOGE("modal callback is nullptr");
55 return;
56 }
57 modalCallback_->SendCommand(contextId, cmdData);
58 iamHitraceHelper_ = nullptr;
59 }
60 } // namespace UserAuth
61 } // namespace UserIam
62 } // namespace OHOS