Chromium Code Reviews| 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/interface_request.h" | 8 #include "mojo/public/cpp/bindings/interface_request.h" |
| 9 #include "mojo/public/cpp/bindings/lib/interface_impl_internal.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 template <typename Interface> | |
| 16 class InterfaceImpl; | |
| 17 | |
| 18 // Mojo provides two ways to bind an implementation of a particular interface to | |
| 19 // a message pipe. The distinction lies in whether the binding logic is | |
| 20 // inherited from a superclass (InterfaceImpl) or whether the binding is managed | |
| 21 // separately from the class (InterfaceBinding). InterfaceImpl is generally | |
| 22 // simpler to use at the cost of flexibility. | |
| 23 | |
| 24 // InterfaceBinding is used to bind an implementation of an interface to a pipe. | |
| 25 // Once bound, client() returns the client of the Interface. | |
| 26 // InterfaceBinding owns the underlying pipe. Deleting InterfaceBinding | |
| 27 // closes the pipe (assuming it is open). | |
| 28 // InterfaceBinding can be configured to delete the supplied interface when | |
| 29 // InterfaceBinding is deleted. See set_owns_interface(). | |
| 30 // . InterfaceBinding can own the | |
|
DaveMoore
2014/11/10 23:54:23
Nit: Extra line?
sky
2014/11/11 00:34:57
Done.
| |
| 31 template <typename Interface> | |
| 32 class InterfaceBinding : public InterfaceBindingDelegate { | |
| 33 public: | |
| 34 typedef typename Interface::Client Client; | |
| 35 | |
| 36 InterfaceBinding(Interface* interface, InterfaceBindingDelegate* delegate) | |
| 37 : delegate_(delegate), | |
| 38 internal_state_(interface, this), | |
| 39 destroyed_(nullptr), | |
| 40 owns_interface_(false) {} | |
| 41 | |
| 42 virtual ~InterfaceBinding() { | |
| 43 if (owns_interface_) | |
| 44 delete internal_state_.instance(); | |
| 45 } | |
| 46 | |
| 47 void BindToPipe( | |
| 48 ScopedMessagePipeHandle handle, | |
| 49 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
| 50 internal_state_.Bind(handle.Pass(), false, waiter); | |
| 51 } | |
| 52 | |
| 53 void BindToRequest( | |
| 54 InterfaceRequest<Interface>* request, | |
| 55 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
| 56 return BindToPipe(request->PassMessagePipe(), waiter); | |
| 57 } | |
| 58 | |
| 59 // Whether the supplied interface should be deleted when InterfaceBinding is | |
| 60 // destroyed. | |
| 61 void set_owns_interface(bool owns_interface) { | |
| 62 owns_interface_ = owns_interface; | |
| 63 } | |
| 64 | |
| 65 Client* client() { return internal_state_.client(); } | |
| 66 | |
| 67 private: | |
| 68 friend class InterfaceImpl<Interface>; | |
| 69 | |
| 70 void set_delete_interface_on_error(bool delete_interface_on_error) { | |
| 71 delete_interface_on_error_ = delete_interface_on_error; | |
| 72 } | |
| 73 | |
| 74 // InterfaceBindingDelegate: | |
| 75 void OnConnectionEstablished() override { | |
| 76 delegate_->OnConnectionEstablished(); | |
| 77 } | |
|
DaveMoore
2014/11/10 23:54:23
Nit: blank line
sky
2014/11/11 00:34:57
The lack of newline is done to reinforce OnConnect
| |
| 78 void OnConnectionError() override { | |
| 79 bool destroyed = false; | |
| 80 destroyed_ = &destroyed; | |
| 81 // Allow OnConnectionError() to delete 'this'. | |
| 82 delegate_->OnConnectionError(); | |
| 83 if (destroyed) | |
| 84 return; | |
| 85 destroyed_ = nullptr; | |
| 86 // |delete_interface_on_error_| means we're in an InterfaceImpl and need | |
| 87 // to delete the InterfaceImpl (which triggers deleting us). | |
| 88 if (internal_state_.instance_bound_to_pipe()) { | |
| 89 if (delete_interface_on_error_) | |
| 90 delete internal_state_.instance(); | |
| 91 else | |
| 92 delete this; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 InterfaceBindingDelegate* delegate_; | |
| 97 | |
| 98 internal::InterfaceImplState<Interface> internal_state_; | |
| 99 // If non-null the destructor sets the value to true. Used to detect deletion | |
| 100 // while calling to delegate. | |
|
DaveMoore
2014/11/10 23:54:23
Where does the destructor set this?
sky
2014/11/11 00:34:57
D'OH! Apparently no tests needed this logic. Added
DaveMoore
2014/11/11 16:04:14
If we keep this cl you should add a test for this
| |
| 101 bool* destroyed_; | |
| 102 | |
| 103 bool owns_interface_; | |
| 104 | |
| 105 // See description in OnConnectionError(). | |
| 106 bool delete_interface_on_error_; | |
| 107 | |
| 108 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceBinding); | |
| 109 }; | |
| 110 | |
| 15 // InterfaceImpl<..> is designed to be the base class of an interface | 111 // InterfaceImpl<..> is designed to be the base class of an interface |
| 16 // implementation. It may be bound to a pipe or a proxy, see BindToPipe and | 112 // implementation. It may be bound to a pipe or a proxy, see BindToPipe and |
| 17 // BindToProxy. | 113 // BindToProxy. |
| 18 template <typename Interface> | 114 template <typename Interface> |
| 19 class InterfaceImpl : public internal::InterfaceImplBase<Interface> { | 115 class InterfaceImpl : public InterfaceBindingDelegate, public Interface { |
| 20 public: | 116 public: |
| 21 typedef typename Interface::Client Client; | 117 typedef typename Interface::Client Client; |
| 22 typedef Interface ImplementedInterface; | 118 typedef Interface ImplementedInterface; |
| 23 | 119 |
| 24 InterfaceImpl() : internal_state_(this) {} | 120 InterfaceImpl() : interface_binding_(nullptr) {} |
| 25 virtual ~InterfaceImpl() {} | 121 virtual ~InterfaceImpl() { delete interface_binding_; } |
| 26 | 122 |
| 27 // Returns a proxy to the client interface. This is null upon construction, | 123 // Returns a proxy to the client interface. This is null upon construction, |
| 28 // and becomes non-null after OnClientConnected. NOTE: It remains non-null | 124 // and becomes non-null after OnClientConnected. NOTE: It remains non-null |
| 29 // until this instance is deleted. | 125 // until this instance is deleted. |
| 30 Client* client() { return internal_state_.client(); } | 126 Client* client() { return internal_state()->client(); } |
| 31 | 127 |
| 32 // Blocks the current thread for the first incoming method call, i.e., either | 128 // 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 | 129 // 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 | 130 // has been called, |false| in case of error. It must only be called on a |
| 35 // bound object. | 131 // bound object. |
| 36 bool WaitForIncomingMethodCall() { | 132 bool WaitForIncomingMethodCall() { |
| 37 return internal_state_.WaitForIncomingMethodCall(); | 133 return internal_state()->WaitForIncomingMethodCall(); |
| 38 } | 134 } |
| 39 | 135 |
| 40 // Called when the client has connected to this instance. | |
| 41 virtual void OnConnectionEstablished() {} | |
| 42 | |
| 43 // Called when the client is no longer connected to this instance. NOTE: The | 136 // Called when the client is no longer connected to this instance. NOTE: The |
| 44 // client() method continues to return a non-null pointer after this method | 137 // client() method continues to return a non-null pointer after this method |
| 45 // is called. After this method is called, any method calls made on client() | 138 // is called. After this method is called, any method calls made on client() |
| 46 // will be silently ignored. | 139 // will be silently ignored. |
| 47 virtual void OnConnectionError() {} | 140 void OnConnectionError() override {} |
| 48 | 141 |
| 49 // DO NOT USE. Exposed only for internal use and for testing. | 142 // DO NOT USE. Exposed only for internal use and for testing. |
| 50 internal::InterfaceImplState<Interface>* internal_state() { | 143 internal::InterfaceImplState<Interface>* internal_state() { |
| 51 return &internal_state_; | 144 return &interface_binding_->internal_state_; |
| 145 } | |
| 146 | |
| 147 // TODO(sky): move to private and friend functions that use. | |
| 148 void CreateInterfaceBinding() { | |
| 149 MOJO_DCHECK(!interface_binding_); | |
| 150 interface_binding_ = new InterfaceBinding<Interface>(this, this); | |
| 151 interface_binding_->set_owns_interface(false); | |
| 152 interface_binding_->set_delete_interface_on_error(true); | |
| 52 } | 153 } |
| 53 | 154 |
| 54 private: | 155 private: |
| 55 internal::InterfaceImplState<Interface> internal_state_; | 156 InterfaceBinding<Interface>* interface_binding_; |
| 157 | |
| 56 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl); | 158 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl); |
| 57 }; | 159 }; |
| 58 | 160 |
| 59 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | 161 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given |
| 60 // MessagePipe. The instance is returned for convenience in member initializer | 162 // MessagePipe. The instance is returned for convenience in member initializer |
| 61 // lists, etc. | 163 // lists, etc. |
| 62 // | 164 // |
| 63 // If the pipe is closed, the instance's OnConnectionError method will be called | 165 // If the pipe is closed, the instance's OnConnectionError method will be called |
| 64 // and then the instance will be deleted. | 166 // and then the instance will be deleted. |
| 65 // | 167 // |
| 66 // The instance is also bound to the current thread. Its methods will only be | 168 // The instance is also bound to the current thread. Its methods will only be |
| 67 // called on the current thread, and if the current thread exits, then the end | 169 // called on the current thread, and if the current thread exits, then the end |
| 68 // point of the pipe will be closed and the error handler's OnConnectionError | 170 // point of the pipe will be closed and the error handler's OnConnectionError |
| 69 // method will be called. | 171 // method will be called. |
| 70 // | 172 // |
| 71 // Before returning, the instance's OnConnectionEstablished method is called. | 173 // Before returning, the instance's OnConnectionEstablished method is called. |
| 72 template <typename Impl> | 174 template <typename Impl, |
| 175 typename Interface = typename Impl::ImplementedInterface> | |
| 73 Impl* BindToPipe( | 176 Impl* BindToPipe( |
| 74 Impl* instance, | 177 Impl* instance, |
| 75 ScopedMessagePipeHandle handle, | 178 ScopedMessagePipeHandle handle, |
| 76 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 179 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 180 instance->CreateInterfaceBinding(); | |
| 77 instance->internal_state()->Bind(handle.Pass(), true, waiter); | 181 instance->internal_state()->Bind(handle.Pass(), true, waiter); |
| 78 return instance; | 182 return instance; |
| 79 } | 183 } |
| 80 | 184 |
| 81 // Like BindToPipe but does not delete the instance after a channel error. | 185 // Like BindToPipe but does not delete the instance after a channel error. |
| 82 template <typename Impl> | 186 template <typename Impl, |
| 187 typename Interface = typename Impl::ImplementedInterface> | |
| 83 Impl* WeakBindToPipe( | 188 Impl* WeakBindToPipe( |
| 84 Impl* instance, | 189 Impl* instance, |
| 85 ScopedMessagePipeHandle handle, | 190 ScopedMessagePipeHandle handle, |
| 86 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 191 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 192 instance->CreateInterfaceBinding(); | |
| 87 instance->internal_state()->Bind(handle.Pass(), false, waiter); | 193 instance->internal_state()->Bind(handle.Pass(), false, waiter); |
| 88 return instance; | 194 return instance; |
| 89 } | 195 } |
| 90 | 196 |
| 91 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | 197 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given |
| 92 // InterfacePtr<..>. The instance is returned for convenience in member | 198 // InterfacePtr<..>. The instance is returned for convenience in member |
| 93 // initializer lists, etc. If the pipe is closed, the instance's | 199 // initializer lists, etc. If the pipe is closed, the instance's |
| 94 // OnConnectionError method will be called and then the instance will be | 200 // OnConnectionError method will be called and then the instance will be |
| 95 // deleted. | 201 // deleted. |
| 96 // | 202 // |
| 97 // The instance is also bound to the current thread. Its methods will only be | 203 // The instance is also bound to the current thread. Its methods will only be |
| 98 // called on the current thread, and if the current thread exits, then it will | 204 // called on the current thread, and if the current thread exits, then it will |
| 99 // also be deleted, and along with it, its end point of the pipe will be closed. | 205 // also be deleted, and along with it, its end point of the pipe will be closed. |
| 100 // | 206 // |
| 101 // Before returning, the instance's OnConnectionEstablished method is called. | 207 // Before returning, the instance's OnConnectionEstablished method is called. |
| 102 template <typename Impl, typename Interface> | 208 template <typename Impl, typename Interface> |
| 103 Impl* BindToProxy( | 209 Impl* BindToProxy( |
| 104 Impl* instance, | 210 Impl* instance, |
| 105 InterfacePtr<Interface>* ptr, | 211 InterfacePtr<Interface>* ptr, |
| 106 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 212 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 213 instance->CreateInterfaceBinding(); | |
| 107 instance->internal_state()->BindProxy(ptr, true, waiter); | 214 instance->internal_state()->BindProxy(ptr, true, waiter); |
| 108 return instance; | 215 return instance; |
| 109 } | 216 } |
| 110 | 217 |
| 111 // Like BindToProxy but does not delete the instance after a channel error. | 218 // Like BindToProxy but does not delete the instance after a channel error. |
| 112 template <typename Impl, typename Interface> | 219 template <typename Impl, typename Interface> |
| 113 Impl* WeakBindToProxy( | 220 Impl* WeakBindToProxy( |
| 114 Impl* instance, | 221 Impl* instance, |
| 115 InterfacePtr<Interface>* ptr, | 222 InterfacePtr<Interface>* ptr, |
| 116 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 223 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 224 instance->CreateInterfaceBinding(); | |
| 117 instance->internal_state()->BindProxy(ptr, false, waiter); | 225 instance->internal_state()->BindProxy(ptr, false, waiter); |
| 118 return instance; | 226 return instance; |
| 119 } | 227 } |
| 120 | 228 |
| 121 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | 229 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given |
| 122 // InterfaceRequest<..>. The instance is returned for convenience in member | 230 // InterfaceRequest<..>. The instance is returned for convenience in member |
| 123 // initializer lists, etc. If the pipe is closed, the instance's | 231 // initializer lists, etc. If the pipe is closed, the instance's |
| 124 // OnConnectionError method will be called and then the instance will be | 232 // OnConnectionError method will be called and then the instance will be |
| 125 // deleted. | 233 // deleted. |
| 126 // | 234 // |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 143 Impl* WeakBindToRequest( | 251 Impl* WeakBindToRequest( |
| 144 Impl* instance, | 252 Impl* instance, |
| 145 InterfaceRequest<Interface>* request, | 253 InterfaceRequest<Interface>* request, |
| 146 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 254 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 147 return WeakBindToPipe(instance, request->PassMessagePipe(), waiter); | 255 return WeakBindToPipe(instance, request->PassMessagePipe(), waiter); |
| 148 } | 256 } |
| 149 | 257 |
| 150 } // namespace mojo | 258 } // namespace mojo |
| 151 | 259 |
| 152 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ | 260 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ |
| OLD | NEW |