OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
| 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/ScriptValueSerializer.h" |
| 10 #include "wtf/ByteOrder.h" |
| 11 #include "wtf/text/StringBuffer.h" |
| 12 |
| 13 namespace blink { |
| 14 |
| 15 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; |
| 16 |
| 17 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Handl
e<v8::Value> value, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffe
rs, WebBlobInfoArray* blobInfo, ExceptionState& exceptionState, v8::Isolate* iso
late) |
| 18 { |
| 19 RefPtr<SerializedScriptValue> serializedValue = create(); |
| 20 SerializedScriptValueWriter writer; |
| 21 ScriptValueSerializer::Status status; |
| 22 String errorMessage; |
| 23 { |
| 24 v8::TryCatch tryCatch; |
| 25 status = doSerialize(value, writer, messagePorts, arrayBuffers, blobInfo
, serializedValue.get(), tryCatch, errorMessage, isolate); |
| 26 if (status == ScriptValueSerializer::JSException) { |
| 27 // If there was a JS exception thrown, re-throw it. |
| 28 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
| 29 return serializedValue.release(); |
| 30 } |
| 31 } |
| 32 switch (status) { |
| 33 case ScriptValueSerializer::InputError: |
| 34 case ScriptValueSerializer::DataCloneError: |
| 35 exceptionState.throwDOMException(ScriptValueSerializer::DataCloneError,
errorMessage); |
| 36 return serializedValue.release(); |
| 37 case ScriptValueSerializer::Success: |
| 38 transferData(serializedValue.get(), writer, arrayBuffers, exceptionState
, isolate); |
| 39 return serializedValue.release(); |
| 40 case ScriptValueSerializer::JSException: |
| 41 ASSERT_NOT_REACHED(); |
| 42 break; |
| 43 } |
| 44 ASSERT_NOT_REACHED(); |
| 45 return serializedValue.release(); |
| 46 } |
| 47 |
| 48 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Handl
e<v8::Value> value, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffe
rs, ExceptionState& exceptionState, v8::Isolate* isolate) |
| 49 { |
| 50 return create(value, messagePorts, arrayBuffers, 0, exceptionState, isolate)
; |
| 51 } |
| 52 |
| 53 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createAndSwallow
Exceptions(v8::Isolate* isolate, v8::Handle<v8::Value> value) |
| 54 { |
| 55 TrackExceptionState exceptionState; |
| 56 return create(value, 0, 0, exceptionState, isolate); |
| 57 } |
| 58 |
| 59 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(const Scr
iptValue& value, WebBlobInfoArray* blobInfo, ExceptionState& exceptionState, v8:
:Isolate* isolate) |
| 60 { |
| 61 ASSERT(isolate->InContext()); |
| 62 return create(value.v8Value(), 0, 0, blobInfo, exceptionState, isolate); |
| 63 } |
| 64 |
| 65 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createFromWire(c
onst String& data) |
| 66 { |
| 67 return adoptRef(new SerializedScriptValue(data)); |
| 68 } |
| 69 |
| 70 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createFromWireBy
tes(const Vector<uint8_t>& data) |
| 71 { |
| 72 // Decode wire data from big endian to host byte order. |
| 73 ASSERT(!(data.size() % sizeof(UChar))); |
| 74 size_t length = data.size() / sizeof(UChar); |
| 75 StringBuffer<UChar> buffer(length); |
| 76 const UChar* src = reinterpret_cast<const UChar*>(data.data()); |
| 77 UChar* dst = buffer.characters(); |
| 78 for (size_t i = 0; i < length; i++) |
| 79 dst[i] = ntohs(src[i]); |
| 80 |
| 81 return createFromWire(String::adopt(buffer)); |
| 82 } |
| 83 |
| 84 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(const Str
ing& data) |
| 85 { |
| 86 return create(data, v8::Isolate::GetCurrent()); |
| 87 } |
| 88 |
| 89 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(const Str
ing& data, v8::Isolate* isolate) |
| 90 { |
| 91 ASSERT_NOT_REACHED(); |
| 92 SerializedScriptValueWriter writer; |
| 93 writer.writeWebCoreString(data); |
| 94 String wireData = writer.takeWireString(); |
| 95 return createFromWire(wireData); |
| 96 } |
| 97 |
| 98 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create() |
| 99 { |
| 100 return adoptRef(new SerializedScriptValue()); |
| 101 } |
| 102 |
| 103 void SerializedScriptValueFactory::transferData(SerializedScriptValue* serialize
dValue, SerializedScriptValueWriter& writer, ArrayBufferArray* arrayBuffers, Exc
eptionState& exceptionState, v8::Isolate* isolate) |
| 104 { |
| 105 serializedValue->setData(writer.takeWireString()); |
| 106 ASSERT(serializedValue->data().impl()->hasOneRef()); |
| 107 if (!arrayBuffers || !arrayBuffers->size()) |
| 108 return; |
| 109 serializedValue->transferArrayBuffers(isolate, *arrayBuffers, exceptionState
); |
| 110 } |
| 111 |
| 112 ScriptValueSerializer::Status SerializedScriptValueFactory::doSerialize(v8::Hand
le<v8::Value> value, SerializedScriptValueWriter& writer, MessagePortArray* mess
agePorts, ArrayBufferArray* arrayBuffers, WebBlobInfoArray* blobInfo, Serialized
ScriptValue* serializedValue, v8::TryCatch& tryCatch, String& errorMessage, v8::
Isolate* isolate) |
| 113 { |
| 114 return doSerialize(value, writer, messagePorts, arrayBuffers, blobInfo, seri
alizedValue->blobDataHandles(), tryCatch, errorMessage, isolate); |
| 115 } |
| 116 |
| 117 ScriptValueSerializer::Status SerializedScriptValueFactory::doSerialize(v8::Hand
le<v8::Value> value, SerializedScriptValueWriter& writer, MessagePortArray* mess
agePorts, ArrayBufferArray* arrayBuffers, WebBlobInfoArray* blobInfo, BlobDataHa
ndleMap& blobDataHandles, v8::TryCatch& tryCatch, String& errorMessage, v8::Isol
ate* isolate) |
| 118 { |
| 119 ScriptValueSerializer serializer(writer, messagePorts, arrayBuffers, blobInf
o, blobDataHandles, tryCatch, ScriptState::current(isolate)); |
| 120 ScriptValueSerializer::Status status = serializer.serialize(value); |
| 121 errorMessage = serializer.errorMessage(); |
| 122 return status; |
| 123 } |
| 124 |
| 125 v8::Handle<v8::Value> SerializedScriptValueFactory::deserialize(SerializedScript
Value* value, v8::Isolate* isolate, MessagePortArray* messagePorts, const WebBlo
bInfoArray* blobInfo) |
| 126 { |
| 127 // 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. |
| 129 RefPtr<SerializedScriptValue> protect(value); |
| 130 return deserialize(value->data(), value->blobDataHandles(), value->arrayBuff
erContentsArray(), isolate, messagePorts, blobInfo); |
| 131 } |
| 132 |
| 133 v8::Handle<v8::Value> SerializedScriptValueFactory::deserialize(String& data, Bl
obDataHandleMap& blobDataHandles, ArrayBufferContentsArray* arrayBufferContentsA
rray, v8::Isolate* isolate, MessagePortArray* messagePorts, const WebBlobInfoArr
ay* blobInfo) |
| 134 { |
| 135 if (!data.impl()) |
| 136 return v8::Null(isolate); |
| 137 COMPILE_ASSERT(sizeof(SerializedScriptValueWriter::BufferValueType) == 2, Bu
fferValueTypeIsTwoBytes); |
| 138 data.ensure16Bit(); |
| 139 // FIXME: SerializedScriptValue shouldn't use String for its underlying |
| 140 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The |
| 141 // information stored in m_data isn't even encoded in UTF-16. Instead, |
| 142 // unicode characters are encoded as UTF-8 with two code units per UChar. |
| 143 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); |
| 145 |
| 146 // 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. |
| 148 return deserializer.deserialize(); |
| 149 } |
| 150 |
| 151 |
| 152 } // namespace blink |
OLD | NEW |