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 #include "screenlock_system_ability_callback.h"
16 
17 #include <memory>
18 #include <new>
19 
20 #include "js_native_api.h"
21 #include "js_native_api_types.h"
22 #include "node_api.h"
23 #include "sclock_log.h"
24 #include "screenlock_common.h"
25 #include "uv_queue.h"
26 
27 namespace OHOS {
28 namespace ScreenLock {
29 std::mutex ScreenlockSystemAbilityCallback::eventHandlerMutex_;
30 std::shared_ptr<AppExecFwk::EventHandler> ScreenlockSystemAbilityCallback::handler_{ nullptr };
ScreenlockSystemAbilityCallback(const EventListener & eventListener)31 ScreenlockSystemAbilityCallback::ScreenlockSystemAbilityCallback(const EventListener &eventListener)
32     : eventListener_(eventListener)
33 {
34 }
35 
~ScreenlockSystemAbilityCallback()36 ScreenlockSystemAbilityCallback::~ScreenlockSystemAbilityCallback()
37 {
38 }
39 
OnCallBack(const SystemEvent & systemEvent)40 void ScreenlockSystemAbilityCallback::OnCallBack(const SystemEvent &systemEvent)
41 {
42     if (handler_ == nullptr) {
43         SCLOCK_HILOGE("eventHandler is nullptr");
44         return;
45     }
46     auto entry = std::make_shared<ScreenlockOnCallBack>();
47     entry->env = eventListener_.env;
48     entry->callbackRef = eventListener_.callbackRef;
49     entry->systemEvent = systemEvent;
50     auto task = [entry]() {
51         napi_handle_scope scope = nullptr;
52         napi_open_handle_scope(entry->env, &scope);
53         napi_value callbackFunc = nullptr;
54         napi_get_reference_value(entry->env, entry->callbackRef, &callbackFunc);
55         napi_value result = nullptr;
56         napi_create_object(entry->env, &result);
57         napi_value eventType = nullptr;
58         napi_value params = nullptr;
59         napi_create_string_utf8(entry->env, entry->systemEvent.eventType_.c_str(), NAPI_AUTO_LENGTH, &eventType);
60         napi_create_string_utf8(entry->env, entry->systemEvent.params_.c_str(), NAPI_AUTO_LENGTH, &params);
61         napi_set_named_property(entry->env, result, "eventType", eventType);
62         napi_set_named_property(entry->env, result, "params", params);
63         napi_value output = nullptr;
64         napi_call_function(entry->env, nullptr, callbackFunc, ARGS_SIZE_ONE, &result, &output);
65         SCLOCK_HILOGI("OnCallBack eventType:%{public}s", entry->systemEvent.eventType_.c_str());
66         napi_close_handle_scope(entry->env, scope);
67     };
68     handler_->PostTask(task, "ScreenlockSystemAbilityCallback");
69 }
70 
GetEventHandler()71 std::shared_ptr<AppExecFwk::EventHandler> ScreenlockSystemAbilityCallback::GetEventHandler()
72 {
73     std::lock_guard<std::mutex> lock(eventHandlerMutex_);
74     if (handler_ == nullptr) {
75         handler_ = AppExecFwk::EventHandler::Current();
76     }
77     return handler_;
78 }
79 } // namespace ScreenLock
80 } // namespace OHOS
81