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