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.
5 #include "content/browser/indexed_db/indexed_db_transaction.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/browser/indexed_db/indexed_db_backing_store.h"
12 #include "content/browser/indexed_db/indexed_db_cursor.h"
13 #include "content/browser/indexed_db/indexed_db_database.h"
14 #include "content/browser/indexed_db/indexed_db_database_callbacks.h"
15 #include "content/browser/indexed_db/indexed_db_tracing.h"
16 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h"
17 #include "third_party/WebKit/public/platform/WebIDBDatabaseException.h"
21 const int64 kInactivityTimeoutPeriodSeconds
= 60;
23 IndexedDBTransaction::TaskQueue::TaskQueue() {}
24 IndexedDBTransaction::TaskQueue::~TaskQueue() { clear(); }
26 void IndexedDBTransaction::TaskQueue::clear() {
27 while (!queue_
.empty())
31 IndexedDBTransaction::Operation
IndexedDBTransaction::TaskQueue::pop() {
32 DCHECK(!queue_
.empty());
33 Operation
task(queue_
.front());
38 IndexedDBTransaction::TaskStack::TaskStack() {}
39 IndexedDBTransaction::TaskStack::~TaskStack() { clear(); }
41 void IndexedDBTransaction::TaskStack::clear() {
42 while (!stack_
.empty())
46 IndexedDBTransaction::Operation
IndexedDBTransaction::TaskStack::pop() {
47 DCHECK(!stack_
.empty());
48 Operation
task(stack_
.top());
53 IndexedDBTransaction::IndexedDBTransaction(
55 scoped_refptr
<IndexedDBDatabaseCallbacks
> callbacks
,
56 const std::set
<int64
>& object_store_ids
,
57 indexed_db::TransactionMode mode
,
58 IndexedDBDatabase
* database
,
59 IndexedDBBackingStore::Transaction
* backing_store_transaction
)
61 object_store_ids_(object_store_ids
),
65 commit_pending_(false),
66 callbacks_(callbacks
),
68 transaction_(backing_store_transaction
),
69 backing_store_transaction_begun_(false),
70 should_process_queue_(false),
71 pending_preemptive_events_(0) {
72 database_
->transaction_coordinator().DidCreateTransaction(this);
74 diagnostics_
.tasks_scheduled
= 0;
75 diagnostics_
.tasks_completed
= 0;
76 diagnostics_
.creation_time
= base::Time::Now();
79 IndexedDBTransaction::~IndexedDBTransaction() {
80 // It shouldn't be possible for this object to get deleted until it's either
81 // complete or aborted.
82 DCHECK_EQ(state_
, FINISHED
);
83 DCHECK(preemptive_task_queue_
.empty());
84 DCHECK(task_queue_
.empty());
85 DCHECK(abort_task_stack_
.empty());
88 void IndexedDBTransaction::ScheduleTask(Operation task
, Operation abort_task
) {
89 if (state_
== FINISHED
)
92 timeout_timer_
.Stop();
94 task_queue_
.push(task
);
95 ++diagnostics_
.tasks_scheduled
;
96 abort_task_stack_
.push(abort_task
);
100 void IndexedDBTransaction::ScheduleTask(IndexedDBDatabase::TaskType type
,
102 if (state_
== FINISHED
)
105 timeout_timer_
.Stop();
107 if (type
== IndexedDBDatabase::NORMAL_TASK
) {
108 task_queue_
.push(task
);
109 ++diagnostics_
.tasks_scheduled
;
111 preemptive_task_queue_
.push(task
);
116 void IndexedDBTransaction::RunTasksIfStarted() {
119 // Not started by the coordinator yet.
120 if (state_
!= STARTED
)
123 // A task is already posted.
124 if (should_process_queue_
)
127 should_process_queue_
= true;
128 base::MessageLoop::current()->PostTask(
129 FROM_HERE
, base::Bind(&IndexedDBTransaction::ProcessTaskQueue
, this));
132 void IndexedDBTransaction::Abort() {
133 Abort(IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError
,
134 "Internal error (unknown cause)"));
137 void IndexedDBTransaction::Abort(const IndexedDBDatabaseError
& error
) {
138 IDB_TRACE("IndexedDBTransaction::Abort");
139 if (state_
== FINISHED
)
142 // The last reference to this object may be released while performing the
143 // abort steps below. We therefore take a self reference to keep ourselves
144 // alive while executing this method.
145 scoped_refptr
<IndexedDBTransaction
> protect(this);
147 timeout_timer_
.Stop();
150 should_process_queue_
= false;
152 if (backing_store_transaction_begun_
)
153 transaction_
->Rollback();
155 // Run the abort tasks, if any.
156 while (!abort_task_stack_
.empty())
157 abort_task_stack_
.pop().Run(NULL
);
159 preemptive_task_queue_
.clear();
162 // Backing store resources (held via cursors) must be released
163 // before script callbacks are fired, as the script callbacks may
164 // release references and allow the backing store itself to be
165 // released, and order is critical.
167 transaction_
->Reset();
169 // Transactions must also be marked as completed before the
170 // front-end is notified, as the transaction completion unblocks
171 // operations like closing connections.
172 database_
->transaction_coordinator().DidFinishTransaction(this);
174 DCHECK(!database_
->transaction_coordinator().IsActive(this));
177 if (callbacks_
.get())
178 callbacks_
->OnAbort(id_
, error
);
180 database_
->TransactionFinished(this, false);
185 bool IndexedDBTransaction::IsTaskQueueEmpty() const {
186 return preemptive_task_queue_
.empty() && task_queue_
.empty();
189 bool IndexedDBTransaction::HasPendingTasks() const {
190 return pending_preemptive_events_
|| !IsTaskQueueEmpty();
193 void IndexedDBTransaction::RegisterOpenCursor(IndexedDBCursor
* cursor
) {
194 open_cursors_
.insert(cursor
);
197 void IndexedDBTransaction::UnregisterOpenCursor(IndexedDBCursor
* cursor
) {
198 open_cursors_
.erase(cursor
);
201 void IndexedDBTransaction::Start() {
202 // TransactionCoordinator has started this transaction.
203 DCHECK_EQ(CREATED
, state_
);
205 diagnostics_
.start_time
= base::Time::Now();
213 void IndexedDBTransaction::Commit() {
214 IDB_TRACE("IndexedDBTransaction::Commit");
216 // In multiprocess ports, front-end may have requested a commit but
217 // an abort has already been initiated asynchronously by the
219 if (state_
== FINISHED
)
222 DCHECK(!used_
|| state_
== STARTED
);
223 commit_pending_
= true;
225 // Front-end has requested a commit, but there may be tasks like
226 // create_index which are considered synchronous by the front-end
227 // but are processed asynchronously.
228 if (HasPendingTasks())
231 // The last reference to this object may be released while performing the
232 // commit steps below. We therefore take a self reference to keep ourselves
233 // alive while executing this method.
234 scoped_refptr
<IndexedDBTransaction
> protect(this);
236 timeout_timer_
.Stop();
240 bool committed
= !used_
|| transaction_
->Commit().ok();
242 // Backing store resources (held via cursors) must be released
243 // before script callbacks are fired, as the script callbacks may
244 // release references and allow the backing store itself to be
245 // released, and order is critical.
247 transaction_
->Reset();
249 // Transactions must also be marked as completed before the
250 // front-end is notified, as the transaction completion unblocks
251 // operations like closing connections.
252 database_
->transaction_coordinator().DidFinishTransaction(this);
255 abort_task_stack_
.clear();
256 callbacks_
->OnComplete(id_
);
257 database_
->TransactionFinished(this, true);
259 while (!abort_task_stack_
.empty())
260 abort_task_stack_
.pop().Run(NULL
);
264 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError
,
265 "Internal error committing transaction."));
266 database_
->TransactionFinished(this, false);
267 database_
->TransactionCommitFailed();
273 void IndexedDBTransaction::ProcessTaskQueue() {
274 IDB_TRACE("IndexedDBTransaction::ProcessTaskQueue");
276 // May have been aborted.
277 if (!should_process_queue_
)
280 DCHECK(!IsTaskQueueEmpty());
281 should_process_queue_
= false;
283 if (!backing_store_transaction_begun_
) {
284 transaction_
->Begin();
285 backing_store_transaction_begun_
= true;
288 // The last reference to this object may be released while performing the
289 // tasks. Take take a self reference to keep this object alive so that
290 // the loop termination conditions can be checked.
291 scoped_refptr
<IndexedDBTransaction
> protect(this);
293 TaskQueue
* task_queue
=
294 pending_preemptive_events_
? &preemptive_task_queue_
: &task_queue_
;
295 while (!task_queue
->empty() && state_
!= FINISHED
) {
296 DCHECK_EQ(STARTED
, state_
);
297 Operation
task(task_queue
->pop());
299 if (!pending_preemptive_events_
) {
300 DCHECK(diagnostics_
.tasks_completed
< diagnostics_
.tasks_scheduled
);
301 ++diagnostics_
.tasks_completed
;
304 // Event itself may change which queue should be processed next.
306 pending_preemptive_events_
? &preemptive_task_queue_
: &task_queue_
;
309 // If there are no pending tasks, we haven't already committed/aborted,
310 // and the front-end requested a commit, it is now safe to do so.
311 if (!HasPendingTasks() && state_
!= FINISHED
&& commit_pending_
) {
316 // The transaction may have been aborted while processing tasks.
317 if (state_
== FINISHED
)
320 // Otherwise, start a timer in case the front-end gets wedged and
321 // never requests further activity. Read-only transactions don't
322 // block other transactions, so don't time those out.
323 if (mode_
!= indexed_db::TRANSACTION_READ_ONLY
) {
324 timeout_timer_
.Start(
326 base::TimeDelta::FromSeconds(kInactivityTimeoutPeriodSeconds
),
327 base::Bind(&IndexedDBTransaction::Timeout
, this));
331 void IndexedDBTransaction::Timeout() {
332 Abort(IndexedDBDatabaseError(
333 blink::WebIDBDatabaseExceptionTimeoutError
,
334 base::ASCIIToUTF16("Transaction timed out due to inactivity.")));
337 void IndexedDBTransaction::CloseOpenCursors() {
338 for (std::set
<IndexedDBCursor
*>::iterator i
= open_cursors_
.begin();
339 i
!= open_cursors_
.end();
342 open_cursors_
.clear();
345 } // namespace content