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