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_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | |
7 | |
8 #include <assert.h> | |
9 #include <mojo/environment/async_waiter.h> | |
10 #include <mojo/system/time.h> | |
11 | |
12 #include "mojo/public/cpp/bindings/binding.h" | |
13 #include "mojo/public/cpp/bindings/callback.h" | |
14 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
15 #include "mojo/public/cpp/bindings/interface_request.h" | |
16 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | |
17 #include "mojo/public/cpp/bindings/lib/router.h" | |
18 #include "mojo/public/cpp/system/macros.h" | |
19 #include "mojo/public/cpp/system/message_pipe.h" | |
20 | |
21 namespace mojo { | |
22 | |
23 // This connects an interface implementation strongly to a pipe. When a | |
24 // connection error is detected the implementation is deleted. Deleting the | |
25 // connector also closes the pipe. | |
26 // | |
27 // The strong binding does not actually take ownership of the implementation. | |
28 // If the implementation is not bound to a message pipe or if the message | |
29 // pipe is closed locally then no connection error will be produced and the | |
30 // implementation will therefore not be deleted by the strong binding. | |
31 // Keep this in mind to avoid unintentional memory leaks. | |
32 // | |
33 // Example of an implementation that is always bound strongly to a pipe | |
34 // | |
35 // class StronglyBound : public Foo { | |
36 // public: | |
37 // explicit StronglyBound(InterfaceRequest<Foo> request) | |
38 // : binding_(this, request.Pass()) {} | |
39 // | |
40 // // Foo implementation here | |
41 // | |
42 // private: | |
43 // StrongBinding<Foo> binding_; | |
44 // }; | |
45 template <typename Interface> | |
46 class StrongBinding { | |
47 public: | |
48 // Constructs an incomplete binding that will use the implementation |impl|. | |
49 // The binding may be completed with a subsequent call to the |Bind| method. | |
50 // Does not take ownership of |impl|, which must outlive the binding. | |
51 explicit StrongBinding(Interface* impl) : binding_(impl) { | |
52 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | |
53 } | |
54 | |
55 // Constructs a completed binding of message pipe |handle| to implementation | |
56 // |impl|. Does not take ownership of |impl|, which must outlive the binding. | |
57 // See class comment for definition of |waiter|. | |
58 StrongBinding( | |
59 Interface* impl, | |
60 ScopedMessagePipeHandle handle, | |
61 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | |
62 : StrongBinding(impl) { | |
63 binding_.Bind(handle.Pass(), waiter); | |
64 } | |
65 | |
66 // Constructs a completed binding of |impl| to a new message pipe, passing the | |
67 // client end to |ptr|, which takes ownership of it. The caller is expected to | |
68 // pass |ptr| on to the client of the service. Does not take ownership of any | |
69 // of the parameters. |impl| must outlive the binding. |ptr| only needs to | |
70 // last until the constructor returns. See class comment for definition of | |
71 // |waiter|. | |
72 StrongBinding( | |
73 Interface* impl, | |
74 InterfaceHandle<Interface>* ptr, | |
75 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | |
76 : StrongBinding(impl) { | |
77 binding_.Bind(ptr, waiter); | |
78 } | |
79 | |
80 // Constructs a completed binding of |impl| to the message pipe endpoint in | |
81 // |request|, taking ownership of the endpoint. Does not take ownership of | |
82 // |impl|, which must outlive the binding. See class comment for definition of | |
83 // |waiter|. | |
84 StrongBinding( | |
85 Interface* impl, | |
86 InterfaceRequest<Interface> request, | |
87 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | |
88 : StrongBinding(impl) { | |
89 binding_.Bind(request.Pass(), waiter); | |
90 } | |
91 | |
92 // Tears down the binding, closing the message pipe and leaving the interface | |
93 // implementation unbound. Does not cause the implementation to be deleted. | |
94 ~StrongBinding() {} | |
95 | |
96 // Completes a binding that was constructed with only an interface | |
97 // implementation. Takes ownership of |handle| and binds it to the previously | |
98 // specified implementation. See class comment for definition of |waiter|. | |
99 void Bind( | |
100 ScopedMessagePipeHandle handle, | |
101 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
102 assert(!binding_.is_bound()); | |
103 binding_.Bind(handle.Pass(), waiter); | |
104 } | |
105 | |
106 // Completes a binding that was constructed with only an interface | |
107 // implementation by creating a new message pipe, binding one end of it to the | |
108 // previously specified implementation, and passing the other to |ptr|, which | |
109 // takes ownership of it. The caller is expected to pass |ptr| on to the | |
110 // eventual client of the service. Does not take ownership of |ptr|. See | |
111 // class comment for definition of |waiter|. | |
112 void Bind( | |
113 InterfaceHandle<Interface>* ptr, | |
114 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
115 assert(!binding_.is_bound()); | |
116 binding_.Bind(ptr, waiter); | |
117 } | |
118 | |
119 // Completes a binding that was constructed with only an interface | |
120 // implementation by removing the message pipe endpoint from |request| and | |
121 // binding it to the previously specified implementation. See class comment | |
122 // for definition of |waiter|. | |
123 void Bind( | |
124 InterfaceRequest<Interface> request, | |
125 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
126 assert(!binding_.is_bound()); | |
127 binding_.Bind(request.Pass(), waiter); | |
128 } | |
129 | |
130 // Blocks the calling thread until either a call arrives on the previously | |
131 // bound message pipe, the deadline is exceeded, or an error occurs. Returns | |
132 // true if a method was successfully read and dispatched. | |
133 bool WaitForIncomingMethodCall( | |
134 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE) { | |
135 return binding_.WaitForIncomingMethodCall(deadline); | |
136 } | |
137 | |
138 // Closes the message pipe that was previously bound. Does not cause the | |
139 // implementation to be deleted. | |
140 void Close() { binding_.Close(); } | |
141 | |
142 // Unbinds the underlying pipe from this binding and returns it so it can be | |
143 // used in another context, such as on another thread or with a different | |
144 // implementation. Put this object into a state where it can be rebound to a | |
145 // new pipe. Does not cause the implementation to be deleted. | |
146 InterfaceRequest<Interface> Unbind() { return binding_.Unbind(); } | |
147 | |
148 // Sets an error handler that will be called if a connection error occurs on | |
149 // the bound message pipe. Note: The error handler must not delete the | |
150 // interface implementation since that will happen immediately after the | |
151 // error handler returns. | |
152 void set_connection_error_handler(const Closure& error_handler) { | |
153 connection_error_handler_ = error_handler; | |
154 } | |
155 | |
156 // Returns the interface implementation that was previously specified. Caller | |
157 // does not take ownership. | |
158 Interface* impl() { return binding_.impl(); } | |
159 | |
160 // Indicates whether the binding has been completed (i.e., whether a message | |
161 // pipe has been bound to the implementation). | |
162 bool is_bound() const { return binding_.is_bound(); } | |
163 | |
164 // Returns the value of the handle currently bound to this Binding which can | |
165 // be used to make explicit Wait/WaitMany calls. Requires that the Binding be | |
166 // bound. Ownership of the handle is retained by the Binding, it is not | |
167 // transferred to the caller. | |
168 MessagePipeHandle handle() const { return binding_.handle(); } | |
169 | |
170 // Exposed for testing, should not generally be used. | |
171 internal::Router* internal_router() { return binding_.internal_router(); } | |
172 | |
173 void OnConnectionError() { | |
174 connection_error_handler_.Run(); | |
175 delete binding_.impl(); | |
176 } | |
177 | |
178 private: | |
179 Closure connection_error_handler_; | |
180 Binding<Interface> binding_; | |
181 | |
182 MOJO_DISALLOW_COPY_AND_ASSIGN(StrongBinding); | |
183 }; | |
184 | |
185 } // namespace mojo | |
186 | |
187 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | |
OLD | NEW |