Automated Commit: Committing new LKGM version 6953.0.0 for chromeos.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_socket_chromeos.h
blobf836b212f51e802765a1b3199a7c91f6cae6aeb2
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_
8 #include <queue>
9 #include <string>
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"
22 namespace dbus {
23 class FileDescriptor;
24 } // namespace dbus
26 namespace chromeos {
28 class BluetoothDeviceChromeOS;
29 class BluetoothAdapterChromeOS;
30 class BluetoothAdapterProfileChromeOS;
32 // The BluetoothSocketChromeOS class implements BluetoothSocket for the
33 // Chrome OS platform.
34 class CHROMEOS_EXPORT BluetoothSocketChromeOS
35 : public device::BluetoothSocketNet,
36 public device::BluetoothAdapter::Observer,
37 public BluetoothProfileServiceProvider::Delegate {
38 public:
39 enum SecurityLevel {
40 SECURITY_LEVEL_LOW,
41 SECURITY_LEVEL_MEDIUM
44 static scoped_refptr<BluetoothSocketChromeOS> CreateBluetoothSocket(
45 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
46 scoped_refptr<device::BluetoothSocketThread> socket_thread);
48 // Connects this socket to the service on |device| published as UUID |uuid|,
49 // the underlying protocol and PSM or Channel is obtained through service
50 // discovery. On a successful connection the socket properties will be updated
51 // and |success_callback| called. On failure |error_callback| will be called
52 // with a message explaining the cause of the failure.
53 virtual void Connect(const BluetoothDeviceChromeOS* device,
54 const device::BluetoothUUID& uuid,
55 SecurityLevel security_level,
56 const base::Closure& success_callback,
57 const ErrorCompletionCallback& error_callback);
59 // Listens using this socket using a service published on |adapter|. The
60 // service is either RFCOMM or L2CAP depending on |socket_type| and published
61 // as UUID |uuid|. The |service_options| argument is interpreted according to
62 // |socket_type|. |success_callback| will be called if the service is
63 // successfully registered, |error_callback| on failure with a message
64 // explaining the cause.
65 enum SocketType { kRfcomm, kL2cap };
66 virtual void Listen(
67 scoped_refptr<device::BluetoothAdapter> adapter,
68 SocketType socket_type,
69 const device::BluetoothUUID& uuid,
70 const device::BluetoothAdapter::ServiceOptions& service_options,
71 const base::Closure& success_callback,
72 const ErrorCompletionCallback& error_callback);
74 // BluetoothSocket:
75 void Close() override;
76 void Disconnect(const base::Closure& callback) override;
77 void Accept(const AcceptCompletionCallback& success_callback,
78 const ErrorCompletionCallback& error_callback) override;
80 protected:
81 ~BluetoothSocketChromeOS() override;
83 private:
84 BluetoothSocketChromeOS(
85 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
86 scoped_refptr<device::BluetoothSocketThread> socket_thread);
88 // Register the underlying profile client object with the Bluetooth Daemon.
89 void RegisterProfile(BluetoothAdapterChromeOS* adapter,
90 const base::Closure& success_callback,
91 const ErrorCompletionCallback& error_callback);
92 void OnRegisterProfile(const base::Closure& success_callback,
93 const ErrorCompletionCallback& error_callback,
94 BluetoothAdapterProfileChromeOS* profile);
95 void OnRegisterProfileError(const ErrorCompletionCallback& error_callback,
96 const std::string& error_message);
98 // Called by dbus:: on completion of the ConnectProfile() method.
99 void OnConnectProfile(const base::Closure& success_callback);
100 void OnConnectProfileError(const ErrorCompletionCallback& error_callback,
101 const std::string& error_name,
102 const std::string& error_message);
104 // BluetoothAdapter::Observer:
105 void AdapterPresentChanged(device::BluetoothAdapter* adapter,
106 bool present) override;
108 // Called by dbus:: on completion of the RegisterProfile() method call
109 // triggered as a result of the adapter becoming present again.
110 void OnInternalRegisterProfile(BluetoothAdapterProfileChromeOS* profile);
111 void OnInternalRegisterProfileError(const std::string& error_message);
113 // BluetoothProfileServiceProvider::Delegate:
114 void Released() override;
115 void NewConnection(
116 const dbus::ObjectPath& device_path,
117 scoped_ptr<dbus::FileDescriptor> fd,
118 const BluetoothProfileServiceProvider::Delegate::Options& options,
119 const ConfirmationCallback& callback) override;
120 void RequestDisconnection(const dbus::ObjectPath& device_path,
121 const ConfirmationCallback& callback) override;
122 void Cancel() override;
124 // Method run to accept a single incoming connection.
125 void AcceptConnectionRequest();
127 // Method run on the socket thread to validate the file descriptor of a new
128 // connection and set up the underlying net::TCPSocket() for it.
129 void DoNewConnection(
130 const dbus::ObjectPath& device_path,
131 scoped_ptr<dbus::FileDescriptor> fd,
132 const BluetoothProfileServiceProvider::Delegate::Options& options,
133 const ConfirmationCallback& callback);
135 // Method run on the UI thread after a new connection has been accepted and
136 // a socket allocated in |socket|. Takes care of calling the Accept()
137 // callback and |callback| with the right arguments based on |status|.
138 void OnNewConnection(scoped_refptr<BluetoothSocket> socket,
139 const ConfirmationCallback& callback,
140 Status status);
142 // Method run on the socket thread with a valid file descriptor |fd|, once
143 // complete calls |callback| on the UI thread with an appropriate argument
144 // indicating success or failure.
145 void DoConnect(scoped_ptr<dbus::FileDescriptor> fd,
146 const ConfirmationCallback& callback);
148 // Method run to clean-up a listening socket.
149 void DoCloseListening();
151 // Unregisters this socket's usage of the Bluetooth profile which cleans up
152 // the profile if no one is using it.
153 void UnregisterProfile();
155 // Adapter the profile is registered against
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 // The profile registered with the adapter for this socket.
170 BluetoothAdapterProfileChromeOS* profile_;
172 // Pending request to an Accept() call.
173 struct AcceptRequest {
174 AcceptRequest();
175 ~AcceptRequest();
177 AcceptCompletionCallback success_callback;
178 ErrorCompletionCallback error_callback;
180 scoped_ptr<AcceptRequest> accept_request_;
182 // Queue of incoming connection requests.
183 struct ConnectionRequest {
184 ConnectionRequest();
185 ~ConnectionRequest();
187 dbus::ObjectPath device_path;
188 scoped_ptr<dbus::FileDescriptor> fd;
189 BluetoothProfileServiceProvider::Delegate::Options options;
190 ConfirmationCallback callback;
191 bool accepting;
192 bool cancelled;
194 std::queue<linked_ptr<ConnectionRequest> > connection_request_queue_;
196 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketChromeOS);
199 } // namespace chromeos
201 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_CHROMEOS_H_