OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "modules/indexeddb/IDBValueWrapping.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "bindings/core/v8/ScriptValue.h" | |
10 #include "bindings/core/v8/serialization/SerializationTag.h" | |
11 #include "bindings/modules/v8/V8BindingForModules.h" | |
12 #include "core/fileapi/Blob.h" | |
13 #include "modules/indexeddb/IDBRequest.h" | |
14 #include "modules/indexeddb/IDBValue.h" | |
15 #include "platform/blob/BlobData.h" | |
16 #include "platform/wtf/text/WTFString.h" | |
17 | |
18 namespace blink { | |
19 | |
20 namespace { | |
21 | |
22 // V8 values are stored on disk by IndexedDB using the format implemented in | |
jsbell
2017/05/22 21:54:20
Docs should probably mention that the blob is push
pwnall
2017/05/25 13:27:12
I tried to add the details to the documentation in
| |
23 // SerializedScriptValue (SSV). The wrapping detection logic in | |
24 // IDBValueUnwrapper::IsWrapped() must be able to distinguish between SSV byte | |
25 // sequences produced and byte sequences expressing the fact that an IDBValue | |
26 // has been wrapped and requires post-processing. | |
27 // | |
28 // The detection logic takes advantage of the highly regular structure around | |
29 // SerializedScriptValue. A version 17 byte sequence always starts with the | |
30 // following four bytes: | |
31 // | |
32 // 1) 0xFF - version tag | |
33 // 2) 0x11 - Blink wrapper version, 17 | |
34 // 3) 0xFF - version tag | |
35 // 4) 0x0D - V8 serialization version, currently 13, doesn't matter | |
36 // | |
37 // It follows that SSV will never produce byte sequences starting with 0xFF, | |
38 // 0x11, and any value except for 0xFF. If the SSV format changes, the version | |
39 // will have to be bumped. | |
jbroman
2017/05/23 20:13:11
Yes, I believe this is true. It saddens me somewha
pwnall
2017/05/25 13:27:12
I'm not sure I understand your proposal.
Did you
jbroman
2017/05/25 14:50:58
Yeah, okay. You're right that it's slightly more c
| |
40 | |
41 // The SSV format version whose encoding hole is (ab)used for wrapping. | |
42 const static uint8_t kRequiresProcessingSSVPseudoVersion = 17; | |
43 | |
44 // Identifies IndexedDB values that were wrapped in Blobs. The wrapper has the | |
jsbell
2017/05/22 21:54:20
Can you explicitly write out the wrapped blob form
pwnall
2017/05/25 13:27:12
Done.
Ah, good call. That was really unclear. Than
| |
45 // following format: | |
46 // | |
47 // 1) varint - Blob size | |
48 // 2) varint - the offset of the SSV-wrapping Blob in the IDBValue list of Blobs | |
49 // (should always be the last Blob) | |
50 const static uint8_t kBlobWrappedValue = 1; | |
51 | |
52 } // namespace | |
53 | |
54 IDBValueWrapper::IDBValueWrapper( | |
55 v8::Isolate* isolate, | |
56 v8::Local<v8::Value> value, | |
57 SerializedScriptValue::SerializeOptions::WasmSerializationPolicy | |
58 wasm_policy, | |
59 ExceptionState& exception_state) { | |
60 SerializedScriptValue::SerializeOptions options; | |
61 options.blob_info = &blob_info_; | |
62 options.for_storage = SerializedScriptValue::kForStorage; | |
63 options.wasm_policy = wasm_policy; | |
64 | |
65 serialized_value_ = SerializedScriptValue::Serialize(isolate, value, options, | |
66 exception_state); | |
67 | |
68 #if DCHECK_IS_ON() | |
69 if (exception_state.HadException()) | |
70 had_exception_ = true; | |
71 #endif // DCHECK_IS_ON() | |
72 } | |
73 | |
74 void IDBValueWrapper::Clone(ScriptState* script_state, ScriptValue* clone) { | |
75 #if DCHECK_IS_ON() | |
76 DCHECK(!had_exception_) << __FUNCTION__ | |
77 << " called on wrapper with serialization exception"; | |
78 DCHECK(!wrap_called_) << "Clone() called after WrapIfBiggerThan()"; | |
79 #endif // DCHECK_IS_ON() | |
80 *clone = DeserializeScriptValue(script_state, serialized_value_.Get(), | |
81 &blob_info_); | |
82 } | |
83 | |
84 void IDBValueWrapper::WriteVarint(unsigned value, Vector<char>& output) { | |
85 // Writes an unsigned integer as a base-128 varint. | |
86 // The number is written, 7 bits at a time, from the least significant to | |
87 // the most significant 7 bits. Each byte, except the last, has the MSB set. | |
88 // See also https://developers.google.com/protocol-buffers/docs/encoding | |
89 do { | |
90 output.push_back((value & 0x7F) | 0x80); | |
91 value >>= 7; | |
92 } while (value); | |
93 output.back() &= 0x7F; | |
94 } | |
95 | |
96 bool IDBValueWrapper::WrapIfBiggerThan(unsigned max_bytes) { | |
97 #if DCHECK_IS_ON() | |
98 DCHECK(!had_exception_) << __FUNCTION__ | |
99 << " called on wrapper with serialization exception"; | |
100 DCHECK(!wrap_called_) << __FUNCTION__ << " called twice on the same wrapper"; | |
101 wrap_called_ = true; | |
102 #endif // DCHECK_IS_ON() | |
103 | |
104 serialized_value_->ToWireBytes(wire_bytes_); | |
105 if (wire_bytes_.size() <= max_bytes) | |
106 return false; | |
107 | |
108 // TODO(pwnall): The MIME type should probably be an atomic string. | |
109 String mime_type(kWrapMimeType); | |
110 // TODO(crbug.com/721516): Use WebBlobRegistry::CreateBuilder instead of | |
111 // Blob::Create to avoid a buffer copy. | |
112 Blob* wrapper = | |
113 Blob::Create(reinterpret_cast<unsigned char*>(wire_bytes_.data()), | |
114 wire_bytes_.size(), mime_type); | |
115 | |
116 wrapper_handle_ = wrapper->GetBlobDataHandle(); | |
117 blob_info_.emplace_back(wrapper_handle_->Uuid(), wrapper_handle_->GetType(), | |
118 wrapper->size()); | |
119 | |
120 wire_bytes_.clear(); | |
121 | |
122 wire_bytes_.push_back(kVersionTag); | |
123 wire_bytes_.push_back(kRequiresProcessingSSVPseudoVersion); | |
124 wire_bytes_.push_back(kBlobWrappedValue); | |
125 IDBValueWrapper::WriteVarint(wrapper->size(), wire_bytes_); | |
126 IDBValueWrapper::WriteVarint(serialized_value_->BlobDataHandles().size(), | |
127 wire_bytes_); | |
128 return true; | |
129 } | |
130 | |
131 void IDBValueWrapper::ExtractBlobDataHandles( | |
132 Vector<RefPtr<BlobDataHandle>>* blob_data_handles) { | |
133 for (const auto& kvp : serialized_value_->BlobDataHandles()) | |
134 blob_data_handles->push_back(kvp.value); | |
135 if (wrapper_handle_) | |
136 blob_data_handles->push_back(std::move(wrapper_handle_)); | |
137 } | |
138 | |
139 RefPtr<SharedBuffer> IDBValueWrapper::ExtractWireBytes() { | |
140 #if DCHECK_IS_ON() | |
141 DCHECK(!had_exception_) << __FUNCTION__ | |
142 << " called on wrapper with serialization exception"; | |
143 #endif // DCHECK_IS_ON() | |
144 | |
145 return SharedBuffer::AdoptVector(wire_bytes_); | |
146 } | |
147 | |
148 IDBValueUnwrapper::IDBValueUnwrapper() { | |
149 Reset(); | |
150 } | |
151 | |
152 bool IDBValueUnwrapper::IsWrapped(IDBValue* value) { | |
jsbell
2017/05/22 21:54:20
Verify that blob_info size is >= 1 ?
pwnall
2017/05/25 13:27:12
Done.
I added a check IDBValueUnwrapper::Parse(),
| |
153 DCHECK(value); | |
154 | |
155 uint8_t header[3]; | |
156 if (!value->data_ || value->data_->size() < sizeof(header)) | |
157 return false; | |
158 | |
159 value->data_->GetPartAsBytes(header, static_cast<size_t>(0), sizeof(header)); | |
160 return header[0] == kVersionTag && | |
161 header[1] == kRequiresProcessingSSVPseudoVersion && | |
162 header[2] == kBlobWrappedValue; | |
163 } | |
164 | |
165 bool IDBValueUnwrapper::IsWrapped(const Vector<RefPtr<IDBValue>>& values) { | |
166 for (const auto& value : values) { | |
167 if (IsWrapped(value.Get())) | |
168 return true; | |
169 } | |
170 return false; | |
171 } | |
172 | |
173 RefPtr<IDBValue> IDBValueUnwrapper::CreateUnwrapped( | |
174 IDBValue* wrapped_value, | |
175 RefPtr<SharedBuffer>&& wrapper_blob_content) { | |
176 DCHECK(wrapped_value); | |
177 DCHECK(wrapped_value->data_); | |
178 DCHECK_GT(wrapped_value->blob_info_->size(), 0U); | |
179 DCHECK_EQ(wrapped_value->blob_info_->size(), | |
180 wrapped_value->blob_data_->size()); | |
181 | |
182 // Create an IDBValue with the same blob information, minus the last blob. | |
183 unsigned blob_count = wrapped_value->BlobInfo()->size() - 1; | |
184 std::unique_ptr<Vector<RefPtr<BlobDataHandle>>> blob_data = | |
185 WTF::MakeUnique<Vector<RefPtr<BlobDataHandle>>>(); | |
186 blob_data->ReserveCapacity(blob_count); | |
187 std::unique_ptr<Vector<WebBlobInfo>> blob_info = | |
188 WTF::MakeUnique<Vector<WebBlobInfo>>(); | |
189 blob_info->ReserveCapacity(blob_count); | |
190 | |
191 for (unsigned i = 0; i < blob_count; ++i) { | |
192 blob_data->push_back((*wrapped_value->blob_data_)[i]); | |
193 blob_info->push_back((*wrapped_value->blob_info_)[i]); | |
194 } | |
195 | |
196 return AdoptRef(new IDBValue(std::move(wrapper_blob_content), | |
197 std::move(blob_data), std::move(blob_info))); | |
198 } | |
199 | |
200 bool IDBValueUnwrapper::Parse(IDBValue* value) { | |
201 // Fast path that avoids unnecessary dynamic allocations. | |
202 if (!IDBValueUnwrapper::IsWrapped(value)) | |
203 return false; | |
204 | |
205 const uint8_t* data = reinterpret_cast<const uint8_t*>(value->data_->Data()); | |
206 end_ = data + value->data_->size(); | |
207 current_ = data + 3; | |
208 | |
209 if (!ReadVarint(blob_size_)) | |
210 return Reset(); | |
211 | |
212 unsigned blob_offset; | |
213 if (!ReadVarint(blob_offset)) | |
214 return Reset(); | |
215 if (blob_offset != value->blob_data_->size() - 1) | |
216 return Reset(); | |
217 | |
218 blob_handle_ = value->blob_data_->back(); | |
219 if (blob_handle_->size() != blob_size_) | |
220 return Reset(); | |
221 | |
222 return true; | |
223 } | |
224 | |
225 RefPtr<BlobDataHandle> IDBValueUnwrapper::WrapperBlobHandle() { | |
226 DCHECK(blob_handle_); | |
227 | |
228 return std::move(blob_handle_); | |
229 } | |
230 | |
231 bool IDBValueUnwrapper::ReadVarint(unsigned& value) { | |
232 value = 0; | |
233 unsigned shift = 0; | |
234 bool has_another_byte; | |
235 do { | |
236 if (current_ >= end_) | |
237 return false; | |
238 | |
239 if (shift >= sizeof(unsigned) * 8) | |
240 return false; | |
241 uint8_t byte = *current_; | |
242 ++current_; | |
243 value |= static_cast<unsigned>(byte & 0x7F) << shift; | |
244 shift += 7; | |
245 | |
246 has_another_byte = byte & 0x80; | |
247 } while (has_another_byte); | |
248 return true; | |
249 } | |
250 | |
251 } // namespace blink | |
OLD | NEW |