1 /*
2 * Copyright (c) 2025 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 // [Start the_node_api_layer_code_for_the_data_channel_between_the_application_side_and_the_frontend_page]
17 #include "napi/native_api.h"
18 #include <bits/alltypes.h>
19 #include <memory>
20 #include <string>
21 #include <sys/types.h>
22 #include <iostream>
23 #include <map>
24 #include "hilog/log.h"
25 #include "web/arkweb_interface.h"
26 #include <thread>
27
28 constexpr unsigned int LOG_PRINT_DOMAIN = 0xFF00;
29 ArkWeb_ControllerAPI *controller = nullptr;
30
31 ArkWeb_WebMessagePortAPI *webMessagePort = nullptr;
32 ArkWeb_WebMessageAPI *webMessage = nullptr;
33 size_t g_webMessagePortSize = 0;
34 ArkWeb_WebMessagePortPtr *g_web_message_port_arr = nullptr;
35
WebMessagePortCallback(const char * webTag,const ArkWeb_WebMessagePortPtr port,const ArkWeb_WebMessagePtr message,void * userData)36 static void WebMessagePortCallback(
37 const char *webTag, const ArkWeb_WebMessagePortPtr port, const ArkWeb_WebMessagePtr message, void *userData)
38 {
39 OH_LOG_Print(
40 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
41 "Native Development Kit WebMesagePortCallback webTag:%{public}s,messageType:%{public}d",
42 webTag, webMessage->getType(message));
43 size_t len = 0;
44 void *back = webMessage->getData(message, &len);
45 if (webMessage->getType(message) == ArkWeb_WebMessageType::ARKWEB_STRING) {
46 OH_LOG_Print(
47 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
48 "Native Development Kit WebMesagePortCallback message:%{public}s,messageSize:%{public}d", back, len);
49 } else if (webMessage->getType(message) == ArkWeb_WebMessageType::ARKWEB_BUFFER) {
50 OH_LOG_Print(
51 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
52 "Native Development Kit WebMesagePortCallback messageSize:%{public}d", len);
53 }
54 }
55
NativeWebInit(napi_env env,napi_callback_info info)56 static napi_value NativeWebInit(napi_env env, napi_callback_info info)
57 {
58 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit NativeWebInit start");
59 size_t argc = 1;
60 napi_value args[1] = {nullptr};
61 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
62 // 获取第一个参数webTag
63 size_t webTagSize = 0;
64 napi_get_value_string_utf8(env, args[0], nullptr, 0, &webTagSize);
65 char *webTagValue = new (std::nothrow) char[webTagSize + 1];
66 size_t webTagLength = 0;
67 napi_get_value_string_utf8(env, args[0], webTagValue, webTagSize + 1, &webTagLength);
68 OH_LOG_Print(
69 LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb",
70 "Native Development Kit NativeWebInit webTag:%{public}s", webTagValue);
71
72 controller = reinterpret_cast<ArkWeb_ControllerAPI *>(OH_ArkWeb_GetNativeAPI(ARKWEB_NATIVE_CONTROLLER));
73 if (controller)
74 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "get ArkWeb_ControllerAPI success");
75
76 webMessagePort =
77 reinterpret_cast<ArkWeb_WebMessagePortAPI *>(OH_ArkWeb_GetNativeAPI(ARKWEB_NATIVE_WEB_MESSAGE_PORT));
78 if (webMessagePort)
79 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "get ArkWeb_WebMessagePortAPI success");
80
81 webMessage = reinterpret_cast<ArkWeb_WebMessageAPI *>(OH_ArkWeb_GetNativeAPI(ARKWEB_NATIVE_WEB_MESSAGE));
82 if (webMessage)
83 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "get ArkWeb_WebMessageAPI success");
84
85 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit NativeWebInit end");
86
87 return nullptr;
88 }
89
createWebMessagePorts(napi_env env,napi_callback_info info)90 static napi_value createWebMessagePorts(napi_env env, napi_callback_info info)
91 {
92 size_t argc = 2;
93 napi_value args[2] = {nullptr};
94 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
95
96 // 获取第一个参数webTag
97 size_t webTagSize = 0;
98 napi_get_value_string_utf8(env, args[0], nullptr, 0, &webTagSize);
99 char *webTagValue = new (std::nothrow) char[webTagSize + 1];
100 size_t webTagLength = 0;
101 napi_get_value_string_utf8(env, args[0], webTagValue, webTagSize + 1, &webTagLength);
102 OH_LOG_Print(
103 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
104 "Native Development Kit Refresh webTag:%{public}s", webTagValue);
105
106 // 初始化端口
107 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit createWebMessagePorts begin");
108 g_web_message_port_arr = controller->createWebMessagePorts(webTagValue, &g_webMessagePortSize);
109 // 把其中一个端口发送给HTML
110 ArkWeb_ErrorCode code =
111 controller->postWebMessage(webTagValue, "init_web_messageport", g_web_message_port_arr, 1, "*");
112 OH_LOG_Print(
113 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
114 "Native Development Kit postWebMessage ArkWeb_ErrorCode:%{public}d", code);
115 OH_LOG_Print(
116 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
117 "Native Development Kit createWebMessagePorts end, web message port size:%{public}d", g_webMessagePortSize);
118 return nullptr;
119 }
120
postMessage(napi_env env,napi_callback_info info)121 static napi_value postMessage(napi_env env, napi_callback_info info)
122 {
123 size_t argc = 2;
124 napi_value args[2] = {nullptr};
125 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
126
127 // 获取第一个参数webTag
128 size_t webTagSize = 0;
129 napi_get_value_string_utf8(env, args[0], nullptr, 0, &webTagSize);
130 char *webTagValue = new (std::nothrow) char[webTagSize + 1];
131 size_t webTagLength = 0;
132 napi_get_value_string_utf8(env, args[0], webTagValue, webTagSize + 1, &webTagLength);
133 OH_LOG_Print(
134 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
135 "Native Development Kit Refresh webTag:%{public}s", webTagValue);
136
137 // 发送消息
138 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit postMessage begin");
139
140 if (g_web_message_port_arr == nullptr) {
141 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "webMessagePort is nullptr");
142 return nullptr;
143 }
144 ArkWeb_WebMessagePtr message = webMessage->createWebMessage();
145 webMessage->setType(message, ArkWeb_WebMessageType::ARKWEB_STRING);
146 std::string str = "send string from native";
147 webMessage->setData(message, (void *)str.c_str(), str.length() + 1);
148 ArkWeb_ErrorCode code = webMessagePort->postMessage(g_web_message_port_arr[1], webTagValue, message);
149 OH_LOG_Print(
150 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
151 "Native Development Kit postMessage ArkWeb_ErrorCode:%{public}d", code);
152 webMessage->destroyWebMessage(&message);
153 OH_LOG_Print(
154 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
155 "Native Development Kit postMessage end, web message port size:%{public}d", g_webMessagePortSize);
156 return nullptr;
157 }
158
159
160 // 在线程中发消息
sendMessage(const char * webTag,const ArkWeb_WebMessagePtr message)161 void sendMessage(const char *webTag, const ArkWeb_WebMessagePtr message)
162 {
163 // 发送1000次
164 for (int i = 0; i < 1000; i++) {
165 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "sendMessage in thread %{public}d", i);
166 if (g_web_message_port_arr && webTag && message) {
167 webMessagePort->postMessage(g_web_message_port_arr[1], webTag, message);
168 }
169 }
170 }
postMessageThread(napi_env env,napi_callback_info info)171 static napi_value postMessageThread(napi_env env, napi_callback_info info)
172 {
173 size_t argc = 2;
174 napi_value args[2] = {nullptr};
175 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
176
177 // 获取第一个参数webTag
178 size_t webTagSize = 0;
179 napi_get_value_string_utf8(env, args[0], nullptr, 0, &webTagSize);
180 char *webTagValue = new (std::nothrow) char[webTagSize + 1];
181 size_t webTagLength = 0;
182 napi_get_value_string_utf8(env, args[0], webTagValue, webTagSize + 1, &webTagLength);
183 OH_LOG_Print(
184 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
185 "Native Development Kit Refresh webTag:%{public}s", webTagValue);
186
187 // 构造消息
188 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit postMessage begin");
189
190 if (g_web_message_port_arr == nullptr) {
191 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "webMessagePort is nullptr");
192 return nullptr;
193 }
194 ArkWeb_WebMessagePtr message = webMessage->createWebMessage();
195 webMessage->setType(message, ArkWeb_WebMessageType::ARKWEB_STRING);
196 std::string str = "thread message";
197 webMessage->setData(message, (void *)str.c_str(), str.length() + 1);
198 const int numThreads = 5;
199 std::thread threads[numThreads];
200
201 // 创建线程
202 for (int i = 0; i < numThreads; ++i) {
203 threads[i] = std::thread(sendMessage, webTagValue, message);
204 }
205
206 // 等待所有线程完成
207 for (int i = 0; i < numThreads; ++i) {
208 threads[i].detach();
209 }
210 return nullptr;
211 }
212
213 // 在线程中注册回调
SetHandler(const char * webTag)214 void SetHandler(const char *webTag)
215 {
216 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "setMessageEventHandler in thread");
217 webMessagePort->setMessageEventHandler(g_web_message_port_arr[1], webTag, WebMessagePortCallback, NULL);
218 }
219
setMessageEventHandlerThread(napi_env env,napi_callback_info info)220 static napi_value setMessageEventHandlerThread(napi_env env, napi_callback_info info)
221 {
222 size_t argc = 2;
223 napi_value args[2] = {nullptr};
224 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
225
226 // 获取第一个参数webTag
227 size_t webTagSize = 0;
228 napi_get_value_string_utf8(env, args[0], nullptr, 0, &webTagSize);
229 char *webTagValue = new (std::nothrow) char[webTagSize + 1];
230 size_t webTagLength = 0;
231 napi_get_value_string_utf8(env, args[0], webTagValue, webTagSize + 1, &webTagLength);
232 OH_LOG_Print(
233 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
234 "Native Development Kit Refresh webTag:%{public}s", webTagValue);
235
236 // 注册回调
237 OH_LOG_Print(
238 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
239 "Native Development Kit SetMessageEventHandler begin");
240 if (g_web_message_port_arr == nullptr) {
241 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "webMessagePort is nullptr");
242 return nullptr;
243 }
244 std::thread thread(SetHandler, webTagValue);
245 thread.detach();
246 webMessagePort->setMessageEventHandler(g_web_message_port_arr[1], webTagValue, WebMessagePortCallback, NULL);
247 OH_LOG_Print(
248 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
249 "Native Development Kit SetMessageEventHandler end, web message port size:%{public}d", g_webMessagePortSize);
250 return nullptr;
251 }
postNoneMessage(napi_env env,napi_callback_info info)252 static napi_value postNoneMessage(napi_env env, napi_callback_info info)
253 {
254 size_t argc = 2;
255 napi_value args[2] = {nullptr};
256 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
257
258 // 获取第一个参数webTag
259 size_t webTagSize = 0;
260 napi_get_value_string_utf8(env, args[0], nullptr, 0, &webTagSize);
261 char *webTagValue = new (std::nothrow) char[webTagSize + 1];
262 size_t webTagLength = 0;
263 napi_get_value_string_utf8(env, args[0], webTagValue, webTagSize + 1, &webTagLength);
264 OH_LOG_Print(
265 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN,
266 "ArkWeb", "Native Development Kit Refresh webTag:%{public}s", webTagValue);
267
268 // 发送消息
269 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit 发消息开始");
270
271 if (g_web_message_port_arr == nullptr) {
272 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "webMessagePort is nullptr");
273 return nullptr;
274 }
275 ArkWeb_WebMessagePtr message = webMessage->createWebMessage();
276 webMessage->setType(message, ArkWeb_WebMessageType::ARKWEB_NONE);
277 std::string str = "send string from native";
278 webMessage->setData(message, (void *)str.c_str(), str.length() + 1);
279 webMessagePort->postMessage(g_web_message_port_arr[1], webTagValue, message);
280 webMessage->destroyWebMessage(&message);
281 OH_LOG_Print(
282 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
283 "Native Development Kit postMessage end, web message port size:%{public}d", g_webMessagePortSize);
284 return nullptr;
285 }
286
postBufferMessage(napi_env env,napi_callback_info info)287 static napi_value postBufferMessage(napi_env env, napi_callback_info info)
288 {
289 size_t argc = 2;
290 napi_value args[2] = {nullptr};
291 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
292
293 // 获取第一个参数webTag
294 size_t webTagSize = 0;
295 napi_get_value_string_utf8(env, args[0], nullptr, 0, &webTagSize);
296 char *webTagValue = new (std::nothrow) char[webTagSize + 1];
297 size_t webTagLength = 0;
298 napi_get_value_string_utf8(env, args[0], webTagValue, webTagSize + 1, &webTagLength);
299 OH_LOG_Print(
300 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
301 "Native Development Kit Refresh webTag:%{public}s", webTagValue);
302
303 // 发送消息
304 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit postMessage begin");
305
306 if (g_web_message_port_arr == nullptr) {
307 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "webMessagePort is nullptr");
308 return nullptr;
309 }
310 ArkWeb_WebMessagePtr message1 = webMessage->createWebMessage();
311 webMessage->setType(message1, ArkWeb_WebMessageType::ARKWEB_BUFFER);
312 std::string str1 = "send buffer from native";
313 webMessage->setData(message1, (void *)str1.c_str(), str1.length());
314 webMessagePort->postMessage(g_web_message_port_arr[1], webTagValue, message1);
315 webMessage->destroyWebMessage(&message1);
316 OH_LOG_Print(
317 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
318 "Native Development Kit postMessage end, web message port size:%{public}d", g_webMessagePortSize);
319 return nullptr;
320 }
321
setMessageEventHandler(napi_env env,napi_callback_info info)322 static napi_value setMessageEventHandler(napi_env env, napi_callback_info info)
323 {
324 size_t argc = 2;
325 napi_value args[2] = {nullptr};
326 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
327
328 // 获取第一个参数webTag
329 size_t webTagSize = 0;
330 napi_get_value_string_utf8(env, args[0], nullptr, 0, &webTagSize);
331 char *webTagValue = new (std::nothrow) char[webTagSize + 1];
332 size_t webTagLength = 0;
333 napi_get_value_string_utf8(env, args[0], webTagValue, webTagSize + 1, &webTagLength);
334 OH_LOG_Print(
335 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
336 "Native Development Kit Refresh webTag:%{public}s", webTagValue);
337
338 // 注册回调
339 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit SetMessageEventHandler begin");
340 if (g_web_message_port_arr == nullptr) {
341 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "webMessagePort is nullptr");
342 return nullptr;
343 }
344 webMessagePort->setMessageEventHandler(g_web_message_port_arr[1], webTagValue, WebMessagePortCallback, NULL);
345 OH_LOG_Print(
346 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
347 "Native Development Kit SetMessageEventHandler end, web message port size:%{public}d", g_webMessagePortSize);
348 return nullptr;
349 }
350
closeMessagePort(napi_env env,napi_callback_info info)351 static napi_value closeMessagePort(napi_env env, napi_callback_info info)
352 {
353 size_t argc = 2;
354 napi_value args[2] = {nullptr};
355 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
356
357 // 获取第一个参数webTag
358 size_t webTagSize = 0;
359 napi_get_value_string_utf8(env, args[0], nullptr, 0, &webTagSize);
360 char *webTagValue = new (std::nothrow) char[webTagSize + 1];
361 size_t webTagLength = 0;
362 napi_get_value_string_utf8(env, args[0], webTagValue, webTagSize + 1, &webTagLength);
363 OH_LOG_Print(
364 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
365 "Native Development Kit Refresh webTag:%{public}s", webTagValue);
366
367 // 关闭端口,先调用close,再调用destroyWebMessagePorts
368 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit SetMessageEventHandler begin");
369 if (g_web_message_port_arr == nullptr) {
370 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "webMessagePort is nullptr");
371 return nullptr;
372 }
373 webMessagePort->close(g_web_message_port_arr[0], webTagValue);
374 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
375 "Native Development Kit SetMessageEventHandler end, web message port size:%{public}d", g_webMessagePortSize);
376 controller->refresh(webTagValue);
377 return nullptr;
378 }
379
destroyMessagePort(napi_env env,napi_callback_info info)380 static napi_value destroyMessagePort(napi_env env, napi_callback_info info)
381 {
382 size_t argc = 2;
383 napi_value args[2] = {nullptr};
384 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
385
386 // 获取第一个参数webTag
387 size_t webTagSize = 0;
388 napi_get_value_string_utf8(env, args[0], nullptr, 0, &webTagSize);
389 char *webTagValue = new (std::nothrow) char[webTagSize + 1];
390 size_t webTagLength = 0;
391 napi_get_value_string_utf8(env, args[0], webTagValue, webTagSize + 1, &webTagLength);
392 OH_LOG_Print(
393 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
394 "Native Development Kit Refresh webTag:%{public}s", webTagValue);
395
396 // 释放内存,先调用close,再调用destroyWebMessagePorts
397 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit SetMessageEventHandler begin");
398 if (g_web_message_port_arr == nullptr) {
399 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "webMessagePort is nullptr");
400 return nullptr;
401 }
402 controller->destroyWebMessagePorts(&g_web_message_port_arr, g_webMessagePortSize);
403 OH_LOG_Print(
404 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
405 "Native Development Kit SetMessageEventHandler end, web message port size:%{public}d", g_webMessagePortSize);
406 return nullptr;
407 }
408
destroyNullMessagePort(napi_env env,napi_callback_info info)409 static napi_value destroyNullMessagePort(napi_env env, napi_callback_info info)
410 {
411 size_t argc = 2;
412 napi_value args[2] = {nullptr};
413 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
414
415 // 获取第一个参数webTag
416 size_t webTagSize = 0;
417 napi_get_value_string_utf8(env, args[0], nullptr, 0, &webTagSize);
418 char *webTagValue = new (std::nothrow) char[webTagSize + 1];
419 size_t webTagLength = 0;
420 napi_get_value_string_utf8(env, args[0], webTagValue, webTagSize + 1, &webTagLength);
421 OH_LOG_Print(
422 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
423 "Native Development Kit Refresh webTag:%{public}s", webTagValue);
424
425 // 释放内存,先调用close,再调用destroyWebMessagePorts
426 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit SetMessageEventHandler begin");
427
428 controller->destroyWebMessagePorts(&g_web_message_port_arr, g_webMessagePortSize);
429
430 OH_LOG_Print(
431 LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb",
432 "Native Development Kit SetMessageEventHandler end, web message port size:%{public}d", g_webMessagePortSize);
433 return nullptr;
434 }
435
436 EXTERN_C_START
Init(napi_env env,napi_value exports)437 static napi_value Init(napi_env env, napi_value exports)
438 {
439 napi_property_descriptor desc[] = {
440 {"nativeWebInit", nullptr, NativeWebInit, nullptr, nullptr, nullptr, napi_default, nullptr},
441 {"createWebMessagePorts", nullptr, createWebMessagePorts, nullptr, nullptr, nullptr, napi_default, nullptr},
442 {"postMessage", nullptr, postMessage, nullptr, nullptr, nullptr, napi_default, nullptr},
443 {"postNoneMessage", nullptr, postNoneMessage, nullptr, nullptr, nullptr, napi_default, nullptr},
444 {"postBufferMessage", nullptr, postBufferMessage, nullptr, nullptr, nullptr, napi_default, nullptr},
445 {"setMessageEventHandler", nullptr, setMessageEventHandler, nullptr, nullptr, nullptr, napi_default, nullptr},
446 {"closeMessagePort", nullptr, closeMessagePort, nullptr, nullptr, nullptr, napi_default, nullptr},
447 {"destroyMessagePort", nullptr, destroyMessagePort, nullptr, nullptr, nullptr, napi_default, nullptr},
448 {"postMessageThread", nullptr, postMessageThread, nullptr, nullptr, nullptr, napi_default, nullptr},
449 {"setMessageEventHandlerThread", nullptr, setMessageEventHandlerThread, nullptr, nullptr, nullptr,
450 napi_default, nullptr},
451 {"destroyNullMessagePort", nullptr, destroyNullMessagePort, nullptr, nullptr, nullptr, napi_default, nullptr},
452 };
453 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
454 return exports;
455 }
456 EXTERN_C_END
457
458 static napi_module demoModule = {
459 .nm_version = 1,
460 .nm_flags = 0,
461 .nm_filename = nullptr,
462 .nm_register_func = Init,
463 .nm_modname = "entry",
464 .nm_priv = ((void *)0),
465 .reserved = {0},
466 };
467
RegisterEntryModule(void)468 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
469 // [End the_node_api_layer_code_for_the_data_channel_between_the_application_side_and_the_frontend_page]