1 |
|
|
#ifndef SRC_NODE_MESSAGING_H_ |
2 |
|
|
#define SRC_NODE_MESSAGING_H_ |
3 |
|
|
|
4 |
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
5 |
|
|
|
6 |
|
|
#include "env.h" |
7 |
|
|
#include "node_mutex.h" |
8 |
|
|
#include "v8.h" |
9 |
|
|
#include <deque> |
10 |
|
|
#include <string> |
11 |
|
|
#include <unordered_map> |
12 |
|
|
#include <set> |
13 |
|
|
|
14 |
|
|
namespace node { |
15 |
|
|
namespace worker { |
16 |
|
|
|
17 |
|
|
class MessagePortData; |
18 |
|
|
class MessagePort; |
19 |
|
|
|
20 |
|
|
typedef MaybeStackBuffer<v8::Local<v8::Value>, 8> TransferList; |
21 |
|
|
|
22 |
|
|
// Used to represent the in-flight structure of an object that is being |
23 |
|
|
// transferred or cloned using postMessage(). |
24 |
|
67005 |
class TransferData : public MemoryRetainer { |
25 |
|
|
public: |
26 |
|
|
// Deserialize this object on the receiving end after a .postMessage() call. |
27 |
|
|
// - `context` may not be the same as `env->context()`. This method should |
28 |
|
|
// not produce JS objects coming from Contexts other than `context`. |
29 |
|
|
// - `self` is a unique_ptr for the object that this is being called on. |
30 |
|
|
// - The return value is treated like a `Maybe`, i.e. if `nullptr` is |
31 |
|
|
// returned, any further deserialization of the message is stopped and |
32 |
|
|
// control is returned to the event loop or JS as soon as possible. |
33 |
|
|
virtual BaseObjectPtr<BaseObject> Deserialize( |
34 |
|
|
Environment* env, |
35 |
|
|
v8::Local<v8::Context> context, |
36 |
|
|
std::unique_ptr<TransferData> self) = 0; |
37 |
|
|
// FinalizeTransferWrite() is the counterpart to |
38 |
|
|
// BaseObject::FinalizeTransferRead(). It is called right after the transfer |
39 |
|
|
// data was created, and defaults to doing nothing. After this function, |
40 |
|
|
// this object should not hold any more Isolate-specific data. |
41 |
|
|
virtual v8::Maybe<bool> FinalizeTransferWrite( |
42 |
|
|
v8::Local<v8::Context> context, v8::ValueSerializer* serializer); |
43 |
|
|
}; |
44 |
|
|
|
45 |
|
|
// Represents a single communication message. |
46 |
|
|
class Message : public MemoryRetainer { |
47 |
|
|
public: |
48 |
|
|
// Create a Message with a specific underlying payload, in the format of the |
49 |
|
|
// V8 ValueSerializer API. If `payload` is empty, this message indicates |
50 |
|
|
// that the receiving message port should close itself. |
51 |
|
|
explicit Message(MallocedBuffer<char>&& payload = MallocedBuffer<char>()); |
52 |
|
78731 |
~Message() = default; |
53 |
|
|
|
54 |
|
|
Message(Message&& other) = default; |
55 |
|
|
Message& operator=(Message&& other) = default; |
56 |
|
|
Message& operator=(const Message&) = delete; |
57 |
|
|
Message(const Message&) = delete; |
58 |
|
|
|
59 |
|
|
// Whether this is a message indicating that the port is to be closed. |
60 |
|
|
// This is the last message to be received by a MessagePort. |
61 |
|
|
bool IsCloseMessage() const; |
62 |
|
|
|
63 |
|
|
// Deserialize the contained JS value. May only be called once, and only |
64 |
|
|
// after Serialize() has been called (e.g. by another thread). |
65 |
|
|
v8::MaybeLocal<v8::Value> Deserialize(Environment* env, |
66 |
|
|
v8::Local<v8::Context> context); |
67 |
|
|
|
68 |
|
|
// Serialize a JS value, and optionally transfer objects, into this message. |
69 |
|
|
// The Message object retains ownership of all transferred objects until |
70 |
|
|
// deserialization. |
71 |
|
|
// The source_port parameter, if provided, will make Serialize() throw a |
72 |
|
|
// "DataCloneError" DOMException if source_port is found in transfer_list. |
73 |
|
|
v8::Maybe<bool> Serialize(Environment* env, |
74 |
|
|
v8::Local<v8::Context> context, |
75 |
|
|
v8::Local<v8::Value> input, |
76 |
|
|
const TransferList& transfer_list, |
77 |
|
|
v8::Local<v8::Object> source_port = |
78 |
|
|
v8::Local<v8::Object>()); |
79 |
|
|
|
80 |
|
|
// Internal method of Message that is called when a new SharedArrayBuffer |
81 |
|
|
// object is encountered in the incoming value's structure. |
82 |
|
|
void AddSharedArrayBuffer(std::shared_ptr<v8::BackingStore> backing_store); |
83 |
|
|
// Internal method of Message that is called once serialization finishes |
84 |
|
|
// and that transfers ownership of `data` to this message. |
85 |
|
|
void AddTransferable(std::unique_ptr<TransferData>&& data); |
86 |
|
|
// Internal method of Message that is called when a new WebAssembly.Module |
87 |
|
|
// object is encountered in the incoming value's structure. |
88 |
|
|
uint32_t AddWASMModule(v8::CompiledWasmModule&& mod); |
89 |
|
|
|
90 |
|
|
// The host objects that will be transferred, as recorded by Serialize() |
91 |
|
|
// (e.g. MessagePorts). |
92 |
|
|
// Used for warning user about posting the target MessagePort to itself, |
93 |
|
|
// which will as a side effect destroy the communication channel. |
94 |
|
44874 |
const std::vector<std::unique_ptr<TransferData>>& transferables() const { |
95 |
|
44874 |
return transferables_; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
99 |
|
|
|
100 |
|
|
SET_MEMORY_INFO_NAME(Message) |
101 |
|
|
SET_SELF_SIZE(Message) |
102 |
|
|
|
103 |
|
|
private: |
104 |
|
|
MallocedBuffer<char> main_message_buf_; |
105 |
|
|
std::vector<std::shared_ptr<v8::BackingStore>> array_buffers_; |
106 |
|
|
std::vector<std::shared_ptr<v8::BackingStore>> shared_array_buffers_; |
107 |
|
|
std::vector<std::unique_ptr<TransferData>> transferables_; |
108 |
|
|
std::vector<v8::CompiledWasmModule> wasm_modules_; |
109 |
|
|
|
110 |
|
|
friend class MessagePort; |
111 |
|
|
}; |
112 |
|
|
|
113 |
|
|
class SiblingGroup { |
114 |
|
|
public: |
115 |
|
|
// Named SiblingGroup, Used for one-to-many BroadcastChannels. |
116 |
|
|
static std::shared_ptr<SiblingGroup> Get(const std::string& name); |
117 |
|
|
|
118 |
|
|
// Anonymous SiblingGroup, Used for one-to-one MessagePort pairs. |
119 |
|
11345 |
SiblingGroup() = default; |
120 |
|
|
explicit SiblingGroup(const std::string& name); |
121 |
|
|
~SiblingGroup(); |
122 |
|
|
|
123 |
|
|
// Dispatches the Message to the collection of associated |
124 |
|
|
// ports. If there is more than one destination port and |
125 |
|
|
// the Message contains transferables, Dispatch will fail. |
126 |
|
|
// Returns Just(true) if successful and the message was |
127 |
|
|
// dispatched to at least one destination. Returns Just(false) |
128 |
|
|
// if there were no destinations. Returns Nothing<bool>() |
129 |
|
|
// if there was an error. If error is not nullptr, it will |
130 |
|
|
// be set to an error message or warning message as appropriate. |
131 |
|
|
v8::Maybe<bool> Dispatch( |
132 |
|
|
MessagePortData* source, |
133 |
|
|
std::shared_ptr<Message> message, |
134 |
|
|
std::string* error = nullptr); |
135 |
|
|
|
136 |
|
|
void Entangle(MessagePortData* data); |
137 |
|
|
|
138 |
|
|
void Disentangle(MessagePortData* data); |
139 |
|
|
|
140 |
|
|
const std::string& name() const { return name_; } |
141 |
|
|
|
142 |
|
112493 |
size_t size() const { return ports_.size(); } |
143 |
|
|
|
144 |
|
|
private: |
145 |
|
|
std::string name_; |
146 |
|
|
std::set<MessagePortData*> ports_; |
147 |
|
|
Mutex group_mutex_; |
148 |
|
|
|
149 |
|
|
static void CheckSiblingGroup(const std::string& name); |
150 |
|
|
|
151 |
|
|
using Map = |
152 |
|
|
std::unordered_map<std::string, std::weak_ptr<SiblingGroup>>; |
153 |
|
|
|
154 |
|
|
static Mutex groups_mutex_; |
155 |
|
|
static Map groups_; |
156 |
|
|
}; |
157 |
|
|
|
158 |
|
|
// This contains all data for a `MessagePort` instance that is not tied to |
159 |
|
|
// a specific Environment/Isolate/event loop, for easier transfer between those. |
160 |
|
|
class MessagePortData : public TransferData { |
161 |
|
|
public: |
162 |
|
|
explicit MessagePortData( |
163 |
|
|
MessagePort* owner, |
164 |
|
|
const std::string& name = std::string()); |
165 |
|
|
~MessagePortData() override; |
166 |
|
|
|
167 |
|
|
MessagePortData(MessagePortData&& other) = delete; |
168 |
|
|
MessagePortData& operator=(MessagePortData&& other) = delete; |
169 |
|
|
MessagePortData(const MessagePortData& other) = delete; |
170 |
|
|
MessagePortData& operator=(const MessagePortData& other) = delete; |
171 |
|
|
|
172 |
|
|
// Add a message to the incoming queue and notify the receiver. |
173 |
|
|
// This may be called from any thread. |
174 |
|
|
void AddToIncomingQueue(std::shared_ptr<Message> message); |
175 |
|
|
v8::Maybe<bool> Dispatch( |
176 |
|
|
std::shared_ptr<Message> message, |
177 |
|
|
std::string* error = nullptr); |
178 |
|
|
|
179 |
|
|
// Turns `a` and `b` into siblings, i.e. connects the sending side of one |
180 |
|
|
// to the receiving side of the other. This is not thread-safe. |
181 |
|
|
static void Entangle(MessagePortData* a, MessagePortData* b); |
182 |
|
|
|
183 |
|
|
// Removes any possible sibling. This is thread-safe (it acquires both |
184 |
|
|
// `sibling_mutex_` and `mutex_`), and has to be because it is called once |
185 |
|
|
// the corresponding JS handle handle wants to close |
186 |
|
|
// which can happen on either side of a worker. |
187 |
|
|
void Disentangle(); |
188 |
|
|
|
189 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
190 |
|
|
BaseObjectPtr<BaseObject> Deserialize( |
191 |
|
|
Environment* env, |
192 |
|
|
v8::Local<v8::Context> context, |
193 |
|
|
std::unique_ptr<TransferData> self) override; |
194 |
|
|
|
195 |
|
2 |
SET_MEMORY_INFO_NAME(MessagePortData) |
196 |
|
2 |
SET_SELF_SIZE(MessagePortData) |
197 |
|
|
|
198 |
|
|
private: |
199 |
|
|
// This mutex protects all fields below it, with the exception of |
200 |
|
|
// sibling_. |
201 |
|
|
mutable Mutex mutex_; |
202 |
|
|
std::deque<std::shared_ptr<Message>> incoming_messages_; |
203 |
|
|
MessagePort* owner_ = nullptr; |
204 |
|
|
std::shared_ptr<SiblingGroup> group_; |
205 |
|
|
friend class MessagePort; |
206 |
|
|
}; |
207 |
|
|
|
208 |
|
|
// A message port that receives messages from other threads, including |
209 |
|
|
// the uv_async_t handle that is used to notify the current event loop of |
210 |
|
|
// new incoming messages. |
211 |
|
|
class MessagePort : public HandleWrap { |
212 |
|
|
private: |
213 |
|
|
// Create a new MessagePort. The `context` argument specifies the Context |
214 |
|
|
// instance that is used for creating the values emitted from this port. |
215 |
|
|
// This is called by MessagePort::New(), which is the public API used for |
216 |
|
|
// creating MessagePort instances. |
217 |
|
|
MessagePort(Environment* env, |
218 |
|
|
v8::Local<v8::Context> context, |
219 |
|
|
v8::Local<v8::Object> wrap, |
220 |
|
|
const std::string& name = std::string()); |
221 |
|
|
|
222 |
|
|
public: |
223 |
|
|
~MessagePort() override; |
224 |
|
|
|
225 |
|
|
// Create a new message port instance, optionally over an existing |
226 |
|
|
// `MessagePortData` object. |
227 |
|
|
static MessagePort* New(Environment* env, |
228 |
|
|
v8::Local<v8::Context> context, |
229 |
|
|
std::unique_ptr<MessagePortData> data = nullptr, |
230 |
|
|
const std::string& name = std::string()); |
231 |
|
|
|
232 |
|
|
// Send a message, i.e. deliver it into the sibling's incoming queue. |
233 |
|
|
// If this port is closed, or if there is no sibling, this message is |
234 |
|
|
// serialized with transfers, then silently discarded. |
235 |
|
|
v8::Maybe<bool> PostMessage(Environment* env, |
236 |
|
|
v8::Local<v8::Value> message, |
237 |
|
|
const TransferList& transfer); |
238 |
|
|
|
239 |
|
|
// Start processing messages on this port as a receiving end. |
240 |
|
|
void Start(); |
241 |
|
|
// Stop processing messages on this port as a receiving end. |
242 |
|
|
void Stop(); |
243 |
|
|
|
244 |
|
|
/* constructor */ |
245 |
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args); |
246 |
|
|
/* prototype methods */ |
247 |
|
|
static void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args); |
248 |
|
|
static void Start(const v8::FunctionCallbackInfo<v8::Value>& args); |
249 |
|
|
static void Stop(const v8::FunctionCallbackInfo<v8::Value>& args); |
250 |
|
|
static void CheckType(const v8::FunctionCallbackInfo<v8::Value>& args); |
251 |
|
|
static void Drain(const v8::FunctionCallbackInfo<v8::Value>& args); |
252 |
|
|
static void ReceiveMessage(const v8::FunctionCallbackInfo<v8::Value>& args); |
253 |
|
|
|
254 |
|
|
/* static */ |
255 |
|
|
static void MoveToContext(const v8::FunctionCallbackInfo<v8::Value>& args); |
256 |
|
|
|
257 |
|
|
// Turns `a` and `b` into siblings, i.e. connects the sending side of one |
258 |
|
|
// to the receiving side of the other. This is not thread-safe. |
259 |
|
|
static void Entangle(MessagePort* a, MessagePort* b); |
260 |
|
|
static void Entangle(MessagePort* a, MessagePortData* b); |
261 |
|
|
|
262 |
|
|
// Detach this port's data for transferring. After this, the MessagePortData |
263 |
|
|
// is no longer associated with this handle, although it can still receive |
264 |
|
|
// messages. |
265 |
|
|
std::unique_ptr<MessagePortData> Detach(); |
266 |
|
|
|
267 |
|
|
void Close( |
268 |
|
|
v8::Local<v8::Value> close_callback = v8::Local<v8::Value>()) override; |
269 |
|
|
|
270 |
|
|
// Returns true if either data_ has been freed, or if the handle is being |
271 |
|
|
// closed. Equivalent to the [[Detached]] internal slot in the HTML Standard. |
272 |
|
|
// |
273 |
|
|
// If checking if a JavaScript MessagePort object is detached, this method |
274 |
|
|
// alone is often not enough, since the backing C++ MessagePort object may |
275 |
|
|
// have been deleted already. For all intents and purposes, an object with a |
276 |
|
|
// NULL pointer to the C++ MessagePort object is also detached. |
277 |
|
|
inline bool IsDetached() const; |
278 |
|
|
|
279 |
|
|
TransferMode GetTransferMode() const override; |
280 |
|
|
std::unique_ptr<TransferData> TransferForMessaging() override; |
281 |
|
|
|
282 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
283 |
|
2 |
SET_MEMORY_INFO_NAME(MessagePort) |
284 |
|
2 |
SET_SELF_SIZE(MessagePort) |
285 |
|
|
|
286 |
|
|
private: |
287 |
|
|
void OnClose() override; |
288 |
|
|
void OnMessage(); |
289 |
|
|
void TriggerAsync(); |
290 |
|
|
v8::MaybeLocal<v8::Value> ReceiveMessage(v8::Local<v8::Context> context, |
291 |
|
|
bool only_if_receiving); |
292 |
|
|
|
293 |
|
|
std::unique_ptr<MessagePortData> data_ = nullptr; |
294 |
|
|
bool receiving_messages_ = false; |
295 |
|
|
uv_async_t async_; |
296 |
|
|
v8::Global<v8::Function> emit_message_fn_; |
297 |
|
|
|
298 |
|
|
friend class MessagePortData; |
299 |
|
|
}; |
300 |
|
|
|
301 |
|
|
// Provide a base class from which JS classes that should be transferable or |
302 |
|
|
// cloneable by postMesssage() can inherit. |
303 |
|
|
// See e.g. FileHandle in internal/fs/promises.js for an example. |
304 |
|
6466 |
class JSTransferable : public BaseObject { |
305 |
|
|
public: |
306 |
|
|
JSTransferable(Environment* env, v8::Local<v8::Object> obj); |
307 |
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args); |
308 |
|
|
|
309 |
|
|
TransferMode GetTransferMode() const override; |
310 |
|
|
std::unique_ptr<TransferData> TransferForMessaging() override; |
311 |
|
|
std::unique_ptr<TransferData> CloneForMessaging() const override; |
312 |
|
|
v8::Maybe<std::vector<BaseObjectPtr<BaseObject>>> |
313 |
|
|
NestedTransferables() const override; |
314 |
|
|
v8::Maybe<bool> FinalizeTransferRead( |
315 |
|
|
v8::Local<v8::Context> context, |
316 |
|
|
v8::ValueDeserializer* deserializer) override; |
317 |
|
|
|
318 |
|
|
SET_NO_MEMORY_INFO() |
319 |
|
|
SET_MEMORY_INFO_NAME(JSTransferable) |
320 |
|
|
SET_SELF_SIZE(JSTransferable) |
321 |
|
|
|
322 |
|
|
private: |
323 |
|
|
std::unique_ptr<TransferData> TransferOrClone(TransferMode mode) const; |
324 |
|
|
|
325 |
|
24 |
class Data : public TransferData { |
326 |
|
|
public: |
327 |
|
|
Data(std::string&& deserialize_info, v8::Global<v8::Value>&& data); |
328 |
|
|
|
329 |
|
|
BaseObjectPtr<BaseObject> Deserialize( |
330 |
|
|
Environment* env, |
331 |
|
|
v8::Local<v8::Context> context, |
332 |
|
|
std::unique_ptr<TransferData> self) override; |
333 |
|
|
v8::Maybe<bool> FinalizeTransferWrite( |
334 |
|
|
v8::Local<v8::Context> context, |
335 |
|
|
v8::ValueSerializer* serializer) override; |
336 |
|
|
|
337 |
|
|
SET_NO_MEMORY_INFO() |
338 |
|
|
SET_MEMORY_INFO_NAME(JSTransferableTransferData) |
339 |
|
|
SET_SELF_SIZE(Data) |
340 |
|
|
|
341 |
|
|
private: |
342 |
|
|
std::string deserialize_info_; |
343 |
|
|
v8::Global<v8::Value> data_; |
344 |
|
|
}; |
345 |
|
|
}; |
346 |
|
|
|
347 |
|
|
v8::Local<v8::FunctionTemplate> GetMessagePortConstructorTemplate( |
348 |
|
|
Environment* env); |
349 |
|
|
|
350 |
|
|
} // namespace worker |
351 |
|
|
} // namespace node |
352 |
|
|
|
353 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
354 |
|
|
|
355 |
|
|
|
356 |
|
|
#endif // SRC_NODE_MESSAGING_H_ |