| 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/V8ArrayBuffer.h" | |
| 11 #include "bindings/core/v8/V8ImageBitmap.h" | |
| 12 #include "bindings/core/v8/V8MessagePort.h" | |
| 13 #include "bindings/core/v8/V8SharedArrayBuffer.h" | |
| 14 #include "core/dom/DOMArrayBuffer.h" | 10 #include "core/dom/DOMArrayBuffer.h" |
| 15 #include "core/dom/MessagePort.h" | 11 #include "core/dom/MessagePort.h" |
| 16 #include "core/frame/ImageBitmap.h" | 12 #include "core/frame/ImageBitmap.h" |
| 17 #include "wtf/ByteOrder.h" | 13 #include "wtf/ByteOrder.h" |
| 18 #include "wtf/text/StringBuffer.h" | 14 #include "wtf/text/StringBuffer.h" |
| 19 | 15 |
| 20 namespace blink { | 16 namespace blink { |
| 21 | 17 |
| 22 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; | 18 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; |
| 23 | 19 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 35 exceptionState.rethrowV8Exception(tryCatch.Exception()); | 31 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
| 36 return serializedValue.release(); | 32 return serializedValue.release(); |
| 37 } | 33 } |
| 38 } | 34 } |
| 39 switch (status) { | 35 switch (status) { |
| 40 case ScriptValueSerializer::InputError: | 36 case ScriptValueSerializer::InputError: |
| 41 case ScriptValueSerializer::DataCloneError: | 37 case ScriptValueSerializer::DataCloneError: |
| 42 exceptionState.throwDOMException(ScriptValueSerializer::DataCloneError,
errorMessage); | 38 exceptionState.throwDOMException(ScriptValueSerializer::DataCloneError,
errorMessage); |
| 43 return serializedValue.release(); | 39 return serializedValue.release(); |
| 44 case ScriptValueSerializer::Success: | 40 case ScriptValueSerializer::Success: |
| 45 transferData(isolate, transferables, exceptionState, serializedValue.get
(), writer); | 41 transferData(serializedValue.get(), writer, transferables, exceptionStat
e, isolate); |
| 46 return serializedValue.release(); | 42 return serializedValue.release(); |
| 47 case ScriptValueSerializer::JSException: | 43 case ScriptValueSerializer::JSException: |
| 48 ASSERT_NOT_REACHED(); | 44 ASSERT_NOT_REACHED(); |
| 49 break; | 45 break; |
| 50 } | 46 } |
| 51 ASSERT_NOT_REACHED(); | 47 ASSERT_NOT_REACHED(); |
| 52 return serializedValue.release(); | 48 return serializedValue.release(); |
| 53 } | 49 } |
| 54 | 50 |
| 55 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, Transferables* transferables, Exception
State& exceptionState) | 51 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, Transferables* transferables, Exception
State& exceptionState) |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 writer.writeWebCoreString(data); | 96 writer.writeWebCoreString(data); |
| 101 String wireData = writer.takeWireString(); | 97 String wireData = writer.takeWireString(); |
| 102 return createFromWire(wireData); | 98 return createFromWire(wireData); |
| 103 } | 99 } |
| 104 | 100 |
| 105 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create() | 101 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create() |
| 106 { | 102 { |
| 107 return adoptRef(new SerializedScriptValue()); | 103 return adoptRef(new SerializedScriptValue()); |
| 108 } | 104 } |
| 109 | 105 |
| 110 void SerializedScriptValueFactory::transferData(v8::Isolate* isolate, Transferab
les* transferables, ExceptionState& exceptionState, SerializedScriptValue* seria
lizedValue, SerializedScriptValueWriter& writer) | 106 void SerializedScriptValueFactory::transferData(SerializedScriptValue* serialize
dValue, SerializedScriptValueWriter& writer, Transferables* transferables, Excep
tionState& exceptionState, v8::Isolate* isolate) |
| 111 { | 107 { |
| 112 serializedValue->setData(writer.takeWireString()); | 108 serializedValue->setData(writer.takeWireString()); |
| 113 ASSERT(serializedValue->data().impl()->hasOneRef()); | 109 ASSERT(serializedValue->data().impl()->hasOneRef()); |
| 114 if (!transferables) | 110 if (!transferables) |
| 115 return; | 111 return; |
| 116 | 112 |
| 117 serializedValue->transferImageBitmaps(isolate, transferables->imageBitmaps,
exceptionState); | 113 serializedValue->transferImageBitmaps(isolate, transferables->imageBitmaps,
exceptionState); |
| 118 if (exceptionState.hadException()) | 114 if (exceptionState.hadException()) |
| 119 return; | 115 return; |
| 120 serializedValue->transferArrayBuffers(isolate, transferables->arrayBuffers,
exceptionState); | 116 serializedValue->transferArrayBuffers(isolate, transferables->arrayBuffers,
exceptionState); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 // information stored in m_data isn't even encoded in UTF-16. Instead, | 148 // information stored in m_data isn't even encoded in UTF-16. Instead, |
| 153 // unicode characters are encoded as UTF-8 with two code units per UChar. | 149 // unicode characters are encoded as UTF-8 with two code units per UChar. |
| 154 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)); |
| 155 ScriptValueDeserializer deserializer(reader, messagePorts, arrayBufferConten
tsArray, imageBitmapContentsArray); | 151 ScriptValueDeserializer deserializer(reader, messagePorts, arrayBufferConten
tsArray, imageBitmapContentsArray); |
| 156 | 152 |
| 157 // 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. |
| 158 // 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. |
| 159 return deserializer.deserialize(); | 155 return deserializer.deserialize(); |
| 160 } | 156 } |
| 161 | 157 |
| 162 bool SerializedScriptValueFactory::extractTransferables(v8::Isolate* isolate, Tr
ansferables& transferables, ExceptionState& exceptionState, v8::Local<v8::Value>
& transferableObject, unsigned index) | |
| 163 { | |
| 164 // Validation of Objects implementing an interface, per WebIDL spec 4.1.15. | |
| 165 if (V8MessagePort::hasInstance(transferableObject, isolate)) { | |
| 166 MessagePort* port = V8MessagePort::toImpl(v8::Local<v8::Object>::Cast(tr
ansferableObject)); | |
| 167 // Check for duplicate MessagePorts. | |
| 168 if (transferables.messagePorts.contains(port)) { | |
| 169 exceptionState.throwDOMException(DataCloneError, "Message port at in
dex " + String::number(index) + " is a duplicate of an earlier port."); | |
| 170 return false; | |
| 171 } | |
| 172 transferables.messagePorts.append(port); | |
| 173 return true; | |
| 174 } | |
| 175 if (V8ArrayBuffer::hasInstance(transferableObject, isolate)) { | |
| 176 DOMArrayBuffer* arrayBuffer = V8ArrayBuffer::toImpl(v8::Local<v8::Object
>::Cast(transferableObject)); | |
| 177 if (transferables.arrayBuffers.contains(arrayBuffer)) { | |
| 178 exceptionState.throwDOMException(DataCloneError, "ArrayBuffer at ind
ex " + String::number(index) + " is a duplicate of an earlier ArrayBuffer."); | |
| 179 return false; | |
| 180 } | |
| 181 transferables.arrayBuffers.append(arrayBuffer); | |
| 182 return true; | |
| 183 } | |
| 184 if (V8SharedArrayBuffer::hasInstance(transferableObject, isolate)) { | |
| 185 DOMSharedArrayBuffer* sharedArrayBuffer = V8SharedArrayBuffer::toImpl(v8
::Local<v8::Object>::Cast(transferableObject)); | |
| 186 if (transferables.arrayBuffers.contains(sharedArrayBuffer)) { | |
| 187 exceptionState.throwDOMException(DataCloneError, "SharedArrayBuffer
at index " + String::number(index) + " is a duplicate of an earlier SharedArrayB
uffer."); | |
| 188 return false; | |
| 189 } | |
| 190 transferables.arrayBuffers.append(sharedArrayBuffer); | |
| 191 return true; | |
| 192 } | |
| 193 if (V8ImageBitmap::hasInstance(transferableObject, isolate)) { | |
| 194 ImageBitmap* imageBitmap = V8ImageBitmap::toImpl(v8::Local<v8::Object>::
Cast(transferableObject)); | |
| 195 if (transferables.imageBitmaps.contains(imageBitmap)) { | |
| 196 exceptionState.throwDOMException(DataCloneError, "ImageBitmap at ind
ex " + String::number(index) + " is a duplicate of an earlier ImageBitmap."); | |
| 197 return false; | |
| 198 } | |
| 199 transferables.imageBitmaps.append(imageBitmap); | |
| 200 return true; | |
| 201 } | |
| 202 return false; | |
| 203 } | |
| 204 | 158 |
| 205 } // namespace blink | 159 } // namespace blink |
| OLD | NEW |