Bug 1568151 - Replace `target.getInspector()` by `target.getFront("inspector")`....
[gecko.git] / js / public / StructuredClone.h
blob847afe001076b9410fe1b7fb5c14b7c0ded4c226
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef js_StructuredClone_h
8 #define js_StructuredClone_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/BufferList.h"
12 #include "mozilla/MemoryReporting.h"
13 #include "mozilla/Move.h"
15 #include <stdint.h>
17 #include "jstypes.h"
19 #include "js/RootingAPI.h"
20 #include "js/TypeDecls.h"
21 #include "js/Value.h"
22 #include "js/Vector.h"
25 * API for safe passing of structured data, HTML 2018 Feb 21 section 2.7.
26 * <https://html.spec.whatwg.org/multipage/structured-data.html>
28 * This is a serialization scheme for JS values, somewhat like JSON. It
29 * preserves some aspects of JS objects (strings, numbers, own data properties
30 * with string keys, array elements) but not others (methods, getters and
31 * setters, prototype chains). Unlike JSON, structured data:
33 * - can contain cyclic references.
35 * - handles Maps, Sets, and some other object types.
37 * - supports *transferring* objects of certain types from one realm to
38 * another, rather than cloning them.
40 * - is specified by a living standard, and continues to evolve.
42 * - is encoded in a nonstandard binary format, and is never exposed to Web
43 * content in its serialized form. It's used internally by the browser to
44 * send data from one thread/realm/domain to another, not across the
45 * network.
48 struct JSStructuredCloneReader;
49 struct JSStructuredCloneWriter;
51 /**
52 * The structured-clone serialization format version number.
54 * When serialized data is stored as bytes, e.g. in your Firefox profile, later
55 * versions of the engine may have to read it. When you upgrade Firefox, we
56 * don't crawl through your whole profile converting all saved data from the
57 * previous version of the serialization format to the latest version. So it is
58 * normal to have data in old formats stored in your profile.
60 * The JS engine can *write* data only in the current format version.
62 * It can *read* any data written in the current version, and data written for
63 * DifferentProcess scope in earlier versions.
66 * ## When to bump this version number
68 * When making a change so drastic that the JS engine needs to know whether
69 * it's reading old or new serialized data in order to handle both correctly,
70 * increment this version number. Make sure the engine can still read all
71 * old data written with previous versions.
73 * If StructuredClone.cpp doesn't contain code that distinguishes between
74 * version 8 and version 9, there should not be a version 9.
76 * Do not increment for changes that only affect SameProcess encoding.
78 * Increment only for changes that would otherwise break old serialized data.
79 * Do not increment for new data types. (Rationale: Modulo bugs, older versions
80 * of the JS engine can already correctly throw errors when they encounter new,
81 * unrecognized features. A version number bump does not actually help them.)
83 #define JS_STRUCTURED_CLONE_VERSION 8
85 namespace JS {
87 /**
88 * Indicates the "scope of validity" of serialized data.
90 * Writing plain JS data produces an array of bytes that can be copied and
91 * read in another process or whatever. The serialized data is Plain Old Data.
92 * However, HTML also supports `Transferable` objects, which, when cloned, can
93 * be moved from the source object into the clone, like when you take a
94 * photograph of someone and it steals their soul.
95 * See <https://developer.mozilla.org/en-US/docs/Web/API/Transferable>.
96 * We support cloning and transferring objects of many types.
98 * For example, when we transfer an ArrayBuffer (within a process), we "detach"
99 * the ArrayBuffer, embed the raw buffer pointer in the serialized data, and
100 * later install it in a new ArrayBuffer in the destination realm. Ownership
101 * of that buffer memory is transferred from the original ArrayBuffer to the
102 * serialized data and then to the clone.
104 * This only makes sense within a single address space. When we transfer an
105 * ArrayBuffer to another process, the contents of the buffer must be copied
106 * into the serialized data. (The original ArrayBuffer is still detached,
107 * though, for consistency; in some cases the caller shouldn't know or care if
108 * the recipient is in the same process.)
110 * ArrayBuffers are actually a lucky case; some objects (like MessagePorts)
111 * can't reasonably be stored by value in serialized data -- it's pointers or
112 * nothing.
114 * So there is a tradeoff between scope of validity -- how far away the
115 * serialized data may be sent and still make sense -- and efficiency or
116 * features. The read and write algorithms therefore take an argument of this
117 * type, allowing the user to control those trade-offs.
119 enum class StructuredCloneScope : uint32_t {
121 * The most restrictive scope, with greatest efficiency and features.
123 * When writing, this means we're writing for an audience in the same
124 * process and same thread. The caller promises that the serialized data
125 * will **not** be shipped off to a different thread/process or stored in a
126 * database. It's OK to produce serialized data that contains pointers. In
127 * Rust terms, the serialized data will be treated as `!Send`.
129 * When reading, this means: Accept transferred objects and buffers
130 * (pointers). The caller promises that the serialized data was written
131 * using this API (otherwise, the serialized data may contain bogus
132 * pointers, leading to undefined behavior).
134 SameProcessSameThread,
137 * When writing, this means: The caller promises that the serialized data
138 * will **not** be shipped off to a different process or stored in a
139 * database. However, it may be shipped to another thread. It's OK to
140 * produce serialized data that contains pointers to data that is safe to
141 * send across threads, such as array buffers. In Rust terms, the
142 * serialized data will be treated as `Send` but not `Copy`.
144 * When reading, this means the same thing as SameProcessSameThread;
145 * the distinction only matters when writing.
147 SameProcessDifferentThread,
150 * When writing, this means we're writing for an audience in a different
151 * process. Produce serialized data that can be sent to other processes,
152 * bitwise copied, or even stored as bytes in a database and read by later
153 * versions of Firefox years from now. The HTML5 spec refers to this as
154 * "ForStorage" as in StructuredSerializeForStorage, though we use
155 * DifferentProcess for IPC as well as storage.
157 * Transferable objects are limited to ArrayBuffers, whose contents are
158 * copied into the serialized data (rather than just writing a pointer).
160 * When reading, this means: Do not accept pointers.
162 DifferentProcess,
165 * Handle a backwards-compatibility case with IndexedDB (bug 1434308): when
166 * reading, this means to treat legacy SameProcessSameThread data as if it
167 * were DifferentProcess.
169 * Do not use this for writing; use DifferentProcess instead.
171 DifferentProcessForIndexedDB,
174 * Existing code wants to be able to create an uninitialized
175 * JSStructuredCloneData without knowing the scope, then populate it with
176 * data (at which point the scope *is* known.)
178 Unassigned
181 enum TransferableOwnership {
182 /** Transferable data has not been filled in yet */
183 SCTAG_TMO_UNFILLED = 0,
185 /** Structured clone buffer does not yet own the data */
186 SCTAG_TMO_UNOWNED = 1,
188 /** All values at least this large are owned by the clone buffer */
189 SCTAG_TMO_FIRST_OWNED = 2,
191 /** Data is a pointer that can be freed */
192 SCTAG_TMO_ALLOC_DATA = 2,
194 /** Data is a memory mapped pointer */
195 SCTAG_TMO_MAPPED_DATA = 3,
198 * Data is embedding-specific. The engine can free it by calling the
199 * freeTransfer op. The embedding can also use SCTAG_TMO_USER_MIN and
200 * greater, up to 32 bits, to distinguish specific ownership variants.
202 SCTAG_TMO_CUSTOM = 4,
204 SCTAG_TMO_USER_MIN
207 class CloneDataPolicy {
208 bool sharedArrayBuffer_;
210 public:
211 // The default is to allow all policy-controlled aspects.
213 CloneDataPolicy() : sharedArrayBuffer_(true) {}
215 // In the JS engine, SharedArrayBuffers can only be cloned intra-process
216 // because the shared memory areas are allocated in process-private memory.
217 // Clients should therefore deny SharedArrayBuffers when cloning data that
218 // are to be transmitted inter-process.
220 // Clients should also deny SharedArrayBuffers when cloning data that are to
221 // be transmitted intra-process if policy needs dictate such denial.
223 CloneDataPolicy& denySharedArrayBuffer() {
224 sharedArrayBuffer_ = false;
225 return *this;
228 bool isSharedArrayBufferAllowed() const { return sharedArrayBuffer_; }
231 } /* namespace JS */
234 * Read structured data from the reader r. This hook is used to read a value
235 * previously serialized by a call to the WriteStructuredCloneOp hook.
237 * tag and data are the pair of uint32_t values from the header. The callback
238 * may use the JS_Read* APIs to read any other relevant parts of the object
239 * from the reader r. closure is any value passed to the JS_ReadStructuredClone
240 * function. Return the new object on success, nullptr on error/exception.
242 typedef JSObject* (*ReadStructuredCloneOp)(JSContext* cx,
243 JSStructuredCloneReader* r,
244 uint32_t tag, uint32_t data,
245 void* closure);
248 * Structured data serialization hook. The engine can write primitive values,
249 * Objects, Arrays, Dates, RegExps, TypedArrays, ArrayBuffers, Sets, Maps,
250 * and SharedTypedArrays. Any other type of object requires application support.
251 * This callback must first use the JS_WriteUint32Pair API to write an object
252 * header, passing a value greater than JS_SCTAG_USER to the tag parameter.
253 * Then it can use the JS_Write* APIs to write any other relevant parts of
254 * the value v to the writer w. closure is any value passed to the
255 * JS_WriteStructuredClone function.
257 * Return true on success, false on error/exception.
259 typedef bool (*WriteStructuredCloneOp)(JSContext* cx,
260 JSStructuredCloneWriter* w,
261 JS::HandleObject obj, void* closure);
264 * This is called when JS_WriteStructuredClone is given an invalid transferable.
265 * To follow HTML5, the application must throw a DATA_CLONE_ERR DOMException
266 * with error set to one of the JS_SCERR_* values.
268 typedef void (*StructuredCloneErrorOp)(JSContext* cx, uint32_t errorid);
271 * This is called when JS_ReadStructuredClone receives a transferable object
272 * not known to the engine. If this hook does not exist or returns false, the
273 * JS engine calls the reportError op if set, otherwise it throws a
274 * DATA_CLONE_ERR DOM Exception. This method is called before any other
275 * callback and must return a non-null object in returnObject on success.
277 typedef bool (*ReadTransferStructuredCloneOp)(
278 JSContext* cx, JSStructuredCloneReader* r, uint32_t tag, void* content,
279 uint64_t extraData, void* closure, JS::MutableHandleObject returnObject);
282 * Called when JS_WriteStructuredClone receives a transferable object not
283 * handled by the engine. If this hook does not exist or returns false, the JS
284 * engine will call the reportError hook or fall back to throwing a
285 * DATA_CLONE_ERR DOM Exception. This method is called before any other
286 * callback.
288 * tag: indicates what type of transferable this is. Must be greater than
289 * 0xFFFF0201 (value of the internal SCTAG_TRANSFER_MAP_PENDING_ENTRY)
291 * ownership: see TransferableOwnership, above. Used to communicate any needed
292 * ownership info to the FreeTransferStructuredCloneOp.
294 * content, extraData: what the ReadTransferStructuredCloneOp will receive
296 typedef bool (*TransferStructuredCloneOp)(JSContext* cx,
297 JS::Handle<JSObject*> obj,
298 void* closure,
299 // Output:
300 uint32_t* tag,
301 JS::TransferableOwnership* ownership,
302 void** content, uint64_t* extraData);
305 * Called when freeing an unknown transferable object. Note that it
306 * should never trigger a garbage collection (and will assert in a
307 * debug build if it does.)
309 typedef void (*FreeTransferStructuredCloneOp)(
310 uint32_t tag, JS::TransferableOwnership ownership, void* content,
311 uint64_t extraData, void* closure);
314 * Called when the transferring objects are checked. If this function returns
315 * false, the serialization ends throwing a DataCloneError exception.
317 typedef bool (*CanTransferStructuredCloneOp)(JSContext* cx,
318 JS::Handle<JSObject*> obj,
319 void* closure);
321 struct JSStructuredCloneCallbacks {
322 ReadStructuredCloneOp read;
323 WriteStructuredCloneOp write;
324 StructuredCloneErrorOp reportError;
325 ReadTransferStructuredCloneOp readTransfer;
326 TransferStructuredCloneOp writeTransfer;
327 FreeTransferStructuredCloneOp freeTransfer;
328 CanTransferStructuredCloneOp canTransfer;
331 enum OwnTransferablePolicy {
333 * The buffer owns any Transferables that it might contain, and should
334 * properly release them upon destruction.
336 OwnsTransferablesIfAny,
339 * Do not free any Transferables within this buffer when deleting it. This
340 * is used to mark as clone buffer as containing data from another process,
341 * and so it can't legitimately contain pointers. If the buffer claims to
342 * have transferables, it's a bug or an attack. This is also used for
343 * abandon(), where a buffer still contains raw data but the ownership has
344 * been given over to some other entity.
346 IgnoreTransferablesIfAny,
349 * A buffer that cannot contain Transferables at all. This usually means
350 * the buffer is empty (not yet filled in, or having been cleared).
352 NoTransferables
355 namespace js {
356 class SharedArrayRawBuffer;
358 class SharedArrayRawBufferRefs {
359 public:
360 SharedArrayRawBufferRefs() = default;
361 SharedArrayRawBufferRefs(SharedArrayRawBufferRefs&& other) = default;
362 SharedArrayRawBufferRefs& operator=(SharedArrayRawBufferRefs&& other);
363 ~SharedArrayRawBufferRefs();
365 MOZ_MUST_USE bool acquire(JSContext* cx, SharedArrayRawBuffer* rawbuf);
366 MOZ_MUST_USE bool acquireAll(JSContext* cx,
367 const SharedArrayRawBufferRefs& that);
368 void takeOwnership(SharedArrayRawBufferRefs&&);
369 void releaseAll();
371 private:
372 js::Vector<js::SharedArrayRawBuffer*, 0, js::SystemAllocPolicy> refs_;
375 template <typename T, typename AllocPolicy>
376 struct BufferIterator;
377 } // namespace js
380 * JSStructuredCloneData represents structured clone data together with the
381 * information needed to read/write/transfer/free the records within it, in the
382 * form of a set of callbacks.
384 class MOZ_NON_MEMMOVABLE JS_PUBLIC_API JSStructuredCloneData {
385 public:
386 using BufferList = mozilla::BufferList<js::SystemAllocPolicy>;
387 using Iterator = BufferList::IterImpl;
389 private:
390 static const size_t kStandardCapacity = 4096;
392 BufferList bufList_;
394 // The (address space, thread) scope within which this clone is valid. Note
395 // that this must be either set during construction, or start out as
396 // Unassigned and transition once to something else.
397 JS::StructuredCloneScope scope_;
399 const JSStructuredCloneCallbacks* callbacks_ = nullptr;
400 void* closure_ = nullptr;
401 OwnTransferablePolicy ownTransferables_ =
402 OwnTransferablePolicy::NoTransferables;
403 js::SharedArrayRawBufferRefs refsHeld_;
405 friend struct JSStructuredCloneWriter;
406 friend class JS_PUBLIC_API JSAutoStructuredCloneBuffer;
407 template <typename T, typename AllocPolicy>
408 friend struct js::BufferIterator;
410 public:
411 // The constructor must be infallible but SystemAllocPolicy is not, so both
412 // the initial size and initial capacity of the BufferList must be zero.
413 explicit JSStructuredCloneData(JS::StructuredCloneScope scope)
414 : bufList_(0, 0, kStandardCapacity, js::SystemAllocPolicy()),
415 scope_(scope),
416 callbacks_(nullptr),
417 closure_(nullptr),
418 ownTransferables_(OwnTransferablePolicy::NoTransferables) {}
420 // Steal the raw data from a BufferList. In this case, we don't know the
421 // scope and none of the callback info is assigned yet.
422 JSStructuredCloneData(BufferList&& buffers, JS::StructuredCloneScope scope)
423 : bufList_(std::move(buffers)),
424 scope_(scope),
425 callbacks_(nullptr),
426 closure_(nullptr),
427 ownTransferables_(OwnTransferablePolicy::NoTransferables) {}
428 MOZ_IMPLICIT JSStructuredCloneData(BufferList&& buffers)
429 : JSStructuredCloneData(std::move(buffers),
430 JS::StructuredCloneScope::Unassigned) {}
431 JSStructuredCloneData(JSStructuredCloneData&& other) = default;
432 JSStructuredCloneData& operator=(JSStructuredCloneData&& other) = default;
433 ~JSStructuredCloneData() { discardTransferables(); }
435 void setCallbacks(const JSStructuredCloneCallbacks* callbacks, void* closure,
436 OwnTransferablePolicy policy) {
437 callbacks_ = callbacks;
438 closure_ = closure;
439 ownTransferables_ = policy;
442 MOZ_MUST_USE bool Init(size_t initialCapacity = 0) {
443 return bufList_.Init(0, initialCapacity);
446 JS::StructuredCloneScope scope() const { return scope_; }
448 void initScope(JS::StructuredCloneScope scope) {
449 MOZ_ASSERT(Size() == 0, "initScope() of nonempty JSStructuredCloneData");
450 if (scope_ != JS::StructuredCloneScope::Unassigned) {
451 MOZ_ASSERT(scope_ == scope,
452 "Cannot change scope after it has been initialized");
454 scope_ = scope;
457 size_t Size() const { return bufList_.Size(); }
459 const Iterator Start() const { return bufList_.Iter(); }
461 MOZ_MUST_USE bool Advance(Iterator& iter, size_t distance) const {
462 return iter.AdvanceAcrossSegments(bufList_, distance);
465 MOZ_MUST_USE bool ReadBytes(Iterator& iter, char* buffer, size_t size) const {
466 return bufList_.ReadBytes(iter, buffer, size);
469 // Append new data to the end of the buffer.
470 MOZ_MUST_USE bool AppendBytes(const char* data, size_t size) {
471 MOZ_ASSERT(scope_ != JS::StructuredCloneScope::Unassigned);
472 return bufList_.WriteBytes(data, size);
475 // Update data stored within the existing buffer. There must be at least
476 // 'size' bytes between the position of 'iter' and the end of the buffer.
477 MOZ_MUST_USE bool UpdateBytes(Iterator& iter, const char* data,
478 size_t size) const {
479 MOZ_ASSERT(scope_ != JS::StructuredCloneScope::Unassigned);
480 while (size > 0) {
481 size_t remaining = iter.RemainingInSegment();
482 size_t nbytes = std::min(remaining, size);
483 memcpy(iter.Data(), data, nbytes);
484 data += nbytes;
485 size -= nbytes;
486 iter.Advance(bufList_, nbytes);
488 return true;
491 char* AllocateBytes(size_t maxSize, size_t* size) {
492 return bufList_.AllocateBytes(maxSize, size);
495 void Clear() {
496 discardTransferables();
497 bufList_.Clear();
500 // Return a new read-only JSStructuredCloneData that "borrows" the contents
501 // of |this|. Its lifetime should not exceed the donor's. This is only
502 // allowed for DifferentProcess clones, so finalization of the borrowing
503 // clone will do nothing.
504 JSStructuredCloneData Borrow(Iterator& iter, size_t size,
505 bool* success) const {
506 MOZ_ASSERT(scope_ == JS::StructuredCloneScope::DifferentProcess);
507 return JSStructuredCloneData(
508 bufList_.Borrow<js::SystemAllocPolicy>(iter, size, success), scope_);
511 // Iterate over all contained data, one BufferList segment's worth at a
512 // time, and invoke the given FunctionToApply with the data pointer and
513 // size. The function should return a bool value, and this loop will exit
514 // with false if the function ever returns false.
515 template <typename FunctionToApply>
516 bool ForEachDataChunk(FunctionToApply&& function) const {
517 Iterator iter = bufList_.Iter();
518 while (!iter.Done()) {
519 if (!function(iter.Data(), iter.RemainingInSegment())) {
520 return false;
522 iter.Advance(bufList_, iter.RemainingInSegment());
524 return true;
527 // Append the entire contents of other's bufList_ to our own.
528 MOZ_MUST_USE bool Append(const JSStructuredCloneData& other) {
529 MOZ_ASSERT(scope_ == other.scope());
530 return other.ForEachDataChunk(
531 [&](const char* data, size_t size) { return AppendBytes(data, size); });
534 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) {
535 return bufList_.SizeOfExcludingThis(mallocSizeOf);
538 void discardTransferables();
542 * Implements StructuredDeserialize and StructuredDeserializeWithTransfer.
544 * Note: If `data` contains transferable objects, it can be read only once.
546 JS_PUBLIC_API bool JS_ReadStructuredClone(
547 JSContext* cx, JSStructuredCloneData& data, uint32_t version,
548 JS::StructuredCloneScope scope, JS::MutableHandleValue vp,
549 const JSStructuredCloneCallbacks* optionalCallbacks, void* closure);
552 * Implements StructuredSerialize, StructuredSerializeForStorage, and
553 * StructuredSerializeWithTransfer.
555 * Note: If the scope is DifferentProcess then the cloneDataPolicy must deny
556 * shared-memory objects, or an error will be signaled if a shared memory object
557 * is seen.
559 JS_PUBLIC_API bool JS_WriteStructuredClone(
560 JSContext* cx, JS::HandleValue v, JSStructuredCloneData* data,
561 JS::StructuredCloneScope scope, JS::CloneDataPolicy cloneDataPolicy,
562 const JSStructuredCloneCallbacks* optionalCallbacks, void* closure,
563 JS::HandleValue transferable);
565 JS_PUBLIC_API bool JS_StructuredCloneHasTransferables(
566 JSStructuredCloneData& data, bool* hasTransferable);
568 JS_PUBLIC_API bool JS_StructuredClone(
569 JSContext* cx, JS::HandleValue v, JS::MutableHandleValue vp,
570 const JSStructuredCloneCallbacks* optionalCallbacks, void* closure);
573 * The C-style API calls to read and write structured clones are fragile --
574 * they rely on the caller to properly handle ownership of the clone data, and
575 * the handling of the input data as well as the interpretation of the contents
576 * of the clone buffer are dependent on the callbacks passed in. If you
577 * serialize and deserialize with different callbacks, the results are
578 * questionable.
580 * JSAutoStructuredCloneBuffer wraps things up in an RAII class for data
581 * management, and uses the same callbacks for both writing and reading
582 * (serializing and deserializing).
584 class JS_PUBLIC_API JSAutoStructuredCloneBuffer {
585 const JS::StructuredCloneScope scope_;
586 JSStructuredCloneData data_;
587 uint32_t version_;
589 public:
590 JSAutoStructuredCloneBuffer(JS::StructuredCloneScope scope,
591 const JSStructuredCloneCallbacks* callbacks,
592 void* closure)
593 : scope_(scope), data_(scope), version_(JS_STRUCTURED_CLONE_VERSION) {
594 data_.setCallbacks(callbacks, closure,
595 OwnTransferablePolicy::NoTransferables);
598 JSAutoStructuredCloneBuffer(JSAutoStructuredCloneBuffer&& other);
599 JSAutoStructuredCloneBuffer& operator=(JSAutoStructuredCloneBuffer&& other);
601 ~JSAutoStructuredCloneBuffer() { clear(); }
603 JSStructuredCloneData& data() { return data_; }
604 bool empty() const { return !data_.Size(); }
606 void clear();
608 JS::StructuredCloneScope scope() const { return scope_; }
611 * Adopt some memory. It will be automatically freed by the destructor.
612 * data must have been allocated by the JS engine (e.g., extracted via
613 * JSAutoStructuredCloneBuffer::steal).
615 void adopt(JSStructuredCloneData&& data,
616 uint32_t version = JS_STRUCTURED_CLONE_VERSION,
617 const JSStructuredCloneCallbacks* callbacks = nullptr,
618 void* closure = nullptr);
621 * Release the buffer and transfer ownership to the caller.
623 void steal(JSStructuredCloneData* data, uint32_t* versionp = nullptr,
624 const JSStructuredCloneCallbacks** callbacks = nullptr,
625 void** closure = nullptr);
628 * Abandon ownership of any transferable objects stored in the buffer,
629 * without freeing the buffer itself. Useful when copying the data out into
630 * an external container, though note that you will need to use adopt() to
631 * properly release that data eventually.
633 void abandon() {
634 data_.ownTransferables_ = OwnTransferablePolicy::IgnoreTransferablesIfAny;
637 bool read(JSContext* cx, JS::MutableHandleValue vp,
638 const JSStructuredCloneCallbacks* optionalCallbacks = nullptr,
639 void* closure = nullptr);
641 bool write(JSContext* cx, JS::HandleValue v,
642 const JSStructuredCloneCallbacks* optionalCallbacks = nullptr,
643 void* closure = nullptr);
645 bool write(JSContext* cx, JS::HandleValue v, JS::HandleValue transferable,
646 JS::CloneDataPolicy cloneDataPolicy,
647 const JSStructuredCloneCallbacks* optionalCallbacks = nullptr,
648 void* closure = nullptr);
650 size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) {
651 return data_.SizeOfExcludingThis(mallocSizeOf);
654 size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) {
655 return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf);
658 private:
659 // Copy and assignment are not supported.
660 JSAutoStructuredCloneBuffer(const JSAutoStructuredCloneBuffer& other) =
661 delete;
662 JSAutoStructuredCloneBuffer& operator=(
663 const JSAutoStructuredCloneBuffer& other) = delete;
666 // The range of tag values the application may use for its own custom object
667 // types.
668 #define JS_SCTAG_USER_MIN ((uint32_t)0xFFFF8000)
669 #define JS_SCTAG_USER_MAX ((uint32_t)0xFFFFFFFF)
671 #define JS_SCERR_RECURSION 0
672 #define JS_SCERR_TRANSFERABLE 1
673 #define JS_SCERR_DUP_TRANSFERABLE 2
674 #define JS_SCERR_UNSUPPORTED_TYPE 3
675 #define JS_SCERR_SHMEM_TRANSFERABLE 4
677 JS_PUBLIC_API bool JS_ReadUint32Pair(JSStructuredCloneReader* r, uint32_t* p1,
678 uint32_t* p2);
680 JS_PUBLIC_API bool JS_ReadBytes(JSStructuredCloneReader* r, void* p,
681 size_t len);
683 JS_PUBLIC_API bool JS_ReadTypedArray(JSStructuredCloneReader* r,
684 JS::MutableHandleValue vp);
686 JS_PUBLIC_API bool JS_WriteUint32Pair(JSStructuredCloneWriter* w, uint32_t tag,
687 uint32_t data);
689 JS_PUBLIC_API bool JS_WriteBytes(JSStructuredCloneWriter* w, const void* p,
690 size_t len);
692 JS_PUBLIC_API bool JS_WriteString(JSStructuredCloneWriter* w,
693 JS::HandleString str);
695 JS_PUBLIC_API bool JS_WriteTypedArray(JSStructuredCloneWriter* w,
696 JS::HandleValue v);
698 JS_PUBLIC_API bool JS_ObjectNotWritten(JSStructuredCloneWriter* w,
699 JS::HandleObject obj);
701 JS_PUBLIC_API JS::StructuredCloneScope JS_GetStructuredCloneScope(
702 JSStructuredCloneWriter* w);
704 #endif /* js_StructuredClone_h */