1 /*
2 * Copyright (c) 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 "js_event_cooperate_target.h"
17
18 #include "devicestatus_define.h"
19 #include "devicestatus_errors.h"
20 #include "interaction_manager.h"
21 #include "napi_constants.h"
22 #include "util_napi_error.h"
23
24 #undef LOG_TAG
25 #define LOG_TAG "JsEventCooperateTarget"
26
27 namespace OHOS {
28 namespace Msdp {
29 namespace DeviceStatus {
30 namespace {
31 inline constexpr std::string_view COORDINATION { "cooperation" };
32 inline constexpr std::string_view CREATE_PROMISE { "napi_create_promise" };
33 inline constexpr std::string_view GET_UNDEFINED { "napi_get_undefined" };
34 inline constexpr std::string_view RESOLVE_DEFERRED { "napi_resolve_deferred" };
35 inline constexpr std::string_view REJECT_DEFERRED { "napi_reject_deferred" };
36 inline constexpr std::string_view CLOSE_SCOPE { "napi_close_handle_scope" };
37 std::mutex mutex_;
38 } // namespace
39
JsEventCooperateTarget()40 JsEventCooperateTarget::JsEventCooperateTarget()
41 {
42 CALL_DEBUG_ENTER;
43 auto ret = coordinationListeners_.insert({ COORDINATION,
44 std::vector<sptr<JsUtilCooperate::CallbackInfo>>()});
45 if (!ret.second) {
46 FI_HILOGW("Failed to add listener, errCode:%{public}d", static_cast<int32_t>(DeviceStatus::VAL_NOT_EXP));
47 }
48 }
49
EmitJsEnable(sptr<JsUtilCooperate::CallbackInfo> cb,const std::string & networkId,const CoordinationMsgInfo & msgInfo)50 void JsEventCooperateTarget::EmitJsEnable(sptr<JsUtilCooperate::CallbackInfo> cb,
51 const std::string &networkId, const CoordinationMsgInfo &msgInfo)
52 {
53 CALL_INFO_TRACE;
54 CHKPV(cb);
55 CHKPV(cb->env);
56 cb->data.enableResult = (msgInfo.msg == CoordinationMessage::PREPARE ||
57 msgInfo.msg == CoordinationMessage::UNPREPARE);
58 cb->data.msgInfo = msgInfo;
59
60 auto task = [cb]() {
61 FI_HILOGI("Execute lambda");
62 if (cb->ref == nullptr) {
63 CallEnablePromiseWork(cb);
64 } else {
65 CallEnableAsyncWork(cb);
66 }
67 };
68 if (napi_status::napi_ok != napi_send_event(cb->env, task, napi_eprio_immediate)) {
69 FI_HILOGE("Failed to SendEvent");
70 }
71 }
72
EmitJsStart(sptr<JsUtilCooperate::CallbackInfo> cb,const std::string & remoteNetworkId,const CoordinationMsgInfo & msgInfo)73 void JsEventCooperateTarget::EmitJsStart(sptr<JsUtilCooperate::CallbackInfo> cb,
74 const std::string &remoteNetworkId, const CoordinationMsgInfo &msgInfo)
75 {
76 CALL_INFO_TRACE;
77 CHKPV(cb);
78 CHKPV(cb->env);
79 cb->data.startResult = (msgInfo.msg == CoordinationMessage::ACTIVATE_SUCCESS);
80 cb->data.msgInfo = msgInfo;
81
82 auto task = [cb]() {
83 FI_HILOGI("Execute lambda");
84 if (cb->ref == nullptr) {
85 CallStartPromiseWork(cb);
86 } else {
87 CallStartAsyncWork(cb);
88 }
89 };
90 if (napi_status::napi_ok != napi_send_event(cb->env, task, napi_eprio_immediate)) {
91 FI_HILOGE("Failed to SendEvent");
92 }
93 }
94
EmitJsStop(sptr<JsUtilCooperate::CallbackInfo> cb,const std::string & networkId,const CoordinationMsgInfo & msgInfo)95 void JsEventCooperateTarget::EmitJsStop(sptr<JsUtilCooperate::CallbackInfo> cb,
96 const std::string &networkId, const CoordinationMsgInfo &msgInfo)
97 {
98 CALL_INFO_TRACE;
99 CHKPV(cb);
100 CHKPV(cb->env);
101 cb->data.stopResult = (msgInfo.msg == CoordinationMessage::DEACTIVATE_SUCCESS);
102 cb->data.msgInfo = msgInfo;
103
104 auto task = [cb]() {
105 FI_HILOGI("Execute lambda");
106 if (cb->ref == nullptr) {
107 CallStopPromiseWork(cb);
108 } else {
109 CallStopAsyncWork(cb);
110 }
111 };
112 if (napi_status::napi_ok != napi_send_event(cb->env, task, napi_eprio_immediate)) {
113 FI_HILOGE("Failed to SendEvent");
114 }
115 }
116
EmitJsGetState(sptr<JsUtilCooperate::CallbackInfo> cb,bool state)117 void JsEventCooperateTarget::EmitJsGetState(sptr<JsUtilCooperate::CallbackInfo> cb, bool state)
118 {
119 CALL_INFO_TRACE;
120 CHKPV(cb);
121 CHKPV(cb->env);
122 cb->data.coordinationOpened = state;
123
124 auto task = [cb]() {
125 FI_HILOGI("Execute lambda");
126 if (cb->ref == nullptr) {
127 CallGetStatePromiseWork(cb);
128 } else {
129 CallGetStateAsyncWork(cb);
130 }
131 };
132 if (napi_status::napi_ok != napi_send_event(cb->env, task, napi_eprio_immediate)) {
133 FI_HILOGE("Failed to SendEvent");
134 }
135 }
136
AddListener(napi_env env,const std::string & type,napi_value handle)137 void JsEventCooperateTarget::AddListener(napi_env env, const std::string &type, napi_value handle)
138 {
139 CALL_INFO_TRACE;
140 std::lock_guard<std::mutex> guard(mutex_);
141 auto iter = coordinationListeners_.find(type);
142 if (iter == coordinationListeners_.end()) {
143 FI_HILOGE("Failed to add listener, type:%{public}s", type.c_str());
144 return;
145 }
146
147 for (const auto &item : iter->second) {
148 CHKPC(item);
149 if (JsUtilCooperate::IsSameHandle(env, handle, item->ref)) {
150 FI_HILOGE("The handle already exists");
151 return;
152 }
153 }
154 napi_ref ref = nullptr;
155 CHKRV(napi_create_reference(env, handle, 1, &ref), CREATE_REFERENCE);
156 sptr<JsUtilCooperate::CallbackInfo> monitor = new (std::nothrow) JsUtilCooperate::CallbackInfo();
157 CHKPV(monitor);
158 monitor->env = env;
159 monitor->ref = ref;
160 iter->second.push_back(std::move(monitor));
161 if (!isListeningProcess_) {
162 isListeningProcess_ = true;
163 INTERACTION_MGR->RegisterCoordinationListener(shared_from_this());
164 }
165 }
166
RemoveListener(napi_env env,const std::string & type,napi_value handle)167 void JsEventCooperateTarget::RemoveListener(napi_env env, const std::string &type, napi_value handle)
168 {
169 CALL_INFO_TRACE;
170 std::lock_guard<std::mutex> guard(mutex_);
171 auto iter = coordinationListeners_.find(type);
172 if (iter == coordinationListeners_.end()) {
173 FI_HILOGE("Failed to remove listener, type:%{public}s", type.c_str());
174 return;
175 }
176 if (handle == nullptr) {
177 iter->second.clear();
178 goto monitorTag;
179 }
180 for (auto it = iter->second.begin(); it != iter->second.end(); ++it) {
181 if (JsUtilCooperate::IsSameHandle(env, handle, (*it)->ref)) {
182 FI_HILOGE("Successfully removed the listener");
183 iter->second.erase(it);
184 goto monitorTag;
185 }
186 }
187
188 monitorTag:
189 if (isListeningProcess_ && iter->second.empty()) {
190 isListeningProcess_ = false;
191 INTERACTION_MGR->UnregisterCoordinationListener(shared_from_this());
192 }
193 }
194
CreateCallbackInfo(napi_env env,napi_value handle,sptr<JsUtilCooperate::CallbackInfo> callback)195 napi_value JsEventCooperateTarget::CreateCallbackInfo(napi_env env,
196 napi_value handle, sptr<JsUtilCooperate::CallbackInfo> callback)
197 {
198 CALL_INFO_TRACE;
199 CHKPP(callback);
200 callback->env = env;
201 napi_value promise = nullptr;
202 if (handle == nullptr) {
203 CHKRP(napi_create_promise(env, &callback->deferred, &promise), CREATE_PROMISE);
204 } else {
205 CHKRP(napi_create_reference(env, handle, 1, &callback->ref), CREATE_REFERENCE);
206 }
207 return promise;
208 }
209
ResetEnv()210 void JsEventCooperateTarget::ResetEnv()
211 {
212 CALL_INFO_TRACE;
213 std::lock_guard<std::mutex> guard(mutex_);
214 INTERACTION_MGR->UnregisterCoordinationListener(shared_from_this());
215 }
216
OnCoordinationMessage(const std::string & networkId,CoordinationMessage msg)217 void JsEventCooperateTarget::OnCoordinationMessage(const std::string &networkId, CoordinationMessage msg)
218 {
219 CALL_INFO_TRACE;
220 std::lock_guard<std::mutex> guard(mutex_);
221 auto changeEvent = coordinationListeners_.find(COORDINATION);
222 if (changeEvent == coordinationListeners_.end()) {
223 FI_HILOGE("Failed to find the %{public}s", std::string(COORDINATION).c_str());
224 return;
225 }
226
227 for (auto &item : changeEvent->second) {
228 CHKPC(item);
229 CHKPC(item->env);
230 item->data.msgInfo.msg = msg;
231 item->data.deviceDescriptor = networkId;
232 item->IncStrongRef(nullptr);
233 auto task = [item]() {
234 FI_HILOGI("Execute lamdba");
235 EmitCoordinationMessageEvent(item);
236 };
237 if (napi_status::napi_ok != napi_send_event(item->env, task, napi_eprio_immediate)) {
238 FI_HILOGE("Failed to SendEvent");
239 }
240 }
241 }
242
CallEnablePromiseWork(sptr<JsUtilCooperate::CallbackInfo> cb)243 void JsEventCooperateTarget::CallEnablePromiseWork(sptr<JsUtilCooperate::CallbackInfo> cb)
244 {
245 CALL_INFO_TRACE;
246 if (cb == nullptr) {
247 FI_HILOGE("Enable promise, check data is nullptr");
248 return;
249 }
250 CHKPV(cb->env);
251 napi_handle_scope scope = nullptr;
252 napi_open_handle_scope(cb->env, &scope);
253 if (scope == nullptr) {
254 FI_HILOGE("Enable promise, scope is nullptr");
255 RELEASE_CALLBACKINFO(cb->env, cb->ref);
256 return;
257 }
258 napi_value object = JsUtilCooperate::GetEnableInfo(cb);
259 if (object == nullptr) {
260 FI_HILOGE("Enable promise, object is nullptr");
261 RELEASE_CALLBACKINFO(cb->env, cb->ref);
262 napi_close_handle_scope(cb->env, scope);
263 return;
264 }
265 napi_valuetype valueType = napi_undefined;
266 if (napi_typeof(cb->env, object, &valueType) != napi_ok) {
267 FI_HILOGE("Enable promise, napi typeof failed");
268 RELEASE_CALLBACKINFO(cb->env, cb->ref);
269 napi_close_handle_scope(cb->env, scope);
270 return;
271 }
272 if (valueType != napi_undefined) {
273 CHKRV_SCOPE(cb->env, napi_reject_deferred(cb->env, cb->deferred, object), REJECT_DEFERRED, scope);
274 } else {
275 CHKRV_SCOPE(cb->env, napi_resolve_deferred(cb->env, cb->deferred, object), RESOLVE_DEFERRED, scope);
276 }
277 RELEASE_CALLBACKINFO(cb->env, cb->ref);
278 napi_close_handle_scope(cb->env, scope);
279 }
280
CallEnableAsyncWork(sptr<JsUtilCooperate::CallbackInfo> cb)281 void JsEventCooperateTarget::CallEnableAsyncWork(sptr<JsUtilCooperate::CallbackInfo> cb)
282 {
283 CALL_INFO_TRACE;
284 if (cb == nullptr) {
285 FI_HILOGE("Enable async, check data is nullptr");
286 return;
287 }
288 CHKPV(cb->env);
289 napi_handle_scope scope = nullptr;
290 napi_open_handle_scope(cb->env, &scope);
291 if (scope == nullptr) {
292 FI_HILOGE("Enable async, scope is nullptr");
293 RELEASE_CALLBACKINFO(cb->env, cb->ref);
294 return;
295 }
296 napi_value object = JsUtilCooperate::GetEnableInfo(cb);
297 if (object == nullptr) {
298 FI_HILOGE("Enable async, object is nullptr");
299 RELEASE_CALLBACKINFO(cb->env, cb->ref);
300 napi_close_handle_scope(cb->env, scope);
301 return;
302 }
303 napi_value processor = nullptr;
304 CHKRV_SCOPE(cb->env, napi_get_reference_value(cb->env, cb->ref, &processor), GET_REFERENCE_VALUE, scope);
305 napi_value ret = nullptr;
306 CHKRV_SCOPE(cb->env, napi_call_function(cb->env, nullptr, processor, 1, &object, &ret), CALL_FUNCTION, scope);
307 RELEASE_CALLBACKINFO(cb->env, cb->ref);
308 napi_close_handle_scope(cb->env, scope);
309 }
310
CallStartPromiseWork(sptr<JsUtilCooperate::CallbackInfo> cb)311 void JsEventCooperateTarget::CallStartPromiseWork(sptr<JsUtilCooperate::CallbackInfo> cb)
312 {
313 CALL_INFO_TRACE;
314 if (cb == nullptr) {
315 FI_HILOGE("Start promise, check data is nullptr");
316 return;
317 }
318 CHKPV(cb->env);
319 napi_handle_scope scope = nullptr;
320 napi_open_handle_scope(cb->env, &scope);
321 if (scope == nullptr) {
322 FI_HILOGE("Start promise, scope is nullptr");
323 RELEASE_CALLBACKINFO(cb->env, cb->ref);
324 return;
325 }
326 napi_value object = JsUtilCooperate::GetStartInfo(cb);
327 if (object == nullptr) {
328 FI_HILOGE("Start promise, object is nullptr");
329 RELEASE_CALLBACKINFO(cb->env, cb->ref);
330 napi_close_handle_scope(cb->env, scope);
331 return;
332 }
333 napi_valuetype napiValueType = napi_undefined;
334 if (napi_typeof(cb->env, object, &napiValueType) != napi_ok) {
335 FI_HILOGE("Start promise, napi typeof failed");
336 RELEASE_CALLBACKINFO(cb->env, cb->ref);
337 napi_close_handle_scope(cb->env, scope);
338 return;
339 }
340 if (napiValueType != napi_undefined) {
341 CHKRV_SCOPE(cb->env, napi_reject_deferred(cb->env, cb->deferred, object), REJECT_DEFERRED, scope);
342 } else {
343 CHKRV_SCOPE(cb->env, napi_resolve_deferred(cb->env, cb->deferred, object), RESOLVE_DEFERRED, scope);
344 }
345 RELEASE_CALLBACKINFO(cb->env, cb->ref);
346 napi_close_handle_scope(cb->env, scope);
347 }
348
CallStartAsyncWork(sptr<JsUtilCooperate::CallbackInfo> cb)349 void JsEventCooperateTarget::CallStartAsyncWork(sptr<JsUtilCooperate::CallbackInfo> cb)
350 {
351 CALL_INFO_TRACE;
352 if (cb == nullptr) {
353 FI_HILOGE("Start async, check data is nullptr");
354 return;
355 }
356 CHKPV(cb->env);
357 napi_handle_scope scope = nullptr;
358 napi_open_handle_scope(cb->env, &scope);
359 if (scope == nullptr) {
360 FI_HILOGE("Start async, scope is nullptr");
361 RELEASE_CALLBACKINFO(cb->env, cb->ref);
362 return;
363 }
364 napi_value object = JsUtilCooperate::GetStartInfo(cb);
365 if (object == nullptr) {
366 FI_HILOGE("Start async, object is nullptr");
367 RELEASE_CALLBACKINFO(cb->env, cb->ref);
368 napi_close_handle_scope(cb->env, scope);
369 return;
370 }
371 napi_value napiHandler = nullptr;
372 CHKRV_SCOPE(cb->env, napi_get_reference_value(cb->env, cb->ref, &napiHandler), GET_REFERENCE_VALUE, scope);
373 napi_value ret = nullptr;
374 CHKRV_SCOPE(cb->env, napi_call_function(cb->env, nullptr, napiHandler, 1, &object, &ret), CALL_FUNCTION, scope);
375 RELEASE_CALLBACKINFO(cb->env, cb->ref);
376 napi_close_handle_scope(cb->env, scope);
377 }
378
CallStopPromiseWork(sptr<JsUtilCooperate::CallbackInfo> cb)379 void JsEventCooperateTarget::CallStopPromiseWork(sptr<JsUtilCooperate::CallbackInfo> cb)
380 {
381 CALL_INFO_TRACE;
382 if (cb == nullptr) {
383 FI_HILOGE("Stop promise, check data is nullptr");
384 return;
385 }
386 CHKPV(cb->env);
387 napi_handle_scope napiHandleScope = nullptr;
388 napi_open_handle_scope(cb->env, &napiHandleScope);
389 if (napiHandleScope == nullptr) {
390 FI_HILOGE("Stop promise, napiHandleScope is nullptr");
391 RELEASE_CALLBACKINFO(cb->env, cb->ref);
392 return;
393 }
394 napi_value object = JsUtilCooperate::GetStopInfo(cb);
395 if (object == nullptr) {
396 FI_HILOGE("Stop promise, object is nullptr");
397 RELEASE_CALLBACKINFO(cb->env, cb->ref);
398 napi_close_handle_scope(cb->env, napiHandleScope);
399 return;
400 }
401
402 napi_valuetype valueType = napi_undefined;
403 if (napi_typeof(cb->env, object, &valueType) != napi_ok) {
404 FI_HILOGE("Stop promise, napi typeof failed");
405 RELEASE_CALLBACKINFO(cb->env, cb->ref);
406 napi_close_handle_scope(cb->env, napiHandleScope);
407 return;
408 }
409 if (valueType != napi_undefined) {
410 CHKRV_SCOPE(cb->env, napi_reject_deferred(cb->env, cb->deferred, object), REJECT_DEFERRED, napiHandleScope);
411 } else {
412 CHKRV_SCOPE(cb->env, napi_resolve_deferred(cb->env, cb->deferred, object), RESOLVE_DEFERRED, napiHandleScope);
413 }
414 RELEASE_CALLBACKINFO(cb->env, cb->ref);
415 napi_close_handle_scope(cb->env, napiHandleScope);
416 }
417
CallStopAsyncWork(sptr<JsUtilCooperate::CallbackInfo> cb)418 void JsEventCooperateTarget::CallStopAsyncWork(sptr<JsUtilCooperate::CallbackInfo> cb)
419 {
420 CALL_INFO_TRACE;
421 if (cb == nullptr) {
422 FI_HILOGE("Stop async, check data is nullptr");
423 return;
424 }
425 CHKPV(cb->env);
426 napi_handle_scope scope = nullptr;
427 napi_open_handle_scope(cb->env, &scope);
428 if (scope == nullptr) {
429 FI_HILOGE("Stop async, scope is nullptr");
430 RELEASE_CALLBACKINFO(cb->env, cb->ref);
431 return;
432 }
433 napi_value object = JsUtilCooperate::GetStopInfo(cb);
434 if (object == nullptr) {
435 FI_HILOGE("Stop async, object is nullptr");
436 RELEASE_CALLBACKINFO(cb->env, cb->ref);
437 napi_close_handle_scope(cb->env, scope);
438 return;
439 }
440 napi_value napiHandler = nullptr;
441 CHKRV_SCOPE(cb->env, napi_get_reference_value(cb->env, cb->ref, &napiHandler), GET_REFERENCE_VALUE, scope);
442 napi_value result = nullptr;
443 CHKRV_SCOPE(cb->env, napi_call_function(cb->env, nullptr, napiHandler, 1, &object, &result), CALL_FUNCTION, scope);
444 RELEASE_CALLBACKINFO(cb->env, cb->ref);
445 napi_close_handle_scope(cb->env, scope);
446 }
447
CallGetStatePromiseWork(sptr<JsUtilCooperate::CallbackInfo> cb)448 void JsEventCooperateTarget::CallGetStatePromiseWork(sptr<JsUtilCooperate::CallbackInfo> cb)
449 {
450 CALL_INFO_TRACE;
451 if (cb == nullptr) {
452 FI_HILOGE("Get start promise, check data is nullptr");
453 return;
454 }
455 CHKPV(cb->env);
456 napi_handle_scope scope = nullptr;
457 napi_open_handle_scope(cb->env, &scope);
458 if (scope == nullptr) {
459 FI_HILOGE("Get start promise, scope is nullptr");
460 RELEASE_CALLBACKINFO(cb->env, cb->ref);
461 return;
462 }
463 napi_value object = JsUtilCooperate::GetStateInfo(cb);
464 if (object == nullptr) {
465 FI_HILOGE("Get start promise, object is nullptr");
466 RELEASE_CALLBACKINFO(cb->env, cb->ref);
467 napi_close_handle_scope(cb->env, scope);
468 return;
469 }
470 CHKRV_SCOPE(cb->env, napi_resolve_deferred(cb->env, cb->deferred, object), RESOLVE_DEFERRED, scope);
471 RELEASE_CALLBACKINFO(cb->env, cb->ref);
472 napi_close_handle_scope(cb->env, scope);
473 }
474
CallGetStateAsyncWork(sptr<JsUtilCooperate::CallbackInfo> cb)475 void JsEventCooperateTarget::CallGetStateAsyncWork(sptr<JsUtilCooperate::CallbackInfo> cb)
476 {
477 CALL_INFO_TRACE;
478 if (cb == nullptr) {
479 FI_HILOGE("Get start async, check data is nullptr");
480 return;
481 }
482 CHKPV(cb->env);
483 napi_handle_scope scope = nullptr;
484 napi_open_handle_scope(cb->env, &scope);
485 if (scope == nullptr) {
486 FI_HILOGE("Get start async, scope is nullptr");
487 RELEASE_CALLBACKINFO(cb->env, cb->ref);
488 return;
489 }
490 napi_value resultObj[2];
491 CHKRV_SCOPE(cb->env, napi_get_undefined(cb->env, &resultObj[0]), GET_UNDEFINED, scope);
492 resultObj[1] = JsUtilCooperate::GetStateInfo(cb);
493 if (resultObj[1] == nullptr) {
494 FI_HILOGE("Get start async, object is nullptr");
495 napi_close_handle_scope(cb->env, scope);
496 return;
497 }
498 napi_value handlerInfo = nullptr;
499 CHKRV_SCOPE(cb->env, napi_get_reference_value(cb->env, cb->ref, &handlerInfo), GET_REFERENCE_VALUE, scope);
500 napi_value ret = nullptr;
501 size_t argc = TWO_PARAM;
502 CHKRV_SCOPE(cb->env, napi_call_function(cb->env, nullptr, handlerInfo, argc, resultObj, &ret),
503 CALL_FUNCTION, scope);
504 RELEASE_CALLBACKINFO(cb->env, cb->ref);
505 napi_close_handle_scope(cb->env, scope);
506 }
507
EmitCoordinationMessageEvent(sptr<JsUtilCooperate::CallbackInfo> cb)508 void JsEventCooperateTarget::EmitCoordinationMessageEvent(sptr<JsUtilCooperate::CallbackInfo> cb)
509 {
510 CALL_INFO_TRACE;
511 if (cb == nullptr) {
512 FI_HILOGE("The data is nullptr");
513 return;
514 }
515 std::lock_guard<std::mutex> guard(mutex_);
516 auto msgEvent = coordinationListeners_.find(COORDINATION);
517 if (msgEvent == coordinationListeners_.end()) {
518 FI_HILOGE("Failed to find the msgEvent");
519 return;
520 }
521 for (const auto &item : msgEvent->second) {
522 CHKPC(item->env);
523 if (item->ref != cb->ref) {
524 continue;
525 }
526 napi_handle_scope scope = nullptr;
527 napi_open_handle_scope(item->env, &scope);
528 napi_value deviceDescriptor = nullptr;
529 CHKRV_SCOPE(item->env, napi_create_string_utf8(item->env, item->data.deviceDescriptor.c_str(),
530 NAPI_AUTO_LENGTH, &deviceDescriptor), CREATE_STRING_UTF8, scope);
531 napi_value eventMsg = nullptr;
532 auto iter = messageTransform.find(item->data.msgInfo.msg);
533 if (iter == messageTransform.end()) {
534 FI_HILOGE("Failed to find the message code");
535 CHKRV(napi_close_handle_scope(item->env, scope), CLOSE_SCOPE);
536 return;
537 }
538 CHKRV_SCOPE(item->env, napi_create_int32(item->env, static_cast<int32_t>(iter->second), &eventMsg),
539 CREATE_INT32, scope);
540 napi_value object = nullptr;
541 CHKRV_SCOPE(item->env, napi_create_object(item->env, &object), CREATE_OBJECT, scope);
542 CHKRV_SCOPE(item->env, napi_set_named_property(item->env, object, "deviceDescriptor", deviceDescriptor),
543 SET_NAMED_PROPERTY, scope);
544 CHKRV_SCOPE(item->env, napi_set_named_property(item->env, object, "eventMsg", eventMsg),
545 SET_NAMED_PROPERTY, scope);
546 napi_value handler = nullptr;
547 CHKRV_SCOPE(item->env, napi_get_reference_value(item->env, item->ref, &handler), GET_REFERENCE_VALUE, scope);
548 napi_value ret = nullptr;
549 CHKRV_SCOPE(item->env, napi_call_function(item->env, nullptr, handler, 1, &object, &ret), CALL_FUNCTION, scope);
550 napi_close_handle_scope(item->env, scope);
551 }
552 }
553 } // namespace DeviceStatus
554 } // namespace Msdp
555 } // namespace OHOS
556