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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 NotifySessionResourceManager* manager = | 144 NotifySessionResourceManager* manager = |
145 NotifySessionResourceManager::Get(context); | 145 NotifySessionResourceManager::Get(context); |
146 DCHECK(manager) | 146 DCHECK(manager) |
147 << "There is no Bluetooth low energy value update session manager." | 147 << "There is no Bluetooth low energy value update session manager." |
148 "If this assertion is failing during a test, then it is likely that " | 148 "If this assertion is failing during a test, then it is likely that " |
149 "TestExtensionSystem is failing to provide an instance of " | 149 "TestExtensionSystem is failing to provide an instance of " |
150 "ApiResourceManager<BluetoothLowEnergyNotifySession>."; | 150 "ApiResourceManager<BluetoothLowEnergyNotifySession>."; |
151 return manager; | 151 return manager; |
152 } | 152 } |
153 | 153 |
| 154 // Translates GattErrorCodes to RouterError Codes |
| 155 extensions::BluetoothLowEnergyEventRouter::Status GattErrorToRouterError( |
| 156 BluetoothGattService::GattErrorCode error_code) { |
| 157 extensions::BluetoothLowEnergyEventRouter::Status error_status = |
| 158 extensions::BluetoothLowEnergyEventRouter::kStatusErrorFailed; |
| 159 if (error_code == BluetoothGattService::GATT_ERROR_IN_PROGRESS) { |
| 160 error_status = |
| 161 extensions::BluetoothLowEnergyEventRouter::kStatusErrorInProgress; |
| 162 } else if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) { |
| 163 error_status = |
| 164 extensions::BluetoothLowEnergyEventRouter::kStatusErrorInvalidLength; |
| 165 } else if (error_code == BluetoothGattService::GATT_ERROR_NOT_PERMITTED) { |
| 166 error_status = |
| 167 extensions::BluetoothLowEnergyEventRouter::kStatusErrorPermissionDenied; |
| 168 } else if (error_code == BluetoothGattService::GATT_ERROR_NOT_AUTHORIZED) { |
| 169 error_status = |
| 170 extensions::BluetoothLowEnergyEventRouter::kStatusErrorPermissionDenied; |
| 171 } else if (error_code == BluetoothGattService::GATT_ERROR_NOT_PAIRED) { |
| 172 error_status = |
| 173 extensions::BluetoothLowEnergyEventRouter::kStatusErrorNotConnected; |
| 174 } else if (error_code == BluetoothGattService::GATT_ERROR_NOT_SUPPORTED) { |
| 175 error_status = |
| 176 extensions::BluetoothLowEnergyEventRouter::kStatusErrorGattNotSupported; |
| 177 } |
| 178 |
| 179 return error_status; |
| 180 } |
| 181 |
154 } // namespace | 182 } // namespace |
155 | 183 |
156 namespace extensions { | 184 namespace extensions { |
157 | 185 |
158 BluetoothLowEnergyEventRouter::BluetoothLowEnergyEventRouter( | 186 BluetoothLowEnergyEventRouter::BluetoothLowEnergyEventRouter( |
159 content::BrowserContext* context) | 187 content::BrowserContext* context) |
160 : adapter_(NULL), browser_context_(context), weak_ptr_factory_(this) { | 188 : adapter_(NULL), browser_context_(context), weak_ptr_factory_(this) { |
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
162 DCHECK(browser_context_); | 190 DCHECK(browser_context_); |
163 VLOG(1) << "Initializing BluetoothLowEnergyEventRouter."; | 191 VLOG(1) << "Initializing BluetoothLowEnergyEventRouter."; |
(...skipping 1099 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1263 << extension_id << ", device: " << device_address; | 1291 << extension_id << ", device: " << device_address; |
1264 } | 1292 } |
1265 | 1293 |
1266 disconnecting_devices_.erase(disconnect_id); | 1294 disconnecting_devices_.erase(disconnect_id); |
1267 callback.Run(); | 1295 callback.Run(); |
1268 } | 1296 } |
1269 | 1297 |
1270 void BluetoothLowEnergyEventRouter::OnError( | 1298 void BluetoothLowEnergyEventRouter::OnError( |
1271 const ErrorCallback& error_callback, | 1299 const ErrorCallback& error_callback, |
1272 BluetoothGattService::GattErrorCode error_code) { | 1300 BluetoothGattService::GattErrorCode error_code) { |
1273 // TODO(jamuraa): do something with |error_code| (crbug.com/277232) | |
1274 VLOG(2) << "Remote characteristic/descriptor value read/write failed."; | 1301 VLOG(2) << "Remote characteristic/descriptor value read/write failed."; |
1275 error_callback.Run(kStatusErrorFailed); | 1302 |
| 1303 error_callback.Run(GattErrorToRouterError(error_code)); |
1276 } | 1304 } |
1277 | 1305 |
1278 void BluetoothLowEnergyEventRouter::OnConnectError( | 1306 void BluetoothLowEnergyEventRouter::OnConnectError( |
1279 const std::string& extension_id, | 1307 const std::string& extension_id, |
1280 const std::string& device_address, | 1308 const std::string& device_address, |
1281 const ErrorCallback& error_callback, | 1309 const ErrorCallback& error_callback, |
1282 BluetoothDevice::ConnectErrorCode error_code) { | 1310 BluetoothDevice::ConnectErrorCode error_code) { |
1283 // TODO(jamuraa): do something with |error_code| (crbug.com/277232) | |
1284 VLOG(2) << "Failed to create GATT connection: " << error_code; | 1311 VLOG(2) << "Failed to create GATT connection: " << error_code; |
1285 | 1312 |
1286 const std::string connect_id = extension_id + device_address; | 1313 const std::string connect_id = extension_id + device_address; |
1287 DCHECK_NE(0U, connecting_devices_.count(connect_id)); | 1314 DCHECK_NE(0U, connecting_devices_.count(connect_id)); |
1288 | 1315 |
1289 connecting_devices_.erase(connect_id); | 1316 connecting_devices_.erase(connect_id); |
1290 error_callback.Run(kStatusErrorFailed); | 1317 Status error_status = kStatusErrorFailed; |
| 1318 if (error_code == BluetoothDevice::ERROR_INPROGRESS) { |
| 1319 error_status = kStatusErrorInProgress; |
| 1320 } else if (error_code == BluetoothDevice::ERROR_AUTH_FAILED || |
| 1321 error_code == BluetoothDevice::ERROR_AUTH_REJECTED) { |
| 1322 error_status = kStatusErrorAuthenticationFailed; |
| 1323 } else if (error_code == BluetoothDevice::ERROR_AUTH_CANCELED) { |
| 1324 error_status = kStatusErrorCanceled; |
| 1325 } else if (error_code == BluetoothDevice::ERROR_AUTH_TIMEOUT) { |
| 1326 error_status = kStatusErrorTimeout; |
| 1327 } else if (error_code == BluetoothDevice::ERROR_UNSUPPORTED_DEVICE) { |
| 1328 error_status = kStatusErrorUnsupportedDevice; |
| 1329 } |
| 1330 // ERROR_UNKNOWN and ERROR_FAILED defaulted to kStatusErrorFailed |
| 1331 |
| 1332 error_callback.Run(error_status); |
1291 } | 1333 } |
1292 | 1334 |
1293 void BluetoothLowEnergyEventRouter::OnStartNotifySession( | 1335 void BluetoothLowEnergyEventRouter::OnStartNotifySession( |
1294 bool persistent, | 1336 bool persistent, |
1295 const std::string& extension_id, | 1337 const std::string& extension_id, |
1296 const std::string& characteristic_id, | 1338 const std::string& characteristic_id, |
1297 const base::Closure& callback, | 1339 const base::Closure& callback, |
1298 scoped_ptr<device::BluetoothGattNotifySession> session) { | 1340 scoped_ptr<device::BluetoothGattNotifySession> session) { |
1299 VLOG(2) << "Value update session created for characteristic: " | 1341 VLOG(2) << "Value update session created for characteristic: " |
1300 << characteristic_id; | 1342 << characteristic_id; |
(...skipping 14 matching lines...) Expand all Loading... |
1315 | 1357 |
1316 pending_session_calls_.erase(session_id); | 1358 pending_session_calls_.erase(session_id); |
1317 callback.Run(); | 1359 callback.Run(); |
1318 } | 1360 } |
1319 | 1361 |
1320 void BluetoothLowEnergyEventRouter::OnStartNotifySessionError( | 1362 void BluetoothLowEnergyEventRouter::OnStartNotifySessionError( |
1321 const std::string& extension_id, | 1363 const std::string& extension_id, |
1322 const std::string& characteristic_id, | 1364 const std::string& characteristic_id, |
1323 const ErrorCallback& error_callback, | 1365 const ErrorCallback& error_callback, |
1324 device::BluetoothGattService::GattErrorCode error_code) { | 1366 device::BluetoothGattService::GattErrorCode error_code) { |
1325 // TODO(jamuraa): do something with |error_code| (crbug.com/277232) | |
1326 VLOG(2) << "Failed to create value update session for characteristic: " | 1367 VLOG(2) << "Failed to create value update session for characteristic: " |
1327 << characteristic_id; | 1368 << characteristic_id; |
1328 | 1369 |
1329 const std::string session_id = extension_id + characteristic_id; | 1370 const std::string session_id = extension_id + characteristic_id; |
1330 DCHECK_NE(0U, pending_session_calls_.count(session_id)); | 1371 DCHECK_NE(0U, pending_session_calls_.count(session_id)); |
1331 | 1372 |
1332 pending_session_calls_.erase(session_id); | 1373 pending_session_calls_.erase(session_id); |
1333 error_callback.Run(kStatusErrorFailed); | 1374 error_callback.Run(GattErrorToRouterError(error_code)); |
1334 } | 1375 } |
1335 | 1376 |
1336 void BluetoothLowEnergyEventRouter::OnStopNotifySession( | 1377 void BluetoothLowEnergyEventRouter::OnStopNotifySession( |
1337 const std::string& extension_id, | 1378 const std::string& extension_id, |
1338 const std::string& characteristic_id, | 1379 const std::string& characteristic_id, |
1339 const base::Closure& callback) { | 1380 const base::Closure& callback) { |
1340 VLOG(2) << "Value update session terminated."; | 1381 VLOG(2) << "Value update session terminated."; |
1341 | 1382 |
1342 if (!RemoveNotifySession(extension_id, characteristic_id)) { | 1383 if (!RemoveNotifySession(extension_id, characteristic_id)) { |
1343 VLOG(1) << "The value update session was removed before Stop completed, " | 1384 VLOG(1) << "The value update session was removed before Stop completed, " |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1446 continue; | 1487 continue; |
1447 | 1488 |
1448 manager->Remove(extension_id, *iter); | 1489 manager->Remove(extension_id, *iter); |
1449 return true; | 1490 return true; |
1450 } | 1491 } |
1451 | 1492 |
1452 return false; | 1493 return false; |
1453 } | 1494 } |
1454 | 1495 |
1455 } // namespace extensions | 1496 } // namespace extensions |
OLD | NEW |