1 | /* |
2 | * Copyright (C) 2009, 2013, 2016 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | * |
25 | */ |
26 | |
27 | #pragma once |
28 | |
29 | #include "ExceptionOr.h" |
30 | #include "ImageBuffer.h" |
31 | #include <JavaScriptCore/ArrayBuffer.h> |
32 | #include <JavaScriptCore/JSCJSValue.h> |
33 | #include <JavaScriptCore/Strong.h> |
34 | #include <pal/SessionID.h> |
35 | #include <wtf/Forward.h> |
36 | #include <wtf/Function.h> |
37 | #include <wtf/Gigacage.h> |
38 | #include <wtf/text/WTFString.h> |
39 | |
40 | typedef const struct OpaqueJSContext* JSContextRef; |
41 | typedef const struct OpaqueJSValue* JSValueRef; |
42 | |
43 | #if ENABLE(WEBASSEMBLY) |
44 | namespace JSC { namespace Wasm { |
45 | class Module; |
46 | } } |
47 | #endif |
48 | |
49 | namespace WebCore { |
50 | |
51 | class IDBValue; |
52 | class ImageBitmap; |
53 | class MessagePort; |
54 | class SharedBuffer; |
55 | enum class SerializationReturnCode; |
56 | |
57 | enum class SerializationErrorMode { NonThrowing, Throwing }; |
58 | enum class SerializationContext { Default, WorkerPostMessage, WindowPostMessage }; |
59 | |
60 | using ArrayBufferContentsArray = Vector<JSC::ArrayBufferContents>; |
61 | #if ENABLE(WEBASSEMBLY) |
62 | using WasmModuleArray = Vector<RefPtr<JSC::Wasm::Module>>; |
63 | #endif |
64 | |
65 | class SerializedScriptValue : public ThreadSafeRefCounted<SerializedScriptValue> { |
66 | public: |
67 | WEBCORE_EXPORT static RefPtr<SerializedScriptValue> create(JSC::ExecState&, JSC::JSValue, SerializationErrorMode = SerializationErrorMode::Throwing); |
68 | |
69 | WEBCORE_EXPORT static ExceptionOr<Ref<SerializedScriptValue>> create(JSC::ExecState&, JSC::JSValue, Vector<JSC::Strong<JSC::JSObject>>&& transfer, Vector<RefPtr<MessagePort>>&, SerializationContext = SerializationContext::Default); |
70 | |
71 | WEBCORE_EXPORT static RefPtr<SerializedScriptValue> create(StringView); |
72 | static Ref<SerializedScriptValue> adopt(Vector<uint8_t>&& buffer) |
73 | { |
74 | return adoptRef(*new SerializedScriptValue(WTFMove(buffer))); |
75 | } |
76 | |
77 | static Ref<SerializedScriptValue> nullValue(); |
78 | |
79 | WEBCORE_EXPORT JSC::JSValue deserialize(JSC::ExecState&, JSC::JSGlobalObject*, SerializationErrorMode = SerializationErrorMode::Throwing); |
80 | WEBCORE_EXPORT JSC::JSValue deserialize(JSC::ExecState&, JSC::JSGlobalObject*, const Vector<RefPtr<MessagePort>>&, SerializationErrorMode = SerializationErrorMode::Throwing); |
81 | JSC::JSValue deserialize(JSC::ExecState&, JSC::JSGlobalObject*, const Vector<RefPtr<MessagePort>>&, const Vector<String>& blobURLs, const PAL::SessionID&, const Vector<String>& blobFilePaths, SerializationErrorMode = SerializationErrorMode::Throwing); |
82 | |
83 | static uint32_t wireFormatVersion(); |
84 | |
85 | String toString(); |
86 | |
87 | // API implementation helpers. These don't expose special behavior for ArrayBuffers or MessagePorts. |
88 | WEBCORE_EXPORT static RefPtr<SerializedScriptValue> create(JSContextRef, JSValueRef, JSValueRef* exception); |
89 | WEBCORE_EXPORT JSValueRef deserialize(JSContextRef, JSValueRef* exception); |
90 | |
91 | const Vector<uint8_t>& data() const { return m_data; } |
92 | bool hasBlobURLs() const { return !m_blobURLs.isEmpty(); } |
93 | |
94 | #if ENABLE(INDEXED_DATABASE) |
95 | Vector<String> blobURLsIsolatedCopy() const; |
96 | const PAL::SessionID& sessionID() const { return m_sessionID; } |
97 | void writeBlobsToDiskForIndexedDB(CompletionHandler<void(IDBValue&&)>&&); |
98 | IDBValue writeBlobsToDiskForIndexedDBSynchronously(); |
99 | #endif // ENABLE(INDEXED_DATABASE) |
100 | |
101 | static Ref<SerializedScriptValue> createFromWireBytes(Vector<uint8_t>&& data) |
102 | { |
103 | return adoptRef(*new SerializedScriptValue(WTFMove(data))); |
104 | } |
105 | const Vector<uint8_t>& toWireBytes() const { return m_data; } |
106 | |
107 | template<class Encoder> void encode(Encoder&) const; |
108 | template<class Decoder> static RefPtr<SerializedScriptValue> decode(Decoder&); |
109 | |
110 | WEBCORE_EXPORT ~SerializedScriptValue(); |
111 | |
112 | private: |
113 | WEBCORE_EXPORT SerializedScriptValue(Vector<unsigned char>&&); |
114 | WEBCORE_EXPORT SerializedScriptValue(Vector<unsigned char>&&, std::unique_ptr<ArrayBufferContentsArray>); |
115 | SerializedScriptValue(Vector<unsigned char>&&, const Vector<String>& blobURLs, const PAL::SessionID&, std::unique_ptr<ArrayBufferContentsArray>, std::unique_ptr<ArrayBufferContentsArray> sharedBuffers, Vector<std::pair<std::unique_ptr<ImageBuffer>, bool>>&& imageBuffers |
116 | #if ENABLE(WEBASSEMBLY) |
117 | , std::unique_ptr<WasmModuleArray> |
118 | #endif |
119 | ); |
120 | |
121 | Vector<unsigned char> m_data; |
122 | std::unique_ptr<ArrayBufferContentsArray> m_arrayBufferContentsArray; |
123 | std::unique_ptr<ArrayBufferContentsArray> m_sharedBufferContentsArray; |
124 | Vector<std::pair<std::unique_ptr<ImageBuffer>, bool>> m_imageBuffers; |
125 | #if ENABLE(WEBASSEMBLY) |
126 | std::unique_ptr<WasmModuleArray> m_wasmModulesArray; |
127 | #endif |
128 | Vector<String> m_blobURLs; |
129 | PAL::SessionID m_sessionID; |
130 | }; |
131 | |
132 | template<class Encoder> |
133 | void SerializedScriptValue::encode(Encoder& encoder) const |
134 | { |
135 | encoder << m_data; |
136 | |
137 | auto hasArray = m_arrayBufferContentsArray && m_arrayBufferContentsArray->size(); |
138 | encoder << hasArray; |
139 | |
140 | if (!hasArray) |
141 | return; |
142 | |
143 | encoder << static_cast<uint64_t>(m_arrayBufferContentsArray->size()); |
144 | for (const auto &arrayBufferContents : *m_arrayBufferContentsArray) { |
145 | encoder << arrayBufferContents.sizeInBytes(); |
146 | encoder.encodeFixedLengthData(static_cast<const uint8_t*>(arrayBufferContents.data()), arrayBufferContents.sizeInBytes(), 1); |
147 | } |
148 | } |
149 | |
150 | template<class Decoder> |
151 | RefPtr<SerializedScriptValue> SerializedScriptValue::decode(Decoder& decoder) |
152 | { |
153 | Vector<uint8_t> data; |
154 | if (!decoder.decode(data)) |
155 | return nullptr; |
156 | |
157 | bool hasArray; |
158 | if (!decoder.decode(hasArray)) |
159 | return nullptr; |
160 | |
161 | if (!hasArray) |
162 | return adoptRef(*new SerializedScriptValue(WTFMove(data))); |
163 | |
164 | uint64_t arrayLength; |
165 | if (!decoder.decode(arrayLength)) |
166 | return nullptr; |
167 | ASSERT(arrayLength); |
168 | |
169 | auto arrayBufferContentsArray = std::make_unique<ArrayBufferContentsArray>(); |
170 | while (arrayLength--) { |
171 | unsigned bufferSize; |
172 | if (!decoder.decode(bufferSize)) |
173 | return nullptr; |
174 | |
175 | auto buffer = Gigacage::tryMalloc(Gigacage::Primitive, bufferSize); |
176 | auto destructor = [] (void* ptr) { |
177 | Gigacage::free(Gigacage::Primitive, ptr); |
178 | }; |
179 | if (!decoder.decodeFixedLengthData(static_cast<uint8_t*>(buffer), bufferSize, 1)) { |
180 | destructor(buffer); |
181 | return nullptr; |
182 | } |
183 | arrayBufferContentsArray->append({ buffer, bufferSize, WTFMove(destructor) }); |
184 | } |
185 | |
186 | return adoptRef(*new SerializedScriptValue(WTFMove(data), WTFMove(arrayBufferContentsArray))); |
187 | } |
188 | |
189 | |
190 | } |
191 | |