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/SerializedScriptValueForModules.h" |
| 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/modules/v8/ScriptValueSerializerForModules.h" |
| 10 #include "core/dom/ExceptionCode.h" |
| 11 #include "wtf/Assertions.h" |
| 12 #include "wtf/ByteOrder.h" |
| 13 #include "wtf/text/StringBuffer.h" |
| 14 |
| 15 namespace blink { |
| 16 |
| 17 SerializedScriptValueForModules::SerializedScriptValueForModules(v8::Handle<v8::
Value> value, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffers, We
bBlobInfoArray* blobInfo, ExceptionState& exceptionState, v8::Isolate* isolate) |
| 18 : SerializedScriptValue() |
| 19 { |
| 20 SerializedScriptValueInternal::WriterForModules writer; |
| 21 SerializedScriptValueInternal::Serializer::Status status; |
| 22 String errorMessage; |
| 23 { |
| 24 v8::TryCatch tryCatch; |
| 25 SerializedScriptValueInternal::SerializerForModules serializer(writer, m
essagePorts, arrayBuffers, blobInfo, blobDataHandles(), tryCatch, ScriptState::c
urrent(isolate)); |
| 26 status = serializer.serialize(value); |
| 27 if (status == SerializedScriptValueInternal::Serializer::JSException) { |
| 28 // If there was a JS exception thrown, re-throw it. |
| 29 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
| 30 return; |
| 31 } |
| 32 errorMessage = serializer.errorMessage(); |
| 33 } |
| 34 switch (status) { |
| 35 case SerializedScriptValueInternal::Serializer::InputError: |
| 36 case SerializedScriptValueInternal::Serializer::DataCloneError: |
| 37 exceptionState.throwDOMException(DataCloneError, errorMessage); |
| 38 return; |
| 39 case SerializedScriptValueInternal::Serializer::Success: |
| 40 setData(writer.takeWireString()); |
| 41 ASSERT(data().impl()->hasOneRef()); |
| 42 if (arrayBuffers && arrayBuffers->size()) |
| 43 transferArrayBuffers(isolate, *arrayBuffers, exceptionState); |
| 44 return; |
| 45 case SerializedScriptValueInternal::Serializer::JSException: |
| 46 ASSERT_NOT_REACHED(); |
| 47 break; |
| 48 } |
| 49 ASSERT_NOT_REACHED(); |
| 50 } |
| 51 |
| 52 v8::Handle<v8::Value> SerializedScriptValueForModules::deserialize(v8::Isolate*
isolate, MessagePortArray* messagePorts, const WebBlobInfoArray* blobInfo) |
| 53 { |
| 54 if (!data().impl()) |
| 55 return v8::Null(isolate); |
| 56 COMPILE_ASSERT(sizeof(SerializedScriptValueInternal::Writer::BufferValueType
) == 2, BufferValueTypeIsTwoBytes); |
| 57 data().ensure16Bit(); |
| 58 // FIXME: SerializedScriptValue shouldn't use String for its underlying |
| 59 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The |
| 60 // information stored in m_data isn't even encoded in UTF-16. Instead, |
| 61 // unicode characters are encoded as UTF-8 with two code units per UChar. |
| 62 SerializedScriptValueInternal::ReaderForModules reader(reinterpret_cast<cons
t uint8_t*>(data().impl()->characters16()), 2 * data().length(), blobInfo, blobD
ataHandles(), ScriptState::current(isolate)); |
| 63 SerializedScriptValueInternal::DeserializerForModules deserializer(reader, m
essagePorts, arrayBufferContentsArray()); |
| 64 |
| 65 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. |
| 66 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. |
| 67 RefPtr<SerializedScriptValue> protect(this); |
| 68 return deserializer.deserialize(); |
| 69 } |
| 70 |
| 71 } // namespace blink |
OLD | NEW |