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

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

Issue 2608163003: Change single-interface mojo bindings to use SequencedTaskRunner. (Closed)
Patch Set: Created 3 years, 10 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/sequenced_task_runner.h"
15 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/sequenced_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/bindings/raw_ptr_impl_ref_traits.h"
22 #include "mojo/public/cpp/system/core.h" 22 #include "mojo/public/cpp/system/core.h"
23 23
24 namespace mojo { 24 namespace mojo {
25 25
(...skipping 28 matching lines...) Expand all
54 // // delete FooImpl on connection errors. 54 // // delete FooImpl on connection errors.
55 // } 55 // }
56 // }; 56 // };
57 // 57 //
58 // This class is thread hostile while bound to a message pipe. All calls to this 58 // This class is thread hostile while bound to a message pipe. All calls to this
59 // class must be from the thread that bound it. The interface implementation's 59 // class must be from the thread that bound it. The interface implementation's
60 // 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
61 // 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.
62 // 62 //
63 // 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
64 // base::SingleThreadTaskRunner. This task runner must belong to the same 64 // base::SequencedTaskRunner. This task runner must belong to the same
65 // 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
66 // 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
67 // 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
68 // 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
69 // reenter outgoing synchrounous calls on the same thread. 69 // reenter outgoing synchrounous calls on the same thread.
70 template <typename Interface, 70 template <typename Interface,
71 typename ImplRefTraits = RawPtrImplRefTraits<Interface>> 71 typename ImplRefTraits = RawPtrImplRefTraits<Interface>>
72 class Binding { 72 class Binding {
73 public: 73 public:
74 using ImplPointerType = typename ImplRefTraits::PointerType; 74 using ImplPointerType = typename ImplRefTraits::PointerType;
75 75
76 // Constructs an incomplete binding that will use the implementation |impl|. 76 // Constructs an incomplete binding that will use the implementation |impl|.
77 // 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.
78 // Does not take ownership of |impl|, which must outlive the binding. 78 // Does not take ownership of |impl|, which must outlive the binding.
79 explicit Binding(ImplPointerType impl) : internal_state_(std::move(impl)) {} 79 explicit Binding(ImplPointerType impl) : internal_state_(std::move(impl)) {}
80 80
81 // Constructs a completed binding of message pipe |handle| to implementation 81 // Constructs a completed binding of message pipe |handle| to implementation
82 // |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.
83 Binding(ImplPointerType impl, 83 Binding(ImplPointerType impl,
84 ScopedMessagePipeHandle handle, 84 ScopedMessagePipeHandle handle,
85 scoped_refptr<base::SingleThreadTaskRunner> runner = 85 scoped_refptr<base::SequencedTaskRunner> runner =
86 base::ThreadTaskRunnerHandle::Get()) 86 base::SequencedTaskRunnerHandle::Get())
87 : Binding(std::move(impl)) { 87 : Binding(std::move(impl)) {
88 Bind(std::move(handle), std::move(runner)); 88 Bind(std::move(handle), std::move(runner));
89 } 89 }
90 90
91 // 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
92 // 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
93 // 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
94 // 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
95 // last until the constructor returns. 95 // last until the constructor returns.
96 Binding(ImplPointerType impl, 96 Binding(ImplPointerType impl,
97 InterfacePtr<Interface>* ptr, 97 InterfacePtr<Interface>* ptr,
98 scoped_refptr<base::SingleThreadTaskRunner> runner = 98 scoped_refptr<base::SequencedTaskRunner> runner =
99 base::ThreadTaskRunnerHandle::Get()) 99 base::SequencedTaskRunnerHandle::Get())
100 : Binding(std::move(impl)) { 100 : Binding(std::move(impl)) {
101 Bind(ptr, std::move(runner)); 101 Bind(ptr, std::move(runner));
102 } 102 }
103 103
104 // 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
105 // |request|, taking ownership of the endpoint. Does not take ownership of 105 // |request|, taking ownership of the endpoint. Does not take ownership of
106 // |impl|, which must outlive the binding. 106 // |impl|, which must outlive the binding.
107 Binding(ImplPointerType impl, 107 Binding(ImplPointerType impl,
108 InterfaceRequest<Interface> request, 108 InterfaceRequest<Interface> request,
109 scoped_refptr<base::SingleThreadTaskRunner> runner = 109 scoped_refptr<base::SequencedTaskRunner> runner =
110 base::ThreadTaskRunnerHandle::Get()) 110 base::SequencedTaskRunnerHandle::Get())
111 : Binding(std::move(impl)) { 111 : Binding(std::move(impl)) {
112 Bind(request.PassMessagePipe(), std::move(runner)); 112 Bind(request.PassMessagePipe(), std::move(runner));
113 } 113 }
114 114
115 // 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
116 // implementation unbound. 116 // implementation unbound.
117 ~Binding() {} 117 ~Binding() {}
118 118
119 // 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
120 // bound to |this|. 120 // bound to |this|.
121 InterfacePtr<Interface> CreateInterfacePtrAndBind( 121 InterfacePtr<Interface> CreateInterfacePtrAndBind(
122 scoped_refptr<base::SingleThreadTaskRunner> runner = 122 scoped_refptr<base::SequencedTaskRunner> runner =
123 base::ThreadTaskRunnerHandle::Get()) { 123 base::SequencedTaskRunnerHandle::Get()) {
124 InterfacePtr<Interface> interface_ptr; 124 InterfacePtr<Interface> interface_ptr;
125 Bind(&interface_ptr, std::move(runner)); 125 Bind(&interface_ptr, std::move(runner));
126 return interface_ptr; 126 return interface_ptr;
127 } 127 }
128 128
129 // Completes a binding that was constructed with only an interface 129 // Completes a binding that was constructed with only an interface
130 // implementation. Takes ownership of |handle| and binds it to the previously 130 // implementation. Takes ownership of |handle| and binds it to the previously
131 // specified implementation. 131 // specified implementation.
132 void Bind(ScopedMessagePipeHandle handle, 132 void Bind(ScopedMessagePipeHandle handle,
133 scoped_refptr<base::SingleThreadTaskRunner> runner = 133 scoped_refptr<base::SequencedTaskRunner> runner =
134 base::ThreadTaskRunnerHandle::Get()) { 134 base::SequencedTaskRunnerHandle::Get()) {
135 internal_state_.Bind(std::move(handle), std::move(runner)); 135 internal_state_.Bind(std::move(handle), std::move(runner));
136 } 136 }
137 137
138 // Completes a binding that was constructed with only an interface 138 // Completes a binding that was constructed with only an interface
139 // implementation by creating a new message pipe, binding one end of it to the 139 // implementation by creating a new message pipe, binding one end of it to the
140 // previously specified implementation, and passing the other to |ptr|, which 140 // previously specified implementation, and passing the other to |ptr|, which
141 // takes ownership of it. The caller is expected to pass |ptr| on to the 141 // takes ownership of it. The caller is expected to pass |ptr| on to the
142 // eventual client of the service. Does not take ownership of |ptr|. 142 // eventual client of the service. Does not take ownership of |ptr|.
143 void Bind(InterfacePtr<Interface>* ptr, 143 void Bind(InterfacePtr<Interface>* ptr,
144 scoped_refptr<base::SingleThreadTaskRunner> runner = 144 scoped_refptr<base::SequencedTaskRunner> runner =
145 base::ThreadTaskRunnerHandle::Get()) { 145 base::SequencedTaskRunnerHandle::Get()) {
146 MessagePipe pipe; 146 MessagePipe pipe;
147 ptr->Bind(InterfacePtrInfo<Interface>(std::move(pipe.handle0), 147 ptr->Bind(InterfacePtrInfo<Interface>(std::move(pipe.handle0),
148 Interface::Version_), 148 Interface::Version_),
149 runner); 149 runner);
150 Bind(std::move(pipe.handle1), std::move(runner)); 150 Bind(std::move(pipe.handle1), std::move(runner));
151 } 151 }
152 152
153 // Completes a binding that was constructed with only an interface 153 // Completes a binding that was constructed with only an interface
154 // implementation by removing the message pipe endpoint from |request| and 154 // implementation by removing the message pipe endpoint from |request| and
155 // binding it to the previously specified implementation. 155 // binding it to the previously specified implementation.
156 void Bind(InterfaceRequest<Interface> request, 156 void Bind(InterfaceRequest<Interface> request,
157 scoped_refptr<base::SingleThreadTaskRunner> runner = 157 scoped_refptr<base::SequencedTaskRunner> runner =
158 base::ThreadTaskRunnerHandle::Get()) { 158 base::SequencedTaskRunnerHandle::Get()) {
159 Bind(request.PassMessagePipe(), std::move(runner)); 159 Bind(request.PassMessagePipe(), std::move(runner));
160 } 160 }
161 161
162 // Adds a message filter to be notified of each incoming message before 162 // Adds a message filter to be notified of each incoming message before
163 // dispatch. If a filter returns |false| from Accept(), the message is not 163 // dispatch. If a filter returns |false| from Accept(), the message is not
164 // dispatched and the pipe is closed. Filters cannot be removed. 164 // dispatched and the pipe is closed. Filters cannot be removed.
165 void AddFilter(std::unique_ptr<MessageReceiver> filter) { 165 void AddFilter(std::unique_ptr<MessageReceiver> filter) {
166 DCHECK(is_bound()); 166 DCHECK(is_bound());
167 internal_state_.AddFilter(std::move(filter)); 167 internal_state_.AddFilter(std::move(filter));
168 } 168 }
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 276
277 private: 277 private:
278 internal::BindingState<Interface, ImplRefTraits> internal_state_; 278 internal::BindingState<Interface, ImplRefTraits> internal_state_;
279 279
280 DISALLOW_COPY_AND_ASSIGN(Binding); 280 DISALLOW_COPY_AND_ASSIGN(Binding);
281 }; 281 };
282 282
283 } // namespace mojo 283 } // namespace mojo
284 284
285 #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_interface_request.h ('k') | mojo/public/cpp/bindings/connector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698