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/ScriptValueSerializerForModules.h" | 5 #include "bindings/modules/v8/ScriptValueSerializerForModules.h" |
6 | 6 |
7 #include "bindings/core/v8/SerializationTag.h" | 7 #include "bindings/core/v8/SerializationTag.h" |
8 #include "bindings/core/v8/V8Binding.h" | 8 #include "bindings/core/v8/V8Binding.h" |
| 9 #include "bindings/modules/v8/TransferableOffscreenCanvas.h" |
9 #include "bindings/modules/v8/V8CryptoKey.h" | 10 #include "bindings/modules/v8/V8CryptoKey.h" |
10 #include "bindings/modules/v8/V8DOMFileSystem.h" | 11 #include "bindings/modules/v8/V8DOMFileSystem.h" |
| 12 #include "bindings/modules/v8/V8OffscreenCanvas.h" |
11 #include "modules/filesystem/DOMFileSystem.h" | 13 #include "modules/filesystem/DOMFileSystem.h" |
12 #include "public/platform/Platform.h" | 14 #include "public/platform/Platform.h" |
13 | 15 |
14 namespace blink { | 16 namespace blink { |
15 | 17 |
16 enum CryptoKeyAlgorithmTag { | 18 enum CryptoKeyAlgorithmTag { |
17 AesCbcTag = 1, | 19 AesCbcTag = 1, |
18 HmacTag = 2, | 20 HmacTag = 2, |
19 RsaSsaPkcs1v1_5Tag = 3, | 21 RsaSsaPkcs1v1_5Tag = 3, |
20 // ID 4 was used by RsaEs, while still behind experimental flag. | 22 // ID 4 was used by RsaEs, while still behind experimental flag. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 NoParamsKeyTag = 6, | 67 NoParamsKeyTag = 6, |
66 // Maximum allowed value is 255 | 68 // Maximum allowed value is 255 |
67 }; | 69 }; |
68 | 70 |
69 enum AssymetricCryptoKeyType { | 71 enum AssymetricCryptoKeyType { |
70 PublicKeyType = 1, | 72 PublicKeyType = 1, |
71 PrivateKeyType = 2, | 73 PrivateKeyType = 2, |
72 // Maximum allowed value is 2^32-1 | 74 // Maximum allowed value is 2^32-1 |
73 }; | 75 }; |
74 | 76 |
| 77 static v8::Local<v8::Object> toV8Object(OffscreenCanvas* impl, v8::Local<v8::Obj
ect> creationContext, v8::Isolate* isolate) |
| 78 { |
| 79 if (!impl) |
| 80 return v8::Local<v8::Object>(); |
| 81 v8::Local<v8::Value> wrapper = toV8(impl, creationContext, isolate); |
| 82 if (wrapper.IsEmpty()) |
| 83 return v8::Local<v8::Object>(); |
| 84 return wrapper.As<v8::Object>(); |
| 85 } |
75 | 86 |
76 ScriptValueSerializerForModules::ScriptValueSerializerForModules(SerializedScrip
tValueWriter& writer, MessagePortArray* messagePorts, TransferableArray* transfe
rables, WebBlobInfoArray* blobInfo, BlobDataHandleMap& blobDataHandles, v8::TryC
atch& tryCatch, ScriptState* scriptState) | 87 ScriptValueSerializerForModules::ScriptValueSerializerForModules(SerializedScrip
tValueWriter& writer, MessagePortArray* messagePorts, TransferableArray* transfe
rables, WebBlobInfoArray* blobInfo, BlobDataHandleMap& blobDataHandles, v8::TryC
atch& tryCatch, ScriptState* scriptState) |
77 : ScriptValueSerializer(writer, messagePorts, transferables, blobInfo, blobD
ataHandles, tryCatch, scriptState) | 88 : ScriptValueSerializer(writer, messagePorts, transferables, blobInfo, blobD
ataHandles, tryCatch, scriptState) |
| 89 , m_writer(writer) |
78 { | 90 { |
| 91 ASSERT(!tryCatch.HasCaught()); |
| 92 v8::Local<v8::Object> creationContext = scriptState->context()->Global(); |
| 93 if (!transferables) |
| 94 return; |
| 95 if (auto* offscreenCanvases = TransferableOffscreenCanvas::get(*transferable
s)) { |
| 96 for (size_t i = 0; i < offscreenCanvases->getArray().size(); i++) { |
| 97 v8::Local<v8::Object> v8OffscreenCanvas = toV8Object(offscreenCanvas
es->getArray().at(i).get(), creationContext, isolate()); |
| 98 if (!m_transferredOffscreenCanvas.contains(v8OffscreenCanvas)) |
| 99 m_transferredOffscreenCanvas.set(v8OffscreenCanvas, i); |
| 100 } |
| 101 } |
79 } | 102 } |
80 | 103 |
81 ScriptValueSerializer::StateBase* ScriptValueSerializerForModules::writeDOMFileS
ystem(v8::Local<v8::Value> value, ScriptValueSerializer::StateBase* next) | 104 ScriptValueSerializer::StateBase* ScriptValueSerializerForModules::writeDOMFileS
ystem(v8::Local<v8::Value> value, ScriptValueSerializer::StateBase* next) |
82 { | 105 { |
83 DOMFileSystem* fs = V8DOMFileSystem::toImpl(value.As<v8::Object>()); | 106 DOMFileSystem* fs = V8DOMFileSystem::toImpl(value.As<v8::Object>()); |
84 if (!fs) | 107 if (!fs) |
85 return 0; | 108 return 0; |
86 if (!fs->clonable()) | 109 if (!fs->clonable()) |
87 return handleError(DataCloneError, "A FileSystem object could not be clo
ned.", next); | 110 return handleError(DataCloneError, "A FileSystem object could not be clo
ned.", next); |
88 | 111 |
(...skipping 10 matching lines...) Expand all Loading... |
99 } | 122 } |
100 | 123 |
101 void SerializedScriptValueWriterForModules::writeDOMFileSystem(int type, const S
tring& name, const String& url) | 124 void SerializedScriptValueWriterForModules::writeDOMFileSystem(int type, const S
tring& name, const String& url) |
102 { | 125 { |
103 append(DOMFileSystemTag); | 126 append(DOMFileSystemTag); |
104 doWriteUint32(type); | 127 doWriteUint32(type); |
105 doWriteWebCoreString(name); | 128 doWriteWebCoreString(name); |
106 doWriteWebCoreString(url); | 129 doWriteWebCoreString(url); |
107 } | 130 } |
108 | 131 |
| 132 void SerializedScriptValueWriterForModules::writeTransferredOffscreenCanvas(uint
32_t index, uint32_t width, uint32_t height, uint32_t id) |
| 133 { |
| 134 append(OffscreenCanvasTransferTag); |
| 135 doWriteUint32(index); |
| 136 doWriteUint32(width); |
| 137 doWriteUint32(height); |
| 138 doWriteUint32(id); |
| 139 } |
| 140 |
109 bool SerializedScriptValueWriterForModules::writeCryptoKey(const WebCryptoKey& k
ey) | 141 bool SerializedScriptValueWriterForModules::writeCryptoKey(const WebCryptoKey& k
ey) |
110 { | 142 { |
111 append(static_cast<uint8_t>(CryptoKeyTag)); | 143 append(static_cast<uint8_t>(CryptoKeyTag)); |
112 | 144 |
113 switch (key.algorithm().paramsType()) { | 145 switch (key.algorithm().paramsType()) { |
114 case WebCryptoKeyAlgorithmParamsTypeAes: | 146 case WebCryptoKeyAlgorithmParamsTypeAes: |
115 doWriteAesKey(key); | 147 doWriteAesKey(key); |
116 break; | 148 break; |
117 case WebCryptoKeyAlgorithmParamsTypeHmac: | 149 case WebCryptoKeyAlgorithmParamsTypeHmac: |
118 doWriteHmacKey(key); | 150 doWriteHmacKey(key); |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 return handleError(DataCloneError, "An object could not be cloned.",
next); | 331 return handleError(DataCloneError, "An object could not be cloned.",
next); |
300 greyObject(jsObject); | 332 greyObject(jsObject); |
301 | 333 |
302 if (isDOMFileSystem) | 334 if (isDOMFileSystem) |
303 return writeDOMFileSystem(value, next); | 335 return writeDOMFileSystem(value, next); |
304 | 336 |
305 if (!writeCryptoKey(value)) | 337 if (!writeCryptoKey(value)) |
306 return handleError(DataCloneError, "Couldn't serialize key data", ne
xt); | 338 return handleError(DataCloneError, "Couldn't serialize key data", ne
xt); |
307 return 0; | 339 return 0; |
308 } | 340 } |
| 341 v8::Local<v8::Object> jsObject = value.As<v8::Object>(); |
| 342 uint32_t offscreenCanvasIndex; |
| 343 if (V8OffscreenCanvas::hasInstance(value, isolate()) && m_transferredOffscre
enCanvas.tryGet(jsObject, &offscreenCanvasIndex)) { |
| 344 return writeTransferredOffscreenCanvas(value, offscreenCanvasIndex, next
); |
| 345 } |
309 return ScriptValueSerializer::doSerializeValue(value, next); | 346 return ScriptValueSerializer::doSerializeValue(value, next); |
310 } | 347 } |
311 | 348 |
| 349 ScriptValueSerializer::StateBase* ScriptValueSerializerForModules::writeTransfer
redOffscreenCanvas(v8::Local<v8::Value> value, uint32_t index, ScriptValueSerial
izer::StateBase* next) |
| 350 { |
| 351 OffscreenCanvas* offscreenCanvas = V8OffscreenCanvas::toImpl(value.As<v8::Ob
ject>()); |
| 352 if (!offscreenCanvas) |
| 353 return 0; |
| 354 if (offscreenCanvas->isNeutered()) |
| 355 return handleError(DataCloneError, "An OffscreenCanvas is neutered and c
ould not be cloned.", next); |
| 356 if (offscreenCanvas->renderingContext()) |
| 357 return handleError(DataCloneError, "An OffscreenCanvas with a context co
uld not be cloned.", next); |
| 358 m_writer.writeTransferredOffscreenCanvas(index, offscreenCanvas->width(), of
fscreenCanvas->height(), offscreenCanvas->getAssociatedCanvasId()); |
| 359 return 0; |
| 360 } |
| 361 |
312 bool SerializedScriptValueReaderForModules::read(v8::Local<v8::Value>* value, Sc
riptValueCompositeCreator& creator) | 362 bool SerializedScriptValueReaderForModules::read(v8::Local<v8::Value>* value, Sc
riptValueCompositeCreator& creator) |
313 { | 363 { |
314 SerializationTag tag; | 364 SerializationTag tag; |
315 if (!readTag(&tag)) | 365 if (!readTag(&tag)) |
316 return false; | 366 return false; |
317 switch (tag) { | 367 switch (tag) { |
318 case DOMFileSystemTag: | 368 case DOMFileSystemTag: |
319 if (!readDOMFileSystem(value)) | 369 if (!readDOMFileSystem(value)) |
320 return false; | 370 return false; |
321 creator.pushObjectReference(*value); | 371 creator.pushObjectReference(*value); |
322 break; | 372 break; |
323 case CryptoKeyTag: | 373 case CryptoKeyTag: |
324 if (!readCryptoKey(value)) | 374 if (!readCryptoKey(value)) |
325 return false; | 375 return false; |
326 creator.pushObjectReference(*value); | 376 creator.pushObjectReference(*value); |
327 break; | 377 break; |
| 378 case OffscreenCanvasTransferTag: |
| 379 uint32_t index, width, height, id; |
| 380 if (!doReadUint32(&index)) |
| 381 return false; |
| 382 if (!doReadUint32(&width)) |
| 383 return false; |
| 384 if (!doReadUint32(&height)) |
| 385 return false; |
| 386 if (!doReadUint32(&id)) |
| 387 return false; |
| 388 if (!creator.tryGetTransferredOffscreenCanvas(index, width, height, id,
value)) |
| 389 return false; |
| 390 break; |
328 default: | 391 default: |
329 return SerializedScriptValueReader::readWithTag(tag, value, creator); | 392 return SerializedScriptValueReader::readWithTag(tag, value, creator); |
330 } | 393 } |
331 return !value->IsEmpty(); | 394 return !value->IsEmpty(); |
332 } | 395 } |
333 | 396 |
334 bool SerializedScriptValueReaderForModules::readDOMFileSystem(v8::Local<v8::Valu
e>* value) | 397 bool SerializedScriptValueReaderForModules::readDOMFileSystem(v8::Local<v8::Valu
e>* value) |
335 { | 398 { |
336 uint32_t type; | 399 uint32_t type; |
337 String name; | 400 String name; |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 if (rawUsages & UnwrapKeyUsage) | 681 if (rawUsages & UnwrapKeyUsage) |
619 usages |= WebCryptoKeyUsageUnwrapKey; | 682 usages |= WebCryptoKeyUsageUnwrapKey; |
620 if (rawUsages & DeriveBitsUsage) | 683 if (rawUsages & DeriveBitsUsage) |
621 usages |= WebCryptoKeyUsageDeriveBits; | 684 usages |= WebCryptoKeyUsageDeriveBits; |
622 | 685 |
623 return true; | 686 return true; |
624 } | 687 } |
625 | 688 |
626 ScriptValueDeserializerForModules::ScriptValueDeserializerForModules(SerializedS
criptValueReaderForModules& reader, MessagePortArray* messagePorts, ArrayBufferC
ontentsArray* arrayBufferContents, ImageBitmapContentsArray* imageBitmapContents
) | 689 ScriptValueDeserializerForModules::ScriptValueDeserializerForModules(SerializedS
criptValueReaderForModules& reader, MessagePortArray* messagePorts, ArrayBufferC
ontentsArray* arrayBufferContents, ImageBitmapContentsArray* imageBitmapContents
) |
627 : ScriptValueDeserializer(reader, messagePorts, arrayBufferContents, imageBi
tmapContents) | 690 : ScriptValueDeserializer(reader, messagePorts, arrayBufferContents, imageBi
tmapContents) |
| 691 , m_offscreenCanvases(0) |
| 692 , m_reader(reader) |
628 { | 693 { |
629 } | 694 } |
630 | 695 |
| 696 bool ScriptValueDeserializerForModules::tryGetTransferredOffscreenCanvas(uint32_
t index, uint32_t width, uint32_t height, uint32_t id, v8::Local<v8::Value>* obj
ect) |
| 697 { |
| 698 v8::Local<v8::Value> result; |
| 699 OffscreenCanvas* offscreenCanvas = OffscreenCanvas::create(width, height); |
| 700 offscreenCanvas->setAssociatedCanvasId(id); |
| 701 v8::Isolate* isolate = m_reader.getScriptState()->isolate(); |
| 702 v8::Local<v8::Object> creationContext = m_reader.getScriptState()->context()
->Global(); |
| 703 result = toV8(offscreenCanvas, creationContext, isolate); |
| 704 if (result.IsEmpty()) { |
| 705 return false; |
| 706 } |
| 707 m_offscreenCanvases.append(result); |
| 708 *object = result; |
| 709 return true; |
| 710 } |
| 711 |
631 bool ScriptValueDeserializerForModules::read(v8::Local<v8::Value>* value) | 712 bool ScriptValueDeserializerForModules::read(v8::Local<v8::Value>* value) |
632 { | 713 { |
633 return toSerializedScriptValueReaderForModules(reader()).read(value, *this); | 714 return toSerializedScriptValueReaderForModules(reader()).read(value, *this); |
634 } | 715 } |
635 | 716 |
636 } // namespace blink | 717 } // namespace blink |
OLD | NEW |