Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/bluetooth/bluetooth_adapter_mac.h" | 5 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 6 | 6 |
| 7 #import <IOBluetooth/objc/IOBluetoothDevice.h> | 7 #import <IOBluetooth/objc/IOBluetoothDevice.h> |
| 8 #import <IOBluetooth/objc/IOBluetoothHostController.h> | 8 #import <IOBluetooth/objc/IOBluetoothHostController.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 int psm, | 131 int psm, |
| 132 const CreateServiceCallback& callback, | 132 const CreateServiceCallback& callback, |
| 133 const CreateServiceErrorCallback& error_callback) { | 133 const CreateServiceErrorCallback& error_callback) { |
| 134 scoped_refptr<BluetoothSocketMac> socket = BluetoothSocketMac::CreateSocket(); | 134 scoped_refptr<BluetoothSocketMac> socket = BluetoothSocketMac::CreateSocket(); |
| 135 socket->ListenUsingL2cap( | 135 socket->ListenUsingL2cap( |
| 136 this, uuid, psm, base::Bind(callback, socket), error_callback); | 136 this, uuid, psm, base::Bind(callback, socket), error_callback); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void BluetoothAdapterMac::DeviceFound(BluetoothDiscoveryManagerMac* manager, | 139 void BluetoothAdapterMac::DeviceFound(BluetoothDiscoveryManagerMac* manager, |
| 140 IOBluetoothDevice* device) { | 140 IOBluetoothDevice* device) { |
| 141 // TODO(isherman): The list of discovered devices is never reset. This should | |
| 142 // probably key off of |devices_| instead. Currently, if a device is paired, | |
| 143 // then unpaired, then paired again, the app would never hear about the second | |
| 144 // pairing. | |
|
armansito
2014/06/17 14:56:43
I agree with this, since to be consistent with the
Ilya Sherman
2014/06/17 21:43:38
Yeah, all of this logic is kinda wrong. It's not
keybuk
2014/06/17 21:48:50
CrOS for a device that was found by discovery, and
armansito
2014/06/17 22:07:22
Yes, let's not include the DeviceRemoved notificat
Ilya Sherman
2014/06/17 22:15:55
Done.
| |
| 141 std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device); | 145 std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device); |
| 142 if (discovered_devices_.find(device_address) == discovered_devices_.end()) { | 146 if (discovered_devices_.find(device_address) == discovered_devices_.end()) { |
| 143 BluetoothDeviceMac device_mac(device); | 147 BluetoothDeviceMac device_mac(device); |
| 144 FOR_EACH_OBSERVER( | 148 FOR_EACH_OBSERVER( |
| 145 BluetoothAdapter::Observer, observers_, DeviceAdded(this, &device_mac)); | 149 BluetoothAdapter::Observer, observers_, DeviceAdded(this, &device_mac)); |
| 146 discovered_devices_.insert(device_address); | 150 discovered_devices_.insert(device_address); |
| 147 } | 151 } |
| 148 } | 152 } |
| 149 | 153 |
| 150 void BluetoothAdapterMac::DiscoveryStopped( | 154 void BluetoothAdapterMac::DiscoveryStopped( |
| 151 BluetoothDiscoveryManagerMac* manager, | 155 BluetoothDiscoveryManagerMac* manager, |
| 152 bool unexpected) { | 156 bool unexpected) { |
| 153 DCHECK_EQ(manager, classic_discovery_manager_.get()); | 157 DCHECK_EQ(manager, classic_discovery_manager_.get()); |
| 154 if (unexpected) { | 158 if (unexpected) { |
| 155 DVLOG(1) << "Discovery stopped unexpectedly"; | 159 DVLOG(1) << "Discovery stopped unexpectedly"; |
| 156 num_discovery_sessions_ = 0; | 160 num_discovery_sessions_ = 0; |
| 157 MarkDiscoverySessionsAsInactive(); | 161 MarkDiscoverySessionsAsInactive(); |
| 158 } | 162 } |
| 159 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | 163 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, |
| 160 observers_, | 164 observers_, |
| 161 AdapterDiscoveringChanged(this, false)); | 165 AdapterDiscoveringChanged(this, false)); |
| 162 } | 166 } |
| 163 | 167 |
| 168 void BluetoothAdapterMac::DeviceConnected(IOBluetoothDevice* device) { | |
| 169 std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device); | |
| 170 DVLOG(1) << "Adapter registered a new connection from device with address: " | |
| 171 << device_address; | |
| 172 | |
| 173 // Only notify once per device. | |
| 174 if (devices_.count(device_address)) | |
| 175 return; | |
| 176 | |
| 177 scoped_ptr<BluetoothDeviceMac> device_mac(new BluetoothDeviceMac(device)); | |
| 178 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
| 179 observers_, | |
| 180 DeviceAdded(this, device_mac.get())); | |
| 181 devices_[device_address] = device_mac.release(); | |
| 182 } | |
| 183 | |
| 164 void BluetoothAdapterMac::AddDiscoverySession( | 184 void BluetoothAdapterMac::AddDiscoverySession( |
| 165 const base::Closure& callback, | 185 const base::Closure& callback, |
| 166 const ErrorCallback& error_callback) { | 186 const ErrorCallback& error_callback) { |
| 167 DVLOG(1) << __func__; | 187 DVLOG(1) << __func__; |
| 168 if (num_discovery_sessions_ > 0) { | 188 if (num_discovery_sessions_ > 0) { |
| 169 DCHECK(IsDiscovering()); | 189 DCHECK(IsDiscovering()); |
| 170 num_discovery_sessions_++; | 190 num_discovery_sessions_++; |
| 171 callback.Run(); | 191 callback.Run(); |
| 172 return; | 192 return; |
| 173 } | 193 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 255 if (was_present != is_present) { | 275 if (was_present != is_present) { |
| 256 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 276 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 257 AdapterPresentChanged(this, is_present)); | 277 AdapterPresentChanged(this, is_present)); |
| 258 } | 278 } |
| 259 if (powered_ != powered) { | 279 if (powered_ != powered) { |
| 260 powered_ = powered; | 280 powered_ = powered; |
| 261 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 281 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 262 AdapterPoweredChanged(this, powered_)); | 282 AdapterPoweredChanged(this, powered_)); |
| 263 } | 283 } |
| 264 | 284 |
| 285 // TODO(isherman): This doesn't detect when a device is unpaired. | |
| 265 IOBluetoothDevice* recent_device = | 286 IOBluetoothDevice* recent_device = |
| 266 [[IOBluetoothDevice recentDevices:1] lastObject]; | 287 [[IOBluetoothDevice recentDevices:1] lastObject]; |
| 267 NSDate* access_timestamp = [recent_device recentAccessDate]; | 288 NSDate* access_timestamp = [recent_device recentAccessDate]; |
| 268 if (recently_accessed_device_timestamp_ == nil || | 289 if (recently_accessed_device_timestamp_ == nil || |
| 269 access_timestamp == nil || | 290 access_timestamp == nil || |
| 270 [recently_accessed_device_timestamp_ compare:access_timestamp] == | 291 [recently_accessed_device_timestamp_ compare:access_timestamp] == |
| 271 NSOrderedAscending) { | 292 NSOrderedAscending) { |
| 272 UpdateDevices([IOBluetoothDevice pairedDevices]); | 293 UpdateDevices(); |
| 273 recently_accessed_device_timestamp_.reset([access_timestamp copy]); | 294 recently_accessed_device_timestamp_.reset([access_timestamp copy]); |
| 274 } | 295 } |
| 275 | 296 |
| 276 ui_task_runner_->PostDelayedTask( | 297 ui_task_runner_->PostDelayedTask( |
| 277 FROM_HERE, | 298 FROM_HERE, |
| 278 base::Bind(&BluetoothAdapterMac::PollAdapter, | 299 base::Bind(&BluetoothAdapterMac::PollAdapter, |
| 279 weak_ptr_factory_.GetWeakPtr()), | 300 weak_ptr_factory_.GetWeakPtr()), |
| 280 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); | 301 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); |
| 281 } | 302 } |
| 282 | 303 |
| 283 void BluetoothAdapterMac::UpdateDevices(NSArray* devices) { | 304 void BluetoothAdapterMac::UpdateDevices() { |
| 284 // TODO(armansito): This code never calls | 305 // Snapshot the devices observers were previously notified of. |
| 285 // BluetoothAdapter::Observer::DeviceRemoved. It should, if a device | 306 // Note that the code below is careful to take ownership of any values that |
| 286 // no longer exists. | 307 // are erased from the map, since the map owns the memory for all its mapped |
| 287 STLDeleteValues(&devices_); | 308 // devices. |
| 288 for (IOBluetoothDevice* device in devices) { | 309 DevicesMap old_devices = devices_; |
| 310 | |
| 311 // Add all the paired devices. | |
| 312 devices_.clear(); | |
| 313 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { | |
| 289 std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device); | 314 std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device); |
| 290 devices_[device_address] = new BluetoothDeviceMac(device); | 315 scoped_ptr<BluetoothDevice> device_mac(old_devices[device_address]); |
| 316 if (!device_mac) | |
| 317 device_mac.reset(new BluetoothDeviceMac(device)); | |
| 318 devices_[device_address] = device_mac.release(); | |
| 319 old_devices.erase(device_address); | |
| 291 } | 320 } |
| 321 | |
| 322 // Add any unpaired connected devices. | |
| 323 for (const auto& old_device : old_devices) { | |
|
armansito
2014/06/17 14:56:43
C++11 is allowed now?
Ilya Sherman
2014/06/17 21:43:38
It is in Mac-only files :D
| |
| 324 if (!old_device.second->IsConnected()) | |
| 325 continue; | |
| 326 | |
| 327 const std::string& device_address = old_device.first; | |
| 328 DCHECK(!devices_.count(device_address)); | |
| 329 devices_[device_address] = old_device.second; | |
| 330 old_devices.erase(device_address); | |
| 331 } | |
| 332 | |
| 333 // Notify observers of any devices that are no longer paired nor connected. | |
| 334 for (const auto& removed_device : old_devices) { | |
| 335 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
| 336 observers_, | |
| 337 DeviceRemoved(this, removed_device.second)); | |
|
armansito
2014/06/17 14:56:43
Good catch on realizing that this was never called
| |
| 338 } | |
| 339 STLDeleteValues(&old_devices); | |
| 292 } | 340 } |
| 293 | 341 |
| 294 } // namespace device | 342 } // namespace device |
| OLD | NEW |