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/connector.h" | |
| 8 #include "mojo/public/cpp/bindings/interface_request.h" | 9 #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 // InterfaceImpl<..> is designed to be the base class of an interface | 15 // 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 | 16 // implementation. It may be bound to a pipe or a proxy, see BindToPipe and |
| 17 // BindToProxy. | 17 // BindToProxy. |
| 18 template <typename Interface> | 18 template <typename Interface> |
| 19 class InterfaceImpl : public internal::InterfaceImplBase<Interface> { | 19 class InterfaceImpl : public Interface, public ErrorHandler { |
| 20 public: | 20 public: |
| 21 typedef typename Interface::Client Client; | 21 using ImplementedInterface = Interface; |
| 22 typedef Interface ImplementedInterface; | 22 using Client = typename Interface::Client; |
| 23 | 23 |
| 24 InterfaceImpl() : internal_state_(this) {} | 24 InterfaceImpl() : connector_(this), error_handler_impl_(this) { |
|
DaveMoore
2014/11/11 17:18:45
I'm finding it surprising that to change the conne
jamesr
2014/11/11 17:47:49
That is the goal. You don't inherit from Interfac
| |
| 25 connector_.set_error_handler(&error_handler_impl_); | |
| 26 } | |
| 25 virtual ~InterfaceImpl() {} | 27 virtual ~InterfaceImpl() {} |
| 26 | 28 |
| 27 // Returns a proxy to the client interface. This is null upon construction, | 29 void BindConnector( |
|
DaveMoore
2014/11/11 17:18:45
Maybe drop the "Connector" from these names, as th
jamesr
2014/11/11 17:47:49
Yeah (I needed a different name in an intermediate
jamesr
2014/11/11 17:49:20
Actually never mind - this name has to be this way
| |
| 28 // and becomes non-null after OnClientConnected. NOTE: It remains non-null | 30 ScopedMessagePipeHandle handle, |
| 29 // until this instance is deleted. | 31 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 30 Client* client() { return internal_state_.client(); } | 32 connector_.Bind(handle.Pass(), waiter); |
| 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(); | |
| 38 } | 33 } |
| 39 | 34 |
| 40 // Called when the client has connected to this instance. | 35 void BindConnector( |
| 41 virtual void OnConnectionEstablished() {} | 36 InterfacePtr<Interface>* ptr, |
|
DaveMoore
2014/11/11 17:18:45
This seemed like useful functionality. What replac
jamesr
2014/11/11 17:47:49
I disagree - it's not useful at all. The connecti
| |
| 37 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
| 38 connector_.Bind(ptr, waiter); | |
| 39 } | |
| 40 | |
| 41 bool WaitForIncomingMethodCall() { | |
| 42 return connector_.WaitForIncomingMethodCall(); | |
| 43 } | |
| 44 | |
| 45 Client* client() { return connector_.client(); } | |
|
DaveMoore
2014/11/11 17:18:45
There should at least be an extra comment about ro
| |
| 46 internal::Router* router() { return connector_.router(); } | |
| 42 | 47 |
| 43 // Called when the client is no longer connected to this instance. NOTE: The | 48 // 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 | 49 // 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() | 50 // is called. After this method is called, any method calls made on client() |
| 46 // will be silently ignored. | 51 // will be silently ignored. |
| 47 virtual void OnConnectionError() {} | 52 virtual void OnConnectionError() {} |
| 48 | 53 |
| 49 // DO NOT USE. Exposed only for internal use and for testing. | 54 void set_delete_on_error(bool delete_on_error) { |
| 50 internal::InterfaceImplState<Interface>* internal_state() { | 55 error_handler_impl_.set_delete_on_error(delete_on_error); |
| 51 return &internal_state_; | |
| 52 } | 56 } |
| 53 | 57 |
| 54 private: | 58 private: |
| 55 internal::InterfaceImplState<Interface> internal_state_; | 59 class ErrorHandlerImpl : public ErrorHandler { |
| 60 public: | |
| 61 explicit ErrorHandlerImpl(InterfaceImpl* impl) : impl_(impl) {} | |
| 62 ~ErrorHandlerImpl() override {} | |
| 63 | |
| 64 // ErrorHandler implementation: | |
| 65 void OnConnectionError() override { | |
| 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 | |
| 80 Connector<Interface> connector_; | |
| 81 ErrorHandlerImpl error_handler_impl_; | |
| 82 | |
| 56 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl); | 83 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl); |
| 57 }; | 84 }; |
| 58 | 85 |
| 59 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | 86 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given |
| 60 // MessagePipe. The instance is returned for convenience in member initializer | 87 // MessagePipe. The instance is returned for convenience in member initializer |
| 61 // lists, etc. | 88 // lists, etc. |
| 62 // | 89 // |
| 63 // If the pipe is closed, the instance's OnConnectionError method will be called | 90 // If the pipe is closed, the instance's OnConnectionError method will be called |
| 64 // and then the instance will be deleted. | 91 // and then the instance will be deleted. |
| 65 // | 92 // |
| 66 // The instance is also bound to the current thread. Its methods will only be | 93 // 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 | 94 // 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 | 95 // point of the pipe will be closed and the error handler's OnConnectionError |
| 69 // method will be called. | 96 // method will be called. |
| 70 // | |
| 71 // Before returning, the instance's OnConnectionEstablished method is called. | |
| 72 template <typename Impl> | 97 template <typename Impl> |
| 73 Impl* BindToPipe( | 98 Impl* BindToPipe( |
| 74 Impl* instance, | 99 Impl* instance, |
| 75 ScopedMessagePipeHandle handle, | 100 ScopedMessagePipeHandle handle, |
| 76 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 101 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 77 instance->internal_state()->Bind(handle.Pass(), true, waiter); | 102 instance->set_delete_on_error(true); |
| 103 instance->BindConnector(handle.Pass(), waiter); | |
| 78 return instance; | 104 return instance; |
| 79 } | 105 } |
| 80 | 106 |
| 81 // Like BindToPipe but does not delete the instance after a channel error. | 107 // Like BindToPipe but does not delete the instance after a channel error. |
| 82 template <typename Impl> | 108 template <typename Impl> |
| 83 Impl* WeakBindToPipe( | 109 Impl* WeakBindToPipe( |
| 84 Impl* instance, | 110 Impl* instance, |
| 85 ScopedMessagePipeHandle handle, | 111 ScopedMessagePipeHandle handle, |
| 86 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 112 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 87 instance->internal_state()->Bind(handle.Pass(), false, waiter); | 113 instance->BindConnector(handle.Pass(), waiter); |
| 88 return instance; | 114 return instance; |
| 89 } | 115 } |
| 90 | 116 |
| 91 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | 117 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given |
| 92 // InterfacePtr<..>. The instance is returned for convenience in member | 118 // InterfacePtr<..>. The instance is returned for convenience in member |
| 93 // initializer lists, etc. If the pipe is closed, the instance's | 119 // initializer lists, etc. If the pipe is closed, the instance's |
| 94 // OnConnectionError method will be called and then the instance will be | 120 // OnConnectionError method will be called and then the instance will be |
| 95 // deleted. | 121 // deleted. |
| 96 // | 122 // |
| 97 // The instance is also bound to the current thread. Its methods will only be | 123 // 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 | 124 // 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. | 125 // 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. | |
| 102 template <typename Impl, typename Interface> | 126 template <typename Impl, typename Interface> |
| 103 Impl* BindToProxy( | 127 Impl* BindToProxy( |
| 104 Impl* instance, | 128 Impl* instance, |
| 105 InterfacePtr<Interface>* ptr, | 129 InterfacePtr<Interface>* ptr, |
| 106 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 130 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 107 instance->internal_state()->BindProxy(ptr, true, waiter); | 131 instance->set_delete_on_error(true); |
| 132 instance->BindConnector(ptr, waiter); | |
| 108 return instance; | 133 return instance; |
| 109 } | 134 } |
| 110 | 135 |
| 111 // Like BindToProxy but does not delete the instance after a channel error. | 136 // Like BindToProxy but does not delete the instance after a channel error. |
| 112 template <typename Impl, typename Interface> | 137 template <typename Impl, typename Interface> |
| 113 Impl* WeakBindToProxy( | 138 Impl* WeakBindToProxy( |
| 114 Impl* instance, | 139 Impl* instance, |
| 115 InterfacePtr<Interface>* ptr, | 140 InterfacePtr<Interface>* ptr, |
| 116 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 141 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 117 instance->internal_state()->BindProxy(ptr, false, waiter); | 142 instance->BindConnector(ptr, waiter); |
| 118 return instance; | 143 return instance; |
| 119 } | 144 } |
| 120 | 145 |
| 121 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | 146 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given |
| 122 // InterfaceRequest<..>. The instance is returned for convenience in member | 147 // InterfaceRequest<..>. The instance is returned for convenience in member |
| 123 // initializer lists, etc. If the pipe is closed, the instance's | 148 // initializer lists, etc. If the pipe is closed, the instance's |
| 124 // OnConnectionError method will be called and then the instance will be | 149 // OnConnectionError method will be called and then the instance will be |
| 125 // deleted. | 150 // deleted. |
| 126 // | 151 // |
| 127 // The instance is also bound to the current thread. Its methods will only be | 152 // The instance is also bound to the current thread. Its methods will only be |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 143 Impl* WeakBindToRequest( | 168 Impl* WeakBindToRequest( |
| 144 Impl* instance, | 169 Impl* instance, |
| 145 InterfaceRequest<Interface>* request, | 170 InterfaceRequest<Interface>* request, |
| 146 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 171 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 147 return WeakBindToPipe(instance, request->PassMessagePipe(), waiter); | 172 return WeakBindToPipe(instance, request->PassMessagePipe(), waiter); |
| 148 } | 173 } |
| 149 | 174 |
| 150 } // namespace mojo | 175 } // namespace mojo |
| 151 | 176 |
| 152 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ | 177 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ |
| OLD | NEW |