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 // 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 Implements; | |
|
jamesr
2014/07/14 21:21:44
this is a little bit strange but without it I'm no
| |
| 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 27 matching lines...) Expand all Loading... | |
| 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. If the pipe is closed, the instance's OnConnectionError method |
| 61 // will be called. | 62 // will be called. |
| 62 // | 63 // |
| 63 // The instance is also bound to the current thread. Its methods will only be | 64 // 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 | 65 // called on the current thread, and if the current thread exits, then it will |
| 65 // also be deleted, and along with it, its end point of the pipe will be closed. | 66 // also be deleted, and along with it, its end point of the pipe will be closed. |
| 66 // | 67 // |
| 67 // Before returning, the instance's OnConnectionEstablished method is called. | 68 // Before returning, the instance's OnConnectionEstablished method is called. |
| 68 template <typename Impl> | 69 template <typename Impl> |
| 69 Impl* BindToPipe( | 70 Impl* BindToPipe( |
|
darin (slow to review)
2014/07/14 21:34:06
Instead of exposing the enum (which is a bit awkwa
| |
| 70 Impl* instance, | 71 Impl* instance, |
| 71 ScopedMessagePipeHandle handle, | 72 ScopedMessagePipeHandle handle, |
| 72 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 73 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter(), |
| 73 instance->internal_state()->Bind(handle.Pass(), waiter); | 74 OnConnectionErrorBehavior behavior = DELETE_ON_ERROR) { |
| 75 instance->internal_state()->Bind(handle.Pass(), waiter, behavior); | |
| 74 return instance; | 76 return instance; |
| 75 } | 77 } |
| 76 | 78 |
| 77 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | 79 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given |
| 78 // InterfacePtr<..>. The instance is returned for convenience in member | 80 // InterfacePtr<..>. The instance is returned for convenience in member |
| 79 // initializer lists, etc. If the pipe is closed, the instance's | 81 // initializer lists, etc. If the pipe is closed, the instance's |
| 80 // OnConnectionError method will be called. | 82 // OnConnectionError method will be called. |
| 81 // | 83 // |
| 82 // The instance is also bound to the current thread. Its methods will only be | 84 // 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 | 85 // 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. | 86 // also be deleted, and along with it, its end point of the pipe will be closed. |
| 85 // | 87 // |
| 86 // Before returning, the instance's OnConnectionEstablished method is called. | 88 // Before returning, the instance's OnConnectionEstablished method is called. |
| 87 template <typename Impl, typename Interface> | 89 template <typename Impl, typename Interface> |
| 88 Impl* BindToProxy( | 90 Impl* BindToProxy( |
| 89 Impl* instance, | 91 Impl* instance, |
| 90 InterfacePtr<Interface>* ptr, | 92 InterfacePtr<Interface>* ptr, |
| 91 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 93 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 92 instance->internal_state()->BindProxy(ptr, waiter); | 94 instance->internal_state()->BindProxy(ptr, waiter); |
| 93 return instance; | 95 return instance; |
| 94 } | 96 } |
| 95 | 97 |
| 96 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | 98 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given |
| 97 // InterfaceRequest<..>. The instance is returned for convenience in member | 99 // InterfaceRequest<..>. The instance is returned for convenience in member |
| 98 // initializer lists, etc. If the pipe is closed, the instance's | 100 // initializer lists, etc. |
| 99 // OnConnectionError method will be called. | 101 // |
| 102 // The bind call passes ownership of the instance to the pipe. If the pipe is | |
| 103 // closed, the instance's OnConnectionError method will be called and then the | |
| 104 // instance will be deleted. | |
| 100 // | 105 // |
| 101 // The instance is also bound to the current thread. Its methods will only be | 106 // 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 | 107 // 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. | 108 // also be deleted, and along with it, its end point of the pipe will be closed. |
| 104 // | 109 // |
| 105 // Before returning, the instance will receive a SetClient call, providing it | 110 // 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. | 111 // with a proxy to the client on the other end of the pipe. |
| 112 // | |
| 113 // (Aside: If we were using C++11, the type of the first parameter would be | |
| 114 // std::unique_ptr<Impl>). | |
| 107 template <typename Impl, typename Interface> | 115 template <typename Impl, typename Interface> |
| 108 Impl* BindToRequest( | 116 Impl* BindToRequest( |
| 109 Impl* instance, | 117 Impl* instance, |
| 110 InterfaceRequest<Interface>* request, | 118 InterfaceRequest<Interface>* request, |
| 111 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 119 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 112 return BindToPipe(instance, request->PassMessagePipe(), waiter); | 120 return BindToPipe(instance, request->PassMessagePipe(), waiter); |
| 113 } | 121 } |
| 114 | 122 |
| 123 // This is the same as BindToRequest except that the lifetime of the instance is | |
| 124 // not bound to the pipe. If the pipe is closed, the instance's OnChannelError | |
| 125 // method will be called but the instance will not be deleted. | |
| 126 template <typename Impl, typename Interface> | |
| 127 Impl* WeakBindToRequest( | |
| 128 Impl* instance, | |
| 129 InterfaceRequest<Interface>* request, | |
| 130 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
| 131 return BindToPipe(instance, request->PassMessagePipe(), waiter, DO_NOTHING); | |
| 132 } | |
| 133 | |
| 115 } // namespace mojo | 134 } // namespace mojo |
| 116 | 135 |
| 117 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ | 136 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ |
| OLD | NEW |