| 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 14 matching lines...) Expand all Loading... |
| 25 #include "device/bluetooth/bluetooth_classic_device_mac.h" | 25 #include "device/bluetooth/bluetooth_classic_device_mac.h" |
| 26 #include "device/bluetooth/bluetooth_discovery_session.h" | 26 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 27 #include "device/bluetooth/bluetooth_socket_mac.h" | 27 #include "device/bluetooth/bluetooth_socket_mac.h" |
| 28 #include "device/bluetooth/bluetooth_uuid.h" | 28 #include "device/bluetooth/bluetooth_uuid.h" |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 // The frequency with which to poll the adapter for updates. | 32 // The frequency with which to poll the adapter for updates. |
| 33 const int kPollIntervalMs = 500; | 33 const int kPollIntervalMs = 500; |
| 34 | 34 |
| 35 // The length of time that must elapse since the last Inquiry response before a | |
| 36 // discovered Classic device is considered to be no longer available. | |
| 37 const NSTimeInterval kDiscoveryTimeoutSec = 3 * 60; // 3 minutes | |
| 38 | |
| 39 } // namespace | 35 } // namespace |
| 40 | 36 |
| 41 namespace device { | 37 namespace device { |
| 42 | 38 |
| 43 // static | 39 // static |
| 40 const NSTimeInterval BluetoothAdapterMac::kDiscoveryTimeoutSec = |
| 41 180; // 3 minutes |
| 42 |
| 43 // static |
| 44 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter( | 44 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter( |
| 45 const InitCallback& init_callback) { | 45 const InitCallback& init_callback) { |
| 46 return BluetoothAdapterMac::CreateAdapter(); | 46 return BluetoothAdapterMac::CreateAdapter(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 // static | 49 // static |
| 50 base::WeakPtr<BluetoothAdapter> BluetoothAdapterMac::CreateAdapter() { | 50 base::WeakPtr<BluetoothAdapter> BluetoothAdapterMac::CreateAdapter() { |
| 51 BluetoothAdapterMac* adapter = new BluetoothAdapterMac(); | 51 BluetoothAdapterMac* adapter = new BluetoothAdapterMac(); |
| 52 adapter->Init(); | 52 adapter->Init(); |
| 53 return adapter->weak_ptr_factory_.GetWeakPtr(); | 53 return adapter->weak_ptr_factory_.GetWeakPtr(); |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 } | 302 } |
| 303 | 303 |
| 304 void BluetoothAdapterMac::Init() { | 304 void BluetoothAdapterMac::Init() { |
| 305 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 305 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 306 PollAdapter(); | 306 PollAdapter(); |
| 307 } | 307 } |
| 308 | 308 |
| 309 void BluetoothAdapterMac::InitForTest( | 309 void BluetoothAdapterMac::InitForTest( |
| 310 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) { | 310 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) { |
| 311 ui_task_runner_ = ui_task_runner; | 311 ui_task_runner_ = ui_task_runner; |
| 312 PollAdapter(); | |
| 313 } | 312 } |
| 314 | 313 |
| 315 void BluetoothAdapterMac::PollAdapter() { | 314 void BluetoothAdapterMac::PollAdapter() { |
| 316 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181 | 315 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181 |
| 317 // is fixed. | 316 // is fixed. |
| 318 tracked_objects::ScopedTracker tracking_profile1( | 317 tracked_objects::ScopedTracker tracking_profile1( |
| 319 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 318 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 320 "461181 BluetoothAdapterMac::PollAdapter::Start")); | 319 "461181 BluetoothAdapterMac::PollAdapter::Start")); |
| 321 bool was_present = IsPresent(); | 320 bool was_present = IsPresent(); |
| 322 std::string address; | 321 std::string address; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 if (powered_ != powered) { | 364 if (powered_ != powered) { |
| 366 powered_ = powered; | 365 powered_ = powered; |
| 367 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 366 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 368 AdapterPoweredChanged(this, powered_)); | 367 AdapterPoweredChanged(this, powered_)); |
| 369 } | 368 } |
| 370 | 369 |
| 371 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181 | 370 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181 |
| 372 // is fixed. | 371 // is fixed. |
| 373 tracked_objects::ScopedTracker tracking_profile5( | 372 tracked_objects::ScopedTracker tracking_profile5( |
| 374 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 373 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 375 "461181 BluetoothAdapterMac::PollAdapter::UpdateDevices")); | 374 "461181 BluetoothAdapterMac::PollAdapter::RemoveTimedOutDevices")); |
| 376 UpdateDevices(); | 375 RemoveTimedOutDevices(); |
| 376 |
| 377 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461181 |
| 378 // is fixed. |
| 379 tracked_objects::ScopedTracker tracking_profile6( |
| 380 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 381 "461181 BluetoothAdapterMac::PollAdapter::AddPairedDevices")); |
| 382 AddPairedDevices(); |
| 377 | 383 |
| 378 ui_task_runner_->PostDelayedTask( | 384 ui_task_runner_->PostDelayedTask( |
| 379 FROM_HERE, | 385 FROM_HERE, |
| 380 base::Bind(&BluetoothAdapterMac::PollAdapter, | 386 base::Bind(&BluetoothAdapterMac::PollAdapter, |
| 381 weak_ptr_factory_.GetWeakPtr()), | 387 weak_ptr_factory_.GetWeakPtr()), |
| 382 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); | 388 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); |
| 383 } | 389 } |
| 384 | 390 |
| 385 void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) { | 391 void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) { |
| 386 std::string device_address = | 392 std::string device_address = |
| 387 BluetoothClassicDeviceMac::GetDeviceAddress(device); | 393 BluetoothClassicDeviceMac::GetDeviceAddress(device); |
| 388 | 394 |
| 389 // Only notify observers once per device. | 395 // Only notify observers once per device. |
| 390 if (devices_.count(device_address)) | 396 if (devices_.count(device_address)) |
| 391 return; | 397 return; |
| 392 | 398 |
| 393 devices_[device_address] = new BluetoothClassicDeviceMac(device); | 399 devices_[device_address] = new BluetoothClassicDeviceMac(device); |
| 394 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | 400 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, |
| 395 observers_, | 401 observers_, |
| 396 DeviceAdded(this, devices_[device_address])); | 402 DeviceAdded(this, devices_[device_address])); |
| 397 } | 403 } |
| 398 | 404 |
| 399 // TODO(krstnmnlsn): This method to be implemented as soon as UpdateDevices can | 405 // TODO(krstnmnlsn): Implement method. http://crbug.com/496987. |
| 400 // handle instances of LowEnergyBluetoothDevice in |devices_|. crbug.com/498009 | |
| 401 void BluetoothAdapterMac::LowEnergyDeviceUpdated( | 406 void BluetoothAdapterMac::LowEnergyDeviceUpdated( |
| 402 CBPeripheral* peripheral, | 407 CBPeripheral* peripheral, |
| 403 NSDictionary* advertisementData, | 408 NSDictionary* advertisement_data, |
| 404 int rssi) { | 409 int rssi) {} |
| 405 } | |
| 406 | 410 |
| 407 void BluetoothAdapterMac::UpdateDevices() { | 411 void BluetoothAdapterMac::RemoveTimedOutDevices() { |
| 408 // Notify observers if any previously seen devices are no longer available, | 412 // Notify observers if any previously seen devices are no longer available, |
| 409 // i.e. if they are no longer paired, connected, nor recently discovered via | 413 // i.e. if they are no longer paired, connected, nor recently discovered via |
| 410 // an inquiry. | 414 // an inquiry. |
| 411 std::set<std::string> removed_devices; | 415 std::set<std::string> removed_devices; |
| 412 for (DevicesMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { | 416 for (DevicesMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
| 413 BluetoothDevice* device = it->second; | 417 BluetoothDevice* device = it->second; |
| 414 if (device->IsPaired() || device->IsConnected()) | 418 if (device->IsPaired() || device->IsConnected()) |
| 415 continue; | 419 continue; |
| 416 | 420 |
| 417 NSDate* last_update_time = | 421 NSDate* last_update_time = |
| 418 static_cast<BluetoothDeviceMac*>(device)->GetLastUpdateTime(); | 422 static_cast<BluetoothDeviceMac*>(device)->GetLastUpdateTime(); |
| 419 if (last_update_time && | 423 if (last_update_time && |
| 420 -[last_update_time timeIntervalSinceNow] < kDiscoveryTimeoutSec) | 424 -[last_update_time timeIntervalSinceNow] < kDiscoveryTimeoutSec) |
| 421 continue; | 425 continue; |
| 422 | 426 |
| 423 FOR_EACH_OBSERVER( | 427 FOR_EACH_OBSERVER( |
| 424 BluetoothAdapter::Observer, observers_, DeviceRemoved(this, device)); | 428 BluetoothAdapter::Observer, observers_, DeviceRemoved(this, device)); |
| 425 delete device; | 429 delete device; |
| 426 removed_devices.insert(it->first); | 430 removed_devices.insert(it->first); |
| 427 // The device will be erased from the map in the loop immediately below. | 431 // The device will be erased from the map in the loop immediately below. |
| 428 } | 432 } |
| 429 for (const std::string& device_address : removed_devices) { | 433 for (const std::string& device_address : removed_devices) { |
| 430 size_t num_removed = devices_.erase(device_address); | 434 size_t num_removed = devices_.erase(device_address); |
| 431 DCHECK_EQ(num_removed, 1U); | 435 DCHECK_EQ(num_removed, 1U); |
| 432 } | 436 } |
| 437 } |
| 433 | 438 |
| 439 void BluetoothAdapterMac::AddPairedDevices() { |
| 434 // Add any new paired devices. | 440 // Add any new paired devices. |
| 435 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { | 441 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { |
| 436 ClassicDeviceAdded(device); | 442 ClassicDeviceAdded(device); |
| 437 } | 443 } |
| 438 } | 444 } |
| 439 | 445 |
| 440 } // namespace device | 446 } // namespace device |
| OLD | NEW |