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

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 dmurph 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 #include "base/stl_util.h"
8
7 namespace content { 9 namespace content {
8 10
9 IndexedDBConnection::IndexedDBConnection( 11 IndexedDBConnection::IndexedDBConnection(
10 scoped_refptr<IndexedDBDatabase> database, 12 scoped_refptr<IndexedDBDatabase> database,
11 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks) 13 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks)
12 : database_(database), callbacks_(callbacks), weak_factory_(this) {} 14 : database_(database), callbacks_(callbacks), weak_factory_(this) {}
13 15
14 IndexedDBConnection::~IndexedDBConnection() {} 16 IndexedDBConnection::~IndexedDBConnection() {}
15 17
16 void IndexedDBConnection::Close() { 18 void IndexedDBConnection::Close() {
17 if (!callbacks_.get()) 19 if (!callbacks_.get())
18 return; 20 return;
19 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr(); 21 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr();
20 database_->Close(this, false /* forced */); 22 database_->Close(this, false /* forced */);
21 if (this_obj) { 23 if (this_obj) {
22 database_ = nullptr; 24 database_ = nullptr;
23 callbacks_ = nullptr; 25 callbacks_ = nullptr;
26 active_observers_.clear();
24 } 27 }
25 } 28 }
26 29
27 void IndexedDBConnection::ForceClose() { 30 void IndexedDBConnection::ForceClose() {
28 if (!callbacks_.get()) 31 if (!callbacks_.get())
29 return; 32 return;
30 33
31 // IndexedDBDatabase::Close() can delete this instance. 34 // IndexedDBDatabase::Close() can delete this instance.
32 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr(); 35 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr();
33 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks(callbacks_); 36 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks(callbacks_);
34 database_->Close(this, true /* forced */); 37 database_->Close(this, true /* forced */);
35 if (this_obj) { 38 if (this_obj) {
36 database_ = nullptr; 39 database_ = nullptr;
37 callbacks_ = nullptr; 40 callbacks_ = nullptr;
41 active_observers_.clear();
38 } 42 }
39 callbacks->OnForcedClose(); 43 callbacks->OnForcedClose();
40 } 44 }
41 45
42 void IndexedDBConnection::VersionChangeIgnored() { 46 void IndexedDBConnection::VersionChangeIgnored() {
43 if (!database_.get()) 47 if (!database_.get())
44 return; 48 return;
45 database_->VersionChangeIgnored(); 49 database_->VersionChangeIgnored();
46 } 50 }
47 51
48 bool IndexedDBConnection::IsConnected() { 52 bool IndexedDBConnection::IsConnected() {
49 return database_.get() != NULL; 53 return database_.get() != NULL;
50 } 54 }
51 55
56 void IndexedDBConnection::ActivatePendingObservers(
57 std::vector<std::unique_ptr<IndexedDBObserver>>* pending_observers) {
58 for (uint32_t i = 0; i < pending_observers->size(); i++) {
59 active_observers_.push_back(std::move(pending_observers->at(i)));
Marijn Kruisselbrink 2016/06/24 00:48:08 I think it's a bit of a weird API that ActivatePen
palakj1 2016/06/27 20:19:22 For the second option, doesn't the chromium conven
60 }
61 }
62
63 void IndexedDBConnection::RemoveObservers(
64 const std::vector<int32_t>& observer_ids_to_remove) {
65 // TODO(palakj): Change from vector to set or create tmp vector instead of
66 // erase.
67 std::vector<int32_t> pending_observer_ids;
68 for (size_t i = 0; i < observer_ids_to_remove.size(); i++) {
Marijn Kruisselbrink 2016/06/24 00:48:08 nit: You're not really using the index |i|, so you
palakj1 2016/06/27 20:19:22 Done.
69 bool removed = false;
70 for (size_t j = 0; j < active_observers_.size(); j++) {
Marijn Kruisselbrink 2016/06/24 00:48:08 You could rewrite the body of the outer loop as (u
palakj1 2016/06/27 20:19:22 this is nice. But, I'm not really comfortable with
71 if (active_observers_[j]->id() == observer_ids_to_remove[i]) {
72 active_observers_.erase(active_observers_.begin() + j);
73 removed = true;
74 break;
75 }
76 }
77 // If observer not in active_observers_ , must be in pending_observer list
78 // of one of the transactions.
79 if (!removed) {
80 pending_observer_ids.push_back(observer_ids_to_remove[i]);
81 }
82 }
83 if (!pending_observer_ids.empty())
84 database_->RemovePendingObservers(this, pending_observer_ids);
85 }
86
52 } // namespace content 87 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698