@lynx-js/lynx-library-headers 0.0.1-alpha.4

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.
Files changed (135) hide show
  1. package/README.md +53 -0
  2. package/include/base/include/value/lynx_api_types.h +57 -0
  3. package/include/base/include/value/lynx_value_api.h +204 -0
  4. package/include/base/include/value/lynx_value_types.h +73 -0
  5. package/include/capi/lynx_env_capi.h +47 -0
  6. package/include/capi/lynx_event_reporter_service_capi.h +67 -0
  7. package/include/capi/lynx_export.h +27 -0
  8. package/include/capi/lynx_extension_module_capi.h +148 -0
  9. package/include/capi/lynx_extension_module_types_capi.h +17 -0
  10. package/include/capi/lynx_generic_resource_fetcher_capi.h +124 -0
  11. package/include/capi/lynx_group_capi.h +42 -0
  12. package/include/capi/lynx_http_service_capi.h +113 -0
  13. package/include/capi/lynx_load_meta_capi.h +52 -0
  14. package/include/capi/lynx_log_capi.h +69 -0
  15. package/include/capi/lynx_memory_capi.h +28 -0
  16. package/include/capi/lynx_native_module_capi.h +51 -0
  17. package/include/capi/lynx_native_view_capi.h +163 -0
  18. package/include/capi/lynx_resource_request_capi.h +71 -0
  19. package/include/capi/lynx_resource_response_capi.h +92 -0
  20. package/include/capi/lynx_runtime_lifecycle_observer_capi.h +62 -0
  21. package/include/capi/lynx_security_service_capi.h +52 -0
  22. package/include/capi/lynx_service_center_capi.h +50 -0
  23. package/include/capi/lynx_template_bundle_capi.h +39 -0
  24. package/include/capi/lynx_template_data_capi.h +33 -0
  25. package/include/capi/lynx_trace_capi.h +62 -0
  26. package/include/capi/lynx_trail_service_capi.h +35 -0
  27. package/include/capi/lynx_types.h +750 -0
  28. package/include/capi/lynx_update_meta_capi.h +33 -0
  29. package/include/capi/lynx_view_builder_capi.h +113 -0
  30. package/include/capi/lynx_view_capi.h +174 -0
  31. package/include/capi/lynx_view_client_capi.h +81 -0
  32. package/include/capi/lynx_vsync_monitor_capi.h +38 -0
  33. package/include/capi/lynx_windowless_renderer_capi.h +213 -0
  34. package/include/js_native_api.h +608 -0
  35. package/include/js_native_api_types.h +202 -0
  36. package/include/lynx/base/include/value/lynx_api_types.h +57 -0
  37. package/include/lynx/base/include/value/lynx_value_api.h +204 -0
  38. package/include/lynx/base/include/value/lynx_value_types.h +73 -0
  39. package/include/lynx/extension.h +6 -0
  40. package/include/lynx/platform/embedder/public/capi/lynx_env_capi.h +47 -0
  41. package/include/lynx/platform/embedder/public/capi/lynx_event_reporter_service_capi.h +67 -0
  42. package/include/lynx/platform/embedder/public/capi/lynx_export.h +27 -0
  43. package/include/lynx/platform/embedder/public/capi/lynx_extension_module_capi.h +148 -0
  44. package/include/lynx/platform/embedder/public/capi/lynx_extension_module_types_capi.h +17 -0
  45. package/include/lynx/platform/embedder/public/capi/lynx_generic_resource_fetcher_capi.h +124 -0
  46. package/include/lynx/platform/embedder/public/capi/lynx_group_capi.h +42 -0
  47. package/include/lynx/platform/embedder/public/capi/lynx_http_service_capi.h +113 -0
  48. package/include/lynx/platform/embedder/public/capi/lynx_load_meta_capi.h +52 -0
  49. package/include/lynx/platform/embedder/public/capi/lynx_log_capi.h +69 -0
  50. package/include/lynx/platform/embedder/public/capi/lynx_memory_capi.h +28 -0
  51. package/include/lynx/platform/embedder/public/capi/lynx_native_module_capi.h +51 -0
  52. package/include/lynx/platform/embedder/public/capi/lynx_native_view_capi.h +163 -0
  53. package/include/lynx/platform/embedder/public/capi/lynx_resource_request_capi.h +71 -0
  54. package/include/lynx/platform/embedder/public/capi/lynx_resource_response_capi.h +92 -0
  55. package/include/lynx/platform/embedder/public/capi/lynx_runtime_lifecycle_observer_capi.h +62 -0
  56. package/include/lynx/platform/embedder/public/capi/lynx_security_service_capi.h +52 -0
  57. package/include/lynx/platform/embedder/public/capi/lynx_service_center_capi.h +50 -0
  58. package/include/lynx/platform/embedder/public/capi/lynx_template_bundle_capi.h +39 -0
  59. package/include/lynx/platform/embedder/public/capi/lynx_template_data_capi.h +33 -0
  60. package/include/lynx/platform/embedder/public/capi/lynx_trace_capi.h +62 -0
  61. package/include/lynx/platform/embedder/public/capi/lynx_trail_service_capi.h +35 -0
  62. package/include/lynx/platform/embedder/public/capi/lynx_types.h +750 -0
  63. package/include/lynx/platform/embedder/public/capi/lynx_update_meta_capi.h +33 -0
  64. package/include/lynx/platform/embedder/public/capi/lynx_view_builder_capi.h +113 -0
  65. package/include/lynx/platform/embedder/public/capi/lynx_view_capi.h +174 -0
  66. package/include/lynx/platform/embedder/public/capi/lynx_view_client_capi.h +81 -0
  67. package/include/lynx/platform/embedder/public/capi/lynx_vsync_monitor_capi.h +38 -0
  68. package/include/lynx/platform/embedder/public/capi/lynx_windowless_renderer_capi.h +213 -0
  69. package/include/lynx/platform/embedder/public/lynx_env.h +104 -0
  70. package/include/lynx/platform/embedder/public/lynx_event_reporter_service.h +96 -0
  71. package/include/lynx/platform/embedder/public/lynx_event_simulation_proxy.h +31 -0
  72. package/include/lynx/platform/embedder/public/lynx_extension_module.h +521 -0
  73. package/include/lynx/platform/embedder/public/lynx_generic_resource_fetcher.h +178 -0
  74. package/include/lynx/platform/embedder/public/lynx_group.h +70 -0
  75. package/include/lynx/platform/embedder/public/lynx_http_service.h +211 -0
  76. package/include/lynx/platform/embedder/public/lynx_load_meta.h +120 -0
  77. package/include/lynx/platform/embedder/public/lynx_native_module.h +9 -0
  78. package/include/lynx/platform/embedder/public/lynx_native_view.h +201 -0
  79. package/include/lynx/platform/embedder/public/lynx_resource_request.h +56 -0
  80. package/include/lynx/platform/embedder/public/lynx_resource_response.h +63 -0
  81. package/include/lynx/platform/embedder/public/lynx_runtime_lifecycle_observer.h +102 -0
  82. package/include/lynx/platform/embedder/public/lynx_security_service.h +78 -0
  83. package/include/lynx/platform/embedder/public/lynx_service_center.h +64 -0
  84. package/include/lynx/platform/embedder/public/lynx_template_bundle.h +65 -0
  85. package/include/lynx/platform/embedder/public/lynx_template_data.h +104 -0
  86. package/include/lynx/platform/embedder/public/lynx_trail_service.h +81 -0
  87. package/include/lynx/platform/embedder/public/lynx_update_meta.h +56 -0
  88. package/include/lynx/platform/embedder/public/lynx_value.h +270 -0
  89. package/include/lynx/platform/embedder/public/lynx_view.h +364 -0
  90. package/include/lynx/platform/embedder/public/lynx_view_client.h +226 -0
  91. package/include/lynx/platform/embedder/public/lynx_vsync_monitor.h +70 -0
  92. package/include/lynx/platform/embedder/public/lynx_windowless_renderer.h +302 -0
  93. package/include/lynx/registration.h +178 -0
  94. package/include/lynx_env.h +104 -0
  95. package/include/lynx_event_reporter_service.h +96 -0
  96. package/include/lynx_event_simulation_proxy.h +31 -0
  97. package/include/lynx_extension.h +10 -0
  98. package/include/lynx_extension_module.h +521 -0
  99. package/include/lynx_generic_resource_fetcher.h +178 -0
  100. package/include/lynx_group.h +70 -0
  101. package/include/lynx_http_service.h +211 -0
  102. package/include/lynx_load_meta.h +120 -0
  103. package/include/lynx_native_module.h +9 -0
  104. package/include/lynx_native_view.h +201 -0
  105. package/include/lynx_resource_request.h +56 -0
  106. package/include/lynx_resource_response.h +63 -0
  107. package/include/lynx_runtime_lifecycle_observer.h +102 -0
  108. package/include/lynx_security_service.h +78 -0
  109. package/include/lynx_service_center.h +64 -0
  110. package/include/lynx_template_bundle.h +65 -0
  111. package/include/lynx_template_data.h +104 -0
  112. package/include/lynx_trail_service.h +81 -0
  113. package/include/lynx_update_meta.h +56 -0
  114. package/include/lynx_value.h +270 -0
  115. package/include/lynx_view.h +364 -0
  116. package/include/lynx_view_client.h +226 -0
  117. package/include/lynx_vsync_monitor.h +70 -0
  118. package/include/lynx_windowless_renderer.h +302 -0
  119. package/include/napi-inl.deprecated.h +186 -0
  120. package/include/napi-inl.h +6607 -0
  121. package/include/napi.h +3208 -0
  122. package/include/node_api.h +276 -0
  123. package/include/node_api_types.h +59 -0
  124. package/include/third_party/weak-node-api/headers/js_native_api.h +608 -0
  125. package/include/third_party/weak-node-api/headers/js_native_api_types.h +202 -0
  126. package/include/third_party/weak-node-api/headers/napi-inl.deprecated.h +186 -0
  127. package/include/third_party/weak-node-api/headers/napi-inl.h +6607 -0
  128. package/include/third_party/weak-node-api/headers/napi.h +3208 -0
  129. package/include/third_party/weak-node-api/headers/node_api.h +276 -0
  130. package/include/third_party/weak-node-api/headers/node_api_types.h +59 -0
  131. package/include/third_party/weak-node-api/headers/weak_napi_defines.h +331 -0
  132. package/include/third_party/weak-node-api/headers/weak_napi_undefs.h +282 -0
  133. package/include/weak_napi_defines.h +331 -0
  134. package/include/weak_napi_undefs.h +282 -0
  135. package/package.json +29 -0
