OLD | NEW |
---|---|
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_string.h" | 10 #include "base/android/jni_string.h" |
11 #include "base/bind.h" | |
12 #include "base/location.h" | |
11 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
12 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
13 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
14 #include "device/bluetooth/android/wrappers.h" | 16 #include "device/bluetooth/android/wrappers.h" |
15 #include "device/bluetooth/bluetooth_advertisement.h" | 17 #include "device/bluetooth/bluetooth_advertisement.h" |
16 #include "device/bluetooth/bluetooth_device_android.h" | 18 #include "device/bluetooth/bluetooth_device_android.h" |
17 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" | 19 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" |
18 #include "jni/ChromeBluetoothAdapter_jni.h" | 20 #include "jni/ChromeBluetoothAdapter_jni.h" |
19 | 21 |
20 using base::android::AttachCurrentThread; | 22 using base::android::AttachCurrentThread; |
(...skipping 10 matching lines...) Expand all Loading... | |
31 | 33 |
32 // static | 34 // static |
33 base::WeakPtr<BluetoothAdapterAndroid> BluetoothAdapterAndroid::Create( | 35 base::WeakPtr<BluetoothAdapterAndroid> BluetoothAdapterAndroid::Create( |
34 jobject bluetooth_adapter_wrapper) { // Java Type: bluetoothAdapterWrapper | 36 jobject bluetooth_adapter_wrapper) { // Java Type: bluetoothAdapterWrapper |
35 BluetoothAdapterAndroid* adapter = new BluetoothAdapterAndroid(); | 37 BluetoothAdapterAndroid* adapter = new BluetoothAdapterAndroid(); |
36 | 38 |
37 adapter->j_adapter_.Reset(Java_ChromeBluetoothAdapter_create( | 39 adapter->j_adapter_.Reset(Java_ChromeBluetoothAdapter_create( |
38 AttachCurrentThread(), reinterpret_cast<intptr_t>(adapter), | 40 AttachCurrentThread(), reinterpret_cast<intptr_t>(adapter), |
39 bluetooth_adapter_wrapper)); | 41 bluetooth_adapter_wrapper)); |
40 | 42 |
43 adapter->ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
44 adapter->PurgeTimedOutDevices(); | |
ortuno
2016/06/14 15:29:33
I don't think we want to be purging devices all th
perja
2016/06/15 13:03:40
Acknowledged.
| |
45 | |
41 return adapter->weak_ptr_factory_.GetWeakPtr(); | 46 return adapter->weak_ptr_factory_.GetWeakPtr(); |
42 } | 47 } |
43 | 48 |
44 // static | 49 // static |
45 bool BluetoothAdapterAndroid::RegisterJNI(JNIEnv* env) { | 50 bool BluetoothAdapterAndroid::RegisterJNI(JNIEnv* env) { |
46 return RegisterNativesImpl(env); // Generated in BluetoothAdapter_jni.h | 51 return RegisterNativesImpl(env); // Generated in BluetoothAdapter_jni.h |
47 } | 52 } |
48 | 53 |
49 std::string BluetoothAdapterAndroid::GetAddress() const { | 54 std::string BluetoothAdapterAndroid::GetAddress() const { |
50 return ConvertJavaStringToUTF8(Java_ChromeBluetoothAdapter_getAddress( | 55 return ConvertJavaStringToUTF8(Java_ChromeBluetoothAdapter_getAddress( |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 const JavaParamRef<jobject>& | 173 const JavaParamRef<jobject>& |
169 advertised_uuids) { // Java Type: List<ParcelUuid> | 174 advertised_uuids) { // Java Type: List<ParcelUuid> |
170 std::string device_address = ConvertJavaStringToUTF8(env, address); | 175 std::string device_address = ConvertJavaStringToUTF8(env, address); |
171 DevicesMap::const_iterator iter = devices_.find(device_address); | 176 DevicesMap::const_iterator iter = devices_.find(device_address); |
172 | 177 |
173 if (iter == devices_.end()) { | 178 if (iter == devices_.end()) { |
174 // New device. | 179 // New device. |
175 BluetoothDeviceAndroid* device_android = | 180 BluetoothDeviceAndroid* device_android = |
176 BluetoothDeviceAndroid::Create(this, bluetooth_device_wrapper); | 181 BluetoothDeviceAndroid::Create(this, bluetooth_device_wrapper); |
177 device_android->UpdateAdvertisedUUIDs(advertised_uuids); | 182 device_android->UpdateAdvertisedUUIDs(advertised_uuids); |
183 device_android->UpdateTimestamp(); | |
178 devices_.add(device_address, | 184 devices_.add(device_address, |
179 std::unique_ptr<BluetoothDevice>(device_android)); | 185 std::unique_ptr<BluetoothDevice>(device_android)); |
180 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 186 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
181 DeviceAdded(this, device_android)); | 187 DeviceAdded(this, device_android)); |
182 } else { | 188 } else { |
183 // Existing device. | 189 // Existing device. |
184 BluetoothDeviceAndroid* device_android = | 190 BluetoothDeviceAndroid* device_android = |
185 static_cast<BluetoothDeviceAndroid*>(iter->second); | 191 static_cast<BluetoothDeviceAndroid*>(iter->second); |
192 device_android->UpdateTimestamp(); | |
186 if (device_android->UpdateAdvertisedUUIDs(advertised_uuids)) { | 193 if (device_android->UpdateAdvertisedUUIDs(advertised_uuids)) { |
187 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 194 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
188 DeviceChanged(this, device_android)); | 195 DeviceChanged(this, device_android)); |
189 } | 196 } |
190 } | 197 } |
191 } | 198 } |
192 | 199 |
193 BluetoothAdapterAndroid::BluetoothAdapterAndroid() : weak_ptr_factory_(this) { | 200 BluetoothAdapterAndroid::BluetoothAdapterAndroid() : weak_ptr_factory_(this) { |
194 } | 201 } |
195 | 202 |
196 BluetoothAdapterAndroid::~BluetoothAdapterAndroid() { | 203 BluetoothAdapterAndroid::~BluetoothAdapterAndroid() { |
197 Java_ChromeBluetoothAdapter_onBluetoothAdapterAndroidDestruction( | 204 Java_ChromeBluetoothAdapter_onBluetoothAdapterAndroidDestruction( |
198 AttachCurrentThread(), j_adapter_.obj()); | 205 AttachCurrentThread(), j_adapter_.obj()); |
199 } | 206 } |
200 | 207 |
208 void BluetoothAdapterAndroid::PurgeTimedOutDevices() { | |
209 RemoveTimedOutDevices(); | |
210 int pollIntervalMs = 11000; | |
ortuno
2016/06/14 15:29:33
Make these enums:
namespace {
// TODO add a com
perja
2016/06/15 13:03:40
Acknowledged.
| |
211 if (IsDiscovering()) { | |
212 pollIntervalMs = 1000; | |
213 } | |
214 ui_task_runner_->PostDelayedTask( | |
ortuno
2016/06/14 15:29:33
I think what Vince was suggesting was that we run
perja
2016/06/15 13:03:40
Acknowledged.
| |
215 FROM_HERE, base::Bind(&BluetoothAdapterAndroid::PurgeTimedOutDevices, | |
216 weak_ptr_factory_.GetWeakPtr()), | |
217 base::TimeDelta::FromMilliseconds(pollIntervalMs)); | |
218 } | |
219 | |
201 void BluetoothAdapterAndroid::AddDiscoverySession( | 220 void BluetoothAdapterAndroid::AddDiscoverySession( |
202 BluetoothDiscoveryFilter* discovery_filter, | 221 BluetoothDiscoveryFilter* discovery_filter, |
203 const base::Closure& callback, | 222 const base::Closure& callback, |
204 const DiscoverySessionErrorCallback& error_callback) { | 223 const DiscoverySessionErrorCallback& error_callback) { |
205 // TODO(scheib): Support filters crbug.com/490401 | 224 // TODO(scheib): Support filters crbug.com/490401 |
206 if (Java_ChromeBluetoothAdapter_addDiscoverySession(AttachCurrentThread(), | 225 if (Java_ChromeBluetoothAdapter_addDiscoverySession(AttachCurrentThread(), |
207 j_adapter_.obj())) { | 226 j_adapter_.obj())) { |
208 callback.Run(); | 227 callback.Run(); |
209 } else { | 228 } else { |
210 // TODO(scheib): Eventually wire the SCAN_FAILED result through to here. | 229 // TODO(scheib): Eventually wire the SCAN_FAILED result through to here. |
(...skipping 21 matching lines...) Expand all Loading... | |
232 // TODO(scheib): Support filters crbug.com/490401 | 251 // TODO(scheib): Support filters crbug.com/490401 |
233 NOTIMPLEMENTED(); | 252 NOTIMPLEMENTED(); |
234 error_callback.Run(UMABluetoothDiscoverySessionOutcome::NOT_IMPLEMENTED); | 253 error_callback.Run(UMABluetoothDiscoverySessionOutcome::NOT_IMPLEMENTED); |
235 } | 254 } |
236 | 255 |
237 void BluetoothAdapterAndroid::RemovePairingDelegateInternal( | 256 void BluetoothAdapterAndroid::RemovePairingDelegateInternal( |
238 device::BluetoothDevice::PairingDelegate* pairing_delegate) { | 257 device::BluetoothDevice::PairingDelegate* pairing_delegate) { |
239 } | 258 } |
240 | 259 |
241 } // namespace device | 260 } // namespace device |
OLD | NEW |