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 "wtf/ByteOrder.h" | 10 #include "wtf/ByteOrder.h" |
11 #include "wtf/text/StringBuffer.h" | 11 #include "wtf/text/StringBuffer.h" |
12 | 12 |
13 namespace blink { | 13 namespace blink { |
14 | 14 |
15 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; | 15 SerializedScriptValueFactory* SerializedScriptValueFactory::m_instance = 0; |
16 | 16 |
17 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Local
<v8::Value> value, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffer
s, WebBlobInfoArray* blobInfo, ExceptionState& exceptionState, v8::Isolate* isol
ate) | 17 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, MessagePortArray* messagePorts, ArrayBu
fferArray* arrayBuffers, WebBlobInfoArray* blobInfo, ExceptionState& exceptionSt
ate) |
18 { | 18 { |
19 RefPtr<SerializedScriptValue> serializedValue = create(); | 19 RefPtr<SerializedScriptValue> serializedValue = create(); |
20 SerializedScriptValueWriter writer; | 20 SerializedScriptValueWriter writer; |
21 ScriptValueSerializer::Status status; | 21 ScriptValueSerializer::Status status; |
22 String errorMessage; | 22 String errorMessage; |
23 { | 23 { |
24 v8::TryCatch tryCatch; | 24 v8::TryCatch tryCatch; |
25 status = doSerialize(value, writer, messagePorts, arrayBuffers, blobInfo
, serializedValue.get(), tryCatch, errorMessage, isolate); | 25 status = doSerialize(value, writer, messagePorts, arrayBuffers, blobInfo
, serializedValue.get(), tryCatch, errorMessage, isolate); |
26 if (status == ScriptValueSerializer::JSException) { | 26 if (status == ScriptValueSerializer::JSException) { |
27 // If there was a JS exception thrown, re-throw it. | 27 // If there was a JS exception thrown, re-throw it. |
(...skipping 10 matching lines...) Expand all Loading... |
38 transferData(serializedValue.get(), writer, arrayBuffers, exceptionState
, isolate); | 38 transferData(serializedValue.get(), writer, arrayBuffers, exceptionState
, isolate); |
39 return serializedValue.release(); | 39 return serializedValue.release(); |
40 case ScriptValueSerializer::JSException: | 40 case ScriptValueSerializer::JSException: |
41 ASSERT_NOT_REACHED(); | 41 ASSERT_NOT_REACHED(); |
42 break; | 42 break; |
43 } | 43 } |
44 ASSERT_NOT_REACHED(); | 44 ASSERT_NOT_REACHED(); |
45 return serializedValue.release(); | 45 return serializedValue.release(); |
46 } | 46 } |
47 | 47 |
48 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Local
<v8::Value> value, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffer
s, ExceptionState& exceptionState, v8::Isolate* isolate) | 48 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, v8::Local<v8::Value> value, MessagePortArray* messagePorts, ArrayBu
fferArray* arrayBuffers, ExceptionState& exceptionState) |
49 { | 49 { |
50 return create(value, messagePorts, arrayBuffers, 0, exceptionState, isolate)
; | 50 return create(isolate, value, messagePorts, arrayBuffers, 0, exceptionState)
; |
51 } | 51 } |
52 | 52 |
53 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createAndSwallow
Exceptions(v8::Isolate* isolate, v8::Local<v8::Value> value) | 53 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createAndSwallow
Exceptions(v8::Isolate* isolate, v8::Local<v8::Value> value) |
54 { | 54 { |
55 TrackExceptionState exceptionState; | 55 TrackExceptionState exceptionState; |
56 return create(value, 0, 0, exceptionState, isolate); | 56 return create(isolate, value, 0, 0, exceptionState); |
57 } | 57 } |
58 | 58 |
59 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(const Scr
iptValue& value, WebBlobInfoArray* blobInfo, ExceptionState& exceptionState, v8:
:Isolate* isolate) | 59 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, const ScriptValue& value, WebBlobInfoArray* blobInfo, ExceptionStat
e& exceptionState) |
60 { | 60 { |
61 ASSERT(isolate->InContext()); | 61 ASSERT(isolate->InContext()); |
62 return create(value.v8Value(), 0, 0, blobInfo, exceptionState, isolate); | 62 return create(isolate, value.v8Value(), 0, 0, blobInfo, exceptionState); |
63 } | 63 } |
64 | 64 |
65 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createFromWire(c
onst String& data) | 65 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createFromWire(c
onst String& data) |
66 { | 66 { |
67 return adoptRef(new SerializedScriptValue(data)); | 67 return adoptRef(new SerializedScriptValue(data)); |
68 } | 68 } |
69 | 69 |
70 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createFromWireBy
tes(const char* data, size_t length) | 70 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::createFromWireBy
tes(const char* data, size_t length) |
71 { | 71 { |
72 // Decode wire data from big endian to host byte order. | 72 // Decode wire data from big endian to host byte order. |
73 ASSERT(!(length % sizeof(UChar))); | 73 ASSERT(!(length % sizeof(UChar))); |
74 size_t stringLength = length / sizeof(UChar); | 74 size_t stringLength = length / sizeof(UChar); |
75 StringBuffer<UChar> buffer(stringLength); | 75 StringBuffer<UChar> buffer(stringLength); |
76 const UChar* src = reinterpret_cast<const UChar*>(data); | 76 const UChar* src = reinterpret_cast<const UChar*>(data); |
77 UChar* dst = buffer.characters(); | 77 UChar* dst = buffer.characters(); |
78 for (size_t i = 0; i < stringLength; i++) | 78 for (size_t i = 0; i < stringLength; i++) |
79 dst[i] = ntohs(src[i]); | 79 dst[i] = ntohs(src[i]); |
80 | 80 |
81 return createFromWire(String::adopt(buffer)); | 81 return createFromWire(String::adopt(buffer)); |
82 } | 82 } |
83 | 83 |
84 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(const Str
ing& data) | 84 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(const Str
ing& data) |
85 { | 85 { |
86 return create(data, v8::Isolate::GetCurrent()); | 86 return create(v8::Isolate::GetCurrent(), data); |
87 } | 87 } |
88 | 88 |
89 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(const Str
ing& data, v8::Isolate* isolate) | 89 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create(v8::Isola
te* isolate, const String& data) |
90 { | 90 { |
91 ASSERT_NOT_REACHED(); | 91 ASSERT_NOT_REACHED(); |
92 SerializedScriptValueWriter writer; | 92 SerializedScriptValueWriter writer; |
93 writer.writeWebCoreString(data); | 93 writer.writeWebCoreString(data); |
94 String wireData = writer.takeWireString(); | 94 String wireData = writer.takeWireString(); |
95 return createFromWire(wireData); | 95 return createFromWire(wireData); |
96 } | 96 } |
97 | 97 |
98 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create() | 98 PassRefPtr<SerializedScriptValue> SerializedScriptValueFactory::create() |
99 { | 99 { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 SerializedScriptValueReader reader(reinterpret_cast<const uint8_t*>(data.imp
l()->characters16()), 2 * data.length(), blobInfo, blobDataHandles, ScriptState:
:current(isolate)); | 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); | 144 ScriptValueDeserializer deserializer(reader, messagePorts, arrayBufferConten
tsArray); |
145 | 145 |
146 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. | 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. | 147 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. |
148 return deserializer.deserialize(); | 148 return deserializer.deserialize(); |
149 } | 149 } |
150 | 150 |
151 | 151 |
152 } // namespace blink | 152 } // namespace blink |
OLD | NEW |