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

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

Issue 2403533003: Mojo C++ Bindings: Support custom impl ref types (Closed)
Patch Set: nit, move Created 4 years, 2 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
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/binding.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_ASSOCIATED_BINDING_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
17 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
18 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
19 #include "mojo/public/cpp/bindings/associated_group.h" 19 #include "mojo/public/cpp/bindings/associated_group.h"
20 #include "mojo/public/cpp/bindings/associated_group_controller.h" 20 #include "mojo/public/cpp/bindings/associated_group_controller.h"
21 #include "mojo/public/cpp/bindings/associated_interface_request.h" 21 #include "mojo/public/cpp/bindings/associated_interface_request.h"
22 #include "mojo/public/cpp/bindings/connection_error_callback.h" 22 #include "mojo/public/cpp/bindings/connection_error_callback.h"
23 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" 23 #include "mojo/public/cpp/bindings/interface_endpoint_client.h"
24 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" 24 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h"
25 #include "mojo/public/cpp/bindings/raw_ptr_impl_ref_traits.h"
25 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" 26 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
26 27
27 namespace mojo { 28 namespace mojo {
28 29
29 class MessageReceiver; 30 class MessageReceiver;
30 31
31 // Represents the implementation side of an associated interface. It is similar 32 // Represents the implementation side of an associated interface. It is similar
32 // to Binding, except that it doesn't own a message pipe handle. 33 // to Binding, except that it doesn't own a message pipe handle.
33 // 34 //
34 // When you bind this class to a request, optionally you can specify a 35 // When you bind this class to a request, optionally you can specify a
35 // base::SingleThreadTaskRunner. This task runner must belong to the same 36 // base::SingleThreadTaskRunner. This task runner must belong to the same
36 // thread. It will be used to dispatch incoming method calls and connection 37 // thread. It will be used to dispatch incoming method calls and connection
37 // error notification. It is useful when you attach multiple task runners to a 38 // error notification. It is useful when you attach multiple task runners to a
38 // single thread for the purposes of task scheduling. Please note that incoming 39 // single thread for the purposes of task scheduling. Please note that incoming
39 // synchrounous method calls may not be run from this task runner, when they 40 // synchrounous method calls may not be run from this task runner, when they
40 // reenter outgoing synchrounous calls on the same thread. 41 // reenter outgoing synchrounous calls on the same thread.
41 template <typename Interface> 42 template <typename Interface,
43 typename ImplRefTraits = RawPtrImplRefTraits<Interface>>
42 class AssociatedBinding { 44 class AssociatedBinding {
43 public: 45 public:
46 using ImplPointerType = typename ImplRefTraits::PointerType;
47
44 // Constructs an incomplete associated binding that will use the 48 // Constructs an incomplete associated binding that will use the
45 // implementation |impl|. It may be completed with a subsequent call to the 49 // implementation |impl|. It may be completed with a subsequent call to the
46 // |Bind| method. Does not take ownership of |impl|, which must outlive this 50 // |Bind| method. Does not take ownership of |impl|, which must outlive this
47 // object. 51 // object.
48 explicit AssociatedBinding(Interface* impl) : impl_(impl) { 52 explicit AssociatedBinding(ImplPointerType impl) { stub_.set_sink(impl); }
49 stub_.set_sink(impl_);
50 }
51 53
52 // Constructs a completed associated binding of |impl|. The output |ptr_info| 54 // Constructs a completed associated binding of |impl|. The output |ptr_info|
53 // should be passed through the message pipe endpoint referred to by 55 // should be passed through the message pipe endpoint referred to by
54 // |associated_group| to setup the corresponding asssociated interface 56 // |associated_group| to setup the corresponding asssociated interface
55 // pointer. |impl| must outlive this object. 57 // pointer. |impl| must outlive this object.
56 AssociatedBinding(Interface* impl, 58 AssociatedBinding(ImplPointerType impl,
57 AssociatedInterfacePtrInfo<Interface>* ptr_info, 59 AssociatedInterfacePtrInfo<Interface>* ptr_info,
58 AssociatedGroup* associated_group, 60 AssociatedGroup* associated_group,
59 scoped_refptr<base::SingleThreadTaskRunner> runner = 61 scoped_refptr<base::SingleThreadTaskRunner> runner =
60 base::ThreadTaskRunnerHandle::Get()) 62 base::ThreadTaskRunnerHandle::Get())
61 : AssociatedBinding(impl) { 63 : AssociatedBinding(std::move(impl)) {
62 Bind(ptr_info, associated_group, std::move(runner)); 64 Bind(ptr_info, associated_group, std::move(runner));
63 } 65 }
64 66
65 // Constructs a completed associated binding of |impl|. |impl| must outlive 67 // Constructs a completed associated binding of |impl|. |impl| must outlive
66 // the binding. 68 // the binding.
67 AssociatedBinding(Interface* impl, 69 AssociatedBinding(ImplPointerType impl,
68 AssociatedInterfaceRequest<Interface> request, 70 AssociatedInterfaceRequest<Interface> request,
69 scoped_refptr<base::SingleThreadTaskRunner> runner = 71 scoped_refptr<base::SingleThreadTaskRunner> runner =
70 base::ThreadTaskRunnerHandle::Get()) 72 base::ThreadTaskRunnerHandle::Get())
71 : AssociatedBinding(impl) { 73 : AssociatedBinding(std::move(impl)) {
72 Bind(std::move(request), std::move(runner)); 74 Bind(std::move(request), std::move(runner));
73 } 75 }
74 76
75 ~AssociatedBinding() {} 77 ~AssociatedBinding() {}
76 78
77 // Creates an associated inteface and sets up this object as the 79 // Creates an associated inteface and sets up this object as the
78 // implementation side. The output |ptr_info| should be passed through the 80 // implementation side. The output |ptr_info| should be passed through the
79 // message pipe endpoint referred to by |associated_group| to setup the 81 // message pipe endpoint referred to by |associated_group| to setup the
80 // corresponding asssociated interface pointer. 82 // corresponding asssociated interface pointer.
81 void Bind(AssociatedInterfacePtrInfo<Interface>* ptr_info, 83 void Bind(AssociatedInterfacePtrInfo<Interface>* ptr_info,
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 endpoint_client_->set_connection_error_handler(error_handler); 161 endpoint_client_->set_connection_error_handler(error_handler);
160 } 162 }
161 163
162 void set_connection_error_with_reason_handler( 164 void set_connection_error_with_reason_handler(
163 const ConnectionErrorWithReasonCallback& error_handler) { 165 const ConnectionErrorWithReasonCallback& error_handler) {
164 DCHECK(is_bound()); 166 DCHECK(is_bound());
165 endpoint_client_->set_connection_error_with_reason_handler(error_handler); 167 endpoint_client_->set_connection_error_with_reason_handler(error_handler);
166 } 168 }
167 169
168 // Returns the interface implementation that was previously specified. 170 // Returns the interface implementation that was previously specified.
169 Interface* impl() { return impl_; } 171 Interface* impl() { return ImplRefTraits::GetRawPointer(&stub_.sink()); }
170 172
171 // Indicates whether the associated binding has been completed. 173 // Indicates whether the associated binding has been completed.
172 bool is_bound() const { return !!endpoint_client_; } 174 bool is_bound() const { return !!endpoint_client_; }
173 175
174 // Returns the associated group that this object belongs to. Returns null if 176 // Returns the associated group that this object belongs to. Returns null if
175 // the object is not bound. 177 // the object is not bound.
176 AssociatedGroup* associated_group() { 178 AssociatedGroup* associated_group() {
177 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr; 179 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr;
178 } 180 }
179 181
180 // Sends a message on the underlying message pipe and runs the current 182 // Sends a message on the underlying message pipe and runs the current
181 // message loop until its response is received. This can be used in tests to 183 // message loop until its response is received. This can be used in tests to
182 // verify that no message was sent on a message pipe in response to some 184 // verify that no message was sent on a message pipe in response to some
183 // stimulus. 185 // stimulus.
184 void FlushForTesting() { 186 void FlushForTesting() {
185 endpoint_client_->control_message_proxy()->FlushForTesting(); 187 endpoint_client_->control_message_proxy()->FlushForTesting();
186 } 188 }
187 189
188 private: 190 private:
189 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; 191 std::unique_ptr<InterfaceEndpointClient> endpoint_client_;
190 192 typename Interface::template Stub_<ImplRefTraits> stub_;
191 typename Interface::Stub_ stub_;
192 Interface* impl_;
193 193
194 DISALLOW_COPY_AND_ASSIGN(AssociatedBinding); 194 DISALLOW_COPY_AND_ASSIGN(AssociatedBinding);
195 }; 195 };
196 196
197 } // namespace mojo 197 } // namespace mojo
198 198
199 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ 199 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_
OLDNEW
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/binding.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698