| 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 // 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 internal::InterfaceImplBase<Interface> { |
| 20 public: | 20 public: |
| 21 typedef typename Interface::Client Client; | 21 typedef typename Interface::Client Client; |
| 22 typedef Interface ImplementedInterface; |
| 22 | 23 |
| 23 InterfaceImpl() : internal_state_(this) {} | 24 InterfaceImpl() : internal_state_(this) {} |
| 24 virtual ~InterfaceImpl() {} | 25 virtual ~InterfaceImpl() {} |
| 25 | 26 |
| 26 // Returns a proxy to the client interface. This is null upon construction, | 27 // Returns a proxy to the client interface. This is null upon construction, |
| 27 // and becomes non-null after OnClientConnected. NOTE: It remains non-null | 28 // and becomes non-null after OnClientConnected. NOTE: It remains non-null |
| 28 // until this instance is deleted. | 29 // until this instance is deleted. |
| 29 Client* client() { return internal_state_.client(); } | 30 Client* client() { return internal_state_.client(); } |
| 30 | 31 |
| 31 // Blocks the current thread for the first incoming method call, i.e., either | 32 // Blocks the current thread for the first incoming method call, i.e., either |
| (...skipping 18 matching lines...) Expand all Loading... |
| 50 return &internal_state_; | 51 return &internal_state_; |
| 51 } | 52 } |
| 52 | 53 |
| 53 private: | 54 private: |
| 54 internal::InterfaceImplState<Interface> internal_state_; | 55 internal::InterfaceImplState<Interface> internal_state_; |
| 55 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl); | 56 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl); |
| 56 }; | 57 }; |
| 57 | 58 |
| 58 // 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 |
| 59 // MessagePipe. The instance is returned for convenience in member initializer | 60 // MessagePipe. The instance is returned for convenience in member initializer |
| 60 // lists, etc. If the pipe is closed, the instance's OnConnectionError method | 61 // lists, etc. |
| 61 // will be called. | 62 // |
| 63 // If the pipe is closed, the instance's OnConnectionError method will be called |
| 64 // and then the instance will be deleted. |
| 62 // | 65 // |
| 63 // 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 |
| 64 // called on the current thread, and if the current thread exits, then it will | 67 // called on the current thread, and if the current thread exits, then the end |
| 65 // also be deleted, and along with it, its end point of the pipe will be closed. | 68 // point of the pipe will be closed and the error handler's OnConnectionError |
| 69 // method will be called. |
| 66 // | 70 // |
| 67 // Before returning, the instance's OnConnectionEstablished method is called. | 71 // Before returning, the instance's OnConnectionEstablished method is called. |
| 68 template <typename Impl> | 72 template <typename Impl> |
| 69 Impl* BindToPipe( | 73 Impl* BindToPipe( |
| 70 Impl* instance, | 74 Impl* instance, |
| 71 ScopedMessagePipeHandle handle, | 75 ScopedMessagePipeHandle handle, |
| 72 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 76 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 73 instance->internal_state()->Bind(handle.Pass(), waiter); | 77 instance->internal_state()->Bind(handle.Pass(), true, waiter); |
| 78 return instance; |
| 79 } |
| 80 |
| 81 // Like BindToPipe but does not delete the instance after a channel error. |
| 82 template <typename Impl> |
| 83 Impl* WeakBindToPipe( |
| 84 Impl* instance, |
| 85 ScopedMessagePipeHandle handle, |
| 86 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 87 instance->internal_state()->Bind(handle.Pass(), false, waiter); |
| 74 return instance; | 88 return instance; |
| 75 } | 89 } |
| 76 | 90 |
| 77 // 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 |
| 78 // InterfacePtr<..>. The instance is returned for convenience in member | 92 // InterfacePtr<..>. The instance is returned for convenience in member |
| 79 // initializer lists, etc. If the pipe is closed, the instance's | 93 // initializer lists, etc. If the pipe is closed, the instance's |
| 80 // OnConnectionError method will be called. | 94 // OnConnectionError method will be called and then the instance will be |
| 95 // deleted. |
| 81 // | 96 // |
| 82 // 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 |
| 83 // 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 |
| 84 // 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. |
| 85 // | 100 // |
| 86 // Before returning, the instance's OnConnectionEstablished method is called. | 101 // Before returning, the instance's OnConnectionEstablished method is called. |
| 87 template <typename Impl, typename Interface> | 102 template <typename Impl, typename Interface> |
| 88 Impl* BindToProxy( | 103 Impl* BindToProxy( |
| 89 Impl* instance, | 104 Impl* instance, |
| 90 InterfacePtr<Interface>* ptr, | 105 InterfacePtr<Interface>* ptr, |
| 91 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 106 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 92 instance->internal_state()->BindProxy(ptr, waiter); | 107 instance->internal_state()->BindProxy(ptr, true, waiter); |
| 108 return instance; |
| 109 } |
| 110 |
| 111 // Like BindToProxy but does not delete the instance after a channel error. |
| 112 template <typename Impl, typename Interface> |
| 113 Impl* WeakBindToProxy( |
| 114 Impl* instance, |
| 115 InterfacePtr<Interface>* ptr, |
| 116 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 117 instance->internal_state()->BindProxy(ptr, false, waiter); |
| 93 return instance; | 118 return instance; |
| 94 } | 119 } |
| 95 | 120 |
| 96 // 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 |
| 97 // InterfaceRequest<..>. The instance is returned for convenience in member | 122 // InterfaceRequest<..>. The instance is returned for convenience in member |
| 98 // initializer lists, etc. If the pipe is closed, the instance's | 123 // initializer lists, etc. If the pipe is closed, the instance's |
| 99 // OnConnectionError method will be called. | 124 // OnConnectionError method will be called and then the instance will be |
| 125 // deleted. |
| 100 // | 126 // |
| 101 // 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 |
| 102 // called on the current thread, and if the current thread exits, then it will | 128 // called on the current thread, and if the current thread exits, then it will |
| 103 // also be deleted, and along with it, its end point of the pipe will be closed. | 129 // also be deleted, and along with it, its end point of the pipe will be closed. |
| 104 // | 130 // |
| 105 // Before returning, the instance will receive a SetClient call, providing it | 131 // Before returning, the instance will receive a SetClient call, providing it |
| 106 // with a proxy to the client on the other end of the pipe. | 132 // with a proxy to the client on the other end of the pipe. |
| 107 template <typename Impl, typename Interface> | 133 template <typename Impl, typename Interface> |
| 108 Impl* BindToRequest( | 134 Impl* BindToRequest( |
| 109 Impl* instance, | 135 Impl* instance, |
| 110 InterfaceRequest<Interface>* request, | 136 InterfaceRequest<Interface>* request, |
| 111 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 137 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 112 return BindToPipe(instance, request->PassMessagePipe(), waiter); | 138 return BindToPipe(instance, request->PassMessagePipe(), waiter); |
| 113 } | 139 } |
| 114 | 140 |
| 141 // Like BindToRequest but does not delete the instance after a channel error. |
| 142 template <typename Impl, typename Interface> |
| 143 Impl* WeakBindToRequest( |
| 144 Impl* instance, |
| 145 InterfaceRequest<Interface>* request, |
| 146 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 147 return WeakBindToPipe(instance, request->PassMessagePipe(), waiter); |
| 148 } |
| 149 |
| 115 } // namespace mojo | 150 } // namespace mojo |
| 116 | 151 |
| 117 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ | 152 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ |
| OLD | NEW |