| OLD | NEW |
| 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 #include "content/browser/indexed_db/indexed_db_transaction.h" | 5 #include "content/browser/indexed_db/indexed_db_transaction.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "third_party/leveldatabase/env_chromium.h" | 23 #include "third_party/leveldatabase/env_chromium.h" |
| 24 | 24 |
| 25 namespace content { | 25 namespace content { |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 const int64_t kInactivityTimeoutPeriodSeconds = 60; | 29 const int64_t kInactivityTimeoutPeriodSeconds = 60; |
| 30 | 30 |
| 31 // Helper for posting a task to call IndexedDBTransaction::Commit when we know | 31 // Helper for posting a task to call IndexedDBTransaction::Commit when we know |
| 32 // the transaction had no requests and therefore the commit must succeed. | 32 // the transaction had no requests and therefore the commit must succeed. |
| 33 void CommitUnused(scoped_refptr<IndexedDBTransaction> transaction) { | 33 void CommitUnused(base::WeakPtr<IndexedDBTransaction> transaction) { |
| 34 if (!transaction) |
| 35 return; |
| 34 leveldb::Status status = transaction->Commit(); | 36 leveldb::Status status = transaction->Commit(); |
| 35 DCHECK(status.ok()); | 37 DCHECK(status.ok()); |
| 36 } | 38 } |
| 37 | 39 |
| 38 // The database will be closed during this call. | 40 // The database will be closed during this call. |
| 39 void ReportError(leveldb::Status status, | 41 void ReportError(leveldb::Status status, |
| 40 const scoped_refptr<IndexedDBDatabase> database, | 42 const scoped_refptr<IndexedDBDatabase> database, |
| 41 IndexedDBFactory* factory) { | 43 IndexedDBFactory* factory) { |
| 42 DCHECK(!status.ok()); | 44 DCHECK(!status.ok()); |
| 43 if (status.IsCorruption()) { | 45 if (status.IsCorruption()) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 80 |
| 79 IndexedDBTransaction::AbortOperation IndexedDBTransaction::TaskStack::pop() { | 81 IndexedDBTransaction::AbortOperation IndexedDBTransaction::TaskStack::pop() { |
| 80 DCHECK(!stack_.empty()); | 82 DCHECK(!stack_.empty()); |
| 81 AbortOperation task = std::move(stack_.top()); | 83 AbortOperation task = std::move(stack_.top()); |
| 82 stack_.pop(); | 84 stack_.pop(); |
| 83 return task; | 85 return task; |
| 84 } | 86 } |
| 85 | 87 |
| 86 IndexedDBTransaction::IndexedDBTransaction( | 88 IndexedDBTransaction::IndexedDBTransaction( |
| 87 int64_t id, | 89 int64_t id, |
| 88 base::WeakPtr<IndexedDBConnection> connection, | 90 IndexedDBConnection* connection, |
| 89 const std::set<int64_t>& object_store_ids, | 91 const std::set<int64_t>& object_store_ids, |
| 90 blink::WebIDBTransactionMode mode, | 92 blink::WebIDBTransactionMode mode, |
| 91 IndexedDBBackingStore::Transaction* backing_store_transaction) | 93 IndexedDBBackingStore::Transaction* backing_store_transaction) |
| 92 : id_(id), | 94 : id_(id), |
| 93 object_store_ids_(object_store_ids), | 95 object_store_ids_(object_store_ids), |
| 94 mode_(mode), | 96 mode_(mode), |
| 95 connection_(std::move(connection)), | 97 connection_(std::move(connection)), |
| 96 transaction_(backing_store_transaction) { | 98 transaction_(backing_store_transaction), |
| 99 ptr_factory_(this) { |
| 97 callbacks_ = connection_->callbacks(); | 100 callbacks_ = connection_->callbacks(); |
| 98 database_ = connection_->database(); | 101 database_ = connection_->database(); |
| 99 | 102 |
| 100 database_->transaction_coordinator().DidCreateTransaction(this); | |
| 101 | |
| 102 diagnostics_.tasks_scheduled = 0; | 103 diagnostics_.tasks_scheduled = 0; |
| 103 diagnostics_.tasks_completed = 0; | 104 diagnostics_.tasks_completed = 0; |
| 104 diagnostics_.creation_time = base::Time::Now(); | 105 diagnostics_.creation_time = base::Time::Now(); |
| 105 } | 106 } |
| 106 | 107 |
| 107 IndexedDBTransaction::~IndexedDBTransaction() { | 108 IndexedDBTransaction::~IndexedDBTransaction() { |
| 108 // It shouldn't be possible for this object to get deleted until it's either | 109 // It shouldn't be possible for this object to get deleted until it's either |
| 109 // complete or aborted. | 110 // complete or aborted. |
| 110 DCHECK_EQ(state_, FINISHED); | 111 DCHECK_EQ(state_, FINISHED); |
| 111 DCHECK(preemptive_task_queue_.empty()); | 112 DCHECK(preemptive_task_queue_.empty()); |
| 112 DCHECK_EQ(pending_preemptive_events_, 0); | 113 DCHECK_EQ(pending_preemptive_events_, 0); |
| 113 DCHECK(task_queue_.empty()); | 114 DCHECK(task_queue_.empty()); |
| 114 DCHECK(abort_task_stack_.empty()); | 115 DCHECK(abort_task_stack_.empty()); |
| 115 DCHECK(pending_observers_.empty()); | |
| 116 DCHECK(!processing_event_queue_); | 116 DCHECK(!processing_event_queue_); |
| 117 } | 117 } |
| 118 | 118 |
| 119 void IndexedDBTransaction::ScheduleTask(blink::WebIDBTaskType type, | 119 void IndexedDBTransaction::ScheduleTask(blink::WebIDBTaskType type, |
| 120 Operation task) { | 120 Operation task) { |
| 121 DCHECK_NE(state_, COMMITTING); | 121 DCHECK_NE(state_, COMMITTING); |
| 122 if (state_ == FINISHED) | 122 if (state_ == FINISHED) |
| 123 return; | 123 return; |
| 124 | 124 |
| 125 timeout_timer_.Stop(); | 125 timeout_timer_.Stop(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 145 // Not started by the coordinator yet. | 145 // Not started by the coordinator yet. |
| 146 if (state_ != STARTED) | 146 if (state_ != STARTED) |
| 147 return; | 147 return; |
| 148 | 148 |
| 149 // A task is already posted. | 149 // A task is already posted. |
| 150 if (should_process_queue_) | 150 if (should_process_queue_) |
| 151 return; | 151 return; |
| 152 | 152 |
| 153 should_process_queue_ = true; | 153 should_process_queue_ = true; |
| 154 base::ThreadTaskRunnerHandle::Get()->PostTask( | 154 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 155 FROM_HERE, base::Bind(&IndexedDBTransaction::ProcessTaskQueue, this)); | 155 FROM_HERE, base::Bind(&IndexedDBTransaction::ProcessTaskQueue, |
| 156 ptr_factory_.GetWeakPtr())); |
| 156 } | 157 } |
| 157 | 158 |
| 158 void IndexedDBTransaction::Abort() { | 159 void IndexedDBTransaction::Abort() { |
| 159 Abort(IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError, | 160 Abort(IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError, |
| 160 "Internal error (unknown cause)")); | 161 "Internal error (unknown cause)")); |
| 161 } | 162 } |
| 162 | 163 |
| 163 void IndexedDBTransaction::Abort(const IndexedDBDatabaseError& error) { | 164 void IndexedDBTransaction::Abort(const IndexedDBDatabaseError& error) { |
| 164 IDB_TRACE1("IndexedDBTransaction::Abort", "txn.id", id()); | 165 IDB_TRACE1("IndexedDBTransaction::Abort", "txn.id", id()); |
| 166 DCHECK(!processing_event_queue_); |
| 165 if (state_ == FINISHED) | 167 if (state_ == FINISHED) |
| 166 return; | 168 return; |
| 167 | 169 |
| 168 // The last reference to this object may be released while performing the | |
| 169 // abort steps below. We therefore take a self reference to keep ourselves | |
| 170 // alive while executing this method. | |
| 171 scoped_refptr<IndexedDBTransaction> protect(this); | |
| 172 | |
| 173 timeout_timer_.Stop(); | 170 timeout_timer_.Stop(); |
| 174 | 171 |
| 175 state_ = FINISHED; | 172 state_ = FINISHED; |
| 176 should_process_queue_ = false; | 173 should_process_queue_ = false; |
| 177 | 174 |
| 178 if (backing_store_transaction_begun_) | 175 if (backing_store_transaction_begun_) |
| 179 transaction_->Rollback(); | 176 transaction_->Rollback(); |
| 180 | 177 |
| 181 // Run the abort tasks, if any. | 178 // Run the abort tasks, if any. |
| 182 while (!abort_task_stack_.empty()) | 179 while (!abort_task_stack_.empty()) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 195 | 192 |
| 196 // Transactions must also be marked as completed before the | 193 // Transactions must also be marked as completed before the |
| 197 // front-end is notified, as the transaction completion unblocks | 194 // front-end is notified, as the transaction completion unblocks |
| 198 // operations like closing connections. | 195 // operations like closing connections. |
| 199 database_->transaction_coordinator().DidFinishTransaction(this); | 196 database_->transaction_coordinator().DidFinishTransaction(this); |
| 200 #ifndef NDEBUG | 197 #ifndef NDEBUG |
| 201 DCHECK(!database_->transaction_coordinator().IsActive(this)); | 198 DCHECK(!database_->transaction_coordinator().IsActive(this)); |
| 202 #endif | 199 #endif |
| 203 | 200 |
| 204 if (callbacks_.get()) | 201 if (callbacks_.get()) |
| 205 callbacks_->OnAbort(id_, error); | 202 callbacks_->OnAbort(*this, error); |
| 206 | 203 |
| 207 database_->TransactionFinished(this, false); | 204 database_->TransactionFinished(this, false); |
| 208 | 205 |
| 209 database_ = NULL; | 206 // EraseTransaction will delete |this|. |
| 210 connection_ = nullptr; | 207 connection_->EraseTransaction(id_); |
| 211 pending_observers_.clear(); | |
| 212 } | 208 } |
| 213 | 209 |
| 214 bool IndexedDBTransaction::IsTaskQueueEmpty() const { | 210 bool IndexedDBTransaction::IsTaskQueueEmpty() const { |
| 215 return preemptive_task_queue_.empty() && task_queue_.empty(); | 211 return preemptive_task_queue_.empty() && task_queue_.empty(); |
| 216 } | 212 } |
| 217 | 213 |
| 218 bool IndexedDBTransaction::HasPendingTasks() const { | 214 bool IndexedDBTransaction::HasPendingTasks() const { |
| 219 return pending_preemptive_events_ || !IsTaskQueueEmpty(); | 215 return pending_preemptive_events_ || !IsTaskQueueEmpty(); |
| 220 } | 216 } |
| 221 | 217 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 232 DCHECK_EQ(CREATED, state_); | 228 DCHECK_EQ(CREATED, state_); |
| 233 state_ = STARTED; | 229 state_ = STARTED; |
| 234 diagnostics_.start_time = base::Time::Now(); | 230 diagnostics_.start_time = base::Time::Now(); |
| 235 | 231 |
| 236 if (!used_) { | 232 if (!used_) { |
| 237 if (commit_pending_) { | 233 if (commit_pending_) { |
| 238 // The transaction has never had requests issued against it, but the | 234 // The transaction has never had requests issued against it, but the |
| 239 // front-end previously requested a commit; do the commit now, but not | 235 // front-end previously requested a commit; do the commit now, but not |
| 240 // re-entrantly as that may renter the coordinator. | 236 // re-entrantly as that may renter the coordinator. |
| 241 base::ThreadTaskRunnerHandle::Get()->PostTask( | 237 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 242 FROM_HERE, base::Bind(&CommitUnused, make_scoped_refptr(this))); | 238 FROM_HERE, base::Bind(&CommitUnused, ptr_factory_.GetWeakPtr())); |
| 243 } | 239 } |
| 244 return; | 240 return; |
| 245 } | 241 } |
| 246 | 242 |
| 247 RunTasksIfStarted(); | 243 RunTasksIfStarted(); |
| 248 } | 244 } |
| 249 | 245 |
| 250 class BlobWriteCallbackImpl : public IndexedDBBackingStore::BlobWriteCallback { | 246 class BlobWriteCallbackImpl : public IndexedDBBackingStore::BlobWriteCallback { |
| 251 public: | 247 public: |
| 252 explicit BlobWriteCallbackImpl( | 248 explicit BlobWriteCallbackImpl( |
| 253 scoped_refptr<IndexedDBTransaction> transaction) | 249 base::WeakPtr<IndexedDBTransaction> transaction) |
| 254 : transaction_(transaction) {} | 250 : transaction_(std::move(transaction)) {} |
| 255 void Run(bool succeeded) override { | 251 leveldb::Status Run(bool succeeded, bool sync_call) override { |
| 256 transaction_->BlobWriteComplete(succeeded); | 252 if (!transaction_) |
| 253 return leveldb::Status::OK(); |
| 254 return transaction_->BlobWriteComplete(succeeded, sync_call); |
| 257 } | 255 } |
| 258 | 256 |
| 259 protected: | 257 protected: |
| 260 ~BlobWriteCallbackImpl() override {} | 258 ~BlobWriteCallbackImpl() override {} |
| 261 | 259 |
| 262 private: | 260 private: |
| 263 scoped_refptr<IndexedDBTransaction> transaction_; | 261 base::WeakPtr<IndexedDBTransaction> transaction_; |
| 264 }; | 262 }; |
| 265 | 263 |
| 266 void IndexedDBTransaction::BlobWriteComplete(bool success) { | 264 leveldb::Status IndexedDBTransaction::BlobWriteComplete(bool success, |
| 265 bool sync_call) { |
| 267 IDB_TRACE("IndexedDBTransaction::BlobWriteComplete"); | 266 IDB_TRACE("IndexedDBTransaction::BlobWriteComplete"); |
| 268 if (state_ == FINISHED) // aborted | 267 if (state_ == FINISHED) // aborted |
| 269 return; | 268 return leveldb::Status::OK(); |
| 270 DCHECK_EQ(state_, COMMITTING); | 269 DCHECK_EQ(state_, COMMITTING); |
| 271 | 270 |
| 272 if (!success) { | 271 if (!success) { |
| 273 Abort(IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionDataError, | 272 Abort(IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionDataError, |
| 274 "Failed to write blobs.")); | 273 "Failed to write blobs.")); |
| 275 return; | 274 return leveldb::Status::OK(); |
| 276 } | 275 } |
| 277 // Save the database as |this| can be destroyed in the next line. | 276 // Save the database as |this| can be destroyed in the next line. We also make |
| 277 // sure to handle the error if we're not being called synchronously. |
| 278 scoped_refptr<IndexedDBDatabase> database = database_; | 278 scoped_refptr<IndexedDBDatabase> database = database_; |
| 279 leveldb::Status s = CommitPhaseTwo(); | 279 leveldb::Status s = CommitPhaseTwo(); |
| 280 if (!s.ok()) | 280 if (!s.ok() && !sync_call) |
| 281 ReportError(s, database, database->factory()); | 281 ReportError(s, database, database->factory()); |
| 282 return s; |
| 282 } | 283 } |
| 283 | 284 |
| 284 leveldb::Status IndexedDBTransaction::Commit() { | 285 leveldb::Status IndexedDBTransaction::Commit() { |
| 285 IDB_TRACE1("IndexedDBTransaction::Commit", "txn.id", id()); | 286 IDB_TRACE1("IndexedDBTransaction::Commit", "txn.id", id()); |
| 286 | 287 |
| 287 timeout_timer_.Stop(); | 288 timeout_timer_.Stop(); |
| 288 | 289 |
| 289 // In multiprocess ports, front-end may have requested a commit but | 290 // In multiprocess ports, front-end may have requested a commit but |
| 290 // an abort has already been initiated asynchronously by the | 291 // an abort has already been initiated asynchronously by the |
| 291 // back-end. | 292 // back-end. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 308 if (HasPendingTasks()) | 309 if (HasPendingTasks()) |
| 309 return leveldb::Status::OK(); | 310 return leveldb::Status::OK(); |
| 310 | 311 |
| 311 state_ = COMMITTING; | 312 state_ = COMMITTING; |
| 312 | 313 |
| 313 leveldb::Status s; | 314 leveldb::Status s; |
| 314 if (!used_) { | 315 if (!used_) { |
| 315 s = CommitPhaseTwo(); | 316 s = CommitPhaseTwo(); |
| 316 } else { | 317 } else { |
| 317 scoped_refptr<IndexedDBBackingStore::BlobWriteCallback> callback( | 318 scoped_refptr<IndexedDBBackingStore::BlobWriteCallback> callback( |
| 318 new BlobWriteCallbackImpl(this)); | 319 new BlobWriteCallbackImpl(ptr_factory_.GetWeakPtr())); |
| 319 // CommitPhaseOne will call the callback synchronously if there are no blobs | 320 // CommitPhaseOne will call the callback synchronously if there are no blobs |
| 320 // to write. | 321 // to write. |
| 321 s = transaction_->CommitPhaseOne(callback); | 322 s = transaction_->CommitPhaseOne(callback); |
| 322 if (!s.ok()) | |
| 323 Abort(IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionDataError, | |
| 324 "Error processing blob journal.")); | |
| 325 } | 323 } |
| 326 | 324 |
| 327 return s; | 325 return s; |
| 328 } | 326 } |
| 329 | 327 |
| 330 leveldb::Status IndexedDBTransaction::CommitPhaseTwo() { | 328 leveldb::Status IndexedDBTransaction::CommitPhaseTwo() { |
| 331 // Abort may have been called just as the blob write completed. | 329 // Abort may have been called just as the blob write completed. |
| 332 if (state_ == FINISHED) | 330 if (state_ == FINISHED) |
| 333 return leveldb::Status::OK(); | 331 return leveldb::Status::OK(); |
| 334 | 332 |
| 335 DCHECK_EQ(state_, COMMITTING); | 333 DCHECK_EQ(state_, COMMITTING); |
| 336 | 334 |
| 337 // The last reference to this object may be released while performing the | |
| 338 // commit steps below. We therefore take a self reference to keep ourselves | |
| 339 // alive while executing this method. | |
| 340 scoped_refptr<IndexedDBTransaction> protect(this); | |
| 341 | |
| 342 state_ = FINISHED; | 335 state_ = FINISHED; |
| 343 | 336 |
| 344 leveldb::Status s; | 337 leveldb::Status s; |
| 345 bool committed; | 338 bool committed; |
| 346 if (!used_) { | 339 if (!used_) { |
| 347 committed = true; | 340 committed = true; |
| 348 } else { | 341 } else { |
| 349 s = transaction_->CommitPhaseTwo(); | 342 s = transaction_->CommitPhaseTwo(); |
| 350 committed = s.ok(); | 343 committed = s.ok(); |
| 351 } | 344 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 368 // SendObservations must be called before OnComplete to ensure consistency | 361 // SendObservations must be called before OnComplete to ensure consistency |
| 369 // of callbacks at renderer. | 362 // of callbacks at renderer. |
| 370 if (!connection_changes_map_.empty()) { | 363 if (!connection_changes_map_.empty()) { |
| 371 database_->SendObservations(std::move(connection_changes_map_)); | 364 database_->SendObservations(std::move(connection_changes_map_)); |
| 372 connection_changes_map_.clear(); | 365 connection_changes_map_.clear(); |
| 373 } | 366 } |
| 374 { | 367 { |
| 375 IDB_TRACE1( | 368 IDB_TRACE1( |
| 376 "IndexedDBTransaction::CommitPhaseTwo.TransactionCompleteCallbacks", | 369 "IndexedDBTransaction::CommitPhaseTwo.TransactionCompleteCallbacks", |
| 377 "txn.id", id()); | 370 "txn.id", id()); |
| 378 callbacks_->OnComplete(id_); | 371 callbacks_->OnComplete(*this); |
| 379 } | 372 } |
| 380 if (!pending_observers_.empty() && connection_) { | 373 if (!pending_observers_.empty() && connection_) { |
| 381 connection_->ActivatePendingObservers(std::move(pending_observers_)); | 374 connection_->ActivatePendingObservers(std::move(pending_observers_)); |
| 382 pending_observers_.clear(); | 375 pending_observers_.clear(); |
| 383 } | 376 } |
| 384 | 377 |
| 385 database_->TransactionFinished(this, true); | 378 database_->TransactionFinished(this, true); |
| 379 // EraseTransaction will delete |this|. |
| 380 connection_->EraseTransaction(id_); |
| 381 return s; |
| 386 } else { | 382 } else { |
| 387 while (!abort_task_stack_.empty()) | 383 while (!abort_task_stack_.empty()) |
| 388 abort_task_stack_.pop().Run(); | 384 abort_task_stack_.pop().Run(); |
| 389 | 385 |
| 390 IndexedDBDatabaseError error; | 386 IndexedDBDatabaseError error; |
| 391 if (leveldb_env::IndicatesDiskFull(s)) { | 387 if (leveldb_env::IndicatesDiskFull(s)) { |
| 392 error = IndexedDBDatabaseError( | 388 error = IndexedDBDatabaseError( |
| 393 blink::WebIDBDatabaseExceptionQuotaError, | 389 blink::WebIDBDatabaseExceptionQuotaError, |
| 394 "Encountered disk full while committing transaction."); | 390 "Encountered disk full while committing transaction."); |
| 395 } else { | 391 } else { |
| 396 error = IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError, | 392 error = IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError, |
| 397 "Internal error committing transaction."); | 393 "Internal error committing transaction."); |
| 398 } | 394 } |
| 399 callbacks_->OnAbort(id_, error); | 395 callbacks_->OnAbort(*this, error); |
| 400 | |
| 401 database_->TransactionFinished(this, false); | 396 database_->TransactionFinished(this, false); |
| 402 } | 397 } |
| 403 | |
| 404 database_ = NULL; | |
| 405 return s; | 398 return s; |
| 406 } | 399 } |
| 407 | 400 |
| 408 void IndexedDBTransaction::ProcessTaskQueue() { | 401 void IndexedDBTransaction::ProcessTaskQueue() { |
| 409 IDB_TRACE1("IndexedDBTransaction::ProcessTaskQueue", "txn.id", id()); | 402 IDB_TRACE1("IndexedDBTransaction::ProcessTaskQueue", "txn.id", id()); |
| 410 | 403 |
| 411 DCHECK(!processing_event_queue_); | 404 DCHECK(!processing_event_queue_); |
| 412 | 405 |
| 413 // May have been aborted. | 406 // May have been aborted. |
| 414 if (!should_process_queue_) | 407 if (!should_process_queue_) |
| 415 return; | 408 return; |
| 416 | 409 |
| 417 processing_event_queue_ = true; | 410 processing_event_queue_ = true; |
| 418 | 411 |
| 419 DCHECK(!IsTaskQueueEmpty()); | 412 DCHECK(!IsTaskQueueEmpty()); |
| 420 should_process_queue_ = false; | 413 should_process_queue_ = false; |
| 421 | 414 |
| 422 if (!backing_store_transaction_begun_) { | 415 if (!backing_store_transaction_begun_) { |
| 423 transaction_->Begin(); | 416 transaction_->Begin(); |
| 424 backing_store_transaction_begun_ = true; | 417 backing_store_transaction_begun_ = true; |
| 425 } | 418 } |
| 426 | 419 |
| 427 // The last reference to this object may be released while performing the | |
| 428 // tasks. Take take a self reference to keep this object alive so that | |
| 429 // the loop termination conditions can be checked. | |
| 430 scoped_refptr<IndexedDBTransaction> protect(this); | |
| 431 | |
| 432 TaskQueue* task_queue = | 420 TaskQueue* task_queue = |
| 433 pending_preemptive_events_ ? &preemptive_task_queue_ : &task_queue_; | 421 pending_preemptive_events_ ? &preemptive_task_queue_ : &task_queue_; |
| 434 while (!task_queue->empty() && state_ != FINISHED) { | 422 while (!task_queue->empty() && state_ != FINISHED) { |
| 435 DCHECK_EQ(state_, STARTED); | 423 DCHECK_EQ(state_, STARTED); |
| 436 Operation task(task_queue->pop()); | 424 Operation task(task_queue->pop()); |
| 437 leveldb::Status result = task.Run(this); | 425 leveldb::Status result = task.Run(this); |
| 438 if (!pending_preemptive_events_) { | 426 if (!pending_preemptive_events_) { |
| 439 DCHECK(diagnostics_.tasks_completed < diagnostics_.tasks_scheduled); | 427 DCHECK(diagnostics_.tasks_completed < diagnostics_.tasks_scheduled); |
| 440 ++diagnostics_.tasks_completed; | 428 ++diagnostics_.tasks_completed; |
| 441 } | 429 } |
| 442 if (!result.ok()) { | 430 if (!result.ok()) { |
| 443 processing_event_queue_ = false; | 431 processing_event_queue_ = false; |
| 444 if (!result.ok()) | 432 ReportError(result, database_, database_->factory()); |
| 445 ReportError(result, database_, database_->factory()); | |
| 446 return; | 433 return; |
| 447 } | 434 } |
| 448 | 435 |
| 449 // Event itself may change which queue should be processed next. | 436 // Event itself may change which queue should be processed next. |
| 450 task_queue = | 437 task_queue = |
| 451 pending_preemptive_events_ ? &preemptive_task_queue_ : &task_queue_; | 438 pending_preemptive_events_ ? &preemptive_task_queue_ : &task_queue_; |
| 452 } | 439 } |
| 453 | 440 |
| 454 // If there are no pending tasks, we haven't already committed/aborted, | 441 // If there are no pending tasks, we haven't already committed/aborted, |
| 455 // and the front-end requested a commit, it is now safe to do so. | 442 // and the front-end requested a commit, it is now safe to do so. |
| 456 if (!HasPendingTasks() && state_ != FINISHED && commit_pending_) { | 443 if (!HasPendingTasks() && state_ != FINISHED && commit_pending_) { |
| 444 processing_event_queue_ = false; |
| 445 // This can delete |this|. |
| 457 leveldb::Status result = Commit(); | 446 leveldb::Status result = Commit(); |
| 458 processing_event_queue_ = false; | |
| 459 if (!result.ok()) | 447 if (!result.ok()) |
| 460 ReportError(result, database_, database_->factory()); | 448 ReportError(result, database_, database_->factory()); |
| 461 return; | 449 return; |
| 462 } | 450 } |
| 463 | 451 |
| 464 // The transaction may have been aborted while processing tasks. | 452 // The transaction may have been aborted while processing tasks. |
| 465 if (state_ == FINISHED) { | 453 if (state_ == FINISHED) { |
| 466 processing_event_queue_ = false; | 454 processing_event_queue_ = false; |
| 467 return; | 455 return; |
| 468 } | 456 } |
| 469 | 457 |
| 470 DCHECK(state_ == STARTED); | 458 DCHECK(state_ == STARTED); |
| 471 | 459 |
| 472 // Otherwise, start a timer in case the front-end gets wedged and | 460 // Otherwise, start a timer in case the front-end gets wedged and |
| 473 // never requests further activity. Read-only transactions don't | 461 // never requests further activity. Read-only transactions don't |
| 474 // block other transactions, so don't time those out. | 462 // block other transactions, so don't time those out. |
| 475 if (mode_ != blink::WebIDBTransactionModeReadOnly) { | 463 if (mode_ != blink::WebIDBTransactionModeReadOnly) { |
| 476 timeout_timer_.Start(FROM_HERE, GetInactivityTimeout(), | 464 timeout_timer_.Start( |
| 477 base::Bind(&IndexedDBTransaction::Timeout, this)); | 465 FROM_HERE, GetInactivityTimeout(), |
| 466 base::Bind(&IndexedDBTransaction::Timeout, ptr_factory_.GetWeakPtr())); |
| 478 } | 467 } |
| 479 processing_event_queue_ = false; | 468 processing_event_queue_ = false; |
| 480 } | 469 } |
| 481 | 470 |
| 482 base::TimeDelta IndexedDBTransaction::GetInactivityTimeout() const { | 471 base::TimeDelta IndexedDBTransaction::GetInactivityTimeout() const { |
| 483 return base::TimeDelta::FromSeconds(kInactivityTimeoutPeriodSeconds); | 472 return base::TimeDelta::FromSeconds(kInactivityTimeoutPeriodSeconds); |
| 484 } | 473 } |
| 485 | 474 |
| 486 void IndexedDBTransaction::Timeout() { | 475 void IndexedDBTransaction::Timeout() { |
| 487 Abort(IndexedDBDatabaseError( | 476 Abort(IndexedDBDatabaseError( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 void IndexedDBTransaction::RecordObserverForLastObservation( | 519 void IndexedDBTransaction::RecordObserverForLastObservation( |
| 531 int32_t connection_id, | 520 int32_t connection_id, |
| 532 int32_t observer_id) { | 521 int32_t observer_id) { |
| 533 auto it = connection_changes_map_.find(connection_id); | 522 auto it = connection_changes_map_.find(connection_id); |
| 534 DCHECK(it != connection_changes_map_.end()); | 523 DCHECK(it != connection_changes_map_.end()); |
| 535 it->second->observation_index_map[observer_id].push_back( | 524 it->second->observation_index_map[observer_id].push_back( |
| 536 it->second->observations.size() - 1); | 525 it->second->observations.size() - 1); |
| 537 } | 526 } |
| 538 | 527 |
| 539 } // namespace content | 528 } // namespace content |
| OLD | NEW |