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; | |
darin (slow to review)
2014/07/15 06:10:38
The name of this typedef bugs me a bit. Interface
jamesr
2014/07/15 18:07:22
yeah, it's used in two places total so I went with
| |
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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( |
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 instance->internal_state()->Bind( |
75 handle.Pass(), waiter, true /* delete_on_error */); | |
74 return instance; | 76 return instance; |
75 } | 77 } |
76 | 78 |
79 template <typename Impl> | |
80 Impl* WeakBindToPipe( | |
81 Impl* instance, | |
82 ScopedMessagePipeHandle handle, | |
83 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
84 instance->internal_state()->Bind( | |
85 handle.Pass(), waiter, false /* delete_on_error */); | |
86 return instance; | |
87 } | |
88 | |
77 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | 89 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given |
78 // InterfacePtr<..>. The instance is returned for convenience in member | 90 // InterfacePtr<..>. The instance is returned for convenience in member |
79 // initializer lists, etc. If the pipe is closed, the instance's | 91 // initializer lists, etc. If the pipe is closed, the instance's |
80 // OnConnectionError method will be called. | 92 // OnConnectionError method will be called. |
81 // | 93 // |
82 // The instance is also bound to the current thread. Its methods will only be | 94 // 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 | 95 // 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. | 96 // also be deleted, and along with it, its end point of the pipe will be closed. |
85 // | 97 // |
86 // Before returning, the instance's OnConnectionEstablished method is called. | 98 // Before returning, the instance's OnConnectionEstablished method is called. |
87 template <typename Impl, typename Interface> | 99 template <typename Impl, typename Interface> |
88 Impl* BindToProxy( | 100 Impl* BindToProxy( |
darin (slow to review)
2014/07/15 06:10:38
I guess this should be renamed WeakBindToProxy. Ma
jamesr
2014/07/15 18:07:22
Turns out I do need a WeakBindToProxy so I made th
| |
89 Impl* instance, | 101 Impl* instance, |
90 InterfacePtr<Interface>* ptr, | 102 InterfacePtr<Interface>* ptr, |
91 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 103 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
92 instance->internal_state()->BindProxy(ptr, waiter); | 104 instance->internal_state()->BindProxy(ptr, waiter); |
93 return instance; | 105 return instance; |
94 } | 106 } |
95 | 107 |
96 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | 108 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given |
97 // InterfaceRequest<..>. The instance is returned for convenience in member | 109 // InterfaceRequest<..>. The instance is returned for convenience in member |
98 // initializer lists, etc. If the pipe is closed, the instance's | 110 // initializer lists, etc. |
99 // OnConnectionError method will be called. | 111 // |
112 // The bind call passes ownership of the instance to the pipe. If the pipe is | |
113 // closed, the instance's OnConnectionError method will be called and then the | |
114 // instance will be deleted. | |
100 // | 115 // |
101 // The instance is also bound to the current thread. Its methods will only be | 116 // 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 | 117 // 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. | 118 // also be deleted, and along with it, its end point of the pipe will be closed. |
104 // | 119 // |
105 // Before returning, the instance will receive a SetClient call, providing it | 120 // 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. | 121 // with a proxy to the client on the other end of the pipe. |
107 template <typename Impl, typename Interface> | 122 template <typename Impl, typename Interface> |
108 Impl* BindToRequest( | 123 Impl* BindToRequest( |
109 Impl* instance, | 124 Impl* instance, |
110 InterfaceRequest<Interface>* request, | 125 InterfaceRequest<Interface>* request, |
111 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 126 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
112 return BindToPipe(instance, request->PassMessagePipe(), waiter); | 127 return BindToPipe(instance, request->PassMessagePipe(), waiter); |
113 } | 128 } |
114 | 129 |
130 // This is the same as BindToRequest except that the lifetime of the instance is | |
131 // not bound to the pipe. If the pipe is closed, the instance's OnChannelError | |
darin (slow to review)
2014/07/15 06:10:38
nit: OnChannelError -> OnConnectionError
| |
132 // method will be called but the instance will not be deleted. | |
133 template <typename Impl, typename Interface> | |
134 Impl* WeakBindToRequest( | |
135 Impl* instance, | |
136 InterfaceRequest<Interface>* request, | |
137 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
138 return WeakBindToPipe(instance, request->PassMessagePipe(), waiter); | |
139 } | |
140 | |
115 } // namespace mojo | 141 } // namespace mojo |
116 | 142 |
117 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ | 143 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ |
OLD | NEW |