OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_adapter_mac.h" | 5 #include "device/bluetooth/bluetooth_adapter_mac.h" |
6 | 6 |
7 #include <IOBluetooth/objc/IOBluetoothHostController.h> | 7 #import <IObluetooth/objc/IOBluetoothDevice.h> |
Mark Mentovai
2013/03/19 16:04:35
Fix capitalization. This only works because you’re
youngki
2013/03/19 19:04:55
Sorry about this; done.
| |
8 #import <IOBluetooth/objc/IOBluetoothHostController.h> | |
8 | 9 |
9 #include <string> | 10 #include <string> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/hash_tables.h" | |
13 #include "base/location.h" | 15 #include "base/location.h" |
14 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
15 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
16 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
17 #include "base/time.h" | 19 #include "base/time.h" |
18 #include "base/strings/sys_string_conversions.h" | 20 #include "base/strings/sys_string_conversions.h" |
21 #include "device/bluetooth/bluetooth_device_mac.h" | |
19 | 22 |
20 // Replicate specific 10.7 SDK declarations for building with prior SDKs. | 23 // Replicate specific 10.7 SDK declarations for building with prior SDKs. |
21 #if !defined(MAC_OS_X_VERSION_10_7) || \ | 24 #if !defined(MAC_OS_X_VERSION_10_7) || \ |
22 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 | 25 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 |
23 | 26 |
24 @interface IOBluetoothHostController (LionSDKDeclarations) | 27 @interface IOBluetoothHostController (LionSDKDeclarations) |
25 - (NSString *)nameAsString; | 28 - (NSString *)nameAsString; |
26 - (BluetoothHCIPowerState)powerState; | 29 - (BluetoothHCIPowerState)powerState; |
27 @end | 30 @end |
28 | 31 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 if (was_present != is_present) { | 129 if (was_present != is_present) { |
127 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 130 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
128 AdapterPresentChanged(this, is_present)); | 131 AdapterPresentChanged(this, is_present)); |
129 } | 132 } |
130 if (powered_ != powered) { | 133 if (powered_ != powered) { |
131 powered_ = powered; | 134 powered_ = powered; |
132 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 135 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
133 AdapterPoweredChanged(this, powered_)); | 136 AdapterPoweredChanged(this, powered_)); |
134 } | 137 } |
135 | 138 |
139 UpdateDevices([IOBluetoothDevice recentDevices:0]); | |
140 | |
136 ui_task_runner_->PostDelayedTask( | 141 ui_task_runner_->PostDelayedTask( |
137 FROM_HERE, | 142 FROM_HERE, |
138 base::Bind(&BluetoothAdapterMac::PollAdapter, | 143 base::Bind(&BluetoothAdapterMac::PollAdapter, |
139 weak_ptr_factory_.GetWeakPtr()), | 144 weak_ptr_factory_.GetWeakPtr()), |
140 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); | 145 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); |
141 } | 146 } |
142 | 147 |
148 void BluetoothAdapterMac::UpdateDevices(NSArray* devices) { | |
149 base::hash_set<std::string> device_address_list; | |
150 // Check if |devices| contains new or updated devices. | |
151 for (IOBluetoothDevice* device in devices) { | |
152 std::string device_address = | |
153 base::SysNSStringToUTF8([device addressString]); | |
154 device_address_list.insert(device_address); | |
155 DevicesMap::iterator found_device_iter = devices_.find(device_address); | |
156 | |
157 if (found_device_iter == devices_.end()) { | |
158 devices_[device_address] = new BluetoothDeviceMac(device); | |
159 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
160 DeviceAdded(this, devices_[device_address])); | |
161 continue; | |
162 } | |
163 BluetoothDeviceMac* device_mac = | |
164 static_cast<BluetoothDeviceMac*>(found_device_iter->second); | |
165 if (device_mac->device_fingerprint() != | |
166 BluetoothDeviceMac::ComputeDeviceFingerprint(device)) { | |
167 devices_[device_address] = new BluetoothDeviceMac(device); | |
168 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
169 DeviceChanged(this, devices_[device_address])); | |
170 delete device_mac; | |
171 } | |
172 } | |
173 | |
174 // Remove devices that are no longer known by the system. | |
Mark Mentovai
2013/03/19 16:04:35
How does something become unknown by the system? T
youngki
2013/03/19 19:04:55
So.. the documentation didn't say that since when
Mark Mentovai
2013/03/19 19:14:20
I don’t know if it’s right to remove it. I wasn’t
youngki
2013/03/20 18:48:26
I actually reviewed the documentations about Bluet
| |
175 DevicesMap::iterator device_iter = devices_.begin(); | |
176 while (device_iter != devices_.end()) { | |
177 DevicesMap::iterator temp = device_iter; | |
Mark Mentovai
2013/03/19 16:04:35
This isn’t really temporary at all, you use it thr
youngki
2013/03/19 19:04:55
I removed the code because this is no longer neces
| |
178 ++device_iter; | |
179 | |
180 if (device_address_list.find(temp->first) != device_address_list.end()) | |
181 continue; | |
182 | |
183 if (temp->second->IsConnected() || temp->second->IsPaired()) { | |
184 BluetoothDeviceMac* device_mac = | |
Mark Mentovai
2013/03/19 16:04:35
Pull this out of the enclosing conditional, so tha
youngki
2013/03/19 19:04:55
I removed the code because this is no longer neces
| |
185 static_cast<BluetoothDeviceMac*>(temp->second); | |
186 device_mac->SetVisible(false); | |
187 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
188 DeviceChanged(this, device_mac)); | |
Mark Mentovai
2013/03/19 16:04:35
…but it’s not in “devices,” so will it ever be cle
youngki
2013/03/19 19:04:55
I removed the code because this is no longer neces
| |
189 continue; | |
190 } | |
191 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
192 DeviceRemoved(this, temp->second)); | |
193 delete temp->second; | |
194 devices_.erase(temp); | |
195 } | |
196 } | |
197 | |
143 } // namespace device | 198 } // namespace device |
OLD | NEW |