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" | |
12 #include "core/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
13 | 11 |
14 namespace blink { | 12 namespace blink { |
15 | 13 |
16 PassRefPtr<SerializedScriptValue> SerializedScriptValueForModulesFactory::create
(v8::Isolate* isolate, v8::Local<v8::Value> value, Transferables* transferables,
WebBlobInfoArray* blobInfo, ExceptionState& exceptionState) | 14 PassRefPtr<SerializedScriptValue> SerializedScriptValueForModulesFactory::create
(v8::Isolate* isolate, v8::Local<v8::Value> value, Transferables* transferables,
WebBlobInfoArray* blobInfo, ExceptionState& exceptionState) |
17 { | 15 { |
18 RefPtr<SerializedScriptValue> serializedValue = SerializedScriptValueFactory
::create(); | 16 RefPtr<SerializedScriptValue> serializedValue = SerializedScriptValueFactory
::create(); |
19 SerializedScriptValueWriterForModules writer; | 17 SerializedScriptValueWriterForModules writer; |
20 ScriptValueSerializer::Status status; | 18 ScriptValueSerializer::Status status; |
21 String errorMessage; | 19 String errorMessage; |
22 { | 20 { |
23 v8::TryCatch tryCatch(isolate); | 21 v8::TryCatch tryCatch(isolate); |
24 status = SerializedScriptValueFactory::doSerialize(value, writer, transf
erables, blobInfo, serializedValue.get(), tryCatch, errorMessage, isolate); | 22 status = SerializedScriptValueFactory::doSerialize(value, writer, transf
erables, blobInfo, serializedValue.get(), tryCatch, errorMessage, isolate); |
25 if (status == ScriptValueSerializer::JSException) { | 23 if (status == ScriptValueSerializer::JSException) { |
26 // If there was a JS exception thrown, re-throw it. | 24 // If there was a JS exception thrown, re-throw it. |
27 exceptionState.rethrowV8Exception(tryCatch.Exception()); | 25 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
28 return serializedValue.release(); | 26 return serializedValue.release(); |
29 } | 27 } |
30 } | 28 } |
31 switch (status) { | 29 switch (status) { |
32 case ScriptValueSerializer::InputError: | 30 case ScriptValueSerializer::InputError: |
33 case ScriptValueSerializer::DataCloneError: | 31 case ScriptValueSerializer::DataCloneError: |
34 exceptionState.throwDOMException(DataCloneError, errorMessage); | 32 exceptionState.throwDOMException(DataCloneError, errorMessage); |
35 return serializedValue.release(); | 33 return serializedValue.release(); |
36 case ScriptValueSerializer::Success: | 34 case ScriptValueSerializer::Success: |
37 transferData(isolate, transferables, exceptionState, serializedValue.get
(), writer); | 35 transferData(serializedValue.get(), writer, transferables, exceptionStat
e, isolate); |
38 return serializedValue.release(); | 36 return serializedValue.release(); |
39 case ScriptValueSerializer::JSException: | 37 case ScriptValueSerializer::JSException: |
40 ASSERT_NOT_REACHED(); | 38 ASSERT_NOT_REACHED(); |
41 break; | 39 break; |
42 } | 40 } |
43 ASSERT_NOT_REACHED(); | 41 ASSERT_NOT_REACHED(); |
44 return serializedValue.release(); | 42 return serializedValue.release(); |
45 } | 43 } |
46 | 44 |
47 PassRefPtr<SerializedScriptValue> SerializedScriptValueForModulesFactory::create
(v8::Isolate* isolate, const String& data) | 45 PassRefPtr<SerializedScriptValue> SerializedScriptValueForModulesFactory::create
(v8::Isolate* isolate, const String& data) |
48 { | 46 { |
49 SerializedScriptValueWriterForModules writer; | 47 SerializedScriptValueWriterForModules writer; |
50 writer.writeWebCoreString(data); | 48 writer.writeWebCoreString(data); |
51 String wireData = writer.takeWireString(); | 49 String wireData = writer.takeWireString(); |
52 return createFromWire(wireData); | 50 return createFromWire(wireData); |
53 } | 51 } |
54 | 52 |
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 | |
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) | 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) |
83 { | 54 { |
84 ScriptValueSerializerForModules serializer(writer, transferables, blobInfo,
blobDataHandles, tryCatch, ScriptState::current(isolate)); | 55 ScriptValueSerializerForModules serializer(writer, transferables, blobInfo,
blobDataHandles, tryCatch, ScriptState::current(isolate)); |
85 ScriptValueSerializer::Status status = serializer.serialize(value); | 56 ScriptValueSerializer::Status status = serializer.serialize(value); |
86 errorMessage = serializer.errorMessage(); | 57 errorMessage = serializer.errorMessage(); |
87 return status; | 58 return status; |
88 } | 59 } |
89 | 60 |
90 v8::Local<v8::Value> SerializedScriptValueForModulesFactory::deserialize(String&
data, BlobDataHandleMap& blobDataHandles, ArrayBufferContentsArray* arrayBuffer
ContentsArray, ImageBitmapContentsArray* imageBitmapContentsArray, v8::Isolate*
isolate, MessagePortArray* messagePorts, const WebBlobInfoArray* blobInfo) | 61 v8::Local<v8::Value> SerializedScriptValueForModulesFactory::deserialize(String&
data, BlobDataHandleMap& blobDataHandles, ArrayBufferContentsArray* arrayBuffer
ContentsArray, ImageBitmapContentsArray* imageBitmapContentsArray, v8::Isolate*
isolate, MessagePortArray* messagePorts, const WebBlobInfoArray* blobInfo) |
91 { | 62 { |
92 if (!data.impl()) | 63 if (!data.impl()) |
93 return v8::Null(isolate); | 64 return v8::Null(isolate); |
94 static_assert(sizeof(SerializedScriptValueWriter::BufferValueType) == 2, "Bu
fferValueType should be 2 bytes"); | 65 static_assert(sizeof(SerializedScriptValueWriter::BufferValueType) == 2, "Bu
fferValueType should be 2 bytes"); |
95 data.ensure16Bit(); | 66 data.ensure16Bit(); |
96 // FIXME: SerializedScriptValue shouldn't use String for its underlying | 67 // FIXME: SerializedScriptValue shouldn't use String for its underlying |
97 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The | 68 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The |
98 // information stored in m_data isn't even encoded in UTF-16. Instead, | 69 // information stored in m_data isn't even encoded in UTF-16. Instead, |
99 // unicode characters are encoded as UTF-8 with two code units per UChar. | 70 // unicode characters are encoded as UTF-8 with two code units per UChar. |
100 SerializedScriptValueReaderForModules reader(reinterpret_cast<const uint8_t*
>(data.impl()->characters16()), 2 * data.length(), blobInfo, blobDataHandles, Sc
riptState::current(isolate)); | 71 SerializedScriptValueReaderForModules reader(reinterpret_cast<const uint8_t*
>(data.impl()->characters16()), 2 * data.length(), blobInfo, blobDataHandles, Sc
riptState::current(isolate)); |
101 ScriptValueDeserializerForModules deserializer(reader, messagePorts, arrayBu
fferContentsArray, imageBitmapContentsArray); | 72 ScriptValueDeserializerForModules deserializer(reader, messagePorts, arrayBu
fferContentsArray, imageBitmapContentsArray); |
102 return deserializer.deserialize(); | 73 return deserializer.deserialize(); |
103 } | 74 } |
104 | 75 |
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 | |
122 } // namespace blink | 76 } // namespace blink |
123 | 77 |
OLD | NEW |