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

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: Final architechture 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 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/logging.h"
8 #include "base/stl_util.h"
9
7 namespace content { 10 namespace content {
8 11
9 IndexedDBConnection::IndexedDBConnection( 12 IndexedDBConnection::IndexedDBConnection(
10 scoped_refptr<IndexedDBDatabase> database, 13 scoped_refptr<IndexedDBDatabase> database,
11 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks) 14 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks)
12 : database_(database), callbacks_(callbacks), weak_factory_(this) {} 15 : database_(database), callbacks_(callbacks), weak_factory_(this) {}
13 16
14 IndexedDBConnection::~IndexedDBConnection() {} 17 IndexedDBConnection::~IndexedDBConnection() {}
15 18
16 void IndexedDBConnection::Close() { 19 void IndexedDBConnection::Close() {
17 if (!callbacks_.get()) 20 if (!callbacks_.get())
18 return; 21 return;
19 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr(); 22 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr();
20 database_->Close(this, false /* forced */); 23 database_->Close(this, false /* forced */);
21 if (this_obj) { 24 if (this_obj) {
22 database_ = nullptr; 25 database_ = nullptr;
23 callbacks_ = nullptr; 26 callbacks_ = nullptr;
27 active_observers_.clear();
24 } 28 }
25 } 29 }
26 30
27 void IndexedDBConnection::ForceClose() { 31 void IndexedDBConnection::ForceClose() {
28 if (!callbacks_.get()) 32 if (!callbacks_.get())
29 return; 33 return;
30 34
31 // IndexedDBDatabase::Close() can delete this instance. 35 // IndexedDBDatabase::Close() can delete this instance.
32 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr(); 36 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr();
33 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks(callbacks_); 37 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks(callbacks_);
34 database_->Close(this, true /* forced */); 38 database_->Close(this, true /* forced */);
35 if (this_obj) { 39 if (this_obj) {
36 database_ = nullptr; 40 database_ = nullptr;
37 callbacks_ = nullptr; 41 callbacks_ = nullptr;
42 active_observers_.clear();
38 } 43 }
39 callbacks->OnForcedClose(); 44 callbacks->OnForcedClose();
40 } 45 }
41 46
42 void IndexedDBConnection::VersionChangeIgnored() { 47 void IndexedDBConnection::VersionChangeIgnored() {
43 if (!database_.get()) 48 if (!database_.get())
44 return; 49 return;
45 database_->VersionChangeIgnored(); 50 database_->VersionChangeIgnored();
46 } 51 }
47 52
48 bool IndexedDBConnection::IsConnected() { 53 bool IndexedDBConnection::IsConnected() {
49 return database_.get() != NULL; 54 return database_.get() != NULL;
50 } 55 }
51 56
57 // The observers begin listening to changes only once they are activated.
58 void IndexedDBConnection::ActivatePendingObservers(
59 std::vector<std::unique_ptr<IndexedDBObserver>> pending_observers) {
60 for (auto& observer : pending_observers) {
61 active_observers_.push_back(std::move(observer));
62 }
63 pending_observers.clear();
64 }
65
66 void IndexedDBConnection::RemoveObservers(
67 const std::vector<int32_t>& observer_ids_to_remove) {
68 std::vector<int32_t> pending_observer_ids;
69 for (int32_t id_to_remove : observer_ids_to_remove) {
70 const auto& it = std::find_if(
71 active_observers_.begin(), active_observers_.end(),
72 [&id_to_remove](const std::unique_ptr<IndexedDBObserver>& o) {
73 return o->id() == id_to_remove;
74 });
75 if (it != active_observers_.end())
76 active_observers_.erase(it);
77 else
78 pending_observer_ids.push_back(id_to_remove);
79 }
80 if (!pending_observer_ids.empty())
81 database_->RemovePendingObservers(this, pending_observer_ids);
82 }
83
52 } // namespace content 84 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/indexed_db/indexed_db_connection.h ('k') | content/browser/indexed_db/indexed_db_database.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698