OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_ |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_ |
7 | 7 |
| 8 #include <queue> |
8 #include <string> | 9 #include <string> |
9 | 10 |
| 11 #include <IOKit/IOreturn.h> |
| 12 |
| 13 #include "base/memory/linked_ptr.h" |
10 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/sequenced_task_runner.h" |
| 16 #include "base/threading/thread_checker.h" |
11 #include "device/bluetooth/bluetooth_socket.h" | 17 #include "device/bluetooth/bluetooth_socket.h" |
12 | 18 |
13 #ifdef __OBJC__ | 19 #ifdef __OBJC__ |
14 @class BluetoothRFCOMMChannelDelegate; | 20 @class BluetoothRFCOMMChannelDelegate; |
15 @class IOBluetoothRFCOMMChannel; | 21 @class IOBluetoothRFCOMMChannel; |
16 @class IOBluetoothSDPServiceRecord; | 22 @class IOBluetoothSDPServiceRecord; |
17 #else | 23 #else |
18 class BluetoothRFCOMMChannelDelegate; | 24 class BluetoothRFCOMMChannelDelegate; |
19 class IOBluetoothRFCOMMChannel; | 25 class IOBluetoothRFCOMMChannel; |
20 class IOBluetoothSDPServiceRecord; | 26 class IOBluetoothSDPServiceRecord; |
21 #endif | 27 #endif |
22 | 28 |
23 namespace net { | 29 namespace net { |
24 class GrowableIOBuffer; | |
25 class IOBuffer; | 30 class IOBuffer; |
| 31 class IOBufferWithSize; |
26 } // namespace net | 32 } // namespace net |
27 | 33 |
28 namespace device { | 34 namespace device { |
29 | 35 |
30 class BluetoothServiceRecord; | 36 class BluetoothServiceRecord; |
31 | 37 |
32 // This class is an implementation of BluetoothSocket class for OSX platform. | 38 // This class is an implementation of BluetoothSocket class for OSX platform. |
| 39 // All methods of this class must all be called on the UI thread, as per Chrome |
| 40 // guidelines on performing Async IO on UI thread on MacOS. |
33 class BluetoothSocketMac : public BluetoothSocket { | 41 class BluetoothSocketMac : public BluetoothSocket { |
34 public: | 42 public: |
35 // TODO(youngki): This method is deprecated; remove this method when | 43 static scoped_refptr<BluetoothSocketMac> CreateBluetoothSocket( |
36 // BluetoothServiceRecord is removed. | 44 const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner, |
37 static scoped_refptr<BluetoothSocket> CreateBluetoothSocket( | 45 IOBluetoothSDPServiceRecord* record); |
38 const BluetoothServiceRecord& service_record); | |
39 | 46 |
40 static scoped_refptr<BluetoothSocket> CreateBluetoothSocket( | 47 // Connect to the peer device and calls |success_callback| when the |
41 IOBluetoothSDPServiceRecord* record); | 48 // connection has been established successfully. If an error occurs, calls |
| 49 // |error_callback| with a system error message. |
| 50 void Connect(const base::Closure& success_callback, |
| 51 const ErrorCompletionCallback& error_callback); |
42 | 52 |
43 // Overriden from BluetoothSocket: | 53 // Overriden from BluetoothSocket: |
44 virtual void Close() OVERRIDE; | 54 virtual void Close() OVERRIDE; |
45 virtual void Disconnect(const base::Closure& callback) OVERRIDE; | 55 virtual void Disconnect(const base::Closure& callback) OVERRIDE; |
46 virtual void Receive(int count, | 56 virtual void Receive(int count, |
47 const ReceiveCompletionCallback& success_callback, | 57 const ReceiveCompletionCallback& success_callback, |
48 const ReceiveErrorCompletionCallback& error_callback) | 58 const ReceiveErrorCompletionCallback& error_callback) |
49 OVERRIDE; | 59 OVERRIDE; |
50 virtual void Send(scoped_refptr<net::IOBuffer> buffer, | 60 virtual void Send(scoped_refptr<net::IOBuffer> buffer, |
51 int buffer_size, | 61 int buffer_size, |
52 const SendCompletionCallback& success_callback, | 62 const SendCompletionCallback& success_callback, |
53 const ErrorCompletionCallback& error_callback) OVERRIDE; | 63 const ErrorCompletionCallback& error_callback) OVERRIDE; |
54 | 64 |
55 // called by BluetoothRFCOMMChannelDelegate. | 65 // called by BluetoothRFCOMMChannelDelegate. |
56 void OnDataReceived(IOBluetoothRFCOMMChannel* rfcomm_channel, | 66 void OnChannelOpened(IOBluetoothRFCOMMChannel* rfcomm_channel, |
57 void* data, | 67 IOReturn status); |
58 size_t length); | 68 |
| 69 // called by BluetoothRFCOMMChannelDelegate. |
| 70 void OnChannelClosed(IOBluetoothRFCOMMChannel* rfcomm_channel); |
| 71 |
| 72 // called by BluetoothRFCOMMChannelDelegate. |
| 73 void OnChannelDataReceived(IOBluetoothRFCOMMChannel* rfcomm_channel, |
| 74 void* data, |
| 75 size_t length); |
| 76 |
| 77 // called by BluetoothRFCOMMChannelDelegate. |
| 78 void OnChannelWriteComplete(IOBluetoothRFCOMMChannel* rfcomm_channel, |
| 79 void* refcon, |
| 80 IOReturn status); |
59 | 81 |
60 protected: | 82 protected: |
61 virtual ~BluetoothSocketMac(); | 83 virtual ~BluetoothSocketMac(); |
62 | 84 |
63 private: | 85 private: |
64 explicit BluetoothSocketMac(IOBluetoothRFCOMMChannel* rfcomm_channel); | 86 BluetoothSocketMac( |
| 87 const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner, |
| 88 IOBluetoothSDPServiceRecord* record); |
65 | 89 |
66 void ResetIncomingDataBuffer(); | 90 struct SendRequest { |
| 91 SendRequest(); |
| 92 ~SendRequest(); |
| 93 int buffer_size; |
| 94 SendCompletionCallback success_callback; |
| 95 ErrorCompletionCallback error_callback; |
| 96 IOReturn status; |
| 97 int active_async_writes; |
| 98 bool error_signaled; |
| 99 }; |
67 | 100 |
| 101 struct ReceiveCallbacks { |
| 102 ReceiveCallbacks(); |
| 103 ~ReceiveCallbacks(); |
| 104 ReceiveCompletionCallback success_callback; |
| 105 ReceiveErrorCompletionCallback error_callback; |
| 106 }; |
| 107 |
| 108 struct ConnectCallbacks { |
| 109 ConnectCallbacks(); |
| 110 ~ConnectCallbacks(); |
| 111 base::Closure success_callback; |
| 112 ErrorCompletionCallback error_callback; |
| 113 }; |
| 114 |
| 115 void ReleaseChannel(); |
| 116 |
| 117 bool connecting() const { return connect_callbacks_; } |
| 118 |
| 119 // Used to verify all methods are called on the same thread. |
| 120 base::ThreadChecker thread_checker_; |
| 121 // Task Runner for the UI thread, used to post tasks. |
| 122 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; |
| 123 // (retained) The Bluetooth Service definition. |
| 124 IOBluetoothSDPServiceRecord* record_; |
| 125 // (weak) The RFCOMM channel delegate. Released when the channel is closed. |
| 126 BluetoothRFCOMMChannelDelegate* delegate_; |
| 127 // (retained) The IOBluetooth RFCOMM channel used to issue commands. |
68 IOBluetoothRFCOMMChannel* rfcomm_channel_; | 128 IOBluetoothRFCOMMChannel* rfcomm_channel_; |
69 BluetoothRFCOMMChannelDelegate* delegate_; | 129 // Connection callbacks -- when a pending async connection is active. |
70 scoped_refptr<net::GrowableIOBuffer> incoming_data_buffer_; | 130 scoped_ptr<ConnectCallbacks> connect_callbacks_; |
71 std::string error_message_; | 131 // Packets received while there is no pending "receive" callback. |
| 132 std::queue<scoped_refptr<net::IOBufferWithSize> > receive_queue_; |
| 133 // Receive callbacks -- when a receive call is active. |
| 134 scoped_ptr<ReceiveCallbacks> receive_callbacks_; |
| 135 // Send queue -- one entry per pending send operation. |
| 136 std::queue<linked_ptr<SendRequest> > send_queue_; |
72 | 137 |
73 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketMac); | 138 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketMac); |
74 }; | 139 }; |
75 | 140 |
76 } // namespace device | 141 } // namespace device |
77 | 142 |
78 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_ | 143 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_MAC_H_ |
OLD | NEW |