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