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

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

Issue 2282763004: bluetooth: mac: Improve classic device discovery and update (Closed)
Patch Set: Override getlastupdate and make constexpr Created 4 years, 3 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 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 // 1 second between cache clean ups is a good compromise between latency and
38 // performance.
39 constexpr base::TimeDelta kCleanCacheDelay = base::TimeDelta::FromSeconds(1);
40
34 // ementation of BluetoothDiscoveryManagerMac for Bluetooth classic device 41 // ementation of BluetoothDiscoveryManagerMac for Bluetooth classic device
35 // discovery, using the IOBluetooth framework. 42 // discovery, using the IOBluetooth framework.
36 class BluetoothDiscoveryManagerMacClassic 43 class BluetoothDiscoveryManagerMacClassic
37 : public BluetoothDiscoveryManagerMac { 44 : public BluetoothDiscoveryManagerMac {
38 public: 45 public:
39 explicit BluetoothDiscoveryManagerMacClassic(Observer* observer) 46 explicit BluetoothDiscoveryManagerMacClassic(Observer* observer)
40 : BluetoothDiscoveryManagerMac(observer), 47 : BluetoothDiscoveryManagerMac(observer),
41 should_do_discovery_(false), 48 should_do_discovery_(false),
42 inquiry_running_(false), 49 inquiry_running_(false),
43 inquiry_delegate_( 50 inquiry_delegate_(
44 [[BluetoothDeviceInquiryDelegate alloc] initWithManager:this]), 51 [[BluetoothDeviceInquiryDelegate alloc] initWithManager:this]),
45 inquiry_([[IOBluetoothDeviceInquiry alloc] 52 inquiry_([[IOBluetoothDeviceInquiry alloc]
46 initWithDelegate:inquiry_delegate_]) {} 53 initWithDelegate:inquiry_delegate_]),
54 clean_cache_timer_(
55 FROM_HERE,
56 kCleanCacheDelay,
57 base::Bind(
58 &BluetoothDiscoveryManagerMacClassic::CleanFoundDevicesCache,
59 // base::Timer guarantees it won't call back after its
60 // destructor starts.
61 base::Unretained(this)),
62 true /* is_repeating */) {}
47 63
48 ~BluetoothDiscoveryManagerMacClassic() override {} 64 ~BluetoothDiscoveryManagerMacClassic() override {}
49 65
50 // BluetoothDiscoveryManagerMac override. 66 // BluetoothDiscoveryManagerMac override.
51 bool IsDiscovering() const override { return should_do_discovery_; } 67 bool IsDiscovering() const override { return should_do_discovery_; }
52 68
53 // BluetoothDiscoveryManagerMac override. 69 // BluetoothDiscoveryManagerMac override.
54 bool StartDiscovery() override { 70 bool StartDiscovery() override {
55 DVLOG(1) << "Bluetooth Classic: StartDiscovery"; 71 DVLOG(1) << "Bluetooth Classic: StartDiscovery";
56 DCHECK(!should_do_discovery_); 72 DCHECK(!should_do_discovery_);
(...skipping 10 matching lines...) Expand all
67 if ([inquiry_ start] != kIOReturnSuccess) { 83 if ([inquiry_ start] != kIOReturnSuccess) {
68 DVLOG(1) << "Failed to start device inquiry"; 84 DVLOG(1) << "Failed to start device inquiry";
69 85
70 // Set |should_do_discovery_| to false here. Since we're reporting an 86 // Set |should_do_discovery_| to false here. Since we're reporting an
71 // error, we're indicating that the adapter call StartDiscovery again 87 // error, we're indicating that the adapter call StartDiscovery again
72 // if needed. 88 // if needed.
73 should_do_discovery_ = false; 89 should_do_discovery_ = false;
74 return false; 90 return false;
75 } 91 }
76 92
93 CleanFoundDevicesCache();
94 clean_cache_timer_.Reset();
77 DVLOG(1) << "Device inquiry start was successful"; 95 DVLOG(1) << "Device inquiry start was successful";
78 return true; 96 return true;
79 } 97 }
80 98
81 // BluetoothDiscoveryManagerMac override. 99 // BluetoothDiscoveryManagerMac override.
82 bool StopDiscovery() override { 100 bool StopDiscovery() override {
83 DVLOG(1) << "Bluetooth Classic: StopDiscovery"; 101 DVLOG(1) << "Bluetooth Classic: StopDiscovery";
84 DCHECK(should_do_discovery_); 102 DCHECK(should_do_discovery_);
85 103
86 should_do_discovery_ = false; 104 should_do_discovery_ = false;
87 105
88 if (!inquiry_running_) { 106 if (!inquiry_running_) {
89 DVLOG(1) << "No device inquiry running; discovery stopped"; 107 DVLOG(1) << "No device inquiry running; discovery stopped";
90 return true; 108 return true;
91 } 109 }
92 110
93 DVLOG(1) << "Requesting to stop device inquiry"; 111 DVLOG(1) << "Requesting to stop device inquiry";
112 CleanFoundDevicesCache();
113 clean_cache_timer_.Stop();
114
94 IOReturn status = [inquiry_ stop]; 115 IOReturn status = [inquiry_ stop];
95 if (status == kIOReturnSuccess) { 116 if (status == kIOReturnSuccess) {
96 DVLOG(1) << "Device inquiry stop was successful"; 117 DVLOG(1) << "Device inquiry stop was successful";
97 return true; 118 return true;
98 } 119 }
99 120
100 if (status == kIOReturnNotPermitted) { 121 if (status == kIOReturnNotPermitted) {
101 DVLOG(1) << "Device inquiry was already stopped"; 122 DVLOG(1) << "Device inquiry was already stopped";
102 return true; 123 return true;
103 } 124 }
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 return; 180 return;
160 } 181 }
161 182
162 DVLOG(1) << "Failed to restart discovery"; 183 DVLOG(1) << "Failed to restart discovery";
163 should_do_discovery_ = false; 184 should_do_discovery_ = false;
164 DCHECK(observer_); 185 DCHECK(observer_);
165 observer_->ClassicDiscoveryStopped(true /* unexpected */); 186 observer_->ClassicDiscoveryStopped(true /* unexpected */);
166 } 187 }
167 188
168 private: 189 private:
190 void CleanFoundDevicesCache() { [inquiry_ clearFoundDevices]; }
191
169 // The requested discovery state. 192 // The requested discovery state.
170 bool should_do_discovery_; 193 bool should_do_discovery_;
171 194
172 // The current inquiry state. 195 // The current inquiry state.
173 bool inquiry_running_; 196 bool inquiry_running_;
174 197
175 // Objective-C objects for running and tracking device inquiry. 198 // Objective-C objects for running and tracking device inquiry.
176 base::scoped_nsobject<BluetoothDeviceInquiryDelegate> inquiry_delegate_; 199 base::scoped_nsobject<BluetoothDeviceInquiryDelegate> inquiry_delegate_;
177 base::scoped_nsobject<IOBluetoothDeviceInquiry> inquiry_; 200 base::scoped_nsobject<IOBluetoothDeviceInquiry> inquiry_;
178 201
202 // Timer to clean up the cache. That way we get notified every time a device
203 // is seen.
204 base::Timer clean_cache_timer_;
205
179 DISALLOW_COPY_AND_ASSIGN(BluetoothDiscoveryManagerMacClassic); 206 DISALLOW_COPY_AND_ASSIGN(BluetoothDiscoveryManagerMacClassic);
180 }; 207 };
181 208
182 BluetoothDiscoveryManagerMac::BluetoothDiscoveryManagerMac( 209 BluetoothDiscoveryManagerMac::BluetoothDiscoveryManagerMac(
183 Observer* observer) : observer_(observer) { 210 Observer* observer) : observer_(observer) {
184 DCHECK(observer); 211 DCHECK(observer);
185 } 212 }
186 213
187 BluetoothDiscoveryManagerMac::~BluetoothDiscoveryManagerMac() { 214 BluetoothDiscoveryManagerMac::~BluetoothDiscoveryManagerMac() {
188 } 215 }
(...skipping 25 matching lines...) Expand all
214 manager_->DeviceFound(sender, device); 241 manager_->DeviceFound(sender, device);
215 } 242 }
216 243
217 - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender 244 - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender
218 error:(IOReturn)error 245 error:(IOReturn)error
219 aborted:(BOOL)aborted { 246 aborted:(BOOL)aborted {
220 manager_->DeviceInquiryComplete(sender, error, aborted); 247 manager_->DeviceInquiryComplete(sender, error, aborted);
221 } 248 }
222 249
223 @end 250 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698