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 #ifndef IDBValueWrapper_h |
| 6 #define IDBValueWrapper_h |
| 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "platform/SharedBuffer.h" |
| 10 #include "platform/wtf/PassRefPtr.h" |
| 11 #include "platform/wtf/RefPtr.h" |
| 12 #include "platform/wtf/Vector.h" |
| 13 #include "public/platform/WebBlobInfo.h" |
| 14 #include "v8/include/v8.h" |
| 15 |
| 16 namespace blink { |
| 17 |
| 18 class Blob; |
| 19 class ExceptionState; |
| 20 class ScriptState; |
| 21 class ScriptValue; |
| 22 class SerializedScriptValue; |
| 23 class SharedBuffer; |
| 24 |
| 25 // Logic for wrapping large IndexedDB values in Blobs. |
| 26 class IDBValueWrapper { |
| 27 public: |
| 28 // Wrapper for an IndexedDB value. |
| 29 // |
| 30 // The serialization process can throw an exception. The caller is responsible |
| 31 // for checking exception_state. |
| 32 IDBValueWrapper(v8::Isolate*, |
| 33 v8::Local<v8::Value>, |
| 34 bool write_wasm_to_stream, |
| 35 ExceptionState&); |
| 36 |
| 37 // Creates a clone of the serialized value. |
| 38 // |
| 39 // This method is necessary because the IndexedDB specification demands that a |
| 40 // value's key and index keys are extracted from a structured clone of the |
| 41 // value. |
| 42 // |
| 43 // This method cannot be called after WrapIfBiggerThan(). |
| 44 void Clone(ScriptState*, ScriptValue* clone); |
| 45 |
| 46 // Wraps the serialized value into a Blob if it exceeds the given size. |
| 47 // |
| 48 // In production, the threshold is currently always kWrapThreshold. |
| 49 // |
| 50 // This method must be called before ExtractWireBytes() and cannot be called |
| 51 // after ExtractWireBytes(). |
| 52 bool WrapIfBiggerThan(unsigned max_bytes); |
| 53 |
| 54 PassRefPtr<SharedBuffer> ExtractWireBytes(); |
| 55 |
| 56 inline Vector<WebBlobInfo>& WrappedBlobInfo() { |
| 57 DCHECK(!had_exception_) |
| 58 << "WrapBlobInfo() called on wrapper with serialization exception"; |
| 59 return blob_info_; |
| 60 } |
| 61 |
| 62 // Default threshold for WrapIfBiggerThan(). |
| 63 // |
| 64 // This should be tuned to achieve a compromise between short-term IndexedDB |
| 65 // throughput and long-term I/O load and memory usage. LevelDB, the underlying |
| 66 // storage for IndexedDB, was not designed with large values in mind. At the |
| 67 // very least, large values will slow down compaction, causing occasional I/O |
| 68 // spikes. |
| 69 static constexpr unsigned kWrapThreshold = 64 * 1024 * 1024; |
| 70 |
| 71 // MIME type used for Blobs that wrap IDBValues. |
| 72 static constexpr const char* kWrapMimeType = |
| 73 "application/x-blink-idb-value-wrapper"; |
| 74 |
| 75 private: |
| 76 // Used to serialize the wrapped value. |
| 77 static void WriteVarint(unsigned value, Vector<char>& output); |
| 78 static void WriteAsciiString(const String& value, Vector<char>& output); |
| 79 |
| 80 RefPtr<SerializedScriptValue> serialized_value_; |
| 81 Vector<WebBlobInfo> blob_info_; |
| 82 Vector<char> wire_bytes_; |
| 83 #if DCHECK_IS_ON() |
| 84 bool had_exception_ = false; |
| 85 bool wrap_called_ = false; |
| 86 #endif // DCHECK_IS_ON() |
| 87 }; |
| 88 |
| 89 } // namespace blink |
| 90 |
| 91 #endif // IDBValueWrapper_h |
OLD | NEW |