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

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

Issue 686883005: Provides a way to use bindings that doesn't require subclassing (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: review feedback Created 6 years, 1 month 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
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_binding.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/bindings/lib/interface_impl_internal.h"
10 #include "mojo/public/cpp/environment/environment.h" 11 #include "mojo/public/cpp/environment/environment.h"
11 #include "mojo/public/cpp/system/macros.h" 12 #include "mojo/public/cpp/system/macros.h"
12 13
13 namespace mojo { 14 namespace mojo {
14 15
16 // Mojo provides two ways to bind an implementation of a particular interface to
17 // a message pipe. The distinction lies in whether the binding logic is
18 // inherited from a superclass (InterfaceImpl) or whether the binding is managed
19 // separately from the class (InterfaceBinding). InterfaceImpl is generally
20 // simpler to use at the cost of flexibility.
21
15 // InterfaceImpl<..> is designed to be the base class of an interface 22 // 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 23 // implementation. It may be bound to a pipe or a proxy, see BindToPipe and
17 // BindToProxy. 24 // BindToProxy.
25 // See also InterfaceBinding.
18 template <typename Interface> 26 template <typename Interface>
19 class InterfaceImpl : public internal::InterfaceImplBase<Interface> { 27 class InterfaceImpl : public InterfaceBindingDelegate, public Interface {
20 public: 28 public:
21 typedef typename Interface::Client Client; 29 typedef typename Interface::Client Client;
22 typedef Interface ImplementedInterface; 30 typedef Interface ImplementedInterface;
23 31
24 InterfaceImpl() : internal_state_(this) {} 32 InterfaceImpl() : interface_binding_(nullptr) {}
25 virtual ~InterfaceImpl() {} 33 virtual ~InterfaceImpl() { delete interface_binding_; }
26 34
27 // Returns a proxy to the client interface. This is null upon construction, 35 // Returns a proxy to the client interface. This is null upon construction,
28 // and becomes non-null after OnClientConnected. NOTE: It remains non-null 36 // and becomes non-null after OnClientConnected. NOTE: It remains non-null
29 // until this instance is deleted. 37 // until this instance is deleted.
30 Client* client() { return internal_state_.client(); } 38 Client* client() { return internal_state()->client(); }
31 39
32 // Blocks the current thread for the first incoming method call, i.e., either 40 // 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 41 // 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 42 // has been called, |false| in case of error. It must only be called on a
35 // bound object. 43 // bound object.
36 bool WaitForIncomingMethodCall() { 44 bool WaitForIncomingMethodCall() {
37 return internal_state_.WaitForIncomingMethodCall(); 45 return internal_state()->WaitForIncomingMethodCall();
38 } 46 }
39 47
40 // Called when the client has connected to this instance.
41 virtual void OnConnectionEstablished() {}
42
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 void OnConnectionError() override {}
48 53
49 // DO NOT USE. Exposed only for internal use and for testing. 54 // DO NOT USE. Exposed only for internal use and for testing.
50 internal::InterfaceImplState<Interface>* internal_state() { 55 internal::InterfaceImplState<Interface>* internal_state() {
51 return &internal_state_; 56 return &interface_binding_->internal_state_;
57 }
58
59 // TODO(sky): move to private and friend functions that use.
60 void CreateInterfaceBinding() {
61 MOJO_DCHECK(!interface_binding_);
62 interface_binding_ = new InterfaceBinding<Interface>(this, this);
63 interface_binding_->set_owns_interface(false);
64 interface_binding_->set_delete_interface_on_error(true);
52 } 65 }
53 66
54 private: 67 private:
55 internal::InterfaceImplState<Interface> internal_state_; 68 InterfaceBinding<Interface>* interface_binding_;
69
56 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl); 70 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl);
57 }; 71 };
58 72
59 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given 73 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given
60 // MessagePipe. The instance is returned for convenience in member initializer 74 // MessagePipe. The instance is returned for convenience in member initializer
61 // lists, etc. 75 // lists, etc.
62 // 76 //
63 // If the pipe is closed, the instance's OnConnectionError method will be called 77 // If the pipe is closed, the instance's OnConnectionError method will be called
64 // and then the instance will be deleted. 78 // and then the instance will be deleted.
65 // 79 //
66 // The instance is also bound to the current thread. Its methods will only be 80 // 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 81 // 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 82 // point of the pipe will be closed and the error handler's OnConnectionError
69 // method will be called. 83 // method will be called.
70 // 84 //
71 // Before returning, the instance's OnConnectionEstablished method is called. 85 // Before returning, the instance's OnConnectionEstablished method is called.
72 template <typename Impl> 86 template <typename Impl,
87 typename Interface = typename Impl::ImplementedInterface>
73 Impl* BindToPipe( 88 Impl* BindToPipe(
74 Impl* instance, 89 Impl* instance,
75 ScopedMessagePipeHandle handle, 90 ScopedMessagePipeHandle handle,
76 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { 91 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
92 instance->CreateInterfaceBinding();
77 instance->internal_state()->Bind(handle.Pass(), true, waiter); 93 instance->internal_state()->Bind(handle.Pass(), true, waiter);
78 return instance; 94 return instance;
79 } 95 }
80 96
81 // Like BindToPipe but does not delete the instance after a channel error. 97 // Like BindToPipe but does not delete the instance after a channel error.
82 template <typename Impl> 98 template <typename Impl,
99 typename Interface = typename Impl::ImplementedInterface>
83 Impl* WeakBindToPipe( 100 Impl* WeakBindToPipe(
84 Impl* instance, 101 Impl* instance,
85 ScopedMessagePipeHandle handle, 102 ScopedMessagePipeHandle handle,
86 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { 103 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
104 instance->CreateInterfaceBinding();
87 instance->internal_state()->Bind(handle.Pass(), false, waiter); 105 instance->internal_state()->Bind(handle.Pass(), false, waiter);
88 return instance; 106 return instance;
89 } 107 }
90 108
91 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given 109 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given
92 // InterfacePtr<..>. The instance is returned for convenience in member 110 // InterfacePtr<..>. The instance is returned for convenience in member
93 // initializer lists, etc. If the pipe is closed, the instance's 111 // initializer lists, etc. If the pipe is closed, the instance's
94 // OnConnectionError method will be called and then the instance will be 112 // OnConnectionError method will be called and then the instance will be
95 // deleted. 113 // deleted.
96 // 114 //
97 // The instance is also bound to the current thread. Its methods will only be 115 // 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 116 // 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. 117 // also be deleted, and along with it, its end point of the pipe will be closed.
100 // 118 //
101 // Before returning, the instance's OnConnectionEstablished method is called. 119 // Before returning, the instance's OnConnectionEstablished method is called.
102 template <typename Impl, typename Interface> 120 template <typename Impl, typename Interface>
103 Impl* BindToProxy( 121 Impl* BindToProxy(
104 Impl* instance, 122 Impl* instance,
105 InterfacePtr<Interface>* ptr, 123 InterfacePtr<Interface>* ptr,
106 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { 124 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
125 instance->CreateInterfaceBinding();
107 instance->internal_state()->BindProxy(ptr, true, waiter); 126 instance->internal_state()->BindProxy(ptr, true, waiter);
108 return instance; 127 return instance;
109 } 128 }
110 129
111 // Like BindToProxy but does not delete the instance after a channel error. 130 // Like BindToProxy but does not delete the instance after a channel error.
112 template <typename Impl, typename Interface> 131 template <typename Impl, typename Interface>
113 Impl* WeakBindToProxy( 132 Impl* WeakBindToProxy(
114 Impl* instance, 133 Impl* instance,
115 InterfacePtr<Interface>* ptr, 134 InterfacePtr<Interface>* ptr,
116 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { 135 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
136 instance->CreateInterfaceBinding();
117 instance->internal_state()->BindProxy(ptr, false, waiter); 137 instance->internal_state()->BindProxy(ptr, false, waiter);
118 return instance; 138 return instance;
119 } 139 }
120 140
121 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given 141 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given
122 // InterfaceRequest<..>. The instance is returned for convenience in member 142 // InterfaceRequest<..>. The instance is returned for convenience in member
123 // initializer lists, etc. If the pipe is closed, the instance's 143 // initializer lists, etc. If the pipe is closed, the instance's
124 // OnConnectionError method will be called and then the instance will be 144 // OnConnectionError method will be called and then the instance will be
125 // deleted. 145 // deleted.
126 // 146 //
(...skipping 16 matching lines...) Expand all
143 Impl* WeakBindToRequest( 163 Impl* WeakBindToRequest(
144 Impl* instance, 164 Impl* instance,
145 InterfaceRequest<Interface>* request, 165 InterfaceRequest<Interface>* request,
146 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { 166 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
147 return WeakBindToPipe(instance, request->PassMessagePipe(), waiter); 167 return WeakBindToPipe(instance, request->PassMessagePipe(), waiter);
148 } 168 }
149 169
150 } // namespace mojo 170 } // namespace mojo
151 171
152 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ 172 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/interface_binding_delegate.h ('k') | mojo/public/cpp/bindings/lib/interface_impl_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698