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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 "ApiResourceManager<BluetoothLowEnergyNotifySession>."; | 150 "ApiResourceManager<BluetoothLowEnergyNotifySession>."; |
151 return manager; | 151 return manager; |
152 } | 152 } |
153 | 153 |
154 } // namespace | 154 } // namespace |
155 | 155 |
156 namespace extensions { | 156 namespace extensions { |
157 | 157 |
158 BluetoothLowEnergyEventRouter::BluetoothLowEnergyEventRouter( | 158 BluetoothLowEnergyEventRouter::BluetoothLowEnergyEventRouter( |
159 content::BrowserContext* context) | 159 content::BrowserContext* context) |
160 : adapter_(NULL), browser_context_(context), weak_ptr_factory_(this) { | 160 : adapter_(nullptr), browser_context_(context), weak_ptr_factory_(this) { |
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
162 DCHECK(browser_context_); | 162 DCHECK(browser_context_); |
163 VLOG(1) << "Initializing BluetoothLowEnergyEventRouter."; | 163 VLOG(1) << "Initializing BluetoothLowEnergyEventRouter."; |
164 | 164 |
165 if (!IsBluetoothSupported()) { | 165 if (!IsBluetoothSupported()) { |
166 VLOG(1) << "Bluetooth not supported on the current platform."; | 166 VLOG(1) << "Bluetooth not supported on the current platform."; |
167 return; | 167 return; |
168 } | 168 } |
169 } | 169 } |
170 | 170 |
171 BluetoothLowEnergyEventRouter::~BluetoothLowEnergyEventRouter() { | 171 BluetoothLowEnergyEventRouter::~BluetoothLowEnergyEventRouter() { |
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
173 if (!adapter_.get()) | 173 if (!adapter_.get()) |
174 return; | 174 return; |
175 | 175 |
176 adapter_->RemoveObserver(this); | 176 adapter_->RemoveObserver(this); |
177 adapter_ = NULL; | 177 adapter_ = nullptr; |
178 } | 178 } |
179 | 179 |
180 bool BluetoothLowEnergyEventRouter::IsBluetoothSupported() const { | 180 bool BluetoothLowEnergyEventRouter::IsBluetoothSupported() const { |
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
182 return adapter_.get() || | 182 return adapter_.get() || |
183 BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); | 183 BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); |
184 } | 184 } |
185 | 185 |
186 bool BluetoothLowEnergyEventRouter::InitializeAdapterAndInvokeCallback( | 186 bool BluetoothLowEnergyEventRouter::InitializeAdapterAndInvokeCallback( |
187 const base::Closure& callback) { | 187 const base::Closure& callback) { |
188 if (!IsBluetoothSupported()) | 188 if (!IsBluetoothSupported()) |
189 return false; | 189 return false; |
190 | 190 |
191 if (adapter_.get()) { | 191 if (adapter_.get()) { |
192 callback.Run(); | 192 callback.Run(); |
193 return true; | 193 return true; |
194 } | 194 } |
195 | 195 |
196 BluetoothAdapterFactory::GetAdapter( | 196 BluetoothAdapterFactory::GetAdapter( |
197 base::Bind(&BluetoothLowEnergyEventRouter::OnGetAdapter, | 197 base::Bind(&BluetoothLowEnergyEventRouter::OnGetAdapter, |
198 weak_ptr_factory_.GetWeakPtr(), | 198 weak_ptr_factory_.GetWeakPtr(), |
199 callback)); | 199 callback)); |
200 return true; | 200 return true; |
201 } | 201 } |
202 | 202 |
203 bool BluetoothLowEnergyEventRouter::HasAdapter() const { | 203 bool BluetoothLowEnergyEventRouter::HasAdapter() const { |
204 return (adapter_.get() != NULL); | 204 return (adapter_.get() != nullptr); |
205 } | 205 } |
206 | 206 |
207 void BluetoothLowEnergyEventRouter::Connect( | 207 void BluetoothLowEnergyEventRouter::Connect( |
208 bool persistent, | 208 bool persistent, |
209 const Extension* extension, | 209 const Extension* extension, |
210 const std::string& device_address, | 210 const std::string& device_address, |
211 const base::Closure& callback, | 211 const base::Closure& callback, |
212 const ErrorCallback& error_callback) { | 212 const ErrorCallback& error_callback) { |
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
214 if (!adapter_.get()) { | 214 if (!adapter_.get()) { |
(...skipping 921 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1136 extension_id, event.Pass()); | 1136 extension_id, event.Pass()); |
1137 } | 1137 } |
1138 } | 1138 } |
1139 | 1139 |
1140 BluetoothGattService* BluetoothLowEnergyEventRouter::FindServiceById( | 1140 BluetoothGattService* BluetoothLowEnergyEventRouter::FindServiceById( |
1141 const std::string& instance_id) const { | 1141 const std::string& instance_id) const { |
1142 InstanceIdMap::const_iterator iter = | 1142 InstanceIdMap::const_iterator iter = |
1143 service_id_to_device_address_.find(instance_id); | 1143 service_id_to_device_address_.find(instance_id); |
1144 if (iter == service_id_to_device_address_.end()) { | 1144 if (iter == service_id_to_device_address_.end()) { |
1145 VLOG(1) << "GATT service identifier unknown: " << instance_id; | 1145 VLOG(1) << "GATT service identifier unknown: " << instance_id; |
1146 return NULL; | 1146 return nullptr; |
1147 } | 1147 } |
1148 | 1148 |
1149 const std::string& address = iter->second; | 1149 const std::string& address = iter->second; |
1150 | 1150 |
1151 BluetoothDevice* device = adapter_->GetDevice(address); | 1151 BluetoothDevice* device = adapter_->GetDevice(address); |
1152 if (!device) { | 1152 if (!device) { |
1153 VLOG(1) << "Bluetooth device not found: " << address; | 1153 VLOG(1) << "Bluetooth device not found: " << address; |
1154 return NULL; | 1154 return nullptr; |
1155 } | 1155 } |
1156 | 1156 |
1157 BluetoothGattService* service = device->GetGattService(instance_id); | 1157 BluetoothGattService* service = device->GetGattService(instance_id); |
1158 if (!service) { | 1158 if (!service) { |
1159 VLOG(1) << "GATT service with ID \"" << instance_id | 1159 VLOG(1) << "GATT service with ID \"" << instance_id |
1160 << "\" not found on device \"" << address << "\""; | 1160 << "\" not found on device \"" << address << "\""; |
1161 return NULL; | 1161 return nullptr; |
1162 } | 1162 } |
1163 | 1163 |
1164 return service; | 1164 return service; |
1165 } | 1165 } |
1166 | 1166 |
1167 BluetoothGattCharacteristic* | 1167 BluetoothGattCharacteristic* |
1168 BluetoothLowEnergyEventRouter::FindCharacteristicById( | 1168 BluetoothLowEnergyEventRouter::FindCharacteristicById( |
1169 const std::string& instance_id) const { | 1169 const std::string& instance_id) const { |
1170 InstanceIdMap::const_iterator iter = chrc_id_to_service_id_.find(instance_id); | 1170 InstanceIdMap::const_iterator iter = chrc_id_to_service_id_.find(instance_id); |
1171 if (iter == chrc_id_to_service_id_.end()) { | 1171 if (iter == chrc_id_to_service_id_.end()) { |
1172 VLOG(1) << "GATT characteristic identifier unknown: " << instance_id; | 1172 VLOG(1) << "GATT characteristic identifier unknown: " << instance_id; |
1173 return NULL; | 1173 return nullptr; |
1174 } | 1174 } |
1175 | 1175 |
1176 const std::string& service_id = iter->second; | 1176 const std::string& service_id = iter->second; |
1177 | 1177 |
1178 BluetoothGattService* service = FindServiceById(service_id); | 1178 BluetoothGattService* service = FindServiceById(service_id); |
1179 if (!service) { | 1179 if (!service) { |
1180 VLOG(1) << "Failed to obtain service for characteristic: " << instance_id; | 1180 VLOG(1) << "Failed to obtain service for characteristic: " << instance_id; |
1181 return NULL; | 1181 return nullptr; |
1182 } | 1182 } |
1183 | 1183 |
1184 BluetoothGattCharacteristic* characteristic = | 1184 BluetoothGattCharacteristic* characteristic = |
1185 service->GetCharacteristic(instance_id); | 1185 service->GetCharacteristic(instance_id); |
1186 if (!characteristic) { | 1186 if (!characteristic) { |
1187 VLOG(1) << "GATT characteristic with ID \"" << instance_id | 1187 VLOG(1) << "GATT characteristic with ID \"" << instance_id |
1188 << "\" not found on service \"" << service_id << "\""; | 1188 << "\" not found on service \"" << service_id << "\""; |
1189 return NULL; | 1189 return nullptr; |
1190 } | 1190 } |
1191 | 1191 |
1192 return characteristic; | 1192 return characteristic; |
1193 } | 1193 } |
1194 | 1194 |
1195 BluetoothGattDescriptor* BluetoothLowEnergyEventRouter::FindDescriptorById( | 1195 BluetoothGattDescriptor* BluetoothLowEnergyEventRouter::FindDescriptorById( |
1196 const std::string& instance_id) const { | 1196 const std::string& instance_id) const { |
1197 InstanceIdMap::const_iterator iter = desc_id_to_chrc_id_.find(instance_id); | 1197 InstanceIdMap::const_iterator iter = desc_id_to_chrc_id_.find(instance_id); |
1198 if (iter == desc_id_to_chrc_id_.end()) { | 1198 if (iter == desc_id_to_chrc_id_.end()) { |
1199 VLOG(1) << "GATT descriptor identifier unknown: " << instance_id; | 1199 VLOG(1) << "GATT descriptor identifier unknown: " << instance_id; |
1200 return NULL; | 1200 return nullptr; |
1201 } | 1201 } |
1202 | 1202 |
1203 const std::string& chrc_id = iter->second; | 1203 const std::string& chrc_id = iter->second; |
1204 BluetoothGattCharacteristic* chrc = FindCharacteristicById(chrc_id); | 1204 BluetoothGattCharacteristic* chrc = FindCharacteristicById(chrc_id); |
1205 if (!chrc) { | 1205 if (!chrc) { |
1206 VLOG(1) << "Failed to obtain characteristic for descriptor: " | 1206 VLOG(1) << "Failed to obtain characteristic for descriptor: " |
1207 << instance_id; | 1207 << instance_id; |
1208 return NULL; | 1208 return nullptr; |
1209 } | 1209 } |
1210 | 1210 |
1211 BluetoothGattDescriptor* descriptor = chrc->GetDescriptor(instance_id); | 1211 BluetoothGattDescriptor* descriptor = chrc->GetDescriptor(instance_id); |
1212 if (!descriptor) { | 1212 if (!descriptor) { |
1213 VLOG(1) << "GATT descriptor with ID \"" << instance_id | 1213 VLOG(1) << "GATT descriptor with ID \"" << instance_id |
1214 << "\" not found on characteristic \"" << chrc_id << "\""; | 1214 << "\" not found on characteristic \"" << chrc_id << "\""; |
1215 return NULL; | 1215 return nullptr; |
1216 } | 1216 } |
1217 | 1217 |
1218 return descriptor; | 1218 return descriptor; |
1219 } | 1219 } |
1220 | 1220 |
1221 void BluetoothLowEnergyEventRouter::OnValueSuccess( | 1221 void BluetoothLowEnergyEventRouter::OnValueSuccess( |
1222 const base::Closure& callback, | 1222 const base::Closure& callback, |
1223 const std::vector<uint8>& value) { | 1223 const std::vector<uint8>& value) { |
1224 VLOG(2) << "Remote characteristic/descriptor value read successful."; | 1224 VLOG(2) << "Remote characteristic/descriptor value read successful."; |
1225 callback.Run(); | 1225 callback.Run(); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1344 } | 1344 } |
1345 | 1345 |
1346 BluetoothLowEnergyConnection* BluetoothLowEnergyEventRouter::FindConnection( | 1346 BluetoothLowEnergyConnection* BluetoothLowEnergyEventRouter::FindConnection( |
1347 const std::string& extension_id, | 1347 const std::string& extension_id, |
1348 const std::string& device_address) { | 1348 const std::string& device_address) { |
1349 ConnectionResourceManager* manager = | 1349 ConnectionResourceManager* manager = |
1350 GetConnectionResourceManager(browser_context_); | 1350 GetConnectionResourceManager(browser_context_); |
1351 | 1351 |
1352 base::hash_set<int>* connection_ids = manager->GetResourceIds(extension_id); | 1352 base::hash_set<int>* connection_ids = manager->GetResourceIds(extension_id); |
1353 if (!connection_ids) | 1353 if (!connection_ids) |
1354 return NULL; | 1354 return nullptr; |
1355 | 1355 |
1356 for (base::hash_set<int>::const_iterator iter = connection_ids->begin(); | 1356 for (base::hash_set<int>::const_iterator iter = connection_ids->begin(); |
1357 iter != connection_ids->end(); | 1357 iter != connection_ids->end(); |
1358 ++iter) { | 1358 ++iter) { |
1359 extensions::BluetoothLowEnergyConnection* conn = | 1359 extensions::BluetoothLowEnergyConnection* conn = |
1360 manager->Get(extension_id, *iter); | 1360 manager->Get(extension_id, *iter); |
1361 if (!conn) | 1361 if (!conn) |
1362 continue; | 1362 continue; |
1363 | 1363 |
1364 if (conn->GetConnection()->GetDeviceAddress() == device_address) | 1364 if (conn->GetConnection()->GetDeviceAddress() == device_address) |
1365 return conn; | 1365 return conn; |
1366 } | 1366 } |
1367 | 1367 |
1368 return NULL; | 1368 return nullptr; |
1369 } | 1369 } |
1370 | 1370 |
1371 bool BluetoothLowEnergyEventRouter::RemoveConnection( | 1371 bool BluetoothLowEnergyEventRouter::RemoveConnection( |
1372 const std::string& extension_id, | 1372 const std::string& extension_id, |
1373 const std::string& device_address) { | 1373 const std::string& device_address) { |
1374 ConnectionResourceManager* manager = | 1374 ConnectionResourceManager* manager = |
1375 GetConnectionResourceManager(browser_context_); | 1375 GetConnectionResourceManager(browser_context_); |
1376 | 1376 |
1377 base::hash_set<int>* connection_ids = manager->GetResourceIds(extension_id); | 1377 base::hash_set<int>* connection_ids = manager->GetResourceIds(extension_id); |
1378 if (!connection_ids) | 1378 if (!connection_ids) |
(...skipping 16 matching lines...) Expand all Loading... |
1395 | 1395 |
1396 BluetoothLowEnergyNotifySession* | 1396 BluetoothLowEnergyNotifySession* |
1397 BluetoothLowEnergyEventRouter::FindNotifySession( | 1397 BluetoothLowEnergyEventRouter::FindNotifySession( |
1398 const std::string& extension_id, | 1398 const std::string& extension_id, |
1399 const std::string& characteristic_id) { | 1399 const std::string& characteristic_id) { |
1400 NotifySessionResourceManager* manager = | 1400 NotifySessionResourceManager* manager = |
1401 GetNotifySessionResourceManager(browser_context_); | 1401 GetNotifySessionResourceManager(browser_context_); |
1402 | 1402 |
1403 base::hash_set<int>* ids = manager->GetResourceIds(extension_id); | 1403 base::hash_set<int>* ids = manager->GetResourceIds(extension_id); |
1404 if (!ids) | 1404 if (!ids) |
1405 return NULL; | 1405 return nullptr; |
1406 | 1406 |
1407 for (base::hash_set<int>::const_iterator iter = ids->begin(); | 1407 for (base::hash_set<int>::const_iterator iter = ids->begin(); |
1408 iter != ids->end(); | 1408 iter != ids->end(); |
1409 ++iter) { | 1409 ++iter) { |
1410 BluetoothLowEnergyNotifySession* session = | 1410 BluetoothLowEnergyNotifySession* session = |
1411 manager->Get(extension_id, *iter); | 1411 manager->Get(extension_id, *iter); |
1412 if (!session) | 1412 if (!session) |
1413 continue; | 1413 continue; |
1414 | 1414 |
1415 if (session->GetSession()->GetCharacteristicIdentifier() == | 1415 if (session->GetSession()->GetCharacteristicIdentifier() == |
1416 characteristic_id) | 1416 characteristic_id) |
1417 return session; | 1417 return session; |
1418 } | 1418 } |
1419 | 1419 |
1420 return NULL; | 1420 return nullptr; |
1421 } | 1421 } |
1422 | 1422 |
1423 bool BluetoothLowEnergyEventRouter::RemoveNotifySession( | 1423 bool BluetoothLowEnergyEventRouter::RemoveNotifySession( |
1424 const std::string& extension_id, | 1424 const std::string& extension_id, |
1425 const std::string& characteristic_id) { | 1425 const std::string& characteristic_id) { |
1426 NotifySessionResourceManager* manager = | 1426 NotifySessionResourceManager* manager = |
1427 GetNotifySessionResourceManager(browser_context_); | 1427 GetNotifySessionResourceManager(browser_context_); |
1428 | 1428 |
1429 base::hash_set<int>* ids = manager->GetResourceIds(extension_id); | 1429 base::hash_set<int>* ids = manager->GetResourceIds(extension_id); |
1430 if (!ids) | 1430 if (!ids) |
(...skipping 10 matching lines...) Expand all Loading... |
1441 continue; | 1441 continue; |
1442 | 1442 |
1443 manager->Remove(extension_id, *iter); | 1443 manager->Remove(extension_id, *iter); |
1444 return true; | 1444 return true; |
1445 } | 1445 } |
1446 | 1446 |
1447 return false; | 1447 return false; |
1448 } | 1448 } |
1449 | 1449 |
1450 } // namespace extensions | 1450 } // namespace extensions |
OLD | NEW |