1 /*
2  * Copyright (c) 2023 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 "widget_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 {
WidgetCallbackService(const std::shared_ptr<IUserAuthWidgetCallback> & impl)27 WidgetCallbackService::WidgetCallbackService(const std::shared_ptr<IUserAuthWidgetCallback> &impl)
28     : widgetCallback_(impl),
29     iamHitraceHelper_(Common::MakeShared<UserIam::UserAuth::IamHitraceHelper>("UserAuthWidget"))
30 {
31     CallbackManager::CallbackAction action = [impl]() {
32         if (impl != nullptr) {
33             IAM_LOGI("user auth service death, auth widget callback return default result to caller");
34             std::string command = "";
35             impl->SendCommand(command);
36         }
37     };
38     CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
39 }
40 
~WidgetCallbackService()41 WidgetCallbackService::~WidgetCallbackService()
42 {
43     CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
44 }
45 
SendCommand(const std::string & cmdData)46 void WidgetCallbackService::SendCommand(const std::string &cmdData)
47 {
48     IAM_LOGI("start");
49     if (widgetCallback_ == nullptr) {
50         IAM_LOGE("both widget callback is nullptr");
51         return;
52     }
53     widgetCallback_->SendCommand(cmdData);
54     iamHitraceHelper_= nullptr;
55 }
56 } // namespace UserAuth
57 } // namespace UserIam
58 } // namespace OHOS