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/modules/v8/SerializedScriptValueForModulesFactory.h" | 5 #include "bindings/modules/v8/SerializedScriptValueForModulesFactory.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/core/v8/SerializedScriptValue.h" | 8 #include "bindings/core/v8/SerializedScriptValue.h" |
9 #include "bindings/modules/v8/ScriptValueSerializerForModules.h" | 9 #include "bindings/modules/v8/ScriptValueSerializerForModules.h" |
| 10 #include "bindings/modules/v8/TransferablesForModules.h" |
| 11 #include "bindings/modules/v8/V8OffscreenCanvas.h" |
10 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
11 | 13 |
12 namespace blink { | 14 namespace blink { |
13 | 15 |
14 PassRefPtr<SerializedScriptValue> SerializedScriptValueForModulesFactory::create
(v8::Isolate* isolate, v8::Local<v8::Value> value, Transferables* transferables,
WebBlobInfoArray* blobInfo, ExceptionState& exceptionState) | 16 PassRefPtr<SerializedScriptValue> SerializedScriptValueForModulesFactory::create
(v8::Isolate* isolate, v8::Local<v8::Value> value, Transferables* transferables,
WebBlobInfoArray* blobInfo, ExceptionState& exceptionState) |
15 { | 17 { |
16 RefPtr<SerializedScriptValue> serializedValue = SerializedScriptValueFactory
::create(); | 18 RefPtr<SerializedScriptValue> serializedValue = SerializedScriptValueFactory
::create(); |
17 SerializedScriptValueWriterForModules writer; | 19 SerializedScriptValueWriterForModules writer; |
18 ScriptValueSerializer::Status status; | 20 ScriptValueSerializer::Status status; |
19 String errorMessage; | 21 String errorMessage; |
20 { | 22 { |
21 v8::TryCatch tryCatch(isolate); | 23 v8::TryCatch tryCatch(isolate); |
22 status = SerializedScriptValueFactory::doSerialize(value, writer, transf
erables, blobInfo, serializedValue.get(), tryCatch, errorMessage, isolate); | 24 status = SerializedScriptValueFactory::doSerialize(value, writer, transf
erables, blobInfo, serializedValue.get(), tryCatch, errorMessage, isolate); |
23 if (status == ScriptValueSerializer::JSException) { | 25 if (status == ScriptValueSerializer::JSException) { |
24 // If there was a JS exception thrown, re-throw it. | 26 // If there was a JS exception thrown, re-throw it. |
25 exceptionState.rethrowV8Exception(tryCatch.Exception()); | 27 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
26 return serializedValue.release(); | 28 return serializedValue.release(); |
27 } | 29 } |
28 } | 30 } |
29 switch (status) { | 31 switch (status) { |
30 case ScriptValueSerializer::InputError: | 32 case ScriptValueSerializer::InputError: |
31 case ScriptValueSerializer::DataCloneError: | 33 case ScriptValueSerializer::DataCloneError: |
32 exceptionState.throwDOMException(DataCloneError, errorMessage); | 34 exceptionState.throwDOMException(DataCloneError, errorMessage); |
33 return serializedValue.release(); | 35 return serializedValue.release(); |
34 case ScriptValueSerializer::Success: | 36 case ScriptValueSerializer::Success: |
35 transferData(serializedValue.get(), writer, transferables, exceptionStat
e, isolate); | 37 transferData(isolate, transferables, exceptionState, serializedValue.get
(), writer); |
36 return serializedValue.release(); | 38 return serializedValue.release(); |
37 case ScriptValueSerializer::JSException: | 39 case ScriptValueSerializer::JSException: |
38 ASSERT_NOT_REACHED(); | 40 ASSERT_NOT_REACHED(); |
39 break; | 41 break; |
40 } | 42 } |
41 ASSERT_NOT_REACHED(); | 43 ASSERT_NOT_REACHED(); |
42 return serializedValue.release(); | 44 return serializedValue.release(); |
43 } | 45 } |
44 | 46 |
45 PassRefPtr<SerializedScriptValue> SerializedScriptValueForModulesFactory::create
(v8::Isolate* isolate, const String& data) | 47 PassRefPtr<SerializedScriptValue> SerializedScriptValueForModulesFactory::create
(v8::Isolate* isolate, const String& data) |
46 { | 48 { |
47 SerializedScriptValueWriterForModules writer; | 49 SerializedScriptValueWriterForModules writer; |
48 writer.writeWebCoreString(data); | 50 writer.writeWebCoreString(data); |
49 String wireData = writer.takeWireString(); | 51 String wireData = writer.takeWireString(); |
50 return createFromWire(wireData); | 52 return createFromWire(wireData); |
51 } | 53 } |
52 | 54 |
| 55 void SerializedScriptValueForModulesFactory::transferData(v8::Isolate* isolate,
Transferables* transferables, ExceptionState& exceptionState, SerializedScriptVa
lue* serializedValue, SerializedScriptValueWriter& writer) |
| 56 { |
| 57 serializedValue->setData(writer.takeWireString()); |
| 58 ASSERT(serializedValue->data().impl()->hasOneRef()); |
| 59 if (!transferables) |
| 60 return; |
| 61 auto& offscreenCanvases = static_cast<TransferablesForModules*>(transferable
s)->offscreenCanvases; |
| 62 if (offscreenCanvases.size()) { |
| 63 HeapHashSet<Member<OffscreenCanvas>> visited; |
| 64 for (size_t i = 0; i < offscreenCanvases.size(); i++) { |
| 65 if (offscreenCanvases[i]->isNeutered()) { |
| 66 exceptionState.throwDOMException(DataCloneError, "OffscreenCanva
s at index " + String::number(i) + " is already neutered."); |
| 67 return; |
| 68 } |
| 69 if (offscreenCanvases[i]->renderingContext()) { |
| 70 exceptionState.throwDOMException(DataCloneError, "OffscreenCanva
s at index " + String::number(i) + " has an associated context."); |
| 71 return; |
| 72 } |
| 73 if (visited.contains(offscreenCanvases[i].get())) |
| 74 continue; |
| 75 visited.add(offscreenCanvases[i].get()); |
| 76 offscreenCanvases[i].get()->setNeutered(); |
| 77 } |
| 78 } |
| 79 SerializedScriptValueFactory::transferData(isolate, transferables, exception
State, serializedValue, writer); |
| 80 } |
| 81 |
53 ScriptValueSerializer::Status SerializedScriptValueForModulesFactory::doSerializ
e(v8::Local<v8::Value> value, SerializedScriptValueWriter& writer, Transferables
* transferables, WebBlobInfoArray* blobInfo, BlobDataHandleMap& blobDataHandles,
v8::TryCatch& tryCatch, String& errorMessage, v8::Isolate* isolate) | 82 ScriptValueSerializer::Status SerializedScriptValueForModulesFactory::doSerializ
e(v8::Local<v8::Value> value, SerializedScriptValueWriter& writer, Transferables
* transferables, WebBlobInfoArray* blobInfo, BlobDataHandleMap& blobDataHandles,
v8::TryCatch& tryCatch, String& errorMessage, v8::Isolate* isolate) |
54 { | 83 { |
55 ScriptValueSerializerForModules serializer(writer, transferables, blobInfo,
blobDataHandles, tryCatch, ScriptState::current(isolate)); | 84 ScriptValueSerializerForModules serializer(writer, transferables, blobInfo,
blobDataHandles, tryCatch, ScriptState::current(isolate)); |
56 ScriptValueSerializer::Status status = serializer.serialize(value); | 85 ScriptValueSerializer::Status status = serializer.serialize(value); |
57 errorMessage = serializer.errorMessage(); | 86 errorMessage = serializer.errorMessage(); |
58 return status; | 87 return status; |
59 } | 88 } |
60 | 89 |
61 v8::Local<v8::Value> SerializedScriptValueForModulesFactory::deserialize(String&
data, BlobDataHandleMap& blobDataHandles, ArrayBufferContentsArray* arrayBuffer
ContentsArray, ImageBitmapContentsArray* imageBitmapContentsArray, v8::Isolate*
isolate, MessagePortArray* messagePorts, const WebBlobInfoArray* blobInfo) | 90 v8::Local<v8::Value> SerializedScriptValueForModulesFactory::deserialize(String&
data, BlobDataHandleMap& blobDataHandles, ArrayBufferContentsArray* arrayBuffer
ContentsArray, ImageBitmapContentsArray* imageBitmapContentsArray, v8::Isolate*
isolate, MessagePortArray* messagePorts, const WebBlobInfoArray* blobInfo) |
62 { | 91 { |
63 if (!data.impl()) | 92 if (!data.impl()) |
64 return v8::Null(isolate); | 93 return v8::Null(isolate); |
65 static_assert(sizeof(SerializedScriptValueWriter::BufferValueType) == 2, "Bu
fferValueType should be 2 bytes"); | 94 static_assert(sizeof(SerializedScriptValueWriter::BufferValueType) == 2, "Bu
fferValueType should be 2 bytes"); |
66 data.ensure16Bit(); | 95 data.ensure16Bit(); |
67 // FIXME: SerializedScriptValue shouldn't use String for its underlying | 96 // FIXME: SerializedScriptValue shouldn't use String for its underlying |
68 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The | 97 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The |
69 // information stored in m_data isn't even encoded in UTF-16. Instead, | 98 // information stored in m_data isn't even encoded in UTF-16. Instead, |
70 // unicode characters are encoded as UTF-8 with two code units per UChar. | 99 // unicode characters are encoded as UTF-8 with two code units per UChar. |
71 SerializedScriptValueReaderForModules reader(reinterpret_cast<const uint8_t*
>(data.impl()->characters16()), 2 * data.length(), blobInfo, blobDataHandles, Sc
riptState::current(isolate)); | 100 SerializedScriptValueReaderForModules reader(reinterpret_cast<const uint8_t*
>(data.impl()->characters16()), 2 * data.length(), blobInfo, blobDataHandles, Sc
riptState::current(isolate)); |
72 ScriptValueDeserializerForModules deserializer(reader, messagePorts, arrayBu
fferContentsArray, imageBitmapContentsArray); | 101 ScriptValueDeserializerForModules deserializer(reader, messagePorts, arrayBu
fferContentsArray, imageBitmapContentsArray); |
73 return deserializer.deserialize(); | 102 return deserializer.deserialize(); |
74 } | 103 } |
75 | 104 |
| 105 bool SerializedScriptValueForModulesFactory::extractTransferables(v8::Isolate* i
solate, Transferables& transferables, ExceptionState& exceptionState, v8::Local<
v8::Value>& transferableObject, unsigned index) |
| 106 { |
| 107 if (SerializedScriptValueFactory::extractTransferables(isolate, transferable
s, exceptionState, transferableObject, index)) |
| 108 return true; |
| 109 if (V8OffscreenCanvas::hasInstance(transferableObject, isolate)) { |
| 110 OffscreenCanvas* offscreenCanvas = V8OffscreenCanvas::toImpl(v8::Local<v
8::Object>::Cast(transferableObject)); |
| 111 auto& offscreenCanvases = static_cast<TransferablesForModules*>(&transfe
rables)->offscreenCanvases; |
| 112 if (offscreenCanvases.contains(offscreenCanvas)) { |
| 113 exceptionState.throwDOMException(DataCloneError, "OffscreenCanvas at
index " + String::number(index) + " is a duplicate of an earlier OffscreenCanva
s."); |
| 114 return false; |
| 115 } |
| 116 offscreenCanvases.append(offscreenCanvas); |
| 117 return true; |
| 118 } |
| 119 return false; |
| 120 } |
| 121 |
76 } // namespace blink | 122 } // namespace blink |
77 | 123 |
OLD | NEW |