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 IDBRequestQueueItem_h |
| 6 #define IDBRequestQueueItem_h |
| 7 |
| 8 #include "platform/heap/Handle.h" |
| 9 #include "platform/wtf/Allocator.h" |
| 10 #include "platform/wtf/PassRefPtr.h" |
| 11 #include "platform/wtf/RefPtr.h" |
| 12 #include "platform/wtf/Vector.h" |
| 13 |
| 14 #include <memory> |
| 15 |
| 16 namespace blink { |
| 17 |
| 18 class DOMException; |
| 19 class IDBKey; |
| 20 class IDBRequest; |
| 21 class IDBRequestLoader; |
| 22 class IDBValue; |
| 23 class WebIDBCursor; |
| 24 |
| 25 // Queues up a transaction's IDBRequest results for orderly delivery. |
| 26 // |
| 27 // The IndexedDB specification requires that the events corresponding to IDB |
| 28 // request results fire in the order in which the requests were issued. The |
| 29 // browser-side backend processes requests in order, but the Blink side may need |
| 30 // to perform post-processing on the results (e.g. large value unwrapping). |
| 31 // When a result needs post-processing, this queue captures all results received |
| 32 // during the post-processing steps. The events for these results may only fire |
| 33 // after the post-processed result's event is fired. |
| 34 // |
| 35 // A queue item holds a Persistent (not garbage-collected) reference to the |
| 36 // IDBRequest whose result it will deliver. This creates a reference cycle, |
| 37 // because IDBRequest holds a pointer to its IDBTransaction, and IDBTransaction |
| 38 // stores all pending IDBRequestQueueItem instances for its requests in a queue |
| 39 // via non-garbage-collected pointers (std::unique_ptr). To avoid leaks, the |
| 40 // request-processing code must ensure that IDBTransaction's queue gets drained |
| 41 // at some point, even if the transaction's ExecutionContext goes away. The |
| 42 // lifecycle tests in IDBTransactionTest aim to cover this requirement. |
| 43 // |
| 44 // Given that the cycle above exists, the closures passed to IDBRequestQueueItem |
| 45 // can safely store Persistent pointers the IDBRequest or to the IDBTransaction. |
| 46 class IDBRequestQueueItem { |
| 47 USING_FAST_MALLOC(IDBRequestQueueItem); |
| 48 |
| 49 public: |
| 50 IDBRequestQueueItem(IDBRequest*, |
| 51 DOMException*, |
| 52 std::unique_ptr<WTF::Closure> on_result_load_complete); |
| 53 IDBRequestQueueItem(IDBRequest*, |
| 54 int64_t, |
| 55 std::unique_ptr<WTF::Closure> on_result_load_complete); |
| 56 IDBRequestQueueItem(IDBRequest*, |
| 57 std::unique_ptr<WTF::Closure> on_result_load_complete); |
| 58 IDBRequestQueueItem(IDBRequest*, |
| 59 IDBKey*, |
| 60 std::unique_ptr<WTF::Closure> on_result_load_complete); |
| 61 IDBRequestQueueItem(IDBRequest*, |
| 62 PassRefPtr<IDBValue>, |
| 63 bool attach_loader, |
| 64 std::unique_ptr<WTF::Closure> on_load_complete); |
| 65 IDBRequestQueueItem(IDBRequest*, |
| 66 const Vector<RefPtr<IDBValue>>&, |
| 67 bool attach_loader, |
| 68 std::unique_ptr<WTF::Closure> on_result_load_complete); |
| 69 IDBRequestQueueItem(IDBRequest*, |
| 70 IDBKey*, |
| 71 IDBKey* primary_key, |
| 72 PassRefPtr<IDBValue>, |
| 73 bool attach_loader, |
| 74 std::unique_ptr<WTF::Closure> on_result_load_complete); |
| 75 IDBRequestQueueItem(IDBRequest*, |
| 76 std::unique_ptr<WebIDBCursor>, |
| 77 IDBKey*, |
| 78 IDBKey* primary_key, |
| 79 PassRefPtr<IDBValue>, |
| 80 bool attach_loader, |
| 81 std::unique_ptr<WTF::Closure> on_result_load_complete); |
| 82 ~IDBRequestQueueItem(); |
| 83 |
| 84 // False if this result still requires post-processing. |
| 85 inline bool IsReady() { return ready_; } |
| 86 |
| 87 // The request whose queued result is tracked by this item. |
| 88 inline IDBRequest* Request() { return request_; } |
| 89 |
| 90 // Starts post-processing the IDBRequest's result. |
| 91 // |
| 92 // This method must be called after the IDBRequestQueueItem is enqueued into |
| 93 // the appropriate queue, because it is possible for the loading operation to |
| 94 // complete synchronously, in which case IDBTransaction::OnResultReady() will |
| 95 // be called with the (presumably) enqueued IDBRequest before this method |
| 96 // returns. |
| 97 void StartLoading(); |
| 98 |
| 99 // Stops post-processing the IDBRequest's result. |
| 100 // |
| 101 // This method may be called without an associated StartLoading(). |
| 102 void CancelLoading(); |
| 103 |
| 104 // Calls the correct EnqueueResponse overload on the associated request. |
| 105 // |
| 106 // This should only be called by the request's IDBTransaction. |
| 107 void EnqueueResponse(); |
| 108 |
| 109 // Called by the associated IDBRequestLoader when result processing is done. |
| 110 void OnResultLoadComplete(); |
| 111 |
| 112 // Called by the associated IDBRequestLoader when result processing fails. |
| 113 void OnResultLoadComplete(DOMException* error); |
| 114 |
| 115 private: |
| 116 // The IDBRequest callback that will be called for this result. |
| 117 enum ResponseType { |
| 118 kCanceled, |
| 119 kCursorKeyPrimaryKeyValue, |
| 120 kError, |
| 121 kNumber, |
| 122 kKey, |
| 123 kKeyPrimaryKeyValue, |
| 124 kValue, |
| 125 kValueArray, |
| 126 kVoid, |
| 127 }; |
| 128 |
| 129 // The IDBRequest that will receive a callback for this result. |
| 130 Persistent<IDBRequest> request_; |
| 131 |
| 132 // The key argument to the IDBRequest callback. |
| 133 // |
| 134 // Only used if mode_ is kKeyPrimaryKeyValue. |
| 135 Persistent<IDBKey> key_; |
| 136 |
| 137 // The primary_key argument to the IDBRequest callback. |
| 138 // |
| 139 // Only used if mode_ is kKeyPrimaryKeyValue. |
| 140 Persistent<IDBKey> primary_key_; |
| 141 |
| 142 // The error argument to the IDBRequest callback. |
| 143 // |
| 144 // Only used if the mode_ is kError. |
| 145 Persistent<DOMException> error_; |
| 146 |
| 147 // All the values that will be passed back to the IDBRequest. |
| 148 Vector<RefPtr<IDBValue>> values_; |
| 149 |
| 150 // The cursor argument to the IDBRequest callback. |
| 151 std::unique_ptr<WebIDBCursor> cursor_; |
| 152 |
| 153 // Performs post-processing on this result. |
| 154 // |
| 155 // nullptr for results that do not require post-processing and for results |
| 156 // whose post-processing has completed. |
| 157 std::unique_ptr<IDBRequestLoader> loader_; |
| 158 |
| 159 // Called when result post-processing has completed. |
| 160 std::unique_ptr<WTF::Closure> on_result_load_complete_; |
| 161 |
| 162 // The integer value argument to the IDBRequest callback. |
| 163 int64_t int64_value_; |
| 164 |
| 165 // Identifies the IDBRequest::EnqueueResponse() overload that will be called. |
| 166 ResponseType response_type_; |
| 167 |
| 168 // False if this result still requires post-processing. |
| 169 bool ready_; |
| 170 |
| 171 #if DCHECK_IS_ON() |
| 172 // True if the appropriate EnqueueResponse() method was called in IDBRequest. |
| 173 // |
| 174 // If CancelLoading() is called, the ResponseType might be kCanceled. In this |
| 175 // case, callback_fired_ can be set to true even though no EnqueueResponse() |
| 176 // call occurs. |
| 177 bool callback_fired_ = false; |
| 178 #endif // DCHECK_IS_ON() |
| 179 }; |
| 180 |
| 181 using IDBRequestQueue = Deque<std::unique_ptr<IDBRequestQueueItem>>; |
| 182 |
| 183 } // namespace blink |
| 184 |
| 185 #endif // IDBRequestQueueItem_h |
OLD | NEW |