Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_adapter_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_ADAPTER_CHROMEOS_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_
7
8 #include <queue>
9 #include <string>
10
11 #include "base/memory/weak_ptr.h"
12 #include "base/sequenced_task_runner.h"
13 #include "chromeos/dbus/bluetooth_adapter_client.h"
14 #include "chromeos/dbus/bluetooth_agent_service_provider.h"
15 #include "chromeos/dbus/bluetooth_device_client.h"
16 #include "chromeos/dbus/bluetooth_input_client.h"
17 #include "dbus/object_path.h"
18 #include "device/bluetooth/bluetooth_adapter.h"
19 #include "device/bluetooth/bluetooth_device.h"
20
21 namespace device {
22 class BluetoothSocketThread;
23 }  // namespace device
24
25 namespace chromeos {
26
27 class BluetoothChromeOSTest;
28 class BluetoothDeviceChromeOS;
29 class BluetoothPairingChromeOS;
30
31 // The BluetoothAdapterChromeOS class implements BluetoothAdapter for the
32 // Chrome OS platform.
33 class BluetoothAdapterChromeOS
34     : public device::BluetoothAdapter,
35       public chromeos::BluetoothAdapterClient::Observer,
36       public chromeos::BluetoothDeviceClient::Observer,
37       public chromeos::BluetoothInputClient::Observer,
38       public chromeos::BluetoothAgentServiceProvider::Delegate {
39  public:
40   static base::WeakPtr<BluetoothAdapter> CreateAdapter();
41
42   // BluetoothAdapter:
43   virtual void AddObserver(
44       device::BluetoothAdapter::Observer* observer) OVERRIDE;
45   virtual void RemoveObserver(
46       device::BluetoothAdapter::Observer* observer) OVERRIDE;
47   virtual std::string GetAddress() const OVERRIDE;
48   virtual std::string GetName() const OVERRIDE;
49   virtual void SetName(const std::string& name,
50                        const base::Closure& callback,
51                        const ErrorCallback& error_callback) OVERRIDE;
52   virtual bool IsInitialized() const OVERRIDE;
53   virtual bool IsPresent() const OVERRIDE;
54   virtual bool IsPowered() const OVERRIDE;
55   virtual void SetPowered(
56       bool powered,
57       const base::Closure& callback,
58       const ErrorCallback& error_callback) OVERRIDE;
59   virtual bool IsDiscoverable() const OVERRIDE;
60   virtual void SetDiscoverable(
61       bool discoverable,
62       const base::Closure& callback,
63       const ErrorCallback& error_callback) OVERRIDE;
64   virtual bool IsDiscovering() const OVERRIDE;
65   virtual void ReadLocalOutOfBandPairingData(
66       const device::BluetoothAdapter::BluetoothOutOfBandPairingDataCallback&
67           callback,
68       const ErrorCallback& error_callback) OVERRIDE;
69
70  protected:
71   // BluetoothAdapter:
72   virtual void RemovePairingDelegateInternal(
73       device::BluetoothDevice::PairingDelegate* pairing_delegate) OVERRIDE;
74
75  private:
76   friend class BluetoothChromeOSTest;
77   friend class BluetoothDeviceChromeOS;
78   friend class BluetoothProfileChromeOS;
79   friend class BluetoothProfileChromeOSTest;
80
81   // typedef for callback parameters that are passed to AddDiscoverySession
82   // and RemoveDiscoverySession. This is used to queue incoming requests while
83   // a call to BlueZ is pending.
84   typedef std::pair<base::Closure, ErrorCallback> DiscoveryCallbackPair;
85   typedef std::queue<DiscoveryCallbackPair> DiscoveryCallbackQueue;
86
87   BluetoothAdapterChromeOS();
88   virtual ~BluetoothAdapterChromeOS();
89
90   // BluetoothAdapterClient::Observer override.
91   virtual void AdapterAdded(const dbus::ObjectPath& object_path) OVERRIDE;
92   virtual void AdapterRemoved(const dbus::ObjectPath& object_path) OVERRIDE;
93   virtual void AdapterPropertyChanged(
94       const dbus::ObjectPath& object_path,
95       const std::string& property_name) OVERRIDE;
96
97   // BluetoothDeviceClient::Observer override.
98   virtual void DeviceAdded(const dbus::ObjectPath& object_path) OVERRIDE;
99   virtual void DeviceRemoved(const dbus::ObjectPath& object_path) OVERRIDE;
100   virtual void DevicePropertyChanged(const dbus::ObjectPath& object_path,
101                                      const std::string& property_name) OVERRIDE;
102
103   // BluetoothInputClient::Observer override.
104   virtual void InputPropertyChanged(const dbus::ObjectPath& object_path,
105                                     const std::string& property_name) OVERRIDE;
106
107   // BluetoothAgentServiceProvider::Delegate override.
108   virtual void Released() OVERRIDE;
109   virtual void RequestPinCode(const dbus::ObjectPath& device_path,
110                               const PinCodeCallback& callback) OVERRIDE;
111   virtual void DisplayPinCode(const dbus::ObjectPath& device_path,
112                               const std::string& pincode) OVERRIDE;
113   virtual void RequestPasskey(const dbus::ObjectPath& device_path,
114                               const PasskeyCallback& callback) OVERRIDE;
115   virtual void DisplayPasskey(const dbus::ObjectPath& device_path,
116                               uint32 passkey, uint16 entered) OVERRIDE;
117   virtual void RequestConfirmation(const dbus::ObjectPath& device_path,
118                                    uint32 passkey,
119                                    const ConfirmationCallback& callback)
120       OVERRIDE;
121   virtual void RequestAuthorization(const dbus::ObjectPath& device_path,
122                                     const ConfirmationCallback& callback)
123       OVERRIDE;
124   virtual void AuthorizeService(const dbus::ObjectPath& device_path,
125                                 const std::string& uuid,
126                                 const ConfirmationCallback& callback) OVERRIDE;
127   virtual void Cancel() OVERRIDE;
128
129   // Called by dbus:: on completion of the D-Bus method call to register the
130   // pairing agent.
131   void OnRegisterAgent();
132   void OnRegisterAgentError(const std::string& error_name,
133                             const std::string& error_message);
134
135   // Called by dbus:: on completion of the D-Bus method call to request that
136   // the pairing agent be made the default.
137   void OnRequestDefaultAgent();
138   void OnRequestDefaultAgentError(const std::string& error_name,
139                                   const std::string& error_message);
140
141   // Internal method used to locate the device object by object path
142   // (the devices map and BluetoothDevice methods are by address)
143   BluetoothDeviceChromeOS* GetDeviceWithPath(
144       const dbus::ObjectPath& object_path);
145
146   // Internal method to obtain a BluetoothPairingChromeOS object for the device
147   // with path |object_path|. Returns the existing pairing object if the device
148   // already has one (usually an outgoing connection in progress) or a new
149   // pairing object with the default pairing delegate if not. If no default
150   // pairing object exists, NULL will be returned.
151   BluetoothPairingChromeOS* GetPairing(const dbus::ObjectPath& object_path);
152
153   // Set the tracked adapter to the one in |object_path|, this object will
154   // subsequently operate on that adapter until it is removed.
155   void SetAdapter(const dbus::ObjectPath& object_path);
156
157   // Set the adapter name to one chosen from the system information.
158   void SetDefaultAdapterName();
159
160   // Remove the currently tracked adapter. IsPresent() will return false after
161   // this is called.
162   void RemoveAdapter();
163
164   // Announce to observers a change in the adapter state.
165   void PoweredChanged(bool powered);
166   void DiscoverableChanged(bool discoverable);
167   void DiscoveringChanged(bool discovering);
168   void PresentChanged(bool present);
169
170   // Announce to observers a change in device state that is not reflected by
171   // its D-Bus properties.
172   void NotifyDeviceChanged(BluetoothDeviceChromeOS* device);
173
174   // Called by dbus:: on completion of the discoverable property change.
175   void OnSetDiscoverable(const base::Closure& callback,
176                          const ErrorCallback& error_callback,
177                          bool success);
178
179   // Called by dbus:: on completion of an adapter property change.
180   void OnPropertyChangeCompleted(const base::Closure& callback,
181                                  const ErrorCallback& error_callback,
182                                  bool success);
183
184   // BluetoothAdapter:
185   virtual void AddDiscoverySession(
186       const base::Closure& callback,
187       const ErrorCallback& error_callback) OVERRIDE;
188   virtual void RemoveDiscoverySession(
189       const base::Closure& callback,
190       const ErrorCallback& error_callback) OVERRIDE;
191
192   // Called by dbus:: on completion of the D-Bus method call to start discovery.
193   void OnStartDiscovery(const base::Closure& callback);
194   void OnStartDiscoveryError(const base::Closure& callback,
195                              const ErrorCallback& error_callback,
196                              const std::string& error_name,
197                              const std::string& error_message);
198
199   // Called by dbus:: on completion of the D-Bus method call to stop discovery.
200   void OnStopDiscovery(const base::Closure& callback);
201   void OnStopDiscoveryError(const ErrorCallback& error_callback,
202                             const std::string& error_name,
203                             const std::string& error_message);
204
205   // Processes the queued discovery requests. For each DiscoveryCallbackPair in
206   // the queue, this method will try to add a new discovery session. This method
207   // is called whenever a pending D-Bus call to start or stop discovery has
208   // ended (with either success or failure).
209   void ProcessQueuedDiscoveryRequests();
210
211   // Number of discovery sessions that have been added.
212   int num_discovery_sessions_;
213
214   // True, if there is a pending request to start or stop discovery.
215   bool discovery_request_pending_;
216
217   // List of queued requests to add new discovery sessions. While there is a
218   // pending request to BlueZ to start or stop discovery, many requests from
219   // within Chrome to start or stop discovery sessions may occur. We only
220   // queue requests to add new sessions to be processed later. All requests to
221   // remove a session while a call is pending immediately return failure. Note
222   // that since BlueZ keeps its own reference count of applications that have
223   // requested discovery, dropping our count to 0 won't necessarily result in
224   // the controller actually stopping discovery if, for example, an application
225   // other than Chrome, such as bt_console, was also used to start discovery.
226   DiscoveryCallbackQueue discovery_request_queue_;
227
228   // Object path of the adapter we track.
229   dbus::ObjectPath object_path_;
230
231   // List of observers interested in event notifications from us.
232   ObserverList<device::BluetoothAdapter::Observer> observers_;
233
234   // Instance of the D-Bus agent object used for pairing, initialized with
235   // our own class as its delegate.
236   scoped_ptr<BluetoothAgentServiceProvider> agent_;
237
238   // UI thread task runner and socket thread object used to create sockets.
239   scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
240   scoped_refptr<device::BluetoothSocketThread> socket_thread_;
241
242   // Note: This should remain the last member so it'll be destroyed and
243   // invalidate its weak pointers before any other members are destroyed.
244   base::WeakPtrFactory<BluetoothAdapterChromeOS> weak_ptr_factory_;
245
246   DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterChromeOS);
247 };
248
249 }  // namespace chromeos
250
251 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_