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

Side by Side Diff: device/bluetooth/bluetooth_adapter_android.cc

Issue 2244693002: bluetooth: Refactor how we update based on Advertising Data (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix arc Created 4 years, 4 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 2015 The Chromium Authors. All rights reserved. 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 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_android.h" 5 #include "device/bluetooth/bluetooth_adapter_android.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/android/jni_android.h" 9 #include "base/android/jni_android.h"
10 #include "base/android/jni_array.h"
10 #include "base/android/jni_string.h" 11 #include "base/android/jni_string.h"
11 #include "base/bind.h" 12 #include "base/bind.h"
12 #include "base/location.h" 13 #include "base/location.h"
13 #include "base/sequenced_task_runner.h" 14 #include "base/sequenced_task_runner.h"
14 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
15 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
16 #include "device/bluetooth/android/wrappers.h" 17 #include "device/bluetooth/android/wrappers.h"
17 #include "device/bluetooth/bluetooth_advertisement.h" 18 #include "device/bluetooth/bluetooth_advertisement.h"
18 #include "device/bluetooth/bluetooth_device_android.h" 19 #include "device/bluetooth/bluetooth_device_android.h"
19 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" 20 #include "device/bluetooth/bluetooth_discovery_session_outcome.h"
20 #include "jni/ChromeBluetoothAdapter_jni.h" 21 #include "jni/ChromeBluetoothAdapter_jni.h"
21 22
22 using base::android::AttachCurrentThread; 23 using base::android::AttachCurrentThread;
23 using base::android::ConvertJavaStringToUTF8; 24 using base::android::ConvertJavaStringToUTF8;
25 using base::android::AppendJavaStringArrayToStringVector;
24 using base::android::JavaParamRef; 26 using base::android::JavaParamRef;
25 27
26 namespace { 28 namespace {
27 // The poll interval in ms when there is no active discovery. This 29 // The poll interval in ms when there is no active discovery. This
28 // matches the max allowed advertisting interval for connectable 30 // matches the max allowed advertisting interval for connectable
29 // devices. 31 // devices.
30 enum { kPassivePollInterval = 11000 }; 32 enum { kPassivePollInterval = 11000 };
31 // The poll interval in ms when there is an active discovery. 33 // The poll interval in ms when there is an active discovery.
32 enum { kActivePollInterval = 1000 }; 34 enum { kActivePollInterval = 1000 };
33 // The delay in ms to wait before purging devices when a scan starts. 35 // The delay in ms to wait before purging devices when a scan starts.
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 JNIEnv* env, 182 JNIEnv* env,
181 const JavaParamRef<jobject>& caller, 183 const JavaParamRef<jobject>& caller,
182 const JavaParamRef<jstring>& address, 184 const JavaParamRef<jstring>& address,
183 const JavaParamRef<jobject>& 185 const JavaParamRef<jobject>&
184 bluetooth_device_wrapper, // Java Type: bluetoothDeviceWrapper 186 bluetooth_device_wrapper, // Java Type: bluetoothDeviceWrapper
185 const JavaParamRef<jobjectArray>& 187 const JavaParamRef<jobjectArray>&
186 advertised_uuids) { // Java Type: List<ParcelUuid> 188 advertised_uuids) { // Java Type: List<ParcelUuid>
187 std::string device_address = ConvertJavaStringToUTF8(env, address); 189 std::string device_address = ConvertJavaStringToUTF8(env, address);
188 DevicesMap::const_iterator iter = devices_.find(device_address); 190 DevicesMap::const_iterator iter = devices_.find(device_address);
189 191
192 bool is_new_device = false;
193 BluetoothDeviceAndroid* device_android;
Jeffrey Yasskin 2016/08/18 16:04:33 To track ownership more clearly, I might write thi
ortuno 2016/08/19 20:50:33 Done.
194
190 if (iter == devices_.end()) { 195 if (iter == devices_.end()) {
191 // New device. 196 // New device.
192 BluetoothDeviceAndroid* device_android = 197 is_new_device = true;
198 device_android =
193 BluetoothDeviceAndroid::Create(this, bluetooth_device_wrapper); 199 BluetoothDeviceAndroid::Create(this, bluetooth_device_wrapper);
194 device_android->UpdateAdvertisedUUIDs(advertised_uuids); 200 } else {
195 device_android->UpdateTimestamp(); 201 // Existing device.
202 device_android = static_cast<BluetoothDeviceAndroid*>(iter->second);
203 }
204 DCHECK(device_android);
205 std::vector<std::string> advertised_uuids_strings;
206 AppendJavaStringArrayToStringVector(env, advertised_uuids,
Jeffrey Yasskin 2016/08/18 16:04:32 You're passing a List<ParcelUuid> to a function th
ortuno 2016/08/19 20:50:33 Changed the comment to String[]. Done.
207 &advertised_uuids_strings);
208
209 device_android->UpdateAdvertisementData(std::move(advertised_uuids_strings),
Jeffrey Yasskin 2016/08/18 16:04:33 I'm skeptical of having a single function that upd
ortuno 2016/08/19 20:50:33 I like having a compile time check to make sure al
Jeffrey Yasskin 2016/08/19 22:08:09 'k.
210 {} /* service_data */);
211
212 if (is_new_device) {
196 devices_.add(device_address, 213 devices_.add(device_address,
197 std::unique_ptr<BluetoothDevice>(device_android)); 214 std::unique_ptr<BluetoothDevice>(device_android));
198 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 215 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
199 DeviceAdded(this, device_android)); 216 DeviceAdded(this, device_android));
200 } else { 217 } else {
201 // Existing device.
202 BluetoothDeviceAndroid* device_android =
203 static_cast<BluetoothDeviceAndroid*>(iter->second);
204 device_android->UpdateTimestamp();
205 device_android->UpdateAdvertisedUUIDs(advertised_uuids);
206
207 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 218 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
208 DeviceChanged(this, device_android)); 219 DeviceChanged(this, device_android));
209 } 220 }
210 } 221 }
211 222
212 BluetoothAdapterAndroid::BluetoothAdapterAndroid() : weak_ptr_factory_(this) { 223 BluetoothAdapterAndroid::BluetoothAdapterAndroid() : weak_ptr_factory_(this) {
213 } 224 }
214 225
215 BluetoothAdapterAndroid::~BluetoothAdapterAndroid() { 226 BluetoothAdapterAndroid::~BluetoothAdapterAndroid() {
216 Java_ChromeBluetoothAdapter_onBluetoothAdapterAndroidDestruction( 227 Java_ChromeBluetoothAdapter_onBluetoothAdapterAndroidDestruction(
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 if (num_discovery_sessions_ == 0) { 286 if (num_discovery_sessions_ == 0) {
276 VLOG(1) << "RemoveDiscoverySession: No scan in progress."; 287 VLOG(1) << "RemoveDiscoverySession: No scan in progress.";
277 NOTREACHED(); 288 NOTREACHED();
278 } else { 289 } else {
279 --num_discovery_sessions_; 290 --num_discovery_sessions_;
280 session_removed = true; 291 session_removed = true;
281 if (num_discovery_sessions_ == 0) { 292 if (num_discovery_sessions_ == 0) {
282 VLOG(1) << "RemoveDiscoverySession: Now 0 sessions. Stopping scan."; 293 VLOG(1) << "RemoveDiscoverySession: Now 0 sessions. Stopping scan.";
283 session_removed = Java_ChromeBluetoothAdapter_stopScan( 294 session_removed = Java_ChromeBluetoothAdapter_stopScan(
284 AttachCurrentThread(), j_adapter_); 295 AttachCurrentThread(), j_adapter_);
296 for (const auto& device_id_object_pair : devices_) {
297 device_id_object_pair.second->ClearAdvertisementData();
298 }
285 } else { 299 } else {
286 VLOG(1) << "RemoveDiscoverySession: Now " 300 VLOG(1) << "RemoveDiscoverySession: Now "
287 << unsigned(num_discovery_sessions_) << " sessions."; 301 << unsigned(num_discovery_sessions_) << " sessions.";
288 } 302 }
289 } 303 }
290 304
291 if (session_removed) { 305 if (session_removed) {
292 callback.Run(); 306 callback.Run();
293 } else { 307 } else {
294 // TODO(scheib): Eventually wire the SCAN_FAILED result through to here. 308 // TODO(scheib): Eventually wire the SCAN_FAILED result through to here.
295 error_callback.Run(UMABluetoothDiscoverySessionOutcome::UNKNOWN); 309 error_callback.Run(UMABluetoothDiscoverySessionOutcome::UNKNOWN);
296 } 310 }
297 } 311 }
298 312
299 void BluetoothAdapterAndroid::SetDiscoveryFilter( 313 void BluetoothAdapterAndroid::SetDiscoveryFilter(
300 std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter, 314 std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
301 const base::Closure& callback, 315 const base::Closure& callback,
302 const DiscoverySessionErrorCallback& error_callback) { 316 const DiscoverySessionErrorCallback& error_callback) {
303 // TODO(scheib): Support filters crbug.com/490401 317 // TODO(scheib): Support filters crbug.com/490401
304 NOTIMPLEMENTED(); 318 NOTIMPLEMENTED();
305 error_callback.Run(UMABluetoothDiscoverySessionOutcome::NOT_IMPLEMENTED); 319 error_callback.Run(UMABluetoothDiscoverySessionOutcome::NOT_IMPLEMENTED);
306 } 320 }
307 321
308 void BluetoothAdapterAndroid::RemovePairingDelegateInternal( 322 void BluetoothAdapterAndroid::RemovePairingDelegateInternal(
309 device::BluetoothDevice::PairingDelegate* pairing_delegate) { 323 device::BluetoothDevice::PairingDelegate* pairing_delegate) {
310 } 324 }
311 325
312 } // namespace device 326 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698