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