@@ -0,0 +1,521 @@
1
+ // Copyright 2025 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ #ifndef PLATFORM_EMBEDDER_PUBLIC_LYNX_EXTENSION_MODULE_H_
6
+ #define PLATFORM_EMBEDDER_PUBLIC_LYNX_EXTENSION_MODULE_H_
7
+
8
+ #include <functional>
9
+ #include <memory>
10
+ #include <utility>
11
+ #include <vector>
12
+
13
+ #include "capi/lynx_extension_module_capi.h"
14
+ #include "capi/lynx_native_module_capi.h"
15
+ #include "lynx_view.h"
16
+ #include "third_party/weak-node-api/headers/napi.h"
17
+ #ifdef USE_WEAK_SUFFIX_NAPI
18
+ #include "third_party/weak-node-api/headers/weak_napi_defines.h"
19
+ #endif
20
+
21
+ namespace lynx {
22
+ namespace pub {
23
+
24
+ using VSyncObserverCallback = std::function<void(int64_t, int64_t)>;
25
+
26
+ class VSyncObserver {
27
+ public:
28
+ VSyncObserver(lynx_vsync_observer_t* c_observer) : c_observer_(c_observer) {}
29
+ virtual ~VSyncObserver() = default;
30
+
31
+ /**
32
+ * @apidoc
33
+ * @brief Set a callback to synchronize with a given VSync.
34
+ * @param id The request instance id.
35
+ * @param callback A functional callback. It is called when the next VSync
36
+ * signal arrives.
37
+ */
38
+ void RequestAnimationFrame(uintptr_t id, VSyncObserverCallback callback) {
39
+ if (!c_observer_) return;
40
+ auto* user_data =
41
+ new std::function<void(int64_t, int64_t)>(std::move(callback));
42
+ lynx_vsync_observer_request_animation_frame(
43
+ c_observer_, id, &RequestAnimationFrameCallback, user_data);
44
+ }
45
+
46
+ /**
47
+ * @apidoc
48
+ * @brief Set a callback to synchronize with a given VSync.
49
+ * @param id The request instance id.
50
+ * @param callback A C-function callback. The callback is called when the next
51
+ * VSync signal arrives.
52
+ * @param user_data The pass-through context.
53
+ */
54
+ inline void RequestAnimationFrame(uintptr_t id,
55
+ vsync_observer_callback callback,
56
+ void* user_data) {
57
+ if (!c_observer_) return;
58
+ lynx_vsync_observer_request_animation_frame(c_observer_, id, callback,
59
+ user_data);
60
+ }
61
+
62
+ /**
63
+ * @apidoc
64
+ * @brief Set a callback to synchronize with a given VSync.
65
+ * @param id The request instance id.
66
+ * @param callback A functional callback. It is called when the next VSync
67
+ * signal arrives. It will be called before any normal callback.
68
+ */
69
+ void RequestBeforeAnimationFrame(uintptr_t id,
70
+ VSyncObserverCallback callback) {
71
+ if (!c_observer_) return;
72
+ auto* user_data =
73
+ new std::function<void(int64_t, int64_t)>(std::move(callback));
74
+ lynx_vsync_observer_request_before_animation_frame(
75
+ c_observer_, id, &RequestAnimationFrameCallback, user_data);
76
+ }
77
+
78
+ /**
79
+ * @apidoc
80
+ * @brief Set a callback to synchronize with a given VSync.
81
+ * @param id The request instance id.
82
+ * @param callback A C-function callback. It is called when the next VSync
83
+ * signal arrives. It will be called before any normal callback.
84
+ * @param user_data The pass-through context.
85
+ */
86
+ inline void RequestBeforeAnimationFrame(uintptr_t id,
87
+ vsync_observer_callback callback,
88
+ void* user_data) {
89
+ if (!c_observer_) return;
90
+ lynx_vsync_observer_request_before_animation_frame(c_observer_, id,
91
+ callback, user_data);
92
+ }
93
+
94
+ /**
95
+ * @apidoc
96
+ * @brief Set a callback to synchronize with a given VSync. It should be
97
+ * called in the BTS thread.
98
+ * @param callback A functional callback. It is called when the next VSync
99
+ * signal arrives. It will be called after each VSync occurs.
100
+ */
101
+ void RegisterAfterAnimationFrameListener(VSyncObserverCallback callback) {
102
+ if (!c_observer_) return;
103
+ after_animation_frame_callbacks_.emplace_back(std::move(callback));
104
+ if (has_registered_) return;
105
+ lynx_vsync_observer_register_after_animation_frame_listener(
106
+ c_observer_, &AfterAnimationFrameListenerCallback, this);
107
+ has_registered_ = true;
108
+ }
109
+
110
+ /**
111
+ * @apidoc
112
+ * @brief Set a callback to synchronize with a given VSync.
113
+ * @param callback A C-function callback. It is called when the next VSync
114
+ * signal arrives. It will be called after each VSync occurs.
115
+ * @param user_data The pass-through context.
116
+ */
117
+ inline void RegisterAfterAnimationFrameListener(
118
+ vsync_observer_callback callback, void* user_data) {
119
+ if (!c_observer_) return;
120
+ lynx_vsync_observer_register_after_animation_frame_listener(
121
+ c_observer_, callback, user_data);
122
+ }
123
+
124
+ private:
125
+ static void RequestAnimationFrameCallback(void* user_data,
126
+ int64_t frame_start_time,
127
+ int64_t frame_end_time) {
128
+ auto* callback =
129
+ reinterpret_cast<std::function<void(int64_t, int64_t)>*>(user_data);
130
+ if (frame_start_time == 0 || frame_end_time == 0) {
131
+ delete callback;
132
+ } else {
133
+ (*callback)(frame_start_time, frame_end_time);
134
+ delete callback;
135
+ }
136
+ }
137
+
138
+ static void AfterAnimationFrameListenerCallback(void* user_data,
139
+ int64_t frame_start_time,
140
+ int64_t frame_end_time) {
141
+ auto* observer = reinterpret_cast<VSyncObserver*>(user_data);
142
+ observer->CallAfterAnimationFrameListenerCallback(frame_start_time,
143
+ frame_end_time);
144
+ }
145
+
146
+ void CallAfterAnimationFrameListenerCallback(int64_t frame_start_time,
147
+ int64_t frame_end_time) {
148
+ for (auto& cb : after_animation_frame_callbacks_) {
149
+ cb(frame_start_time, frame_end_time);
150
+ }
151
+ }
152
+
153
+ lynx_vsync_observer_t* c_observer_;
154
+ std::vector<VSyncObserverCallback> after_animation_frame_callbacks_;
155
+ bool has_registered_ = false;
156
+ };
157
+
158
+ class LynxExtensionModule {
159
+ public:
160
+ LynxExtensionModule() = default;
161
+ virtual ~LynxExtensionModule() = default;
162
+
163
+ /**
164
+ * @apidoc
165
+ * @brief Set up lynx_extension_module_t instance.
166
+ * @param c_module Current LynxView instance.
167
+ */
168
+ void SetCModule(lynx_extension_module_t* c_module) {
169
+ c_module_ = c_module;
170
+ BindFunction();
171
+ }
172
+
173
+ /**
174
+ * @apidoc
175
+ * @brief Called when LynxView instance is created. It is always called on the
176
+ * UI thread.
177
+ * @param lynx_view Current LynxView instance.
178
+ */
179
+ virtual void OnLynxViewCreate(lynx_view_t* lynx_view) {}
180
+ /**
181
+ * @apidoc
182
+ * @brief Called when LynxView instance will be destroyed. It is always called
183
+ * on the UI thread.
184
+ */
185
+ virtual void OnLynxViewDestroy() {}
186
+ /**
187
+ * @apidoc
188
+ * @brief Called when BTS Runtime instance is created. It is always called
189
+ * on the UI thread.
190
+ */
191
+ virtual void OnRuntimeInit() {}
192
+ /**
193
+ * @apidoc
194
+ * @brief Called when BTS runtime is attached. It is always called on the
195
+ * BTS thread.
196
+ * @param env The Napi environment.
197
+ * @param vsync_observer The VSyncObserver instance. It will be invalid when
198
+ * LynxExtensionModule is Destroyed.
199
+ */
200
+ virtual void OnRuntimeAttach(Napi::Env env,
201
+ std::unique_ptr<VSyncObserver> vsync_observer) {}
202
+ /**
203
+ * @apidoc
204
+ * @brief Called when BTS Runtime is ready. It is always called on the
205
+ * BTS thread.
206
+ * @param env The Napi environment.
207
+ * @param lynx The lynx object in BTS.
208
+ * @param url The url of the current LynxView.
209
+ */
210
+ virtual void OnRuntimeReady(Napi::Env env, Napi::Value lynx,
211
+ const char* url) {}
212
+ /**
213
+ * @apidoc
214
+ * @brief Called when BTS Runtime is detached. It is always called on the
215
+ * BTS thread.
216
+ */
217
+ virtual void OnRuntimeDetach() {}
218
+ /**
219
+ * @apidoc
220
+ * @brief Called when Application did enter foreground. It is always called on
221
+ * the UI thread.
222
+ */
223
+ virtual void OnEnterForeground() {}
224
+ /**
225
+ * @apidoc
226
+ * @brief Called when Application did enter background. It is always called on
227
+ * the UI thread.
228
+ */
229
+ virtual void OnEnterBackground() {}
230
+ /**
231
+ * @apidoc
232
+ * @brief Called when LynxExtensionModule instance will be destroyed. It is
233
+ * always called on the BTS thread.
234
+ */
235
+ virtual void Destroy() {}
236
+
237
+ /**
238
+ * @apidoc
239
+ * @brief Post task to BTS Thread.
240
+ * @param task A functional task.
241
+ */
242
+ void PostTaskToRuntime(std::function<void()> task) {
243
+ auto* user_data = new std::function<void()>(std::move(task));
244
+ lynx_extension_module_post_task_to_runtime(c_module_, &PostTaskCallback,
245
+ user_data);
246
+ }
247
+
248
+ /**
249
+ * @apidoc
250
+ * @brief Post task to BTS Thread.
251
+ * @param task A c-function task.
252
+ * @param user_data The pass-through context.
253
+ */
254
+ inline void PostTaskToRuntime(lynx_extension_module_post_task_func func,
255
+ void* user_data) {
256
+ lynx_extension_module_post_task_to_runtime(c_module_, func, user_data);
257
+ }
258
+
259
+ /**
260
+ * @apidoc
261
+ * @brief Is running tasks on current thread.
262
+ */
263
+ inline bool IsRunningTasksOnBTSThread() {
264
+ return lynx_extension_module_is_running_on_bts_thread(c_module_);
265
+ }
266
+
267
+ /**
268
+ * @apidoc
269
+ * @brief Set a napi_module creator to bind custom native methods.
270
+ *
271
+ * The module type T is automatically deduced from the creator function's
272
+ * fourth argument.
273
+ *
274
+ * @param creator The creator function/functor with signature:
275
+ * Napi::Value(Napi::Env, Napi::Value, const char*, T&)
276
+ * - env: The Napi environment
277
+ * - exports: The exports object to populate
278
+ * - module_name: The name of the module
279
+ * - module: The LynxExtensionModule instance (of type T)
280
+ * @return The modified exports object
281
+ */
282
+ template <typename Creator>
283
+ inline void SetNapiModuleCreator(Creator&& creator) {
284
+ using ModuleType = typename std::remove_reference<
285
+ typename ArgumentTypeDeducer<std::decay_t<Creator>>::ModuleType>::type;
286
+ static_assert(std::is_base_of<LynxExtensionModule, ModuleType>::value,
287
+ "Module type must inherit from LynxExtensionModule");
288
+
289
+ napi_module_creator_ =
290
+ [creator_holder = std::forward<Creator>(creator)](
291
+ Napi::Env env, Napi::Value exports, const char* module_name,
292
+ LynxExtensionModule& module) mutable -> Napi::Value {
293
+ return creator_holder(env, exports, module_name,
294
+ static_cast<ModuleType&>(module));
295
+ };
296
+ lynx_extension_module_set_napi_module_creator(c_module_,
297
+ &NapiModuleCreatorCallback);
298
+ }
299
+
300
+ /**
301
+ * @apidoc
302
+ * @brief Stores a data with key to the global map with C++ finalizer.
303
+ * @param env The Napi environment.
304
+ * @param key The key to identify the data.
305
+ * @param data The data to store.
306
+ * @param finalize_cb The C++ finalizer callback. It must be a callable object
307
+ * (e.g., function pointer, lambda, functor) with the
308
+ * signature: void(Napi::Env, void* data, void* hint).
309
+ * @param finalize_hint The hint for the finalizer.
310
+ */
311
+ template <typename Finalizer>
312
+ inline static void SetNapiInstanceData(Napi::Env env, uint64_t key,
313
+ void* data, Finalizer finalize_cb,
314
+ void* finalize_hint) {
315
+ auto* wrapper =
316
+ new FinalizerWrapper<Finalizer>{std::move(finalize_cb), finalize_hint};
317
+ lynx_napi_set_instance_data(static_cast<napi_env>(env), key, data,
318
+ &FinalizerWrapper<Finalizer>::Finalize,
319
+ wrapper);
320
+ }
321
+
322
+ /**
323
+ * @apidoc
324
+ * @brief Gets the data with key from the global map.
325
+ * @param env The Napi environment.
326
+ * @param key The key to identify the data.
327
+ * @param data The pointer to receive the data.
328
+ */
329
+ inline static void GetNapiInstanceData(Napi::Env env, uint64_t key,
330
+ void** data) {
331
+ lynx_napi_get_instance_data(static_cast<napi_env>(env), key, data);
332
+ }
333
+
334
+ inline void Retain() { lynx_extension_module_ref(c_module_); }
335
+ inline void Release() { lynx_extension_module_unref(c_module_); }
336
+
337
+ protected:
338
+ using NapiModuleCreator = std::function<Napi::Value(
339
+ Napi::Env, Napi::Value, const char*, LynxExtensionModule&)>;
340
+
341
+ lynx_extension_module_t* c_module_ = nullptr;
342
+ NapiModuleCreator napi_module_creator_ = nullptr;
343
+
344
+ private:
345
+ template <typename T>
346
+ struct ArgumentTypeDeducer;
347
+
348
+ // Specialization for function pointers
349
+ template <typename Ret, typename Arg1, typename Arg2, typename Arg3,
350
+ typename Arg4>
351
+ struct ArgumentTypeDeducer<Ret (*)(Arg1, Arg2, Arg3, Arg4)> {
352
+ using ModuleType = Arg4;
353
+ };
354
+
355
+ // Specialization for functors/lambdas (via operator())
356
+ template <typename T>
357
+ struct ArgumentTypeDeducer : ArgumentTypeDeducer<decltype(&T::operator())> {};
358
+
359
+ // Specialization for const member functions (lambdas)
360
+ template <typename C, typename Ret, typename Arg1, typename Arg2,
361
+ typename Arg3, typename Arg4>
362
+ struct ArgumentTypeDeducer<Ret (C::*)(Arg1, Arg2, Arg3, Arg4) const> {
363
+ using ModuleType = Arg4;
364
+ };
365
+
366
+ // Specialization for mutable member functions (mutable lambdas)
367
+ template <typename C, typename Ret, typename Arg1, typename Arg2,
368
+ typename Arg3, typename Arg4>
369
+ struct ArgumentTypeDeducer<Ret (C::*)(Arg1, Arg2, Arg3, Arg4)> {
370
+ using ModuleType = Arg4;
371
+ };
372
+
373
+ template <typename Finalizer>
374
+ struct FinalizerWrapper {
375
+ Finalizer finalizer;
376
+ void* hint;
377
+
378
+ FinalizerWrapper(Finalizer fin, void* h)
379
+ : finalizer(std::move(fin)), hint(h) {}
380
+
381
+ static void Finalize(napi_env env, void* finalize_data,
382
+ void* finalize_hint) {
383
+ auto* wrapper = static_cast<FinalizerWrapper*>(finalize_hint);
384
+ wrapper->finalizer(Napi::Env(env), finalize_data, wrapper->hint);
385
+ delete wrapper;
386
+ }
387
+ };
388
+
389
+ static napi_value NapiModuleCreatorCallback(napi_env env, napi_value exports,
390
+ const char* module_name,
391
+ void* opaque) {
392
+ auto* extension_module = reinterpret_cast<LynxExtensionModule*>(opaque);
393
+ if (extension_module->napi_module_creator_) {
394
+ return extension_module->napi_module_creator_(
395
+ Napi::Env(env), Napi::Value(env, exports), module_name,
396
+ *extension_module);
397
+ }
398
+ return exports;
399
+ }
400
+
401
+ void BindFunction() {
402
+ // on lynx_view create
403
+ lynx_extension_module_bind_lynx_view_create(
404
+ c_module_,
405
+ [](lynx_extension_module_t* c_module, lynx_view_t* lynx_view) {
406
+ auto* user_data = lynx_extension_module_get_user_data(c_module);
407
+ if (!user_data) {
408
+ return;
409
+ }
410
+ auto* extension_module =
411
+ reinterpret_cast<LynxExtensionModule*>(user_data);
412
+ extension_module->OnLynxViewCreate(lynx_view);
413
+ });
414
+ // on lynx_view destroy
415
+ lynx_extension_module_bind_lynx_view_destroy(
416
+ c_module_, [](lynx_extension_module_t* c_module) {
417
+ auto* user_data = lynx_extension_module_get_user_data(c_module);
418
+ if (!user_data) {
419
+ return;
420
+ }
421
+ auto* extension_module =
422
+ reinterpret_cast<LynxExtensionModule*>(user_data);
423
+ extension_module->OnLynxViewDestroy();
424
+ });
425
+ // on runtime init
426
+ lynx_extension_module_bind_runtime_init(
427
+ c_module_, [](lynx_extension_module_t* c_module) {
428
+ auto* user_data = lynx_extension_module_get_user_data(c_module);
429
+ if (!user_data) {
430
+ return;
431
+ }
432
+ auto* extension_module =
433
+ reinterpret_cast<LynxExtensionModule*>(user_data);
434
+ extension_module->OnRuntimeInit();
435
+ });
436
+ // on runtime attach
437
+ lynx_extension_module_bind_runtime_attach(
438
+ c_module_, [](lynx_extension_module_t* c_module, napi_env env,
439
+ lynx_vsync_observer_t* observer) {
440
+ auto* user_data = lynx_extension_module_get_user_data(c_module);
441
+ if (!user_data) {
442
+ return;
443
+ }
444
+ auto* extension_module =
445
+ reinterpret_cast<LynxExtensionModule*>(user_data);
446
+ extension_module->OnRuntimeAttach(
447
+ env, std::make_unique<VSyncObserver>(observer));
448
+ });
449
+ // on runtime ready
450
+ lynx_extension_module_bind_runtime_ready(
451
+ c_module_, [](lynx_extension_module_t* c_module, napi_env env,
452
+ napi_value lynx, const char* url) {
453
+ auto* user_data = lynx_extension_module_get_user_data(c_module);
454
+ if (!user_data) {
455
+ return;
456
+ }
457
+ auto* extension_module =
458
+ reinterpret_cast<LynxExtensionModule*>(user_data);
459
+ extension_module->OnRuntimeReady(env, Napi::Value(env, lynx), url);
460
+ });
461
+ // on runtime detach
462
+ lynx_extension_module_bind_runtime_detach(
463
+ c_module_, [](lynx_extension_module_t* c_module) {
464
+ auto* user_data = lynx_extension_module_get_user_data(c_module);
465
+ if (!user_data) {
466
+ return;
467
+ }
468
+ auto* extension_module =
469
+ reinterpret_cast<LynxExtensionModule*>(user_data);
470
+ extension_module->OnRuntimeDetach();
471
+ });
472
+ // on enter foreground
473
+ lynx_extension_module_bind_enter_foreground(
474
+ c_module_, [](lynx_extension_module_t* c_module) {
475
+ auto* user_data = lynx_extension_module_get_user_data(c_module);
476
+ if (!user_data) {
477
+ return;
478
+ }
479
+ auto* extension_module =
480
+ reinterpret_cast<LynxExtensionModule*>(user_data);
481
+ extension_module->OnEnterForeground();
482
+ });
483
+ // on enter background
484
+ lynx_extension_module_bind_enter_background(
485
+ c_module_, [](lynx_extension_module_t* c_module) {
486
+ auto* user_data = lynx_extension_module_get_user_data(c_module);
487
+ if (!user_data) {
488
+ return;
489
+ }
490
+ auto* extension_module =
491
+ reinterpret_cast<LynxExtensionModule*>(user_data);
492
+ extension_module->OnEnterBackground();
493
+ });
494
+ // destroy
495
+ lynx_extension_module_bind_on_destroy(
496
+ c_module_, [](lynx_extension_module_t* c_module) {
497
+ auto* user_data = lynx_extension_module_get_user_data(c_module);
498
+ if (!user_data) {
499
+ return;
500
+ }
501
+ auto* extension_module =
502
+ reinterpret_cast<LynxExtensionModule*>(user_data);
503
+ extension_module->Destroy();
504
+ });
505
+ }
506
+
507
+ static void PostTaskCallback(void* user_data) {
508
+ auto* task = reinterpret_cast<std::function<void()>*>(user_data);
509
+ (*task)();
510
+ delete task;
511
+ }
512
+ };
513
+
514
+ } // namespace pub
515
+ } // namespace lynx
516
+
517
+ #ifdef USE_WEAK_SUFFIX_NAPI
518
+ #include "third_party/weak-node-api/headers/weak_napi_undefs.h"
519
+ #endif
520
+
521
+ #endif // PLATFORM_EMBEDDER_PUBLIC_LYNX_EXTENSION_MODULE_H_
@@ -0,0 +1,178 @@
1
+ // Copyright 2025 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ #ifndef PLATFORM_EMBEDDER_PUBLIC_LYNX_GENERIC_RESOURCE_FETCHER_H_
5
+ #define PLATFORM_EMBEDDER_PUBLIC_LYNX_GENERIC_RESOURCE_FETCHER_H_
6
+
7
+ #include <memory>
8
+ #include <string>
9
+
10
+ #include "capi/lynx_generic_resource_fetcher_capi.h"
11
+ #include "capi/lynx_memory_capi.h"
12
+ #include "lynx_resource_request.h"
13
+ #include "lynx_resource_response.h"
14
+
15
+ namespace lynx {
16
+ namespace pub {
17
+
18
+ /**
19
+ * @apidoc
20
+ * @brief `LynxGenericResourceFetcher` is defined inside `LynxEngine`
21
+ * and injected from outside to implement a general resource loading interface.
22
+ */
23
+ class LynxGenericResourceFetcher
24
+ : public std::enable_shared_from_this<LynxGenericResourceFetcher> {
25
+ public:
26
+ LynxGenericResourceFetcher() : fetcher_(nullptr) {}
27
+
28
+ void InitIfNeeded() {
29
+ if (fetcher_) {
30
+ return;
31
+ }
32
+ fetcher_ = lynx_generic_resource_fetcher_create_with_finalizer(
33
+ new std::weak_ptr<LynxGenericResourceFetcher>(weak_from_this()),
34
+ [](lynx_generic_resource_fetcher_t* fetcher, void* user_data) {
35
+ std::weak_ptr<LynxGenericResourceFetcher>* weak_ptr =
36
+ reinterpret_cast<std::weak_ptr<LynxGenericResourceFetcher>*>(
37
+ user_data);
38
+ delete weak_ptr;
39
+ });
40
+ lynx_generic_resource_fetcher_bind_fetch_resource(
41
+ fetcher_, [](lynx_generic_resource_fetcher_t* fetcher,
42
+ lynx_resource_request_t* request,
43
+ lynx_resource_response_t* response) {
44
+ auto req = std::make_shared<resource::LynxResourceRequest>(request);
45
+ auto resp =
46
+ std::make_shared<resource::LynxResourceResponse>(response);
47
+ std::weak_ptr<LynxGenericResourceFetcher>* weak_ptr =
48
+ reinterpret_cast<std::weak_ptr<LynxGenericResourceFetcher>*>(
49
+ lynx_generic_resource_fetcher_get_user_data(fetcher));
50
+ std::shared_ptr<LynxGenericResourceFetcher> shared_fetcher =
51
+ weak_ptr ? weak_ptr->lock() : nullptr;
52
+ if (!shared_fetcher) {
53
+ resp->SetCode(-1);
54
+ resp->SetErrorMessage("resource fetcher is destroyed");
55
+ } else {
56
+ shared_fetcher->FetchResource(req, resp);
57
+ }
58
+ });
59
+ lynx_generic_resource_fetcher_bind_fetch_resource_path(
60
+ fetcher_, [](lynx_generic_resource_fetcher_t* fetcher,
61
+ lynx_resource_request_t* request,
62
+ lynx_resource_response_t* response) {
63
+ auto req = std::make_shared<resource::LynxResourceRequest>(request);
64
+ auto resp =
65
+ std::make_shared<resource::LynxResourceResponse>(response);
66
+ std::weak_ptr<LynxGenericResourceFetcher>* weak_ptr =
67
+ reinterpret_cast<std::weak_ptr<LynxGenericResourceFetcher>*>(
68
+ lynx_generic_resource_fetcher_get_user_data(fetcher));
69
+ std::shared_ptr<LynxGenericResourceFetcher> shared_fetcher =
70
+ weak_ptr ? weak_ptr->lock() : nullptr;
71
+ if (!shared_fetcher) {
72
+ resp->SetCode(-1);
73
+ resp->SetErrorMessage("resource fetcher is destroyed");
74
+ } else {
75
+ shared_fetcher->FetchResourcePath(req, resp);
76
+ }
77
+ });
78
+
79
+ lynx_generic_resource_fetcher_bind_cancel_fetch(
80
+ fetcher_, [](lynx_generic_resource_fetcher_t* fetcher,
81
+ lynx_resource_request_id request_id) {
82
+ std::weak_ptr<LynxGenericResourceFetcher>* weak_ptr =
83
+ reinterpret_cast<std::weak_ptr<LynxGenericResourceFetcher>*>(
84
+ lynx_generic_resource_fetcher_get_user_data(fetcher));
85
+ std::shared_ptr<LynxGenericResourceFetcher> shared_fetcher =
86
+ weak_ptr ? weak_ptr->lock() : nullptr;
87
+ if (shared_fetcher) {
88
+ shared_fetcher->Cancel(request_id);
89
+ }
90
+ });
91
+ lynx_generic_resource_fetcher_bind_intercept_func(
92
+ fetcher_,
93
+ [](const char* url, bool should_decode, void* user_data) -> char* {
94
+ if (!url) {
95
+ return nullptr;
96
+ }
97
+ std::weak_ptr<LynxGenericResourceFetcher>* weak_ptr =
98
+ reinterpret_cast<std::weak_ptr<LynxGenericResourceFetcher>*>(
99
+ user_data);
100
+ std::shared_ptr<LynxGenericResourceFetcher> shared_fetcher =
101
+ weak_ptr ? weak_ptr->lock() : nullptr;
102
+ if (!shared_fetcher) {
103
+ return nullptr;
104
+ }
105
+ std::string intercepted_url =
106
+ shared_fetcher->InterceptUrl(url, should_decode);
107
+ if (intercepted_url.empty()) {
108
+ return nullptr;
109
+ }
110
+ return lynx_strdup(intercepted_url.c_str());
111
+ });
112
+ }
113
+
114
+ virtual ~LynxGenericResourceFetcher() {
115
+ if (fetcher_) {
116
+ lynx_generic_resource_fetcher_release(fetcher_);
117
+ }
118
+ }
119
+ /**
120
+ * @apidoc
121
+ * @brief This function is designed to fetch a resource. It's designed to be
122
+ * called potentially from the UI thread. To avoid blocking the UI and ensure
123
+ * smooth user experience, developers are expected to override this method and
124
+ * post the actual resource fetching task to an IO thread.
125
+ * @param request The resource request object.
126
+ * @param response The resource response object.
127
+ */
128
+ virtual void FetchResource(
129
+ std::shared_ptr<resource::LynxResourceRequest> request,
130
+ std::shared_ptr<resource::LynxResourceResponse> response) {}
131
+
132
+ /**
133
+ * @apidoc
134
+ * @brief This function is designed to fetch a resource path. It's designed to
135
+ * be called potentially from the UI thread. To avoid blocking the UI and
136
+ * ensure smooth user experience, developers are expected to override this
137
+ * method and post the actual resource fetching task to an IO thread.
138
+ * @param request The resource request object.
139
+ * @param response The resource response object.
140
+ */
141
+ virtual void FetchResourcePath(
142
+ std::shared_ptr<resource::LynxResourceRequest> request,
143
+ std::shared_ptr<resource::LynxResourceResponse> response) {}
144
+
145
+ /**
146
+ * @apidoc
147
+ * @brief Cancels a resource request with the specified ID. This virtual
148
+ * method provides a way to cancel an ongoing resource request identified by
149
+ * the given ID. The default implementation does nothing, and derived classes
150
+ * should override this method to implement the actual cancellation logic.
151
+ * @param request_id The ID of the resource request to be canceled.
152
+ */
153
+ virtual void Cancel(lynx_resource_request_id request_id) {}
154
+
155
+ /**
156
+ * @apidoc
157
+ * @brief Intercepts the input resource URL before it is fetched.
158
+ * Derived classes can override this method to rewrite the original URL.
159
+ * @param origin_url The original resource URL.
160
+ * @param should_decode Whether the returned URL should be decoded.
161
+ * @return The intercepted URL. The default implementation returns the
162
+ * original URL unchanged.
163
+ */
164
+ virtual std::string InterceptUrl(const std::string& origin_url,
165
+ bool should_decode) {
166
+ return origin_url;
167
+ }
168
+
169
+ lynx_generic_resource_fetcher_t* Impl() { return fetcher_; }
170
+
171
+ private:
172
+ lynx_generic_resource_fetcher_t* fetcher_ = nullptr;
173
+ };
174
+
175
+ } // namespace pub
176
+ } // namespace lynx
177
+
178
+ #endif // PLATFORM_EMBEDDER_PUBLIC_LYNX_GENERIC_RESOURCE_FETCHER_H_