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
;
30 // The BluetoothSocketChromeOS class implements BluetoothSocket for the
31 // Chrome OS platform.
32 class CHROMEOS_EXPORT BluetoothSocketChromeOS
33 : public device::BluetoothSocketNet
,
34 public device::BluetoothAdapter::Observer
,
35 public BluetoothProfileServiceProvider::Delegate
{
37 static scoped_refptr
<BluetoothSocketChromeOS
> CreateBluetoothSocket(
38 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
39 scoped_refptr
<device::BluetoothSocketThread
> socket_thread
);
41 // Connects this socket to the service on |device| published as UUID |uuid|,
42 // the underlying protocol and PSM or Channel is obtained through service
43 // discovery. On a successful connection the socket properties will be updated
44 // and |success_callback| called. On failure |error_callback| will be called
45 // with a message explaining the cause of the failure.
46 virtual void Connect(const BluetoothDeviceChromeOS
* device
,
47 const device::BluetoothUUID
& uuid
,
48 const base::Closure
& success_callback
,
49 const ErrorCompletionCallback
& error_callback
);
51 // Listens using this socket using a service published on |adapter|. The
52 // service is either RFCOMM or L2CAP depending on |socket_type| and published
53 // as UUID |uuid|. The |service_options| argument is interpreted according to
54 // |socket_type|. |success_callback| will be called if the service is
55 // successfully registered, |error_callback| on failure with a message
56 // explaining the cause.
57 enum SocketType
{ kRfcomm
, kL2cap
};
59 scoped_refptr
<device::BluetoothAdapter
> adapter
,
60 SocketType socket_type
,
61 const device::BluetoothUUID
& uuid
,
62 const device::BluetoothAdapter::ServiceOptions
& service_options
,
63 const base::Closure
& success_callback
,
64 const ErrorCompletionCallback
& error_callback
);
67 virtual void Close() OVERRIDE
;
68 virtual void Disconnect(const base::Closure
& callback
) OVERRIDE
;
69 virtual void Accept(const AcceptCompletionCallback
& success_callback
,
70 const ErrorCompletionCallback
& error_callback
) OVERRIDE
;
72 // Returns the object path of the socket.
73 const dbus::ObjectPath
& object_path() const { return object_path_
; }
76 virtual ~BluetoothSocketChromeOS();
79 BluetoothSocketChromeOS(
80 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
81 scoped_refptr
<device::BluetoothSocketThread
> socket_thread
);
83 // Register the underlying profile client object with the Bluetooth Daemon.
84 void RegisterProfile(const base::Closure
& success_callback
,
85 const ErrorCompletionCallback
& error_callback
);
86 void OnRegisterProfile(const base::Closure
& success_callback
,
87 const ErrorCompletionCallback
& error_callback
);
88 void OnRegisterProfileError(const ErrorCompletionCallback
& error_callback
,
89 const std::string
& error_name
,
90 const std::string
& error_message
);
92 // Called by dbus:: on completion of the ConnectProfile() method.
93 void OnConnectProfile(const base::Closure
& success_callback
);
94 void OnConnectProfileError(const ErrorCompletionCallback
& error_callback
,
95 const std::string
& error_name
,
96 const std::string
& error_message
);
98 // BluetoothAdapter::Observer:
99 virtual void AdapterPresentChanged(device::BluetoothAdapter
* adapter
,
100 bool present
) OVERRIDE
;
102 // Called by dbus:: on completion of the RegisterProfile() method call
103 // triggered as a result of the adapter becoming present again.
104 void OnInternalRegisterProfile();
105 void OnInternalRegisterProfileError(const std::string
& error_name
,
106 const std::string
& error_message
);
108 // BluetoothProfileServiceProvider::Delegate:
109 virtual void Released() OVERRIDE
;
110 virtual void NewConnection(
111 const dbus::ObjectPath
& device_path
,
112 scoped_ptr
<dbus::FileDescriptor
> fd
,
113 const BluetoothProfileServiceProvider::Delegate::Options
& options
,
114 const ConfirmationCallback
& callback
) OVERRIDE
;
115 virtual void RequestDisconnection(
116 const dbus::ObjectPath
& device_path
,
117 const ConfirmationCallback
& callback
) OVERRIDE
;
118 virtual void Cancel() OVERRIDE
;
120 // Method run to accept a single incoming connection.
121 void AcceptConnectionRequest();
123 // Method run on the socket thread to validate the file descriptor of a new
124 // connection and set up the underlying net::TCPSocket() for it.
125 void DoNewConnection(
126 const dbus::ObjectPath
& device_path
,
127 scoped_ptr
<dbus::FileDescriptor
> fd
,
128 const BluetoothProfileServiceProvider::Delegate::Options
& options
,
129 const ConfirmationCallback
& callback
);
131 // Method run on the UI thread after a new connection has been accepted and
132 // a socket allocated in |socket|. Takes care of calling the Accept()
133 // callback and |callback| with the right arguments based on |status|.
134 void OnNewConnection(scoped_refptr
<BluetoothSocket
> socket
,
135 const ConfirmationCallback
& callback
,
138 // Method run on the socket thread with a valid file descriptor |fd|, once
139 // complete calls |callback| on the UI thread with an appropriate argument
140 // indicating success or failure.
141 void DoConnect(scoped_ptr
<dbus::FileDescriptor
> fd
,
142 const ConfirmationCallback
& callback
);
144 // Method run to clean-up a listening socket.
145 void DoCloseListening();
147 // Unregister the underlying profile client object from the Bluetooth Daemon.
148 void UnregisterProfile();
149 void OnUnregisterProfile(const dbus::ObjectPath
& object_path
);
150 void OnUnregisterProfileError(const dbus::ObjectPath
& object_path
,
151 const std::string
& error_name
,
152 const std::string
& error_message
);
154 // Adapter the profile is registered against; this is only present when the
155 // socket is listening.
156 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
158 // Address and D-Bus object path of the device being connected to, empty and
159 // ignored if the socket is listening.
160 std::string device_address_
;
161 dbus::ObjectPath device_path_
;
163 // UUID of the profile being connected to, or listening on.
164 device::BluetoothUUID uuid_
;
166 // Copy of the profile options used for registering the profile.
167 scoped_ptr
<BluetoothProfileManagerClient::Options
> options_
;
169 // Object path of the local profile D-Bus object.
170 dbus::ObjectPath object_path_
;
172 // Local profile D-Bus object used for receiving profile delegate methods
174 scoped_ptr
<BluetoothProfileServiceProvider
> profile_
;
176 // Pending request to an Accept() call.
177 struct AcceptRequest
{
181 AcceptCompletionCallback success_callback
;
182 ErrorCompletionCallback error_callback
;
184 scoped_ptr
<AcceptRequest
> accept_request_
;
186 // Queue of incoming connection requests.
187 struct ConnectionRequest
{
189 ~ConnectionRequest();
191 dbus::ObjectPath device_path
;
192 scoped_ptr
<dbus::FileDescriptor
> fd
;
193 BluetoothProfileServiceProvider::Delegate::Options options
;
194 ConfirmationCallback callback
;
198 std::queue
<linked_ptr
<ConnectionRequest
> > connection_request_queue_
;
200 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketChromeOS
);
203 } // namespace chromeos
205 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_CHROMEOS_H_