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. 4 5#ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_NET_H_ 6#define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_NET_H_ 7 8#include <queue> 9#include <string> 10 11#include "base/basictypes.h" 12#include "base/memory/linked_ptr.h" 13#include "base/memory/ref_counted.h" 14#include "base/memory/scoped_ptr.h" 15#include "base/sequenced_task_runner.h" 16#include "device/bluetooth/bluetooth_socket.h" 17#include "device/bluetooth/bluetooth_socket_thread.h" 18#include "net/socket/tcp_socket.h" 19 20namespace net { 21class IOBuffer; 22class IOBufferWithSize; 23} // namespace net 24 25namespace device { 26 27// This class is a base-class for implementations of BluetoothSocket that can 28// use net::TCPSocket. All public methods (including the factory method) must 29// be called on the UI thread, while underlying socket operations are 30// performed on a separate thread. 31class BluetoothSocketNet : public BluetoothSocket { 32 public: 33 // BluetoothSocket: 34 virtual void Close() OVERRIDE; 35 virtual void Disconnect(const base::Closure& callback) OVERRIDE; 36 virtual void Receive(int buffer_size, 37 const ReceiveCompletionCallback& success_callback, 38 const ReceiveErrorCompletionCallback& error_callback) 39 OVERRIDE; 40 virtual void Send(scoped_refptr<net::IOBuffer> buffer, 41 int buffer_size, 42 const SendCompletionCallback& success_callback, 43 const ErrorCompletionCallback& error_callback) OVERRIDE; 44 45 protected: 46 BluetoothSocketNet(scoped_refptr<base::SequencedTaskRunner> ui_task_runner, 47 scoped_refptr<BluetoothSocketThread> socket_thread); 48 virtual ~BluetoothSocketNet(); 49 50 // Resets locally held data after a socket is closed. Default implementation 51 // does nothing, subclasses may override. 52 virtual void ResetData(); 53 54 // Methods for subclasses to obtain the members. 55 scoped_refptr<base::SequencedTaskRunner> ui_task_runner() const { 56 return ui_task_runner_; 57 } 58 59 scoped_refptr<BluetoothSocketThread> socket_thread() const { 60 return socket_thread_; 61 } 62 63 net::TCPSocket* tcp_socket() { return tcp_socket_.get(); } 64 65 void ResetTCPSocket(); 66 void SetTCPSocket(scoped_ptr<net::TCPSocket> tcp_socket); 67 68 void PostSuccess(const base::Closure& callback); 69 void PostErrorCompletion(const ErrorCompletionCallback& callback, 70 const std::string& error); 71 72 private: 73 struct WriteRequest { 74 WriteRequest(); 75 ~WriteRequest(); 76 77 scoped_refptr<net::IOBuffer> buffer; 78 int buffer_size; 79 SendCompletionCallback success_callback; 80 ErrorCompletionCallback error_callback; 81 }; 82 83 void DoClose(); 84 void DoDisconnect(const base::Closure& callback); 85 void DoReceive(int buffer_size, 86 const ReceiveCompletionCallback& success_callback, 87 const ReceiveErrorCompletionCallback& error_callback); 88 void OnSocketReadComplete( 89 const ReceiveCompletionCallback& success_callback, 90 const ReceiveErrorCompletionCallback& error_callback, 91 int read_result); 92 void DoSend(scoped_refptr<net::IOBuffer> buffer, 93 int buffer_size, 94 const SendCompletionCallback& success_callback, 95 const ErrorCompletionCallback& error_callback); 96 void SendFrontWriteRequest(); 97 void OnSocketWriteComplete(const SendCompletionCallback& success_callback, 98 const ErrorCompletionCallback& error_callback, 99 int send_result); 100 101 void PostReceiveCompletion(const ReceiveCompletionCallback& callback, 102 int io_buffer_size, 103 scoped_refptr<net::IOBuffer> io_buffer); 104 void PostReceiveErrorCompletion( 105 const ReceiveErrorCompletionCallback& callback, 106 ErrorReason reason, 107 const std::string& error_message); 108 void PostSendCompletion(const SendCompletionCallback& callback, 109 int bytes_written); 110 111 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; 112 scoped_refptr<BluetoothSocketThread> socket_thread_; 113 114 scoped_ptr<net::TCPSocket> tcp_socket_; 115 scoped_refptr<net::IOBufferWithSize> read_buffer_; 116 std::queue<linked_ptr<WriteRequest> > write_queue_; 117 118 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketNet); 119}; 120 121} // namespace device 122 123#endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_NET_H_ 124