• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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: %s", eventType.c_str());
42     auto listener = std::make_shared<DmEventListener>();
43     listener->eventType = eventType;
44     napi_create_reference(env_, handler, 1, &listener->handlerRef);
45     eventMap_[eventType] = listener;
46 }
47 
Off(std::string & eventType)48 void DmNativeEvent::Off(std::string &eventType)
49 {
50     LOGI("DmNativeEvent Off in for event: %s", eventType.c_str());
51     napi_handle_scope scope = nullptr;
52     napi_open_handle_scope(env_, &scope);
53     if (scope == nullptr) {
54         LOGE("scope is nullptr");
55         return;
56     }
57 
58     auto iter = eventMap_.find(eventType);
59     if (iter == eventMap_.end()) {
60         LOGE("eventType %s not find", eventType.c_str());
61         return;
62     }
63     auto listener = iter->second;
64     napi_delete_reference(env_, listener->handlerRef);
65     eventMap_.erase(eventType);
66     napi_close_handle_scope(env_, scope);
67 }
68 
OnEvent(const std::string & eventType,size_t argc,const napi_value * argv)69 void DmNativeEvent::OnEvent(const std::string &eventType, size_t argc, const napi_value *argv)
70 {
71     LOGI("OnEvent for %s", eventType.c_str());
72     napi_handle_scope scope = nullptr;
73     napi_open_handle_scope(env_, &scope);
74     if (scope == nullptr) {
75         LOGE("scope is nullptr");
76         return;
77     }
78 
79     auto iter = eventMap_.find(eventType);
80     if (iter == eventMap_.end()) {
81         LOGE("eventType %s not find", eventType.c_str());
82         return;
83     }
84     auto listener = iter->second;
85     napi_value thisVar = nullptr;
86     napi_status status = napi_get_reference_value(env_, thisVarRef_, &thisVar);
87     if (status != napi_ok) {
88         LOGE("napi_get_reference_value thisVar for %s failed, status=%d", eventType.c_str(), status);
89         return;
90     }
91 
92     napi_value handler = nullptr;
93     status = napi_get_reference_value(env_, listener->handlerRef, &handler);
94     if (status != napi_ok) {
95         LOGE("napi_get_reference_value handler for %s failed, status=%d", eventType.c_str(), status);
96         return;
97     }
98 
99     napi_value callResult = nullptr;
100     status = napi_call_function(env_, thisVar, handler, argc, argv, &callResult);
101     if (status != napi_ok) {
102         LOGE("napi_call_function for %s failed, status=%d", eventType.c_str(), status);
103         return;
104     }
105     napi_close_handle_scope(env_, scope);
106 }