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 #include "bindings/core/v8/SerializedScriptValueFactory.h" | 5 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/core/v8/ScriptValueSerializer.h" | 8 #include "bindings/core/v8/ScriptValueSerializer.h" |
9 #include "bindings/core/v8/Transferables.h" | 9 #include "bindings/core/v8/Transferables.h" |
10 #include "bindings/core/v8/serialization/V8ScriptValueDeserializer.h" | 10 #include "bindings/core/v8/serialization/V8ScriptValueDeserializer.h" |
11 #include "bindings/core/v8/serialization/V8ScriptValueSerializer.h" | 11 #include "bindings/core/v8/serialization/V8ScriptValueSerializer.h" |
12 #include "core/dom/DOMArrayBuffer.h" | 12 #include "core/dom/DOMArrayBuffer.h" |
13 #include "core/dom/MessagePort.h" | 13 #include "core/dom/MessagePort.h" |
14 #include "core/frame/ImageBitmap.h" | 14 #include "core/frame/ImageBitmap.h" |
15 #include "wtf/ByteOrder.h" | 15 #include "wtf/ByteOrder.h" |
16 #include "wtf/text/StringBuffer.h" | 16 #include "wtf/text/StringBuffer.h" |
17 | 17 |
18 namespace blink { | 18 namespace blink { |
19 | 19 |
20 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; | 20 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; |
21 | 21 |
22 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, Transferables* transferables, WebBlobIn
foArray* blobInfo, ExceptionState& exceptionState) | 22 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, Transferables* transferables, WebBlobIn
foArray* blobInfo, ExceptionState& exceptionState) |
23 { | 23 { |
24 if (RuntimeEnabledFeatures::v8BasedStructuredCloneEnabled()) { | 24 if (RuntimeEnabledFeatures::v8BasedStructuredCloneEnabled()) { |
25 V8ScriptValueSerializer serializer(ScriptState::current(isolate)); | 25 V8ScriptValueSerializer serializer(ScriptState::current(isolate)); |
| 26 serializer.setBlobInfoArray(blobInfo); |
26 return serializer.serialize(value, transferables, exceptionState); | 27 return serializer.serialize(value, transferables, exceptionState); |
27 } | 28 } |
28 SerializedScriptValueWriter writer; | 29 SerializedScriptValueWriter writer; |
29 ScriptValueSerializer serializer(writer, blobInfo, ScriptState::current(isol
ate)); | 30 ScriptValueSerializer serializer(writer, blobInfo, ScriptState::current(isol
ate)); |
30 return serializer.serialize(value, transferables, exceptionState); | 31 return serializer.serialize(value, transferables, exceptionState); |
31 } | 32 } |
32 | 33 |
33 v8::Local<v8::Value> SerializedScriptValueFactory::deserialize(SerializedScriptV
alue* value, v8::Isolate* isolate, MessagePortArray* messagePorts, const WebBlob
InfoArray* blobInfo) | 34 v8::Local<v8::Value> SerializedScriptValueFactory::deserialize(SerializedScriptV
alue* value, v8::Isolate* isolate, MessagePortArray* messagePorts, const WebBlob
InfoArray* blobInfo) |
34 { | 35 { |
35 if (RuntimeEnabledFeatures::v8BasedStructuredCloneEnabled()) { | 36 if (RuntimeEnabledFeatures::v8BasedStructuredCloneEnabled()) { |
36 V8ScriptValueDeserializer deserializer(ScriptState::current(isolate), va
lue); | 37 V8ScriptValueDeserializer deserializer(ScriptState::current(isolate), va
lue); |
37 deserializer.setTransferredMessagePorts(messagePorts); | 38 deserializer.setTransferredMessagePorts(messagePorts); |
| 39 deserializer.setBlobInfoArray(blobInfo); |
38 return deserializer.deserialize(); | 40 return deserializer.deserialize(); |
39 } | 41 } |
40 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. | 42 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. |
41 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. | 43 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. |
42 RefPtr<SerializedScriptValue> protect(value); | 44 RefPtr<SerializedScriptValue> protect(value); |
43 String& data = value->data(); | 45 String& data = value->data(); |
44 if (!data.impl()) | 46 if (!data.impl()) |
45 return v8::Null(isolate); | 47 return v8::Null(isolate); |
46 static_assert(sizeof(SerializedScriptValueWriter::BufferValueType) == 2, "Bu
fferValueType should be 2 bytes"); | 48 static_assert(sizeof(SerializedScriptValueWriter::BufferValueType) == 2, "Bu
fferValueType should be 2 bytes"); |
47 data.ensure16Bit(); | 49 data.ensure16Bit(); |
48 // FIXME: SerializedScriptValue shouldn't use String for its underlying | 50 // FIXME: SerializedScriptValue shouldn't use String for its underlying |
49 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The | 51 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The |
50 // information stored in m_data isn't even encoded in UTF-16. Instead, | 52 // information stored in m_data isn't even encoded in UTF-16. Instead, |
51 // unicode characters are encoded as UTF-8 with two code units per UChar. | 53 // unicode characters are encoded as UTF-8 with two code units per UChar. |
52 SerializedScriptValueReader reader(reinterpret_cast<const uint8_t*>(data.imp
l()->characters16()), 2 * data.length(), blobInfo, value->blobDataHandles(), Scr
iptState::current(isolate)); | 54 SerializedScriptValueReader reader(reinterpret_cast<const uint8_t*>(data.imp
l()->characters16()), 2 * data.length(), blobInfo, value->blobDataHandles(), Scr
iptState::current(isolate)); |
53 ScriptValueDeserializer deserializer(reader, messagePorts, value->getArrayBu
fferContentsArray(), value->getImageBitmapContentsArray()); | 55 ScriptValueDeserializer deserializer(reader, messagePorts, value->getArrayBu
fferContentsArray(), value->getImageBitmapContentsArray()); |
54 | 56 |
55 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. | 57 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. |
56 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. | 58 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. |
57 return deserializer.deserialize(); | 59 return deserializer.deserialize(); |
58 } | 60 } |
59 | 61 |
60 | 62 |
61 } // namespace blink | 63 } // namespace blink |
OLD | NEW |