Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_discovery_manager_mac.h" | 5 #include "device/bluetooth/bluetooth_discovery_manager_mac.h" |
| 6 | 6 |
| 7 #import <IOBluetooth/objc/IOBluetoothDevice.h> | 7 #import <IOBluetooth/objc/IOBluetoothDevice.h> |
| 8 #import <IOBluetooth/objc/IOBluetoothDeviceInquiry.h> | 8 #import <IOBluetooth/objc/IOBluetoothDeviceInquiry.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | |
| 11 #include "base/bind_helpers.h" | |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/mac/scoped_nsobject.h" | 13 #include "base/mac/scoped_nsobject.h" |
| 12 #include "base/mac/sdk_forward_declarations.h" | 14 #include "base/mac/sdk_forward_declarations.h" |
| 13 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/timer/timer.h" | |
| 14 | 17 |
| 15 namespace device { | 18 namespace device { |
| 16 | 19 |
| 17 class BluetoothDiscoveryManagerMacClassic; | 20 class BluetoothDiscoveryManagerMacClassic; |
| 18 | 21 |
| 19 } // namespace device | 22 } // namespace device |
| 20 | 23 |
| 21 // IOBluetoothDeviceInquiryDelegate implementation. | 24 // IOBluetoothDeviceInquiryDelegate implementation. |
| 22 @interface BluetoothDeviceInquiryDelegate | 25 @interface BluetoothDeviceInquiryDelegate |
| 23 : NSObject<IOBluetoothDeviceInquiryDelegate> { | 26 : NSObject<IOBluetoothDeviceInquiryDelegate> { |
| 24 @private | 27 @private |
| 25 device::BluetoothDiscoveryManagerMacClassic* manager_; // weak | 28 device::BluetoothDiscoveryManagerMacClassic* manager_; // weak |
| 26 } | 29 } |
| 27 | 30 |
| 28 - (id)initWithManager:(device::BluetoothDiscoveryManagerMacClassic*)manager; | 31 - (id)initWithManager:(device::BluetoothDiscoveryManagerMacClassic*)manager; |
| 29 | 32 |
| 30 @end | 33 @end |
| 31 | 34 |
| 32 namespace device { | 35 namespace device { |
| 33 | 36 |
| 37 const int kCleanCacheTimer = 1; | |
|
scheib
2016/08/29 02:42:32
Comment explaining why we're using 1. And include
ortuno
2016/09/13 08:02:04
Done.
| |
| 38 | |
| 34 // ementation of BluetoothDiscoveryManagerMac for Bluetooth classic device | 39 // ementation of BluetoothDiscoveryManagerMac for Bluetooth classic device |
| 35 // discovery, using the IOBluetooth framework. | 40 // discovery, using the IOBluetooth framework. |
| 36 class BluetoothDiscoveryManagerMacClassic | 41 class BluetoothDiscoveryManagerMacClassic |
| 37 : public BluetoothDiscoveryManagerMac { | 42 : public BluetoothDiscoveryManagerMac { |
| 38 public: | 43 public: |
| 39 explicit BluetoothDiscoveryManagerMacClassic(Observer* observer) | 44 explicit BluetoothDiscoveryManagerMacClassic(Observer* observer) |
| 40 : BluetoothDiscoveryManagerMac(observer), | 45 : BluetoothDiscoveryManagerMac(observer), |
| 41 should_do_discovery_(false), | 46 should_do_discovery_(false), |
| 42 inquiry_running_(false), | 47 inquiry_running_(false), |
| 43 inquiry_delegate_( | 48 inquiry_delegate_( |
| 44 [[BluetoothDeviceInquiryDelegate alloc] initWithManager:this]), | 49 [[BluetoothDeviceInquiryDelegate alloc] initWithManager:this]), |
| 45 inquiry_([[IOBluetoothDeviceInquiry alloc] | 50 inquiry_([[IOBluetoothDeviceInquiry alloc] |
| 46 initWithDelegate:inquiry_delegate_]) {} | 51 initWithDelegate:inquiry_delegate_]), |
| 52 clean_cache_timer_( | |
| 53 FROM_HERE, | |
| 54 base::TimeDelta::FromSeconds(kCleanCacheTimer), | |
| 55 base::Bind( | |
| 56 &BluetoothDiscoveryManagerMacClassic::CleanFoundDevicesCache, | |
| 57 // base::Timer guarantees it won't call back after its | |
| 58 // destructor starts. | |
| 59 base::Unretained(this)), | |
| 60 true /* is_repeating */) {} | |
| 47 | 61 |
| 48 ~BluetoothDiscoveryManagerMacClassic() override {} | 62 ~BluetoothDiscoveryManagerMacClassic() override {} |
| 49 | 63 |
| 50 // BluetoothDiscoveryManagerMac override. | 64 // BluetoothDiscoveryManagerMac override. |
| 51 bool IsDiscovering() const override { return should_do_discovery_; } | 65 bool IsDiscovering() const override { return should_do_discovery_; } |
| 52 | 66 |
| 53 // BluetoothDiscoveryManagerMac override. | 67 // BluetoothDiscoveryManagerMac override. |
| 54 bool StartDiscovery() override { | 68 bool StartDiscovery() override { |
| 55 DVLOG(1) << "Bluetooth Classic: StartDiscovery"; | 69 DVLOG(1) << "Bluetooth Classic: StartDiscovery"; |
| 56 DCHECK(!should_do_discovery_); | 70 DCHECK(!should_do_discovery_); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 67 if ([inquiry_ start] != kIOReturnSuccess) { | 81 if ([inquiry_ start] != kIOReturnSuccess) { |
| 68 DVLOG(1) << "Failed to start device inquiry"; | 82 DVLOG(1) << "Failed to start device inquiry"; |
| 69 | 83 |
| 70 // Set |should_do_discovery_| to false here. Since we're reporting an | 84 // Set |should_do_discovery_| to false here. Since we're reporting an |
| 71 // error, we're indicating that the adapter call StartDiscovery again | 85 // error, we're indicating that the adapter call StartDiscovery again |
| 72 // if needed. | 86 // if needed. |
| 73 should_do_discovery_ = false; | 87 should_do_discovery_ = false; |
| 74 return false; | 88 return false; |
| 75 } | 89 } |
| 76 | 90 |
| 91 clean_cache_timer_.Reset(); | |
| 77 DVLOG(1) << "Device inquiry start was successful"; | 92 DVLOG(1) << "Device inquiry start was successful"; |
| 78 return true; | 93 return true; |
| 79 } | 94 } |
| 80 | 95 |
| 81 // BluetoothDiscoveryManagerMac override. | 96 // BluetoothDiscoveryManagerMac override. |
| 82 bool StopDiscovery() override { | 97 bool StopDiscovery() override { |
| 83 DVLOG(1) << "Bluetooth Classic: StopDiscovery"; | 98 DVLOG(1) << "Bluetooth Classic: StopDiscovery"; |
| 84 DCHECK(should_do_discovery_); | 99 DCHECK(should_do_discovery_); |
| 85 | 100 |
| 86 should_do_discovery_ = false; | 101 should_do_discovery_ = false; |
| 87 | 102 |
| 88 if (!inquiry_running_) { | 103 if (!inquiry_running_) { |
| 89 DVLOG(1) << "No device inquiry running; discovery stopped"; | 104 DVLOG(1) << "No device inquiry running; discovery stopped"; |
| 90 return true; | 105 return true; |
| 91 } | 106 } |
| 92 | 107 |
| 93 DVLOG(1) << "Requesting to stop device inquiry"; | 108 DVLOG(1) << "Requesting to stop device inquiry"; |
| 109 CleanFoundDevicesCache(); | |
| 110 clean_cache_timer_.Stop(); | |
| 111 | |
| 94 IOReturn status = [inquiry_ stop]; | 112 IOReturn status = [inquiry_ stop]; |
| 95 if (status == kIOReturnSuccess) { | 113 if (status == kIOReturnSuccess) { |
| 96 DVLOG(1) << "Device inquiry stop was successful"; | 114 DVLOG(1) << "Device inquiry stop was successful"; |
| 97 return true; | 115 return true; |
| 98 } | 116 } |
| 99 | 117 |
| 100 if (status == kIOReturnNotPermitted) { | 118 if (status == kIOReturnNotPermitted) { |
| 101 DVLOG(1) << "Device inquiry was already stopped"; | 119 DVLOG(1) << "Device inquiry was already stopped"; |
| 102 return true; | 120 return true; |
| 103 } | 121 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 return; | 177 return; |
| 160 } | 178 } |
| 161 | 179 |
| 162 DVLOG(1) << "Failed to restart discovery"; | 180 DVLOG(1) << "Failed to restart discovery"; |
| 163 should_do_discovery_ = false; | 181 should_do_discovery_ = false; |
| 164 DCHECK(observer_); | 182 DCHECK(observer_); |
| 165 observer_->ClassicDiscoveryStopped(true /* unexpected */); | 183 observer_->ClassicDiscoveryStopped(true /* unexpected */); |
| 166 } | 184 } |
| 167 | 185 |
| 168 private: | 186 private: |
| 187 void CleanFoundDevicesCache() { [inquiry_ clearFoundDevices]; } | |
| 188 | |
| 169 // The requested discovery state. | 189 // The requested discovery state. |
| 170 bool should_do_discovery_; | 190 bool should_do_discovery_; |
| 171 | 191 |
| 172 // The current inquiry state. | 192 // The current inquiry state. |
| 173 bool inquiry_running_; | 193 bool inquiry_running_; |
| 174 | 194 |
| 175 // Objective-C objects for running and tracking device inquiry. | 195 // Objective-C objects for running and tracking device inquiry. |
| 176 base::scoped_nsobject<BluetoothDeviceInquiryDelegate> inquiry_delegate_; | 196 base::scoped_nsobject<BluetoothDeviceInquiryDelegate> inquiry_delegate_; |
| 177 base::scoped_nsobject<IOBluetoothDeviceInquiry> inquiry_; | 197 base::scoped_nsobject<IOBluetoothDeviceInquiry> inquiry_; |
| 178 | 198 |
| 199 // Timer to clean up the cache. That way we get notified every time a device | |
| 200 // is seen. | |
| 201 base::Timer clean_cache_timer_; | |
| 202 | |
| 179 DISALLOW_COPY_AND_ASSIGN(BluetoothDiscoveryManagerMacClassic); | 203 DISALLOW_COPY_AND_ASSIGN(BluetoothDiscoveryManagerMacClassic); |
| 180 }; | 204 }; |
| 181 | 205 |
| 182 BluetoothDiscoveryManagerMac::BluetoothDiscoveryManagerMac( | 206 BluetoothDiscoveryManagerMac::BluetoothDiscoveryManagerMac( |
| 183 Observer* observer) : observer_(observer) { | 207 Observer* observer) : observer_(observer) { |
| 184 DCHECK(observer); | 208 DCHECK(observer); |
| 185 } | 209 } |
| 186 | 210 |
| 187 BluetoothDiscoveryManagerMac::~BluetoothDiscoveryManagerMac() { | 211 BluetoothDiscoveryManagerMac::~BluetoothDiscoveryManagerMac() { |
| 188 } | 212 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 214 manager_->DeviceFound(sender, device); | 238 manager_->DeviceFound(sender, device); |
| 215 } | 239 } |
| 216 | 240 |
| 217 - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender | 241 - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender |
| 218 error:(IOReturn)error | 242 error:(IOReturn)error |
| 219 aborted:(BOOL)aborted { | 243 aborted:(BOOL)aborted { |
| 220 manager_->DeviceInquiryComplete(sender, error, aborted); | 244 manager_->DeviceInquiryComplete(sender, error, aborted); |
| 221 } | 245 } |
| 222 | 246 |
| 223 @end | 247 @end |
| OLD | NEW |