Chromium Code Reviews| 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 |
| 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 void IndexedDBConnection::ActivatePendingObservers( | |
| 58 std::vector<std::unique_ptr<IndexedDBObserver>> pending_observers) { | |
| 59 for (auto& observer : pending_observers) { | |
| 60 active_observers_.push_back(std::move(observer)); | |
| 61 } | |
| 62 pending_observers.clear(); | |
| 63 } | |
| 64 | |
| 65 void IndexedDBConnection::RemoveObservers( | |
| 66 const std::vector<int32_t>& observer_ids_to_remove) { | |
| 67 // TODO(palakj): Change from vector to set or create tmp vector instead of | |
|
cmumford
2016/07/01 18:35:00
This seems like a TODO that can be done now - any
| |
| 68 // erase. | |
| 69 std::vector<int32_t> pending_observer_ids; | |
| 70 for (int32_t id_to_remove : observer_ids_to_remove) { | |
| 71 bool removed = false; | |
|
cmumford
2016/07/01 18:35:00
Your implementation is fine, but using a lambda mi
| |
| 72 for (size_t j = 0; j < active_observers_.size(); j++) { | |
| 73 if (active_observers_[j]->id() == id_to_remove) { | |
| 74 active_observers_.erase(active_observers_.begin() + j); | |
| 75 removed = true; | |
| 76 break; | |
| 77 } | |
| 78 } | |
| 79 // If observer not in active_observers_ , must be in pending_observer list | |
| 80 // of one of the transactions. | |
| 81 if (!removed) { | |
|
cmumford
2016/07/01 18:35:00
Nit: Curly-braces not required for single-line sta
palakj1
2016/07/02 00:48:13
Done.
| |
| 82 pending_observer_ids.push_back(id_to_remove); | |
| 83 } | |
| 84 } | |
| 85 if (!pending_observer_ids.empty()) | |
| 86 database_->RemovePendingObservers(this, pending_observer_ids); | |
| 87 } | |
| 88 | |
| 52 } // namespace content | 89 } // namespace content |
| OLD | NEW |