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

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

Issue 2318793002: Mojo C++ bindings: support disconnect with a reason. (Closed)
Patch Set: . Created 4 years, 3 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 | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/associated_interface_ptr.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 <utility> 10 #include <utility>
10 11
11 #include "base/bind.h" 12 #include "base/bind.h"
12 #include "base/callback.h" 13 #include "base/callback.h"
13 #include "base/macros.h" 14 #include "base/macros.h"
14 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
15 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
16 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
17 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
18 #include "mojo/public/cpp/bindings/associated_group.h" 19 #include "mojo/public/cpp/bindings/associated_group.h"
19 #include "mojo/public/cpp/bindings/associated_group_controller.h" 20 #include "mojo/public/cpp/bindings/associated_group_controller.h"
20 #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"
21 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" 23 #include "mojo/public/cpp/bindings/interface_endpoint_client.h"
22 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" 24 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h"
23 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" 25 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
24 26
25 namespace mojo { 27 namespace mojo {
26 28
27 class MessageReceiver; 29 class MessageReceiver;
28 30
29 // Represents the implementation side of an associated interface. It is similar 31 // Represents the implementation side of an associated interface. It is similar
30 // to Binding, except that it doesn't own a message pipe handle. 32 // to Binding, except that it doesn't own a message pipe handle.
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 100
99 if (!handle.is_valid() || !handle.is_local()) { 101 if (!handle.is_valid() || !handle.is_local()) {
100 endpoint_client_.reset(); 102 endpoint_client_.reset();
101 return; 103 return;
102 } 104 }
103 105
104 endpoint_client_.reset(new InterfaceEndpointClient( 106 endpoint_client_.reset(new InterfaceEndpointClient(
105 std::move(handle), &stub_, 107 std::move(handle), &stub_,
106 base::WrapUnique(new typename Interface::RequestValidator_()), 108 base::WrapUnique(new typename Interface::RequestValidator_()),
107 Interface::HasSyncMethods_, std::move(runner), Interface::Version_)); 109 Interface::HasSyncMethods_, std::move(runner), Interface::Version_));
108 endpoint_client_->set_connection_error_handler(
109 base::Bind(&AssociatedBinding::RunConnectionErrorHandler,
110 base::Unretained(this)));
111 110
112 stub_.serialization_context()->group_controller = 111 stub_.serialization_context()->group_controller =
113 endpoint_client_->group_controller(); 112 endpoint_client_->group_controller();
114 } 113 }
115 114
116 // Adds a message filter to be notified of each incoming message before 115 // Adds a message filter to be notified of each incoming message before
117 // dispatch. If a filter returns |false| from Accept(), the message is not 116 // dispatch. If a filter returns |false| from Accept(), the message is not
118 // dispatched and the pipe is closed. Filters cannot be removed. 117 // dispatched and the pipe is closed. Filters cannot be removed.
119 void AddFilter(std::unique_ptr<MessageReceiver> filter) { 118 void AddFilter(std::unique_ptr<MessageReceiver> filter) {
120 DCHECK(endpoint_client_); 119 DCHECK(endpoint_client_);
121 endpoint_client_->AddFilter(std::move(filter)); 120 endpoint_client_->AddFilter(std::move(filter));
122 } 121 }
123 122
124 // Closes the associated interface. Puts this object into a state where it can 123 // Closes the associated interface. Puts this object into a state where it can
125 // be rebound. 124 // be rebound.
126 void Close() { 125 void Close() {
127 DCHECK(endpoint_client_); 126 DCHECK(endpoint_client_);
128 endpoint_client_.reset(); 127 endpoint_client_.reset();
129 connection_error_handler_.Reset(); 128 }
129
130 // Similar to the method above, but also specifies a disconnect reason.
131 void CloseWithReason(uint32_t custom_reason, const std::string& description) {
132 DCHECK(endpoint_client_);
133 endpoint_client_->control_message_proxy()->SendDisconnectReason(
134 custom_reason, description);
135 Close();
130 } 136 }
131 137
132 // Unbinds and returns the associated interface request so it can be 138 // Unbinds and returns the associated interface request so it can be
133 // used in another context, such as on another thread or with a different 139 // used in another context, such as on another thread or with a different
134 // implementation. Puts this object into a state where it can be rebound. 140 // implementation. Puts this object into a state where it can be rebound.
135 AssociatedInterfaceRequest<Interface> Unbind() { 141 AssociatedInterfaceRequest<Interface> Unbind() {
136 DCHECK(endpoint_client_); 142 DCHECK(endpoint_client_);
137 143
138 AssociatedInterfaceRequest<Interface> request; 144 AssociatedInterfaceRequest<Interface> request;
139 request.Bind(endpoint_client_->PassHandle()); 145 request.Bind(endpoint_client_->PassHandle());
140 146
141 endpoint_client_.reset(); 147 endpoint_client_.reset();
142 connection_error_handler_.Reset();
143 148
144 return request; 149 return request;
145 } 150 }
146 151
147 // Sets an error handler that will be called if a connection error occurs. 152 // Sets an error handler that will be called if a connection error occurs.
148 // 153 //
149 // This method may only be called after this AssociatedBinding has been bound 154 // This method may only be called after this AssociatedBinding has been bound
150 // to a message pipe. The error handler will be reset when this 155 // to a message pipe. The error handler will be reset when this
151 // AssociatedBinding is unbound or closed. 156 // AssociatedBinding is unbound or closed.
152 void set_connection_error_handler(const base::Closure& error_handler) { 157 void set_connection_error_handler(const base::Closure& error_handler) {
153 DCHECK(is_bound()); 158 DCHECK(is_bound());
154 connection_error_handler_ = error_handler; 159 endpoint_client_->set_connection_error_handler(error_handler);
160 }
161
162 void set_connection_error_with_reason_handler(
163 const ConnectionErrorWithReasonCallback& error_handler) {
164 DCHECK(is_bound());
165 endpoint_client_->set_connection_error_with_reason_handler(error_handler);
155 } 166 }
156 167
157 // Returns the interface implementation that was previously specified. 168 // Returns the interface implementation that was previously specified.
158 Interface* impl() { return impl_; } 169 Interface* impl() { return impl_; }
159 170
160 // Indicates whether the associated binding has been completed. 171 // Indicates whether the associated binding has been completed.
161 bool is_bound() const { return !!endpoint_client_; } 172 bool is_bound() const { return !!endpoint_client_; }
162 173
163 // Returns the associated group that this object belongs to. Returns null if 174 // Returns the associated group that this object belongs to. Returns null if
164 // the object is not bound. 175 // the object is not bound.
165 AssociatedGroup* associated_group() { 176 AssociatedGroup* associated_group() {
166 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr; 177 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr;
167 } 178 }
168 179
169 // Sends a message on the underlying message pipe and runs the current 180 // Sends a message on the underlying message pipe and runs the current
170 // message loop until its response is received. This can be used in tests to 181 // message loop until its response is received. This can be used in tests to
171 // verify that no message was sent on a message pipe in response to some 182 // verify that no message was sent on a message pipe in response to some
172 // stimulus. 183 // stimulus.
173 void FlushForTesting() { 184 void FlushForTesting() {
174 endpoint_client_->control_message_proxy()->FlushForTesting(); 185 endpoint_client_->control_message_proxy()->FlushForTesting();
175 } 186 }
176 187
177 private: 188 private:
178 void RunConnectionErrorHandler() {
179 if (!connection_error_handler_.is_null())
180 connection_error_handler_.Run();
181 }
182
183 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; 189 std::unique_ptr<InterfaceEndpointClient> endpoint_client_;
184 190
185 typename Interface::Stub_ stub_; 191 typename Interface::Stub_ stub_;
186 Interface* impl_; 192 Interface* impl_;
187 base::Closure connection_error_handler_;
188 193
189 DISALLOW_COPY_AND_ASSIGN(AssociatedBinding); 194 DISALLOW_COPY_AND_ASSIGN(AssociatedBinding);
190 }; 195 };
191 196
192 } // namespace mojo 197 } // namespace mojo
193 198
194 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ 199 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/associated_interface_ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698