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