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 "devicestatus_event.h"
17
18 #include <js_native_api.h>
19 #include <map>
20 #include <uv.h>
21
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24
25 #include "devicestatus_define.h"
26
27 #undef LOG_TAG
28 #define LOG_TAG "DeviceStatusEvent"
29
30 namespace OHOS {
31 namespace Msdp {
32 namespace DeviceStatus {
33 namespace {
34 constexpr size_t EVENT_MAP_MAX { 20 };
35 constexpr size_t EVENT_LIST_MAX { 30 };
36 } // namespace
37
DeviceStatusEvent(napi_env env)38 DeviceStatusEvent::DeviceStatusEvent(napi_env env)
39 {
40 env_ = env;
41 }
42
~DeviceStatusEvent()43 DeviceStatusEvent::~DeviceStatusEvent()
44 {
45 eventOnces_.clear();
46 events_.clear();
47 }
48
On(int32_t eventType,napi_value handler,bool isOnce)49 bool DeviceStatusEvent::On(int32_t eventType, napi_value handler, bool isOnce)
50 {
51 FI_HILOGD("On for event:%{public}d, isOnce:%{public}d", eventType, isOnce);
52 std::lock_guard<std::mutex> guard(mutex_);
53 if ((events_.size() > EVENT_MAP_MAX) || (eventOnces_.size() > EVENT_MAP_MAX)) {
54 FI_HILOGE("events_ or eventOnces_ size over");
55 return false;
56 }
57 if (events_[eventType].size() > EVENT_LIST_MAX || eventOnces_[eventType].size() > EVENT_LIST_MAX) {
58 FI_HILOGE("list size over");
59 return false;
60 }
61 if (isOnce) {
62 if (!SaveCallbackByEvent(eventType, handler, isOnce, eventOnces_)) {
63 FI_HILOGE("Failed to save eventOnces_ callback");
64 return false;
65 }
66 } else {
67 if (!SaveCallbackByEvent(eventType, handler, isOnce, events_)) {
68 FI_HILOGE("Failed to save events_ callback");
69 return false;
70 }
71 }
72 return true;
73 }
74
SaveCallbackByEvent(int32_t eventType,napi_value handler,bool isOnce,std::map<int32_t,std::list<std::shared_ptr<DeviceStatusEventListener>>> events)75 bool DeviceStatusEvent::SaveCallbackByEvent(int32_t eventType, napi_value handler, bool isOnce,
76 std::map<int32_t, std::list<std::shared_ptr<DeviceStatusEventListener>>> events)
77 {
78 CALL_DEBUG_ENTER;
79 napi_ref onHandlerRef = nullptr;
80 napi_status status = napi_create_reference(env_, handler, 1, &onHandlerRef);
81 if (status != napi_ok) {
82 FI_HILOGE("Failed to napi_create_reference");
83 return false;
84 }
85 auto iter = events.find(eventType);
86 if (iter == events.end()) {
87 FI_HILOGE("eventType:%{public}d not exists", eventType);
88 events[eventType] = std::list<std::shared_ptr<DeviceStatusEventListener>>();
89 }
90 if (events[eventType].empty()) {
91 FI_HILOGE("events save callback");
92 SaveCallback(eventType, onHandlerRef, isOnce);
93 return true;
94 }
95 if (!IsNoExistCallback(events[eventType], handler, eventType)) {
96 FI_HILOGE("Callback already exists");
97 return false;
98 }
99 SaveCallback(eventType, onHandlerRef, isOnce);
100 return true;
101 }
102
IsNoExistCallback(std::list<std::shared_ptr<DeviceStatusEventListener>>,napi_value handler,int32_t eventType)103 bool DeviceStatusEvent::IsNoExistCallback(std::list<std::shared_ptr<DeviceStatusEventListener>>,
104 napi_value handler, int32_t eventType)
105 {
106 CALL_DEBUG_ENTER;
107 napi_value result = nullptr;
108 bool equal = false;
109 for (const auto &item : events_[eventType]) {
110 napi_status napiStatus = napi_get_reference_value(env_, item->onHandlerRef, &result);
111 if (napiStatus != napi_ok) {
112 FI_HILOGE("Failed to napi_get_reference_value");
113 return false;
114 }
115 napiStatus = napi_strict_equals(env_, result, handler, &equal);
116 if (napiStatus != napi_ok) {
117 FI_HILOGE("Failed to napi_strict_equals");
118 return false;
119 }
120 if (equal) {
121 FI_HILOGE("Map callback is exist");
122 return false;
123 }
124 }
125 return true;
126 }
127
SaveCallback(int32_t eventType,napi_ref onHandlerRef,bool isOnce)128 void DeviceStatusEvent::SaveCallback(int32_t eventType, napi_ref onHandlerRef, bool isOnce)
129 {
130 auto listener = std::make_shared<DeviceStatusEventListener>();
131 listener->onHandlerRef = onHandlerRef;
132 if (isOnce) {
133 eventOnces_[eventType].push_back(listener);
134 } else {
135 events_[eventType].push_back(listener);
136 }
137 FI_HILOGD("Add handler to list %{public}d", eventType);
138 }
139
Off(int32_t eventType,napi_value handler)140 bool DeviceStatusEvent::Off(int32_t eventType, napi_value handler)
141 {
142 FI_HILOGD("Unregister handler of event(%{public}d)", eventType);
143 std::lock_guard<std::mutex> guard(mutex_);
144 return RemoveAllCallback(eventType);
145 }
146
OffOnce(int32_t eventType,napi_value handler)147 bool DeviceStatusEvent::OffOnce(int32_t eventType, napi_value handler)
148 {
149 FI_HILOGD("DeviceStatusEvent OffOnce in for event:%{public}d", eventType);
150 auto iter = eventOnces_.find(eventType);
151 if (iter == eventOnces_.end()) {
152 FI_HILOGE("eventType %{public}d not found", eventType);
153 return false;
154 }
155 bool equal = false;
156 napi_value result = nullptr;
157 for (const auto &listener : eventOnces_[eventType]) {
158 napi_get_reference_value(env_, listener->onHandlerRef, &result);
159 napi_strict_equals(env_, result, handler, &equal);
160 if (equal) {
161 FI_HILOGI("Delete once handler from list %{public}d", eventType);
162 napi_status status = napi_delete_reference(env_, listener->onHandlerRef);
163 if (status != napi_ok) {
164 FI_HILOGW("Failed to napi_delete_reference");
165 }
166 eventOnces_[eventType].remove(listener);
167 break;
168 }
169 }
170 FI_HILOGI("%{public}zu listeners in the once list of %{public}d",
171 eventOnces_[eventType].size(), eventType);
172 return events_[eventType].empty();
173 }
174
RemoveAllCallback(int32_t eventType)175 bool DeviceStatusEvent::RemoveAllCallback(int32_t eventType)
176 {
177 CALL_DEBUG_ENTER;
178 auto iter = events_.find(eventType);
179 if (iter == events_.end()) {
180 FI_HILOGE("evenType %{public}d not found", eventType);
181 return false;
182 }
183 events_.erase(eventType);
184 return true;
185 }
186
CheckRet(int32_t eventType,size_t argc,int32_t value,std::shared_ptr<DeviceStatusEventListener> & typeHandler)187 void DeviceStatusEvent::CheckRet(int32_t eventType, size_t argc, int32_t value,
188 std::shared_ptr<DeviceStatusEventListener> &typeHandler)
189 {
190 CHKPV(typeHandler);
191 napi_value handler = nullptr;
192 napi_status status = napi_ok;
193 status = napi_get_reference_value(env_, typeHandler->onHandlerRef, &handler);
194 if (status != napi_ok) {
195 FI_HILOGE("OnEvent handler for %{public}d failed, status:%{public}d", eventType, status);
196 return;
197 }
198 napi_value result = nullptr;
199 SendRet(eventType, value, result);
200 napi_value callResult = nullptr;
201 FI_HILOGD("Report to hap");
202 status = napi_call_function(env_, nullptr, handler, argc, &result, &callResult);
203 if (status != napi_ok) {
204 FI_HILOGE("CheckRet:napi_call_function for %{public}d failed, status:%{public}d", eventType, status);
205 return;
206 }
207 }
208
SendRet(int32_t eventType,int32_t value,napi_value & result)209 void DeviceStatusEvent::SendRet(int32_t eventType, int32_t value, napi_value &result)
210 {
211 napi_status status = napi_create_object(env_, &result);
212 if (status != napi_ok) {
213 FI_HILOGE("Failed to create object");
214 return;
215 }
216 napi_value tmpValue = nullptr;
217 status = napi_create_int32(env_, eventType, &tmpValue);
218 if (status != napi_ok) {
219 FI_HILOGE("Failed to create object");
220 return;
221 }
222 status = napi_set_named_property(env_, result, "type", tmpValue);
223 if (status != napi_ok) {
224 FI_HILOGE("Failed to set name");
225 return;
226 }
227
228 status = napi_create_int32(env_, value, &tmpValue);
229 if (status != napi_ok) {
230 FI_HILOGE("Failed to create value");
231 return;
232 }
233 status = napi_set_named_property(env_, result, "value", tmpValue);
234 if (status != napi_ok) {
235 FI_HILOGE("Failed to set_named");
236 return;
237 }
238 }
239
OnEvent(int32_t eventType,size_t argc,int32_t value,bool isOnce)240 void DeviceStatusEvent::OnEvent(int32_t eventType, size_t argc, int32_t value, bool isOnce)
241 {
242 CALL_DEBUG_ENTER;
243 FI_HILOGD("OnEvent for %{public}d, isOnce:%{public}d", eventType, isOnce);
244 std::map<int32_t, std::list<std::shared_ptr<DeviceStatusEventListener>>>::iterator typeHandler;
245 if (isOnce) {
246 typeHandler = eventOnces_.find(eventType);
247 if (typeHandler == eventOnces_.end()) {
248 FI_HILOGE("OnEvent eventType %{public}d not found", eventType);
249 return;
250 }
251 } else {
252 typeHandler = events_.find(eventType);
253 if (typeHandler == events_.end()) {
254 FI_HILOGE("OnEvent eventType %{public}d not found", eventType);
255 return;
256 }
257 }
258 FI_HILOGD("%{public}zu callbacks of eventType %{public}d are sent",
259 typeHandler->second.size(), eventType);
260 for (auto handler : typeHandler->second) {
261 CheckRet(eventType, argc, value, handler);
262 }
263 }
264
ClearEventMap()265 void DeviceStatusEvent::ClearEventMap()
266 {
267 for (const auto &iter : events_) {
268 for (const auto &eventListener : iter.second) {
269 napi_status status = napi_delete_reference(env_, eventListener->onHandlerRef);
270 if (status != napi_ok) {
271 FI_HILOGW("Failed to napi_delete_reference");
272 }
273 }
274 }
275 for (const auto &iter : eventOnces_) {
276 for (const auto &eventListener : iter.second) {
277 napi_status status = napi_delete_reference(env_, eventListener->onHandlerRef);
278 if (status != napi_ok) {
279 FI_HILOGW("Failed to napi_delete_reference");
280 napi_delete_reference(env_, eventListener->onHandlerRef);
281 }
282 }
283 }
284 events_.clear();
285 eventOnces_.clear();
286 }
287 } // namespace DeviceStatus
288 } // namespace Msdp
289 } // namespace OHOS
290