Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: mojo/public/cpp/bindings/interface_impl.h

Issue 380413003: Mojo: Use InterfaceFactory<Interface> for service registration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review feedback Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 */);
76 return instance;
77 }
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 */);
74 return instance; 86 return instance;
75 } 87 }
76 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 and then the instance will be
93 // deleted.
81 // 94 //
82 // The instance is also bound to the current thread. Its methods will only be 95 // 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 96 // 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. 97 // also be deleted, and along with it, its end point of the pipe will be closed.
85 // 98 //
86 // Before returning, the instance's OnConnectionEstablished method is called. 99 // Before returning, the instance's OnConnectionEstablished method is called.
87 template <typename Impl, typename Interface> 100 template <typename Impl, typename Interface>
88 Impl* BindToProxy( 101 Impl* BindToProxy(
89 Impl* instance, 102 Impl* instance,
90 InterfacePtr<Interface>* ptr, 103 InterfacePtr<Interface>* ptr,
91 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { 104 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
92 instance->internal_state()->BindProxy(ptr, waiter); 105 instance->internal_state()->BindProxy(
106 ptr, waiter, true /* delete_on_error */);
107 return instance;
108 }
109
110 // Same as BindToProxy but does not delete instance after a channel error.
darin (slow to review) 2014/07/15 18:44:08 nit: it'd be good to standardize on a comment for
111 template <typename Impl, typename Interface>
112 Impl* WeakBindToProxy(
113 Impl* instance,
114 InterfacePtr<Interface>* ptr,
115 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
116 instance->internal_state()->BindProxy(
117 ptr, waiter, false /* delete_on_error */);
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.
99 // OnConnectionError method will be called. 124 //
125 // The bind call passes ownership of the instance to the pipe. If the pipe is
126 // closed, the instance's OnConnectionError method will be called and then the
127 // instance will be deleted.
100 // 128 //
101 // The instance is also bound to the current thread. Its methods will only be 129 // 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 130 // 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. 131 // also be deleted, and along with it, its end point of the pipe will be closed.
104 // 132 //
105 // Before returning, the instance will receive a SetClient call, providing it 133 // 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. 134 // with a proxy to the client on the other end of the pipe.
107 template <typename Impl, typename Interface> 135 template <typename Impl, typename Interface>
108 Impl* BindToRequest( 136 Impl* BindToRequest(
109 Impl* instance, 137 Impl* instance,
110 InterfaceRequest<Interface>* request, 138 InterfaceRequest<Interface>* request,
111 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { 139 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
112 return BindToPipe(instance, request->PassMessagePipe(), waiter); 140 return BindToPipe(instance, request->PassMessagePipe(), waiter);
113 } 141 }
114 142
143 // This is the same as BindToRequest except that the lifetime of the instance is
144 // not bound to the pipe. If the pipe is closed, the instance's
145 // OnConnectionError
146 // method will be called but the instance will not be deleted.
147 template <typename Impl, typename Interface>
148 Impl* WeakBindToRequest(
149 Impl* instance,
150 InterfaceRequest<Interface>* request,
151 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
152 return WeakBindToPipe(instance, request->PassMessagePipe(), waiter);
153 }
154
115 } // namespace mojo 155 } // namespace mojo
116 156
117 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ 157 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698