Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / device / bluetooth / bluetooth_low_energy_discovery_manager_mac.mm
blobd14b5784356e634bce3959fdc0fb0460b98ced04
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
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_low_energy_discovery_manager_mac.h"
7 #include "base/mac/mac_util.h"
8 #include "base/mac/sdk_forward_declarations.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "device/bluetooth/bluetooth_adapter_mac.h"
12 #include "device/bluetooth/bluetooth_low_energy_device_mac.h"
14 namespace device {
16 BluetoothLowEnergyDiscoveryManagerMac::
17     ~BluetoothLowEnergyDiscoveryManagerMac() {
20 bool BluetoothLowEnergyDiscoveryManagerMac::IsDiscovering() const {
21   return discovering_;
24 void BluetoothLowEnergyDiscoveryManagerMac::StartDiscovery(
25     BluetoothDevice::UUIDList services_uuids) {
26   discovering_ = true;
27   pending_ = true;
28   services_uuids_ = services_uuids;
29   TryStartDiscovery();
32 void BluetoothLowEnergyDiscoveryManagerMac::TryStartDiscovery() {
33   if (!discovering_) {
34     VLOG(1) << "TryStartDiscovery !discovering_";
35     return;
36   }
38   if (!pending_) {
39     VLOG(1) << "TryStartDiscovery !pending_";
40     return;
41   }
43   if (!central_manager_) {
44     VLOG(1) << "TryStartDiscovery !central_manager_";
45     return;
46   }
48   if ([central_manager_ state] != CBCentralManagerStatePoweredOn) {
49     VLOG(1) << "TryStartDiscovery != CBCentralManagerStatePoweredOn";
50     return;
51   }
53   // Converts the services UUIDs to a CoreBluetooth data structure.
54   NSMutableArray* services = nil;
55   if (!services_uuids_.empty()) {
56     services = [NSMutableArray array];
57     for (auto& service_uuid : services_uuids_) {
58       NSString* uuidString =
59           base::SysUTF8ToNSString(service_uuid.canonical_value().c_str());
60       Class aClass = NSClassFromString(@"CBUUID");
61       CBUUID* uuid = [aClass UUIDWithString:uuidString];
62       [services addObject:uuid];
63     }
64   };
66   VLOG(1) << "TryStartDiscovery scanForPeripheralsWithServices";
67   [central_manager_ scanForPeripheralsWithServices:services options:nil];
68   pending_ = false;
71 void BluetoothLowEnergyDiscoveryManagerMac::StopDiscovery() {
72   VLOG(1) << "StopDiscovery";
73   if (discovering_ && !pending_) {
74     [central_manager_ stopScan];
75   }
76   discovering_ = false;
79 void BluetoothLowEnergyDiscoveryManagerMac::SetCentralManager(
80     CBCentralManager* central_manager) {
81   central_manager_ = central_manager;
84 void BluetoothLowEnergyDiscoveryManagerMac::DiscoveredPeripheral(
85     CBPeripheral* peripheral,
86     NSDictionary* advertisementData,
87     int rssi) {
88   VLOG(1) << "DiscoveredPeripheral";
89   observer_->LowEnergyDeviceUpdated(peripheral, advertisementData, rssi);
92 BluetoothLowEnergyDiscoveryManagerMac*
93 BluetoothLowEnergyDiscoveryManagerMac::Create(Observer* observer) {
94   return new BluetoothLowEnergyDiscoveryManagerMac(observer);
97 BluetoothLowEnergyDiscoveryManagerMac::BluetoothLowEnergyDiscoveryManagerMac(
98     Observer* observer)
99     : observer_(observer) {
100   DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable());
101   discovering_ = false;
104 }  // namespace device