• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     {
32         std::lock_guard<std::mutex> autoLock(eventMapLock_);
33         for (auto iter = eventMap_.begin(); iter != eventMap_.end(); iter++) {
34             auto listener = iter->second;
35             if (listener != nullptr) {
36                 napi_delete_reference(env_, listener->handlerRef);
37             }
38         }
39         eventMap_.clear();
40     }
41     napi_delete_reference(env_, thisVarRef_);
42 }
43 
On(std::string & eventType,napi_value handler)44 void DmNativeEvent::On(std::string &eventType, napi_value handler)
45 {
46     LOGI("DmNativeEvent On in for event: %{public}s", eventType.c_str());
47     std::lock_guard<std::mutex> autoLock(eventMapLock_);
48     auto listener = std::make_shared<DmEventListener>();
49     listener->eventType = eventType;
50     napi_create_reference(env_, handler, 1, &listener->handlerRef);
51     eventMap_[eventType] = listener;
52 }
53 
Off(std::string & eventType)54 void DmNativeEvent::Off(std::string &eventType)
55 {
56     LOGI("DmNativeEvent Off in for event: %{public}s", eventType.c_str());
57     napi_handle_scope scope = nullptr;
58     napi_status status = napi_open_handle_scope(env_, &scope);
59     if (status != napi_ok || scope == nullptr) {
60         LOGE("open handle scope failed");
61         return;
62     }
63 
64     std::lock_guard<std::mutex> autoLock(eventMapLock_);
65     auto iter = eventMap_.find(eventType);
66     if (iter == eventMap_.end()) {
67         LOGE("eventType %{public}s not find", eventType.c_str());
68         napi_close_handle_scope(env_, scope);
69         return;
70     }
71     auto listener = iter->second;
72     napi_delete_reference(env_, listener->handlerRef);
73     eventMap_.erase(eventType);
74     napi_close_handle_scope(env_, scope);
75 }
76 
OnEvent(const std::string & eventType,size_t argc,const napi_value * argv)77 void DmNativeEvent::OnEvent(const std::string &eventType, size_t argc, const napi_value *argv)
78 {
79     LOGI("OnEvent for %{public}s", eventType.c_str());
80     napi_handle_scope scope = nullptr;
81     napi_status status = napi_open_handle_scope(env_, &scope);
82     if (status != napi_ok || scope == nullptr) {
83         LOGE("open handle scope failed");
84         return;
85     }
86 
87     std::lock_guard<std::mutex> autoLock(eventMapLock_);
88     auto iter = eventMap_.find(eventType);
89     if (iter == eventMap_.end()) {
90         LOGE("eventType %{public}s not find", eventType.c_str());
91         napi_close_handle_scope(env_, scope);
92         return;
93     }
94     auto listener = iter->second;
95     napi_value thisVar = nullptr;
96     status = napi_get_reference_value(env_, thisVarRef_, &thisVar);
97     if (status != napi_ok) {
98         LOGE("napi_get_reference_value thisVar for %{public}s failed, status = %{public}d", eventType.c_str(), status);
99         napi_close_handle_scope(env_, scope);
100         return;
101     }
102 
103     napi_value handler = nullptr;
104     status = napi_get_reference_value(env_, listener->handlerRef, &handler);
105     if (status != napi_ok) {
106         LOGE("napi_get_reference_value handler for %{public}s failed, status = %{public}d", eventType.c_str(), status);
107         napi_close_handle_scope(env_, scope);
108         return;
109     }
110 
111     napi_value callResult = nullptr;
112     status = napi_call_function(env_, thisVar, handler, argc, argv, &callResult);
113     if (status != napi_ok) {
114         LOGE("napi_call_function for %{public}s failed, status = %{public}d", eventType.c_str(), status);
115         napi_close_handle_scope(env_, scope);
116         return;
117     }
118     napi_close_handle_scope(env_, scope);
119 }
120