Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_socket_chromeos.h
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.
4
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_CHROMEOS_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_CHROMEOS_H_
7
8 #include <queue>
9 #include <string>
10
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"
21
22 namespace dbus {
23 class FileDescriptor;
24 }  // namespace dbus
25
26 namespace chromeos {
27
28 class BluetoothDeviceChromeOS;
29
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 {
36  public:
37   static scoped_refptr<BluetoothSocketChromeOS> CreateBluetoothSocket(
38       scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
39       scoped_refptr<device::BluetoothSocketThread> socket_thread);
40
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);
50
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 };
58   virtual void Listen(
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);
65
66   // BluetoothSocket:
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;
71
72   // Returns the object path of the socket.
73   const dbus::ObjectPath& object_path() const { return object_path_; }
74
75  protected:
76   virtual ~BluetoothSocketChromeOS();
77
78  private:
79   BluetoothSocketChromeOS(
80       scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
81       scoped_refptr<device::BluetoothSocketThread> socket_thread);
82
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);
91
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);
97
98   // BluetoothAdapter::Observer:
99   virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
100                                      bool present) OVERRIDE;
101
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);
107
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;
119
120   // Method run to accept a single incoming connection.
121   void AcceptConnectionRequest();
122
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);
130
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,
136                        Status status);
137
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);
143
144   // Method run to clean-up a listening socket.
145   void DoCloseListening();
146
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);
153
154   // Adapter the profile is registered against; this is only present when the
155   // socket is listening.
156   scoped_refptr<device::BluetoothAdapter> adapter_;
157
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_;
162
163   // UUID of the profile being connected to, or listening on.
164   device::BluetoothUUID uuid_;
165
166   // Copy of the profile options used for registering the profile.
167   scoped_ptr<BluetoothProfileManagerClient::Options> options_;
168
169   // Object path of the local profile D-Bus object.
170   dbus::ObjectPath object_path_;
171
172   // Local profile D-Bus object used for receiving profile delegate methods
173   // from BlueZ.
174   scoped_ptr<BluetoothProfileServiceProvider> profile_;
175
176   // Pending request to an Accept() call.
177   struct AcceptRequest {
178     AcceptRequest();
179     ~AcceptRequest();
180
181     AcceptCompletionCallback success_callback;
182     ErrorCompletionCallback error_callback;
183   };
184   scoped_ptr<AcceptRequest> accept_request_;
185
186   // Queue of incoming connection requests.
187   struct ConnectionRequest {
188     ConnectionRequest();
189     ~ConnectionRequest();
190
191     dbus::ObjectPath device_path;
192     scoped_ptr<dbus::FileDescriptor> fd;
193     BluetoothProfileServiceProvider::Delegate::Options options;
194     ConfirmationCallback callback;
195     bool accepting;
196     bool cancelled;
197   };
198   std::queue<linked_ptr<ConnectionRequest> > connection_request_queue_;
199
200   DISALLOW_COPY_AND_ASSIGN(BluetoothSocketChromeOS);
201 };
202
203 }  // namespace chromeos
204
205 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_CHROMEOS_H_