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

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

Issue 2282763004: bluetooth: mac: Improve classic device discovery and update (Closed)
Patch Set: Add comment and fix comment 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 const int kCleanCacheTimer = 1;
Jeffrey Yasskin 2016/09/13 20:58:05 I think you can use a global constexpr TimeDelta n
ortuno 2016/09/14 04:48:25 Argh, I always forget that we should use TimeDelta
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 base::TimeDelta::FromSeconds(kCleanCacheTimer),
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 clean_cache_timer_.Reset();
77 DVLOG(1) << "Device inquiry start was successful"; 94 DVLOG(1) << "Device inquiry start was successful";
78 return true; 95 return true;
79 } 96 }
80 97
81 // BluetoothDiscoveryManagerMac override. 98 // BluetoothDiscoveryManagerMac override.
82 bool StopDiscovery() override { 99 bool StopDiscovery() override {
83 DVLOG(1) << "Bluetooth Classic: StopDiscovery"; 100 DVLOG(1) << "Bluetooth Classic: StopDiscovery";
84 DCHECK(should_do_discovery_); 101 DCHECK(should_do_discovery_);
85 102
86 should_do_discovery_ = false; 103 should_do_discovery_ = false;
87 104
88 if (!inquiry_running_) { 105 if (!inquiry_running_) {
89 DVLOG(1) << "No device inquiry running; discovery stopped"; 106 DVLOG(1) << "No device inquiry running; discovery stopped";
90 return true; 107 return true;
91 } 108 }
92 109
93 DVLOG(1) << "Requesting to stop device inquiry"; 110 DVLOG(1) << "Requesting to stop device inquiry";
111 CleanFoundDevicesCache();
112 clean_cache_timer_.Stop();
113
94 IOReturn status = [inquiry_ stop]; 114 IOReturn status = [inquiry_ stop];
95 if (status == kIOReturnSuccess) { 115 if (status == kIOReturnSuccess) {
96 DVLOG(1) << "Device inquiry stop was successful"; 116 DVLOG(1) << "Device inquiry stop was successful";
97 return true; 117 return true;
98 } 118 }
99 119
100 if (status == kIOReturnNotPermitted) { 120 if (status == kIOReturnNotPermitted) {
101 DVLOG(1) << "Device inquiry was already stopped"; 121 DVLOG(1) << "Device inquiry was already stopped";
102 return true; 122 return true;
103 } 123 }
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 return; 179 return;
160 } 180 }
161 181
162 DVLOG(1) << "Failed to restart discovery"; 182 DVLOG(1) << "Failed to restart discovery";
163 should_do_discovery_ = false; 183 should_do_discovery_ = false;
164 DCHECK(observer_); 184 DCHECK(observer_);
165 observer_->ClassicDiscoveryStopped(true /* unexpected */); 185 observer_->ClassicDiscoveryStopped(true /* unexpected */);
166 } 186 }
167 187
168 private: 188 private:
189 void CleanFoundDevicesCache() { [inquiry_ clearFoundDevices]; }
190
169 // The requested discovery state. 191 // The requested discovery state.
170 bool should_do_discovery_; 192 bool should_do_discovery_;
171 193
172 // The current inquiry state. 194 // The current inquiry state.
173 bool inquiry_running_; 195 bool inquiry_running_;
174 196
175 // Objective-C objects for running and tracking device inquiry. 197 // Objective-C objects for running and tracking device inquiry.
176 base::scoped_nsobject<BluetoothDeviceInquiryDelegate> inquiry_delegate_; 198 base::scoped_nsobject<BluetoothDeviceInquiryDelegate> inquiry_delegate_;
177 base::scoped_nsobject<IOBluetoothDeviceInquiry> inquiry_; 199 base::scoped_nsobject<IOBluetoothDeviceInquiry> inquiry_;
178 200
201 // Timer to clean up the cache. That way we get notified every time a device
202 // is seen.
203 base::Timer clean_cache_timer_;
204
179 DISALLOW_COPY_AND_ASSIGN(BluetoothDiscoveryManagerMacClassic); 205 DISALLOW_COPY_AND_ASSIGN(BluetoothDiscoveryManagerMacClassic);
180 }; 206 };
181 207
182 BluetoothDiscoveryManagerMac::BluetoothDiscoveryManagerMac( 208 BluetoothDiscoveryManagerMac::BluetoothDiscoveryManagerMac(
183 Observer* observer) : observer_(observer) { 209 Observer* observer) : observer_(observer) {
184 DCHECK(observer); 210 DCHECK(observer);
185 } 211 }
186 212
187 BluetoothDiscoveryManagerMac::~BluetoothDiscoveryManagerMac() { 213 BluetoothDiscoveryManagerMac::~BluetoothDiscoveryManagerMac() {
188 } 214 }
(...skipping 25 matching lines...) Expand all
214 manager_->DeviceFound(sender, device); 240 manager_->DeviceFound(sender, device);
215 } 241 }
216 242
217 - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender 243 - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender
218 error:(IOReturn)error 244 error:(IOReturn)error
219 aborted:(BOOL)aborted { 245 aborted:(BOOL)aborted {
220 manager_->DeviceInquiryComplete(sender, error, aborted); 246 manager_->DeviceInquiryComplete(sender, error, aborted);
221 } 247 }
222 248
223 @end 249 @end
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_device.h ('k') | device/bluetooth/bluetooth_low_energy_discovery_manager_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698