From 724d9901b9f8f44e67a9b9d6da0722bb443e5d0c Mon Sep 17 00:00:00 2001 From: tapted Date: Wed, 7 Jun 2017 20:00:44 -0700 Subject: [PATCH] Allow device/bluetooth to build on the macOS 10.13 SDK Some CoreBluetooth declarations tweaked in ways that are not compatible. Also changes import/includes to match style guide: https://google.github.io/styleguide/objcguide.xml?showone=_import_and__include#_import_and__include BUG=729906 Review-Url: https://codereview.chromium.org/2924753002 Cr-Commit-Position: refs/heads/master@{#477867} --- device/bluetooth/bluetooth_adapter_mac.h | 19 ++++++++++ device/bluetooth/bluetooth_adapter_mac.mm | 10 ++--- .../bluetooth_adapter_mac_unittest.mm | 26 ++++++------- .../bluetooth_low_energy_device_mac.mm | 12 ++++++ ...etooth_low_energy_discovery_manager_mac.mm | 4 +- device/bluetooth/test/bluetooth_test_mac.mm | 37 +++++++++---------- .../test/mock_bluetooth_central_manager_mac.h | 11 +++--- 7 files changed, 75 insertions(+), 44 deletions(-) diff --git a/device/bluetooth/bluetooth_adapter_mac.h b/device/bluetooth/bluetooth_adapter_mac.h index fb832c862bd3ad..52b1634a364c2f 100644 --- a/device/bluetooth/bluetooth_adapter_mac.h +++ b/device/bluetooth/bluetooth_adapter_mac.h @@ -29,6 +29,25 @@ @class NSArray; @class NSDate; +#if !defined(MAC_OS_X_VERSION_10_13) + +// The 10.13 SDK deprecates the CBCentralManagerState enum. When building +// against older SDKs, define the new enum in terms of the deprecated one. +using CBManagerState = CBCentralManagerState; +constexpr CBManagerState CBManagerStateUnknown = CBCentralManagerStateUnknown; +constexpr CBManagerState CBManagerStateResetting = + CBCentralManagerStateResetting; +constexpr CBManagerState CBManagerStateUnsupported = + CBCentralManagerStateUnsupported; +constexpr CBManagerState CBManagerStateUnauthorized = + CBCentralManagerStateUnauthorized; +constexpr CBManagerState CBManagerStatePoweredOff = + CBCentralManagerStatePoweredOff; +constexpr CBManagerState CBManagerStatePoweredOn = + CBCentralManagerStatePoweredOn; + +#endif // MAC_OS_X_VERSION_10_13 + namespace base { class SequencedTaskRunner; diff --git a/device/bluetooth/bluetooth_adapter_mac.mm b/device/bluetooth/bluetooth_adapter_mac.mm index 64dbb0393113d9..f02859d3719a9c 100644 --- a/device/bluetooth/bluetooth_adapter_mac.mm +++ b/device/bluetooth/bluetooth_adapter_mac.mm @@ -155,7 +155,7 @@ bool is_present = !address_.empty(); if (IsLowEnergyAvailable()) { is_present = is_present || ([low_energy_central_manager_ state] != - CBCentralManagerStateUnsupported); + CBManagerStateUnsupported); } return is_present; } @@ -164,7 +164,7 @@ bool is_powered = classic_powered_; if (IsLowEnergyAvailable()) { is_powered = is_powered || ([low_energy_central_manager_ state] == - CBCentralManagerStatePoweredOn); + CBManagerStatePoweredOn); } return is_powered; } @@ -588,17 +588,17 @@ } } -// TODO(crbug.com/511025): Handle state < CBCentralManagerStatePoweredOff. +// TODO(crbug.com/511025): Handle state < CBManagerStatePoweredOff. void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() { VLOG(1) << "Central manager state updated: " << [low_energy_central_manager_ state]; - // A state with a value lower than CBCentralManagerStatePoweredOn implies that + // A state with a value lower than CBManagerStatePoweredOn implies that // scanning has stopped and that any connected peripherals have been // disconnected. Call DidDisconnectPeripheral manually to update the devices' // states since macOS doesn't call it. // See // https://developer.apple.com/reference/corebluetooth/cbcentralmanagerdelegate/1518888-centralmanagerdidupdatestate?language=objc - if ([low_energy_central_manager_ state] < CBCentralManagerStatePoweredOn) { + if ([low_energy_central_manager_ state] < CBManagerStatePoweredOn) { VLOG(1) << "Central no longer powered on. Notifying of device disconnection."; for (BluetoothDevice* device : GetDevices()) { diff --git a/device/bluetooth/bluetooth_adapter_mac_unittest.mm b/device/bluetooth/bluetooth_adapter_mac_unittest.mm index 8dc4c00dcf071f..47ca54a1451730 100644 --- a/device/bluetooth/bluetooth_adapter_mac_unittest.mm +++ b/device/bluetooth/bluetooth_adapter_mac_unittest.mm @@ -4,6 +4,8 @@ #include "device/bluetooth/bluetooth_adapter_mac.h" +#import + #include #include "base/bind.h" @@ -15,11 +17,11 @@ #include "device/bluetooth/bluetooth_common.h" #include "device/bluetooth/bluetooth_discovery_session.h" #include "device/bluetooth/bluetooth_discovery_session_outcome.h" -#include "device/bluetooth/bluetooth_low_energy_device_mac.h" -#include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" -#include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" +#import "device/bluetooth/bluetooth_low_energy_device_mac.h" +#import "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" +#import "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" #include "testing/gtest/include/gtest/gtest.h" -#include "third_party/ocmock/OCMock/OCMock.h" +#import "third_party/ocmock/OCMock/OCMock.h" #if defined(OS_IOS) #import @@ -27,8 +29,6 @@ #import #endif // defined(OS_IOS) -#import - namespace { // |kTestHashAddress| is the hash corresponding to identifier |kTestNSUUID|. const char* const kTestNSUUID = "00000000-1111-2222-3333-444444444444"; @@ -92,7 +92,7 @@ bool DevicePresent(CBPeripheral* peripheral) { return (device != NULL); } - bool SetMockCentralManager(CBCentralManagerState desired_state) { + bool SetMockCentralManager(CBManagerState desired_state) { if (!BluetoothAdapterMac::IsLowEnergyAvailable()) { LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; return false; @@ -148,7 +148,7 @@ void DiscoveryErrorCallback(UMABluetoothDiscoverySessionOutcome) { } TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) { - if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) + if (!SetMockCentralManager(CBManagerStatePoweredOn)) return; EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); EXPECT_EQ(0, NumDiscoverySessions()); @@ -168,7 +168,7 @@ void DiscoveryErrorCallback(UMABluetoothDiscoverySessionOutcome) { // TODO(krstnmnlsn): Test changing the filter when adding the second discovery // session (once we have that ability). TEST_F(BluetoothAdapterMacTest, AddSecondDiscoverySessionWithLowEnergyFilter) { - if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) + if (!SetMockCentralManager(CBManagerStatePoweredOn)) return; std::unique_ptr discovery_filter( new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE)); @@ -189,7 +189,7 @@ void DiscoveryErrorCallback(UMABluetoothDiscoverySessionOutcome) { } TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilter) { - if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) + if (!SetMockCentralManager(CBManagerStatePoweredOn)) return; EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); @@ -212,7 +212,7 @@ void DiscoveryErrorCallback(UMABluetoothDiscoverySessionOutcome) { } TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilterFail) { - if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) + if (!SetMockCentralManager(CBManagerStatePoweredOn)) return; EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); @@ -230,7 +230,7 @@ void DiscoveryErrorCallback(UMABluetoothDiscoverySessionOutcome) { } TEST_F(BluetoothAdapterMacTest, CheckGetPeripheralHashAddress) { - if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) + if (!SetMockCentralManager(CBManagerStatePoweredOn)) return; base::scoped_nsobject mock_peripheral( CreateMockPeripheral(kTestNSUUID)); @@ -240,7 +240,7 @@ void DiscoveryErrorCallback(UMABluetoothDiscoverySessionOutcome) { } TEST_F(BluetoothAdapterMacTest, LowEnergyDeviceUpdatedNewDevice) { - if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) + if (!SetMockCentralManager(CBManagerStatePoweredOn)) return; base::scoped_nsobject mock_peripheral( CreateMockPeripheral(kTestNSUUID)); diff --git a/device/bluetooth/bluetooth_low_energy_device_mac.mm b/device/bluetooth/bluetooth_low_energy_device_mac.mm index e80b9c5192c18e..1e25ff41326e93 100644 --- a/device/bluetooth/bluetooth_low_energy_device_mac.mm +++ b/device/bluetooth/bluetooth_low_energy_device_mac.mm @@ -20,6 +20,18 @@ #include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h" #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" +// Remove when Chrome no longer supports 10.12. +#if defined(MAC_OS_X_VERSION_10_13) + +// In the 10.13 SDK, CBPeripheral became a subclass of CBPeer, which defines +// -[CBPeer identifier] as partially available. Pretend it still exists on +// CBPeripheral. At runtime the implementation on CBPeer will be invoked. +@interface CBPeripheral (HighSierraSDK) +@property(readonly, nonatomic) NSUUID* identifier; +@end + +#endif // MAC_OS_X_VERSION_10_13 + namespace device { BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( diff --git a/device/bluetooth/bluetooth_low_energy_discovery_manager_mac.mm b/device/bluetooth/bluetooth_low_energy_discovery_manager_mac.mm index 609186b25b74dc..7cb766f863ec7e 100644 --- a/device/bluetooth/bluetooth_low_energy_discovery_manager_mac.mm +++ b/device/bluetooth/bluetooth_low_energy_discovery_manager_mac.mm @@ -46,8 +46,8 @@ return; } - if ([central_manager_ state] != CBCentralManagerStatePoweredOn) { - VLOG(1) << "TryStartDiscovery != CBCentralManagerStatePoweredOn"; + if ([central_manager_ state] != CBManagerStatePoweredOn) { + VLOG(1) << "TryStartDiscovery != CBManagerStatePoweredOn"; return; } diff --git a/device/bluetooth/test/bluetooth_test_mac.mm b/device/bluetooth/test/bluetooth_test_mac.mm index 5f16078f45873c..eac0c6a3e28e7f 100644 --- a/device/bluetooth/test/bluetooth_test_mac.mm +++ b/device/bluetooth/test/bluetooth_test_mac.mm @@ -2,28 +2,27 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "device/bluetooth/test/bluetooth_test_mac.h" +#import "device/bluetooth/test/bluetooth_test_mac.h" +#import #include -#include "base/mac/foundation_util.h" +#import "base/mac/foundation_util.h" #include "base/strings/string_number_conversions.h" #include "base/strings/sys_string_conversions.h" #include "build/build_config.h" -#include "device/bluetooth/bluetooth_adapter_mac.h" -#include "device/bluetooth/bluetooth_device_mac.h" -#include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" -#include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h" -#include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" -#include "device/bluetooth/test/mock_bluetooth_cbcharacteristic_mac.h" -#include "device/bluetooth/test/mock_bluetooth_cbdescriptor_mac.h" -#include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" -#include "device/bluetooth/test/mock_bluetooth_cbservice_mac.h" -#include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" -#include "device/bluetooth/test/test_bluetooth_adapter_observer.h" -#include "third_party/ocmock/OCMock/OCMock.h" - -#import +#import "device/bluetooth/bluetooth_adapter_mac.h" +#import "device/bluetooth/bluetooth_device_mac.h" +#import "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" +#import "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h" +#import "device/bluetooth/bluetooth_remote_gatt_service_mac.h" +#import "device/bluetooth/test/mock_bluetooth_cbcharacteristic_mac.h" +#import "device/bluetooth/test/mock_bluetooth_cbdescriptor_mac.h" +#import "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" +#import "device/bluetooth/test/mock_bluetooth_cbservice_mac.h" +#import "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" +#import "device/bluetooth/test/test_bluetooth_adapter_observer.h" +#import "third_party/ocmock/OCMock/OCMock.h" using base::mac::ObjCCast; using base::scoped_nsobject; @@ -114,7 +113,7 @@ explicit ScopedMockCentralManager(MockCentralManager* mock_central_manager) { mock_central_manager_.reset( new ScopedMockCentralManager([[MockCentralManager alloc] init])); [mock_central_manager_->get() setBluetoothTestMac:this]; - [mock_central_manager_->get() setState:CBCentralManagerStateUnsupported]; + [mock_central_manager_->get() setState:CBManagerStateUnsupported]; adapter_mac_->SetCentralManagerForTesting((id)mock_central_manager_->get()); } } @@ -130,7 +129,7 @@ explicit ScopedMockCentralManager(MockCentralManager* mock_central_manager) { mock_central_manager_.reset( new ScopedMockCentralManager([[MockCentralManager alloc] init])); mock_central_manager_->get().bluetoothTestMac = this; - [mock_central_manager_->get() setState:CBCentralManagerStatePoweredOn]; + [mock_central_manager_->get() setState:CBManagerStatePoweredOn]; adapter_mac_->SetCentralManagerForTesting((id)mock_central_manager_->get()); } } @@ -141,7 +140,7 @@ explicit ScopedMockCentralManager(MockCentralManager* mock_central_manager) { } void BluetoothTestMac::SimulateAdapterPoweredOff() { - [mock_central_manager_->get() setState:CBCentralManagerStatePoweredOff]; + [mock_central_manager_->get() setState:CBManagerStatePoweredOff]; for (BluetoothDevice* device : adapter_->GetDevices()) { MockCBPeripheral* peripheral_mock = GetMockCBPeripheral(device); diff --git a/device/bluetooth/test/mock_bluetooth_central_manager_mac.h b/device/bluetooth/test/mock_bluetooth_central_manager_mac.h index aee59e3953fb75..e20f967f605ef9 100644 --- a/device/bluetooth/test/mock_bluetooth_central_manager_mac.h +++ b/device/bluetooth/test/mock_bluetooth_central_manager_mac.h @@ -5,12 +5,13 @@ #ifndef DEVICE_BLUETOOTH_MOCK_BLUETOOTH_CENTRAL_MANAGER_MAC_H_ #define DEVICE_BLUETOOTH_MOCK_BLUETOOTH_CENTRAL_MANAGER_MAC_H_ -#include "base/mac/sdk_forward_declarations.h" -#include "build/build_config.h" -#include "device/bluetooth/test/bluetooth_test_mac.h" - #import +#import "base/mac/sdk_forward_declarations.h" +#include "build/build_config.h" +#import "device/bluetooth/bluetooth_adapter_mac.h" +#import "device/bluetooth/test/bluetooth_test_mac.h" + // Class to mock a CBCentralManager. Cannot use a OCMockObject because mocking // the 'state' property gives a compiler warning when mock_central_manager is of // type id (multiple methods named 'state' found), and a compiler warning when @@ -21,7 +22,7 @@ @property(nonatomic, assign) NSInteger scanForPeripheralsCallCount; @property(nonatomic, assign) NSInteger stopScanCallCount; @property(nonatomic, assign) id delegate; -@property(nonatomic, assign) CBCentralManagerState state; +@property(nonatomic, assign) CBManagerState state; @property(nonatomic, assign) device::BluetoothTestMac* bluetoothTestMac; @property(nonatomic, readonly) NSArray* retrieveConnectedPeripheralServiceUUIDs;