| 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 "config.h" | 5 #include "config.h" |
| 6 #include "bindings/core/v8/SerializedScriptValueFactory.h" | 6 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/ScriptValueSerializer.h" | 9 #include "bindings/core/v8/ScriptValueSerializer.h" |
| 10 #include "core/dom/DOMArrayBuffer.h" |
| 11 #include "core/dom/DOMSharedArrayBuffer.h" |
| 10 #include "wtf/ByteOrder.h" | 12 #include "wtf/ByteOrder.h" |
| 11 #include "wtf/text/StringBuffer.h" | 13 #include "wtf/text/StringBuffer.h" |
| 12 | 14 |
| 13 namespace blink { | 15 namespace blink { |
| 14 | 16 |
| 15 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; | 17 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; |
| 16 | 18 |
| 17 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, MessagePortArray* messagePorts, ArrayBu
fferArray* arrayBuffers, WebBlobInfoArray* blobInfo, ExceptionState& exceptionSt
ate) | 19 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, MessagePortArray* messagePorts, ArrayBu
fferArray* arrayBuffers, SharedArrayBufferArray* sharedArrayBuffers, WebBlobInfo
Array* blobInfo, ExceptionState& exceptionState) |
| 18 { | 20 { |
| 19 RefPtr<SerializedScriptValue> serializedValue = create(); | 21 RefPtr<SerializedScriptValue> serializedValue = create(); |
| 20 SerializedScriptValueWriter writer; | 22 SerializedScriptValueWriter writer; |
| 21 ScriptValueSerializer::Status status; | 23 ScriptValueSerializer::Status status; |
| 22 String errorMessage; | 24 String errorMessage; |
| 23 { | 25 { |
| 24 v8::TryCatch tryCatch; | 26 v8::TryCatch tryCatch; |
| 25 status = doSerialize(value, writer, messagePorts, arrayBuffers, blobInfo
, serializedValue.get(), tryCatch, errorMessage, isolate); | 27 status = doSerialize(value, writer, messagePorts, arrayBuffers, sharedAr
rayBuffers, blobInfo, serializedValue.get(), tryCatch, errorMessage, isolate); |
| 26 if (status == ScriptValueSerializer::JSException) { | 28 if (status == ScriptValueSerializer::JSException) { |
| 27 // If there was a JS exception thrown, re-throw it. | 29 // If there was a JS exception thrown, re-throw it. |
| 28 exceptionState.rethrowV8Exception(tryCatch.Exception()); | 30 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
| 29 return serializedValue.release(); | 31 return serializedValue.release(); |
| 30 } | 32 } |
| 31 } | 33 } |
| 32 switch (status) { | 34 switch (status) { |
| 33 case ScriptValueSerializer::InputError: | 35 case ScriptValueSerializer::InputError: |
| 34 case ScriptValueSerializer::DataCloneError: | 36 case ScriptValueSerializer::DataCloneError: |
| 35 exceptionState.throwDOMException(ScriptValueSerializer::DataCloneError,
errorMessage); | 37 exceptionState.throwDOMException(ScriptValueSerializer::DataCloneError,
errorMessage); |
| 36 return serializedValue.release(); | 38 return serializedValue.release(); |
| 37 case ScriptValueSerializer::Success: | 39 case ScriptValueSerializer::Success: |
| 38 transferData(serializedValue.get(), writer, arrayBuffers, exceptionState
, isolate); | 40 transferData(serializedValue.get(), writer, arrayBuffers, sharedArrayBuf
fers, exceptionState, isolate); |
| 39 return serializedValue.release(); | 41 return serializedValue.release(); |
| 40 case ScriptValueSerializer::JSException: | 42 case ScriptValueSerializer::JSException: |
| 41 ASSERT_NOT_REACHED(); | 43 ASSERT_NOT_REACHED(); |
| 42 break; | 44 break; |
| 43 } | 45 } |
| 44 ASSERT_NOT_REACHED(); | 46 ASSERT_NOT_REACHED(); |
| 45 return serializedValue.release(); | 47 return serializedValue.release(); |
| 46 } | 48 } |
| 47 | 49 |
| 48 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, MessagePortArray* messagePorts, ArrayBu
fferArray* arrayBuffers, ExceptionState& exceptionState) | 50 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, MessagePortArray* messagePorts, ArrayBu
fferArray* arrayBuffers, SharedArrayBufferArray* sharedArrayBuffers, ExceptionSt
ate& exceptionState) |
| 49 { | 51 { |
| 50 return create(isolate, value, messagePorts, arrayBuffers, 0, exceptionState)
; | 52 return create(isolate, value, messagePorts, arrayBuffers, sharedArrayBuffers
, 0, exceptionState); |
| 51 } | 53 } |
| 52 | 54 |
| 53 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createAndSwallow
Exceptions(v8::Isolate* isolate, v8::Local<v8::Value> value) | 55 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createAndSwallow
Exceptions(v8::Isolate* isolate, v8::Local<v8::Value> value) |
| 54 { | 56 { |
| 55 TrackExceptionState exceptionState; | 57 TrackExceptionState exceptionState; |
| 56 return create(isolate, value, 0, 0, exceptionState); | 58 return create(isolate, value, 0, 0, 0, exceptionState); |
| 57 } | 59 } |
| 58 | 60 |
| 59 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, const ScriptValue& value, WebBlobInfoArray* blobInfo, ExceptionStat
e& exceptionState) | 61 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, const ScriptValue& value, WebBlobInfoArray* blobInfo, ExceptionStat
e& exceptionState) |
| 60 { | 62 { |
| 61 ASSERT(isolate->InContext()); | 63 ASSERT(isolate->InContext()); |
| 62 return create(isolate, value.v8Value(), 0, 0, blobInfo, exceptionState); | 64 return create(isolate, value.v8Value(), 0, 0, 0, blobInfo, exceptionState); |
| 63 } | 65 } |
| 64 | 66 |
| 65 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createFromWire(c
onst String& data) | 67 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createFromWire(c
onst String& data) |
| 66 { | 68 { |
| 67 return adoptRef(new SerializedScriptValue(data)); | 69 return adoptRef(new SerializedScriptValue(data)); |
| 68 } | 70 } |
| 69 | 71 |
| 70 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createFromWireBy
tes(const char* data, size_t length) | 72 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createFromWireBy
tes(const char* data, size_t length) |
| 71 { | 73 { |
| 72 // Decode wire data from big endian to host byte order. | 74 // Decode wire data from big endian to host byte order. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 93 writer.writeWebCoreString(data); | 95 writer.writeWebCoreString(data); |
| 94 String wireData = writer.takeWireString(); | 96 String wireData = writer.takeWireString(); |
| 95 return createFromWire(wireData); | 97 return createFromWire(wireData); |
| 96 } | 98 } |
| 97 | 99 |
| 98 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create() | 100 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create() |
| 99 { | 101 { |
| 100 return adoptRef(new SerializedScriptValue()); | 102 return adoptRef(new SerializedScriptValue()); |
| 101 } | 103 } |
| 102 | 104 |
| 103 void SerializedScriptValueFactory::transferData(SerializedScriptValue* serialize
dValue, SerializedScriptValueWriter& writer, ArrayBufferArray* arrayBuffers, Exc
eptionState& exceptionState, v8::Isolate* isolate) | 105 void SerializedScriptValueFactory::transferData(SerializedScriptValue* serialize
dValue, SerializedScriptValueWriter& writer, ArrayBufferArray* arrayBuffers, Sha
redArrayBufferArray* sharedArrayBuffers, ExceptionState& exceptionState, v8::Iso
late* isolate) |
| 104 { | 106 { |
| 105 serializedValue->setData(writer.takeWireString()); | 107 serializedValue->setData(writer.takeWireString()); |
| 106 ASSERT(serializedValue->data().impl()->hasOneRef()); | 108 ASSERT(serializedValue->data().impl()->hasOneRef()); |
| 107 if (!arrayBuffers || !arrayBuffers->size()) | 109 |
| 110 ArrayBufferArray emptyArrayBufferArray; |
| 111 if (!arrayBuffers) |
| 112 arrayBuffers = &emptyArrayBufferArray; |
| 113 |
| 114 SharedArrayBufferArray emptySharedArrayBufferArray; |
| 115 if (!sharedArrayBuffers) |
| 116 sharedArrayBuffers = &emptySharedArrayBufferArray; |
| 117 |
| 118 if (arrayBuffers->size() + sharedArrayBuffers->size() == 0) |
| 108 return; | 119 return; |
| 109 serializedValue->transferArrayBuffers(isolate, *arrayBuffers, exceptionState
); | 120 |
| 121 serializedValue->transferArrayBuffers(isolate, *arrayBuffers, *sharedArrayBu
ffers, exceptionState); |
| 110 } | 122 } |
| 111 | 123 |
| 112 ScriptValueSerializer::Status SerializedScriptValueFactory::doSerialize(v8::Loca
l<v8::Value> value, SerializedScriptValueWriter& writer, MessagePortArray* messa
gePorts, ArrayBufferArray* arrayBuffers, WebBlobInfoArray* blobInfo, SerializedS
criptValue* serializedValue, v8::TryCatch& tryCatch, String& errorMessage, v8::I
solate* isolate) | 124 ScriptValueSerializer::Status SerializedScriptValueFactory::doSerialize(v8::Loca
l<v8::Value> value, SerializedScriptValueWriter& writer, MessagePortArray* messa
gePorts, ArrayBufferArray* arrayBuffers, SharedArrayBufferArray* sharedArrayBuff
ers, WebBlobInfoArray* blobInfo, SerializedScriptValue* serializedValue, v8::Try
Catch& tryCatch, String& errorMessage, v8::Isolate* isolate) |
| 113 { | 125 { |
| 114 return doSerialize(value, writer, messagePorts, arrayBuffers, blobInfo, seri
alizedValue->blobDataHandles(), tryCatch, errorMessage, isolate); | 126 return doSerialize(value, writer, messagePorts, arrayBuffers, sharedArrayBuf
fers, blobInfo, serializedValue->blobDataHandles(), tryCatch, errorMessage, isol
ate); |
| 115 } | 127 } |
| 116 | 128 |
| 117 ScriptValueSerializer::Status SerializedScriptValueFactory::doSerialize(v8::Loca
l<v8::Value> value, SerializedScriptValueWriter& writer, MessagePortArray* messa
gePorts, ArrayBufferArray* arrayBuffers, WebBlobInfoArray* blobInfo, BlobDataHan
dleMap& blobDataHandles, v8::TryCatch& tryCatch, String& errorMessage, v8::Isola
te* isolate) | 129 ScriptValueSerializer::Status SerializedScriptValueFactory::doSerialize(v8::Loca
l<v8::Value> value, SerializedScriptValueWriter& writer, MessagePortArray* messa
gePorts, ArrayBufferArray* arrayBuffers, SharedArrayBufferArray* sharedArrayBuff
ers, WebBlobInfoArray* blobInfo, BlobDataHandleMap& blobDataHandles, v8::TryCatc
h& tryCatch, String& errorMessage, v8::Isolate* isolate) |
| 118 { | 130 { |
| 119 ScriptValueSerializer serializer(writer, messagePorts, arrayBuffers, blobInf
o, blobDataHandles, tryCatch, ScriptState::current(isolate)); | 131 ScriptValueSerializer serializer(writer, messagePorts, arrayBuffers, sharedA
rrayBuffers, blobInfo, blobDataHandles, tryCatch, ScriptState::current(isolate))
; |
| 120 ScriptValueSerializer::Status status = serializer.serialize(value); | 132 ScriptValueSerializer::Status status = serializer.serialize(value); |
| 121 errorMessage = serializer.errorMessage(); | 133 errorMessage = serializer.errorMessage(); |
| 122 return status; | 134 return status; |
| 123 } | 135 } |
| 124 | 136 |
| 125 v8::Local<v8::Value> SerializedScriptValueFactory::deserialize(SerializedScriptV
alue* value, v8::Isolate* isolate, MessagePortArray* messagePorts, const WebBlob
InfoArray* blobInfo) | 137 v8::Local<v8::Value> SerializedScriptValueFactory::deserialize(SerializedScriptV
alue* value, v8::Isolate* isolate, MessagePortArray* messagePorts, const WebBlob
InfoArray* blobInfo) |
| 126 { | 138 { |
| 127 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. | 139 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. |
| 128 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. | 140 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. |
| 129 RefPtr<SerializedScriptValue> protect(value); | 141 RefPtr<SerializedScriptValue> protect(value); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 143 SerializedScriptValueReader reader(reinterpret_cast<const uint8_t*>(data.imp
l()->characters16()), 2 * data.length(), blobInfo, blobDataHandles, ScriptState:
:current(isolate)); | 155 SerializedScriptValueReader reader(reinterpret_cast<const uint8_t*>(data.imp
l()->characters16()), 2 * data.length(), blobInfo, blobDataHandles, ScriptState:
:current(isolate)); |
| 144 ScriptValueDeserializer deserializer(reader, messagePorts, arrayBufferConten
tsArray); | 156 ScriptValueDeserializer deserializer(reader, messagePorts, arrayBufferConten
tsArray); |
| 145 | 157 |
| 146 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. | 158 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. |
| 147 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. | 159 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. |
| 148 return deserializer.deserialize(); | 160 return deserializer.deserialize(); |
| 149 } | 161 } |
| 150 | 162 |
| 151 | 163 |
| 152 } // namespace blink | 164 } // namespace blink |
| OLD | NEW |