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_INTERFACE_IMPL_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ | |
7 | |
8 #include "mojo/public/cpp/bindings/binding.h" | |
9 #include "mojo/public/cpp/bindings/interface_request.h" | |
10 #include "mojo/public/cpp/environment/environment.h" | |
11 #include "mojo/public/cpp/system/macros.h" | |
12 | |
13 namespace mojo { | |
14 | |
15 // DEPRECATED! Please use mojo::Binding instead of InterfaceImpl<> in new code. | |
16 // | |
17 // InterfaceImpl<..> is designed to be the base class of an interface | |
18 // implementation. It may be bound to a pipe or a proxy, see BindToPipe and | |
19 // BindToProxy. | |
20 template <typename Interface> | |
21 class InterfaceImpl : public Interface, public ErrorHandler { | |
22 public: | |
23 using ImplementedInterface = Interface; | |
24 using Client = typename Interface::Client; | |
25 | |
26 InterfaceImpl() : binding_(this), error_handler_impl_(this) { | |
27 binding_.set_error_handler(&error_handler_impl_); | |
28 } | |
29 virtual ~InterfaceImpl() {} | |
30 | |
31 void BindToHandle( | |
32 ScopedMessagePipeHandle handle, | |
33 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
34 binding_.Bind(handle.Pass(), waiter); | |
35 } | |
36 | |
37 bool WaitForIncomingMethodCall() { | |
38 return binding_.WaitForIncomingMethodCall(); | |
39 } | |
40 | |
41 Client* client() { return binding_.client(); } | |
42 internal::Router* internal_router() { return binding_.internal_router(); } | |
43 | |
44 // Implements ErrorHandler. | |
45 // | |
46 // Called when the client is no longer connected to this instance. NOTE: The | |
47 // client() method continues to return a non-null pointer after this method | |
48 // is called. After this method is called, any method calls made on client() | |
49 // will be silently ignored. | |
50 void OnConnectionError() override {} | |
51 | |
52 void set_delete_on_error(bool delete_on_error) { | |
53 error_handler_impl_.set_delete_on_error(delete_on_error); | |
54 } | |
55 | |
56 private: | |
57 class ErrorHandlerImpl : public ErrorHandler { | |
58 public: | |
59 explicit ErrorHandlerImpl(InterfaceImpl* impl) : impl_(impl) {} | |
60 ~ErrorHandlerImpl() override {} | |
61 | |
62 // ErrorHandler implementation: | |
63 void OnConnectionError() override { | |
64 // If the the instance is not bound to the pipe, the instance might choose | |
65 // to delete the binding in the OnConnectionError handler, which would in | |
66 // turn delete |this|. Save the error behavior before invoking the error | |
67 // handler so we can correctly decide what to do. | |
68 bool delete_on_error = delete_on_error_; | |
69 impl_->OnConnectionError(); | |
70 if (delete_on_error) | |
71 delete impl_; | |
72 } | |
73 | |
74 void set_delete_on_error(bool delete_on_error) { | |
75 delete_on_error_ = delete_on_error; | |
76 } | |
77 | |
78 private: | |
79 InterfaceImpl* impl_; | |
80 bool delete_on_error_ = false; | |
81 | |
82 MOJO_DISALLOW_COPY_AND_ASSIGN(ErrorHandlerImpl); | |
83 }; | |
84 | |
85 Binding<Interface> binding_; | |
86 ErrorHandlerImpl error_handler_impl_; | |
87 | |
88 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl); | |
89 }; | |
90 | |
91 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | |
92 // MessagePipe. The instance is returned for convenience in member initializer | |
93 // lists, etc. | |
94 // | |
95 // If the pipe is closed, the instance's OnConnectionError method will be called | |
96 // and then the instance will be deleted. | |
97 // | |
98 // The instance is also bound to the current thread. Its methods will only be | |
99 // called on the current thread, and if the current thread exits, then the end | |
100 // point of the pipe will be closed and the error handler's OnConnectionError | |
101 // method will be called. | |
102 template <typename Impl> | |
103 Impl* BindToPipe( | |
104 Impl* instance, | |
105 ScopedMessagePipeHandle handle, | |
106 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
107 instance->set_delete_on_error(true); | |
108 instance->BindToHandle(handle.Pass(), waiter); | |
109 return instance; | |
110 } | |
111 | |
112 // Like BindToPipe but does not delete the instance after a channel error. | |
113 template <typename Impl> | |
114 Impl* WeakBindToPipe( | |
115 Impl* instance, | |
116 ScopedMessagePipeHandle handle, | |
117 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
118 instance->BindToHandle(handle.Pass(), waiter); | |
119 return instance; | |
120 } | |
121 | |
122 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | |
123 // InterfacePtr<..>. The instance is returned for convenience in member | |
124 // initializer lists, etc. If the pipe is closed, the instance's | |
125 // OnConnectionError method will be called and then the instance will be | |
126 // deleted. | |
127 // | |
128 // The instance is also bound to the current thread. Its methods will only be | |
129 // called on the current thread, and if the current thread exits, then it will | |
130 // also be deleted, and along with it, its end point of the pipe will be closed. | |
131 template <typename Impl, typename Interface> | |
132 Impl* BindToProxy( | |
133 Impl* instance, | |
134 InterfacePtr<Interface>* ptr, | |
135 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
136 instance->set_delete_on_error(true); | |
137 WeakBindToProxy(instance, ptr, waiter); | |
138 return instance; | |
139 } | |
140 | |
141 // Like BindToProxy but does not delete the instance after a channel error. | |
142 template <typename Impl, typename Interface> | |
143 Impl* WeakBindToProxy( | |
144 Impl* instance, | |
145 InterfacePtr<Interface>* ptr, | |
146 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
147 MessagePipe pipe; | |
148 ptr->Bind(pipe.handle0.Pass(), waiter); | |
149 instance->BindToHandle(pipe.handle1.Pass(), waiter); | |
150 return instance; | |
151 } | |
152 | |
153 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | |
154 // InterfaceRequest<..>. The instance is returned for convenience in member | |
155 // initializer lists, etc. If the pipe is closed, the instance's | |
156 // OnConnectionError method will be called and then the instance will be | |
157 // deleted. | |
158 // | |
159 // The instance is also bound to the current thread. Its methods will only be | |
160 // called on the current thread, and if the current thread exits, then it will | |
161 // also be deleted, and along with it, its end point of the pipe will be closed. | |
162 // | |
163 // Before returning, the instance will receive a SetClient call, providing it | |
164 // with a proxy to the client on the other end of the pipe. | |
165 template <typename Impl, typename Interface> | |
166 Impl* BindToRequest( | |
167 Impl* instance, | |
168 InterfaceRequest<Interface>* request, | |
169 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
170 return BindToPipe(instance, request->PassMessagePipe(), waiter); | |
171 } | |
172 | |
173 // Like BindToRequest but does not delete the instance after a channel error. | |
174 template <typename Impl, typename Interface> | |
175 Impl* WeakBindToRequest( | |
176 Impl* instance, | |
177 InterfaceRequest<Interface>* request, | |
178 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
179 return WeakBindToPipe(instance, request->PassMessagePipe(), waiter); | |
180 } | |
181 | |
182 } // namespace mojo | |
183 | |
184 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ | |
OLD | NEW |