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/TransferableArrayBuffer.h" | 9 #include "bindings/core/v8/Transferables.h" |
10 #include "bindings/core/v8/TransferableImageBitmap.h" | 10 #include "core/dom/DOMArrayBuffer.h" |
11 #include "bindings/core/v8/TransferableMessagePort.h" | 11 #include "core/dom/MessagePort.h" |
| 12 #include "core/frame/ImageBitmap.h" |
12 #include "wtf/ByteOrder.h" | 13 #include "wtf/ByteOrder.h" |
13 #include "wtf/text/StringBuffer.h" | 14 #include "wtf/text/StringBuffer.h" |
14 | 15 |
15 namespace blink { | 16 namespace blink { |
16 | 17 |
17 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; | 18 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; |
18 | 19 |
19 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, TransferableArray* transferables, WebBl
obInfoArray* blobInfo, ExceptionState& exceptionState) | 20 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, Transferables* transferables, WebBlobIn
foArray* blobInfo, ExceptionState& exceptionState) |
20 { | 21 { |
21 RefPtr<SerializedScriptValue> serializedValue = create(); | 22 RefPtr<SerializedScriptValue> serializedValue = create(); |
22 SerializedScriptValueWriter writer; | 23 SerializedScriptValueWriter writer; |
23 ScriptValueSerializer::Status status; | 24 ScriptValueSerializer::Status status; |
24 String errorMessage; | 25 String errorMessage; |
25 { | 26 { |
26 v8::TryCatch tryCatch(isolate); | 27 v8::TryCatch tryCatch(isolate); |
27 status = doSerialize(value, writer, transferables, blobInfo, serializedV
alue.get(), tryCatch, errorMessage, isolate); | 28 status = doSerialize(value, writer, transferables, blobInfo, serializedV
alue.get(), tryCatch, errorMessage, isolate); |
28 if (status == ScriptValueSerializer::JSException) { | 29 if (status == ScriptValueSerializer::JSException) { |
29 // If there was a JS exception thrown, re-throw it. | 30 // If there was a JS exception thrown, re-throw it. |
(...skipping 10 matching lines...) Expand all Loading... |
40 transferData(serializedValue.get(), writer, transferables, exceptionStat
e, isolate); | 41 transferData(serializedValue.get(), writer, transferables, exceptionStat
e, isolate); |
41 return serializedValue.release(); | 42 return serializedValue.release(); |
42 case ScriptValueSerializer::JSException: | 43 case ScriptValueSerializer::JSException: |
43 ASSERT_NOT_REACHED(); | 44 ASSERT_NOT_REACHED(); |
44 break; | 45 break; |
45 } | 46 } |
46 ASSERT_NOT_REACHED(); | 47 ASSERT_NOT_REACHED(); |
47 return serializedValue.release(); | 48 return serializedValue.release(); |
48 } | 49 } |
49 | 50 |
50 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, TransferableArray* transferables, Excep
tionState& exceptionState) | 51 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, Transferables* transferables, Exception
State& exceptionState) |
51 { | 52 { |
52 return create(isolate, value, transferables, 0, exceptionState); | 53 return create(isolate, value, transferables, 0, exceptionState); |
53 } | 54 } |
54 | 55 |
55 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createAndSwallow
Exceptions(v8::Isolate* isolate, v8::Local<v8::Value> value) | 56 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createAndSwallow
Exceptions(v8::Isolate* isolate, v8::Local<v8::Value> value) |
56 { | 57 { |
57 TrackExceptionState exceptionState; | 58 TrackExceptionState exceptionState; |
58 return create(isolate, value, nullptr, exceptionState); | 59 return create(isolate, value, nullptr, exceptionState); |
59 } | 60 } |
60 | 61 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 writer.writeWebCoreString(data); | 96 writer.writeWebCoreString(data); |
96 String wireData = writer.takeWireString(); | 97 String wireData = writer.takeWireString(); |
97 return createFromWire(wireData); | 98 return createFromWire(wireData); |
98 } | 99 } |
99 | 100 |
100 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create() | 101 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create() |
101 { | 102 { |
102 return adoptRef(new SerializedScriptValue()); | 103 return adoptRef(new SerializedScriptValue()); |
103 } | 104 } |
104 | 105 |
105 void SerializedScriptValueFactory::transferData(SerializedScriptValue* serialize
dValue, SerializedScriptValueWriter& writer, TransferableArray* transferables, E
xceptionState& exceptionState, v8::Isolate* isolate) | 106 void SerializedScriptValueFactory::transferData(SerializedScriptValue* serialize
dValue, SerializedScriptValueWriter& writer, Transferables* transferables, Excep
tionState& exceptionState, v8::Isolate* isolate) |
106 { | 107 { |
107 serializedValue->setData(writer.takeWireString()); | 108 serializedValue->setData(writer.takeWireString()); |
108 ASSERT(serializedValue->data().impl()->hasOneRef()); | 109 ASSERT(serializedValue->data().impl()->hasOneRef()); |
109 if (!transferables) | 110 if (!transferables) |
110 return; | 111 return; |
111 if (auto* imageBitmaps = TransferableImageBitmap::get(*transferables)) { | 112 |
112 if (imageBitmaps->getArray().size()) | 113 serializedValue->transferImageBitmaps(isolate, transferables->imageBitmaps,
exceptionState); |
113 serializedValue->transferImageBitmaps(isolate, imageBitmaps, excepti
onState); | 114 if (exceptionState.hadException()) |
114 } | 115 return; |
115 if (auto* arrayBuffers = TransferableArrayBuffer::get(*transferables)) { | 116 serializedValue->transferArrayBuffers(isolate, transferables->arrayBuffers,
exceptionState); |
116 if (arrayBuffers->getArray().size()) | |
117 serializedValue->transferArrayBuffers(isolate, arrayBuffers, excepti
onState); | |
118 } | |
119 } | 117 } |
120 | 118 |
121 ScriptValueSerializer::Status SerializedScriptValueFactory::doSerialize(v8::Loca
l<v8::Value> value, SerializedScriptValueWriter& writer, TransferableArray* tran
sferables, WebBlobInfoArray* blobInfo, SerializedScriptValue* serializedValue, v
8::TryCatch& tryCatch, String& errorMessage, v8::Isolate* isolate) | 119 ScriptValueSerializer::Status SerializedScriptValueFactory::doSerialize(v8::Loca
l<v8::Value> value, SerializedScriptValueWriter& writer, Transferables* transfer
ables, WebBlobInfoArray* blobInfo, SerializedScriptValue* serializedValue, v8::T
ryCatch& tryCatch, String& errorMessage, v8::Isolate* isolate) |
122 { | 120 { |
123 return doSerialize(value, writer, transferables, blobInfo, serializedValue->
blobDataHandles(), tryCatch, errorMessage, isolate); | 121 return doSerialize(value, writer, transferables, blobInfo, serializedValue->
blobDataHandles(), tryCatch, errorMessage, isolate); |
124 } | 122 } |
125 | 123 |
126 ScriptValueSerializer::Status SerializedScriptValueFactory::doSerialize(v8::Loca
l<v8::Value> value, SerializedScriptValueWriter& writer, TransferableArray* tran
sferables, WebBlobInfoArray* blobInfo, BlobDataHandleMap& blobDataHandles, v8::T
ryCatch& tryCatch, String& errorMessage, v8::Isolate* isolate) | 124 ScriptValueSerializer::Status SerializedScriptValueFactory::doSerialize(v8::Loca
l<v8::Value> value, SerializedScriptValueWriter& writer, Transferables* transfer
ables, WebBlobInfoArray* blobInfo, BlobDataHandleMap& blobDataHandles, v8::TryCa
tch& tryCatch, String& errorMessage, v8::Isolate* isolate) |
127 { | 125 { |
128 ScriptValueSerializer serializer(writer, transferables, blobInfo, blobDataHa
ndles, tryCatch, ScriptState::current(isolate)); | 126 ScriptValueSerializer serializer(writer, transferables, blobInfo, blobDataHa
ndles, tryCatch, ScriptState::current(isolate)); |
129 ScriptValueSerializer::Status status = serializer.serialize(value); | 127 ScriptValueSerializer::Status status = serializer.serialize(value); |
130 errorMessage = serializer.errorMessage(); | 128 errorMessage = serializer.errorMessage(); |
131 return status; | 129 return status; |
132 } | 130 } |
133 | 131 |
134 v8::Local<v8::Value> SerializedScriptValueFactory::deserialize(SerializedScriptV
alue* value, v8::Isolate* isolate, MessagePortArray* messagePorts, const WebBlob
InfoArray* blobInfo) | 132 v8::Local<v8::Value> SerializedScriptValueFactory::deserialize(SerializedScriptV
alue* value, v8::Isolate* isolate, MessagePortArray* messagePorts, const WebBlob
InfoArray* blobInfo) |
135 { | 133 { |
136 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. | 134 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. |
(...skipping 15 matching lines...) Expand all Loading... |
152 SerializedScriptValueReader reader(reinterpret_cast<const uint8_t*>(data.imp
l()->characters16()), 2 * data.length(), blobInfo, blobDataHandles, ScriptState:
:current(isolate)); | 150 SerializedScriptValueReader reader(reinterpret_cast<const uint8_t*>(data.imp
l()->characters16()), 2 * data.length(), blobInfo, blobDataHandles, ScriptState:
:current(isolate)); |
153 ScriptValueDeserializer deserializer(reader, messagePorts, arrayBufferConten
tsArray, imageBitmapContentsArray); | 151 ScriptValueDeserializer deserializer(reader, messagePorts, arrayBufferConten
tsArray, imageBitmapContentsArray); |
154 | 152 |
155 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. | 153 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. |
156 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. | 154 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. |
157 return deserializer.deserialize(); | 155 return deserializer.deserialize(); |
158 } | 156 } |
159 | 157 |
160 | 158 |
161 } // namespace blink | 159 } // namespace blink |
OLD | NEW |