2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: set ts=8 sts=2 et sw=2 tw=80:
|
2013-08-20 10:43:47 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef js_StructuredClone_h
|
|
|
|
#define js_StructuredClone_h
|
|
|
|
|
2016-08-10 09:18:29 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2016-04-22 13:04:20 +03:00
|
|
|
#include "mozilla/BufferList.h"
|
2017-07-26 00:53:41 +03:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-04-22 13:04:20 +03:00
|
|
|
|
2013-08-20 10:43:47 +04:00
|
|
|
#include <stdint.h>
|
2020-01-20 19:18:40 +03:00
|
|
|
#include <utility>
|
2013-08-20 10:43:47 +04:00
|
|
|
|
|
|
|
#include "jstypes.h"
|
|
|
|
|
2020-09-15 14:04:31 +03:00
|
|
|
#include "js/AllocPolicy.h"
|
2013-10-23 04:18:32 +04:00
|
|
|
#include "js/RootingAPI.h"
|
2013-08-28 06:59:14 +04:00
|
|
|
#include "js/TypeDecls.h"
|
2013-10-23 04:18:32 +04:00
|
|
|
#include "js/Value.h"
|
2017-04-03 15:18:15 +03:00
|
|
|
#include "js/Vector.h"
|
2013-08-28 06:59:14 +04:00
|
|
|
|
2018-02-21 06:40:49 +03:00
|
|
|
/*
|
|
|
|
* API for safe passing of structured data, HTML 2018 Feb 21 section 2.7.
|
|
|
|
* <https://html.spec.whatwg.org/multipage/structured-data.html>
|
|
|
|
*
|
|
|
|
* This is a serialization scheme for JS values, somewhat like JSON. It
|
|
|
|
* preserves some aspects of JS objects (strings, numbers, own data properties
|
|
|
|
* with string keys, array elements) but not others (methods, getters and
|
|
|
|
* setters, prototype chains). Unlike JSON, structured data:
|
|
|
|
*
|
|
|
|
* - can contain cyclic references.
|
|
|
|
*
|
|
|
|
* - handles Maps, Sets, and some other object types.
|
|
|
|
*
|
|
|
|
* - supports *transferring* objects of certain types from one realm to
|
|
|
|
* another, rather than cloning them.
|
|
|
|
*
|
|
|
|
* - is specified by a living standard, and continues to evolve.
|
|
|
|
*
|
|
|
|
* - is encoded in a nonstandard binary format, and is never exposed to Web
|
|
|
|
* content in its serialized form. It's used internally by the browser to
|
|
|
|
* send data from one thread/realm/domain to another, not across the
|
|
|
|
* network.
|
|
|
|
*/
|
|
|
|
|
2013-08-20 10:43:47 +04:00
|
|
|
struct JSStructuredCloneReader;
|
|
|
|
struct JSStructuredCloneWriter;
|
|
|
|
|
2018-02-21 06:40:49 +03:00
|
|
|
/**
|
|
|
|
* The structured-clone serialization format version number.
|
|
|
|
*
|
|
|
|
* When serialized data is stored as bytes, e.g. in your Firefox profile, later
|
|
|
|
* versions of the engine may have to read it. When you upgrade Firefox, we
|
|
|
|
* don't crawl through your whole profile converting all saved data from the
|
|
|
|
* previous version of the serialization format to the latest version. So it is
|
|
|
|
* normal to have data in old formats stored in your profile.
|
|
|
|
*
|
|
|
|
* The JS engine can *write* data only in the current format version.
|
|
|
|
*
|
|
|
|
* It can *read* any data written in the current version, and data written for
|
|
|
|
* DifferentProcess scope in earlier versions.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* ## When to bump this version number
|
|
|
|
*
|
|
|
|
* When making a change so drastic that the JS engine needs to know whether
|
|
|
|
* it's reading old or new serialized data in order to handle both correctly,
|
|
|
|
* increment this version number. Make sure the engine can still read all
|
|
|
|
* old data written with previous versions.
|
|
|
|
*
|
|
|
|
* If StructuredClone.cpp doesn't contain code that distinguishes between
|
|
|
|
* version 8 and version 9, there should not be a version 9.
|
|
|
|
*
|
|
|
|
* Do not increment for changes that only affect SameProcess encoding.
|
|
|
|
*
|
|
|
|
* Increment only for changes that would otherwise break old serialized data.
|
|
|
|
* Do not increment for new data types. (Rationale: Modulo bugs, older versions
|
|
|
|
* of the JS engine can already correctly throw errors when they encounter new,
|
|
|
|
* unrecognized features. A version number bump does not actually help them.)
|
|
|
|
*/
|
|
|
|
#define JS_STRUCTURED_CLONE_VERSION 8
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2013-11-15 08:42:34 +04:00
|
|
|
namespace JS {
|
2016-07-21 16:29:42 +03:00
|
|
|
|
2018-02-21 06:40:49 +03:00
|
|
|
/**
|
|
|
|
* Indicates the "scope of validity" of serialized data.
|
|
|
|
*
|
|
|
|
* Writing plain JS data produces an array of bytes that can be copied and
|
|
|
|
* read in another process or whatever. The serialized data is Plain Old Data.
|
|
|
|
* However, HTML also supports `Transferable` objects, which, when cloned, can
|
|
|
|
* be moved from the source object into the clone, like when you take a
|
|
|
|
* photograph of someone and it steals their soul.
|
|
|
|
* See <https://developer.mozilla.org/en-US/docs/Web/API/Transferable>.
|
|
|
|
* We support cloning and transferring objects of many types.
|
|
|
|
*
|
|
|
|
* For example, when we transfer an ArrayBuffer (within a process), we "detach"
|
|
|
|
* the ArrayBuffer, embed the raw buffer pointer in the serialized data, and
|
|
|
|
* later install it in a new ArrayBuffer in the destination realm. Ownership
|
|
|
|
* of that buffer memory is transferred from the original ArrayBuffer to the
|
|
|
|
* serialized data and then to the clone.
|
|
|
|
*
|
|
|
|
* This only makes sense within a single address space. When we transfer an
|
|
|
|
* ArrayBuffer to another process, the contents of the buffer must be copied
|
|
|
|
* into the serialized data. (The original ArrayBuffer is still detached,
|
|
|
|
* though, for consistency; in some cases the caller shouldn't know or care if
|
|
|
|
* the recipient is in the same process.)
|
|
|
|
*
|
|
|
|
* ArrayBuffers are actually a lucky case; some objects (like MessagePorts)
|
|
|
|
* can't reasonably be stored by value in serialized data -- it's pointers or
|
|
|
|
* nothing.
|
|
|
|
*
|
|
|
|
* So there is a tradeoff between scope of validity -- how far away the
|
|
|
|
* serialized data may be sent and still make sense -- and efficiency or
|
|
|
|
* features. The read and write algorithms therefore take an argument of this
|
|
|
|
* type, allowing the user to control those trade-offs.
|
|
|
|
*/
|
2016-07-21 16:29:42 +03:00
|
|
|
enum class StructuredCloneScope : uint32_t {
|
2018-02-21 06:40:49 +03:00
|
|
|
/**
|
|
|
|
* The most restrictive scope, with greatest efficiency and features.
|
|
|
|
*
|
|
|
|
* When writing, this means: The caller promises that the serialized data
|
|
|
|
* will **not** be shipped off to a different process or stored in a
|
|
|
|
* database. However, it may be shipped to another thread. It's OK to
|
|
|
|
* produce serialized data that contains pointers to data that is safe to
|
|
|
|
* send across threads, such as array buffers. In Rust terms, the
|
|
|
|
* serialized data will be treated as `Send` but not `Copy`.
|
|
|
|
*
|
2020-01-15 15:02:19 +03:00
|
|
|
* When reading, this means: Accept transferred objects and buffers
|
|
|
|
* (pointers). The caller promises that the serialized data was written
|
|
|
|
* using this API (otherwise, the serialized data may contain bogus
|
|
|
|
* pointers, leading to undefined behavior).
|
|
|
|
*
|
|
|
|
* Starts from 1 because there used to be a SameProcessSameThread enum value
|
|
|
|
* of 0 and these values are encoded into the structured serialization format
|
|
|
|
* as part of the SCTAG_HEADER, and IndexedDB persists the representation to
|
|
|
|
* disk.
|
2018-02-21 06:40:49 +03:00
|
|
|
*/
|
2020-01-15 15:02:17 +03:00
|
|
|
SameProcess = 1,
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-02-21 06:40:49 +03:00
|
|
|
/**
|
|
|
|
* When writing, this means we're writing for an audience in a different
|
|
|
|
* process. Produce serialized data that can be sent to other processes,
|
|
|
|
* bitwise copied, or even stored as bytes in a database and read by later
|
2018-04-25 01:21:47 +03:00
|
|
|
* versions of Firefox years from now. The HTML5 spec refers to this as
|
|
|
|
* "ForStorage" as in StructuredSerializeForStorage, though we use
|
|
|
|
* DifferentProcess for IPC as well as storage.
|
|
|
|
*
|
|
|
|
* Transferable objects are limited to ArrayBuffers, whose contents are
|
|
|
|
* copied into the serialized data (rather than just writing a pointer).
|
2018-02-21 06:40:49 +03:00
|
|
|
*
|
|
|
|
* When reading, this means: Do not accept pointers.
|
|
|
|
*/
|
2018-04-25 01:21:47 +03:00
|
|
|
DifferentProcess,
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-25 01:21:47 +03:00
|
|
|
/**
|
|
|
|
* Handle a backwards-compatibility case with IndexedDB (bug 1434308): when
|
2020-01-15 15:02:17 +03:00
|
|
|
* reading, this means to treat legacy SameProcess data as if it were
|
|
|
|
* DifferentProcess.
|
2018-04-25 01:21:47 +03:00
|
|
|
*
|
|
|
|
* Do not use this for writing; use DifferentProcess instead.
|
|
|
|
*/
|
2018-04-03 21:17:33 +03:00
|
|
|
DifferentProcessForIndexedDB,
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-03 21:17:33 +03:00
|
|
|
/**
|
|
|
|
* Existing code wants to be able to create an uninitialized
|
|
|
|
* JSStructuredCloneData without knowing the scope, then populate it with
|
|
|
|
* data (at which point the scope *is* known.)
|
|
|
|
*/
|
2020-01-24 10:57:56 +03:00
|
|
|
Unassigned,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This scope is used when the deserialization context is unknown. When
|
|
|
|
* writing, DifferentProcess or SameProcess scope is chosen based on the
|
|
|
|
* nature of the object.
|
|
|
|
*/
|
|
|
|
UnknownDestination,
|
2016-07-21 16:29:42 +03:00
|
|
|
};
|
|
|
|
|
2013-11-15 08:42:34 +04:00
|
|
|
enum TransferableOwnership {
|
2015-10-17 20:27:16 +03:00
|
|
|
/** Transferable data has not been filled in yet */
|
2013-11-15 08:42:34 +04:00
|
|
|
SCTAG_TMO_UNFILLED = 0,
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/** Structured clone buffer does not yet own the data */
|
2013-11-15 08:42:34 +04:00
|
|
|
SCTAG_TMO_UNOWNED = 1,
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/** All values at least this large are owned by the clone buffer */
|
2013-11-15 08:42:34 +04:00
|
|
|
SCTAG_TMO_FIRST_OWNED = 2,
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/** Data is a pointer that can be freed */
|
2013-11-15 08:42:34 +04:00
|
|
|
SCTAG_TMO_ALLOC_DATA = 2,
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/** Data is a memory mapped pointer */
|
2016-10-24 15:22:47 +03:00
|
|
|
SCTAG_TMO_MAPPED_DATA = 3,
|
2014-04-16 15:26:39 +04:00
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
|
|
|
* Data is embedding-specific. The engine can free it by calling the
|
|
|
|
* freeTransfer op. The embedding can also use SCTAG_TMO_USER_MIN and
|
|
|
|
* greater, up to 32 bits, to distinguish specific ownership variants.
|
|
|
|
*/
|
2016-10-24 15:22:47 +03:00
|
|
|
SCTAG_TMO_CUSTOM = 4,
|
2013-11-15 08:42:34 +04:00
|
|
|
|
|
|
|
SCTAG_TMO_USER_MIN
|
|
|
|
};
|
2016-10-24 15:22:47 +03:00
|
|
|
|
|
|
|
class CloneDataPolicy {
|
2020-01-24 19:31:21 +03:00
|
|
|
bool allowIntraClusterClonableSharedObjects_;
|
2020-02-06 16:49:28 +03:00
|
|
|
bool allowSharedMemoryObjects_;
|
2016-10-24 15:22:47 +03:00
|
|
|
|
|
|
|
public:
|
2019-10-23 10:20:05 +03:00
|
|
|
// The default is to deny all policy-controlled aspects.
|
2016-10-24 15:22:47 +03:00
|
|
|
|
2020-02-06 16:49:28 +03:00
|
|
|
CloneDataPolicy()
|
|
|
|
: allowIntraClusterClonableSharedObjects_(false),
|
|
|
|
allowSharedMemoryObjects_(false) {}
|
2016-10-24 15:22:47 +03:00
|
|
|
|
2020-02-06 16:49:28 +03:00
|
|
|
// SharedArrayBuffers and WASM modules can only be cloned intra-process
|
|
|
|
// because the shared memory areas are allocated in process-private memory or
|
|
|
|
// because there are security issues of sharing them cross agent clusters.
|
|
|
|
// y default, we don't allow shared-memory and intra-cluster objects. Clients
|
|
|
|
// should therefore enable these 2 clone features when needed.
|
2016-10-24 15:22:47 +03:00
|
|
|
|
2020-01-24 19:31:21 +03:00
|
|
|
void allowIntraClusterClonableSharedObjects() {
|
|
|
|
allowIntraClusterClonableSharedObjects_ = true;
|
|
|
|
}
|
2016-10-24 15:22:47 +03:00
|
|
|
|
2020-01-24 19:31:21 +03:00
|
|
|
bool areIntraClusterClonableSharedObjectsAllowed() const {
|
|
|
|
return allowIntraClusterClonableSharedObjects_;
|
|
|
|
}
|
2020-02-06 16:49:28 +03:00
|
|
|
|
|
|
|
void allowSharedMemoryObjects() { allowSharedMemoryObjects_ = true; }
|
|
|
|
|
|
|
|
bool areSharedMemoryObjectsAllowed() const {
|
|
|
|
return allowSharedMemoryObjects_;
|
|
|
|
}
|
2016-10-24 15:22:47 +03:00
|
|
|
};
|
|
|
|
|
2013-11-15 08:42:34 +04:00
|
|
|
} /* namespace JS */
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
|
|
|
* Read structured data from the reader r. This hook is used to read a value
|
|
|
|
* previously serialized by a call to the WriteStructuredCloneOp hook.
|
|
|
|
*
|
|
|
|
* tag and data are the pair of uint32_t values from the header. The callback
|
|
|
|
* may use the JS_Read* APIs to read any other relevant parts of the object
|
|
|
|
* from the reader r. closure is any value passed to the JS_ReadStructuredClone
|
|
|
|
* function. Return the new object on success, nullptr on error/exception.
|
|
|
|
*/
|
2020-01-24 19:31:21 +03:00
|
|
|
typedef JSObject* (*ReadStructuredCloneOp)(
|
|
|
|
JSContext* cx, JSStructuredCloneReader* r,
|
|
|
|
const JS::CloneDataPolicy& cloneDataPolicy, uint32_t tag, uint32_t data,
|
|
|
|
void* closure);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
|
|
|
* Structured data serialization hook. The engine can write primitive values,
|
|
|
|
* Objects, Arrays, Dates, RegExps, TypedArrays, ArrayBuffers, Sets, Maps,
|
|
|
|
* and SharedTypedArrays. Any other type of object requires application support.
|
|
|
|
* This callback must first use the JS_WriteUint32Pair API to write an object
|
|
|
|
* header, passing a value greater than JS_SCTAG_USER to the tag parameter.
|
|
|
|
* Then it can use the JS_Write* APIs to write any other relevant parts of
|
|
|
|
* the value v to the writer w. closure is any value passed to the
|
|
|
|
* JS_WriteStructuredClone function.
|
|
|
|
*
|
|
|
|
* Return true on success, false on error/exception.
|
|
|
|
*/
|
2015-03-29 01:22:11 +03:00
|
|
|
typedef bool (*WriteStructuredCloneOp)(JSContext* cx,
|
|
|
|
JSStructuredCloneWriter* w,
|
2020-01-24 10:57:56 +03:00
|
|
|
JS::HandleObject obj,
|
|
|
|
bool* sameProcessScopeRequired,
|
|
|
|
void* closure);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
|
|
|
* This is called when JS_WriteStructuredClone is given an invalid transferable.
|
|
|
|
* To follow HTML5, the application must throw a DATA_CLONE_ERR DOMException
|
|
|
|
* with error set to one of the JS_SCERR_* values.
|
|
|
|
*/
|
2020-02-14 16:45:47 +03:00
|
|
|
typedef void (*StructuredCloneErrorOp)(JSContext* cx, uint32_t errorid,
|
|
|
|
void* closure, const char* errorMessage);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
|
|
|
* This is called when JS_ReadStructuredClone receives a transferable object
|
|
|
|
* not known to the engine. If this hook does not exist or returns false, the
|
|
|
|
* JS engine calls the reportError op if set, otherwise it throws a
|
|
|
|
* DATA_CLONE_ERR DOM Exception. This method is called before any other
|
|
|
|
* callback and must return a non-null object in returnObject on success.
|
|
|
|
*/
|
2015-03-29 01:22:11 +03:00
|
|
|
typedef bool (*ReadTransferStructuredCloneOp)(
|
|
|
|
JSContext* cx, JSStructuredCloneReader* r, uint32_t tag, void* content,
|
2013-11-15 08:42:34 +04:00
|
|
|
uint64_t extraData, void* closure, JS::MutableHandleObject returnObject);
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
|
|
|
* Called when JS_WriteStructuredClone receives a transferable object not
|
|
|
|
* handled by the engine. If this hook does not exist or returns false, the JS
|
|
|
|
* engine will call the reportError hook or fall back to throwing a
|
|
|
|
* DATA_CLONE_ERR DOM Exception. This method is called before any other
|
|
|
|
* callback.
|
|
|
|
*
|
|
|
|
* tag: indicates what type of transferable this is. Must be greater than
|
|
|
|
* 0xFFFF0201 (value of the internal SCTAG_TRANSFER_MAP_PENDING_ENTRY)
|
|
|
|
*
|
|
|
|
* ownership: see TransferableOwnership, above. Used to communicate any needed
|
|
|
|
* ownership info to the FreeTransferStructuredCloneOp.
|
|
|
|
*
|
|
|
|
* content, extraData: what the ReadTransferStructuredCloneOp will receive
|
|
|
|
*/
|
2015-03-29 01:22:11 +03:00
|
|
|
typedef bool (*TransferStructuredCloneOp)(JSContext* cx,
|
2013-11-15 08:42:34 +04:00
|
|
|
JS::Handle<JSObject*> obj,
|
2015-03-29 01:22:11 +03:00
|
|
|
void* closure,
|
2013-11-15 08:42:34 +04:00
|
|
|
// Output:
|
2015-03-29 01:22:11 +03:00
|
|
|
uint32_t* tag,
|
|
|
|
JS::TransferableOwnership* ownership,
|
|
|
|
void** content, uint64_t* extraData);
|
2013-11-15 08:42:34 +04:00
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2016-04-22 13:04:20 +03:00
|
|
|
* Called when freeing an unknown transferable object. Note that it
|
|
|
|
* should never trigger a garbage collection (and will assert in a
|
|
|
|
* debug build if it does.)
|
2015-10-17 20:27:16 +03:00
|
|
|
*/
|
2013-11-15 08:42:34 +04:00
|
|
|
typedef void (*FreeTransferStructuredCloneOp)(
|
|
|
|
uint32_t tag, JS::TransferableOwnership ownership, void* content,
|
2015-03-29 01:22:11 +03:00
|
|
|
uint64_t extraData, void* closure);
|
2013-11-15 08:42:34 +04:00
|
|
|
|
2018-03-28 10:23:17 +03:00
|
|
|
/**
|
|
|
|
* Called when the transferring objects are checked. If this function returns
|
|
|
|
* false, the serialization ends throwing a DataCloneError exception.
|
|
|
|
*/
|
|
|
|
typedef bool (*CanTransferStructuredCloneOp)(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2020-01-24 10:57:56 +03:00
|
|
|
bool* sameProcessScopeRequired,
|
2018-03-28 10:23:17 +03:00
|
|
|
void* closure);
|
|
|
|
|
2020-02-19 12:25:52 +03:00
|
|
|
/**
|
|
|
|
* Called when a SharedArrayBuffer (including one owned by a Wasm memory object)
|
|
|
|
* has been processed in context `cx` by structured cloning. If `receiving` is
|
|
|
|
* true then the SAB has been received from a channel and a new SAB object has
|
|
|
|
* been created; if false then an existing SAB has been serialized onto a
|
|
|
|
* channel.
|
|
|
|
*
|
|
|
|
* If the callback returns false then the clone operation (read or write) will
|
|
|
|
* signal a failure.
|
|
|
|
*/
|
|
|
|
typedef bool (*SharedArrayBufferClonedOp)(JSContext* cx, bool receiving,
|
|
|
|
void* closure);
|
|
|
|
|
2013-08-20 10:43:47 +04:00
|
|
|
struct JSStructuredCloneCallbacks {
|
|
|
|
ReadStructuredCloneOp read;
|
|
|
|
WriteStructuredCloneOp write;
|
|
|
|
StructuredCloneErrorOp reportError;
|
2013-11-15 08:42:34 +04:00
|
|
|
ReadTransferStructuredCloneOp readTransfer;
|
|
|
|
TransferStructuredCloneOp writeTransfer;
|
|
|
|
FreeTransferStructuredCloneOp freeTransfer;
|
2018-03-28 10:23:17 +03:00
|
|
|
CanTransferStructuredCloneOp canTransfer;
|
2020-02-19 12:25:52 +03:00
|
|
|
SharedArrayBufferClonedOp sabCloned;
|
2013-08-20 10:43:47 +04:00
|
|
|
};
|
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
enum OwnTransferablePolicy {
|
2018-04-25 22:15:51 +03:00
|
|
|
/**
|
|
|
|
* The buffer owns any Transferables that it might contain, and should
|
|
|
|
* properly release them upon destruction.
|
|
|
|
*/
|
2016-04-22 13:04:20 +03:00
|
|
|
OwnsTransferablesIfAny,
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-25 22:15:51 +03:00
|
|
|
/**
|
|
|
|
* Do not free any Transferables within this buffer when deleting it. This
|
|
|
|
* is used to mark as clone buffer as containing data from another process,
|
|
|
|
* and so it can't legitimately contain pointers. If the buffer claims to
|
|
|
|
* have transferables, it's a bug or an attack. This is also used for
|
|
|
|
* abandon(), where a buffer still contains raw data but the ownership has
|
|
|
|
* been given over to some other entity.
|
|
|
|
*/
|
2016-04-22 13:04:20 +03:00
|
|
|
IgnoreTransferablesIfAny,
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-25 22:15:51 +03:00
|
|
|
/**
|
|
|
|
* A buffer that cannot contain Transferables at all. This usually means
|
|
|
|
* the buffer is empty (not yet filled in, or having been cleared).
|
|
|
|
*/
|
2016-04-22 13:04:20 +03:00
|
|
|
NoTransferables
|
|
|
|
};
|
|
|
|
|
2017-04-03 15:18:15 +03:00
|
|
|
namespace js {
|
|
|
|
class SharedArrayRawBuffer;
|
|
|
|
|
|
|
|
class SharedArrayRawBufferRefs {
|
|
|
|
public:
|
|
|
|
SharedArrayRawBufferRefs() = default;
|
|
|
|
SharedArrayRawBufferRefs(SharedArrayRawBufferRefs&& other) = default;
|
|
|
|
SharedArrayRawBufferRefs& operator=(SharedArrayRawBufferRefs&& other);
|
|
|
|
~SharedArrayRawBufferRefs();
|
|
|
|
|
2021-02-12 07:09:22 +03:00
|
|
|
[[nodiscard]] bool acquire(JSContext* cx, SharedArrayRawBuffer* rawbuf);
|
|
|
|
[[nodiscard]] bool acquireAll(JSContext* cx,
|
|
|
|
const SharedArrayRawBufferRefs& that);
|
2017-04-03 15:18:15 +03:00
|
|
|
void takeOwnership(SharedArrayRawBufferRefs&&);
|
|
|
|
void releaseAll();
|
|
|
|
|
|
|
|
private:
|
|
|
|
js::Vector<js::SharedArrayRawBuffer*, 0, js::SystemAllocPolicy> refs_;
|
|
|
|
};
|
2018-03-16 02:56:09 +03:00
|
|
|
|
|
|
|
template <typename T, typename AllocPolicy>
|
|
|
|
struct BufferIterator;
|
2017-04-03 15:18:15 +03:00
|
|
|
} // namespace js
|
|
|
|
|
2018-03-16 00:04:24 +03:00
|
|
|
/**
|
|
|
|
* JSStructuredCloneData represents structured clone data together with the
|
|
|
|
* information needed to read/write/transfer/free the records within it, in the
|
|
|
|
* form of a set of callbacks.
|
|
|
|
*/
|
2018-11-19 20:02:47 +03:00
|
|
|
class MOZ_NON_MEMMOVABLE JS_PUBLIC_API JSStructuredCloneData {
|
2018-03-16 02:56:09 +03:00
|
|
|
public:
|
|
|
|
using BufferList = mozilla::BufferList<js::SystemAllocPolicy>;
|
|
|
|
using Iterator = BufferList::IterImpl;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-16 02:56:09 +03:00
|
|
|
private:
|
2016-04-22 13:04:20 +03:00
|
|
|
static const size_t kStandardCapacity = 4096;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-16 02:56:09 +03:00
|
|
|
BufferList bufList_;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-03 21:17:33 +03:00
|
|
|
// The (address space, thread) scope within which this clone is valid. Note
|
|
|
|
// that this must be either set during construction, or start out as
|
|
|
|
// Unassigned and transition once to something else.
|
|
|
|
JS::StructuredCloneScope scope_;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-06-20 02:48:03 +03:00
|
|
|
const JSStructuredCloneCallbacks* callbacks_ = nullptr;
|
|
|
|
void* closure_ = nullptr;
|
|
|
|
OwnTransferablePolicy ownTransferables_ =
|
2016-04-22 13:04:20 +03:00
|
|
|
OwnTransferablePolicy::NoTransferables;
|
|
|
|
js::SharedArrayRawBufferRefs refsHeld_;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
friend struct JSStructuredCloneWriter;
|
|
|
|
friend class JS_PUBLIC_API JSAutoStructuredCloneBuffer;
|
|
|
|
template <typename T, typename AllocPolicy>
|
2018-04-25 01:21:47 +03:00
|
|
|
friend struct js::BufferIterator;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
public:
|
2018-04-25 01:21:47 +03:00
|
|
|
// The constructor must be infallible but SystemAllocPolicy is not, so both
|
|
|
|
// the initial size and initial capacity of the BufferList must be zero.
|
2018-04-03 21:17:33 +03:00
|
|
|
explicit JSStructuredCloneData(JS::StructuredCloneScope scope)
|
2018-05-30 22:15:35 +03:00
|
|
|
: bufList_(0, 0, kStandardCapacity, js::SystemAllocPolicy()),
|
|
|
|
scope_(scope),
|
2016-04-22 13:04:20 +03:00
|
|
|
callbacks_(nullptr),
|
|
|
|
closure_(nullptr),
|
2018-03-16 00:04:24 +03:00
|
|
|
ownTransferables_(OwnTransferablePolicy::NoTransferables) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-16 00:04:24 +03:00
|
|
|
// Steal the raw data from a BufferList. In this case, we don't know the
|
|
|
|
// scope and none of the callback info is assigned yet.
|
2018-04-03 21:17:33 +03:00
|
|
|
JSStructuredCloneData(BufferList&& buffers, JS::StructuredCloneScope scope)
|
|
|
|
: bufList_(std::move(buffers)),
|
|
|
|
scope_(scope),
|
2016-04-22 13:04:20 +03:00
|
|
|
callbacks_(nullptr),
|
2018-04-03 21:17:33 +03:00
|
|
|
closure_(nullptr),
|
|
|
|
ownTransferables_(OwnTransferablePolicy::NoTransferables) {}
|
|
|
|
MOZ_IMPLICIT JSStructuredCloneData(BufferList&& buffers)
|
|
|
|
: JSStructuredCloneData(std::move(buffers),
|
|
|
|
JS::StructuredCloneScope::Unassigned) {}
|
2018-03-16 02:56:09 +03:00
|
|
|
JSStructuredCloneData(JSStructuredCloneData&& other) = default;
|
2018-04-03 21:17:33 +03:00
|
|
|
JSStructuredCloneData& operator=(JSStructuredCloneData&& other) = default;
|
2018-03-16 02:56:09 +03:00
|
|
|
~JSStructuredCloneData() { discardTransferables(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-16 02:56:09 +03:00
|
|
|
void setCallbacks(const JSStructuredCloneCallbacks* callbacks, void* closure,
|
2018-03-16 00:04:24 +03:00
|
|
|
OwnTransferablePolicy policy) {
|
|
|
|
callbacks_ = callbacks;
|
|
|
|
closure_ = closure;
|
|
|
|
ownTransferables_ = policy;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 07:09:22 +03:00
|
|
|
[[nodiscard]] bool Init(size_t initialCapacity = 0) {
|
2018-03-16 02:56:09 +03:00
|
|
|
return bufList_.Init(0, initialCapacity);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-01-24 10:57:56 +03:00
|
|
|
JS::StructuredCloneScope scope() const {
|
|
|
|
if (scope_ == JS::StructuredCloneScope::UnknownDestination) {
|
|
|
|
return JS::StructuredCloneScope::DifferentProcess;
|
|
|
|
}
|
|
|
|
return scope_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sameProcessScopeRequired() {
|
|
|
|
if (scope_ == JS::StructuredCloneScope::UnknownDestination) {
|
|
|
|
scope_ = JS::StructuredCloneScope::SameProcess;
|
|
|
|
}
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-01-24 10:57:28 +03:00
|
|
|
void initScope(JS::StructuredCloneScope newScope) {
|
2018-03-16 02:56:09 +03:00
|
|
|
MOZ_ASSERT(Size() == 0, "initScope() of nonempty JSStructuredCloneData");
|
2020-01-24 10:57:28 +03:00
|
|
|
if (scope() != JS::StructuredCloneScope::Unassigned) {
|
|
|
|
MOZ_ASSERT(scope() == newScope,
|
2018-03-16 02:56:09 +03:00
|
|
|
"Cannot change scope after it has been initialized");
|
|
|
|
}
|
2020-01-24 10:57:28 +03:00
|
|
|
scope_ = newScope;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-04-03 21:17:33 +03:00
|
|
|
size_t Size() const { return bufList_.Size(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-03 21:17:33 +03:00
|
|
|
const Iterator Start() const { return bufList_.Iter(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2021-02-12 07:09:22 +03:00
|
|
|
[[nodiscard]] bool Advance(Iterator& iter, size_t distance) const {
|
2018-04-03 21:17:33 +03:00
|
|
|
return iter.AdvanceAcrossSegments(bufList_, distance);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 07:09:22 +03:00
|
|
|
[[nodiscard]] bool ReadBytes(Iterator& iter, char* buffer,
|
|
|
|
size_t size) const {
|
2018-03-16 02:56:09 +03:00
|
|
|
return bufList_.ReadBytes(iter, buffer, size);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-04-03 21:17:33 +03:00
|
|
|
// Append new data to the end of the buffer.
|
2021-02-12 07:09:22 +03:00
|
|
|
[[nodiscard]] bool AppendBytes(const char* data, size_t size) {
|
2020-01-24 10:57:28 +03:00
|
|
|
MOZ_ASSERT(scope() != JS::StructuredCloneScope::Unassigned);
|
2018-04-03 21:17:33 +03:00
|
|
|
return bufList_.WriteBytes(data, size);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-04-03 21:17:33 +03:00
|
|
|
// Update data stored within the existing buffer. There must be at least
|
|
|
|
// 'size' bytes between the position of 'iter' and the end of the buffer.
|
2021-02-12 07:09:22 +03:00
|
|
|
[[nodiscard]] bool UpdateBytes(Iterator& iter, const char* data,
|
|
|
|
size_t size) const {
|
2020-01-24 10:57:28 +03:00
|
|
|
MOZ_ASSERT(scope() != JS::StructuredCloneScope::Unassigned);
|
2018-03-16 02:56:09 +03:00
|
|
|
while (size > 0) {
|
2018-04-03 21:17:33 +03:00
|
|
|
size_t remaining = iter.RemainingInSegment();
|
|
|
|
size_t nbytes = std::min(remaining, size);
|
|
|
|
memcpy(iter.Data(), data, nbytes);
|
2018-03-16 02:56:09 +03:00
|
|
|
data += nbytes;
|
|
|
|
size -= nbytes;
|
2018-04-03 21:17:33 +03:00
|
|
|
iter.Advance(bufList_, nbytes);
|
2018-03-16 02:56:09 +03:00
|
|
|
}
|
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 02:56:09 +03:00
|
|
|
char* AllocateBytes(size_t maxSize, size_t* size) {
|
|
|
|
return bufList_.AllocateBytes(maxSize, size);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 02:56:09 +03:00
|
|
|
void Clear() {
|
|
|
|
discardTransferables();
|
|
|
|
bufList_.Clear();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 00:04:24 +03:00
|
|
|
// Return a new read-only JSStructuredCloneData that "borrows" the contents
|
2015-10-17 20:27:16 +03:00
|
|
|
// of |this|. Its lifetime should not exceed the donor's. This is only
|
2018-04-03 21:17:33 +03:00
|
|
|
// allowed for DifferentProcess clones, so finalization of the borrowing
|
|
|
|
// clone will do nothing.
|
2018-03-16 02:56:09 +03:00
|
|
|
JSStructuredCloneData Borrow(Iterator& iter, size_t size,
|
|
|
|
bool* success) const {
|
2020-01-24 10:57:28 +03:00
|
|
|
MOZ_ASSERT(scope() == JS::StructuredCloneScope::DifferentProcess);
|
2016-04-22 13:04:20 +03:00
|
|
|
return JSStructuredCloneData(
|
2020-01-24 10:57:28 +03:00
|
|
|
bufList_.Borrow<js::SystemAllocPolicy>(iter, size, success), scope());
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 02:56:09 +03:00
|
|
|
// Iterate over all contained data, one BufferList segment's worth at a
|
|
|
|
// time, and invoke the given FunctionToApply with the data pointer and
|
|
|
|
// size. The function should return a bool value, and this loop will exit
|
|
|
|
// with false if the function ever returns false.
|
|
|
|
template <typename FunctionToApply>
|
|
|
|
bool ForEachDataChunk(FunctionToApply&& function) const {
|
|
|
|
Iterator iter = bufList_.Iter();
|
|
|
|
while (!iter.Done()) {
|
|
|
|
if (!function(iter.Data(), iter.RemainingInSegment())) {
|
|
|
|
return false;
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2018-03-16 02:56:09 +03:00
|
|
|
iter.Advance(bufList_, iter.RemainingInSegment());
|
|
|
|
}
|
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-03-16 02:56:09 +03:00
|
|
|
|
|
|
|
// Append the entire contents of other's bufList_ to our own.
|
2021-02-12 07:09:22 +03:00
|
|
|
[[nodiscard]] bool Append(const JSStructuredCloneData& other) {
|
2020-01-24 10:57:28 +03:00
|
|
|
MOZ_ASSERT(scope() == other.scope());
|
2018-03-16 02:56:09 +03:00
|
|
|
return other.ForEachDataChunk(
|
|
|
|
[&](const char* data, size_t size) { return AppendBytes(data, size); });
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) {
|
|
|
|
return bufList_.SizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void discardTransferables();
|
2020-02-06 16:47:08 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
// This internal method exposes the real value of scope_. It's meant to be
|
|
|
|
// used only when starting the writing.
|
|
|
|
JS::StructuredCloneScope scopeForInternalWriting() const { return scope_; }
|
2016-04-22 13:04:20 +03:00
|
|
|
};
|
|
|
|
|
2018-02-21 06:40:49 +03:00
|
|
|
/**
|
|
|
|
* Implements StructuredDeserialize and StructuredDeserializeWithTransfer.
|
|
|
|
*
|
|
|
|
* Note: If `data` contains transferable objects, it can be read only once.
|
|
|
|
*/
|
2016-04-22 13:04:20 +03:00
|
|
|
JS_PUBLIC_API bool JS_ReadStructuredClone(
|
2020-02-25 19:05:14 +03:00
|
|
|
JSContext* cx, const JSStructuredCloneData& data, uint32_t version,
|
2016-07-21 16:29:42 +03:00
|
|
|
JS::StructuredCloneScope scope, JS::MutableHandleValue vp,
|
2020-02-06 16:46:25 +03:00
|
|
|
const JS::CloneDataPolicy& cloneDataPolicy,
|
2015-03-29 01:22:11 +03:00
|
|
|
const JSStructuredCloneCallbacks* optionalCallbacks, void* closure);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2018-01-15 13:31:35 +03:00
|
|
|
/**
|
2018-02-21 06:40:49 +03:00
|
|
|
* Implements StructuredSerialize, StructuredSerializeForStorage, and
|
|
|
|
* StructuredSerializeWithTransfer.
|
|
|
|
*
|
2018-01-15 13:31:35 +03:00
|
|
|
* Note: If the scope is DifferentProcess then the cloneDataPolicy must deny
|
|
|
|
* shared-memory objects, or an error will be signaled if a shared memory object
|
|
|
|
* is seen.
|
|
|
|
*/
|
2016-04-22 13:04:20 +03:00
|
|
|
JS_PUBLIC_API bool JS_WriteStructuredClone(
|
|
|
|
JSContext* cx, JS::HandleValue v, JSStructuredCloneData* data,
|
2020-02-06 16:46:25 +03:00
|
|
|
JS::StructuredCloneScope scope, const JS::CloneDataPolicy& cloneDataPolicy,
|
2015-03-29 01:22:11 +03:00
|
|
|
const JSStructuredCloneCallbacks* optionalCallbacks, void* closure,
|
|
|
|
JS::HandleValue transferable);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
JS_PUBLIC_API bool JS_StructuredCloneHasTransferables(
|
|
|
|
JSStructuredCloneData& data, bool* hasTransferable);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2018-11-19 20:02:47 +03:00
|
|
|
JS_PUBLIC_API bool JS_StructuredClone(
|
2015-03-29 01:22:11 +03:00
|
|
|
JSContext* cx, JS::HandleValue v, JS::MutableHandleValue vp,
|
|
|
|
const JSStructuredCloneCallbacks* optionalCallbacks, void* closure);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2018-03-16 06:08:58 +03:00
|
|
|
/**
|
|
|
|
* The C-style API calls to read and write structured clones are fragile --
|
|
|
|
* they rely on the caller to properly handle ownership of the clone data, and
|
|
|
|
* the handling of the input data as well as the interpretation of the contents
|
|
|
|
* of the clone buffer are dependent on the callbacks passed in. If you
|
|
|
|
* serialize and deserialize with different callbacks, the results are
|
|
|
|
* questionable.
|
|
|
|
*
|
|
|
|
* JSAutoStructuredCloneBuffer wraps things up in an RAII class for data
|
|
|
|
* management, and uses the same callbacks for both writing and reading
|
|
|
|
* (serializing and deserializing).
|
|
|
|
*/
|
2018-11-19 20:02:47 +03:00
|
|
|
class JS_PUBLIC_API JSAutoStructuredCloneBuffer {
|
2016-04-22 13:04:20 +03:00
|
|
|
JSStructuredCloneData data_;
|
2013-08-20 10:43:47 +04:00
|
|
|
uint32_t version_;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-08-20 10:43:47 +04:00
|
|
|
public:
|
2016-07-21 16:29:42 +03:00
|
|
|
JSAutoStructuredCloneBuffer(JS::StructuredCloneScope scope,
|
|
|
|
const JSStructuredCloneCallbacks* callbacks,
|
|
|
|
void* closure)
|
2020-01-24 10:57:28 +03:00
|
|
|
: data_(scope), version_(JS_STRUCTURED_CLONE_VERSION) {
|
2018-03-16 00:04:24 +03:00
|
|
|
data_.setCallbacks(callbacks, closure,
|
|
|
|
OwnTransferablePolicy::NoTransferables);
|
2016-04-22 13:04:20 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JSAutoStructuredCloneBuffer(JSAutoStructuredCloneBuffer&& other);
|
|
|
|
JSAutoStructuredCloneBuffer& operator=(JSAutoStructuredCloneBuffer&& other);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-08-20 10:43:47 +04:00
|
|
|
~JSAutoStructuredCloneBuffer() { clear(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-04-22 13:04:20 +03:00
|
|
|
JSStructuredCloneData& data() { return data_; }
|
|
|
|
bool empty() const { return !data_.Size(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-16 03:11:31 +03:00
|
|
|
void clear();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-01-24 10:57:28 +03:00
|
|
|
JS::StructuredCloneScope scope() const { return data_.scope(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
|
|
|
* Adopt some memory. It will be automatically freed by the destructor.
|
|
|
|
* data must have been allocated by the JS engine (e.g., extracted via
|
|
|
|
* JSAutoStructuredCloneBuffer::steal).
|
|
|
|
*/
|
2016-04-22 13:04:20 +03:00
|
|
|
void adopt(JSStructuredCloneData&& data,
|
|
|
|
uint32_t version = JS_STRUCTURED_CLONE_VERSION,
|
2015-10-12 06:21:03 +03:00
|
|
|
const JSStructuredCloneCallbacks* callbacks = nullptr,
|
|
|
|
void* closure = nullptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2016-04-22 13:04:20 +03:00
|
|
|
* Release the buffer and transfer ownership to the caller.
|
2015-10-17 20:27:16 +03:00
|
|
|
*/
|
2016-04-22 13:04:20 +03:00
|
|
|
void steal(JSStructuredCloneData* data, uint32_t* versionp = nullptr,
|
2015-10-12 06:21:03 +03:00
|
|
|
const JSStructuredCloneCallbacks** callbacks = nullptr,
|
|
|
|
void** closure = nullptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
|
|
|
* Abandon ownership of any transferable objects stored in the buffer,
|
|
|
|
* without freeing the buffer itself. Useful when copying the data out into
|
2016-04-22 13:04:20 +03:00
|
|
|
* an external container, though note that you will need to use adopt() to
|
|
|
|
* properly release that data eventually.
|
2015-10-17 20:27:16 +03:00
|
|
|
*/
|
2016-04-22 13:04:20 +03:00
|
|
|
void abandon() {
|
|
|
|
data_.ownTransferables_ = OwnTransferablePolicy::IgnoreTransferablesIfAny;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool read(JSContext* cx, JS::MutableHandleValue vp,
|
2020-02-06 16:46:25 +03:00
|
|
|
const JS::CloneDataPolicy& cloneDataPolicy = JS::CloneDataPolicy(),
|
2015-03-29 01:22:11 +03:00
|
|
|
const JSStructuredCloneCallbacks* optionalCallbacks = nullptr,
|
|
|
|
void* closure = nullptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool write(JSContext* cx, JS::HandleValue v,
|
|
|
|
const JSStructuredCloneCallbacks* optionalCallbacks = nullptr,
|
|
|
|
void* closure = nullptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool write(JSContext* cx, JS::HandleValue v, JS::HandleValue transferable,
|
2020-02-06 16:46:25 +03:00
|
|
|
const JS::CloneDataPolicy& cloneDataPolicy,
|
2015-03-29 01:22:11 +03:00
|
|
|
const JSStructuredCloneCallbacks* optionalCallbacks = nullptr,
|
|
|
|
void* closure = nullptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-07-26 00:53:41 +03:00
|
|
|
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) {
|
|
|
|
return data_.SizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-07-26 00:53:41 +03:00
|
|
|
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) {
|
|
|
|
return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-08-20 10:43:47 +04:00
|
|
|
private:
|
|
|
|
// Copy and assignment are not supported.
|
2015-03-29 01:22:11 +03:00
|
|
|
JSAutoStructuredCloneBuffer(const JSAutoStructuredCloneBuffer& other) =
|
|
|
|
delete;
|
|
|
|
JSAutoStructuredCloneBuffer& operator=(
|
|
|
|
const JSAutoStructuredCloneBuffer& other) = delete;
|
2013-08-20 10:43:47 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// The range of tag values the application may use for its own custom object
|
|
|
|
// types.
|
|
|
|
#define JS_SCTAG_USER_MIN ((uint32_t)0xFFFF8000)
|
|
|
|
#define JS_SCTAG_USER_MAX ((uint32_t)0xFFFFFFFF)
|
|
|
|
|
|
|
|
#define JS_SCERR_RECURSION 0
|
|
|
|
#define JS_SCERR_TRANSFERABLE 1
|
2015-08-10 18:13:24 +03:00
|
|
|
#define JS_SCERR_DUP_TRANSFERABLE 2
|
2016-06-04 10:24:28 +03:00
|
|
|
#define JS_SCERR_UNSUPPORTED_TYPE 3
|
2017-10-31 15:38:28 +03:00
|
|
|
#define JS_SCERR_SHMEM_TRANSFERABLE 4
|
2020-02-07 22:13:15 +03:00
|
|
|
#define JS_SCERR_TYPED_ARRAY_DETACHED 5
|
|
|
|
#define JS_SCERR_WASM_NO_TRANSFER 6
|
2020-02-13 11:37:43 +03:00
|
|
|
#define JS_SCERR_NOT_CLONABLE 7
|
|
|
|
#define JS_SCERR_NOT_CLONABLE_WITH_COOP_COEP 8
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JS_PUBLIC_API bool JS_ReadUint32Pair(JSStructuredCloneReader* r, uint32_t* p1,
|
|
|
|
uint32_t* p2);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JS_PUBLIC_API bool JS_ReadBytes(JSStructuredCloneReader* r, void* p,
|
|
|
|
size_t len);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JS_PUBLIC_API bool JS_ReadTypedArray(JSStructuredCloneReader* r,
|
|
|
|
JS::MutableHandleValue vp);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JS_PUBLIC_API bool JS_WriteUint32Pair(JSStructuredCloneWriter* w, uint32_t tag,
|
|
|
|
uint32_t data);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JS_PUBLIC_API bool JS_WriteBytes(JSStructuredCloneWriter* w, const void* p,
|
|
|
|
size_t len);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JS_PUBLIC_API bool JS_WriteString(JSStructuredCloneWriter* w,
|
|
|
|
JS::HandleString str);
|
2015-02-05 21:50:43 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JS_PUBLIC_API bool JS_WriteTypedArray(JSStructuredCloneWriter* w,
|
|
|
|
JS::HandleValue v);
|
2013-08-20 10:43:47 +04:00
|
|
|
|
2016-04-19 11:26:00 +03:00
|
|
|
JS_PUBLIC_API bool JS_ObjectNotWritten(JSStructuredCloneWriter* w,
|
|
|
|
JS::HandleObject obj);
|
|
|
|
|
2016-08-17 08:54:00 +03:00
|
|
|
JS_PUBLIC_API JS::StructuredCloneScope JS_GetStructuredCloneScope(
|
|
|
|
JSStructuredCloneWriter* w);
|
|
|
|
|
2013-08-20 10:43:47 +04:00
|
|
|
#endif /* js_StructuredClone_h */
|