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