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
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_socket_net.h"
10 #include "base/logging.h"
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/sequenced_task_runner.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "device/bluetooth/bluetooth_socket.h"
17 #include "device/bluetooth/bluetooth_socket_thread.h"
18 #include "net/base/io_buffer.h"
19 #include "net/base/net_errors.h"
23 const char kSocketNotConnected[] = "Socket is not connected.";
25 static void DeactivateSocket(
26 const scoped_refptr<device::BluetoothSocketThread>& socket_thread) {
27 socket_thread->OnSocketDeactivate();
35 scoped_refptr<BluetoothSocketNet>
36 BluetoothSocketNet::CreateBluetoothSocket(
37 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
38 scoped_refptr<BluetoothSocketThread> socket_thread,
40 const net::NetLog::Source& source) {
41 DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
43 return make_scoped_refptr(
44 new BluetoothSocketNet(ui_task_runner, socket_thread, net_log, source));
47 BluetoothSocketNet::WriteRequest::WriteRequest()
50 BluetoothSocketNet::WriteRequest::~WriteRequest() {}
52 BluetoothSocketNet::BluetoothSocketNet(
53 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
54 scoped_refptr<BluetoothSocketThread> socket_thread,
56 const net::NetLog::Source& source)
57 : ui_task_runner_(ui_task_runner),
58 socket_thread_(socket_thread),
61 DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
62 socket_thread_->OnSocketActivate();
65 BluetoothSocketNet::~BluetoothSocketNet() {
66 DCHECK(tcp_socket_.get() == NULL);
67 ui_task_runner_->PostTask(FROM_HERE,
68 base::Bind(&DeactivateSocket, socket_thread_));
71 void BluetoothSocketNet::Close() {
72 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
73 socket_thread_->task_runner()->PostTask(
74 FROM_HERE, base::Bind(&BluetoothSocketNet::DoClose, this));
77 void BluetoothSocketNet::Disconnect(
78 const base::Closure& success_callback) {
79 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
80 socket_thread_->task_runner()->PostTask(
83 &BluetoothSocketNet::DoDisconnect,
85 base::Bind(&BluetoothSocketNet::PostSuccess,
90 void BluetoothSocketNet::Receive(
92 const ReceiveCompletionCallback& success_callback,
93 const ReceiveErrorCompletionCallback& error_callback) {
94 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
95 socket_thread_->task_runner()->PostTask(
98 &BluetoothSocketNet::DoReceive,
101 base::Bind(&BluetoothSocketNet::PostReceiveCompletion,
104 base::Bind(&BluetoothSocketNet::PostReceiveErrorCompletion,
109 void BluetoothSocketNet::Send(
110 scoped_refptr<net::IOBuffer> buffer,
112 const SendCompletionCallback& success_callback,
113 const ErrorCompletionCallback& error_callback) {
114 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
115 socket_thread_->task_runner()->PostTask(
118 &BluetoothSocketNet::DoSend,
122 base::Bind(&BluetoothSocketNet::PostSendCompletion,
125 base::Bind(&BluetoothSocketNet::PostErrorCompletion,
130 void BluetoothSocketNet::ResetData() {
133 void BluetoothSocketNet::ResetTCPSocket() {
134 tcp_socket_.reset(new net::TCPSocket(net_log_, source_));
137 void BluetoothSocketNet::SetTCPSocket(scoped_ptr<net::TCPSocket> tcp_socket) {
138 tcp_socket_ = tcp_socket.Pass();
141 void BluetoothSocketNet::PostSuccess(const base::Closure& callback) {
142 ui_task_runner_->PostTask(FROM_HERE, callback);
145 void BluetoothSocketNet::PostErrorCompletion(
146 const ErrorCompletionCallback& callback,
147 const std::string& error) {
148 ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, error));
151 void BluetoothSocketNet::DoClose() {
152 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread());
153 base::ThreadRestrictions::AssertIOAllowed();
156 tcp_socket_->Close();
157 tcp_socket_.reset(NULL);
160 // Note: Closing |tcp_socket_| above released all potential pending
161 // Send/Receive operations, so we can no safely release the state associated
162 // to those pending operations.
164 std::queue<linked_ptr<WriteRequest> > empty;
165 std::swap(write_queue_, empty);
170 void BluetoothSocketNet::DoDisconnect(const base::Closure& callback) {
171 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread());
172 base::ThreadRestrictions::AssertIOAllowed();
178 void BluetoothSocketNet::DoReceive(
180 const ReceiveCompletionCallback& success_callback,
181 const ReceiveErrorCompletionCallback& error_callback) {
182 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread());
183 base::ThreadRestrictions::AssertIOAllowed();
186 error_callback.Run(BluetoothSocket::kDisconnected, kSocketNotConnected);
190 // Only one pending read at a time
191 if (read_buffer_.get()) {
192 error_callback.Run(BluetoothSocket::kIOPending,
193 net::ErrorToString(net::ERR_IO_PENDING));
197 scoped_refptr<net::IOBufferWithSize> buffer(
198 new net::IOBufferWithSize(buffer_size));
200 tcp_socket_->Read(buffer.get(),
202 base::Bind(&BluetoothSocketNet::OnSocketReadComplete,
207 if (read_result > 0) {
208 success_callback.Run(read_result, buffer);
209 } else if (read_result == net::OK ||
210 read_result == net::ERR_CONNECTION_CLOSED) {
211 error_callback.Run(BluetoothSocket::kDisconnected,
212 net::ErrorToString(net::ERR_CONNECTION_CLOSED));
213 } else if (read_result == net::ERR_IO_PENDING) {
214 read_buffer_ = buffer;
216 error_callback.Run(BluetoothSocket::kSystemError,
217 net::ErrorToString(read_result));
221 void BluetoothSocketNet::OnSocketReadComplete(
222 const ReceiveCompletionCallback& success_callback,
223 const ReceiveErrorCompletionCallback& error_callback,
225 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread());
226 base::ThreadRestrictions::AssertIOAllowed();
228 scoped_refptr<net::IOBufferWithSize> buffer;
229 buffer.swap(read_buffer_);
230 if (read_result > 0) {
231 success_callback.Run(read_result, buffer);
232 } else if (read_result == net::OK ||
233 read_result == net::ERR_CONNECTION_CLOSED) {
234 error_callback.Run(BluetoothSocket::kDisconnected,
235 net::ErrorToString(net::ERR_CONNECTION_CLOSED));
237 error_callback.Run(BluetoothSocket::kSystemError,
238 net::ErrorToString(read_result));
242 void BluetoothSocketNet::DoSend(
243 scoped_refptr<net::IOBuffer> buffer,
245 const SendCompletionCallback& success_callback,
246 const ErrorCompletionCallback& error_callback) {
247 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread());
248 base::ThreadRestrictions::AssertIOAllowed();
251 error_callback.Run(kSocketNotConnected);
255 linked_ptr<WriteRequest> request(new WriteRequest());
256 request->buffer = buffer;
257 request->buffer_size = buffer_size;
258 request->success_callback = success_callback;
259 request->error_callback = error_callback;
261 write_queue_.push(request);
262 if (write_queue_.size() == 1) {
263 SendFrontWriteRequest();
267 void BluetoothSocketNet::SendFrontWriteRequest() {
268 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread());
269 base::ThreadRestrictions::AssertIOAllowed();
274 if (write_queue_.size() == 0)
277 linked_ptr<WriteRequest> request = write_queue_.front();
278 net::CompletionCallback callback =
279 base::Bind(&BluetoothSocketNet::OnSocketWriteComplete,
281 request->success_callback,
282 request->error_callback);
284 tcp_socket_->Write(request->buffer, request->buffer_size, callback);
285 if (send_result != net::ERR_IO_PENDING) {
286 callback.Run(send_result);
290 void BluetoothSocketNet::OnSocketWriteComplete(
291 const SendCompletionCallback& success_callback,
292 const ErrorCompletionCallback& error_callback,
294 DCHECK(socket_thread_->task_runner()->RunsTasksOnCurrentThread());
295 base::ThreadRestrictions::AssertIOAllowed();
299 if (send_result >= net::OK) {
300 success_callback.Run(send_result);
302 error_callback.Run(net::ErrorToString(send_result));
305 // Don't call directly to avoid potentail large recursion.
306 socket_thread_->task_runner()->PostNonNestableTask(
308 base::Bind(&BluetoothSocketNet::SendFrontWriteRequest, this));
311 void BluetoothSocketNet::PostReceiveCompletion(
312 const ReceiveCompletionCallback& callback,
314 scoped_refptr<net::IOBuffer> io_buffer) {
315 ui_task_runner_->PostTask(FROM_HERE,
316 base::Bind(callback, io_buffer_size, io_buffer));
319 void BluetoothSocketNet::PostReceiveErrorCompletion(
320 const ReceiveErrorCompletionCallback& callback,
322 const std::string& error_message) {
323 ui_task_runner_->PostTask(FROM_HERE,
324 base::Bind(callback, reason, error_message));
327 void BluetoothSocketNet::PostSendCompletion(
328 const SendCompletionCallback& callback,
330 ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, bytes_written));
333 } // namespace device