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/IDBValueWrapper.h" | |
6 | |
7 #include "bindings/core/v8/ScriptValue.h" | |
8 #include "bindings/core/v8/SerializationTag.h" | |
9 #include "bindings/core/v8/SerializedScriptValue.h" | |
10 #include "bindings/modules/v8/V8BindingForModules.h" | |
11 #include "core/fileapi/Blob.h" | |
12 #include "modules/indexeddb/IDBRequest.h" | |
13 #include "platform/wtf/text/WTFString.h" | |
14 | |
15 namespace blink { | |
16 | |
17 IDBValueWrapper::IDBValueWrapper(v8::Isolate* isolate, | |
18 v8::Local<v8::Value> value, | |
19 bool write_wasm_to_stream, | |
20 ExceptionState& exception_state) { | |
21 SerializedScriptValue::SerializeOptions options; | |
22 options.blob_info = &blob_info_; | |
23 options.write_wasm_to_stream = write_wasm_to_stream; | |
24 | |
25 serialized_value_ = SerializedScriptValue::Serialize(isolate, value, options, | |
26 exception_state); | |
27 #if DCHECK_IS_ON() | |
28 if (exception_state.HadException()) | |
29 had_exception_ = true; | |
30 #endif // DCHECK_IS_ON() | |
31 } | |
32 | |
33 void IDBValueWrapper::Clone(ScriptState* script_state, ScriptValue* clone) { | |
34 DCHECK(!had_exception_) | |
35 << "Clone() called on wrapper with serialization exception"; | |
36 DCHECK(!wrap_called_) << "Clone() called after WrapIfBiggerThan()"; | |
37 *clone = DeserializeScriptValue(script_state, serialized_value_.Get(), | |
38 &blob_info_); | |
39 } | |
40 | |
41 void IDBValueWrapper::WriteVarint(unsigned value, Vector<char>& output) { | |
42 // Writes an unsigned integer as a base-128 varint. | |
43 // The number is written, 7 bits at a time, from the least significant to | |
44 // the most significant 7 bits. Each byte, except the last, has the MSB set. | |
45 // See also https://developers.google.com/protocol-buffers/docs/encoding | |
46 do { | |
47 output.push_back((value & 0x7F) | 0x80); | |
48 value >>= 7; | |
49 } while (value); | |
50 output.back() &= 0x7F; | |
51 } | |
52 | |
53 void IDBValueWrapper::WriteAsciiString(const String& value, | |
54 Vector<char>& output) { | |
55 DCHECK(value.Is8Bit() && value.ContainsOnlyASCII()); | |
56 | |
57 IDBValueWrapper::WriteVarint(value.length(), output); | |
58 output.Append(value.Characters8(), value.length()); | |
59 } | |
60 | |
61 bool IDBValueWrapper::WrapIfBiggerThan(unsigned max_bytes) { | |
62 DCHECK(!had_exception_) | |
63 << "WrapIfBiggerThan() called on wrapper with serialization exception"; | |
64 DCHECK(!wrap_called_) | |
65 << "WrapIfBiggerThan() called twice on the same wrapper"; | |
66 | |
67 #if DCHECK_IS_ON() | |
68 wrap_called_ = true; | |
69 #endif // DCHECK_IS_ON() | |
70 | |
71 serialized_value_->ToWireBytes(wire_bytes_); | |
72 if (wire_bytes_.size() <= max_bytes) | |
73 return false; | |
74 | |
75 // TODO(pwnall): The MIME type should probably be an atomic string. | |
76 String mime_type(kWrapMimeType); | |
77 Blob* wrapper = | |
dmurph
2017/04/13 18:05:00
Ok so this is how we'll deal with the lifetime. Th
pwnall
2017/05/11 23:54:23
You implemented a solution for keeping Blobs alive
| |
78 Blob::Create(reinterpret_cast<unsigned char*>(wire_bytes_.Data()), | |
79 wire_bytes_.size(), mime_type); | |
80 | |
81 // TODO(pwnall): Do we need to stash the blob somewhere else to keep it alive? | |
82 blob_info_.emplace_back(wrapper->Uuid(), wrapper->type(), wrapper->size()); | |
83 | |
84 wire_bytes_.Clear(); | |
85 | |
86 // Version 17 of SSV always writes a V8 envelope after the Blink envelope, so | |
87 // its output starts with 0xFF 0x11 0xFF. Therefore, we can use 0xFF 0x11 0xvv | |
88 // as an escape prefix, for 0x00 <= 0xvv < 0xFF. | |
89 wire_bytes_.push_back(kVersionTag); | |
90 wire_bytes_.push_back(17); | |
91 wire_bytes_.push_back(1); | |
92 IDBValueWrapper::WriteVarint(wrapper->size(), wire_bytes_); | |
93 IDBValueWrapper::WriteAsciiString(wrapper->Uuid(), wire_bytes_); | |
94 return true; | |
95 } | |
96 | |
97 PassRefPtr<SharedBuffer> IDBValueWrapper::ExtractWireBytes() { | |
98 DCHECK(!had_exception_) | |
99 << "ExtractWireBytes() called on wrapper with serialization exception"; | |
100 | |
101 return SharedBuffer::AdoptVector(wire_bytes_); | |
102 } | |
103 | |
104 } // namespace blink | |
OLD | NEW |