| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef MOJO_COMMON_WEAK_BINDING_SET_H_ | 5 #ifndef MOJO_COMMON_BINDING_SET_H_ |
| 6 #define MOJO_COMMON_WEAK_BINDING_SET_H_ | 6 #define MOJO_COMMON_BINDING_SET_H_ |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "mojo/public/cpp/bindings/binding.h" | 12 #include "mojo/public/cpp/bindings/binding.h" |
| 13 | 13 |
| 14 namespace mojo { | 14 namespace mojo { |
| 15 | 15 |
| 16 // Use this class to manage a set of bindings each of which is | 16 // Use this class to manage a set of bindings each of which is |
| 17 // owned by the pipe it is bound to. | 17 // owned by the pipe it is bound to. |
| 18 template <typename Interface> | 18 template <typename Interface> |
| 19 class WeakBindingSet { | 19 class BindingSet { |
| 20 public: | 20 public: |
| 21 WeakBindingSet() {} | 21 BindingSet() {} |
| 22 ~WeakBindingSet() { CloseAllBindings(); } | 22 ~BindingSet() { CloseAllBindings(); } |
| 23 | 23 |
| 24 void AddBinding(Interface* impl, InterfaceRequest<Interface> request) { | 24 void AddBinding(Interface* impl, InterfaceRequest<Interface> request) { |
| 25 bindings_.emplace_back(new Binding<Interface>(impl, request.Pass())); | 25 bindings_.emplace_back(new Binding<Interface>(impl, request.Pass())); |
| 26 auto* binding = bindings_.back().get(); | 26 auto* binding = bindings_.back().get(); |
| 27 // Set the connection error handler for the newly added Binding to be a | 27 // Set the connection error handler for the newly added Binding to be a |
| 28 // function that will erase it from the vector. | 28 // function that will erase it from the vector. |
| 29 binding->set_connection_error_handler([this, binding]() { | 29 binding->set_connection_error_handler([this, binding]() { |
| 30 auto it = | 30 auto it = |
| 31 std::find_if(bindings_.begin(), bindings_.end(), | 31 std::find_if(bindings_.begin(), bindings_.end(), |
| 32 [binding](const std::unique_ptr<Binding<Interface>>& b) { | 32 [binding](const std::unique_ptr<Binding<Interface>>& b) { |
| 33 return (b.get() == binding); | 33 return (b.get() == binding); |
| 34 }); | 34 }); |
| 35 DCHECK(it != bindings_.end()); | 35 DCHECK(it != bindings_.end()); |
| 36 bindings_.erase(it); | 36 bindings_.erase(it); |
| 37 }); | 37 }); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void CloseAllBindings() { | 40 void CloseAllBindings() { bindings_.clear(); } |
| 41 bindings_.clear(); | |
| 42 } | |
| 43 | 41 |
| 44 size_t size() const { return bindings_.size(); } | 42 size_t size() const { return bindings_.size(); } |
| 45 | 43 |
| 46 private: | 44 private: |
| 47 std::vector<std::unique_ptr<Binding<Interface>>> bindings_; | 45 std::vector<std::unique_ptr<Binding<Interface>>> bindings_; |
| 48 | 46 |
| 49 DISALLOW_COPY_AND_ASSIGN(WeakBindingSet); | 47 DISALLOW_COPY_AND_ASSIGN(BindingSet); |
| 50 }; | 48 }; |
| 51 | 49 |
| 52 } // namespace mojo | 50 } // namespace mojo |
| 53 | 51 |
| 54 #endif // MOJO_COMMON_WEAK_BINDING_SET_H_ | 52 #endif // MOJO_COMMON_BINDING_SET_H_ |
| OLD | NEW |