| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "bindings/core/v8/SerializedScriptValue.h" | 32 #include "bindings/core/v8/SerializedScriptValue.h" |
| 33 | 33 |
| 34 #include "bindings/core/v8/DOMDataStore.h" | 34 #include "bindings/core/v8/DOMDataStore.h" |
| 35 #include "bindings/core/v8/DOMWrapperWorld.h" | 35 #include "bindings/core/v8/DOMWrapperWorld.h" |
| 36 #include "bindings/core/v8/ExceptionState.h" | 36 #include "bindings/core/v8/ExceptionState.h" |
| 37 #include "bindings/core/v8/ScriptState.h" |
| 37 #include "bindings/core/v8/ScriptValueSerializer.h" | 38 #include "bindings/core/v8/ScriptValueSerializer.h" |
| 38 #include "bindings/core/v8/V8ArrayBuffer.h" | 39 #include "bindings/core/v8/V8ArrayBuffer.h" |
| 39 #include "bindings/core/v8/V8ArrayBufferView.h" | |
| 40 #include "bindings/core/v8/V8Binding.h" | |
| 41 #include "bindings/core/v8/V8MessagePort.h" | 40 #include "bindings/core/v8/V8MessagePort.h" |
| 42 #include "core/dom/DOMArrayBuffer.h" | |
| 43 #include "core/dom/ExceptionCode.h" | 41 #include "core/dom/ExceptionCode.h" |
| 44 #include "platform/SharedBuffer.h" | 42 #include "platform/SharedBuffer.h" |
| 45 #include "platform/blob/BlobData.h" | 43 #include "platform/blob/BlobData.h" |
| 46 #include "platform/heap/Handle.h" | 44 #include "platform/heap/Handle.h" |
| 47 #include "public/platform/Platform.h" | |
| 48 #include "wtf/ArrayBuffer.h" | |
| 49 #include "wtf/ArrayBufferContents.h" | |
| 50 #include "wtf/ArrayBufferView.h" | |
| 51 #include "wtf/Assertions.h" | 45 #include "wtf/Assertions.h" |
| 52 #include "wtf/ByteOrder.h" | 46 #include "wtf/ByteOrder.h" |
| 53 #include "wtf/Uint8ClampedArray.h" | |
| 54 #include "wtf/Vector.h" | 47 #include "wtf/Vector.h" |
| 55 #include "wtf/text/StringBuffer.h" | 48 #include "wtf/text/StringBuffer.h" |
| 56 #include "wtf/text/StringHash.h" | 49 #include "wtf/text/StringHash.h" |
| 57 #include "wtf/text/StringUTF8Adaptor.h" | |
| 58 | 50 |
| 59 namespace blink { | 51 namespace blink { |
| 60 | 52 |
| 61 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(v8::Handle<v8::V
alue> value, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffers, Exc
eptionState& exceptionState, v8::Isolate* isolate) | |
| 62 { | |
| 63 return adoptRef(new SerializedScriptValue(value, messagePorts, arrayBuffers,
0, exceptionState, isolate)); | |
| 64 } | |
| 65 | |
| 66 PassRefPtr<SerializedScriptValue> SerializedScriptValue::createAndSwallowExcepti
ons(v8::Isolate* isolate, v8::Handle<v8::Value> value) | |
| 67 { | |
| 68 TrackExceptionState exceptionState; | |
| 69 return adoptRef(new SerializedScriptValue(value, 0, 0, 0, exceptionState, is
olate)); | |
| 70 } | |
| 71 | |
| 72 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const ScriptValu
e& value, WebBlobInfoArray* blobInfo, ExceptionState& exceptionState, v8::Isolat
e* isolate) | |
| 73 { | |
| 74 ASSERT(isolate->InContext()); | |
| 75 return adoptRef(new SerializedScriptValue(value.v8Value(), 0, 0, blobInfo, e
xceptionState, isolate)); | |
| 76 } | |
| 77 | |
| 78 PassRefPtr<SerializedScriptValue> SerializedScriptValue::createFromWire(const St
ring& data) | |
| 79 { | |
| 80 return adoptRef(new SerializedScriptValue(data)); | |
| 81 } | |
| 82 | |
| 83 PassRefPtr<SerializedScriptValue> SerializedScriptValue::createFromWireBytes(con
st Vector<uint8_t>& data) | |
| 84 { | |
| 85 // Decode wire data from big endian to host byte order. | |
| 86 ASSERT(!(data.size() % sizeof(UChar))); | |
| 87 size_t length = data.size() / sizeof(UChar); | |
| 88 StringBuffer<UChar> buffer(length); | |
| 89 const UChar* src = reinterpret_cast<const UChar*>(data.data()); | |
| 90 UChar* dst = buffer.characters(); | |
| 91 for (size_t i = 0; i < length; i++) | |
| 92 dst[i] = ntohs(src[i]); | |
| 93 | |
| 94 return createFromWire(String::adopt(buffer)); | |
| 95 } | |
| 96 | |
| 97 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const String& da
ta) | |
| 98 { | |
| 99 return create(data, v8::Isolate::GetCurrent()); | |
| 100 } | |
| 101 | |
| 102 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const String& da
ta, v8::Isolate* isolate) | |
| 103 { | |
| 104 SerializedScriptValueInternal::Writer writer; | |
| 105 writer.writeWebCoreString(data); | |
| 106 String wireData = writer.takeWireString(); | |
| 107 return adoptRef(new SerializedScriptValue(wireData)); | |
| 108 } | |
| 109 | |
| 110 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create() | |
| 111 { | |
| 112 return adoptRef(new SerializedScriptValue()); | |
| 113 } | |
| 114 | |
| 115 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() | 53 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() |
| 116 { | 54 { |
| 117 SerializedScriptValueInternal::Writer writer; | 55 SerializedScriptValueWriter writer; |
| 118 writer.writeNull(); | 56 writer.writeNull(); |
| 119 String wireData = writer.takeWireString(); | 57 String wireData = writer.takeWireString(); |
| 120 return adoptRef(new SerializedScriptValue(wireData)); | 58 return adoptRef(new SerializedScriptValue(wireData)); |
| 121 } | 59 } |
| 122 | 60 |
| 123 // Convert serialized string to big endian wire data. | 61 // Convert serialized string to big endian wire data. |
| 124 void SerializedScriptValue::toWireBytes(Vector<char>& result) const | 62 void SerializedScriptValue::toWireBytes(Vector<char>& result) const |
| 125 { | 63 { |
| 126 ASSERT(result.isEmpty()); | 64 ASSERT(result.isEmpty()); |
| 127 size_t length = m_data.length(); | 65 size_t length = m_data.length(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 } | 97 } |
| 160 } else { | 98 } else { |
| 161 v8::Handle<v8::Object> wrapper = DOMWrapperWorld::current(isolate).domDa
taStore().get(object, isolate); | 99 v8::Handle<v8::Object> wrapper = DOMWrapperWorld::current(isolate).domDa
taStore().get(object, isolate); |
| 162 if (!wrapper.IsEmpty()) { | 100 if (!wrapper.IsEmpty()) { |
| 163 ASSERT(wrapper->IsArrayBuffer()); | 101 ASSERT(wrapper->IsArrayBuffer()); |
| 164 v8::Handle<v8::ArrayBuffer>::Cast(wrapper)->Neuter(); | 102 v8::Handle<v8::ArrayBuffer>::Cast(wrapper)->Neuter(); |
| 165 } | 103 } |
| 166 } | 104 } |
| 167 } | 105 } |
| 168 | 106 |
| 169 PassOwnPtr<SerializedScriptValue::ArrayBufferContentsArray> SerializedScriptValu
e::transferArrayBuffers(v8::Isolate* isolate, ArrayBufferArray& arrayBuffers, Ex
ceptionState& exceptionState) | 107 PassOwnPtr<SerializedScriptValue::ArrayBufferContentsArray> SerializedScriptValu
e::createArrayBuffers(v8::Isolate* isolate, ArrayBufferArray& arrayBuffers, Exce
ptionState& exceptionState) |
| 170 { | 108 { |
| 171 ASSERT(arrayBuffers.size()); | 109 ASSERT(arrayBuffers.size()); |
| 172 | 110 |
| 173 for (size_t i = 0; i < arrayBuffers.size(); i++) { | 111 for (size_t i = 0; i < arrayBuffers.size(); i++) { |
| 174 if (arrayBuffers[i]->isNeutered()) { | 112 if (arrayBuffers[i]->isNeutered()) { |
| 175 exceptionState.throwDOMException(DataCloneError, "ArrayBuffer at ind
ex " + String::number(i) + " is already neutered."); | 113 exceptionState.throwDOMException(DataCloneError, "ArrayBuffer at ind
ex " + String::number(i) + " is already neutered."); |
| 176 return nullptr; | 114 return nullptr; |
| 177 } | 115 } |
| 178 } | 116 } |
| 179 | 117 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 192 } | 130 } |
| 193 | 131 |
| 194 neuterArrayBufferInAllWorlds(arrayBuffers[i].get()); | 132 neuterArrayBufferInAllWorlds(arrayBuffers[i].get()); |
| 195 } | 133 } |
| 196 return contents.release(); | 134 return contents.release(); |
| 197 } | 135 } |
| 198 | 136 |
| 199 SerializedScriptValue::SerializedScriptValue(v8::Handle<v8::Value> value, Messag
ePortArray* messagePorts, ArrayBufferArray* arrayBuffers, WebBlobInfoArray* blob
Info, ExceptionState& exceptionState, v8::Isolate* isolate) | 137 SerializedScriptValue::SerializedScriptValue(v8::Handle<v8::Value> value, Messag
ePortArray* messagePorts, ArrayBufferArray* arrayBuffers, WebBlobInfoArray* blob
Info, ExceptionState& exceptionState, v8::Isolate* isolate) |
| 200 : m_externallyAllocatedMemory(0) | 138 : m_externallyAllocatedMemory(0) |
| 201 { | 139 { |
| 202 SerializedScriptValueInternal::Writer writer; | 140 SerializedScriptValueWriter writer; |
| 203 SerializedScriptValueInternal::Serializer::Status status; | 141 ScriptValueSerializer::Status status; |
| 204 String errorMessage; | 142 String errorMessage; |
| 205 { | 143 { |
| 206 v8::TryCatch tryCatch; | 144 v8::TryCatch tryCatch; |
| 207 SerializedScriptValueInternal::Serializer serializer(writer, messagePort
s, arrayBuffers, blobInfo, m_blobDataHandles, tryCatch, ScriptState::current(iso
late)); | 145 ScriptValueSerializer serializer(writer, messagePorts, arrayBuffers, blo
bInfo, m_blobDataHandles, tryCatch, ScriptState::current(isolate)); |
| 208 status = serializer.serialize(value); | 146 status = serializer.serialize(value); |
| 209 if (status == SerializedScriptValueInternal::Serializer::JSException) { | 147 if (status == ScriptValueSerializer::JSException) { |
| 210 // If there was a JS exception thrown, re-throw it. | 148 // If there was a JS exception thrown, re-throw it. |
| 211 exceptionState.rethrowV8Exception(tryCatch.Exception()); | 149 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
| 212 return; | 150 return; |
| 213 } | 151 } |
| 214 errorMessage = serializer.errorMessage(); | 152 errorMessage = serializer.errorMessage(); |
| 215 } | 153 } |
| 216 switch (status) { | 154 switch (status) { |
| 217 case SerializedScriptValueInternal::Serializer::InputError: | 155 case ScriptValueSerializer::InputError: |
| 218 case SerializedScriptValueInternal::Serializer::DataCloneError: | 156 case ScriptValueSerializer::DataCloneError: |
| 219 exceptionState.throwDOMException(DataCloneError, errorMessage); | 157 exceptionState.throwDOMException(DataCloneError, errorMessage); |
| 220 return; | 158 return; |
| 221 case SerializedScriptValueInternal::Serializer::Success: | 159 case ScriptValueSerializer::Success: |
| 222 m_data = writer.takeWireString(); | 160 m_data = writer.takeWireString(); |
| 223 ASSERT(m_data.impl()->hasOneRef()); | 161 ASSERT(m_data.impl()->hasOneRef()); |
| 224 if (arrayBuffers && arrayBuffers->size()) | 162 if (arrayBuffers && arrayBuffers->size()) |
| 225 m_arrayBufferContentsArray = transferArrayBuffers(isolate, *arrayBuf
fers, exceptionState); | 163 transferArrayBuffers(isolate, *arrayBuffers, exceptionState); |
| 226 return; | 164 return; |
| 227 case SerializedScriptValueInternal::Serializer::JSException: | 165 case ScriptValueSerializer::JSException: |
| 228 ASSERT_NOT_REACHED(); | 166 ASSERT_NOT_REACHED(); |
| 229 break; | 167 break; |
| 230 } | 168 } |
| 231 ASSERT_NOT_REACHED(); | 169 ASSERT_NOT_REACHED(); |
| 232 } | 170 } |
| 233 | 171 |
| 234 SerializedScriptValue::SerializedScriptValue(const String& wireData) | 172 SerializedScriptValue::SerializedScriptValue(const String& wireData) |
| 235 : m_externallyAllocatedMemory(0) | 173 : m_externallyAllocatedMemory(0) |
| 236 { | 174 { |
| 237 m_data = wireData.isolatedCopy(); | 175 m_data = wireData.isolatedCopy(); |
| 238 } | 176 } |
| 239 | 177 |
| 240 v8::Handle<v8::Value> SerializedScriptValue::deserialize(MessagePortArray* messa
gePorts) | 178 v8::Handle<v8::Value> SerializedScriptValue::deserialize(MessagePortArray* messa
gePorts) |
| 241 { | 179 { |
| 242 return deserialize(v8::Isolate::GetCurrent(), messagePorts, 0); | 180 return deserialize(v8::Isolate::GetCurrent(), messagePorts, 0); |
| 243 } | 181 } |
| 244 | 182 |
| 245 v8::Handle<v8::Value> SerializedScriptValue::deserialize(v8::Isolate* isolate, M
essagePortArray* messagePorts, const WebBlobInfoArray* blobInfo) | 183 v8::Handle<v8::Value> SerializedScriptValue::deserialize(v8::Isolate* isolate, M
essagePortArray* messagePorts, const WebBlobInfoArray* blobInfo) |
| 246 { | 184 { |
| 247 if (!m_data.impl()) | 185 if (!m_data.impl()) |
| 248 return v8::Null(isolate); | 186 return v8::Null(isolate); |
| 249 COMPILE_ASSERT(sizeof(SerializedScriptValueInternal::Writer::BufferValueType
) == 2, BufferValueTypeIsTwoBytes); | 187 COMPILE_ASSERT(sizeof(SerializedScriptValueWriter::BufferValueType) == 2, Bu
fferValueTypeIsTwoBytes); |
| 250 m_data.ensure16Bit(); | 188 m_data.ensure16Bit(); |
| 251 // FIXME: SerializedScriptValue shouldn't use String for its underlying | 189 // FIXME: SerializedScriptValue shouldn't use String for its underlying |
| 252 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The | 190 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The |
| 253 // information stored in m_data isn't even encoded in UTF-16. Instead, | 191 // information stored in m_data isn't even encoded in UTF-16. Instead, |
| 254 // unicode characters are encoded as UTF-8 with two code units per UChar. | 192 // unicode characters are encoded as UTF-8 with two code units per UChar. |
| 255 SerializedScriptValueInternal::Reader reader(reinterpret_cast<const uint8_t*
>(m_data.impl()->characters16()), 2 * m_data.length(), blobInfo, m_blobDataHandl
es, ScriptState::current(isolate)); | 193 SerializedScriptValueReader reader(reinterpret_cast<const uint8_t*>(m_data.i
mpl()->characters16()), 2 * m_data.length(), blobInfo, m_blobDataHandles, Script
State::current(isolate)); |
| 256 SerializedScriptValueInternal::Deserializer deserializer(reader, messagePort
s, m_arrayBufferContentsArray.get()); | 194 ScriptValueDeserializer deserializer(reader, messagePorts, m_arrayBufferCont
entsArray.get()); |
| 257 | 195 |
| 258 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. | 196 // deserialize() can run arbitrary script (e.g., setters), which could resul
t in |this| being destroyed. |
| 259 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. | 197 // Holding a RefPtr ensures we are alive (along with our internal data) thro
ughout the operation. |
| 260 RefPtr<SerializedScriptValue> protect(this); | 198 RefPtr<SerializedScriptValue> protect(this); |
| 261 return deserializer.deserialize(); | 199 return deserializer.deserialize(); |
| 262 } | 200 } |
| 263 | 201 |
| 264 bool SerializedScriptValue::extractTransferables(v8::Isolate* isolate, v8::Local
<v8::Value> value, int argumentIndex, MessagePortArray& ports, ArrayBufferArray&
arrayBuffers, ExceptionState& exceptionState) | 202 bool SerializedScriptValue::extractTransferables(v8::Isolate* isolate, v8::Local
<v8::Value> value, int argumentIndex, MessagePortArray& ports, ArrayBufferArray&
arrayBuffers, ExceptionState& exceptionState) |
| 265 { | 203 { |
| 266 if (isUndefinedOrNull(value)) { | 204 if (isUndefinedOrNull(value)) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 { | 263 { |
| 326 // If the allocated memory was not registered before, then this class is lik
ely | 264 // If the allocated memory was not registered before, then this class is lik
ely |
| 327 // used in a context other then Worker's onmessage environment and the prese
nce of | 265 // used in a context other then Worker's onmessage environment and the prese
nce of |
| 328 // current v8 context is not guaranteed. Avoid calling v8 then. | 266 // current v8 context is not guaranteed. Avoid calling v8 then. |
| 329 if (m_externallyAllocatedMemory) { | 267 if (m_externallyAllocatedMemory) { |
| 330 ASSERT(v8::Isolate::GetCurrent()); | 268 ASSERT(v8::Isolate::GetCurrent()); |
| 331 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-m_exte
rnallyAllocatedMemory); | 269 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-m_exte
rnallyAllocatedMemory); |
| 332 } | 270 } |
| 333 } | 271 } |
| 334 | 272 |
| 273 void SerializedScriptValue::transferArrayBuffers(v8::Isolate* isolate, ArrayBuff
erArray& arrayBuffers, ExceptionState& exceptionState) |
| 274 { |
| 275 m_arrayBufferContentsArray = createArrayBuffers(isolate, arrayBuffers, excep
tionState); |
| 276 } |
| 277 |
| 335 } // namespace blink | 278 } // namespace blink |
| OLD | NEW |