OLD | NEW |
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 |
| 19 void IndexedDBConnection::set_id(int32_t id) { |
| 20 DCHECK_EQ(id_, kInvalidId); |
| 21 id_ = id; |
| 22 } |
| 23 |
16 void IndexedDBConnection::Close() { | 24 void IndexedDBConnection::Close() { |
17 if (!callbacks_.get()) | 25 if (!callbacks_.get()) |
18 return; | 26 return; |
19 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr(); | 27 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr(); |
20 database_->Close(this, false /* forced */); | 28 database_->Close(this, false /* forced */); |
21 if (this_obj) { | 29 if (this_obj) { |
22 database_ = nullptr; | 30 database_ = nullptr; |
23 callbacks_ = nullptr; | 31 callbacks_ = nullptr; |
| 32 active_observers_.clear(); |
24 } | 33 } |
25 } | 34 } |
26 | 35 |
27 void IndexedDBConnection::ForceClose() { | 36 void IndexedDBConnection::ForceClose() { |
28 if (!callbacks_.get()) | 37 if (!callbacks_.get()) |
29 return; | 38 return; |
30 | 39 |
31 // IndexedDBDatabase::Close() can delete this instance. | 40 // IndexedDBDatabase::Close() can delete this instance. |
32 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr(); | 41 base::WeakPtr<IndexedDBConnection> this_obj = weak_factory_.GetWeakPtr(); |
33 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks(callbacks_); | 42 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks(callbacks_); |
34 database_->Close(this, true /* forced */); | 43 database_->Close(this, true /* forced */); |
35 if (this_obj) { | 44 if (this_obj) { |
36 database_ = nullptr; | 45 database_ = nullptr; |
37 callbacks_ = nullptr; | 46 callbacks_ = nullptr; |
| 47 active_observers_.clear(); |
38 } | 48 } |
39 callbacks->OnForcedClose(); | 49 callbacks->OnForcedClose(); |
40 } | 50 } |
41 | 51 |
42 void IndexedDBConnection::VersionChangeIgnored() { | 52 void IndexedDBConnection::VersionChangeIgnored() { |
43 if (!database_.get()) | 53 if (!database_.get()) |
44 return; | 54 return; |
45 database_->VersionChangeIgnored(); | 55 database_->VersionChangeIgnored(); |
46 } | 56 } |
47 | 57 |
48 bool IndexedDBConnection::IsConnected() { | 58 bool IndexedDBConnection::IsConnected() { |
49 return database_.get() != NULL; | 59 return database_.get() != NULL; |
50 } | 60 } |
51 | 61 |
| 62 // The observers begin listening to changes only once they are activated. |
| 63 void IndexedDBConnection::ActivatePendingObservers( |
| 64 std::vector<std::unique_ptr<IndexedDBObserver>> pending_observers) { |
| 65 for (auto& observer : pending_observers) { |
| 66 active_observers_.push_back(std::move(observer)); |
| 67 } |
| 68 pending_observers.clear(); |
| 69 } |
| 70 |
| 71 void IndexedDBConnection::RemoveObservers( |
| 72 const std::vector<int32_t>& observer_ids_to_remove) { |
| 73 std::vector<int32_t> pending_observer_ids; |
| 74 for (int32_t id_to_remove : observer_ids_to_remove) { |
| 75 const auto& it = std::find_if( |
| 76 active_observers_.begin(), active_observers_.end(), |
| 77 [&id_to_remove](const std::unique_ptr<IndexedDBObserver>& o) { |
| 78 return o->id() == id_to_remove; |
| 79 }); |
| 80 if (it != active_observers_.end()) |
| 81 active_observers_.erase(it); |
| 82 else |
| 83 pending_observer_ids.push_back(id_to_remove); |
| 84 } |
| 85 if (!pending_observer_ids.empty()) |
| 86 database_->RemovePendingObservers(this, pending_observer_ids); |
| 87 } |
| 88 |
52 } // namespace content | 89 } // namespace content |
OLD | NEW |