@pwaos.net/runtime-client-api 0.0.0-stage → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/app_obj.cc ADDED
@@ -0,0 +1,832 @@
1
+ #include <node_api.h>
2
+ #include <string.h>
3
+ #include <stdio.h>
4
+ #include <memory>
5
+
6
+ #include "common.h"
7
+
8
+ struct AppCallbackContext;
9
+ struct app_status {
10
+ AppCallbackContext* userData;
11
+ };
12
+ static std::unordered_map<PWAOS_APP_HANDLE, napi_ref> g_apps;
13
+ static std::unordered_map<PWAOS_APP_HANDLE, app_status> g_apps_status;
14
+ static app_status* CreateAppStatus(PWAOS_APP_HANDLE app_handle) {
15
+ g_apps_status.try_emplace(app_handle);
16
+ const auto& iter = g_apps_status.find(app_handle);
17
+ return &iter->second;
18
+ }
19
+ static app_status* GetAppStatus(PWAOS_APP_HANDLE app_handle) {
20
+ const auto& iter = g_apps_status.find(app_handle);
21
+ if (iter != g_apps_status.end())
22
+ return &iter->second;
23
+ return nullptr;
24
+ }
25
+
26
+ struct AppCallbackContext {
27
+ napi_threadsafe_function tsfn = nullptr;
28
+ napi_ref user_data_ref = nullptr;
29
+
30
+ void release(napi_env env) {
31
+ if (tsfn)
32
+ napi_release_threadsafe_function(tsfn, napi_tsfn_abort);
33
+ if (user_data_ref)
34
+ napi_delete_reference(env, user_data_ref);
35
+ }
36
+ };
37
+
38
+ static void AppCallback(const pwaosAppStatus stat, void* userData) {
39
+ auto* callback_ctx = (AppCallbackContext*)userData;
40
+ // napi_acquire_threadsafe_function(callback_ctx->tsfn);
41
+ napi_call_threadsafe_function(callback_ctx->tsfn, (void*)stat, napi_tsfn_blocking);
42
+ }
43
+
44
+ static void CallAppStatusCallback(napi_env env, napi_value js_cb, void* context, void* data) {
45
+ napi_value recv;
46
+ if (napi_get_undefined(env, &recv) != napi_ok) {
47
+ napi_throw_error(env,
48
+ "ERR_NAPI_TSFN_GET_UNDEFINED",
49
+ "Failed to retrieve undefined value");
50
+ return;
51
+ }
52
+
53
+ auto status = (pwaosAppStatus)(intptr_t)data;
54
+ napi_value arg;
55
+ napi_create_int32(env, status, &arg);
56
+
57
+ // 如果有 userData,还需要取出
58
+ napi_value user_data;
59
+ AppCallbackContext* ctx = (AppCallbackContext*)context;
60
+ napi_value args[2];
61
+ size_t argc = 1;
62
+ if (napi_ok == napi_get_reference_value(env, ctx->user_data_ref, &user_data)) {
63
+ args[1] = user_data;
64
+ argc = 2;
65
+ }
66
+ napi_call_function(env, recv, js_cb, argc, args, nullptr);
67
+ }
68
+
69
+ static void app_obj_finalizer(napi_env env,
70
+ void* finalize_data,
71
+ void* finalize_hint) {
72
+ PWAOS_APP_HANDLE app_handle = (PWAOS_APP_HANDLE)finalize_data;
73
+ if (!app_handle)
74
+ return;
75
+
76
+ pwaosInstance_destroyPwa(app_handle);
77
+
78
+ const auto& it = g_apps.find(app_handle);
79
+ if (it != g_apps.end()) {
80
+ napi_delete_reference(env, it->second);
81
+ g_apps.erase(it);
82
+ }
83
+
84
+ if (auto* userData = g_apps_status[app_handle].userData) {
85
+ auto* callback_ctx = (AppCallbackContext*)userData;
86
+ callback_ctx->release(env);
87
+ delete callback_ctx;
88
+ }
89
+ g_apps_status.erase(app_handle);
90
+ }
91
+
92
+ // 将 JS 对象转换为 pwaosWindowBoundsInfo
93
+ static napi_status bounds_from_js(napi_env env, napi_value js_bounds, pwaosWindowBoundsInfo* bounds) {
94
+ napi_status status;
95
+
96
+ napi_value x_val, y_val, w_val, h_val;
97
+ status = napi_get_named_property(env, js_bounds, "x", &x_val);
98
+ if (status == napi_ok) status = napi_get_value_int32(env, x_val, &bounds->x);
99
+ if (status != napi_ok) return status;
100
+ status = napi_get_named_property(env, js_bounds, "y", &y_val);
101
+ if (status == napi_ok) status = napi_get_value_int32(env, y_val, &bounds->y);
102
+ if (status != napi_ok) return status;
103
+ status = napi_get_named_property(env, js_bounds, "width", &w_val);
104
+ if (status == napi_ok) status = napi_get_value_int32(env, w_val, &bounds->width);
105
+ if (status != napi_ok) return status;
106
+ status = napi_get_named_property(env, js_bounds, "height", &h_val);
107
+ if (status == napi_ok) status = napi_get_value_int32(env, h_val, &bounds->height);
108
+ return status;
109
+ }
110
+
111
+ // 将 pwaosWindowBoundsInfo 转换为 JS 对象
112
+ static napi_value bounds_to_js(napi_env env, const pwaosWindowBoundsInfo* bounds) {
113
+ napi_value obj;
114
+ napi_create_object(env, &obj);
115
+ napi_value x, y, w, h;
116
+ napi_create_int32(env, bounds->x, &x);
117
+ napi_create_int32(env, bounds->y, &y);
118
+ napi_create_int32(env, bounds->width, &w);
119
+ napi_create_int32(env, bounds->height, &h);
120
+ napi_set_named_property(env, obj, "x", x);
121
+ napi_set_named_property(env, obj, "y", y);
122
+ napi_set_named_property(env, obj, "width", w);
123
+ napi_set_named_property(env, obj, "height", h);
124
+ return obj;
125
+ }
126
+
127
+ static napi_value AppCreateProjector(napi_env env, napi_callback_info info) {
128
+ size_t argc = 1;
129
+ napi_value argv[1];
130
+ napi_value this_arg;
131
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, &this_arg, nullptr));
132
+
133
+ if (argc < 1) {
134
+ napi_throw_type_error(env, nullptr, "Expected 1 arguments: createInfo");
135
+ return nullptr;
136
+ }
137
+
138
+ // 从 this 对象中取出 interface 句柄
139
+ PWAOS_APP_HANDLE app_handle;
140
+ napi_unwrap(env, this_arg, (void**)&app_handle);
141
+ if (!app_handle) {
142
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
143
+ return nullptr;
144
+ }
145
+
146
+ // 解析 createInfo 对象
147
+ napi_valuetype create_type;
148
+ NODE_API_CALL(env, napi_typeof(env, argv[0], &create_type));
149
+ if (create_type != napi_object) {
150
+ napi_throw_type_error(env, nullptr, "Second argument must be an object");
151
+ return nullptr;
152
+ }
153
+
154
+ pwaosProjectorCreateInfo c_info;
155
+ memset(&c_info, 0, sizeof(c_info));
156
+ c_info.type = PWAOS_TYPE_PROJECTOR_CREATE_INFO;
157
+ c_info.next = nullptr;
158
+
159
+ // 读取 projectorType
160
+ napi_value pt_val;
161
+ if (napi_get_named_property(env, argv[0], "projectorType", &pt_val) == napi_ok) {
162
+ int32_t pt;
163
+ NODE_API_CALL(env, napi_get_value_int32(env, pt_val, &pt));
164
+ c_info.projectorType = (pwaosProjectorType)pt;
165
+ } else {
166
+ napi_throw_type_error(env, nullptr, "Missing 'projectorType'");
167
+ return nullptr;
168
+ }
169
+
170
+ // 读取 width, height (可选)
171
+ napi_value w_val, h_val;
172
+ if (napi_get_named_property(env, argv[0], "width", &w_val) == napi_ok) {
173
+ NODE_API_CALL(env, napi_get_value_int32(env, w_val, &c_info.width));
174
+ } else {
175
+ c_info.width = 0;
176
+ }
177
+ if (napi_get_named_property(env, argv[0], "height", &h_val) == napi_ok) {
178
+ NODE_API_CALL(env, napi_get_value_int32(env, h_val, &c_info.height));
179
+ } else {
180
+ c_info.height = 0;
181
+ }
182
+
183
+ // 注意:更复杂的扩展结构(如 EGL/Vulkan/D3D)暂不处理,使用默认
184
+
185
+ PWAOS_PROJECTOR_HANDLE projector_handle = nullptr;
186
+ PWAOS_API_CALL(env, pwaosApp_createProjector(app_handle, &c_info, &projector_handle));
187
+
188
+ napi_value js_handle;
189
+ NODE_API_CALL(env, napi_create_external(env, projector_handle, nullptr, nullptr, &js_handle));
190
+ return js_handle;
191
+ }
192
+
193
+ static napi_value AppDestroyProjector(napi_env env, napi_callback_info info) {
194
+ size_t argc = 1;
195
+ napi_value argv[1];
196
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
197
+
198
+ if (argc < 1) {
199
+ napi_throw_type_error(env, nullptr, "Expected 1 argument: projectorHandle");
200
+ return nullptr;
201
+ }
202
+
203
+ PWAOS_PROJECTOR_HANDLE projector_handle = nullptr;
204
+ napi_valuetype handle_type;
205
+ NODE_API_CALL(env, napi_typeof(env, argv[0], &handle_type));
206
+ if (handle_type == napi_external) {
207
+ NODE_API_CALL(env, napi_get_value_external(env, argv[0], (void**)&projector_handle));
208
+ } else {
209
+ napi_throw_type_error(env, nullptr, "Argument must be an external handle");
210
+ return nullptr;
211
+ }
212
+
213
+ pwaosResult result = pwaosApp_destroyProjector(projector_handle);
214
+ if (result != PWAOS_RESULT_SUCCESS) {
215
+ throw_pwaos_error(env, result);
216
+ return nullptr;
217
+ }
218
+
219
+ napi_value ret;
220
+ NODE_API_CALL(env, napi_create_int32(env, result, &ret));
221
+ return ret;
222
+ }
223
+
224
+ static napi_value AppGetBounds(napi_env env, napi_callback_info info) {
225
+ size_t argc = 0;
226
+ napi_value this_arg;
227
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &this_arg, nullptr));
228
+
229
+ // 从 this 对象中取出 interface 句柄
230
+ PWAOS_APP_HANDLE app_handle;
231
+ napi_unwrap(env, this_arg, (void**)&app_handle);
232
+ if (!app_handle) {
233
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
234
+ return nullptr;
235
+ }
236
+
237
+ // SDK 0.2.x validates the out-param's type field: zero the struct and
238
+ // set the type before the call or getBounds fails VALIDATION_FAILURE.
239
+ pwaosWindowBoundsInfo bounds {PWAOS_TYPE_WINDOW_BOUNDS_INFO, nullptr,
240
+ 0, 0, 0, 0};
241
+ pwaosResult result = pwaosApp_getBounds(app_handle, &bounds);
242
+ if (result != PWAOS_RESULT_SUCCESS) {
243
+ throw_pwaos_error(env, result);
244
+ return nullptr;
245
+ }
246
+
247
+ return bounds_to_js(env, &bounds);
248
+ }
249
+
250
+ static napi_value AppSetBounds(napi_env env, napi_callback_info info) {
251
+ size_t argc = 1;
252
+ napi_value argv[1];
253
+ napi_value this_arg;
254
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, &this_arg, nullptr));
255
+
256
+ if (argc < 1) {
257
+ napi_throw_type_error(env, nullptr, "Expected 1 arguments: bounds");
258
+ return nullptr;
259
+ }
260
+
261
+ // 从 this 对象中取出 interface 句柄
262
+ PWAOS_APP_HANDLE app_handle;
263
+ napi_unwrap(env, this_arg, (void**)&app_handle);
264
+ if (!app_handle) {
265
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
266
+ return nullptr;
267
+ }
268
+
269
+ napi_valuetype bounds_type;
270
+ NODE_API_CALL(env, napi_typeof(env, argv[0], &bounds_type));
271
+ if (bounds_type != napi_object) {
272
+ napi_throw_type_error(env, nullptr, "First argument must be an object");
273
+ return nullptr;
274
+ }
275
+
276
+ pwaosWindowBoundsInfo bounds {PWAOS_TYPE_WINDOW_BOUNDS_INFO, nullptr,
277
+ 0, 0, 0, 0};
278
+ napi_status status = bounds_from_js(env, argv[0], &bounds);
279
+ if (status != napi_ok) {
280
+ napi_throw_type_error(env, nullptr, "Invalid bounds object");
281
+ return nullptr;
282
+ }
283
+
284
+ PWAOS_API_CALL_AND_RETURN(env, pwaosApp_setBounds(app_handle, &bounds));
285
+ }
286
+
287
+ static napi_value AppExecuteJavaScript(napi_env env, napi_callback_info info) {
288
+ size_t argc = 1;
289
+ napi_value argv[1];
290
+ napi_value this_arg;
291
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, &this_arg, nullptr));
292
+
293
+ if (argc < 1) {
294
+ napi_throw_type_error(env, nullptr, "Expected 1 arguments: javascript");
295
+ return nullptr;
296
+ }
297
+
298
+ // 从 this 对象中取出 interface 句柄
299
+ PWAOS_APP_HANDLE app_handle;
300
+ napi_unwrap(env, this_arg, (void**)&app_handle);
301
+ if (!app_handle) {
302
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
303
+ return nullptr;
304
+ }
305
+
306
+ napi_valuetype js_type;
307
+ NODE_API_CALL(env, napi_typeof(env, argv[0], &js_type));
308
+ if (js_type != napi_string) {
309
+ napi_throw_type_error(env, nullptr, "Second argument must be a string");
310
+ return nullptr;
311
+ }
312
+
313
+ size_t js_len;
314
+ NODE_API_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &js_len));
315
+ auto js_code = std::make_unique<char[]>(js_len + 1);
316
+ if (!js_code) {
317
+ napi_throw_error(env, nullptr, "Out of memory");
318
+ return nullptr;
319
+ }
320
+ NODE_API_CALL(env, napi_get_value_string_utf8(env, argv[0], js_code.get(), js_len + 1, &js_len));
321
+
322
+ PWAOS_API_CALL_AND_RETURN(env, pwaosApp_executeJavaScript(app_handle, js_code.get()));
323
+ }
324
+
325
+ static napi_value AppGetDensity(napi_env env, napi_callback_info info) {
326
+ size_t argc = 0;
327
+ napi_value this_arg;
328
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &this_arg, nullptr));
329
+
330
+ // 从 this 对象中取出 interface 句柄
331
+ PWAOS_APP_HANDLE app_handle;
332
+ napi_unwrap(env, this_arg, (void**)&app_handle);
333
+ if (!app_handle) {
334
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
335
+ return nullptr;
336
+ }
337
+
338
+ float density;
339
+ PWAOS_API_CALL(env, pwaosApp_getDensity(app_handle, &density));
340
+
341
+ napi_value js_density;
342
+ NODE_API_CALL(env, napi_create_double(env, density, &js_density));
343
+ return js_density;
344
+ }
345
+
346
+ static napi_value AppSetDensity(napi_env env, napi_callback_info info) {
347
+ size_t argc = 1;
348
+ napi_value argv[1];
349
+ napi_value this_arg;
350
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, &this_arg, nullptr));
351
+
352
+ if (argc < 1) {
353
+ napi_throw_type_error(env, nullptr, "Expected 1 arguments: density");
354
+ return nullptr;
355
+ }
356
+
357
+ // 从 this 对象中取出 app 句柄
358
+ PWAOS_APP_HANDLE app_handle;
359
+ napi_unwrap(env, this_arg, (void**)&app_handle);
360
+ if (!app_handle) {
361
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
362
+ return nullptr;
363
+ }
364
+
365
+ double density;
366
+ NODE_API_CALL(env, napi_get_value_double(env, argv[0], &density));
367
+
368
+ PWAOS_API_CALL_AND_RETURN(env, pwaosApp_setDensity(app_handle, (float)density));
369
+ }
370
+
371
+ static napi_value AppGetDevToolsUri(napi_env env, napi_callback_info info) {
372
+ size_t argc = 0;
373
+ napi_value this_arg;
374
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &this_arg, nullptr));
375
+
376
+ // 从 this 对象中取出 app 句柄
377
+ PWAOS_APP_HANDLE app_handle;
378
+ napi_unwrap(env, this_arg, (void**)&app_handle);
379
+ if (!app_handle) {
380
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
381
+ return nullptr;
382
+ }
383
+
384
+ // Zero-init ({}): SDK 0.2.x validates out-params and url[] must never leak
385
+ // uninitialized bytes into the JS string (getBounds hazard class).
386
+ pwaosUriInfo uri_data{};
387
+ uri_data.type = PWAOS_TYPE_DEVTOOLS_URI_INFO;
388
+ uri_data.next = NULL;
389
+ auto result = pwaosApp_getDevToolsUri(app_handle, &uri_data);
390
+ if (PWAOS_RESULT_SUCCESS == result ) {
391
+ napi_value js_uri;
392
+ NODE_API_CALL(env, napi_create_string_utf8(env, uri_data.url, NAPI_AUTO_LENGTH, &js_uri));
393
+ return js_uri;
394
+ }
395
+
396
+ napi_value ret;
397
+ NODE_API_CALL(env, napi_create_int32(env, result, &ret));
398
+ return ret;
399
+ }
400
+
401
+ static napi_value AppSetFPS(napi_env env, napi_callback_info info) {
402
+ size_t argc = 1;
403
+ napi_value argv[1];
404
+ napi_value this_arg;
405
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, &this_arg, nullptr));
406
+
407
+ if (argc < 1) {
408
+ napi_throw_type_error(env, nullptr, "Expected 1 arguments: fps");
409
+ return nullptr;
410
+ }
411
+
412
+ // 从 this 对象中取出 app 句柄
413
+ PWAOS_APP_HANDLE app_handle;
414
+ napi_unwrap(env, this_arg, (void**)&app_handle);
415
+ if (!app_handle) {
416
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
417
+ return nullptr;
418
+ }
419
+
420
+ double fps;
421
+ NODE_API_CALL(env, napi_get_value_double(env, argv[0], &fps));
422
+ pwaosAppFpsInfo fpsInfo{PWAOS_TYPE_APP_FPS_INFO, nullptr, static_cast<float>(fps)};
423
+ PWAOS_API_CALL_AND_RETURN(env, pwaosApp_setFPS(app_handle, &fpsInfo));
424
+ }
425
+
426
+ static napi_value AppHandleClick(napi_env env, napi_callback_info info) {
427
+ size_t argc = 2;
428
+ napi_value argv[2];
429
+ napi_value this_arg;
430
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, &this_arg, nullptr));
431
+
432
+ if (argc < 2) {
433
+ napi_throw_type_error(env, nullptr, "Expected 2 arguments: isLeft, isClicked");
434
+ return nullptr;
435
+ }
436
+
437
+ // 从 this 对象中取出 app 句柄
438
+ PWAOS_APP_HANDLE app_handle;
439
+ napi_unwrap(env, this_arg, (void**)&app_handle);
440
+ if (!app_handle) {
441
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
442
+ return nullptr;
443
+ }
444
+
445
+ bool is_left, is_clicked;
446
+ NODE_API_CALL(env, napi_get_value_bool(env, argv[0], &is_left));
447
+ NODE_API_CALL(env, napi_get_value_bool(env, argv[1], &is_clicked));
448
+
449
+ pwaosResult result = pwaosApp_handleClick(app_handle, is_left, is_clicked);
450
+ if (result != PWAOS_RESULT_SUCCESS) {
451
+ throw_pwaos_error(env, result);
452
+ return nullptr;
453
+ }
454
+
455
+ napi_value ret;
456
+ NODE_API_CALL(env, napi_create_int32(env, result, &ret));
457
+ return ret;
458
+ }
459
+
460
+ static napi_value AppHandlePointerInput(napi_env env, napi_callback_info info) {
461
+ size_t argc = 3;
462
+ napi_value argv[3];
463
+ napi_value this_arg;
464
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, &this_arg, nullptr));
465
+
466
+ if (argc < 3) {
467
+ napi_throw_type_error(env, nullptr, "Expected 3 arguments: isLeft, x, y");
468
+ return nullptr;
469
+ }
470
+
471
+ // 从 this 对象中取出 app 句柄
472
+ PWAOS_APP_HANDLE app_handle;
473
+ napi_unwrap(env, this_arg, (void**)&app_handle);
474
+ if (!app_handle) {
475
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
476
+ return nullptr;
477
+ }
478
+
479
+ bool is_left;
480
+ double x, y;
481
+ NODE_API_CALL(env, napi_get_value_bool(env, argv[0], &is_left));
482
+ NODE_API_CALL(env, napi_get_value_double(env, argv[1], &x));
483
+ NODE_API_CALL(env, napi_get_value_double(env, argv[2], &y));
484
+
485
+ PWAOS_API_CALL_AND_RETURN(env, pwaosApp_handlePointerInput(app_handle, is_left, (float)x, (float)y));
486
+ }
487
+
488
+ static napi_value AppGetUserAgent(napi_env env, napi_callback_info info) {
489
+ size_t argc = 0;
490
+ napi_value this_arg;
491
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &this_arg, nullptr));
492
+
493
+ // 从 this 对象中取出 app 句柄
494
+ PWAOS_APP_HANDLE app_handle;
495
+ napi_unwrap(env, this_arg, (void**)&app_handle);
496
+ if (!app_handle) {
497
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
498
+ return nullptr;
499
+ }
500
+
501
+ // Zero-init ({}): userAgent[] is read back as a C-string below and must
502
+ // never contain uninitialized bytes (getBounds hazard class).
503
+ pwaosUserAgentInfo ua_data{};
504
+ ua_data.type = PWAOS_TYPE_USER_AGENT_INFO;
505
+ ua_data.next = NULL;
506
+
507
+ PWAOS_API_CALL(env, pwaosApp_getUserAgent(app_handle, &ua_data));
508
+
509
+ napi_value js_ua;
510
+ NODE_API_CALL(env, napi_create_string_utf8(env, ua_data.userAgent, NAPI_AUTO_LENGTH, &js_ua));
511
+ return js_ua;
512
+ }
513
+
514
+ static napi_value AppSetUserAgent(napi_env env, napi_callback_info info) {
515
+ size_t argc = 1;
516
+ napi_value argv[1];
517
+ napi_value this_arg;
518
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, &this_arg, nullptr));
519
+
520
+ if (argc < 1) {
521
+ napi_throw_type_error(env, nullptr, "Expected 1 arguments: userAgent");
522
+ return nullptr;
523
+ }
524
+
525
+ // 从 this 对象中取出 app 句柄
526
+ PWAOS_APP_HANDLE app_handle;
527
+ napi_unwrap(env, this_arg, (void**)&app_handle);
528
+ if (!app_handle) {
529
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
530
+ return nullptr;
531
+ }
532
+
533
+ napi_valuetype ua_type;
534
+ NODE_API_CALL(env, napi_typeof(env, argv[0], &ua_type));
535
+ if (ua_type != napi_string) {
536
+ napi_throw_type_error(env, nullptr, "Second argument must be a string");
537
+ return nullptr;
538
+ }
539
+
540
+ size_t ua_len;
541
+ NODE_API_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &ua_len));
542
+ if (ua_len >= MAX_URI_LEN) {
543
+ napi_throw_error(env, nullptr, "UserAgent too long");
544
+ return nullptr;
545
+ }
546
+
547
+ // Zero-init ({}): the tail of userAgent[] beyond the copied string goes
548
+ // over the IPC channel and must be deterministic (getBounds hazard class).
549
+ pwaosUserAgentInfo ua_data{};
550
+ ua_data.type = PWAOS_TYPE_USER_AGENT_INFO;
551
+ ua_data.next = NULL;
552
+ NODE_API_CALL(env, napi_get_value_string_utf8(env, argv[0], ua_data.userAgent, MAX_URI_LEN, &ua_len));
553
+
554
+ PWAOS_API_CALL_AND_RETURN(env, pwaosApp_setUserAgent(app_handle, &ua_data));
555
+ }
556
+
557
+ static napi_value AppAddJavascriptInterface(napi_env env, napi_callback_info info) {
558
+ size_t argc = 2;
559
+ napi_value argv[2];
560
+ napi_value this_arg;
561
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, &this_arg, nullptr));
562
+
563
+ if (argc < 2) {
564
+ napi_throw_type_error(env, nullptr, "Expected 2 arguments: name, objInfHandle");
565
+ return nullptr;
566
+ }
567
+
568
+ // 从 this 对象中取出 app 句柄
569
+ PWAOS_APP_HANDLE app_handle;
570
+ napi_unwrap(env, this_arg, (void**)&app_handle);
571
+ if (!app_handle) {
572
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
573
+ return nullptr;
574
+ }
575
+
576
+ // 获取接口名称
577
+ napi_valuetype name_type;
578
+ NODE_API_CALL(env, napi_typeof(env, argv[0], &name_type));
579
+ if (name_type != napi_string) {
580
+ napi_throw_type_error(env, nullptr, "Second argument must be a string");
581
+ return nullptr;
582
+ }
583
+ size_t name_len;
584
+ NODE_API_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &name_len));
585
+ char name[256];
586
+ if (name_len >= sizeof(name)) {
587
+ napi_throw_error(env, nullptr, "Interface name too long");
588
+ return nullptr;
589
+ }
590
+ NODE_API_CALL(env, napi_get_value_string_utf8(env, argv[0], name, sizeof(name), &name_len));
591
+
592
+ // 获取对象接口句柄
593
+ PWAOS_OBJECT_INTERFACE_HANDLE obj_handle = nullptr;
594
+ napi_valuetype obj_type;
595
+ NODE_API_CALL(env, napi_typeof(env, argv[1], &obj_type));
596
+ if (obj_type == napi_external) {
597
+ NODE_API_CALL(env, napi_get_value_external(env, argv[1], (void**)&obj_handle));
598
+ } else if (obj_type == napi_number) {
599
+ int64_t ptr_val;
600
+ NODE_API_CALL(env, napi_get_value_int64(env, argv[1], &ptr_val));
601
+ obj_handle = (PWAOS_OBJECT_INTERFACE_HANDLE)(uintptr_t)ptr_val;
602
+ } else {
603
+ napi_throw_type_error(env, nullptr, "Third argument must be an external or number handle");
604
+ return nullptr;
605
+ }
606
+
607
+ PWAOS_API_CALL_AND_RETURN(env, pwaosApp_addJavascriptInterface(app_handle, name, obj_handle));
608
+ }
609
+
610
+ static napi_value AppRemoveJavascriptInterface(napi_env env, napi_callback_info info) {
611
+ size_t argc = 1;
612
+ napi_value argv[1];
613
+ napi_value this_arg;
614
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, &this_arg, nullptr));
615
+
616
+ if (argc < 1) {
617
+ napi_throw_type_error(env, nullptr, "Expected 1 arguments: name");
618
+ return nullptr;
619
+ }
620
+
621
+ // 从 this 对象中取出 app 句柄
622
+ PWAOS_APP_HANDLE app_handle;
623
+ napi_unwrap(env, this_arg, (void**)&app_handle);
624
+ if (!app_handle) {
625
+ napi_throw_error(env, nullptr, "Invalid pwa handle");
626
+ return nullptr;
627
+ }
628
+
629
+ // 获取接口名称
630
+ napi_valuetype name_type;
631
+ NODE_API_CALL(env, napi_typeof(env, argv[0], &name_type));
632
+ if (name_type != napi_string) {
633
+ napi_throw_type_error(env, nullptr, "Second argument must be a string");
634
+ return nullptr;
635
+ }
636
+ size_t name_len;
637
+ NODE_API_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &name_len));
638
+ char name[256];
639
+ if (name_len >= sizeof(name)) {
640
+ napi_throw_error(env, nullptr, "Interface name too long");
641
+ return nullptr;
642
+ }
643
+ NODE_API_CALL(env, napi_get_value_string_utf8(env, argv[0], name, sizeof(name), &name_len));
644
+
645
+ PWAOS_API_CALL_AND_RETURN(env, pwaosApp_removeJavascriptInterface(app_handle, name));
646
+ }
647
+
648
+ static napi_value AppSetCallback(napi_env env, napi_callback_info info) {
649
+ size_t argc = 2;
650
+ napi_value argv[2];
651
+ napi_value this_arg;
652
+ NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, &this_arg, nullptr));
653
+
654
+ if (argc < 1) {
655
+ napi_throw_type_error(env, nullptr, "Expected at least 1 argument: callback[, userData]");
656
+ return nullptr;
657
+ }
658
+
659
+ // 从 this 对象中取出 app 句柄
660
+ PWAOS_APP_HANDLE app_handle;
661
+ napi_unwrap(env, this_arg, (void**)&app_handle);
662
+ if (!app_handle) {
663
+ napi_throw_error(env, nullptr, "Invalid app handle");
664
+ return nullptr;
665
+ }
666
+
667
+ auto* app_status = GetAppStatus(app_handle);
668
+ if (!app_status) {
669
+ napi_throw_error(env, nullptr, "Internal error");
670
+ return nullptr;
671
+ }
672
+
673
+ auto* last_callback_ctx = (AppCallbackContext*)app_status->userData;
674
+
675
+ // 处理清除回调(callback 为 null/undefined)
676
+ napi_valuetype cb_type;
677
+ NODE_API_CALL(env, napi_typeof(env, argv[0], &cb_type));
678
+ if (cb_type == napi_null || cb_type == napi_undefined) {
679
+ napi_value ret;
680
+ napi_create_int32(env, pwaosApp_setCallback(app_handle, nullptr, nullptr), &ret);
681
+ if (last_callback_ctx) {
682
+ app_status->userData = nullptr;
683
+ last_callback_ctx->release(env);
684
+ delete last_callback_ctx;
685
+ }
686
+ return ret;
687
+ }
688
+
689
+ // 必须是函数
690
+ if (cb_type != napi_function) {
691
+ napi_throw_type_error(env, nullptr, "First argument must be a function, null, or undefined");
692
+ return nullptr;
693
+ }
694
+
695
+ // 获取 userData(可选)
696
+ napi_value user_data;
697
+ if (argc >= 2) {
698
+ user_data = argv[1];
699
+ } else {
700
+ napi_get_undefined(env, &user_data);
701
+ }
702
+
703
+ auto ctx = std::make_unique<AppCallbackContext>();
704
+ if (!ctx) {
705
+ napi_throw_type_error(env, nullptr, "Out Of Memory!!!");
706
+ return nullptr;
707
+ }
708
+ auto* raw_ctx = ctx.get();
709
+
710
+ // 创建线程安全函数
711
+ napi_value async_name;
712
+ napi_create_string_utf8(env, "AppStatusCallback", NAPI_AUTO_LENGTH, &async_name);
713
+ napi_create_threadsafe_function(env, argv[0], nullptr, async_name, 0, 1, nullptr,
714
+ nullptr, raw_ctx, CallAppStatusCallback, &raw_ctx->tsfn);
715
+ // 创建 userData 的持久引用
716
+ napi_create_reference(env, user_data, 1, &raw_ctx->user_data_ref);
717
+
718
+ // 注册 C 回调
719
+ pwaosResult result = pwaosApp_setCallback(app_handle, AppCallback, raw_ctx);
720
+ if (result != PWAOS_RESULT_SUCCESS) {
721
+ // 失败则清理
722
+ raw_ctx->release(env);
723
+ throw_pwaos_error(env, result);
724
+ return nullptr;
725
+ }
726
+
727
+ if (last_callback_ctx) {
728
+ last_callback_ctx->release(env);
729
+ delete last_callback_ctx;
730
+ app_status->userData = nullptr;
731
+ }
732
+
733
+ app_status->userData = raw_ctx;
734
+ ctx.release();
735
+
736
+ napi_value ret;
737
+ napi_create_int32(env, result, &ret);
738
+ return ret;
739
+ }
740
+
741
+ static napi_value AppConstructor(napi_env env, napi_callback_info info) {
742
+ return nullptr;
743
+ }
744
+
745
+ static napi_ref g_app_class_ref = nullptr;
746
+ napi_value InitAppClass(napi_env env, napi_value exports) {
747
+ napi_property_descriptor methods[] = {
748
+ DECLARE_NODE_API_PROPERTY("addJavascriptInterface", AppAddJavascriptInterface),
749
+ DECLARE_NODE_API_PROPERTY("createProjector", AppCreateProjector),
750
+ DECLARE_NODE_API_PROPERTY("destroyProjector", AppDestroyProjector),
751
+ DECLARE_NODE_API_PROPERTY("getBounds", AppGetBounds),
752
+ DECLARE_NODE_API_PROPERTY("executeJavaScript", AppExecuteJavaScript),
753
+ DECLARE_NODE_API_PROPERTY("getDensity", AppGetDensity),
754
+ DECLARE_NODE_API_PROPERTY("getDevToolsUri", AppGetDevToolsUri),
755
+ DECLARE_NODE_API_PROPERTY("getUserAgent", AppGetUserAgent),
756
+ DECLARE_NODE_API_PROPERTY("handleClick", AppHandleClick),
757
+ DECLARE_NODE_API_PROPERTY("handlePointerInput", AppHandlePointerInput),
758
+ DECLARE_NODE_API_PROPERTY("removeJavascriptInterface", AppRemoveJavascriptInterface),
759
+ DECLARE_NODE_API_PROPERTY("setBounds", AppSetBounds),
760
+ DECLARE_NODE_API_PROPERTY("setCallback", AppSetCallback),
761
+ DECLARE_NODE_API_PROPERTY("setDensity", AppSetDensity),
762
+ DECLARE_NODE_API_PROPERTY("setFPS", AppSetFPS),
763
+ DECLARE_NODE_API_PROPERTY("setUserAgent", AppSetUserAgent),
764
+ };
765
+
766
+ napi_value app_class;
767
+ NODE_API_CALL(
768
+ env,
769
+ napi_define_class(env, "pwaosApp", NAPI_AUTO_LENGTH,
770
+ AppConstructor, nullptr,
771
+ sizeof(methods) / sizeof(methods[0]),
772
+ methods, &app_class));
773
+
774
+ NODE_API_CALL(env, napi_create_reference(env, app_class, 1, &g_app_class_ref));
775
+
776
+ // pwaosAppType
777
+ DefineStaticInt32Property(env, app_class, "appTypeUnknown", appTypeUnknown);
778
+ DefineStaticInt32Property(env, app_class, "appTypePwa", appTypePwa);
779
+ DefineStaticInt32Property(env, app_class, "appTypeBrowser", appTypeBrowser);
780
+ DefineStaticInt32Property(env, app_class, "appTypeConsole", appTypeConsole);
781
+ DefineStaticInt32Property(env, app_class, "appTypeDesktop", appTypeDesktop);
782
+ DefineStaticInt32Property(env, app_class, "appTypePwaContents", appTypePwaContents);
783
+
784
+ // pwaosAppStatus
785
+ DefineStaticInt32Property(env, app_class, "appStatusInVisible", appStatusInVisible);
786
+ DefineStaticInt32Property(env, app_class, "appStatusStopping", appStatusStopping);
787
+ DefineStaticInt32Property(env, app_class, "appStatusReady", appStatusReady);
788
+ DefineStaticInt32Property(env, app_class, "appStatusSynchronized", appStatusSynchronized);
789
+ DefineStaticInt32Property(env, app_class, "appStatusVisible", appStatusVisible);
790
+ DefineStaticInt32Property(env, app_class, "appStatusFocused", appStatusFocused);
791
+ DefineStaticInt32Property(env, app_class, "appStatusUnFocused", appStatusUnFocused);
792
+
793
+ return app_class;
794
+ }
795
+
796
+ napi_value GetOrCreateAppObj(napi_env env, PWAOS_APP_HANDLE app_handle) {
797
+ const auto& iter = g_apps.find(app_handle);
798
+ if (iter != g_apps.end()) {
799
+ napi_value obj;
800
+ NODE_API_CALL(env, napi_get_reference_value(env, iter->second, &obj));
801
+ return obj;
802
+ }
803
+
804
+ napi_value app_class;
805
+ NODE_API_CALL(env, napi_get_reference_value(env, g_app_class_ref, &app_class));
806
+
807
+ napi_value pwa_obj;
808
+ NODE_API_CALL(env, napi_new_instance(env, app_class, 0, 0, &pwa_obj));
809
+ napi_wrap(env, pwa_obj, app_handle, app_obj_finalizer, nullptr, nullptr);
810
+
811
+ napi_ref ref;
812
+ NODE_API_CALL(env, napi_create_reference(env, pwa_obj, 0, &ref));
813
+
814
+ g_apps.emplace(app_handle, ref);
815
+
816
+ CreateAppStatus(app_handle);
817
+ return pwa_obj;
818
+ }
819
+
820
+ void ReleaseAllApps(napi_env env) {
821
+ for (auto& [instance, ref] : g_apps)
822
+ napi_delete_reference(env, ref);
823
+ g_apps.clear();
824
+
825
+ for (auto& [instance, app_status] : g_apps_status)
826
+ if (app_status.userData) {
827
+ auto* callback_ctx = (AppCallbackContext*)app_status.userData;
828
+ callback_ctx->release(env);
829
+ delete callback_ctx;
830
+ }
831
+ g_apps_status.clear();
832
+ }