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

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 & rebase Created 4 years, 1 month 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"
27 #include "url/origin.h"
26 28
27 namespace content { 29 namespace content {
28 30
29 class BlobWriteCallbackImpl; 31 class BlobWriteCallbackImpl;
30 class IndexedDBCursor; 32 class IndexedDBCursor;
31 class IndexedDBDatabaseCallbacks; 33 class IndexedDBDatabaseCallbacks;
32 class IndexedDBObservation; 34 class IndexedDBObservation;
33 class IndexedDBObserverChanges; 35 class IndexedDBObserverChanges;
34 36
35 class CONTENT_EXPORT IndexedDBTransaction 37 class CONTENT_EXPORT IndexedDBTransaction {
36 : public NON_EXPORTED_BASE(base::RefCounted<IndexedDBTransaction>) {
37 public: 38 public:
38 typedef base::Callback<void(IndexedDBTransaction*)> Operation; 39 typedef base::Callback<void(IndexedDBTransaction*)> Operation;
39 40
40 enum State { 41 enum State {
41 CREATED, // Created, but not yet started by coordinator. 42 CREATED, // Created, but not yet started by coordinator.
42 STARTED, // Started by the coordinator. 43 STARTED, // Started by the coordinator.
43 COMMITTING, // In the process of committing, possibly waiting for blobs 44 COMMITTING, // In the process of committing, possibly waiting for blobs
44 // to be written. 45 // to be written.
45 FINISHED, // Either aborted or committed. 46 FINISHED, // Either aborted or committed.
46 }; 47 };
47 48
49 virtual ~IndexedDBTransaction();
50
51 leveldb::Status Commit();
52
53 // This object is destroyed by these method calls.
48 virtual void Abort(); 54 virtual void Abort();
49 leveldb::Status Commit();
50 void Abort(const IndexedDBDatabaseError& error); 55 void Abort(const IndexedDBDatabaseError& error);
51 56
52 // Called by the transaction coordinator when this transaction is unblocked. 57 // Called by the transaction coordinator when this transaction is unblocked.
53 void Start(); 58 void Start();
54 59
55 blink::WebIDBTransactionMode mode() const { return mode_; } 60 blink::WebIDBTransactionMode mode() const { return mode_; }
56 const std::set<int64_t>& scope() const { return object_store_ids_; } 61 const std::set<int64_t>& scope() const { return object_store_ids_; }
57 62
58 void ScheduleTask(Operation task) { 63 void ScheduleTask(Operation task) {
59 ScheduleTask(blink::WebIDBTaskTypeNormal, task); 64 ScheduleTask(blink::WebIDBTaskTypeNormal, task);
(...skipping 20 matching lines...) Expand all
80 void RecordObserverForLastObservation(int32_t connection_id, 85 void RecordObserverForLastObservation(int32_t connection_id,
81 int32_t observer_id); 86 int32_t observer_id);
82 87
83 IndexedDBBackingStore::Transaction* BackingStoreTransaction() { 88 IndexedDBBackingStore::Transaction* BackingStoreTransaction() {
84 return transaction_.get(); 89 return transaction_.get();
85 } 90 }
86 int64_t id() const { return id_; } 91 int64_t id() const { return id_; }
87 92
88 IndexedDBDatabase* database() const { return database_.get(); } 93 IndexedDBDatabase* database() const { return database_.get(); }
89 IndexedDBDatabaseCallbacks* callbacks() const { return callbacks_.get(); } 94 IndexedDBDatabaseCallbacks* callbacks() const { return callbacks_.get(); }
90 IndexedDBConnection* connection() const { return connection_.get(); } 95 IndexedDBConnection* connection() const { return connection_; }
91 96
92 State state() const { return state_; } 97 State state() const { return state_; }
93 bool IsTimeoutTimerRunning() const { return timeout_timer_.IsRunning(); } 98 bool IsTimeoutTimerRunning() const { return timeout_timer_.IsRunning(); }
94 99
95 struct Diagnostics { 100 struct Diagnostics {
96 base::Time creation_time; 101 base::Time creation_time;
97 base::Time start_time; 102 base::Time start_time;
98 int tasks_scheduled; 103 int tasks_scheduled;
99 int tasks_completed; 104 int tasks_completed;
100 }; 105 };
101 106
102 const Diagnostics& diagnostics() const { return diagnostics_; } 107 const Diagnostics& diagnostics() const { return diagnostics_; }
103 108
109 void set_size(uint64_t size) { size_ = size; }
110 uint64_t size() const { return size_; }
111
104 protected: 112 protected:
105 // Test classes may derive, but most creation should be done via
106 // IndexedDBClassFactory.
107 IndexedDBTransaction(
108 int64_t id,
109 base::WeakPtr<IndexedDBConnection> connection,
110 const std::set<int64_t>& object_store_ids,
111 blink::WebIDBTransactionMode mode,
112 IndexedDBBackingStore::Transaction* backing_store_transaction);
113 virtual ~IndexedDBTransaction();
114
115 // May be overridden in tests. 113 // May be overridden in tests.
116 virtual base::TimeDelta GetInactivityTimeout() const; 114 virtual base::TimeDelta GetInactivityTimeout() const;
117 115
118 private: 116 private:
119 friend class BlobWriteCallbackImpl; 117 friend class BlobWriteCallbackImpl;
120 friend class IndexedDBClassFactory; 118 friend class IndexedDBClassFactory;
119 friend class IndexedDBConnection;
121 friend class base::RefCounted<IndexedDBTransaction>; 120 friend class base::RefCounted<IndexedDBTransaction>;
122 121
123 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortPreemptive); 122 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortPreemptive);
124 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortTasks); 123 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortTasks);
125 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, NoTimeoutReadOnly); 124 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, NoTimeoutReadOnly);
126 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, 125 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest,
127 SchedulePreemptiveTask); 126 SchedulePreemptiveTask);
128 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, 127 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode,
129 ScheduleNormalTask); 128 ScheduleNormalTask);
130 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, Timeout); 129 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, Timeout);
131 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, IndexedDBObserver); 130 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, IndexedDBObserver);
132 131
132 IndexedDBTransaction(
133 int64_t id,
134 IndexedDBConnection* connection,
135 const std::set<int64_t>& object_store_ids,
136 blink::WebIDBTransactionMode mode,
137 IndexedDBBackingStore::Transaction* backing_store_transaction);
138
133 void RunTasksIfStarted(); 139 void RunTasksIfStarted();
134 140
135 bool IsTaskQueueEmpty() const; 141 bool IsTaskQueueEmpty() const;
136 bool HasPendingTasks() const; 142 bool HasPendingTasks() const;
137 143
138 void BlobWriteComplete(bool success); 144 void BlobWriteComplete(bool success);
139 void ProcessTaskQueue(); 145 void ProcessTaskQueue();
140 void CloseOpenCursors(); 146 void CloseOpenCursors();
141 leveldb::Status CommitPhaseTwo(); 147 leveldb::Status CommitPhaseTwo();
142 void Timeout(); 148 void Timeout();
143 149
144 const int64_t id_; 150 const int64_t id_;
145 const std::set<int64_t> object_store_ids_; 151 const std::set<int64_t> object_store_ids_;
146 const blink::WebIDBTransactionMode mode_; 152 const blink::WebIDBTransactionMode mode_;
147 153
148 bool used_; 154 bool used_;
149 State state_; 155 State state_;
150 bool commit_pending_; 156 bool commit_pending_;
151 base::WeakPtr<IndexedDBConnection> connection_; 157 // We are owned by the connection object.
158 IndexedDBConnection* connection_;
152 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_; 159 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_;
153 scoped_refptr<IndexedDBDatabase> database_; 160 scoped_refptr<IndexedDBDatabase> database_;
154 161
155 // Observers in pending queue do not listen to changes until activated. 162 // Observers in pending queue do not listen to changes until activated.
156 std::vector<std::unique_ptr<IndexedDBObserver>> pending_observers_; 163 std::vector<std::unique_ptr<IndexedDBObserver>> pending_observers_;
157 std::map<int32_t, std::unique_ptr<IndexedDBObserverChanges>> 164 std::map<int32_t, std::unique_ptr<IndexedDBObserverChanges>>
158 connection_changes_map_; 165 connection_changes_map_;
159 166
167 // Metrics for quota.
168 uint64_t size_;
169
160 class TaskQueue { 170 class TaskQueue {
161 public: 171 public:
162 TaskQueue(); 172 TaskQueue();
163 ~TaskQueue(); 173 ~TaskQueue();
164 bool empty() const { return queue_.empty(); } 174 bool empty() const { return queue_.empty(); }
165 void push(Operation task) { queue_.push(task); } 175 void push(Operation task) { queue_.push(task); }
166 Operation pop(); 176 Operation pop();
167 void clear(); 177 void clear();
168 178
169 private: 179 private:
(...skipping 28 matching lines...) Expand all
198 int pending_preemptive_events_; 208 int pending_preemptive_events_;
199 209
200 std::set<IndexedDBCursor*> open_cursors_; 210 std::set<IndexedDBCursor*> open_cursors_;
201 211
202 // This timer is started after requests have been processed. If no subsequent 212 // This timer is started after requests have been processed. If no subsequent
203 // requests are processed before the timer fires, assume the script is 213 // requests are processed before the timer fires, assume the script is
204 // unresponsive and abort to unblock the transaction queue. 214 // unresponsive and abort to unblock the transaction queue.
205 base::OneShotTimer timeout_timer_; 215 base::OneShotTimer timeout_timer_;
206 216
207 Diagnostics diagnostics_; 217 Diagnostics diagnostics_;
218
219 base::WeakPtrFactory<IndexedDBTransaction> ptr_factory_;
208 }; 220 };
209 221
210 } // namespace content 222 } // namespace content
211 223
212 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ 224 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698