| OLD | NEW |
| 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 "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_event
_router.h" | 5 #include "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_event
_router.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 const base::Closure& callback, | 295 const base::Closure& callback, |
| 296 const ErrorCallback& error_callback) { | 296 const ErrorCallback& error_callback) { |
| 297 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 297 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 298 if (!adapter_.get()) { | 298 if (!adapter_.get()) { |
| 299 VLOG(1) << "BluetoothAdapter not ready."; | 299 VLOG(1) << "BluetoothAdapter not ready."; |
| 300 error_callback.Run(kStatusErrorFailed); | 300 error_callback.Run(kStatusErrorFailed); |
| 301 return; | 301 return; |
| 302 } | 302 } |
| 303 | 303 |
| 304 const std::string extension_id = extension->id(); | 304 const std::string extension_id = extension->id(); |
| 305 const std::string disconnect_id = extension_id + device_address; | |
| 306 | |
| 307 if (disconnecting_devices_.count(disconnect_id) != 0) { | |
| 308 error_callback.Run(kStatusErrorInProgress); | |
| 309 return; | |
| 310 } | |
| 311 | 305 |
| 312 BluetoothLowEnergyConnection* conn = | 306 BluetoothLowEnergyConnection* conn = |
| 313 FindConnection(extension_id, device_address); | 307 FindConnection(extension_id, device_address); |
| 314 if (!conn || !conn->GetConnection()->IsConnected()) { | 308 if (!conn || !conn->GetConnection()->IsConnected()) { |
| 315 VLOG(1) << "Application not connected to device: " << device_address; | 309 VLOG(1) << "Application not connected to device: " << device_address; |
| 316 error_callback.Run(kStatusErrorNotConnected); | 310 error_callback.Run(kStatusErrorNotConnected); |
| 317 return; | 311 return; |
| 318 } | 312 } |
| 319 | 313 |
| 320 disconnecting_devices_.insert(disconnect_id); | 314 conn->GetConnection()->Disconnect(); |
| 321 conn->GetConnection()->Disconnect( | 315 VLOG(2) << "GATT connection terminated."; |
| 322 base::Bind(&BluetoothLowEnergyEventRouter::OnDisconnect, | 316 |
| 323 weak_ptr_factory_.GetWeakPtr(), | 317 if (!RemoveConnection(extension_id, device_address)) { |
| 324 extension_id, | 318 VLOG(1) << "The connection was removed before disconnect completed, id: " |
| 325 device_address, | 319 << extension_id << ", device: " << device_address; |
| 326 callback)); | 320 } |
| 321 |
| 322 callback.Run(); |
| 327 } | 323 } |
| 328 | 324 |
| 329 bool BluetoothLowEnergyEventRouter::GetServices( | 325 bool BluetoothLowEnergyEventRouter::GetServices( |
| 330 const std::string& device_address, | 326 const std::string& device_address, |
| 331 ServiceList* out_services) const { | 327 ServiceList* out_services) const { |
| 332 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 328 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 333 DCHECK(out_services); | 329 DCHECK(out_services); |
| 334 if (!adapter_.get()) { | 330 if (!adapter_.get()) { |
| 335 VLOG(1) << "BluetoothAdapter not ready."; | 331 VLOG(1) << "BluetoothAdapter not ready."; |
| 336 return false; | 332 return false; |
| (...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1274 BluetoothLowEnergyConnection* conn = new BluetoothLowEnergyConnection( | 1270 BluetoothLowEnergyConnection* conn = new BluetoothLowEnergyConnection( |
| 1275 persistent, extension_id, connection.Pass()); | 1271 persistent, extension_id, connection.Pass()); |
| 1276 ConnectionResourceManager* manager = | 1272 ConnectionResourceManager* manager = |
| 1277 GetConnectionResourceManager(browser_context_); | 1273 GetConnectionResourceManager(browser_context_); |
| 1278 manager->Add(conn); | 1274 manager->Add(conn); |
| 1279 | 1275 |
| 1280 connecting_devices_.erase(connect_id); | 1276 connecting_devices_.erase(connect_id); |
| 1281 callback.Run(); | 1277 callback.Run(); |
| 1282 } | 1278 } |
| 1283 | 1279 |
| 1284 void BluetoothLowEnergyEventRouter::OnDisconnect( | |
| 1285 const std::string& extension_id, | |
| 1286 const std::string& device_address, | |
| 1287 const base::Closure& callback) { | |
| 1288 VLOG(2) << "GATT connection terminated."; | |
| 1289 | |
| 1290 const std::string disconnect_id = extension_id + device_address; | |
| 1291 DCHECK_NE(0U, disconnecting_devices_.count(disconnect_id)); | |
| 1292 | |
| 1293 if (!RemoveConnection(extension_id, device_address)) { | |
| 1294 VLOG(1) << "The connection was removed before disconnect completed, id: " | |
| 1295 << extension_id << ", device: " << device_address; | |
| 1296 } | |
| 1297 | |
| 1298 disconnecting_devices_.erase(disconnect_id); | |
| 1299 callback.Run(); | |
| 1300 } | |
| 1301 | |
| 1302 void BluetoothLowEnergyEventRouter::OnError( | 1280 void BluetoothLowEnergyEventRouter::OnError( |
| 1303 const ErrorCallback& error_callback, | 1281 const ErrorCallback& error_callback, |
| 1304 BluetoothGattService::GattErrorCode error_code) { | 1282 BluetoothGattService::GattErrorCode error_code) { |
| 1305 VLOG(2) << "Remote characteristic/descriptor value read/write failed."; | 1283 VLOG(2) << "Remote characteristic/descriptor value read/write failed."; |
| 1306 | 1284 |
| 1307 error_callback.Run(GattErrorToRouterError(error_code)); | 1285 error_callback.Run(GattErrorToRouterError(error_code)); |
| 1308 } | 1286 } |
| 1309 | 1287 |
| 1310 void BluetoothLowEnergyEventRouter::OnConnectError( | 1288 void BluetoothLowEnergyEventRouter::OnConnectError( |
| 1311 const std::string& extension_id, | 1289 const std::string& extension_id, |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1491 continue; | 1469 continue; |
| 1492 | 1470 |
| 1493 manager->Remove(extension_id, *iter); | 1471 manager->Remove(extension_id, *iter); |
| 1494 return true; | 1472 return true; |
| 1495 } | 1473 } |
| 1496 | 1474 |
| 1497 return false; | 1475 return false; |
| 1498 } | 1476 } |
| 1499 | 1477 |
| 1500 } // namespace extensions | 1478 } // namespace extensions |
| OLD | NEW |