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

Side by Side Diff: device/bluetooth/bluetooth_socket_net.cc

Issue 2891853003: Rename TaskRunner::RunsTasksOnCurrentThread() in //device, //services (Closed)
Patch Set: fixed build error Created 3 years, 7 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 #include "device/bluetooth/bluetooth_socket_net.h" 5 #include "device/bluetooth/bluetooth_socket_net.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <queue> 8 #include <queue>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 26 matching lines...) Expand all
37 BluetoothSocketNet::WriteRequest::WriteRequest() 37 BluetoothSocketNet::WriteRequest::WriteRequest()
38 : buffer_size(0) {} 38 : buffer_size(0) {}
39 39
40 BluetoothSocketNet::WriteRequest::~WriteRequest() {} 40 BluetoothSocketNet::WriteRequest::~WriteRequest() {}
41 41
42 BluetoothSocketNet::BluetoothSocketNet( 42 BluetoothSocketNet::BluetoothSocketNet(
43 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, 43 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
44 scoped_refptr<BluetoothSocketThread> socket_thread) 44 scoped_refptr<BluetoothSocketThread> socket_thread)
45 : ui_task_runner_(ui_task_runner), 45 : ui_task_runner_(ui_task_runner),
46 socket_thread_(socket_thread) { 46 socket_thread_(socket_thread) {
47 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); 47 DCHECK(ui_task_runner->RunsTasksInCurrentSequence());
48 socket_thread_->OnSocketActivate(); 48 socket_thread_->OnSocketActivate();
49 } 49 }
50 50
51 BluetoothSocketNet::~BluetoothSocketNet() { 51 BluetoothSocketNet::~BluetoothSocketNet() {
52 DCHECK(!tcp_socket_); 52 DCHECK(!tcp_socket_);
53 ui_task_runner_->PostTask(FROM_HERE, 53 ui_task_runner_->PostTask(FROM_HERE,
54 base::Bind(&DeactivateSocket, socket_thread_)); 54 base::Bind(&DeactivateSocket, socket_thread_));
55 } 55 }
56 56
57 void BluetoothSocketNet::Close() { 57 void BluetoothSocketNet::Close() {
58 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 58 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence());
59 socket_thread_->task_runner()->PostTask( 59 socket_thread_->task_runner()->PostTask(
60 FROM_HERE, base::Bind(&BluetoothSocketNet::DoClose, this)); 60 FROM_HERE, base::Bind(&BluetoothSocketNet::DoClose, this));
61 } 61 }
62 62
63 void BluetoothSocketNet::Disconnect( 63 void BluetoothSocketNet::Disconnect(
64 const base::Closure& success_callback) { 64 const base::Closure& success_callback) {
65 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 65 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence());
66 socket_thread_->task_runner()->PostTask( 66 socket_thread_->task_runner()->PostTask(
67 FROM_HERE, 67 FROM_HERE,
68 base::Bind( 68 base::Bind(
69 &BluetoothSocketNet::DoDisconnect, 69 &BluetoothSocketNet::DoDisconnect,
70 this, 70 this,
71 base::Bind(&BluetoothSocketNet::PostSuccess, 71 base::Bind(&BluetoothSocketNet::PostSuccess,
72 this, 72 this,
73 success_callback))); 73 success_callback)));
74 } 74 }
75 75
76 void BluetoothSocketNet::Receive( 76 void BluetoothSocketNet::Receive(
77 int buffer_size, 77 int buffer_size,
78 const ReceiveCompletionCallback& success_callback, 78 const ReceiveCompletionCallback& success_callback,
79 const ReceiveErrorCompletionCallback& error_callback) { 79 const ReceiveErrorCompletionCallback& error_callback) {
80 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 80 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence());
81 socket_thread_->task_runner()->PostTask( 81 socket_thread_->task_runner()->PostTask(
82 FROM_HERE, 82 FROM_HERE,
83 base::Bind( 83 base::Bind(
84 &BluetoothSocketNet::DoReceive, 84 &BluetoothSocketNet::DoReceive,
85 this, 85 this,
86 buffer_size, 86 buffer_size,
87 base::Bind(&BluetoothSocketNet::PostReceiveCompletion, 87 base::Bind(&BluetoothSocketNet::PostReceiveCompletion,
88 this, 88 this,
89 success_callback), 89 success_callback),
90 base::Bind(&BluetoothSocketNet::PostReceiveErrorCompletion, 90 base::Bind(&BluetoothSocketNet::PostReceiveErrorCompletion,
91 this, 91 this,
92 error_callback))); 92 error_callback)));
93 } 93 }
94 94
95 void BluetoothSocketNet::Send( 95 void BluetoothSocketNet::Send(
96 scoped_refptr<net::IOBuffer> buffer, 96 scoped_refptr<net::IOBuffer> buffer,
97 int buffer_size, 97 int buffer_size,
98 const SendCompletionCallback& success_callback, 98 const SendCompletionCallback& success_callback,
99 const ErrorCompletionCallback& error_callback) { 99 const ErrorCompletionCallback& error_callback) {
100 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 100 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence());
101 socket_thread_->task_runner()->PostTask( 101 socket_thread_->task_runner()->PostTask(
102 FROM_HERE, 102 FROM_HERE,
103 base::Bind( 103 base::Bind(
104 &BluetoothSocketNet::DoSend, 104 &BluetoothSocketNet::DoSend,
105 this, 105 this,
106 buffer, 106 buffer,
107 buffer_size, 107 buffer_size,
108 base::Bind(&BluetoothSocketNet::PostSendCompletion, 108 base::Bind(&BluetoothSocketNet::PostSendCompletion,
109 this, 109 this,
110 success_callback), 110 success_callback),
(...skipping 18 matching lines...) Expand all
129 ui_task_runner_->PostTask(FROM_HERE, callback); 129 ui_task_runner_->PostTask(FROM_HERE, callback);
130 } 130 }
131 131
132 void BluetoothSocketNet::PostErrorCompletion( 132 void BluetoothSocketNet::PostErrorCompletion(
133 const ErrorCompletionCallback& callback, 133 const ErrorCompletionCallback& callback,
134 const std::string& error) { 134 const std::string& error) {
135 ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, error)); 135 ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, error));
136 } 136 }
137 137
138 void BluetoothSocketNet::DoClose() { 138 void BluetoothSocketNet::DoClose() {
139 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); 139 DCHECK(socket_thread_->task_runner()->RunsTasksInCurrentSequence());
140 base::ThreadRestrictions::AssertIOAllowed(); 140 base::ThreadRestrictions::AssertIOAllowed();
141 141
142 if (tcp_socket_) { 142 if (tcp_socket_) {
143 tcp_socket_->Close(); 143 tcp_socket_->Close();
144 tcp_socket_.reset(NULL); 144 tcp_socket_.reset(NULL);
145 } 145 }
146 146
147 // Note: Closing |tcp_socket_| above released all potential pending 147 // Note: Closing |tcp_socket_| above released all potential pending
148 // Send/Receive operations, so we can no safely release the state associated 148 // Send/Receive operations, so we can no safely release the state associated
149 // to those pending operations. 149 // to those pending operations.
150 read_buffer_ = NULL; 150 read_buffer_ = NULL;
151 std::queue<linked_ptr<WriteRequest> > empty; 151 std::queue<linked_ptr<WriteRequest> > empty;
152 std::swap(write_queue_, empty); 152 std::swap(write_queue_, empty);
153 153
154 ResetData(); 154 ResetData();
155 } 155 }
156 156
157 void BluetoothSocketNet::DoDisconnect(const base::Closure& callback) { 157 void BluetoothSocketNet::DoDisconnect(const base::Closure& callback) {
158 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); 158 DCHECK(socket_thread_->task_runner()->RunsTasksInCurrentSequence());
159 base::ThreadRestrictions::AssertIOAllowed(); 159 base::ThreadRestrictions::AssertIOAllowed();
160 160
161 DoClose(); 161 DoClose();
162 callback.Run(); 162 callback.Run();
163 } 163 }
164 164
165 void BluetoothSocketNet::DoReceive( 165 void BluetoothSocketNet::DoReceive(
166 int buffer_size, 166 int buffer_size,
167 const ReceiveCompletionCallback& success_callback, 167 const ReceiveCompletionCallback& success_callback,
168 const ReceiveErrorCompletionCallback& error_callback) { 168 const ReceiveErrorCompletionCallback& error_callback) {
169 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); 169 DCHECK(socket_thread_->task_runner()->RunsTasksInCurrentSequence());
170 base::ThreadRestrictions::AssertIOAllowed(); 170 base::ThreadRestrictions::AssertIOAllowed();
171 171
172 if (!tcp_socket_) { 172 if (!tcp_socket_) {
173 error_callback.Run(BluetoothSocket::kDisconnected, kSocketNotConnected); 173 error_callback.Run(BluetoothSocket::kDisconnected, kSocketNotConnected);
174 return; 174 return;
175 } 175 }
176 176
177 // Only one pending read at a time 177 // Only one pending read at a time
178 if (read_buffer_.get()) { 178 if (read_buffer_.get()) {
179 error_callback.Run(BluetoothSocket::kIOPending, 179 error_callback.Run(BluetoothSocket::kIOPending,
(...skipping 13 matching lines...) Expand all
193 193
194 read_buffer_ = buffer; 194 read_buffer_ = buffer;
195 if (read_result != net::ERR_IO_PENDING) 195 if (read_result != net::ERR_IO_PENDING)
196 OnSocketReadComplete(success_callback, error_callback, read_result); 196 OnSocketReadComplete(success_callback, error_callback, read_result);
197 } 197 }
198 198
199 void BluetoothSocketNet::OnSocketReadComplete( 199 void BluetoothSocketNet::OnSocketReadComplete(
200 const ReceiveCompletionCallback& success_callback, 200 const ReceiveCompletionCallback& success_callback,
201 const ReceiveErrorCompletionCallback& error_callback, 201 const ReceiveErrorCompletionCallback& error_callback,
202 int read_result) { 202 int read_result) {
203 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); 203 DCHECK(socket_thread_->task_runner()->RunsTasksInCurrentSequence());
204 base::ThreadRestrictions::AssertIOAllowed(); 204 base::ThreadRestrictions::AssertIOAllowed();
205 205
206 scoped_refptr<net::IOBufferWithSize> buffer; 206 scoped_refptr<net::IOBufferWithSize> buffer;
207 buffer.swap(read_buffer_); 207 buffer.swap(read_buffer_);
208 if (read_result > 0) { 208 if (read_result > 0) {
209 success_callback.Run(read_result, buffer); 209 success_callback.Run(read_result, buffer);
210 } else if (read_result == net::OK || 210 } else if (read_result == net::OK ||
211 read_result == net::ERR_CONNECTION_CLOSED || 211 read_result == net::ERR_CONNECTION_CLOSED ||
212 read_result == net::ERR_CONNECTION_RESET) { 212 read_result == net::ERR_CONNECTION_RESET) {
213 error_callback.Run(BluetoothSocket::kDisconnected, 213 error_callback.Run(BluetoothSocket::kDisconnected,
214 net::ErrorToString(read_result)); 214 net::ErrorToString(read_result));
215 } else { 215 } else {
216 error_callback.Run(BluetoothSocket::kSystemError, 216 error_callback.Run(BluetoothSocket::kSystemError,
217 net::ErrorToString(read_result)); 217 net::ErrorToString(read_result));
218 } 218 }
219 } 219 }
220 220
221 void BluetoothSocketNet::DoSend( 221 void BluetoothSocketNet::DoSend(
222 scoped_refptr<net::IOBuffer> buffer, 222 scoped_refptr<net::IOBuffer> buffer,
223 int buffer_size, 223 int buffer_size,
224 const SendCompletionCallback& success_callback, 224 const SendCompletionCallback& success_callback,
225 const ErrorCompletionCallback& error_callback) { 225 const ErrorCompletionCallback& error_callback) {
226 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); 226 DCHECK(socket_thread_->task_runner()->RunsTasksInCurrentSequence());
227 base::ThreadRestrictions::AssertIOAllowed(); 227 base::ThreadRestrictions::AssertIOAllowed();
228 228
229 if (!tcp_socket_) { 229 if (!tcp_socket_) {
230 error_callback.Run(kSocketNotConnected); 230 error_callback.Run(kSocketNotConnected);
231 return; 231 return;
232 } 232 }
233 233
234 linked_ptr<WriteRequest> request(new WriteRequest()); 234 linked_ptr<WriteRequest> request(new WriteRequest());
235 request->buffer = buffer; 235 request->buffer = buffer;
236 request->buffer_size = buffer_size; 236 request->buffer_size = buffer_size;
237 request->success_callback = success_callback; 237 request->success_callback = success_callback;
238 request->error_callback = error_callback; 238 request->error_callback = error_callback;
239 239
240 write_queue_.push(request); 240 write_queue_.push(request);
241 if (write_queue_.size() == 1) { 241 if (write_queue_.size() == 1) {
242 SendFrontWriteRequest(); 242 SendFrontWriteRequest();
243 } 243 }
244 } 244 }
245 245
246 void BluetoothSocketNet::SendFrontWriteRequest() { 246 void BluetoothSocketNet::SendFrontWriteRequest() {
247 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); 247 DCHECK(socket_thread_->task_runner()->RunsTasksInCurrentSequence());
248 base::ThreadRestrictions::AssertIOAllowed(); 248 base::ThreadRestrictions::AssertIOAllowed();
249 249
250 if (!tcp_socket_) 250 if (!tcp_socket_)
251 return; 251 return;
252 252
253 if (write_queue_.size() == 0) 253 if (write_queue_.size() == 0)
254 return; 254 return;
255 255
256 linked_ptr<WriteRequest> request = write_queue_.front(); 256 linked_ptr<WriteRequest> request = write_queue_.front();
257 net::CompletionCallback callback = 257 net::CompletionCallback callback =
258 base::Bind(&BluetoothSocketNet::OnSocketWriteComplete, 258 base::Bind(&BluetoothSocketNet::OnSocketWriteComplete,
259 this, 259 this,
260 request->success_callback, 260 request->success_callback,
261 request->error_callback); 261 request->error_callback);
262 int send_result = 262 int send_result =
263 tcp_socket_->Write(request->buffer.get(), request->buffer_size, callback); 263 tcp_socket_->Write(request->buffer.get(), request->buffer_size, callback);
264 if (send_result != net::ERR_IO_PENDING) { 264 if (send_result != net::ERR_IO_PENDING) {
265 callback.Run(send_result); 265 callback.Run(send_result);
266 } 266 }
267 } 267 }
268 268
269 void BluetoothSocketNet::OnSocketWriteComplete( 269 void BluetoothSocketNet::OnSocketWriteComplete(
270 const SendCompletionCallback& success_callback, 270 const SendCompletionCallback& success_callback,
271 const ErrorCompletionCallback& error_callback, 271 const ErrorCompletionCallback& error_callback,
272 int send_result) { 272 int send_result) {
273 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread()); 273 DCHECK(socket_thread_->task_runner()->RunsTasksInCurrentSequence());
274 base::ThreadRestrictions::AssertIOAllowed(); 274 base::ThreadRestrictions::AssertIOAllowed();
275 275
276 write_queue_.pop(); 276 write_queue_.pop();
277 277
278 if (send_result >= net::OK) { 278 if (send_result >= net::OK) {
279 success_callback.Run(send_result); 279 success_callback.Run(send_result);
280 } else { 280 } else {
281 error_callback.Run(net::ErrorToString(send_result)); 281 error_callback.Run(net::ErrorToString(send_result));
282 } 282 }
283 283
(...skipping 19 matching lines...) Expand all
303 base::Bind(callback, reason, error_message)); 303 base::Bind(callback, reason, error_message));
304 } 304 }
305 305
306 void BluetoothSocketNet::PostSendCompletion( 306 void BluetoothSocketNet::PostSendCompletion(
307 const SendCompletionCallback& callback, 307 const SendCompletionCallback& callback,
308 int bytes_written) { 308 int bytes_written) {
309 ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, bytes_written)); 309 ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, bytes_written));
310 } 310 }
311 311
312 } // namespace device 312 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_remote_gatt_service_win.cc ('k') | device/bluetooth/bluetooth_socket_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698