Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(454)

Side by Side Diff: device/bluetooth/bluetooth_adapter_mac.mm

Issue 1220713006: Renaming BluetoothDeviceMac as BluetoothClassicDeviceMac (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@dm
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/containers/hash_tables.h" 14 #include "base/containers/hash_tables.h"
15 #include "base/location.h" 15 #include "base/location.h"
16 #include "base/mac/sdk_forward_declarations.h" 16 #include "base/mac/sdk_forward_declarations.h"
17 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
18 #include "base/profiler/scoped_tracker.h" 18 #include "base/profiler/scoped_tracker.h"
19 #include "base/sequenced_task_runner.h" 19 #include "base/sequenced_task_runner.h"
20 #include "base/single_thread_task_runner.h" 20 #include "base/single_thread_task_runner.h"
21 #include "base/strings/sys_string_conversions.h" 21 #include "base/strings/sys_string_conversions.h"
22 #include "base/thread_task_runner_handle.h" 22 #include "base/thread_task_runner_handle.h"
23 #include "base/time/time.h" 23 #include "base/time/time.h"
24 #include "device/bluetooth/bluetooth_device_mac.h" 24 #include "device/bluetooth/bluetooth_classic_device_mac.h"
25 #include "device/bluetooth/bluetooth_discovery_session.h" 25 #include "device/bluetooth/bluetooth_discovery_session.h"
26 #include "device/bluetooth/bluetooth_socket_mac.h" 26 #include "device/bluetooth/bluetooth_socket_mac.h"
27 #include "device/bluetooth/bluetooth_uuid.h" 27 #include "device/bluetooth/bluetooth_uuid.h"
28 28
29 namespace { 29 namespace {
30 30
31 // The frequency with which to poll the adapter for updates. 31 // The frequency with which to poll the adapter for updates.
32 const int kPollIntervalMs = 500; 32 const int kPollIntervalMs = 500;
33 33
34 // The length of time that must elapse since the last Inquiry response before a 34 // The length of time that must elapse since the last Inquiry response before a
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 } 164 }
165 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 165 FOR_EACH_OBSERVER(BluetoothAdapter::Observer,
166 observers_, 166 observers_,
167 AdapterDiscoveringChanged(this, false)); 167 AdapterDiscoveringChanged(this, false));
168 } 168 }
169 169
170 void BluetoothAdapterMac::DeviceConnected(IOBluetoothDevice* device) { 170 void BluetoothAdapterMac::DeviceConnected(IOBluetoothDevice* device) {
171 // TODO(isherman): Investigate whether this method can be replaced with a call 171 // TODO(isherman): Investigate whether this method can be replaced with a call
172 // to +registerForConnectNotifications:selector:. 172 // to +registerForConnectNotifications:selector:.
173 DVLOG(1) << "Adapter registered a new connection from device with address: " 173 DVLOG(1) << "Adapter registered a new connection from device with address: "
174 << BluetoothDeviceMac::GetDeviceAddress(device); 174 << BluetoothClassicDeviceMac::GetDeviceAddress(device);
175 ClassicDeviceAdded(device); 175 ClassicDeviceAdded(device);
176 } 176 }
177 177
178 void BluetoothAdapterMac::RemovePairingDelegateInternal( 178 void BluetoothAdapterMac::RemovePairingDelegateInternal(
179 BluetoothDevice::PairingDelegate* pairing_delegate) { 179 BluetoothDevice::PairingDelegate* pairing_delegate) {
180 } 180 }
181 181
182 void BluetoothAdapterMac::AddDiscoverySession( 182 void BluetoothAdapterMac::AddDiscoverySession(
183 BluetoothDiscoveryFilter* discovery_filter, 183 BluetoothDiscoveryFilter* discovery_filter,
184 const base::Closure& callback, 184 const base::Closure& callback,
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 UpdateDevices(); 363 UpdateDevices();
364 364
365 ui_task_runner_->PostDelayedTask( 365 ui_task_runner_->PostDelayedTask(
366 FROM_HERE, 366 FROM_HERE,
367 base::Bind(&BluetoothAdapterMac::PollAdapter, 367 base::Bind(&BluetoothAdapterMac::PollAdapter,
368 weak_ptr_factory_.GetWeakPtr()), 368 weak_ptr_factory_.GetWeakPtr()),
369 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); 369 base::TimeDelta::FromMilliseconds(kPollIntervalMs));
370 } 370 }
371 371
372 void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) { 372 void BluetoothAdapterMac::ClassicDeviceAdded(IOBluetoothDevice* device) {
373 std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device); 373 std::string device_address =
374 BluetoothClassicDeviceMac::GetDeviceAddress(device);
374 375
375 // Only notify observers once per device. 376 // Only notify observers once per device.
376 if (devices_.count(device_address)) 377 if (devices_.count(device_address))
377 return; 378 return;
378 379
379 devices_[device_address] = new BluetoothDeviceMac(device); 380 devices_[device_address] = new BluetoothClassicDeviceMac(device);
380 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 381 FOR_EACH_OBSERVER(BluetoothAdapter::Observer,
381 observers_, 382 observers_,
382 DeviceAdded(this, devices_[device_address])); 383 DeviceAdded(this, devices_[device_address]));
383 } 384 }
384 385
385 // TODO(krstnmnlsn): This method to be implemented as soon as UpdateDevices can 386 // TODO(krstnmnlsn): This method to be implemented as soon as UpdateDevices can
386 // handle instances of LowEnergyBluetoothDevice in |devices_|. crbug.com/498009 387 // handle instances of LowEnergyBluetoothDevice in |devices_|. crbug.com/498009
387 void BluetoothAdapterMac::LowEnergyDeviceUpdated( 388 void BluetoothAdapterMac::LowEnergyDeviceUpdated(
388 CBPeripheral* peripheral, 389 CBPeripheral* peripheral,
389 NSDictionary* advertisementData, 390 NSDictionary* advertisementData,
390 int rssi) { 391 int rssi) {
391 } 392 }
392 393
393 // TODO(krstnmnlsn): This method assumes all BluetoothDevices in devices_ are
394 // instances of BluetoothDeviceMac. Add support for low energy devices.
395 // crbug.com/498009
396 void BluetoothAdapterMac::UpdateDevices() { 394 void BluetoothAdapterMac::UpdateDevices() {
397 // Notify observers if any previously seen devices are no longer available, 395 // Notify observers if any previously seen devices are no longer available,
398 // i.e. if they are no longer paired, connected, nor recently discovered via 396 // i.e. if they are no longer paired, connected, nor recently discovered via
399 // an inquiry. 397 // an inquiry.
400 std::set<std::string> removed_devices; 398 std::set<std::string> removed_devices;
401 for (DevicesMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { 399 for (DevicesMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
402 BluetoothDevice* device = it->second; 400 BluetoothDevice* device = it->second;
403 if (device->IsPaired() || device->IsConnected()) 401 if (device->IsPaired() || device->IsConnected())
404 continue; 402 continue;
405 403
406 NSDate* last_inquiry_update = 404 // The device must support exactly one of Classic or Low Energy because this
407 static_cast<BluetoothDeviceMac*>(device)->GetLastInquiryUpdate(); 405 // indicates to us its class. Run time type checking is required here as
408 if (last_inquiry_update && 406 // BluetoothDevice does not currently support giving timing information (it
scheib 2015/06/29 20:44:24 Drop reference to ChromeOS. How about just "Device
krstnmnlsn 2015/06/30 00:50:35 Done. See https://codereview.chromium.org/12110130
409 -[last_inquiry_update timeIntervalSinceNow] < kDiscoveryTimeoutSec) 407 // was unnecessary on ChromeOS).
408 DCHECK((device->SupportsClassic() || device->SupportsLowEnergy()) &&
409 !(device->SupportsClassic() && device->SupportsLowEnergy()));
410 NSDate* last_update = [NSDate dateWithTimeIntervalSince1970:0];
411 if (device->SupportsClassic()) {
412 last_update = static_cast<BluetoothClassicDeviceMac*>(device)
413 ->GetLastInquiryUpdate();
414 }
415 if (device->SupportsLowEnergy()) {
416 last_update = static_cast<BluetoothLowEnergyDeviceMac*>(device)
417 ->GetLastCallToUpdate();
418 }
419
420 if (last_update &&
421 -[last_update timeIntervalSinceNow] < kDiscoveryTimeoutSec)
410 continue; 422 continue;
411 423
412 FOR_EACH_OBSERVER( 424 FOR_EACH_OBSERVER(
413 BluetoothAdapter::Observer, observers_, DeviceRemoved(this, device)); 425 BluetoothAdapter::Observer, observers_, DeviceRemoved(this, device));
414 delete device; 426 delete device;
415 removed_devices.insert(it->first); 427 removed_devices.insert(it->first);
416 // The device will be erased from the map in the loop immediately below. 428 // The device will be erased from the map in the loop immediately below.
417 } 429 }
418 for (const std::string& device_address : removed_devices) { 430 for (const std::string& device_address : removed_devices) {
419 size_t num_removed = devices_.erase(device_address); 431 size_t num_removed = devices_.erase(device_address);
420 DCHECK_EQ(num_removed, 1U); 432 DCHECK_EQ(num_removed, 1U);
421 } 433 }
422 434
423 // Add any new paired devices. 435 // Add any new paired devices.
424 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { 436 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) {
425 ClassicDeviceAdded(device); 437 ClassicDeviceAdded(device);
426 } 438 }
427 } 439 }
428 440
429 } // namespace device 441 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698