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

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

Issue 1228863002: Runtime checks so that CoreBluetooth only used on >= 10.10 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@timeinfo
Patch Set: not compiling 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_low_energy_discovery_manager_mac.h" 5 #include "device/bluetooth/bluetooth_low_energy_discovery_manager_mac.h"
6 6
7 #include "base/mac/mac_util.h" 7 #include "base/mac/mac_util.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/sys_string_conversions.h" 9 #include "base/strings/sys_string_conversions.h"
10 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" 10 #include "device/bluetooth/bluetooth_low_energy_device_mac.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 72
73 - (void)centralManagerDidUpdateState:(CBCentralManager*)central { 73 - (void)centralManagerDidUpdateState:(CBCentralManager*)central {
74 // Notifies when the powered state of the central manager changed. 74 // Notifies when the powered state of the central manager changed.
75 delegate_->TryStartDiscovery(); 75 delegate_->TryStartDiscovery();
76 } 76 }
77 77
78 @end 78 @end
79 79
80 BluetoothLowEnergyDiscoveryManagerMac:: 80 BluetoothLowEnergyDiscoveryManagerMac::
81 ~BluetoothLowEnergyDiscoveryManagerMac() { 81 ~BluetoothLowEnergyDiscoveryManagerMac() {
82 // Set the manager's delegate to nil since the object it points to
83 // (|bridge_|) will be deallocated while |manager_| is leaked.
84 if (base::mac::IsOSLionOrLater()) {
85 // CoreBluetooth only available in OSX 10.7 and later.
86 SEL selector = NSSelectorFromString(@"setDelegate:");
87 [manager_ performSelector:selector withObject:nil];
88 }
89 } 82 }
90 83
91 bool BluetoothLowEnergyDiscoveryManagerMac::IsDiscovering() const { 84 bool BluetoothLowEnergyDiscoveryManagerMac::IsDiscovering() const {
92 return discovering_; 85 return discovering_;
93 } 86 }
94 87
95 void BluetoothLowEnergyDiscoveryManagerMac::StartDiscovery( 88 void BluetoothLowEnergyDiscoveryManagerMac::StartDiscovery(
96 BluetoothDevice::UUIDList services_uuids) { 89 BluetoothDevice::UUIDList services_uuids) {
97 discovering_ = true; 90 discovering_ = true;
98 pending_ = true; 91 pending_ = true;
(...skipping 15 matching lines...) Expand all
114 return; 107 return;
115 } 108 }
116 109
117 // Converts the services UUIDs to a CoreBluetooth data structure. 110 // Converts the services UUIDs to a CoreBluetooth data structure.
118 NSMutableArray* services = nil; 111 NSMutableArray* services = nil;
119 if (!services_uuids_.empty()) { 112 if (!services_uuids_.empty()) {
120 services = [NSMutableArray array]; 113 services = [NSMutableArray array];
121 for (auto& service_uuid : services_uuids_) { 114 for (auto& service_uuid : services_uuids_) {
122 NSString* uuidString = 115 NSString* uuidString =
123 base::SysUTF8ToNSString(service_uuid.canonical_value().c_str()); 116 base::SysUTF8ToNSString(service_uuid.canonical_value().c_str());
124 Class aClass = NSClassFromString(@"CBUUID"); 117 CBUUID* uuid = [CBUUID UUIDWithString:uuidString];
125 CBUUID* uuid = [aClass UUIDWithString:uuidString];
126 [services addObject:uuid]; 118 [services addObject:uuid];
127 } 119 }
128 }; 120 };
129 121
130 [manager_ scanForPeripheralsWithServices:services options:nil]; 122 [manager_ scanForPeripheralsWithServices:services options:nil];
131 pending_ = false; 123 pending_ = false;
132 } 124 }
133 125
134 void BluetoothLowEnergyDiscoveryManagerMac::StopDiscovery() { 126 void BluetoothLowEnergyDiscoveryManagerMac::StopDiscovery() {
135 if (discovering_ && !pending_) { 127 if (discovering_ && !pending_) {
136 [manager_ stopScan]; 128 [manager_ stopScan];
137 } 129 }
138 discovering_ = false; 130 discovering_ = false;
139 } 131 }
140 132
141 void BluetoothLowEnergyDiscoveryManagerMac::DiscoveredPeripheral( 133 void BluetoothLowEnergyDiscoveryManagerMac::DiscoveredPeripheral(
142 CBPeripheral* peripheral, 134 CBPeripheral* peripheral,
143 NSDictionary* advertisementData, 135 NSDictionary* advertisementData,
144 int rssi) { 136 int rssi) {
145 observer_->LowEnergyDeviceUpdated(peripheral, advertisementData, rssi); 137 observer_->LowEnergyDeviceUpdated(peripheral, advertisementData, rssi);
146 } 138 }
147 139
148 void BluetoothLowEnergyDiscoveryManagerMac::SetManagerForTesting( 140 void BluetoothLowEnergyDiscoveryManagerMac::SetManagerForTesting(
149 CBCentralManager* manager) { 141 CBCentralManager* manager) {
150 // setDelegate is only available in OSX 10.7 and later. 142 // For stability we only use CoreBluetooth on OS X >= 10.10.
151 CHECK(base::mac::IsOSLionOrLater()); 143 CHECK(base::mac::IsOSYosemiteOrLater());
152 SEL selector = NSSelectorFromString(@"setDelegate:"); 144 [manager setDelegate:bridge_];
153 [manager performSelector:selector withObject:bridge_];
154 manager_.reset(manager); 145 manager_.reset(manager);
155 } 146 }
156 147
157 BluetoothLowEnergyDiscoveryManagerMac* 148 BluetoothLowEnergyDiscoveryManagerMac*
158 BluetoothLowEnergyDiscoveryManagerMac::Create(Observer* observer) { 149 BluetoothLowEnergyDiscoveryManagerMac::Create(Observer* observer) {
159 return new BluetoothLowEnergyDiscoveryManagerMac(observer); 150 return new BluetoothLowEnergyDiscoveryManagerMac(observer);
160 } 151 }
161 152
162 BluetoothLowEnergyDiscoveryManagerMac::BluetoothLowEnergyDiscoveryManagerMac( 153 BluetoothLowEnergyDiscoveryManagerMac::BluetoothLowEnergyDiscoveryManagerMac(
163 Observer* observer) 154 Observer* observer)
164 : observer_(observer) { 155 : observer_(observer) {
156 // For stability we only use CoreBluetooth on OS X >= 10.10. Thus
157 // BluetoothLowEnergyDiscoveryManagerMac should only be instantiated if on >=
158 // 10.10.
159 CHECK(base::mac::IsOSYosemiteOrLater());
165 bridge_.reset([[BluetoothLowEnergyDiscoveryManagerMacBridge alloc] 160 bridge_.reset([[BluetoothLowEnergyDiscoveryManagerMacBridge alloc]
166 initWithManager:this]); 161 initWithManager:this]);
167 // Since CoreBluetooth is only available on OS X 10.7 or later, we 162 manager_.reset(
168 // instantiate CBCentralManager only for OS X >= 10.7. 163 [[CBCentralManager alloc] initWithDelegate:bridge_
169 if (base::mac::IsOSLionOrLater()) { 164 queue:dispatch_get_main_queue()]);
170 Class aClass = NSClassFromString(@"CBCentralManager");
171 manager_.reset(
172 [[aClass alloc] initWithDelegate:bridge_
173 queue:dispatch_get_main_queue()]);
174 // Increment reference count, see comment at declaration.
175 [manager_ retain];
176 }
177 discovering_ = false; 165 discovering_ = false;
178 } 166 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698