Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_device_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_DEVICE_CHROMEOS_H
6 #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H
7
8 #include <string>
9
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/sequenced_task_runner.h"
14 #include "chromeos/dbus/bluetooth_device_client.h"
15 #include "chromeos/dbus/bluetooth_gatt_service_client.h"
16 #include "dbus/object_path.h"
17 #include "device/bluetooth/bluetooth_device.h"
18
19 namespace device {
20 class BluetoothSocketThread;
21 }  // namespace device
22
23 namespace chromeos {
24
25 class BluetoothAdapterChromeOS;
26 class BluetoothPairingChromeOS;
27
28 // The BluetoothDeviceChromeOS class implements BluetoothDevice for the
29 // Chrome OS platform.
30 class BluetoothDeviceChromeOS
31     : public device::BluetoothDevice,
32       public BluetoothGattServiceClient::Observer {
33  public:
34   // BluetoothDevice override
35   virtual uint32 GetBluetoothClass() const OVERRIDE;
36   virtual std::string GetAddress() const OVERRIDE;
37   virtual VendorIDSource GetVendorIDSource() const OVERRIDE;
38   virtual uint16 GetVendorID() const OVERRIDE;
39   virtual uint16 GetProductID() const OVERRIDE;
40   virtual uint16 GetDeviceID() const OVERRIDE;
41   virtual int GetRSSI() const OVERRIDE;
42   virtual int GetCurrentHostTransmitPower() const OVERRIDE;
43   virtual int GetMaximumHostTransmitPower() const OVERRIDE;
44   virtual bool IsPaired() const OVERRIDE;
45   virtual bool IsConnected() const OVERRIDE;
46   virtual bool IsConnectable() const OVERRIDE;
47   virtual bool IsConnecting() const OVERRIDE;
48   virtual UUIDList GetUUIDs() const OVERRIDE;
49   virtual bool ExpectingPinCode() const OVERRIDE;
50   virtual bool ExpectingPasskey() const OVERRIDE;
51   virtual bool ExpectingConfirmation() const OVERRIDE;
52   virtual void Connect(
53       device::BluetoothDevice::PairingDelegate* pairing_delegate,
54       const base::Closure& callback,
55       const ConnectErrorCallback& error_callback) OVERRIDE;
56   virtual void SetPinCode(const std::string& pincode) OVERRIDE;
57   virtual void SetPasskey(uint32 passkey) OVERRIDE;
58   virtual void ConfirmPairing() OVERRIDE;
59   virtual void RejectPairing() OVERRIDE;
60   virtual void CancelPairing() OVERRIDE;
61   virtual void Disconnect(
62       const base::Closure& callback,
63       const ErrorCallback& error_callback) OVERRIDE;
64   virtual void Forget(const ErrorCallback& error_callback) OVERRIDE;
65   virtual void ConnectToService(
66       const device::BluetoothUUID& uuid,
67       const ConnectToServiceCallback& callback,
68       const ConnectToServiceErrorCallback& error_callback) OVERRIDE;
69   virtual void CreateGattConnection(
70       const GattConnectionCallback& callback,
71       const ConnectErrorCallback& error_callback) OVERRIDE;
72   virtual void StartConnectionMonitor(
73       const base::Closure& callback,
74       const ErrorCallback& error_callback) OVERRIDE;
75
76   // Creates a pairing object with the given delegate |pairing_delegate| and
77   // establishes it as the pairing context for this device. All pairing-related
78   // method calls will be forwarded to this object until it is released.
79   BluetoothPairingChromeOS* BeginPairing(
80       BluetoothDevice::PairingDelegate* pairing_delegate);
81
82   // Releases the current pairing object, any pairing-related method calls will
83   // be ignored.
84   void EndPairing();
85
86   // Returns the current pairing object or NULL if no pairing is in progress.
87   BluetoothPairingChromeOS* GetPairing() const;
88
89   // Returns the object path of the device.
90   const dbus::ObjectPath& object_path() const { return object_path_; }
91
92  protected:
93    // BluetoothDevice override
94   virtual std::string GetDeviceName() const OVERRIDE;
95
96  private:
97   friend class BluetoothAdapterChromeOS;
98
99   BluetoothDeviceChromeOS(
100       BluetoothAdapterChromeOS* adapter,
101       const dbus::ObjectPath& object_path,
102       scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
103       scoped_refptr<device::BluetoothSocketThread> socket_thread);
104   virtual ~BluetoothDeviceChromeOS();
105
106   // BluetoothGattServiceClient::Observer overrides.
107   virtual void GattServiceAdded(const dbus::ObjectPath& object_path) OVERRIDE;
108   virtual void GattServiceRemoved(const dbus::ObjectPath& object_path) OVERRIDE;
109
110   // Internal method to initiate a connection to this device, and methods called
111   // by dbus:: on completion of the D-Bus method call.
112   void ConnectInternal(bool after_pairing,
113                        const base::Closure& callback,
114                        const ConnectErrorCallback& error_callback);
115   void OnConnect(bool after_pairing,
116                  const base::Closure& callback);
117   void OnCreateGattConnection(const GattConnectionCallback& callback);
118   void OnConnectError(bool after_pairing,
119                       const ConnectErrorCallback& error_callback,
120                       const std::string& error_name,
121                       const std::string& error_message);
122
123   // Called by dbus:: on completion of the D-Bus method call to pair the device.
124   void OnPair(const base::Closure& callback,
125               const ConnectErrorCallback& error_callback);
126   void OnPairError(const ConnectErrorCallback& error_callback,
127                    const std::string& error_name,
128                    const std::string& error_message);
129
130   // Called by dbus:: on failure of the D-Bus method call to cancel pairing,
131   // there is no matching completion call since we don't do anything special
132   // in that case.
133   void OnCancelPairingError(const std::string& error_name,
134                             const std::string& error_message);
135
136   // Internal method to set the device as trusted. Trusted devices can connect
137   // to us automatically, and we can connect to them after rebooting; it also
138   // causes the device to be remembered by the stack even if not paired.
139   // |success| to the callback indicates whether or not the request succeeded.
140   void SetTrusted();
141   void OnSetTrusted(bool success);
142
143   // Called by dbus:: on completion of the D-Bus method call to disconnect the
144   // device.
145   void OnDisconnect(const base::Closure& callback);
146   void OnDisconnectError(const ErrorCallback& error_callback,
147                          const std::string& error_name,
148                          const std::string& error_message);
149
150   // Called by dbus:: on failure of the D-Bus method call to unpair the device;
151   // there is no matching completion call since this object is deleted in the
152   // process of unpairing.
153   void OnForgetError(const ErrorCallback& error_callback,
154                      const std::string& error_name,
155                      const std::string& error_message);
156
157   // Called by dbus:: on completion of the D-Bus method call to start the
158   // connection monitor.
159   void OnStartConnectionMonitor(const base::Closure& callback);
160   void OnStartConnectionMonitorError(const ErrorCallback& error_callback,
161                                      const std::string& error_name,
162                                      const std::string& error_message);
163
164   // The adapter that owns this device instance.
165   BluetoothAdapterChromeOS* adapter_;
166
167   // The dbus object path of the device object.
168   dbus::ObjectPath object_path_;
169
170   // Number of ongoing calls to Connect().
171   int num_connecting_calls_;
172
173   // True if the connection monitor has been started, tracking the connection
174   // RSSI and TX power.
175   bool connection_monitor_started_;
176
177   // UI thread task runner and socket thread object used to create sockets.
178   scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
179   scoped_refptr<device::BluetoothSocketThread> socket_thread_;
180
181   // During pairing this is set to an object that we don't own, but on which
182   // we can make method calls to request, display or confirm PIN Codes and
183   // Passkeys. Generally it is the object that owns this one.
184   scoped_ptr<BluetoothPairingChromeOS> pairing_;
185
186   // Note: This should remain the last member so it'll be destroyed and
187   // invalidate its weak pointers before any other members are destroyed.
188   base::WeakPtrFactory<BluetoothDeviceChromeOS> weak_ptr_factory_;
189
190   DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOS);
191 };
192
193 }  // namespace chromeos
194
195 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H