1 /*
2  * Copyright (c) 2022-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 "dm_native_event.h"
17 
18 #include "dm_log.h"
19 
20 using namespace OHOS::DistributedHardware;
21 
DmNativeEvent(napi_env env,napi_value thisVar)22 DmNativeEvent::DmNativeEvent(napi_env env, napi_value thisVar)
23 {
24     env_ = env;
25     thisVarRef_ = nullptr;
26     napi_create_reference(env, thisVar, 1, &thisVarRef_);
27 }
28 
~DmNativeEvent()29 DmNativeEvent::~DmNativeEvent()
30 {
31     for (auto iter = eventMap_.begin(); iter != eventMap_.end(); iter++) {
32         auto listener = iter->second;
33         napi_delete_reference(env_, listener->handlerRef);
34     }
35     eventMap_.clear();
36     napi_delete_reference(env_, thisVarRef_);
37 }
38 
On(std::string & eventType,napi_value handler)39 void DmNativeEvent::On(std::string &eventType, napi_value handler)
40 {
41     LOGI("DmNativeEvent On in for event: %{public}s", eventType.c_str());
42     std::lock_guard<std::mutex> autoLock(lock_);
43     auto listener = std::make_shared<DmEventListener>();
44     listener->eventType = eventType;
45     napi_create_reference(env_, handler, 1, &listener->handlerRef);
46     eventMap_[eventType] = listener;
47 }
48 
Off(std::string & eventType)49 void DmNativeEvent::Off(std::string &eventType)
50 {
51     LOGI("DmNativeEvent Off in for event: %{public}s", eventType.c_str());
52     napi_handle_scope scope = nullptr;
53     napi_open_handle_scope(env_, &scope);
54     if (scope == nullptr) {
55         LOGE("scope is nullptr");
56         return;
57     }
58 
59     std::lock_guard<std::mutex> autoLock(lock_);
60     auto iter = eventMap_.find(eventType);
61     if (iter == eventMap_.end()) {
62         LOGE("eventType %{public}s not find", eventType.c_str());
63         napi_close_handle_scope(env_, scope);
64         return;
65     }
66     auto listener = iter->second;
67     napi_delete_reference(env_, listener->handlerRef);
68     eventMap_.erase(eventType);
69     napi_close_handle_scope(env_, scope);
70 }
71 
OnEvent(const std::string & eventType,size_t argc,const napi_value * argv)72 void DmNativeEvent::OnEvent(const std::string &eventType, size_t argc, const napi_value *argv)
73 {
74     LOGI("OnEvent for %{public}s", eventType.c_str());
75     napi_handle_scope scope = nullptr;
76     napi_open_handle_scope(env_, &scope);
77     if (scope == nullptr) {
78         LOGE("scope is nullptr");
79         return;
80     }
81 
82     std::lock_guard<std::mutex> autoLock(lock_);
83     auto iter = eventMap_.find(eventType);
84     if (iter == eventMap_.end()) {
85         LOGE("eventType %{public}s not find", eventType.c_str());
86         napi_close_handle_scope(env_, scope);
87         return;
88     }
89     auto listener = iter->second;
90     napi_value thisVar = nullptr;
91     napi_status status = napi_get_reference_value(env_, thisVarRef_, &thisVar);
92     if (status != napi_ok) {
93         LOGE("napi_get_reference_value thisVar for %{public}s failed, status = %{public}d", eventType.c_str(), status);
94         napi_close_handle_scope(env_, scope);
95         return;
96     }
97 
98     napi_value handler = nullptr;
99     status = napi_get_reference_value(env_, listener->handlerRef, &handler);
100     if (status != napi_ok) {
101         LOGE("napi_get_reference_value handler for %{public}s failed, status = %{public}d", eventType.c_str(), status);
102         napi_close_handle_scope(env_, scope);
103         return;
104     }
105 
106     napi_value callResult = nullptr;
107     status = napi_call_function(env_, thisVar, handler, argc, argv, &callResult);
108     if (status != napi_ok) {
109         LOGE("napi_call_function for %{public}s failed, status = %{public}d", eventType.c_str(), status);
110         napi_close_handle_scope(env_, scope);
111         return;
112     }
113     napi_close_handle_scope(env_, scope);
114 }
115