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/macros.h" | 13 #include "base/macros.h" |
13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
14 #include "mojo/public/cpp/bindings/binding.h" | 15 #include "mojo/public/cpp/bindings/binding.h" |
15 | 16 |
16 namespace mojo { | 17 namespace mojo { |
17 | 18 |
18 // 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 |
19 // 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. |
20 template <typename Interface> | 21 template <typename Interface> |
21 class BindingSet { | 22 class BindingSet { |
22 public: | 23 public: |
23 BindingSet() {} | 24 BindingSet() {} |
24 ~BindingSet() { CloseAllBindings(); } | 25 ~BindingSet() { CloseAllBindings(); } |
25 | 26 |
26 void set_connection_error_handler(const Closure& error_handler) { | 27 void set_connection_error_handler(const Closure& error_handler) { |
27 error_handler_ = error_handler; | 28 error_handler_ = error_handler; |
28 } | 29 } |
29 | 30 |
30 void AddBinding(Interface* impl, InterfaceRequest<Interface> request) { | 31 void AddBinding(Interface* impl, InterfaceRequest<Interface> request) { |
31 auto binding = new Element(impl, std::move(request)); | 32 auto binding = new Element(impl, std::move(request)); |
32 binding->set_connection_error_handler([this]() { OnConnectionError(); }); | 33 binding->set_connection_error_handler( |
| 34 base::Bind(&BindingSet::OnConnectionError, base::Unretained(this))); |
33 bindings_.push_back(binding->GetWeakPtr()); | 35 bindings_.push_back(binding->GetWeakPtr()); |
34 } | 36 } |
35 | 37 |
36 // Returns an InterfacePtr bound to one end of a pipe whose other end is | 38 // Returns an InterfacePtr bound to one end of a pipe whose other end is |
37 // bound to |this|. | 39 // bound to |this|. |
38 InterfacePtr<Interface> CreateInterfacePtrAndBind(Interface* impl) { | 40 InterfacePtr<Interface> CreateInterfacePtrAndBind(Interface* impl) { |
39 InterfacePtr<Interface> interface_ptr; | 41 InterfacePtr<Interface> interface_ptr; |
40 AddBinding(impl, GetProxy(&interface_ptr)); | 42 AddBinding(impl, GetProxy(&interface_ptr)); |
41 return interface_ptr; | 43 return interface_ptr; |
42 } | 44 } |
43 | 45 |
44 void CloseAllBindings() { | 46 void CloseAllBindings() { |
45 for (const auto& it : bindings_) { | 47 for (const auto& it : bindings_) { |
46 if (it) { | 48 if (it) { |
47 it->Close(); | 49 it->Close(); |
48 delete it.get(); | 50 delete it.get(); |
49 } | 51 } |
50 } | 52 } |
51 bindings_.clear(); | 53 bindings_.clear(); |
52 } | 54 } |
53 | 55 |
54 bool empty() const { return bindings_.empty(); } | 56 bool empty() const { return bindings_.empty(); } |
55 | 57 |
56 private: | 58 private: |
57 class Element { | 59 class Element { |
58 public: | 60 public: |
59 Element(Interface* impl, InterfaceRequest<Interface> request) | 61 Element(Interface* impl, InterfaceRequest<Interface> request) |
60 : binding_(impl, std::move(request)), weak_ptr_factory_(this) { | 62 : binding_(impl, std::move(request)), weak_ptr_factory_(this) { |
61 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 63 binding_.set_connection_error_handler( |
| 64 base::Bind(&Element::OnConnectionError, base::Unretained(this))); |
62 } | 65 } |
63 | 66 |
64 ~Element() {} | 67 ~Element() {} |
65 | 68 |
66 void set_connection_error_handler(const Closure& error_handler) { | 69 void set_connection_error_handler(const Closure& error_handler) { |
67 error_handler_ = error_handler; | 70 error_handler_ = error_handler; |
68 } | 71 } |
69 | 72 |
70 base::WeakPtr<Element> GetWeakPtr() { | 73 base::WeakPtr<Element> GetWeakPtr() { |
71 return weak_ptr_factory_.GetWeakPtr(); | 74 return weak_ptr_factory_.GetWeakPtr(); |
72 } | 75 } |
73 | 76 |
74 void Close() { binding_.Close(); } | 77 void Close() { binding_.Close(); } |
75 | 78 |
76 void OnConnectionError() { | 79 void OnConnectionError() { |
77 Closure error_handler = error_handler_; | 80 Closure error_handler = error_handler_; |
78 delete this; | 81 delete this; |
79 error_handler.Run(); | 82 if (!error_handler.is_null()) |
| 83 error_handler.Run(); |
80 } | 84 } |
81 | 85 |
82 private: | 86 private: |
83 Binding<Interface> binding_; | 87 Binding<Interface> binding_; |
84 Closure error_handler_; | 88 Closure error_handler_; |
85 base::WeakPtrFactory<Element> weak_ptr_factory_; | 89 base::WeakPtrFactory<Element> weak_ptr_factory_; |
86 | 90 |
87 DISALLOW_COPY_AND_ASSIGN(Element); | 91 DISALLOW_COPY_AND_ASSIGN(Element); |
88 }; | 92 }; |
89 | 93 |
90 void OnConnectionError() { | 94 void OnConnectionError() { |
91 // Clear any deleted bindings. | 95 // Clear any deleted bindings. |
92 bindings_.erase(std::remove_if(bindings_.begin(), bindings_.end(), | 96 bindings_.erase(std::remove_if(bindings_.begin(), bindings_.end(), |
93 [](const base::WeakPtr<Element>& p) { | 97 [](const base::WeakPtr<Element>& p) { |
94 return p.get() == nullptr; | 98 return p.get() == nullptr; |
95 }), | 99 }), |
96 bindings_.end()); | 100 bindings_.end()); |
97 | 101 |
98 error_handler_.Run(); | 102 if (!error_handler_.is_null()) |
| 103 error_handler_.Run(); |
99 } | 104 } |
100 | 105 |
101 Closure error_handler_; | 106 Closure error_handler_; |
102 std::vector<base::WeakPtr<Element>> bindings_; | 107 std::vector<base::WeakPtr<Element>> bindings_; |
103 | 108 |
104 DISALLOW_COPY_AND_ASSIGN(BindingSet); | 109 DISALLOW_COPY_AND_ASSIGN(BindingSet); |
105 }; | 110 }; |
106 | 111 |
107 } // namespace mojo | 112 } // namespace mojo |
108 | 113 |
109 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ | 114 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ |
OLD | NEW |