Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: content/browser/indexed_db/indexed_db_transaction.h

Issue 2472213003: [IndexedDB] Refactoring to remove ref ptrs and host transaction ids. (Closed)
Patch Set: comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ 5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <queue> 11 #include <queue>
12 #include <set> 12 #include <set>
13 #include <stack> 13 #include <stack>
14 14
15 #include "base/gtest_prod_util.h" 15 #include "base/gtest_prod_util.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/memory/ref_counted.h" 17 #include "base/memory/ref_counted.h"
18 #include "base/memory/weak_ptr.h"
18 #include "base/time/time.h" 19 #include "base/time/time.h"
19 #include "base/timer/timer.h" 20 #include "base/timer/timer.h"
20 #include "content/browser/indexed_db/indexed_db_backing_store.h" 21 #include "content/browser/indexed_db/indexed_db_backing_store.h"
21 #include "content/browser/indexed_db/indexed_db_connection.h" 22 #include "content/browser/indexed_db/indexed_db_connection.h"
22 #include "content/browser/indexed_db/indexed_db_database.h" 23 #include "content/browser/indexed_db/indexed_db_database.h"
23 #include "content/browser/indexed_db/indexed_db_database_error.h" 24 #include "content/browser/indexed_db/indexed_db_database_error.h"
24 #include "content/browser/indexed_db/indexed_db_observer.h" 25 #include "content/browser/indexed_db/indexed_db_observer.h"
25 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" 26 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h"
26 27
27 namespace content { 28 namespace content {
28 29
29 class BlobWriteCallbackImpl; 30 class BlobWriteCallbackImpl;
30 class IndexedDBCursor; 31 class IndexedDBCursor;
31 class IndexedDBDatabaseCallbacks; 32 class IndexedDBDatabaseCallbacks;
32 33
33 class CONTENT_EXPORT IndexedDBTransaction 34 class CONTENT_EXPORT IndexedDBTransaction {
34 : public NON_EXPORTED_BASE(base::RefCounted<IndexedDBTransaction>) {
35 public: 35 public:
36 using Operation = base::Callback<leveldb::Status(IndexedDBTransaction*)>; 36 using Operation = base::Callback<leveldb::Status(IndexedDBTransaction*)>;
37 using AbortOperation = base::Closure; 37 using AbortOperation = base::Closure;
38 38
39 enum State { 39 enum State {
40 CREATED, // Created, but not yet started by coordinator. 40 CREATED, // Created, but not yet started by coordinator.
41 STARTED, // Started by the coordinator. 41 STARTED, // Started by the coordinator.
42 COMMITTING, // In the process of committing, possibly waiting for blobs 42 COMMITTING, // In the process of committing, possibly waiting for blobs
43 // to be written. 43 // to be written.
44 FINISHED, // Either aborted or committed. 44 FINISHED, // Either aborted or committed.
45 }; 45 };
46 46
47 virtual ~IndexedDBTransaction();
48
49 leveldb::Status Commit();
50
51 // This object is destroyed by these method calls.
47 virtual void Abort(); 52 virtual void Abort();
48 leveldb::Status Commit();
49 void Abort(const IndexedDBDatabaseError& error); 53 void Abort(const IndexedDBDatabaseError& error);
50 54
51 // Called by the transaction coordinator when this transaction is unblocked. 55 // Called by the transaction coordinator when this transaction is unblocked.
52 void Start(); 56 void Start();
53 57
54 blink::WebIDBTransactionMode mode() const { return mode_; } 58 blink::WebIDBTransactionMode mode() const { return mode_; }
55 const std::set<int64_t>& scope() const { return object_store_ids_; } 59 const std::set<int64_t>& scope() const { return object_store_ids_; }
56 60
61 // Tasks cannot call Commit.
57 void ScheduleTask(Operation task) { 62 void ScheduleTask(Operation task) {
58 ScheduleTask(blink::WebIDBTaskTypeNormal, task); 63 ScheduleTask(blink::WebIDBTaskTypeNormal, task);
59 } 64 }
60 void ScheduleTask(blink::WebIDBTaskType, Operation task); 65 void ScheduleTask(blink::WebIDBTaskType, Operation task);
61 void ScheduleAbortTask(AbortOperation abort_task); 66 void ScheduleAbortTask(AbortOperation abort_task);
62 void RegisterOpenCursor(IndexedDBCursor* cursor); 67 void RegisterOpenCursor(IndexedDBCursor* cursor);
63 void UnregisterOpenCursor(IndexedDBCursor* cursor); 68 void UnregisterOpenCursor(IndexedDBCursor* cursor);
64 void AddPreemptiveEvent() { pending_preemptive_events_++; } 69 void AddPreemptiveEvent() { pending_preemptive_events_++; }
65 void DidCompletePreemptiveEvent() { 70 void DidCompletePreemptiveEvent() {
66 pending_preemptive_events_--; 71 pending_preemptive_events_--;
(...skipping 12 matching lines...) Expand all
79 void RecordObserverForLastObservation(int32_t connection_id, 84 void RecordObserverForLastObservation(int32_t connection_id,
80 int32_t observer_id); 85 int32_t observer_id);
81 86
82 IndexedDBBackingStore::Transaction* BackingStoreTransaction() { 87 IndexedDBBackingStore::Transaction* BackingStoreTransaction() {
83 return transaction_.get(); 88 return transaction_.get();
84 } 89 }
85 int64_t id() const { return id_; } 90 int64_t id() const { return id_; }
86 91
87 IndexedDBDatabase* database() const { return database_.get(); } 92 IndexedDBDatabase* database() const { return database_.get(); }
88 IndexedDBDatabaseCallbacks* callbacks() const { return callbacks_.get(); } 93 IndexedDBDatabaseCallbacks* callbacks() const { return callbacks_.get(); }
89 IndexedDBConnection* connection() const { return connection_.get(); } 94 IndexedDBConnection* connection() const { return connection_; }
90 95
91 State state() const { return state_; } 96 State state() const { return state_; }
92 bool IsTimeoutTimerRunning() const { return timeout_timer_.IsRunning(); } 97 bool IsTimeoutTimerRunning() const { return timeout_timer_.IsRunning(); }
93 98
94 struct Diagnostics { 99 struct Diagnostics {
95 base::Time creation_time; 100 base::Time creation_time;
96 base::Time start_time; 101 base::Time start_time;
97 int tasks_scheduled; 102 int tasks_scheduled;
98 int tasks_completed; 103 int tasks_completed;
99 }; 104 };
100 105
101 const Diagnostics& diagnostics() const { return diagnostics_; } 106 const Diagnostics& diagnostics() const { return diagnostics_; }
102 107
108 void set_size(int64_t size) { size_ = size; }
109 int64_t size() const { return size_; }
110
103 protected: 111 protected:
104 // Test classes may derive, but most creation should be done via 112 // Test classes may derive, but most creation should be done via
105 // IndexedDBClassFactory. 113 // IndexedDBClassFactory.
106 IndexedDBTransaction( 114 IndexedDBTransaction(
107 int64_t id, 115 int64_t id,
108 base::WeakPtr<IndexedDBConnection> connection, 116 IndexedDBConnection* connection,
109 const std::set<int64_t>& object_store_ids, 117 const std::set<int64_t>& object_store_ids,
110 blink::WebIDBTransactionMode mode, 118 blink::WebIDBTransactionMode mode,
111 IndexedDBBackingStore::Transaction* backing_store_transaction); 119 IndexedDBBackingStore::Transaction* backing_store_transaction);
112 virtual ~IndexedDBTransaction();
113 120
114 // May be overridden in tests. 121 // May be overridden in tests.
115 virtual base::TimeDelta GetInactivityTimeout() const; 122 virtual base::TimeDelta GetInactivityTimeout() const;
116 123
117 private: 124 private:
118 friend class BlobWriteCallbackImpl; 125 friend class BlobWriteCallbackImpl;
119 friend class IndexedDBClassFactory; 126 friend class IndexedDBClassFactory;
127 friend class IndexedDBConnection;
120 friend class base::RefCounted<IndexedDBTransaction>; 128 friend class base::RefCounted<IndexedDBTransaction>;
121 129
122 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortPreemptive); 130 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortPreemptive);
123 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortTasks); 131 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortTasks);
124 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, NoTimeoutReadOnly); 132 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, NoTimeoutReadOnly);
125 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, 133 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest,
126 SchedulePreemptiveTask); 134 SchedulePreemptiveTask);
127 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, 135 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode,
128 ScheduleNormalTask); 136 ScheduleNormalTask);
129 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, TaskFails); 137 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, TaskFails);
130 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, Timeout); 138 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, Timeout);
131 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, IndexedDBObserver); 139 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, IndexedDBObserver);
132 140
133 void RunTasksIfStarted(); 141 void RunTasksIfStarted();
134 142
135 bool IsTaskQueueEmpty() const; 143 bool IsTaskQueueEmpty() const;
136 bool HasPendingTasks() const; 144 bool HasPendingTasks() const;
137 145
138 void BlobWriteComplete(bool success); 146 leveldb::Status BlobWriteComplete(
147 IndexedDBBackingStore::BlobWriteResult result);
139 void ProcessTaskQueue(); 148 void ProcessTaskQueue();
140 void CloseOpenCursors(); 149 void CloseOpenCursors();
141 leveldb::Status CommitPhaseTwo(); 150 leveldb::Status CommitPhaseTwo();
142 void Timeout(); 151 void Timeout();
143 152
144 const int64_t id_; 153 const int64_t id_;
145 const std::set<int64_t> object_store_ids_; 154 const std::set<int64_t> object_store_ids_;
146 const blink::WebIDBTransactionMode mode_; 155 const blink::WebIDBTransactionMode mode_;
147 156
148 bool used_ = false; 157 bool used_ = false;
149 State state_ = CREATED; 158 State state_ = CREATED;
150 bool commit_pending_ = false; 159 bool commit_pending_ = false;
151 base::WeakPtr<IndexedDBConnection> connection_; 160 // We are owned by the connection object.
161 IndexedDBConnection* connection_;
152 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_; 162 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_;
153 scoped_refptr<IndexedDBDatabase> database_; 163 scoped_refptr<IndexedDBDatabase> database_;
154 164
155 // Observers in pending queue do not listen to changes until activated. 165 // Observers in pending queue do not listen to changes until activated.
156 std::vector<std::unique_ptr<IndexedDBObserver>> pending_observers_; 166 std::vector<std::unique_ptr<IndexedDBObserver>> pending_observers_;
157 std::map<int32_t, ::indexed_db::mojom::ObserverChangesPtr> 167 std::map<int32_t, ::indexed_db::mojom::ObserverChangesPtr>
158 connection_changes_map_; 168 connection_changes_map_;
159 169
170 // Metrics for quota.
171 int64_t size_ = 0;
172
160 class TaskQueue { 173 class TaskQueue {
161 public: 174 public:
162 TaskQueue(); 175 TaskQueue();
163 ~TaskQueue(); 176 ~TaskQueue();
164 bool empty() const { return queue_.empty(); } 177 bool empty() const { return queue_.empty(); }
165 void push(Operation task) { queue_.push(std::move(task)); } 178 void push(Operation task) { queue_.push(std::move(task)); }
166 Operation pop(); 179 Operation pop();
167 void clear(); 180 void clear();
168 181
169 private: 182 private:
(...skipping 29 matching lines...) Expand all
199 bool processing_event_queue_ = false; 212 bool processing_event_queue_ = false;
200 213
201 std::set<IndexedDBCursor*> open_cursors_; 214 std::set<IndexedDBCursor*> open_cursors_;
202 215
203 // This timer is started after requests have been processed. If no subsequent 216 // This timer is started after requests have been processed. If no subsequent
204 // requests are processed before the timer fires, assume the script is 217 // requests are processed before the timer fires, assume the script is
205 // unresponsive and abort to unblock the transaction queue. 218 // unresponsive and abort to unblock the transaction queue.
206 base::OneShotTimer timeout_timer_; 219 base::OneShotTimer timeout_timer_;
207 220
208 Diagnostics diagnostics_; 221 Diagnostics diagnostics_;
222
223 base::WeakPtrFactory<IndexedDBTransaction> ptr_factory_;
209 }; 224 };
210 225
211 } // namespace content 226 } // namespace content
212 227
213 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ 228 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698