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
3 // found in the LICENSE file.
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_CHROMEOS_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_CHROMEOS_H_
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "chromeos/chromeos_export.h"
14 #include "chromeos/dbus/bluetooth_profile_manager_client.h"
15 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
16 #include "dbus/object_path.h"
17 #include "device/bluetooth/bluetooth_adapter.h"
18 #include "device/bluetooth/bluetooth_socket.h"
19 #include "device/bluetooth/bluetooth_socket_net.h"
20 #include "device/bluetooth/bluetooth_uuid.h"
28 class BluetoothDeviceChromeOS
;
29 class BluetoothAdapterChromeOS
;
30 class BluetoothAdapterProfileChromeOS
;
32 // The BluetoothSocketChromeOS class implements BluetoothSocket for the
33 // Chrome OS platform.
35 // This class is not thread-safe, but is only called from the UI thread.
36 class CHROMEOS_EXPORT BluetoothSocketChromeOS
37 : public device::BluetoothSocketNet
,
38 public device::BluetoothAdapter::Observer
,
39 public BluetoothProfileServiceProvider::Delegate
{
46 static scoped_refptr
<BluetoothSocketChromeOS
> CreateBluetoothSocket(
47 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
48 scoped_refptr
<device::BluetoothSocketThread
> socket_thread
);
50 // Connects this socket to the service on |device| published as UUID |uuid|,
51 // the underlying protocol and PSM or Channel is obtained through service
52 // discovery. On a successful connection the socket properties will be updated
53 // and |success_callback| called. On failure |error_callback| will be called
54 // with a message explaining the cause of the failure.
55 virtual void Connect(const BluetoothDeviceChromeOS
* device
,
56 const device::BluetoothUUID
& uuid
,
57 SecurityLevel security_level
,
58 const base::Closure
& success_callback
,
59 const ErrorCompletionCallback
& error_callback
);
61 // Listens using this socket using a service published on |adapter|. The
62 // service is either RFCOMM or L2CAP depending on |socket_type| and published
63 // as UUID |uuid|. The |service_options| argument is interpreted according to
64 // |socket_type|. |success_callback| will be called if the service is
65 // successfully registered, |error_callback| on failure with a message
66 // explaining the cause.
67 enum SocketType
{ kRfcomm
, kL2cap
};
69 scoped_refptr
<device::BluetoothAdapter
> adapter
,
70 SocketType socket_type
,
71 const device::BluetoothUUID
& uuid
,
72 const device::BluetoothAdapter::ServiceOptions
& service_options
,
73 const base::Closure
& success_callback
,
74 const ErrorCompletionCallback
& error_callback
);
77 void Close() override
;
78 void Disconnect(const base::Closure
& callback
) override
;
79 void Accept(const AcceptCompletionCallback
& success_callback
,
80 const ErrorCompletionCallback
& error_callback
) override
;
83 ~BluetoothSocketChromeOS() override
;
86 BluetoothSocketChromeOS(
87 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
88 scoped_refptr
<device::BluetoothSocketThread
> socket_thread
);
90 // Register the underlying profile client object with the Bluetooth Daemon.
91 void RegisterProfile(BluetoothAdapterChromeOS
* adapter
,
92 const base::Closure
& success_callback
,
93 const ErrorCompletionCallback
& error_callback
);
94 void OnRegisterProfile(const base::Closure
& success_callback
,
95 const ErrorCompletionCallback
& error_callback
,
96 BluetoothAdapterProfileChromeOS
* profile
);
97 void OnRegisterProfileError(const ErrorCompletionCallback
& error_callback
,
98 const std::string
& error_message
);
100 // Called by dbus:: on completion of the ConnectProfile() method.
101 void OnConnectProfile(const base::Closure
& success_callback
);
102 void OnConnectProfileError(const ErrorCompletionCallback
& error_callback
,
103 const std::string
& error_name
,
104 const std::string
& error_message
);
106 // BluetoothAdapter::Observer:
107 void AdapterPresentChanged(device::BluetoothAdapter
* adapter
,
108 bool present
) override
;
110 // Called by dbus:: on completion of the RegisterProfile() method call
111 // triggered as a result of the adapter becoming present again.
112 void OnInternalRegisterProfile(BluetoothAdapterProfileChromeOS
* profile
);
113 void OnInternalRegisterProfileError(const std::string
& error_message
);
115 // BluetoothProfileServiceProvider::Delegate:
116 void Released() override
;
118 const dbus::ObjectPath
& device_path
,
119 scoped_ptr
<dbus::FileDescriptor
> fd
,
120 const BluetoothProfileServiceProvider::Delegate::Options
& options
,
121 const ConfirmationCallback
& callback
) override
;
122 void RequestDisconnection(const dbus::ObjectPath
& device_path
,
123 const ConfirmationCallback
& callback
) override
;
124 void Cancel() override
;
126 // Method run to accept a single incoming connection.
127 void AcceptConnectionRequest();
129 // Method run on the socket thread to validate the file descriptor of a new
130 // connection and set up the underlying net::TCPSocket() for it.
131 void DoNewConnection(
132 const dbus::ObjectPath
& device_path
,
133 scoped_ptr
<dbus::FileDescriptor
> fd
,
134 const BluetoothProfileServiceProvider::Delegate::Options
& options
,
135 const ConfirmationCallback
& callback
);
137 // Method run on the UI thread after a new connection has been accepted and
138 // a socket allocated in |socket|. Takes care of calling the Accept()
139 // callback and |callback| with the right arguments based on |status|.
140 void OnNewConnection(scoped_refptr
<BluetoothSocket
> socket
,
141 const ConfirmationCallback
& callback
,
144 // Method run on the socket thread with a valid file descriptor |fd|, once
145 // complete calls |callback| on the UI thread with an appropriate argument
146 // indicating success or failure.
147 void DoConnect(scoped_ptr
<dbus::FileDescriptor
> fd
,
148 const ConfirmationCallback
& callback
);
150 // Method run to clean-up a listening socket.
151 void DoCloseListening();
153 // Unregisters this socket's usage of the Bluetooth profile which cleans up
154 // the profile if no one is using it.
155 void UnregisterProfile();
157 // Adapter the profile is registered against
158 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
160 // Address and D-Bus object path of the device being connected to, empty and
161 // ignored if the socket is listening.
162 std::string device_address_
;
163 dbus::ObjectPath device_path_
;
165 // UUID of the profile being connected to, or listening on.
166 device::BluetoothUUID uuid_
;
168 // Copy of the profile options used for registering the profile.
169 scoped_ptr
<BluetoothProfileManagerClient::Options
> options_
;
171 // The profile registered with the adapter for this socket.
172 BluetoothAdapterProfileChromeOS
* profile_
;
174 // Pending request to an Accept() call.
175 struct AcceptRequest
{
179 AcceptCompletionCallback success_callback
;
180 ErrorCompletionCallback error_callback
;
182 scoped_ptr
<AcceptRequest
> accept_request_
;
184 // Queue of incoming connection requests.
185 struct ConnectionRequest
{
187 ~ConnectionRequest();
189 dbus::ObjectPath device_path
;
190 scoped_ptr
<dbus::FileDescriptor
> fd
;
191 BluetoothProfileServiceProvider::Delegate::Options options
;
192 ConfirmationCallback callback
;
196 std::queue
<linked_ptr
<ConnectionRequest
> > connection_request_queue_
;
198 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketChromeOS
);
201 } // namespace chromeos
203 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_CHROMEOS_H_