OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef ScriptValueSerializer_h | 5 #ifndef ScriptValueSerializer_h |
6 #define ScriptValueSerializer_h | 6 #define ScriptValueSerializer_h |
7 | 7 |
8 #include "bindings/core/v8/SerializationTag.h" | 8 #include "bindings/core/v8/SerializationTag.h" |
9 #include "bindings/core/v8/SerializedScriptValue.h" | 9 #include "bindings/core/v8/SerializedScriptValue.h" |
10 #include "bindings/core/v8/V8Binding.h" | 10 #include "bindings/core/v8/V8Binding.h" |
11 #include "public/platform/WebCrypto.h" | |
12 #include "public/platform/WebCryptoKey.h" | |
13 #include "public/platform/WebCryptoKeyAlgorithm.h" | |
14 #include "wtf/ArrayBufferContents.h" | 11 #include "wtf/ArrayBufferContents.h" |
15 #include "wtf/HashMap.h" | 12 #include "wtf/HashMap.h" |
16 #include "wtf/Noncopyable.h" | 13 #include "wtf/Noncopyable.h" |
17 #include "wtf/Vector.h" | 14 #include "wtf/Vector.h" |
18 #include "wtf/text/WTFString.h" | 15 #include "wtf/text/WTFString.h" |
19 #include <v8.h> | 16 #include <v8.h> |
20 | 17 |
21 namespace WTF { | 18 namespace WTF { |
22 | 19 |
23 class ArrayBuffer; | 20 class ArrayBuffer; |
24 class ArrayBufferView; | 21 class ArrayBufferView; |
25 | 22 |
26 } | 23 } |
27 | 24 |
28 namespace blink { | 25 namespace blink { |
29 | 26 |
30 class File; | 27 class File; |
31 class FileList; | 28 class FileList; |
32 | 29 |
33 namespace SerializedScriptValueInternal { | |
34 | |
35 typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray; | 30 typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray; |
36 | 31 |
37 // V8ObjectMap is a map from V8 objects to arbitrary values of type T. | 32 // V8ObjectMap is a map from V8 objects to arbitrary values of type T. |
38 // V8 objects (or handles to V8 objects) cannot be used as keys in ordinary wtf:
:HashMaps; | 33 // V8 objects (or handles to V8 objects) cannot be used as keys in ordinary wtf:
:HashMaps; |
39 // this class should be used instead. GCObject must be a subtype of v8::Object. | 34 // this class should be used instead. GCObject must be a subtype of v8::Object. |
40 // Suggested usage: | 35 // Suggested usage: |
41 // V8ObjectMap<v8::Object, int> map; | 36 // V8ObjectMap<v8::Object, int> map; |
42 // v8::Handle<v8::Object> obj = ...; | 37 // v8::Handle<v8::Object> obj = ...; |
43 // map.set(obj, 42); | 38 // map.set(obj, 42); |
44 template<typename GCObject, typename T> | 39 template<typename GCObject, typename T> |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 return unsafeHandleFromRawValue(a) == unsafeHandleFromRawValue(b); | 87 return unsafeHandleFromRawValue(a) == unsafeHandleFromRawValue(b); |
93 } | 88 } |
94 // For HashArg. | 89 // For HashArg. |
95 static const bool safeToCompareToEmptyOrDeleted = false; | 90 static const bool safeToCompareToEmptyOrDeleted = false; |
96 }; | 91 }; |
97 | 92 |
98 typedef WTF::HashMap<GCObject*, T, V8HandlePtrHash<GCObject> > HandleToT; | 93 typedef WTF::HashMap<GCObject*, T, V8HandlePtrHash<GCObject> > HandleToT; |
99 HandleToT m_map; | 94 HandleToT m_map; |
100 }; | 95 }; |
101 | 96 |
102 // Writer is responsible for serializing primitive types and storing | 97 // SerializedScriptValueWriter is responsible for serializing primitive types an
d storing |
103 // information used to reconstruct composite types. | 98 // information used to reconstruct composite types. |
104 class Writer { | 99 class SerializedScriptValueWriter { |
105 STACK_ALLOCATED(); | 100 STACK_ALLOCATED(); |
106 WTF_MAKE_NONCOPYABLE(Writer); | 101 WTF_MAKE_NONCOPYABLE(SerializedScriptValueWriter); |
107 public: | 102 public: |
108 typedef UChar BufferValueType; | 103 typedef UChar BufferValueType; |
109 | 104 |
110 Writer() | 105 SerializedScriptValueWriter() |
111 : m_position(0) | 106 : m_position(0) |
112 { | 107 { |
113 } | 108 } |
114 | 109 |
115 // Write functions for primitive types. | 110 // Write functions for primitive types. |
116 | 111 |
117 void writeUndefined(); | 112 void writeUndefined(); |
118 void writeNull(); | 113 void writeNull(); |
119 void writeTrue(); | 114 void writeTrue(); |
120 void writeFalse(); | 115 void writeFalse(); |
121 void writeBooleanObject(bool value); | 116 void writeBooleanObject(bool value); |
122 void writeOneByteString(v8::Handle<v8::String>&); | 117 void writeOneByteString(v8::Handle<v8::String>&); |
123 void writeUCharString(v8::Handle<v8::String>&); | 118 void writeUCharString(v8::Handle<v8::String>&); |
124 void writeStringObject(const char* data, int length); | 119 void writeStringObject(const char* data, int length); |
125 void writeWebCoreString(const String&); | 120 void writeWebCoreString(const String&); |
126 void writeVersion(); | 121 void writeVersion(); |
127 void writeInt32(int32_t value); | 122 void writeInt32(int32_t value); |
128 void writeUint32(uint32_t value); | 123 void writeUint32(uint32_t value); |
129 void writeDate(double numberValue); | 124 void writeDate(double numberValue); |
130 void writeNumber(double number); | 125 void writeNumber(double number); |
131 void writeNumberObject(double number); | 126 void writeNumberObject(double number); |
132 void writeBlob(const String& uuid, const String& type, unsigned long long si
ze); | 127 void writeBlob(const String& uuid, const String& type, unsigned long long si
ze); |
133 void writeBlobIndex(int blobIndex); | 128 void writeBlobIndex(int blobIndex); |
134 void writeDOMFileSystem(int type, const String& name, const String& url); | |
135 void writeFile(const File&); | 129 void writeFile(const File&); |
136 void writeFileIndex(int blobIndex); | 130 void writeFileIndex(int blobIndex); |
137 void writeFileList(const FileList&); | 131 void writeFileList(const FileList&); |
138 void writeFileListIndex(const Vector<int>& blobIndices); | 132 void writeFileListIndex(const Vector<int>& blobIndices); |
139 bool writeCryptoKey(const WebCryptoKey&); | |
140 void writeArrayBuffer(const ArrayBuffer&); | 133 void writeArrayBuffer(const ArrayBuffer&); |
141 void writeArrayBufferView(const ArrayBufferView&); | 134 void writeArrayBufferView(const ArrayBufferView&); |
142 void writeImageData(uint32_t width, uint32_t height, const uint8_t* pixelDat
a, uint32_t pixelDataLength); | 135 void writeImageData(uint32_t width, uint32_t height, const uint8_t* pixelDat
a, uint32_t pixelDataLength); |
143 void writeRegExp(v8::Local<v8::String> pattern, v8::RegExp::Flags); | 136 void writeRegExp(v8::Local<v8::String> pattern, v8::RegExp::Flags); |
144 void writeTransferredMessagePort(uint32_t index); | 137 void writeTransferredMessagePort(uint32_t index); |
145 void writeTransferredArrayBuffer(uint32_t index); | 138 void writeTransferredArrayBuffer(uint32_t index); |
146 void writeObjectReference(uint32_t reference); | 139 void writeObjectReference(uint32_t reference); |
147 void writeObject(uint32_t numProperties); | 140 void writeObject(uint32_t numProperties); |
148 void writeSparseArray(uint32_t numProperties, uint32_t length); | 141 void writeSparseArray(uint32_t numProperties, uint32_t length); |
149 void writeDenseArray(uint32_t numProperties, uint32_t length); | 142 void writeDenseArray(uint32_t numProperties, uint32_t length); |
150 String takeWireString(); | 143 String takeWireString(); |
151 void writeReferenceCount(uint32_t numberOfReferences); | 144 void writeReferenceCount(uint32_t numberOfReferences); |
152 void writeGenerateFreshObject(); | 145 void writeGenerateFreshObject(); |
153 void writeGenerateFreshSparseArray(uint32_t length); | 146 void writeGenerateFreshSparseArray(uint32_t length); |
154 void writeGenerateFreshDenseArray(uint32_t length); | 147 void writeGenerateFreshDenseArray(uint32_t length); |
155 | 148 |
156 protected: | 149 protected: |
157 void doWriteFile(const File&); | 150 void doWriteFile(const File&); |
158 void doWriteArrayBuffer(const ArrayBuffer&); | 151 void doWriteArrayBuffer(const ArrayBuffer&); |
159 void doWriteString(const char* data, int length); | 152 void doWriteString(const char* data, int length); |
160 void doWriteWebCoreString(const String&); | 153 void doWriteWebCoreString(const String&); |
161 void doWriteHmacKey(const WebCryptoKey&); | |
162 void doWriteAesKey(const WebCryptoKey&); | |
163 void doWriteRsaHashedKey(const WebCryptoKey&); | |
164 void doWriteEcKey(const WebCryptoKey&); | |
165 void doWriteAlgorithmId(WebCryptoAlgorithmId); | |
166 void doWriteAsymmetricKeyType(WebCryptoKeyType); | |
167 void doWriteNamedCurve(WebCryptoNamedCurve); | |
168 void doWriteKeyUsages(const WebCryptoKeyUsageMask usages, bool extractable); | |
169 int bytesNeededToWireEncode(uint32_t value); | 154 int bytesNeededToWireEncode(uint32_t value); |
170 | 155 |
171 template<class T> | 156 template<class T> |
172 void doWriteUintHelper(T value) | 157 void doWriteUintHelper(T value) |
173 { | 158 { |
174 while (true) { | 159 while (true) { |
175 uint8_t b = (value & SerializedScriptValue::varIntMask); | 160 uint8_t b = (value & SerializedScriptValue::varIntMask); |
176 value >>= SerializedScriptValue::varIntShift; | 161 value >>= SerializedScriptValue::varIntShift; |
177 if (!value) { | 162 if (!value) { |
178 append(b); | 163 append(b); |
(...skipping 12 matching lines...) Expand all Loading... |
191 void ensureSpace(unsigned extra); | 176 void ensureSpace(unsigned extra); |
192 void fillHole(); | 177 void fillHole(); |
193 uint8_t* byteAt(int position); | 178 uint8_t* byteAt(int position); |
194 int v8StringWriteOptions(); | 179 int v8StringWriteOptions(); |
195 | 180 |
196 private: | 181 private: |
197 Vector<BufferValueType> m_buffer; | 182 Vector<BufferValueType> m_buffer; |
198 unsigned m_position; | 183 unsigned m_position; |
199 }; | 184 }; |
200 | 185 |
201 class Serializer { | 186 class ScriptValueSerializer { |
202 STACK_ALLOCATED(); | 187 STACK_ALLOCATED(); |
203 WTF_MAKE_NONCOPYABLE(Serializer); | 188 WTF_MAKE_NONCOPYABLE(ScriptValueSerializer); |
204 protected: | 189 protected: |
205 class StateBase; | 190 class StateBase; |
206 public: | 191 public: |
207 enum Status { | 192 enum Status { |
208 Success, | 193 Success, |
209 InputError, | 194 InputError, |
210 DataCloneError, | 195 DataCloneError, |
211 JSException | 196 JSException |
212 }; | 197 }; |
213 | 198 |
214 Serializer(Writer&, MessagePortArray* messagePorts, ArrayBufferArray* arrayB
uffers, WebBlobInfoArray*, BlobDataHandleMap& blobDataHandles, v8::TryCatch&, Sc
riptState*); | 199 ScriptValueSerializer(SerializedScriptValueWriter&, MessagePortArray* messag
ePorts, ArrayBufferArray* arrayBuffers, WebBlobInfoArray*, BlobDataHandleMap& bl
obDataHandles, v8::TryCatch&, ScriptState*); |
215 v8::Isolate* isolate() { return m_scriptState->isolate(); } | 200 v8::Isolate* isolate() { return m_scriptState->isolate(); } |
216 | 201 |
217 Status serialize(v8::Handle<v8::Value>); | 202 Status serialize(v8::Handle<v8::Value>); |
218 String errorMessage() { return m_errorMessage; } | 203 String errorMessage() { return m_errorMessage; } |
219 | 204 |
220 // Functions used by serialization states. | 205 // Functions used by serialization states. |
221 Serializer::StateBase* doSerialize(v8::Handle<v8::Value>, Serializer::StateB
ase* next); | 206 virtual ScriptValueSerializer::StateBase* doSerialize(v8::Handle<v8::Value>,
ScriptValueSerializer::StateBase* next); |
222 | 207 |
223 StateBase* doSerializeArrayBuffer(v8::Handle<v8::Value> arrayBuffer, StateBa
se* next); | 208 StateBase* doSerializeArrayBuffer(v8::Handle<v8::Value> arrayBuffer, StateBa
se* next); |
224 StateBase* checkException(StateBase*); | 209 StateBase* checkException(StateBase*); |
225 StateBase* writeObject(uint32_t numProperties, StateBase*); | 210 StateBase* writeObject(uint32_t numProperties, StateBase*); |
226 StateBase* writeSparseArray(uint32_t numProperties, uint32_t length, StateBa
se*); | 211 StateBase* writeSparseArray(uint32_t numProperties, uint32_t length, StateBa
se*); |
227 StateBase* writeDenseArray(uint32_t numProperties, uint32_t length, StateBas
e*); | 212 StateBase* writeDenseArray(uint32_t numProperties, uint32_t length, StateBas
e*); |
228 | 213 |
229 protected: | 214 protected: |
230 class StateBase { | 215 class StateBase { |
231 WTF_MAKE_NONCOPYABLE(StateBase); | 216 WTF_MAKE_NONCOPYABLE(StateBase); |
232 public: | 217 public: |
233 virtual ~StateBase() { } | 218 virtual ~StateBase() { } |
234 | 219 |
235 // Link to the next state to form a stack. | 220 // Link to the next state to form a stack. |
236 StateBase* nextState() { return m_next; } | 221 StateBase* nextState() { return m_next; } |
237 | 222 |
238 // Composite object we're processing in this state. | 223 // Composite object we're processing in this state. |
239 v8::Handle<v8::Value> composite() { return m_composite; } | 224 v8::Handle<v8::Value> composite() { return m_composite; } |
240 | 225 |
241 // Serializes (a part of) the current composite and returns | 226 // Serializes (a part of) the current composite and returns |
242 // the next state to process or null when this is the final | 227 // the next state to process or null when this is the final |
243 // state. | 228 // state. |
244 virtual StateBase* advance(Serializer&) = 0; | 229 virtual StateBase* advance(ScriptValueSerializer&) = 0; |
245 | 230 |
246 protected: | 231 protected: |
247 StateBase(v8::Handle<v8::Value> composite, StateBase* next) | 232 StateBase(v8::Handle<v8::Value> composite, StateBase* next) |
248 : m_composite(composite) | 233 : m_composite(composite) |
249 , m_next(next) | 234 , m_next(next) |
250 { | 235 { |
251 } | 236 } |
252 | 237 |
253 private: | 238 private: |
254 v8::Handle<v8::Value> m_composite; | 239 v8::Handle<v8::Value> m_composite; |
255 StateBase* m_next; | 240 StateBase* m_next; |
256 }; | 241 }; |
257 | 242 |
258 // Dummy state that is used to signal serialization errors. | 243 // Dummy state that is used to signal serialization errors. |
259 class ErrorState final : public StateBase { | 244 class ErrorState final : public StateBase { |
260 public: | 245 public: |
261 ErrorState() | 246 ErrorState() |
262 : StateBase(v8Undefined(), 0) | 247 : StateBase(v8Undefined(), 0) |
263 { | 248 { |
264 } | 249 } |
265 | 250 |
266 virtual StateBase* advance(Serializer&) override | 251 virtual StateBase* advance(ScriptValueSerializer&) override |
267 { | 252 { |
268 delete this; | 253 delete this; |
269 return 0; | 254 return 0; |
270 } | 255 } |
271 }; | 256 }; |
272 | 257 |
273 template <typename T> | 258 template <typename T> |
274 class State : public StateBase { | 259 class State : public StateBase { |
275 public: | 260 public: |
276 v8::Handle<T> composite() { return v8::Handle<T>::Cast(StateBase::compos
ite()); } | 261 v8::Handle<T> composite() { return v8::Handle<T>::Cast(StateBase::compos
ite()); } |
277 | 262 |
278 protected: | 263 protected: |
279 State(v8::Handle<T> composite, StateBase* next) | 264 State(v8::Handle<T> composite, StateBase* next) |
280 : StateBase(composite, next) | 265 : StateBase(composite, next) |
281 { | 266 { |
282 } | 267 } |
283 }; | 268 }; |
284 | 269 |
285 class AbstractObjectState : public State<v8::Object> { | 270 class AbstractObjectState : public State<v8::Object> { |
286 public: | 271 public: |
287 AbstractObjectState(v8::Handle<v8::Object> object, StateBase* next) | 272 AbstractObjectState(v8::Handle<v8::Object> object, StateBase* next) |
288 : State<v8::Object>(object, next) | 273 : State<v8::Object>(object, next) |
289 , m_index(0) | 274 , m_index(0) |
290 , m_numSerializedProperties(0) | 275 , m_numSerializedProperties(0) |
291 , m_nameDone(false) | 276 , m_nameDone(false) |
292 { | 277 { |
293 } | 278 } |
294 | 279 |
295 protected: | 280 protected: |
296 virtual StateBase* objectDone(unsigned numProperties, Serializer&) = 0; | 281 virtual StateBase* objectDone(unsigned numProperties, ScriptValueSeriali
zer&) = 0; |
297 | 282 |
298 StateBase* serializeProperties(bool ignoreIndexed, Serializer&); | 283 StateBase* serializeProperties(bool ignoreIndexed, ScriptValueSerializer
&); |
299 v8::Local<v8::Array> m_propertyNames; | 284 v8::Local<v8::Array> m_propertyNames; |
300 | 285 |
301 private: | 286 private: |
302 v8::Local<v8::Value> m_propertyName; | 287 v8::Local<v8::Value> m_propertyName; |
303 unsigned m_index; | 288 unsigned m_index; |
304 unsigned m_numSerializedProperties; | 289 unsigned m_numSerializedProperties; |
305 bool m_nameDone; | 290 bool m_nameDone; |
306 }; | 291 }; |
307 | 292 |
308 class ObjectState final : public AbstractObjectState { | 293 class ObjectState final : public AbstractObjectState { |
309 public: | 294 public: |
310 ObjectState(v8::Handle<v8::Object> object, StateBase* next) | 295 ObjectState(v8::Handle<v8::Object> object, StateBase* next) |
311 : AbstractObjectState(object, next) | 296 : AbstractObjectState(object, next) |
312 { | 297 { |
313 } | 298 } |
314 | 299 |
315 virtual StateBase* advance(Serializer&) override; | 300 virtual StateBase* advance(ScriptValueSerializer&) override; |
316 | 301 |
317 protected: | 302 protected: |
318 virtual StateBase* objectDone(unsigned numProperties, Serializer&) overr
ide; | 303 virtual StateBase* objectDone(unsigned numProperties, ScriptValueSeriali
zer&) override; |
319 }; | 304 }; |
320 | 305 |
321 class DenseArrayState final : public AbstractObjectState { | 306 class DenseArrayState final : public AbstractObjectState { |
322 public: | 307 public: |
323 DenseArrayState(v8::Handle<v8::Array> array, v8::Handle<v8::Array> prope
rtyNames, StateBase* next, v8::Isolate* isolate) | 308 DenseArrayState(v8::Handle<v8::Array> array, v8::Handle<v8::Array> prope
rtyNames, StateBase* next, v8::Isolate* isolate) |
324 : AbstractObjectState(array, next) | 309 : AbstractObjectState(array, next) |
325 , m_arrayIndex(0) | 310 , m_arrayIndex(0) |
326 , m_arrayLength(array->Length()) | 311 , m_arrayLength(array->Length()) |
327 { | 312 { |
328 m_propertyNames = v8::Local<v8::Array>::New(isolate, propertyNames); | 313 m_propertyNames = v8::Local<v8::Array>::New(isolate, propertyNames); |
329 } | 314 } |
330 | 315 |
331 virtual StateBase* advance(Serializer&) override; | 316 virtual StateBase* advance(ScriptValueSerializer&) override; |
332 | 317 |
333 protected: | 318 protected: |
334 virtual StateBase* objectDone(unsigned numProperties, Serializer&) overr
ide; | 319 virtual StateBase* objectDone(unsigned numProperties, ScriptValueSeriali
zer&) override; |
335 | 320 |
336 private: | 321 private: |
337 uint32_t m_arrayIndex; | 322 uint32_t m_arrayIndex; |
338 uint32_t m_arrayLength; | 323 uint32_t m_arrayLength; |
339 }; | 324 }; |
340 | 325 |
341 class SparseArrayState final : public AbstractObjectState { | 326 class SparseArrayState final : public AbstractObjectState { |
342 public: | 327 public: |
343 SparseArrayState(v8::Handle<v8::Array> array, v8::Handle<v8::Array> prop
ertyNames, StateBase* next, v8::Isolate* isolate) | 328 SparseArrayState(v8::Handle<v8::Array> array, v8::Handle<v8::Array> prop
ertyNames, StateBase* next, v8::Isolate* isolate) |
344 : AbstractObjectState(array, next) | 329 : AbstractObjectState(array, next) |
345 { | 330 { |
346 m_propertyNames = v8::Local<v8::Array>::New(isolate, propertyNames); | 331 m_propertyNames = v8::Local<v8::Array>::New(isolate, propertyNames); |
347 } | 332 } |
348 | 333 |
349 virtual StateBase* advance(Serializer&) override; | 334 virtual StateBase* advance(ScriptValueSerializer&) override; |
350 | 335 |
351 protected: | 336 protected: |
352 virtual StateBase* objectDone(unsigned numProperties, Serializer&) overr
ide; | 337 virtual StateBase* objectDone(unsigned numProperties, ScriptValueSeriali
zer&) override; |
353 }; | 338 }; |
354 | 339 |
355 private: | 340 private: |
356 StateBase* push(StateBase* state) | 341 StateBase* push(StateBase* state) |
357 { | 342 { |
358 ASSERT(state); | 343 ASSERT(state); |
359 ++m_depth; | 344 ++m_depth; |
360 return checkComposite(state) ? state : handleError(InputError, "Value be
ing cloned is either cyclic or too deeply nested.", state); | 345 return checkComposite(state) ? state : handleError(InputError, "Value be
ing cloned is either cyclic or too deeply nested.", state); |
361 } | 346 } |
362 | 347 |
363 StateBase* pop(StateBase* state) | 348 StateBase* pop(StateBase* state) |
364 { | 349 { |
365 ASSERT(state); | 350 ASSERT(state); |
366 --m_depth; | 351 --m_depth; |
367 StateBase* next = state->nextState(); | 352 StateBase* next = state->nextState(); |
368 delete state; | 353 delete state; |
369 return next; | 354 return next; |
370 } | 355 } |
371 | 356 |
372 bool checkComposite(StateBase* top); | 357 bool checkComposite(StateBase* top); |
373 void writeString(v8::Handle<v8::Value>); | 358 void writeString(v8::Handle<v8::Value>); |
374 void writeStringObject(v8::Handle<v8::Value>); | 359 void writeStringObject(v8::Handle<v8::Value>); |
375 void writeNumberObject(v8::Handle<v8::Value>); | 360 void writeNumberObject(v8::Handle<v8::Value>); |
376 void writeBooleanObject(v8::Handle<v8::Value>); | 361 void writeBooleanObject(v8::Handle<v8::Value>); |
377 StateBase* writeBlob(v8::Handle<v8::Value>, StateBase* next); | 362 StateBase* writeBlob(v8::Handle<v8::Value>, StateBase* next); |
378 StateBase* writeDOMFileSystem(v8::Handle<v8::Value>, StateBase* next); | |
379 StateBase* writeFile(v8::Handle<v8::Value>, StateBase* next); | 363 StateBase* writeFile(v8::Handle<v8::Value>, StateBase* next); |
380 StateBase* writeFileList(v8::Handle<v8::Value>, StateBase* next); | 364 StateBase* writeFileList(v8::Handle<v8::Value>, StateBase* next); |
381 bool writeCryptoKey(v8::Handle<v8::Value>); | |
382 void writeImageData(v8::Handle<v8::Value>); | 365 void writeImageData(v8::Handle<v8::Value>); |
383 void writeRegExp(v8::Handle<v8::Value>); | 366 void writeRegExp(v8::Handle<v8::Value>); |
384 StateBase* writeAndGreyArrayBufferView(v8::Handle<v8::Object>, StateBase* ne
xt); | 367 StateBase* writeAndGreyArrayBufferView(v8::Handle<v8::Object>, StateBase* ne
xt); |
385 StateBase* writeArrayBuffer(v8::Handle<v8::Value>, StateBase* next); | 368 StateBase* writeArrayBuffer(v8::Handle<v8::Value>, StateBase* next); |
386 StateBase* writeTransferredArrayBuffer(v8::Handle<v8::Value>, uint32_t index
, StateBase* next); | 369 StateBase* writeTransferredArrayBuffer(v8::Handle<v8::Value>, uint32_t index
, StateBase* next); |
387 static bool shouldSerializeDensely(uint32_t length, uint32_t propertyCount); | 370 static bool shouldSerializeDensely(uint32_t length, uint32_t propertyCount); |
388 | 371 |
389 StateBase* startArrayState(v8::Handle<v8::Array>, StateBase* next); | 372 StateBase* startArrayState(v8::Handle<v8::Array>, StateBase* next); |
390 StateBase* startObjectState(v8::Handle<v8::Object>, StateBase* next); | 373 StateBase* startObjectState(v8::Handle<v8::Object>, StateBase* next); |
391 | 374 |
392 // Marks object as having been visited by the serializer and assigns it a un
ique object reference ID. | 375 // Marks object as having been visited by the serializer and assigns it a un
ique object reference ID. |
393 // An object may only be greyed once. | 376 // An object may only be greyed once. |
394 void greyObject(const v8::Handle<v8::Object>&); | 377 void greyObject(const v8::Handle<v8::Object>&); |
395 bool appendBlobInfo(const String& uuid, const String& type, unsigned long lo
ng size, int* index); | 378 bool appendBlobInfo(const String& uuid, const String& type, unsigned long lo
ng size, int* index); |
396 bool appendFileInfo(const File*, int* index); | 379 bool appendFileInfo(const File*, int* index); |
397 | 380 |
398 protected: | 381 protected: |
399 StateBase* handleError(Status errorStatus, const String& message, StateBase*
); | 382 StateBase* handleError(Status errorStatus, const String& message, StateBase*
); |
400 | 383 |
401 Writer& writer() { return m_writer; } | 384 SerializedScriptValueWriter& writer() { return m_writer; } |
402 uint32_t nextObjectReference() const { return m_nextObjectReference; } | 385 uint32_t nextObjectReference() const { return m_nextObjectReference; } |
403 | 386 |
404 private: | 387 private: |
405 RefPtr<ScriptState> m_scriptState; | 388 RefPtr<ScriptState> m_scriptState; |
406 Writer& m_writer; | 389 SerializedScriptValueWriter& m_writer; |
407 v8::TryCatch& m_tryCatch; | 390 v8::TryCatch& m_tryCatch; |
408 int m_depth; | 391 int m_depth; |
409 Status m_status; | 392 Status m_status; |
410 String m_errorMessage; | 393 String m_errorMessage; |
411 typedef V8ObjectMap<v8::Object, uint32_t> ObjectPool; | 394 typedef V8ObjectMap<v8::Object, uint32_t> ObjectPool; |
412 ObjectPool m_objectPool; | 395 ObjectPool m_objectPool; |
413 ObjectPool m_transferredMessagePorts; | 396 ObjectPool m_transferredMessagePorts; |
414 ObjectPool m_transferredArrayBuffers; | 397 ObjectPool m_transferredArrayBuffers; |
415 uint32_t m_nextObjectReference; | 398 uint32_t m_nextObjectReference; |
416 WebBlobInfoArray* m_blobInfo; | 399 WebBlobInfoArray* m_blobInfo; |
417 BlobDataHandleMap& m_blobDataHandles; | 400 BlobDataHandleMap& m_blobDataHandles; |
418 }; | 401 }; |
419 | 402 |
420 // Interface used by Reader to create objects of composite types. | 403 // Interface used by SerializedScriptValueReader to create objects of composite
types. |
421 class CompositeCreator { | 404 class ScriptValueCompositeCreator { |
422 STACK_ALLOCATED(); | 405 STACK_ALLOCATED(); |
423 WTF_MAKE_NONCOPYABLE(CompositeCreator); | 406 WTF_MAKE_NONCOPYABLE(ScriptValueCompositeCreator); |
424 public: | 407 public: |
425 CompositeCreator() { } | 408 ScriptValueCompositeCreator() { } |
426 virtual ~CompositeCreator() { } | 409 virtual ~ScriptValueCompositeCreator() { } |
427 | 410 |
428 virtual bool consumeTopOfStack(v8::Handle<v8::Value>*) = 0; | 411 virtual bool consumeTopOfStack(v8::Handle<v8::Value>*) = 0; |
429 virtual uint32_t objectReferenceCount() = 0; | 412 virtual uint32_t objectReferenceCount() = 0; |
430 virtual void pushObjectReference(const v8::Handle<v8::Value>&) = 0; | 413 virtual void pushObjectReference(const v8::Handle<v8::Value>&) = 0; |
431 virtual bool tryGetObjectFromObjectReference(uint32_t reference, v8::Handle<
v8::Value>*) = 0; | 414 virtual bool tryGetObjectFromObjectReference(uint32_t reference, v8::Handle<
v8::Value>*) = 0; |
432 virtual bool tryGetTransferredMessagePort(uint32_t index, v8::Handle<v8::Val
ue>*) = 0; | 415 virtual bool tryGetTransferredMessagePort(uint32_t index, v8::Handle<v8::Val
ue>*) = 0; |
433 virtual bool tryGetTransferredArrayBuffer(uint32_t index, v8::Handle<v8::Val
ue>*) = 0; | 416 virtual bool tryGetTransferredArrayBuffer(uint32_t index, v8::Handle<v8::Val
ue>*) = 0; |
434 virtual bool newSparseArray(uint32_t length) = 0; | 417 virtual bool newSparseArray(uint32_t length) = 0; |
435 virtual bool newDenseArray(uint32_t length) = 0; | 418 virtual bool newDenseArray(uint32_t length) = 0; |
436 virtual bool newObject() = 0; | 419 virtual bool newObject() = 0; |
437 virtual bool completeObject(uint32_t numProperties, v8::Handle<v8::Value>*)
= 0; | 420 virtual bool completeObject(uint32_t numProperties, v8::Handle<v8::Value>*)
= 0; |
438 virtual bool completeSparseArray(uint32_t numProperties, uint32_t length, v8
::Handle<v8::Value>*) = 0; | 421 virtual bool completeSparseArray(uint32_t numProperties, uint32_t length, v8
::Handle<v8::Value>*) = 0; |
439 virtual bool completeDenseArray(uint32_t numProperties, uint32_t length, v8:
:Handle<v8::Value>*) = 0; | 422 virtual bool completeDenseArray(uint32_t numProperties, uint32_t length, v8:
:Handle<v8::Value>*) = 0; |
440 }; | 423 }; |
441 | 424 |
442 // Reader is responsible for deserializing primitive types and | 425 // SerializedScriptValueReader is responsible for deserializing primitive types
and |
443 // restoring information about saved objects of composite types. | 426 // restoring information about saved objects of composite types. |
444 class Reader { | 427 class SerializedScriptValueReader { |
445 STACK_ALLOCATED(); | 428 STACK_ALLOCATED(); |
446 WTF_MAKE_NONCOPYABLE(Reader); | 429 WTF_MAKE_NONCOPYABLE(SerializedScriptValueReader); |
447 public: | 430 public: |
448 Reader(const uint8_t* buffer, int length, const WebBlobInfoArray* blobInfo,
BlobDataHandleMap& blobDataHandles, ScriptState* scriptState) | 431 SerializedScriptValueReader(const uint8_t* buffer, int length, const WebBlob
InfoArray* blobInfo, BlobDataHandleMap& blobDataHandles, ScriptState* scriptStat
e) |
449 : m_scriptState(scriptState) | 432 : m_scriptState(scriptState) |
450 , m_buffer(buffer) | 433 , m_buffer(buffer) |
451 , m_length(length) | 434 , m_length(length) |
452 , m_position(0) | 435 , m_position(0) |
453 , m_version(0) | 436 , m_version(0) |
454 , m_blobInfo(blobInfo) | 437 , m_blobInfo(blobInfo) |
455 , m_blobDataHandles(blobDataHandles) | 438 , m_blobDataHandles(blobDataHandles) |
456 { | 439 { |
457 ASSERT(!(reinterpret_cast<size_t>(buffer) & 1)); | 440 ASSERT(!(reinterpret_cast<size_t>(buffer) & 1)); |
458 ASSERT(length >= 0); | 441 ASSERT(length >= 0); |
459 } | 442 } |
460 | 443 |
461 bool isEof() const { return m_position >= m_length; } | 444 bool isEof() const { return m_position >= m_length; } |
462 | 445 |
463 ScriptState* scriptState() const { return m_scriptState.get(); } | 446 ScriptState* scriptState() const { return m_scriptState.get(); } |
464 | 447 |
465 protected: | 448 protected: |
466 v8::Isolate* isolate() const { return m_scriptState->isolate(); } | 449 v8::Isolate* isolate() const { return m_scriptState->isolate(); } |
467 unsigned length() const { return m_length; } | 450 unsigned length() const { return m_length; } |
468 unsigned position() const { return m_position; } | 451 unsigned position() const { return m_position; } |
469 | 452 |
470 const uint8_t* allocate(uint32_t size) | 453 const uint8_t* allocate(uint32_t size) |
471 { | 454 { |
472 const uint8_t* allocated = m_buffer + m_position; | 455 const uint8_t* allocated = m_buffer + m_position; |
473 m_position += size; | 456 m_position += size; |
474 return allocated; | 457 return allocated; |
475 } | 458 } |
476 | 459 |
477 public: | 460 public: |
478 bool read(v8::Handle<v8::Value>*, CompositeCreator&); | 461 virtual bool read(v8::Handle<v8::Value>*, ScriptValueCompositeCreator&); |
479 bool readVersion(uint32_t& version); | 462 bool readVersion(uint32_t& version); |
480 void setVersion(uint32_t); | 463 void setVersion(uint32_t); |
481 | 464 |
482 private: | 465 protected: |
| 466 bool readWithTag(SerializationTag, v8::Handle<v8::Value>*, ScriptValueCompos
iteCreator&); |
| 467 |
483 bool readTag(SerializationTag*); | 468 bool readTag(SerializationTag*); |
484 bool readWebCoreString(String*); | 469 bool readWebCoreString(String*); |
485 bool readUint32(v8::Handle<v8::Value>*); | 470 bool readUint32(v8::Handle<v8::Value>*); |
| 471 |
| 472 bool doReadUint32(uint32_t* value); |
| 473 |
| 474 private: |
486 void undoReadTag(); | 475 void undoReadTag(); |
487 bool readArrayBufferViewSubTag(ArrayBufferViewSubTag*); | 476 bool readArrayBufferViewSubTag(ArrayBufferViewSubTag*); |
488 bool readString(v8::Handle<v8::Value>*); | 477 bool readString(v8::Handle<v8::Value>*); |
489 bool readUCharString(v8::Handle<v8::Value>*); | 478 bool readUCharString(v8::Handle<v8::Value>*); |
490 bool readStringObject(v8::Handle<v8::Value>*); | 479 bool readStringObject(v8::Handle<v8::Value>*); |
491 bool readInt32(v8::Handle<v8::Value>*); | 480 bool readInt32(v8::Handle<v8::Value>*); |
492 bool readDate(v8::Handle<v8::Value>*); | 481 bool readDate(v8::Handle<v8::Value>*); |
493 bool readNumber(v8::Handle<v8::Value>*); | 482 bool readNumber(v8::Handle<v8::Value>*); |
494 bool readNumberObject(v8::Handle<v8::Value>*); | 483 bool readNumberObject(v8::Handle<v8::Value>*); |
495 bool readImageData(v8::Handle<v8::Value>*); | 484 bool readImageData(v8::Handle<v8::Value>*); |
496 PassRefPtr<ArrayBuffer> doReadArrayBuffer(); | 485 PassRefPtr<ArrayBuffer> doReadArrayBuffer(); |
497 bool readArrayBuffer(v8::Handle<v8::Value>*); | 486 bool readArrayBuffer(v8::Handle<v8::Value>*); |
498 bool readArrayBufferView(v8::Handle<v8::Value>*, CompositeCreator&); | 487 bool readArrayBufferView(v8::Handle<v8::Value>*, ScriptValueCompositeCreator
&); |
499 bool readRegExp(v8::Handle<v8::Value>*); | 488 bool readRegExp(v8::Handle<v8::Value>*); |
500 bool readBlob(v8::Handle<v8::Value>*, bool isIndexed); | 489 bool readBlob(v8::Handle<v8::Value>*, bool isIndexed); |
501 bool readDOMFileSystem(v8::Handle<v8::Value>*); | |
502 bool readFile(v8::Handle<v8::Value>*, bool isIndexed); | 490 bool readFile(v8::Handle<v8::Value>*, bool isIndexed); |
503 bool readFileList(v8::Handle<v8::Value>*, bool isIndexed); | 491 bool readFileList(v8::Handle<v8::Value>*, bool isIndexed); |
504 bool readCryptoKey(v8::Handle<v8::Value>*); | |
505 File* readFileHelper(); | 492 File* readFileHelper(); |
506 File* readFileIndexHelper(); | 493 File* readFileIndexHelper(); |
507 | 494 |
508 template<class T> | 495 template<class T> |
509 bool doReadUintHelper(T* value) | 496 bool doReadUintHelper(T* value) |
510 { | 497 { |
511 *value = 0; | 498 *value = 0; |
512 uint8_t currentByte; | 499 uint8_t currentByte; |
513 int shift = 0; | 500 int shift = 0; |
514 do { | 501 do { |
515 if (m_position >= m_length) | 502 if (m_position >= m_length) |
516 return false; | 503 return false; |
517 currentByte = m_buffer[m_position++]; | 504 currentByte = m_buffer[m_position++]; |
518 *value |= ((currentByte & SerializedScriptValue::varIntMask) << shif
t); | 505 *value |= ((currentByte & SerializedScriptValue::varIntMask) << shif
t); |
519 shift += SerializedScriptValue::varIntShift; | 506 shift += SerializedScriptValue::varIntShift; |
520 } while (currentByte & (1 << SerializedScriptValue::varIntShift)); | 507 } while (currentByte & (1 << SerializedScriptValue::varIntShift)); |
521 return true; | 508 return true; |
522 } | 509 } |
523 | 510 |
524 bool doReadUint32(uint32_t* value); | |
525 bool doReadUint64(uint64_t* value); | 511 bool doReadUint64(uint64_t* value); |
526 bool doReadNumber(double* number); | 512 bool doReadNumber(double* number); |
527 PassRefPtr<BlobDataHandle> getOrCreateBlobDataHandle(const String& uuid, con
st String& type, long long size = -1); | 513 PassRefPtr<BlobDataHandle> getOrCreateBlobDataHandle(const String& uuid, con
st String& type, long long size = -1); |
528 bool doReadHmacKey(WebCryptoKeyAlgorithm&, WebCryptoKeyType&); | |
529 bool doReadAesKey(WebCryptoKeyAlgorithm&, WebCryptoKeyType&); | |
530 bool doReadRsaHashedKey(WebCryptoKeyAlgorithm&, WebCryptoKeyType&); | |
531 bool doReadEcKey(WebCryptoKeyAlgorithm&, WebCryptoKeyType&); | |
532 bool doReadAlgorithmId(WebCryptoAlgorithmId&); | |
533 bool doReadAsymmetricKeyType(WebCryptoKeyType&); | |
534 bool doReadNamedCurve(WebCryptoNamedCurve&); | |
535 bool doReadKeyUsages(WebCryptoKeyUsageMask& usages, bool& extractable); | |
536 | 514 |
537 private: | 515 private: |
538 RefPtr<ScriptState> m_scriptState; | 516 RefPtr<ScriptState> m_scriptState; |
539 const uint8_t* m_buffer; | 517 const uint8_t* m_buffer; |
540 const unsigned m_length; | 518 const unsigned m_length; |
541 unsigned m_position; | 519 unsigned m_position; |
542 uint32_t m_version; | 520 uint32_t m_version; |
543 const WebBlobInfoArray* m_blobInfo; | 521 const WebBlobInfoArray* m_blobInfo; |
544 const BlobDataHandleMap& m_blobDataHandles; | 522 const BlobDataHandleMap& m_blobDataHandles; |
545 }; | 523 }; |
546 | 524 |
547 class Deserializer : public CompositeCreator { | 525 class ScriptValueDeserializer : public ScriptValueCompositeCreator { |
548 STACK_ALLOCATED(); | 526 STACK_ALLOCATED(); |
549 WTF_MAKE_NONCOPYABLE(Deserializer); | 527 WTF_MAKE_NONCOPYABLE(ScriptValueDeserializer); |
550 public: | 528 public: |
551 Deserializer(Reader& reader, MessagePortArray* messagePorts, ArrayBufferCont
entsArray* arrayBufferContents) | 529 ScriptValueDeserializer(SerializedScriptValueReader& reader, MessagePortArra
y* messagePorts, ArrayBufferContentsArray* arrayBufferContents) |
552 : m_reader(reader) | 530 : m_reader(reader) |
553 , m_transferredMessagePorts(messagePorts) | 531 , m_transferredMessagePorts(messagePorts) |
554 , m_arrayBufferContents(arrayBufferContents) | 532 , m_arrayBufferContents(arrayBufferContents) |
555 , m_arrayBuffers(arrayBufferContents ? arrayBufferContents->size() : 0) | 533 , m_arrayBuffers(arrayBufferContents ? arrayBufferContents->size() : 0) |
556 , m_version(0) | 534 , m_version(0) |
557 { | 535 { |
558 } | 536 } |
559 | 537 |
560 v8::Handle<v8::Value> deserialize(); | 538 v8::Handle<v8::Value> deserialize(); |
561 virtual bool newSparseArray(uint32_t) override; | 539 virtual bool newSparseArray(uint32_t) override; |
562 virtual bool newDenseArray(uint32_t length) override; | 540 virtual bool newDenseArray(uint32_t length) override; |
563 virtual bool consumeTopOfStack(v8::Handle<v8::Value>*) override; | 541 virtual bool consumeTopOfStack(v8::Handle<v8::Value>*) override; |
564 virtual bool newObject() override; | 542 virtual bool newObject() override; |
565 virtual bool completeObject(uint32_t numProperties, v8::Handle<v8::Value>*)
override; | 543 virtual bool completeObject(uint32_t numProperties, v8::Handle<v8::Value>*)
override; |
566 virtual bool completeSparseArray(uint32_t numProperties, uint32_t length, v8
::Handle<v8::Value>*) override; | 544 virtual bool completeSparseArray(uint32_t numProperties, uint32_t length, v8
::Handle<v8::Value>*) override; |
567 virtual bool completeDenseArray(uint32_t numProperties, uint32_t length, v8:
:Handle<v8::Value>*) override; | 545 virtual bool completeDenseArray(uint32_t numProperties, uint32_t length, v8:
:Handle<v8::Value>*) override; |
568 virtual void pushObjectReference(const v8::Handle<v8::Value>&) override; | 546 virtual void pushObjectReference(const v8::Handle<v8::Value>&) override; |
569 virtual bool tryGetTransferredMessagePort(uint32_t index, v8::Handle<v8::Val
ue>*) override; | 547 virtual bool tryGetTransferredMessagePort(uint32_t index, v8::Handle<v8::Val
ue>*) override; |
570 virtual bool tryGetTransferredArrayBuffer(uint32_t index, v8::Handle<v8::Val
ue>*) override; | 548 virtual bool tryGetTransferredArrayBuffer(uint32_t index, v8::Handle<v8::Val
ue>*) override; |
571 virtual bool tryGetObjectFromObjectReference(uint32_t reference, v8::Handle<
v8::Value>*) override; | 549 virtual bool tryGetObjectFromObjectReference(uint32_t reference, v8::Handle<
v8::Value>*) override; |
572 virtual uint32_t objectReferenceCount() override; | 550 virtual uint32_t objectReferenceCount() override; |
573 | 551 |
574 protected: | 552 protected: |
575 Reader& reader() { return m_reader; } | 553 SerializedScriptValueReader& reader() { return m_reader; } |
576 bool read(v8::Local<v8::Value>*); | 554 virtual bool read(v8::Local<v8::Value>*); |
577 | 555 |
578 private: | 556 private: |
579 bool initializeObject(v8::Handle<v8::Object>, uint32_t numProperties, v8::Ha
ndle<v8::Value>*); | 557 bool initializeObject(v8::Handle<v8::Object>, uint32_t numProperties, v8::Ha
ndle<v8::Value>*); |
580 bool doDeserialize(); | 558 bool doDeserialize(); |
581 void push(v8::Local<v8::Value> value) { m_stack.append(value); }; | 559 void push(v8::Local<v8::Value> value) { m_stack.append(value); }; |
582 void pop(unsigned length) | 560 void pop(unsigned length) |
583 { | 561 { |
584 ASSERT(length <= m_stack.size()); | 562 ASSERT(length <= m_stack.size()); |
585 m_stack.shrink(m_stack.size() - length); | 563 m_stack.shrink(m_stack.size() - length); |
586 } | 564 } |
587 unsigned stackDepth() const { return m_stack.size(); } | 565 unsigned stackDepth() const { return m_stack.size(); } |
588 | 566 |
589 v8::Local<v8::Value> element(unsigned index); | 567 v8::Local<v8::Value> element(unsigned index); |
590 void openComposite(const v8::Local<v8::Value>&); | 568 void openComposite(const v8::Local<v8::Value>&); |
591 bool closeComposite(v8::Handle<v8::Value>*); | 569 bool closeComposite(v8::Handle<v8::Value>*); |
592 | 570 |
593 Reader& m_reader; | 571 SerializedScriptValueReader& m_reader; |
594 Vector<v8::Local<v8::Value> > m_stack; | 572 Vector<v8::Local<v8::Value> > m_stack; |
595 Vector<v8::Handle<v8::Value> > m_objectPool; | 573 Vector<v8::Handle<v8::Value> > m_objectPool; |
596 Vector<uint32_t> m_openCompositeReferenceStack; | 574 Vector<uint32_t> m_openCompositeReferenceStack; |
597 RawPtrWillBeMember<MessagePortArray> m_transferredMessagePorts; | 575 RawPtrWillBeMember<MessagePortArray> m_transferredMessagePorts; |
598 ArrayBufferContentsArray* m_arrayBufferContents; | 576 ArrayBufferContentsArray* m_arrayBufferContents; |
599 Vector<v8::Handle<v8::Object> > m_arrayBuffers; | 577 Vector<v8::Handle<v8::Object> > m_arrayBuffers; |
600 uint32_t m_version; | 578 uint32_t m_version; |
601 }; | 579 }; |
602 | 580 |
603 } // namespace SerializedScriptValueInternal | |
604 | |
605 } // namespace blink | 581 } // namespace blink |
606 | 582 |
607 #endif // ScriptValueSerializer_h | 583 #endif // ScriptValueSerializer_h |
OLD | NEW |