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

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

Issue 2160163002: [IndexedDB] Propogating Changes to Observer : Browser (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Browser changes Created 4 years, 5 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
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 #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/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
12 #include "base/stl_util.h"
11 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
12 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
13 #include "content/browser/indexed_db/indexed_db_backing_store.h" 15 #include "content/browser/indexed_db/indexed_db_backing_store.h"
14 #include "content/browser/indexed_db/indexed_db_cursor.h" 16 #include "content/browser/indexed_db/indexed_db_cursor.h"
15 #include "content/browser/indexed_db/indexed_db_database.h" 17 #include "content/browser/indexed_db/indexed_db_database.h"
16 #include "content/browser/indexed_db/indexed_db_database_callbacks.h" 18 #include "content/browser/indexed_db/indexed_db_database_callbacks.h"
19 #include "content/browser/indexed_db/indexed_db_observation.h"
20 #include "content/browser/indexed_db/indexed_db_observer_changes.h"
17 #include "content/browser/indexed_db/indexed_db_tracing.h" 21 #include "content/browser/indexed_db/indexed_db_tracing.h"
18 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h" 22 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h"
19 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc eption.h" 23 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc eption.h"
20 #include "third_party/leveldatabase/env_chromium.h" 24 #include "third_party/leveldatabase/env_chromium.h"
21 25
22 namespace content { 26 namespace content {
23 27
24 namespace { 28 namespace {
25 29
26 const int64_t kInactivityTimeoutPeriodSeconds = 60; 30 const int64_t kInactivityTimeoutPeriodSeconds = 60;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 63
60 IndexedDBTransaction::Operation IndexedDBTransaction::TaskStack::pop() { 64 IndexedDBTransaction::Operation IndexedDBTransaction::TaskStack::pop() {
61 DCHECK(!stack_.empty()); 65 DCHECK(!stack_.empty());
62 Operation task(stack_.top()); 66 Operation task(stack_.top());
63 stack_.pop(); 67 stack_.pop();
64 return task; 68 return task;
65 } 69 }
66 70
67 IndexedDBTransaction::IndexedDBTransaction( 71 IndexedDBTransaction::IndexedDBTransaction(
68 int64_t id, 72 int64_t id,
69 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks, 73 base::WeakPtr<IndexedDBConnection> connection,
70 const std::set<int64_t>& object_store_ids, 74 const std::set<int64_t>& object_store_ids,
71 blink::WebIDBTransactionMode mode, 75 blink::WebIDBTransactionMode mode,
72 IndexedDBDatabase* database,
73 IndexedDBBackingStore::Transaction* backing_store_transaction) 76 IndexedDBBackingStore::Transaction* backing_store_transaction)
74 : id_(id), 77 : id_(id),
75 object_store_ids_(object_store_ids), 78 object_store_ids_(object_store_ids),
76 mode_(mode), 79 mode_(mode),
77 used_(false), 80 used_(false),
78 state_(CREATED), 81 state_(CREATED),
79 commit_pending_(false), 82 commit_pending_(false),
80 callbacks_(callbacks), 83 connection_(std::move(connection)),
81 database_(database),
82 transaction_(backing_store_transaction), 84 transaction_(backing_store_transaction),
83 backing_store_transaction_begun_(false), 85 backing_store_transaction_begun_(false),
84 should_process_queue_(false), 86 should_process_queue_(false),
85 pending_preemptive_events_(0) { 87 pending_preemptive_events_(0) {
88 callbacks_ = connection_->callbacks();
89 database_ = connection_->database();
90
86 database_->transaction_coordinator().DidCreateTransaction(this); 91 database_->transaction_coordinator().DidCreateTransaction(this);
87 92
88 diagnostics_.tasks_scheduled = 0; 93 diagnostics_.tasks_scheduled = 0;
89 diagnostics_.tasks_completed = 0; 94 diagnostics_.tasks_completed = 0;
90 diagnostics_.creation_time = base::Time::Now(); 95 diagnostics_.creation_time = base::Time::Now();
91 } 96 }
92 97
93 IndexedDBTransaction::~IndexedDBTransaction() { 98 IndexedDBTransaction::~IndexedDBTransaction() {
94 // It shouldn't be possible for this object to get deleted until it's either 99 // It shouldn't be possible for this object to get deleted until it's either
95 // complete or aborted. 100 // complete or aborted.
96 DCHECK_EQ(state_, FINISHED); 101 DCHECK_EQ(state_, FINISHED);
97 DCHECK(preemptive_task_queue_.empty()); 102 DCHECK(preemptive_task_queue_.empty());
98 DCHECK_EQ(pending_preemptive_events_, 0); 103 DCHECK_EQ(pending_preemptive_events_, 0);
99 DCHECK(task_queue_.empty()); 104 DCHECK(task_queue_.empty());
100 DCHECK(abort_task_stack_.empty()); 105 DCHECK(abort_task_stack_.empty());
106 DCHECK(pending_observers_.empty());
101 } 107 }
102 108
103 void IndexedDBTransaction::ScheduleTask(blink::WebIDBTaskType type, 109 void IndexedDBTransaction::ScheduleTask(blink::WebIDBTaskType type,
104 Operation task) { 110 Operation task) {
105 DCHECK_NE(state_, COMMITTING); 111 DCHECK_NE(state_, COMMITTING);
106 if (state_ == FINISHED) 112 if (state_ == FINISHED)
107 return; 113 return;
108 114
109 timeout_timer_.Stop(); 115 timeout_timer_.Stop();
110 used_ = true; 116 used_ = true;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 #ifndef NDEBUG 190 #ifndef NDEBUG
185 DCHECK(!database_->transaction_coordinator().IsActive(this)); 191 DCHECK(!database_->transaction_coordinator().IsActive(this));
186 #endif 192 #endif
187 193
188 if (callbacks_.get()) 194 if (callbacks_.get())
189 callbacks_->OnAbort(id_, error); 195 callbacks_->OnAbort(id_, error);
190 196
191 database_->TransactionFinished(this, false); 197 database_->TransactionFinished(this, false);
192 198
193 database_ = NULL; 199 database_ = NULL;
200 connection_ = nullptr;
201 pending_observers_.clear();
194 } 202 }
195 203
196 bool IndexedDBTransaction::IsTaskQueueEmpty() const { 204 bool IndexedDBTransaction::IsTaskQueueEmpty() const {
197 return preemptive_task_queue_.empty() && task_queue_.empty(); 205 return preemptive_task_queue_.empty() && task_queue_.empty();
198 } 206 }
199 207
200 bool IndexedDBTransaction::HasPendingTasks() const { 208 bool IndexedDBTransaction::HasPendingTasks() const {
201 return pending_preemptive_events_ || !IsTaskQueueEmpty(); 209 return pending_preemptive_events_ || !IsTaskQueueEmpty();
202 } 210 }
203 211
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 CloseOpenCursors(); 341 CloseOpenCursors();
334 transaction_->Reset(); 342 transaction_->Reset();
335 343
336 // Transactions must also be marked as completed before the 344 // Transactions must also be marked as completed before the
337 // front-end is notified, as the transaction completion unblocks 345 // front-end is notified, as the transaction completion unblocks
338 // operations like closing connections. 346 // operations like closing connections.
339 database_->transaction_coordinator().DidFinishTransaction(this); 347 database_->transaction_coordinator().DidFinishTransaction(this);
340 348
341 if (committed) { 349 if (committed) {
342 abort_task_stack_.clear(); 350 abort_task_stack_.clear();
351
352 // SendObservations must be called before OnComplete to ensure consistency
353 // of callbacks at renderer.
354 if (!connection_changes_map_.empty()) {
355 database_->SendObservations(std::move(connection_changes_map_));
356 connection_changes_map_.clear();
357 }
343 { 358 {
344 IDB_TRACE1( 359 IDB_TRACE1(
345 "IndexedDBTransaction::CommitPhaseTwo.TransactionCompleteCallbacks", 360 "IndexedDBTransaction::CommitPhaseTwo.TransactionCompleteCallbacks",
346 "txn.id", id()); 361 "txn.id", id());
347 callbacks_->OnComplete(id_); 362 callbacks_->OnComplete(id_);
348 } 363 }
364 if (!pending_observers_.empty() && connection_) {
365 connection_->ActivatePendingObservers(std::move(pending_observers_));
366 pending_observers_.clear();
367 }
368
349 database_->TransactionFinished(this, true); 369 database_->TransactionFinished(this, true);
350 } else { 370 } else {
351 while (!abort_task_stack_.empty()) 371 while (!abort_task_stack_.empty())
352 abort_task_stack_.pop().Run(NULL); 372 abort_task_stack_.pop().Run(NULL);
353 373
354 IndexedDBDatabaseError error; 374 IndexedDBDatabaseError error;
355 if (leveldb_env::IndicatesDiskFull(s)) { 375 if (leveldb_env::IndicatesDiskFull(s)) {
356 error = IndexedDBDatabaseError( 376 error = IndexedDBDatabaseError(
357 blink::WebIDBDatabaseExceptionQuotaError, 377 blink::WebIDBDatabaseExceptionQuotaError,
358 "Encountered disk full while committing transaction."); 378 "Encountered disk full while committing transaction.");
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 base::ASCIIToUTF16("Transaction timed out due to inactivity."))); 458 base::ASCIIToUTF16("Transaction timed out due to inactivity.")));
439 } 459 }
440 460
441 void IndexedDBTransaction::CloseOpenCursors() { 461 void IndexedDBTransaction::CloseOpenCursors() {
442 IDB_TRACE1("IndexedDBTransaction::CloseOpenCursors", "txn.id", id()); 462 IDB_TRACE1("IndexedDBTransaction::CloseOpenCursors", "txn.id", id());
443 for (auto* cursor : open_cursors_) 463 for (auto* cursor : open_cursors_)
444 cursor->Close(); 464 cursor->Close();
445 open_cursors_.clear(); 465 open_cursors_.clear();
446 } 466 }
447 467
468 void IndexedDBTransaction::AddPendingObserver(
469 int32_t observer_id,
470 IndexedDBObserver::Options options) {
471 pending_observers_.push_back(base::WrapUnique(
472 new IndexedDBObserver(observer_id, object_store_ids_, options)));
473 }
474
475 void IndexedDBTransaction::RemovePendingObservers(
476 const std::vector<int32_t>& pending_observer_ids) {
477 const auto& it = std::remove_if(
478 pending_observers_.begin(), pending_observers_.end(),
479 [&pending_observer_ids](const std::unique_ptr<IndexedDBObserver>& o) {
480 return ContainsValue(pending_observer_ids, o->id());
481 });
482 if (it != pending_observers_.end())
483 pending_observers_.erase(it, pending_observers_.end());
484 }
485
486 void IndexedDBTransaction::AddObservation(
487 int32_t connection_id,
488 std::unique_ptr<IndexedDBObservation> observation) {
489 auto it = connection_changes_map_.find(connection_id);
490 if (it == connection_changes_map_.end()) {
491 it = connection_changes_map_
492 .insert(std::make_pair(
493 connection_id,
494 base::WrapUnique(new IndexedDBObserverChanges())))
495 .first;
496 }
497 it->second->AddObservation(std::move(observation));
498 }
499
500 void IndexedDBTransaction::RecordObserverForLastObservation(
501 int32_t connection_id,
502 int32_t observer_id) {
503 auto it = connection_changes_map_.find(connection_id);
504 DCHECK(it != connection_changes_map_.end());
505 it->second->RecordObserverForLastObservation(observer_id);
506 }
507
448 } // namespace content 508 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698