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