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