![]() |
|
|||
File indexing completed on 2025-02-21 10:05:27
0001 // Copyright 2021 the V8 project authors. All rights reserved. 0002 // Use of this source code is governed by a BSD-style license that can be 0003 // found in the LICENSE file. 0004 0005 #ifndef INCLUDE_V8_VALUE_SERIALIZER_H_ 0006 #define INCLUDE_V8_VALUE_SERIALIZER_H_ 0007 0008 #include <stddef.h> 0009 #include <stdint.h> 0010 0011 #include <memory> 0012 #include <utility> 0013 0014 #include "v8-local-handle.h" // NOLINT(build/include_directory) 0015 #include "v8-maybe.h" // NOLINT(build/include_directory) 0016 #include "v8config.h" // NOLINT(build/include_directory) 0017 0018 namespace v8 { 0019 0020 class ArrayBuffer; 0021 class Isolate; 0022 class Object; 0023 class SharedArrayBuffer; 0024 class String; 0025 class WasmModuleObject; 0026 class Value; 0027 0028 namespace internal { 0029 struct ScriptStreamingData; 0030 class SharedObjectConveyorHandles; 0031 class ValueDeserializer; 0032 class ValueSerializer; 0033 } // namespace internal 0034 0035 /** 0036 * A move-only class for managing the lifetime of shared value conveyors used 0037 * by V8 to keep JS shared values alive in transit when serialized. 0038 * 0039 * This class is not directly constructible and is always passed to a 0040 * ValueSerializer::Delegate via ValueSerializer::SetSharedValueConveyor. 0041 * 0042 * The embedder must not destruct the SharedValueConveyor until the associated 0043 * serialized data will no longer be deserialized. 0044 */ 0045 class V8_EXPORT SharedValueConveyor final { 0046 public: 0047 SharedValueConveyor(SharedValueConveyor&&) noexcept; 0048 ~SharedValueConveyor(); 0049 0050 SharedValueConveyor& operator=(SharedValueConveyor&&) noexcept; 0051 0052 private: 0053 friend class internal::ValueSerializer; 0054 friend class internal::ValueDeserializer; 0055 0056 explicit SharedValueConveyor(Isolate* isolate); 0057 0058 std::unique_ptr<internal::SharedObjectConveyorHandles> private_; 0059 }; 0060 0061 /** 0062 * Value serialization compatible with the HTML structured clone algorithm. 0063 * The format is backward-compatible (i.e. safe to store to disk). 0064 */ 0065 class V8_EXPORT ValueSerializer { 0066 public: 0067 class V8_EXPORT Delegate { 0068 public: 0069 virtual ~Delegate() = default; 0070 0071 /** 0072 * Handles the case where a DataCloneError would be thrown in the structured 0073 * clone spec. Other V8 embedders may throw some other appropriate exception 0074 * type. 0075 */ 0076 virtual void ThrowDataCloneError(Local<String> message) = 0; 0077 0078 /** 0079 * The embedder overrides this method to enable custom host object filter 0080 * with Delegate::IsHostObject. 0081 * 0082 * This method is called at most once per serializer. 0083 */ 0084 virtual bool HasCustomHostObject(Isolate* isolate); 0085 0086 /** 0087 * The embedder overrides this method to determine if an JS object is a 0088 * host object and needs to be serialized by the host. 0089 */ 0090 virtual Maybe<bool> IsHostObject(Isolate* isolate, Local<Object> object); 0091 0092 /** 0093 * The embedder overrides this method to write some kind of host object, if 0094 * possible. If not, a suitable exception should be thrown and 0095 * Nothing<bool>() returned. 0096 */ 0097 virtual Maybe<bool> WriteHostObject(Isolate* isolate, Local<Object> object); 0098 0099 /** 0100 * Called when the ValueSerializer is going to serialize a 0101 * SharedArrayBuffer object. The embedder must return an ID for the 0102 * object, using the same ID if this SharedArrayBuffer has already been 0103 * serialized in this buffer. When deserializing, this ID will be passed to 0104 * ValueDeserializer::GetSharedArrayBufferFromId as |clone_id|. 0105 * 0106 * If the object cannot be serialized, an 0107 * exception should be thrown and Nothing<uint32_t>() returned. 0108 */ 0109 virtual Maybe<uint32_t> GetSharedArrayBufferId( 0110 Isolate* isolate, Local<SharedArrayBuffer> shared_array_buffer); 0111 0112 virtual Maybe<uint32_t> GetWasmModuleTransferId( 0113 Isolate* isolate, Local<WasmModuleObject> module); 0114 0115 /** 0116 * Called when the first shared value is serialized. All subsequent shared 0117 * values will use the same conveyor. 0118 * 0119 * The embedder must ensure the lifetime of the conveyor matches the 0120 * lifetime of the serialized data. 0121 * 0122 * If the embedder supports serializing shared values, this method should 0123 * return true. Otherwise the embedder should throw an exception and return 0124 * false. 0125 * 0126 * This method is called at most once per serializer. 0127 */ 0128 virtual bool AdoptSharedValueConveyor(Isolate* isolate, 0129 SharedValueConveyor&& conveyor); 0130 0131 /** 0132 * Allocates memory for the buffer of at least the size provided. The actual 0133 * size (which may be greater or equal) is written to |actual_size|. If no 0134 * buffer has been allocated yet, nullptr will be provided. 0135 * 0136 * If the memory cannot be allocated, nullptr should be returned. 0137 * |actual_size| will be ignored. It is assumed that |old_buffer| is still 0138 * valid in this case and has not been modified. 0139 * 0140 * The default implementation uses the stdlib's `realloc()` function. 0141 */ 0142 virtual void* ReallocateBufferMemory(void* old_buffer, size_t size, 0143 size_t* actual_size); 0144 0145 /** 0146 * Frees a buffer allocated with |ReallocateBufferMemory|. 0147 * 0148 * The default implementation uses the stdlib's `free()` function. 0149 */ 0150 virtual void FreeBufferMemory(void* buffer); 0151 }; 0152 0153 explicit ValueSerializer(Isolate* isolate); 0154 ValueSerializer(Isolate* isolate, Delegate* delegate); 0155 ~ValueSerializer(); 0156 0157 /** 0158 * Writes out a header, which includes the format version. 0159 */ 0160 void WriteHeader(); 0161 0162 /** 0163 * Serializes a JavaScript value into the buffer. 0164 */ 0165 V8_WARN_UNUSED_RESULT Maybe<bool> WriteValue(Local<Context> context, 0166 Local<Value> value); 0167 0168 /** 0169 * Returns the stored data (allocated using the delegate's 0170 * ReallocateBufferMemory) and its size. This serializer should not be used 0171 * once the buffer is released. The contents are undefined if a previous write 0172 * has failed. Ownership of the buffer is transferred to the caller. 0173 */ 0174 V8_WARN_UNUSED_RESULT std::pair<uint8_t*, size_t> Release(); 0175 0176 /** 0177 * Marks an ArrayBuffer as havings its contents transferred out of band. 0178 * Pass the corresponding ArrayBuffer in the deserializing context to 0179 * ValueDeserializer::TransferArrayBuffer. 0180 */ 0181 void TransferArrayBuffer(uint32_t transfer_id, 0182 Local<ArrayBuffer> array_buffer); 0183 0184 /** 0185 * Indicate whether to treat ArrayBufferView objects as host objects, 0186 * i.e. pass them to Delegate::WriteHostObject. This should not be 0187 * called when no Delegate was passed. 0188 * 0189 * The default is not to treat ArrayBufferViews as host objects. 0190 */ 0191 void SetTreatArrayBufferViewsAsHostObjects(bool mode); 0192 0193 /** 0194 * Write raw data in various common formats to the buffer. 0195 * Note that integer types are written in base-128 varint format, not with a 0196 * binary copy. For use during an override of Delegate::WriteHostObject. 0197 */ 0198 void WriteUint32(uint32_t value); 0199 void WriteUint64(uint64_t value); 0200 void WriteDouble(double value); 0201 void WriteRawBytes(const void* source, size_t length); 0202 0203 ValueSerializer(const ValueSerializer&) = delete; 0204 void operator=(const ValueSerializer&) = delete; 0205 0206 private: 0207 struct PrivateData; 0208 PrivateData* private_; 0209 }; 0210 0211 /** 0212 * Deserializes values from data written with ValueSerializer, or a compatible 0213 * implementation. 0214 */ 0215 class V8_EXPORT ValueDeserializer { 0216 public: 0217 class V8_EXPORT Delegate { 0218 public: 0219 virtual ~Delegate() = default; 0220 0221 /** 0222 * The embedder overrides this method to read some kind of host object, if 0223 * possible. If not, a suitable exception should be thrown and 0224 * MaybeLocal<Object>() returned. 0225 */ 0226 virtual MaybeLocal<Object> ReadHostObject(Isolate* isolate); 0227 0228 /** 0229 * Get a WasmModuleObject given a transfer_id previously provided 0230 * by ValueSerializer::Delegate::GetWasmModuleTransferId 0231 */ 0232 virtual MaybeLocal<WasmModuleObject> GetWasmModuleFromId( 0233 Isolate* isolate, uint32_t transfer_id); 0234 0235 /** 0236 * Get a SharedArrayBuffer given a clone_id previously provided 0237 * by ValueSerializer::Delegate::GetSharedArrayBufferId 0238 */ 0239 virtual MaybeLocal<SharedArrayBuffer> GetSharedArrayBufferFromId( 0240 Isolate* isolate, uint32_t clone_id); 0241 0242 /** 0243 * Get the SharedValueConveyor previously provided by 0244 * ValueSerializer::Delegate::AdoptSharedValueConveyor. 0245 */ 0246 virtual const SharedValueConveyor* GetSharedValueConveyor(Isolate* isolate); 0247 }; 0248 0249 ValueDeserializer(Isolate* isolate, const uint8_t* data, size_t size); 0250 ValueDeserializer(Isolate* isolate, const uint8_t* data, size_t size, 0251 Delegate* delegate); 0252 ~ValueDeserializer(); 0253 0254 /** 0255 * Reads and validates a header (including the format version). 0256 * May, for example, reject an invalid or unsupported wire format. 0257 */ 0258 V8_WARN_UNUSED_RESULT Maybe<bool> ReadHeader(Local<Context> context); 0259 0260 /** 0261 * Deserializes a JavaScript value from the buffer. 0262 */ 0263 V8_WARN_UNUSED_RESULT MaybeLocal<Value> ReadValue(Local<Context> context); 0264 0265 /** 0266 * Accepts the array buffer corresponding to the one passed previously to 0267 * ValueSerializer::TransferArrayBuffer. 0268 */ 0269 void TransferArrayBuffer(uint32_t transfer_id, 0270 Local<ArrayBuffer> array_buffer); 0271 0272 /** 0273 * Similar to TransferArrayBuffer, but for SharedArrayBuffer. 0274 * The id is not necessarily in the same namespace as unshared ArrayBuffer 0275 * objects. 0276 */ 0277 void TransferSharedArrayBuffer(uint32_t id, 0278 Local<SharedArrayBuffer> shared_array_buffer); 0279 0280 /** 0281 * Must be called before ReadHeader to enable support for reading the legacy 0282 * wire format (i.e., which predates this being shipped). 0283 * 0284 * Don't use this unless you need to read data written by previous versions of 0285 * blink::ScriptValueSerializer. 0286 */ 0287 void SetSupportsLegacyWireFormat(bool supports_legacy_wire_format); 0288 0289 /** 0290 * Reads the underlying wire format version. Likely mostly to be useful to 0291 * legacy code reading old wire format versions. Must be called after 0292 * ReadHeader. 0293 */ 0294 uint32_t GetWireFormatVersion() const; 0295 0296 /** 0297 * Reads raw data in various common formats to the buffer. 0298 * Note that integer types are read in base-128 varint format, not with a 0299 * binary copy. For use during an override of Delegate::ReadHostObject. 0300 */ 0301 V8_WARN_UNUSED_RESULT bool ReadUint32(uint32_t* value); 0302 V8_WARN_UNUSED_RESULT bool ReadUint64(uint64_t* value); 0303 V8_WARN_UNUSED_RESULT bool ReadDouble(double* value); 0304 V8_WARN_UNUSED_RESULT bool ReadRawBytes(size_t length, const void** data); 0305 0306 ValueDeserializer(const ValueDeserializer&) = delete; 0307 void operator=(const ValueDeserializer&) = delete; 0308 0309 private: 0310 struct PrivateData; 0311 PrivateData* private_; 0312 }; 0313 0314 } // namespace v8 0315 0316 #endif // INCLUDE_V8_VALUE_SERIALIZER_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |