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

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

Issue 2062203004: IDBObserver: Lifetime Management: Adding Observer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: post dmuprh review Created 4 years, 6 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 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_connection.h" 5 #include "content/browser/indexed_db/indexed_db_connection.h"
6 6
7 namespace content { 7 namespace content {
8 8
9 IndexedDBConnection::IndexedDBConnection( 9 IndexedDBConnection::IndexedDBConnection(
10 scoped_refptr<IndexedDBDatabase> database, 10 scoped_refptr<IndexedDBDatabase> database,
11 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks) 11 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks)
12 : database_(database), callbacks_(callbacks), weak_factory_(this) {} 12 : database_(database), callbacks_(callbacks), weak_factory_(this) {}
13 13
14 IndexedDBConnection::~IndexedDBConnection() {} 14 IndexedDBConnection::~IndexedDBConnection() {}
15 15
16 void IndexedDBConnection::Close() { 16 void IndexedDBConnection::Close() {
17 if (!callbacks_.get()) 17 if (!callbacks_.get())
18 return; 18 return;
19 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr(); 19 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr();
20 database_->Close(this, false /* forced */); 20 database_->Close(this, false /* forced */);
21 if (this_obj) { 21 if (this_obj) {
22 database_ = nullptr; 22 database_ = nullptr;
23 callbacks_ = nullptr; 23 callbacks_ = nullptr;
24 active_observers_.clear();
24 } 25 }
25 } 26 }
26 27
27 void IndexedDBConnection::ForceClose() { 28 void IndexedDBConnection::ForceClose() {
28 if (!callbacks_.get()) 29 if (!callbacks_.get())
29 return; 30 return;
30 31
31 // IndexedDBDatabase::Close() can delete this instance. 32 // IndexedDBDatabase::Close() can delete this instance.
32 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr(); 33 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr();
33 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks(callbacks_); 34 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks(callbacks_);
34 database_->Close(this, true /* forced */); 35 database_->Close(this, true /* forced */);
35 if (this_obj) { 36 if (this_obj) {
36 database_ = nullptr; 37 database_ = nullptr;
37 callbacks_ = nullptr; 38 callbacks_ = nullptr;
39 active_observers_.clear();
38 } 40 }
39 callbacks->OnForcedClose(); 41 callbacks->OnForcedClose();
40 } 42 }
41 43
42 void IndexedDBConnection::VersionChangeIgnored() { 44 void IndexedDBConnection::VersionChangeIgnored() {
43 if (!database_.get()) 45 if (!database_.get())
44 return; 46 return;
45 database_->VersionChangeIgnored(); 47 database_->VersionChangeIgnored();
46 } 48 }
47 49
48 bool IndexedDBConnection::IsConnected() { 50 bool IndexedDBConnection::IsConnected() {
49 return database_.get() != NULL; 51 return database_.get() != NULL;
50 } 52 }
51 53
54 void IndexedDBConnection::ActivatePendingObservers(
55 std::vector<std::unique_ptr<IndexedDBObserver>>* pending_observers) {
56 for (uint32_t i = 0; i < pending_observers->size(); i++) {
57 active_observers_.emplace_back(std::move(pending_observers->at(i)));
dmurph 2016/06/23 21:53:04 I believe we just want push_back here. emplace_bac
palakj1 2016/06/24 00:03:00 Changed.
58 }
59 }
60
61 void IndexedDBConnection::RemoveObservers(
62 const std::vector<int32_t>& remove_observer_ids) {
63 // TODO(palakj): Change from vector to set or create tmp vector instead of
64 // erase.
65 std::vector<int32_t> pending_observer_ids;
66 for (uint32_t i = 0; i < remove_observer_ids.size(); i++) {
67 bool removed = false;
68 for (uint32_t j = 0; j < active_observers_.size(); j++) {
69 if (active_observers_[j]->id() == remove_observer_ids[i]) {
70 active_observers_.erase(active_observers_.begin() + j);
dmurph 2016/06/23 21:53:04 In order for us to be able to erase while iteratin
palakj1 2016/06/24 00:03:00 After each erase, we break. So the state of iterat
71 removed = true;
72 break;
73 }
74 }
75 // If observer not in active_observers_ , must be in pending_observer list
76 // of one of the transactions.
77 if (!removed)
78 pending_observer_ids.push_back(remove_observer_ids[i]);
79 }
80
81 if (!pending_observer_ids.empty())
82 database_->RemovePendingObservers(this, pending_observer_ids);
83 }
84
52 } // namespace content 85 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698