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_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
15 #include "mojo/public/cpp/bindings/binding.h" | 16 #include "mojo/public/cpp/bindings/binding.h" |
16 | 17 |
17 namespace mojo { | 18 namespace mojo { |
18 | 19 |
19 // Use this class to manage a set of bindings, which are automatically destroyed | 20 // Use this class to manage a set of bindings, which are automatically destroyed |
20 // and removed from the set when the pipe they are bound to is disconnected. | 21 // and removed from the set when the pipe they are bound to is disconnected. |
21 template <typename Interface> | 22 template <typename Interface> |
22 class BindingSet { | 23 class BindingSet { |
23 public: | 24 public: |
24 BindingSet() {} | 25 BindingSet() {} |
25 ~BindingSet() { CloseAllBindings(); } | 26 ~BindingSet() { CloseAllBindings(); } |
26 | 27 |
27 void set_connection_error_handler(const Closure& error_handler) { | 28 void set_connection_error_handler(const base::Closure& error_handler) { |
28 error_handler_ = error_handler; | 29 error_handler_ = error_handler; |
29 } | 30 } |
30 | 31 |
31 void AddBinding(Interface* impl, InterfaceRequest<Interface> request) { | 32 void AddBinding(Interface* impl, InterfaceRequest<Interface> request) { |
32 auto binding = new Element(impl, std::move(request)); | 33 auto binding = new Element(impl, std::move(request)); |
33 binding->set_connection_error_handler( | 34 binding->set_connection_error_handler( |
34 base::Bind(&BindingSet::OnConnectionError, base::Unretained(this))); | 35 base::Bind(&BindingSet::OnConnectionError, base::Unretained(this))); |
35 bindings_.push_back(binding->GetWeakPtr()); | 36 bindings_.push_back(binding->GetWeakPtr()); |
36 } | 37 } |
37 | 38 |
(...skipping 21 matching lines...) Expand all Loading... |
59 class Element { | 60 class Element { |
60 public: | 61 public: |
61 Element(Interface* impl, InterfaceRequest<Interface> request) | 62 Element(Interface* impl, InterfaceRequest<Interface> request) |
62 : binding_(impl, std::move(request)), weak_ptr_factory_(this) { | 63 : binding_(impl, std::move(request)), weak_ptr_factory_(this) { |
63 binding_.set_connection_error_handler( | 64 binding_.set_connection_error_handler( |
64 base::Bind(&Element::OnConnectionError, base::Unretained(this))); | 65 base::Bind(&Element::OnConnectionError, base::Unretained(this))); |
65 } | 66 } |
66 | 67 |
67 ~Element() {} | 68 ~Element() {} |
68 | 69 |
69 void set_connection_error_handler(const Closure& error_handler) { | 70 void set_connection_error_handler(const base::Closure& error_handler) { |
70 error_handler_ = error_handler; | 71 error_handler_ = error_handler; |
71 } | 72 } |
72 | 73 |
73 base::WeakPtr<Element> GetWeakPtr() { | 74 base::WeakPtr<Element> GetWeakPtr() { |
74 return weak_ptr_factory_.GetWeakPtr(); | 75 return weak_ptr_factory_.GetWeakPtr(); |
75 } | 76 } |
76 | 77 |
77 void Close() { binding_.Close(); } | 78 void Close() { binding_.Close(); } |
78 | 79 |
79 void OnConnectionError() { | 80 void OnConnectionError() { |
80 Closure error_handler = error_handler_; | 81 base::Closure error_handler = error_handler_; |
81 delete this; | 82 delete this; |
82 if (!error_handler.is_null()) | 83 if (!error_handler.is_null()) |
83 error_handler.Run(); | 84 error_handler.Run(); |
84 } | 85 } |
85 | 86 |
86 private: | 87 private: |
87 Binding<Interface> binding_; | 88 Binding<Interface> binding_; |
88 Closure error_handler_; | 89 base::Closure error_handler_; |
89 base::WeakPtrFactory<Element> weak_ptr_factory_; | 90 base::WeakPtrFactory<Element> weak_ptr_factory_; |
90 | 91 |
91 DISALLOW_COPY_AND_ASSIGN(Element); | 92 DISALLOW_COPY_AND_ASSIGN(Element); |
92 }; | 93 }; |
93 | 94 |
94 void OnConnectionError() { | 95 void OnConnectionError() { |
95 // Clear any deleted bindings. | 96 // Clear any deleted bindings. |
96 bindings_.erase(std::remove_if(bindings_.begin(), bindings_.end(), | 97 bindings_.erase(std::remove_if(bindings_.begin(), bindings_.end(), |
97 [](const base::WeakPtr<Element>& p) { | 98 [](const base::WeakPtr<Element>& p) { |
98 return p.get() == nullptr; | 99 return p.get() == nullptr; |
99 }), | 100 }), |
100 bindings_.end()); | 101 bindings_.end()); |
101 | 102 |
102 if (!error_handler_.is_null()) | 103 if (!error_handler_.is_null()) |
103 error_handler_.Run(); | 104 error_handler_.Run(); |
104 } | 105 } |
105 | 106 |
106 Closure error_handler_; | 107 base::Closure error_handler_; |
107 std::vector<base::WeakPtr<Element>> bindings_; | 108 std::vector<base::WeakPtr<Element>> bindings_; |
108 | 109 |
109 DISALLOW_COPY_AND_ASSIGN(BindingSet); | 110 DISALLOW_COPY_AND_ASSIGN(BindingSet); |
110 }; | 111 }; |
111 | 112 |
112 } // namespace mojo | 113 } // namespace mojo |
113 | 114 |
114 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ | 115 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ |
OLD | NEW |