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

Side by Side Diff: mojo/public/cpp/bindings/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
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_BINDING_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_
7 7
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/callback_forward.h" 11 #include "base/callback_forward.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/single_thread_task_runner.h" 14 #include "base/single_thread_task_runner.h"
15 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
16 #include "mojo/public/cpp/bindings/connection_error_callback.h" 16 #include "mojo/public/cpp/bindings/connection_error_callback.h"
17 #include "mojo/public/cpp/bindings/interface_ptr.h" 17 #include "mojo/public/cpp/bindings/interface_ptr.h"
18 #include "mojo/public/cpp/bindings/interface_ptr_info.h" 18 #include "mojo/public/cpp/bindings/interface_ptr_info.h"
19 #include "mojo/public/cpp/bindings/interface_request.h" 19 #include "mojo/public/cpp/bindings/interface_request.h"
20 #include "mojo/public/cpp/bindings/lib/binding_state.h" 20 #include "mojo/public/cpp/bindings/lib/binding_state.h"
21 #include "mojo/public/cpp/bindings/raw_ptr_impl_ref_traits.h"
21 #include "mojo/public/cpp/system/core.h" 22 #include "mojo/public/cpp/system/core.h"
22 23
23 namespace mojo { 24 namespace mojo {
24 25
25 class AssociatedGroup; 26 class AssociatedGroup;
26 class MessageReceiver; 27 class MessageReceiver;
27 28
28 // Represents the binding of an interface implementation to a message pipe. 29 // Represents the binding of an interface implementation to a message pipe.
29 // When the |Binding| object is destroyed, the binding between the message pipe 30 // When the |Binding| object is destroyed, the binding between the message pipe
30 // and the interface is torn down and the message pipe is closed, leaving the 31 // and the interface is torn down and the message pipe is closed, leaving the
(...skipping 28 matching lines...) Expand all
59 // methods will be called from the thread that bound this. If a Binding is not 60 // methods will be called from the thread that bound this. If a Binding is not
60 // bound to a message pipe, it may be bound or destroyed on any thread. 61 // bound to a message pipe, it may be bound or destroyed on any thread.
61 // 62 //
62 // When you bind this class to a message pipe, optionally you can specify a 63 // When you bind this class to a message pipe, optionally you can specify a
63 // base::SingleThreadTaskRunner. This task runner must belong to the same 64 // base::SingleThreadTaskRunner. This task runner must belong to the same
64 // thread. It will be used to dispatch incoming method calls and connection 65 // thread. It will be used to dispatch incoming method calls and connection
65 // error notification. It is useful when you attach multiple task runners to a 66 // error notification. It is useful when you attach multiple task runners to a
66 // single thread for the purposes of task scheduling. Please note that incoming 67 // single thread for the purposes of task scheduling. Please note that incoming
67 // synchrounous method calls may not be run from this task runner, when they 68 // synchrounous method calls may not be run from this task runner, when they
68 // reenter outgoing synchrounous calls on the same thread. 69 // reenter outgoing synchrounous calls on the same thread.
69 template <typename Interface> 70 template <typename Interface,
71 typename ImplRefTraits = RawPtrImplRefTraits<Interface>>
70 class Binding { 72 class Binding {
71 public: 73 public:
74 using ImplPointerType = typename ImplRefTraits::PointerType;
75
72 // Constructs an incomplete binding that will use the implementation |impl|. 76 // Constructs an incomplete binding that will use the implementation |impl|.
73 // The binding may be completed with a subsequent call to the |Bind| method. 77 // The binding may be completed with a subsequent call to the |Bind| method.
74 // Does not take ownership of |impl|, which must outlive the binding. 78 // Does not take ownership of |impl|, which must outlive the binding.
75 explicit Binding(Interface* impl) : internal_state_(impl) {} 79 explicit Binding(ImplPointerType impl) : internal_state_(std::move(impl)) {}
76 80
77 // Constructs a completed binding of message pipe |handle| to implementation 81 // Constructs a completed binding of message pipe |handle| to implementation
78 // |impl|. Does not take ownership of |impl|, which must outlive the binding. 82 // |impl|. Does not take ownership of |impl|, which must outlive the binding.
79 Binding(Interface* impl, 83 Binding(ImplPointerType impl,
80 ScopedMessagePipeHandle handle, 84 ScopedMessagePipeHandle handle,
81 scoped_refptr<base::SingleThreadTaskRunner> runner = 85 scoped_refptr<base::SingleThreadTaskRunner> runner =
82 base::ThreadTaskRunnerHandle::Get()) 86 base::ThreadTaskRunnerHandle::Get())
83 : Binding(impl) { 87 : Binding(std::move(impl)) {
84 Bind(std::move(handle), std::move(runner)); 88 Bind(std::move(handle), std::move(runner));
85 } 89 }
86 90
87 // Constructs a completed binding of |impl| to a new message pipe, passing the 91 // Constructs a completed binding of |impl| to a new message pipe, passing the
88 // client end to |ptr|, which takes ownership of it. The caller is expected to 92 // client end to |ptr|, which takes ownership of it. The caller is expected to
89 // pass |ptr| on to the client of the service. Does not take ownership of any 93 // pass |ptr| on to the client of the service. Does not take ownership of any
90 // of the parameters. |impl| must outlive the binding. |ptr| only needs to 94 // of the parameters. |impl| must outlive the binding. |ptr| only needs to
91 // last until the constructor returns. 95 // last until the constructor returns.
92 Binding(Interface* impl, 96 Binding(ImplPointerType impl,
93 InterfacePtr<Interface>* ptr, 97 InterfacePtr<Interface>* ptr,
94 scoped_refptr<base::SingleThreadTaskRunner> runner = 98 scoped_refptr<base::SingleThreadTaskRunner> runner =
95 base::ThreadTaskRunnerHandle::Get()) 99 base::ThreadTaskRunnerHandle::Get())
96 : Binding(impl) { 100 : Binding(std::move(impl)) {
97 Bind(ptr, std::move(runner)); 101 Bind(ptr, std::move(runner));
98 } 102 }
99 103
100 // Constructs a completed binding of |impl| to the message pipe endpoint in 104 // Constructs a completed binding of |impl| to the message pipe endpoint in
101 // |request|, taking ownership of the endpoint. Does not take ownership of 105 // |request|, taking ownership of the endpoint. Does not take ownership of
102 // |impl|, which must outlive the binding. 106 // |impl|, which must outlive the binding.
103 Binding(Interface* impl, 107 Binding(ImplPointerType impl,
104 InterfaceRequest<Interface> request, 108 InterfaceRequest<Interface> request,
105 scoped_refptr<base::SingleThreadTaskRunner> runner = 109 scoped_refptr<base::SingleThreadTaskRunner> runner =
106 base::ThreadTaskRunnerHandle::Get()) 110 base::ThreadTaskRunnerHandle::Get())
107 : Binding(impl) { 111 : Binding(std::move(impl)) {
108 Bind(request.PassMessagePipe(), std::move(runner)); 112 Bind(request.PassMessagePipe(), std::move(runner));
109 } 113 }
110 114
111 // Tears down the binding, closing the message pipe and leaving the interface 115 // Tears down the binding, closing the message pipe and leaving the interface
112 // implementation unbound. 116 // implementation unbound.
113 ~Binding() {} 117 ~Binding() {}
114 118
115 // Returns an InterfacePtr bound to one end of a pipe whose other end is 119 // Returns an InterfacePtr bound to one end of a pipe whose other end is
116 // bound to |this|. 120 // bound to |this|.
117 InterfacePtr<Interface> CreateInterfacePtrAndBind( 121 InterfacePtr<Interface> CreateInterfacePtrAndBind(
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 // Sends a no-op message on the underlying message pipe and runs the current 268 // Sends a no-op message on the underlying message pipe and runs the current
265 // message loop until its response is received. This can be used in tests to 269 // message loop until its response is received. This can be used in tests to
266 // verify that no message was sent on a message pipe in response to some 270 // verify that no message was sent on a message pipe in response to some
267 // stimulus. 271 // stimulus.
268 void FlushForTesting() { internal_state_.FlushForTesting(); } 272 void FlushForTesting() { internal_state_.FlushForTesting(); }
269 273
270 // Exposed for testing, should not generally be used. 274 // Exposed for testing, should not generally be used.
271 void EnableTestingMode() { internal_state_.EnableTestingMode(); } 275 void EnableTestingMode() { internal_state_.EnableTestingMode(); }
272 276
273 private: 277 private:
274 internal::BindingState<Interface, true> internal_state_; 278 internal::BindingState<Interface, true, ImplRefTraits> internal_state_;
275 279
276 DISALLOW_COPY_AND_ASSIGN(Binding); 280 DISALLOW_COPY_AND_ASSIGN(Binding);
277 }; 281 };
278 282
279 } // namespace mojo 283 } // namespace mojo
280 284
281 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ 285 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/associated_binding.h ('k') | mojo/public/cpp/bindings/lib/binding_state.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698