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