| 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 "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_event_router.h" | 5 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_event_router.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 828 service_id_to_device_address_.end()); | 828 service_id_to_device_address_.end()); |
| 829 | 829 |
| 830 DCHECK(device->GetAddress() == | 830 DCHECK(device->GetAddress() == |
| 831 service_id_to_device_address_[service->GetIdentifier()]); | 831 service_id_to_device_address_[service->GetIdentifier()]); |
| 832 service_id_to_device_address_.erase(service->GetIdentifier()); | 832 service_id_to_device_address_.erase(service->GetIdentifier()); |
| 833 | 833 |
| 834 // Signal API event. | 834 // Signal API event. |
| 835 apibtle::Service api_service; | 835 apibtle::Service api_service; |
| 836 PopulateService(service, &api_service); | 836 PopulateService(service, &api_service); |
| 837 | 837 |
| 838 scoped_ptr<base::ListValue> args = | 838 std::unique_ptr<base::ListValue> args = |
| 839 apibtle::OnServiceRemoved::Create(api_service); | 839 apibtle::OnServiceRemoved::Create(api_service); |
| 840 scoped_ptr<Event> event( | 840 std::unique_ptr<Event> event( |
| 841 new Event(events::BLUETOOTH_LOW_ENERGY_ON_SERVICE_REMOVED, | 841 new Event(events::BLUETOOTH_LOW_ENERGY_ON_SERVICE_REMOVED, |
| 842 apibtle::OnServiceRemoved::kEventName, std::move(args))); | 842 apibtle::OnServiceRemoved::kEventName, std::move(args))); |
| 843 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event)); | 843 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event)); |
| 844 } | 844 } |
| 845 | 845 |
| 846 void BluetoothLowEnergyEventRouter::GattDiscoveryCompleteForService( | 846 void BluetoothLowEnergyEventRouter::GattDiscoveryCompleteForService( |
| 847 BluetoothAdapter* adapter, | 847 BluetoothAdapter* adapter, |
| 848 BluetoothGattService* service) { | 848 BluetoothGattService* service) { |
| 849 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 849 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 850 DCHECK_EQ(adapter, adapter_.get()); | 850 DCHECK_EQ(adapter, adapter_.get()); |
| 851 VLOG(2) << "GATT service discovery complete: " << service->GetIdentifier(); | 851 VLOG(2) << "GATT service discovery complete: " << service->GetIdentifier(); |
| 852 | 852 |
| 853 DCHECK(service_id_to_device_address_.find(service->GetIdentifier()) != | 853 DCHECK(service_id_to_device_address_.find(service->GetIdentifier()) != |
| 854 service_id_to_device_address_.end()); | 854 service_id_to_device_address_.end()); |
| 855 | 855 |
| 856 // Signal the service added event here. | 856 // Signal the service added event here. |
| 857 apibtle::Service api_service; | 857 apibtle::Service api_service; |
| 858 PopulateService(service, &api_service); | 858 PopulateService(service, &api_service); |
| 859 | 859 |
| 860 scoped_ptr<base::ListValue> args = | 860 std::unique_ptr<base::ListValue> args = |
| 861 apibtle::OnServiceAdded::Create(api_service); | 861 apibtle::OnServiceAdded::Create(api_service); |
| 862 scoped_ptr<Event> event( | 862 std::unique_ptr<Event> event( |
| 863 new Event(events::BLUETOOTH_LOW_ENERGY_ON_SERVICE_ADDED, | 863 new Event(events::BLUETOOTH_LOW_ENERGY_ON_SERVICE_ADDED, |
| 864 apibtle::OnServiceAdded::kEventName, std::move(args))); | 864 apibtle::OnServiceAdded::kEventName, std::move(args))); |
| 865 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event)); | 865 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event)); |
| 866 } | 866 } |
| 867 | 867 |
| 868 void BluetoothLowEnergyEventRouter::DeviceAddressChanged( | 868 void BluetoothLowEnergyEventRouter::DeviceAddressChanged( |
| 869 BluetoothAdapter* adapter, | 869 BluetoothAdapter* adapter, |
| 870 BluetoothDevice* device, | 870 BluetoothDevice* device, |
| 871 const std::string& old_address) { | 871 const std::string& old_address) { |
| 872 for (auto& iter : service_id_to_device_address_) { | 872 for (auto& iter : service_id_to_device_address_) { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 986 DCHECK(chrc_id_to_service_id_.find(characteristic->GetIdentifier()) != | 986 DCHECK(chrc_id_to_service_id_.find(characteristic->GetIdentifier()) != |
| 987 chrc_id_to_service_id_.end()); | 987 chrc_id_to_service_id_.end()); |
| 988 DCHECK(chrc_id_to_service_id_[characteristic->GetIdentifier()] == | 988 DCHECK(chrc_id_to_service_id_[characteristic->GetIdentifier()] == |
| 989 service->GetIdentifier()); | 989 service->GetIdentifier()); |
| 990 | 990 |
| 991 // Send the event; manually construct the arguments, instead of using | 991 // Send the event; manually construct the arguments, instead of using |
| 992 // apibtle::OnCharacteristicValueChanged::Create, as it doesn't convert | 992 // apibtle::OnCharacteristicValueChanged::Create, as it doesn't convert |
| 993 // lists of enums correctly. | 993 // lists of enums correctly. |
| 994 apibtle::Characteristic api_characteristic; | 994 apibtle::Characteristic api_characteristic; |
| 995 PopulateCharacteristic(characteristic, &api_characteristic); | 995 PopulateCharacteristic(characteristic, &api_characteristic); |
| 996 scoped_ptr<base::ListValue> args(new base::ListValue()); | 996 std::unique_ptr<base::ListValue> args(new base::ListValue()); |
| 997 args->Append(apibtle::CharacteristicToValue(&api_characteristic).release()); | 997 args->Append(apibtle::CharacteristicToValue(&api_characteristic).release()); |
| 998 | 998 |
| 999 DispatchEventToExtensionsWithPermission( | 999 DispatchEventToExtensionsWithPermission( |
| 1000 events::BLUETOOTH_LOW_ENERGY_ON_CHARACTERISTIC_VALUE_CHANGED, | 1000 events::BLUETOOTH_LOW_ENERGY_ON_CHARACTERISTIC_VALUE_CHANGED, |
| 1001 apibtle::OnCharacteristicValueChanged::kEventName, service->GetUUID(), | 1001 apibtle::OnCharacteristicValueChanged::kEventName, service->GetUUID(), |
| 1002 characteristic->GetIdentifier(), std::move(args)); | 1002 characteristic->GetIdentifier(), std::move(args)); |
| 1003 } | 1003 } |
| 1004 | 1004 |
| 1005 void BluetoothLowEnergyEventRouter::GattDescriptorValueChanged( | 1005 void BluetoothLowEnergyEventRouter::GattDescriptorValueChanged( |
| 1006 BluetoothAdapter* adapter, | 1006 BluetoothAdapter* adapter, |
| 1007 BluetoothGattDescriptor* descriptor, | 1007 BluetoothGattDescriptor* descriptor, |
| 1008 const std::vector<uint8_t>& value) { | 1008 const std::vector<uint8_t>& value) { |
| 1009 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1009 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1010 DCHECK_EQ(adapter, adapter_.get()); | 1010 DCHECK_EQ(adapter, adapter_.get()); |
| 1011 VLOG(2) << "GATT descriptor value changed: " << descriptor->GetIdentifier(); | 1011 VLOG(2) << "GATT descriptor value changed: " << descriptor->GetIdentifier(); |
| 1012 | 1012 |
| 1013 BluetoothGattCharacteristic* characteristic = descriptor->GetCharacteristic(); | 1013 BluetoothGattCharacteristic* characteristic = descriptor->GetCharacteristic(); |
| 1014 DCHECK(characteristic); | 1014 DCHECK(characteristic); |
| 1015 | 1015 |
| 1016 DCHECK(desc_id_to_chrc_id_.find(descriptor->GetIdentifier()) != | 1016 DCHECK(desc_id_to_chrc_id_.find(descriptor->GetIdentifier()) != |
| 1017 desc_id_to_chrc_id_.end()); | 1017 desc_id_to_chrc_id_.end()); |
| 1018 DCHECK(characteristic->GetIdentifier() == | 1018 DCHECK(characteristic->GetIdentifier() == |
| 1019 desc_id_to_chrc_id_[descriptor->GetIdentifier()]); | 1019 desc_id_to_chrc_id_[descriptor->GetIdentifier()]); |
| 1020 | 1020 |
| 1021 // Send the event; manually construct the arguments, instead of using | 1021 // Send the event; manually construct the arguments, instead of using |
| 1022 // apibtle::OnDescriptorValueChanged::Create, as it doesn't convert | 1022 // apibtle::OnDescriptorValueChanged::Create, as it doesn't convert |
| 1023 // lists of enums correctly. | 1023 // lists of enums correctly. |
| 1024 apibtle::Descriptor api_descriptor; | 1024 apibtle::Descriptor api_descriptor; |
| 1025 PopulateDescriptor(descriptor, &api_descriptor); | 1025 PopulateDescriptor(descriptor, &api_descriptor); |
| 1026 scoped_ptr<base::ListValue> args(new base::ListValue()); | 1026 std::unique_ptr<base::ListValue> args(new base::ListValue()); |
| 1027 args->Append(apibtle::DescriptorToValue(&api_descriptor).release()); | 1027 args->Append(apibtle::DescriptorToValue(&api_descriptor).release()); |
| 1028 | 1028 |
| 1029 DispatchEventToExtensionsWithPermission( | 1029 DispatchEventToExtensionsWithPermission( |
| 1030 events::BLUETOOTH_LOW_ENERGY_ON_DESCRIPTOR_VALUE_CHANGED, | 1030 events::BLUETOOTH_LOW_ENERGY_ON_DESCRIPTOR_VALUE_CHANGED, |
| 1031 apibtle::OnDescriptorValueChanged::kEventName, | 1031 apibtle::OnDescriptorValueChanged::kEventName, |
| 1032 characteristic->GetService()->GetUUID(), "" /* characteristic_id */, | 1032 characteristic->GetService()->GetUUID(), "" /* characteristic_id */, |
| 1033 std::move(args)); | 1033 std::move(args)); |
| 1034 } | 1034 } |
| 1035 | 1035 |
| 1036 void BluetoothLowEnergyEventRouter::OnGetAdapter( | 1036 void BluetoothLowEnergyEventRouter::OnGetAdapter( |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1095 } | 1095 } |
| 1096 } | 1096 } |
| 1097 } | 1097 } |
| 1098 } | 1098 } |
| 1099 | 1099 |
| 1100 void BluetoothLowEnergyEventRouter::DispatchEventToExtensionsWithPermission( | 1100 void BluetoothLowEnergyEventRouter::DispatchEventToExtensionsWithPermission( |
| 1101 events::HistogramValue histogram_value, | 1101 events::HistogramValue histogram_value, |
| 1102 const std::string& event_name, | 1102 const std::string& event_name, |
| 1103 const device::BluetoothUUID& uuid, | 1103 const device::BluetoothUUID& uuid, |
| 1104 const std::string& characteristic_id, | 1104 const std::string& characteristic_id, |
| 1105 scoped_ptr<base::ListValue> args) { | 1105 std::unique_ptr<base::ListValue> args) { |
| 1106 // Obtain the listeners of |event_name|. The list can contain multiple | 1106 // Obtain the listeners of |event_name|. The list can contain multiple |
| 1107 // entries for the same extension, so we keep track of the extensions that we | 1107 // entries for the same extension, so we keep track of the extensions that we |
| 1108 // already sent the event to, since we want the send an event to an extension | 1108 // already sent the event to, since we want the send an event to an extension |
| 1109 // only once. | 1109 // only once. |
| 1110 BluetoothPermissionRequest request(uuid.value()); | 1110 BluetoothPermissionRequest request(uuid.value()); |
| 1111 std::set<std::string> handled_extensions; | 1111 std::set<std::string> handled_extensions; |
| 1112 const EventListenerMap::ListenerList listeners = | 1112 const EventListenerMap::ListenerList listeners = |
| 1113 EventRouter::Get(browser_context_)->listeners().GetEventListenersByName( | 1113 EventRouter::Get(browser_context_)->listeners().GetEventListenersByName( |
| 1114 event_name); | 1114 event_name); |
| 1115 | 1115 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1135 | 1135 |
| 1136 // If |event_name| is "onCharacteristicValueChanged", then send the | 1136 // If |event_name| is "onCharacteristicValueChanged", then send the |
| 1137 // event only if the extension has requested notifications from the | 1137 // event only if the extension has requested notifications from the |
| 1138 // related characteristic. | 1138 // related characteristic. |
| 1139 if (event_name == apibtle::OnCharacteristicValueChanged::kEventName && | 1139 if (event_name == apibtle::OnCharacteristicValueChanged::kEventName && |
| 1140 !characteristic_id.empty() && | 1140 !characteristic_id.empty() && |
| 1141 !FindNotifySession(extension_id, characteristic_id)) | 1141 !FindNotifySession(extension_id, characteristic_id)) |
| 1142 continue; | 1142 continue; |
| 1143 | 1143 |
| 1144 // Send the event. | 1144 // Send the event. |
| 1145 scoped_ptr<base::ListValue> args_copy(args->DeepCopy()); | 1145 std::unique_ptr<base::ListValue> args_copy(args->DeepCopy()); |
| 1146 scoped_ptr<Event> event( | 1146 std::unique_ptr<Event> event( |
| 1147 new Event(histogram_value, event_name, std::move(args_copy))); | 1147 new Event(histogram_value, event_name, std::move(args_copy))); |
| 1148 EventRouter::Get(browser_context_) | 1148 EventRouter::Get(browser_context_) |
| 1149 ->DispatchEventToExtension(extension_id, std::move(event)); | 1149 ->DispatchEventToExtension(extension_id, std::move(event)); |
| 1150 } | 1150 } |
| 1151 } | 1151 } |
| 1152 | 1152 |
| 1153 BluetoothGattService* BluetoothLowEnergyEventRouter::FindServiceById( | 1153 BluetoothGattService* BluetoothLowEnergyEventRouter::FindServiceById( |
| 1154 const std::string& instance_id) const { | 1154 const std::string& instance_id) const { |
| 1155 InstanceIdMap::const_iterator iter = | 1155 InstanceIdMap::const_iterator iter = |
| 1156 service_id_to_device_address_.find(instance_id); | 1156 service_id_to_device_address_.find(instance_id); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1236 const std::vector<uint8_t>& value) { | 1236 const std::vector<uint8_t>& value) { |
| 1237 VLOG(2) << "Remote characteristic/descriptor value read successful."; | 1237 VLOG(2) << "Remote characteristic/descriptor value read successful."; |
| 1238 callback.Run(); | 1238 callback.Run(); |
| 1239 } | 1239 } |
| 1240 | 1240 |
| 1241 void BluetoothLowEnergyEventRouter::OnCreateGattConnection( | 1241 void BluetoothLowEnergyEventRouter::OnCreateGattConnection( |
| 1242 bool persistent, | 1242 bool persistent, |
| 1243 const std::string& extension_id, | 1243 const std::string& extension_id, |
| 1244 const std::string& device_address, | 1244 const std::string& device_address, |
| 1245 const base::Closure& callback, | 1245 const base::Closure& callback, |
| 1246 scoped_ptr<BluetoothGattConnection> connection) { | 1246 std::unique_ptr<BluetoothGattConnection> connection) { |
| 1247 VLOG(2) << "GATT connection created."; | 1247 VLOG(2) << "GATT connection created."; |
| 1248 DCHECK(connection.get()); | 1248 DCHECK(connection.get()); |
| 1249 DCHECK(!FindConnection(extension_id, device_address)); | 1249 DCHECK(!FindConnection(extension_id, device_address)); |
| 1250 DCHECK_EQ(device_address, connection->GetDeviceAddress()); | 1250 DCHECK_EQ(device_address, connection->GetDeviceAddress()); |
| 1251 | 1251 |
| 1252 const std::string connect_id = extension_id + device_address; | 1252 const std::string connect_id = extension_id + device_address; |
| 1253 DCHECK_NE(0U, connecting_devices_.count(connect_id)); | 1253 DCHECK_NE(0U, connecting_devices_.count(connect_id)); |
| 1254 | 1254 |
| 1255 BluetoothLowEnergyConnection* conn = new BluetoothLowEnergyConnection( | 1255 BluetoothLowEnergyConnection* conn = new BluetoothLowEnergyConnection( |
| 1256 persistent, extension_id, std::move(connection)); | 1256 persistent, extension_id, std::move(connection)); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1335 } | 1335 } |
| 1336 | 1336 |
| 1337 error_callback.Run(error_status); | 1337 error_callback.Run(error_status); |
| 1338 } | 1338 } |
| 1339 | 1339 |
| 1340 void BluetoothLowEnergyEventRouter::OnStartNotifySession( | 1340 void BluetoothLowEnergyEventRouter::OnStartNotifySession( |
| 1341 bool persistent, | 1341 bool persistent, |
| 1342 const std::string& extension_id, | 1342 const std::string& extension_id, |
| 1343 const std::string& characteristic_id, | 1343 const std::string& characteristic_id, |
| 1344 const base::Closure& callback, | 1344 const base::Closure& callback, |
| 1345 scoped_ptr<device::BluetoothGattNotifySession> session) { | 1345 std::unique_ptr<device::BluetoothGattNotifySession> session) { |
| 1346 VLOG(2) << "Value update session created for characteristic: " | 1346 VLOG(2) << "Value update session created for characteristic: " |
| 1347 << characteristic_id; | 1347 << characteristic_id; |
| 1348 DCHECK(session.get()); | 1348 DCHECK(session.get()); |
| 1349 DCHECK(!FindNotifySession(extension_id, characteristic_id)); | 1349 DCHECK(!FindNotifySession(extension_id, characteristic_id)); |
| 1350 DCHECK_EQ(characteristic_id, session->GetCharacteristicIdentifier()); | 1350 DCHECK_EQ(characteristic_id, session->GetCharacteristicIdentifier()); |
| 1351 | 1351 |
| 1352 const std::string session_id = extension_id + characteristic_id; | 1352 const std::string session_id = extension_id + characteristic_id; |
| 1353 DCHECK_NE(0U, pending_session_calls_.count(session_id)); | 1353 DCHECK_NE(0U, pending_session_calls_.count(session_id)); |
| 1354 | 1354 |
| 1355 BluetoothLowEnergyNotifySession* resource = | 1355 BluetoothLowEnergyNotifySession* resource = |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1492 continue; | 1492 continue; |
| 1493 | 1493 |
| 1494 manager->Remove(extension_id, *iter); | 1494 manager->Remove(extension_id, *iter); |
| 1495 return true; | 1495 return true; |
| 1496 } | 1496 } |
| 1497 | 1497 |
| 1498 return false; | 1498 return false; |
| 1499 } | 1499 } |
| 1500 | 1500 |
| 1501 } // namespace extensions | 1501 } // namespace extensions |
| OLD | NEW |