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/modules/v8/SerializedScriptValueForModulesFactory.h" |
| 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/SerializedScriptValue.h" |
| 10 #include "bindings/modules/v8/ScriptValueSerializerForModules.h" |
| 11 #include "core/dom/ExceptionCode.h" |
| 12 |
| 13 namespace blink { |
| 14 |
| 15 PassRefPtr<SerializedScriptValue> SerializedScriptValueForModulesFactory::create
(v8::Handle<v8::Value> value, MessagePortArray* messagePorts, ArrayBufferArray*
arrayBuffers, WebBlobInfoArray* blobInfo, ExceptionState& exceptionState, v8::Is
olate* isolate) |
| 16 { |
| 17 RefPtr<SerializedScriptValue> serializedValue = SerializedScriptValueFactory
::create(); |
| 18 SerializedScriptValueWriterForModules writer; |
| 19 ScriptValueSerializer::Status status; |
| 20 String errorMessage; |
| 21 { |
| 22 v8::TryCatch tryCatch; |
| 23 status = SerializedScriptValueFactory::doSerialize(value, writer, messag
ePorts, arrayBuffers, blobInfo, serializedValue.get(), tryCatch, errorMessage, i
solate); |
| 24 if (status == ScriptValueSerializer::JSException) { |
| 25 // If there was a JS exception thrown, re-throw it. |
| 26 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
| 27 return serializedValue.release(); |
| 28 } |
| 29 } |
| 30 switch (status) { |
| 31 case ScriptValueSerializer::InputError: |
| 32 case ScriptValueSerializer::DataCloneError: |
| 33 exceptionState.throwDOMException(DataCloneError, errorMessage); |
| 34 return serializedValue.release(); |
| 35 case ScriptValueSerializer::Success: |
| 36 transferData(serializedValue.get(), writer, arrayBuffers, exceptionState
, isolate); |
| 37 return serializedValue.release(); |
| 38 case ScriptValueSerializer::JSException: |
| 39 ASSERT_NOT_REACHED(); |
| 40 break; |
| 41 } |
| 42 ASSERT_NOT_REACHED(); |
| 43 return serializedValue.release(); |
| 44 } |
| 45 |
| 46 PassRefPtr<SerializedScriptValue> SerializedScriptValueForModulesFactory::create
(const String& data, v8::Isolate* isolate) |
| 47 { |
| 48 SerializedScriptValueWriterForModules writer; |
| 49 writer.writeWebCoreString(data); |
| 50 String wireData = writer.takeWireString(); |
| 51 return createFromWire(wireData); |
| 52 } |
| 53 |
| 54 ScriptValueSerializer::Status SerializedScriptValueForModulesFactory::doSerializ
e(v8::Handle<v8::Value> value, SerializedScriptValueWriter& writer, MessagePortA
rray* messagePorts, ArrayBufferArray* arrayBuffers, WebBlobInfoArray* blobInfo,
BlobDataHandleMap& blobDataHandles, v8::TryCatch& tryCatch, String& errorMessage
, v8::Isolate* isolate) |
| 55 { |
| 56 ScriptValueSerializerForModules serializer(writer, messagePorts, arrayBuffer
s, blobInfo, blobDataHandles, tryCatch, ScriptState::current(isolate)); |
| 57 ScriptValueSerializer::Status status = serializer.serialize(value); |
| 58 errorMessage = serializer.errorMessage(); |
| 59 return status; |
| 60 } |
| 61 |
| 62 v8::Handle<v8::Value> SerializedScriptValueForModulesFactory::deserialize(String
& data, BlobDataHandleMap& blobDataHandles, ArrayBufferContentsArray* arrayBuffe
rContentsArray, v8::Isolate* isolate, MessagePortArray* messagePorts, const WebB
lobInfoArray* blobInfo) |
| 63 { |
| 64 if (!data.impl()) |
| 65 return v8::Null(isolate); |
| 66 COMPILE_ASSERT(sizeof(SerializedScriptValueWriter::BufferValueType) == 2, Bu
fferValueTypeIsTwoBytes); |
| 67 data.ensure16Bit(); |
| 68 // FIXME: SerializedScriptValue shouldn't use String for its underlying |
| 69 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The |
| 70 // information stored in m_data isn't even encoded in UTF-16. Instead, |
| 71 // unicode characters are encoded as UTF-8 with two code units per UChar. |
| 72 SerializedScriptValueReaderForModules reader(reinterpret_cast<const uint8_t*
>(data.impl()->characters16()), 2 * data.length(), blobInfo, blobDataHandles, Sc
riptState::current(isolate)); |
| 73 ScriptValueDeserializerForModules deserializer(reader, messagePorts, arrayBu
fferContentsArray); |
| 74 return deserializer.deserialize(); |
| 75 } |
| 76 |
| 77 } // namespace blink |
| 78 |
OLD | NEW |