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 "core/dom/DOMArrayBuffer.h" | 10 #include "core/dom/DOMArrayBuffer.h" |
11 #include "core/dom/MessagePort.h" | 11 #include "core/dom/MessagePort.h" |
12 #include "core/frame/ImageBitmap.h" | 12 #include "core/frame/ImageBitmap.h" |
13 #include "wtf/ByteOrder.h" | 13 #include "wtf/ByteOrder.h" |
14 #include "wtf/text/StringBuffer.h" | 14 #include "wtf/text/StringBuffer.h" |
15 | 15 |
16 namespace blink { | 16 namespace blink { |
17 | 17 |
18 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; | 18 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; |
19 | 19 |
20 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, SerializedScriptValueWriter& writer, Tr
ansferables* transferables, WebBlobInfoArray* blobInfo, ExceptionState& exceptio
nState) | 20 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, SerializedScriptValueWriter& writer, Tr
ansferables* transferables, WebBlobInfoArray* blobInfo, ExceptionState& exceptio
nState) |
21 { | 21 { |
22 RefPtr<SerializedScriptValue> serializedValue = create(); | 22 RefPtr<SerializedScriptValue> serializedValue = SerializedScriptValue::creat
e(); |
23 ScriptValueSerializer::Status status; | 23 ScriptValueSerializer::Status status; |
24 String errorMessage; | 24 String errorMessage; |
25 { | 25 { |
26 v8::TryCatch tryCatch(isolate); | 26 v8::TryCatch tryCatch(isolate); |
27 status = doSerialize(value, writer, transferables, blobInfo, serializedV
alue.get(), tryCatch, errorMessage, isolate); | 27 status = doSerialize(value, writer, transferables, blobInfo, serializedV
alue.get(), tryCatch, errorMessage, isolate); |
28 if (status == ScriptValueSerializer::JSException) { | 28 if (status == ScriptValueSerializer::JSException) { |
29 // If there was a JS exception thrown, re-throw it. | 29 // If there was a JS exception thrown, re-throw it. |
30 exceptionState.rethrowV8Exception(tryCatch.Exception()); | 30 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
31 return serializedValue.release(); | 31 return serializedValue.release(); |
32 } | 32 } |
(...skipping 30 matching lines...) Expand all Loading... |
63 TrackExceptionState exceptionState; | 63 TrackExceptionState exceptionState; |
64 return create(isolate, value, nullptr, exceptionState); | 64 return create(isolate, value, nullptr, exceptionState); |
65 } | 65 } |
66 | 66 |
67 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, const ScriptValue& value, WebBlobInfoArray* blobInfo, ExceptionStat
e& exceptionState) | 67 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, const ScriptValue& value, WebBlobInfoArray* blobInfo, ExceptionStat
e& exceptionState) |
68 { | 68 { |
69 ASSERT(isolate->InContext()); | 69 ASSERT(isolate->InContext()); |
70 return create(isolate, value.v8Value(), nullptr, blobInfo, exceptionState); | 70 return create(isolate, value.v8Value(), nullptr, blobInfo, exceptionState); |
71 } | 71 } |
72 | 72 |
73 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createFromWire(c
onst String& data) | |
74 { | |
75 return adoptRef(new SerializedScriptValue(data)); | |
76 } | |
77 | |
78 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createFromWireBy
tes(const char* data, size_t length) | |
79 { | |
80 // Decode wire data from big endian to host byte order. | |
81 ASSERT(!(length % sizeof(UChar))); | |
82 size_t stringLength = length / sizeof(UChar); | |
83 StringBuffer<UChar> buffer(stringLength); | |
84 const UChar* src = reinterpret_cast<const UChar*>(data); | |
85 UChar* dst = buffer.characters(); | |
86 for (size_t i = 0; i < stringLength; i++) | |
87 dst[i] = ntohs(src[i]); | |
88 | |
89 return createFromWire(String::adopt(buffer)); | |
90 } | |
91 | |
92 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(const Str
ing& data) | 73 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(const Str
ing& data) |
93 { | 74 { |
94 return create(v8::Isolate::GetCurrent(), data); | |
95 } | |
96 | |
97 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, const String& data) | |
98 { | |
99 ASSERT_NOT_REACHED(); | |
100 SerializedScriptValueWriter writer; | 75 SerializedScriptValueWriter writer; |
101 writer.writeWebCoreString(data); | 76 writer.writeWebCoreString(data); |
102 String wireData = writer.takeWireString(); | 77 String wireData = writer.takeWireString(); |
103 return createFromWire(wireData); | 78 return SerializedScriptValue::create(wireData); |
104 } | |
105 | |
106 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create() | |
107 { | |
108 return adoptRef(new SerializedScriptValue()); | |
109 } | 79 } |
110 | 80 |
111 void SerializedScriptValueFactory::transferData(SerializedScriptValue* serialize
dValue, SerializedScriptValueWriter& writer, Transferables* transferables, Excep
tionState& exceptionState, v8::Isolate* isolate) | 81 void SerializedScriptValueFactory::transferData(SerializedScriptValue* serialize
dValue, SerializedScriptValueWriter& writer, Transferables* transferables, Excep
tionState& exceptionState, v8::Isolate* isolate) |
112 { | 82 { |
113 serializedValue->setData(writer.takeWireString()); | 83 serializedValue->setData(writer.takeWireString()); |
114 ASSERT(serializedValue->data().impl()->hasOneRef()); | 84 ASSERT(serializedValue->data().impl()->hasOneRef()); |
115 if (!transferables) | 85 if (!transferables) |
116 return; | 86 return; |
117 | 87 |
118 serializedValue->transferImageBitmaps(isolate, transferables->imageBitmaps,
exceptionState); | 88 serializedValue->transferImageBitmaps(isolate, transferables->imageBitmaps,
exceptionState); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 SerializedScriptValueReader reader(reinterpret_cast<const uint8_t*>(data.imp
l()->characters16()), 2 * data.length(), blobInfo, blobDataHandles, ScriptState:
:current(isolate)); | 128 SerializedScriptValueReader reader(reinterpret_cast<const uint8_t*>(data.imp
l()->characters16()), 2 * data.length(), blobInfo, blobDataHandles, ScriptState:
:current(isolate)); |
159 ScriptValueDeserializer deserializer(reader, messagePorts, arrayBufferConten
tsArray, imageBitmapContentsArray); | 129 ScriptValueDeserializer deserializer(reader, messagePorts, arrayBufferConten
tsArray, imageBitmapContentsArray); |
160 | 130 |
161 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. | 131 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. |
162 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. | 132 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. |
163 return deserializer.deserialize(); | 133 return deserializer.deserialize(); |
164 } | 134 } |
165 | 135 |
166 | 136 |
167 } // namespace blink | 137 } // namespace blink |
OLD | NEW |