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

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

Issue 15564008: Migrate the IndexedDB backend from Blink to Chromium (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Coding style fixes Created 7 years, 7 months 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/indexed_db/indexed_db_transaction.h"
6
7 #include <vector>
8 #include "base/logging.h"
9 #include "base/utf_string_conversions.h"
10 #include "content/browser/indexed_db/indexed_db_backing_store.h"
11 #include "content/browser/indexed_db/indexed_db_cursor_impl.h"
12 #include "content/browser/indexed_db/indexed_db_database_callbacks_wrapper.h"
13 #include "content/browser/indexed_db/indexed_db_database_impl.h"
14 #include "content/browser/indexed_db/indexed_db_tracing.h"
15 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h"
16 #include "third_party/WebKit/Source/Platform/chromium/public/WebIDBDatabaseExcep tion.h"
17
18 namespace content {
19
20 IndexedDBTransaction::TaskQueue::TaskQueue() {}
21 IndexedDBTransaction::TaskQueue::~TaskQueue() { clear(); }
22
23 void IndexedDBTransaction::TaskQueue::clear() {
24 while (!queue_.empty())
25 scoped_ptr<Operation> task(pop());
26 }
27
28 scoped_ptr<IndexedDBTransaction::Operation>
29 IndexedDBTransaction::TaskQueue::pop() {
30 DCHECK(!queue_.empty());
31 scoped_ptr<Operation> task(queue_.front());
32 queue_.pop();
33 return task.Pass();
34 }
35
36 IndexedDBTransaction::TaskStack::TaskStack() {}
37 IndexedDBTransaction::TaskStack::~TaskStack() { clear(); }
38
39 void IndexedDBTransaction::TaskStack::clear() {
40 while (!stack_.empty())
41 scoped_ptr<Operation> task(pop());
42 }
43
44 scoped_ptr<IndexedDBTransaction::Operation>
45 IndexedDBTransaction::TaskStack::pop() {
46 DCHECK(!stack_.empty());
47 scoped_ptr<Operation> task(stack_.top());
48 stack_.pop();
49 return task.Pass();
50 }
51
52 scoped_refptr<IndexedDBTransaction> IndexedDBTransaction::Create(
53 int64_t id,
54 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks,
55 const std::vector<int64_t>& object_store_ids,
56 indexed_db::TransactionMode mode,
57 IndexedDBDatabaseImpl* database) {
58 std::set<int64_t> object_store_hash_set;
59 for (size_t i = 0; i < object_store_ids.size(); ++i)
60 object_store_hash_set.insert(object_store_ids[i]);
61
62 return make_scoped_refptr(new IndexedDBTransaction(
63 id, callbacks, object_store_hash_set, mode, database));
64 }
65
66 IndexedDBTransaction::IndexedDBTransaction(
67 int64_t id,
68 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks,
69 const std::set<int64_t>& object_store_ids,
70 indexed_db::TransactionMode mode,
71 IndexedDBDatabaseImpl* database)
72 : id_(id),
73 object_store_ids_(object_store_ids),
74 mode_(mode),
75 state_(UNUSED),
76 commit_pending_(false),
77 callbacks_(callbacks),
78 database_(database),
79 transaction_(database->BackingStore().get()),
80 pending_preemptive_events_(0) {
81 database_->transaction_coordinator().DidCreateTransaction(this);
82 }
83
84 IndexedDBTransaction::~IndexedDBTransaction() {
85 // It shouldn't be possible for this object to get deleted until it's either
86 // complete or aborted.
87 DCHECK_EQ(state_, FINISHED);
88 DCHECK(preemptive_task_queue_.empty());
89 DCHECK(task_queue_.empty());
90 DCHECK(abort_task_stack_.empty());
91 }
92
93 void IndexedDBTransaction::ScheduleTask(IndexedDBDatabase::TaskType type,
94 Operation* task,
95 Operation* abort_task) {
96 if (state_ == FINISHED)
97 return;
98
99 if (type == IndexedDBDatabase::NORMAL_TASK)
100 task_queue_.push(task);
101 else
102 preemptive_task_queue_.push(task);
103
104 if (abort_task)
105 abort_task_stack_.push(abort_task);
106
107 if (state_ == UNUSED)
108 Start();
109 else if (state_ == RUNNING && !task_timer_.IsRunning())
110 task_timer_.Start(FROM_HERE,
111 base::TimeDelta::FromSeconds(0),
112 this,
113 &IndexedDBTransaction::TaskTimerFired);
114 }
115
116 void IndexedDBTransaction::Abort() {
117 Abort(IndexedDBDatabaseError::Create(
118 WebKit::WebIDBDatabaseExceptionUnknownError,
119 ASCIIToUTF16("Internal error (unknown cause)")));
120 }
121
122 void IndexedDBTransaction::Abort(scoped_refptr<IndexedDBDatabaseError> error) {
123 IDB_TRACE("IndexedDBTransaction::abort");
124 if (state_ == FINISHED)
125 return;
126
127 bool was_running = state_ == RUNNING;
128
129 // The last reference to this object may be released while performing the
130 // abort steps below. We therefore take a self reference to keep ourselves
131 // alive while executing this method.
132 scoped_refptr<IndexedDBTransaction> protect(this);
133
134 state_ = FINISHED;
135 task_timer_.Stop();
136
137 if (was_running)
138 transaction_.Rollback();
139
140 // Run the abort tasks, if any.
141 while (!abort_task_stack_.empty()) {
142 scoped_ptr<Operation> task(abort_task_stack_.pop());
143 task->Perform(0);
144 }
145 preemptive_task_queue_.clear();
146 task_queue_.clear();
147
148 // Backing store resources (held via cursors) must be released
149 // before script callbacks are fired, as the script callbacks may
150 // release references and allow the backing store itself to be
151 // released, and order is critical.
152 CloseOpenCursors();
153 transaction_.Reset();
154
155 // Transactions must also be marked as completed before the
156 // front-end is notified, as the transaction completion unblocks
157 // operations like closing connections.
158 database_->transaction_coordinator().DidFinishTransaction(this);
159 DCHECK(!database_->transaction_coordinator().IsActive(this));
160 database_->TransactionFinished(this);
161
162 if (callbacks_)
163 callbacks_->OnAbort(id_, error);
164
165 database_->TransactionFinishedAndAbortFired(this);
166
167 database_ = NULL;
168 }
169
170 bool IndexedDBTransaction::IsTaskQueueEmpty() const {
171 return preemptive_task_queue_.empty() && task_queue_.empty();
172 }
173
174 bool IndexedDBTransaction::HasPendingTasks() const {
175 return pending_preemptive_events_ || !IsTaskQueueEmpty();
176 }
177
178 void IndexedDBTransaction::RegisterOpenCursor(IndexedDBCursorImpl* cursor) {
179 open_cursors_.insert(cursor);
180 }
181
182 void IndexedDBTransaction::UnregisterOpenCursor(IndexedDBCursorImpl* cursor) {
183 open_cursors_.erase(cursor);
184 }
185
186 void IndexedDBTransaction::Run() {
187 // TransactionCoordinator has started this transaction. Schedule a timer
188 // to process the first task.
189 DCHECK_EQ(state_ == START_PENDING || state_, RUNNING);
190 DCHECK(!task_timer_.IsRunning());
191
192 task_timer_.Start(FROM_HERE,
193 base::TimeDelta::FromSeconds(0),
194 this,
195 &IndexedDBTransaction::TaskTimerFired);
196 }
197
198 void IndexedDBTransaction::Start() {
199 DCHECK_EQ(state_, UNUSED);
200
201 state_ = START_PENDING;
202 database_->transaction_coordinator().DidStartTransaction(this);
203 database_->TransactionStarted(this);
204 }
205
206 void IndexedDBTransaction::Commit() {
207 IDB_TRACE("IndexedDBTransaction::commit");
208
209 // In multiprocess ports, front-end may have requested a commit but
210 // an abort has already been initiated asynchronously by the
211 // back-end.
212 if (state_ == FINISHED)
213 return;
214
215 DCHECK_EQ(state_ == UNUSED || state_, RUNNING);
216 commit_pending_ = true;
217
218 // Front-end has requested a commit, but there may be tasks like
219 // create_index which are considered synchronous by the front-end
220 // but are processed asynchronously.
221 if (HasPendingTasks())
222 return;
223
224 // The last reference to this object may be released while performing the
225 // commit steps below. We therefore take a self reference to keep ourselves
226 // alive while executing this method.
227 scoped_refptr<IndexedDBTransaction> protect(this);
228
229 // TODO(jsbell): Run abort tasks if commit fails? http://crbug.com/241843
230 abort_task_stack_.clear();
231
232 bool unused = state_ == UNUSED;
233 state_ = FINISHED;
234
235 bool committed = unused || transaction_.Commit();
236
237 // Backing store resources (held via cursors) must be released
238 // before script callbacks are fired, as the script callbacks may
239 // release references and allow the backing store itself to be
240 // released, and order is critical.
241 CloseOpenCursors();
242 transaction_.Reset();
243
244 // Transactions must also be marked as completed before the
245 // front-end is notified, as the transaction completion unblocks
246 // operations like closing connections.
247 if (!unused)
248 database_->transaction_coordinator().DidFinishTransaction(this);
249 database_->TransactionFinished(this);
250
251 if (committed) {
252 callbacks_->OnComplete(id_);
253 database_->TransactionFinishedAndCompleteFired(this);
254 } else {
255 callbacks_->OnAbort(
256 id_,
257 IndexedDBDatabaseError::Create(
258 WebKit::WebIDBDatabaseExceptionUnknownError,
259 ASCIIToUTF16("Internal error committing transaction.")));
260 database_->TransactionFinishedAndAbortFired(this);
261 }
262
263 database_ = NULL;
264 }
265
266 void IndexedDBTransaction::TaskTimerFired() {
267 IDB_TRACE("IndexedDBTransaction::task_timer_fired");
268 DCHECK(!IsTaskQueueEmpty());
269
270 if (state_ == START_PENDING) {
271 transaction_.begin();
272 state_ = RUNNING;
273 }
274
275 // The last reference to this object may be released while performing the
276 // tasks. Take take a self reference to keep this object alive so that
277 // the loop termination conditions can be checked.
278 scoped_refptr<IndexedDBTransaction> protect(this);
279
280 TaskQueue* task_queue =
281 pending_preemptive_events_ ? &preemptive_task_queue_ : &task_queue_;
282 while (!task_queue->empty() && state_ != FINISHED) {
283 DCHECK_EQ(state_, RUNNING);
284 scoped_ptr<Operation> task(task_queue->pop());
285 task->Perform(this);
286
287 // Event itself may change which queue should be processed next.
288 task_queue =
289 pending_preemptive_events_ ? &preemptive_task_queue_ : &task_queue_;
290 }
291
292 // If there are no pending tasks, we haven't already committed/aborted,
293 // and the front-end requested a commit, it is now safe to do so.
294 if (!HasPendingTasks() && state_ != FINISHED && commit_pending_)
295 Commit();
296 }
297
298 void IndexedDBTransaction::CloseOpenCursors() {
299 for (std::set<IndexedDBCursorImpl*>::iterator i = open_cursors_.begin();
300 i != open_cursors_.end();
301 ++i)
302 (*i)->Close();
303 open_cursors_.clear();
304 }
305
306 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698