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 // ID Not In Map Note: | 5 // ID Not In Map Note: |
6 // A service, characteristic, or descriptor ID not in the corresponding | 6 // A service, characteristic, or descriptor ID not in the corresponding |
7 // BluetoothDispatcherHost map [service_to_device_, characteristic_to_service_, | 7 // BluetoothDispatcherHost map [service_to_device_, characteristic_to_service_, |
8 // descriptor_to_characteristic_] implies a hostile renderer because a renderer | 8 // descriptor_to_characteristic_] implies a hostile renderer because a renderer |
9 // obtains the corresponding ID from this class and it will be added to the map | 9 // obtains the corresponding ID from this class and it will be added to the map |
10 // at that time. | 10 // at that time. |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
272 // See class comment: UI Thread Note. | 272 // See class comment: UI Thread Note. |
273 *thread = BrowserThread::UI; | 273 *thread = BrowserThread::UI; |
274 } | 274 } |
275 | 275 |
276 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { | 276 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
277 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 277 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
278 bool handled = true; | 278 bool handled = true; |
279 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) | 279 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) |
280 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) | 280 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) |
281 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) | 281 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) |
282 IPC_MESSAGE_HANDLER(BluetoothHostMsg_Disconnect, OnDisconnect) | |
282 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) | 283 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) |
283 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetCharacteristic, OnGetCharacteristic) | 284 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetCharacteristic, OnGetCharacteristic) |
284 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ReadValue, OnReadValue) | 285 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ReadValue, OnReadValue) |
285 IPC_MESSAGE_HANDLER(BluetoothHostMsg_WriteValue, OnWriteValue) | 286 IPC_MESSAGE_HANDLER(BluetoothHostMsg_WriteValue, OnWriteValue) |
286 IPC_MESSAGE_HANDLER(BluetoothHostMsg_StartNotifications, OnStartNotifications) | 287 IPC_MESSAGE_HANDLER(BluetoothHostMsg_StartNotifications, OnStartNotifications) |
287 IPC_MESSAGE_HANDLER(BluetoothHostMsg_StopNotifications, OnStopNotifications) | 288 IPC_MESSAGE_HANDLER(BluetoothHostMsg_StopNotifications, OnStopNotifications) |
288 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RegisterCharacteristic, | 289 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RegisterCharacteristic, |
289 OnRegisterCharacteristicObject); | 290 OnRegisterCharacteristicObject); |
290 IPC_MESSAGE_HANDLER(BluetoothHostMsg_UnregisterCharacteristic, | 291 IPC_MESSAGE_HANDLER(BluetoothHostMsg_UnregisterCharacteristic, |
291 OnUnregisterCharacteristicObject); | 292 OnUnregisterCharacteristicObject); |
(...skipping 22 matching lines...) Expand all Loading... | |
314 DCHECK(pending_primary_services_requests_.empty()); | 315 DCHECK(pending_primary_services_requests_.empty()); |
315 | 316 |
316 // The following data structures are cleaned up when a | 317 // The following data structures are cleaned up when a |
317 // device/service/characteristic is removed. | 318 // device/service/characteristic is removed. |
318 // Since this can happen after the test is done and the cleanup function is | 319 // Since this can happen after the test is done and the cleanup function is |
319 // called, we clean them here. | 320 // called, we clean them here. |
320 service_to_device_.clear(); | 321 service_to_device_.clear(); |
321 characteristic_to_service_.clear(); | 322 characteristic_to_service_.clear(); |
322 characteristic_id_to_notify_session_.clear(); | 323 characteristic_id_to_notify_session_.clear(); |
323 active_characteristic_threads_.clear(); | 324 active_characteristic_threads_.clear(); |
324 connections_.clear(); | 325 device_id_to_connection_map_.clear(); |
325 devices_with_discovered_services_.clear(); | 326 devices_with_discovered_services_.clear(); |
326 } | 327 } |
327 | 328 |
328 set_adapter(std::move(mock_adapter)); | 329 set_adapter(std::move(mock_adapter)); |
329 } | 330 } |
330 | 331 |
331 BluetoothDispatcherHost::~BluetoothDispatcherHost() { | 332 BluetoothDispatcherHost::~BluetoothDispatcherHost() { |
332 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 333 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
333 // Clear adapter, releasing observer references. | 334 // Clear adapter, releasing observer references. |
334 set_adapter(scoped_refptr<device::BluetoothAdapter>()); | 335 set_adapter(scoped_refptr<device::BluetoothAdapter>()); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
428 | 429 |
429 int thread_id; | 430 int thread_id; |
430 int request_id; | 431 int request_id; |
431 std::string service_uuid; | 432 std::string service_uuid; |
432 CallingFunction func; | 433 CallingFunction func; |
433 }; | 434 }; |
434 | 435 |
435 void BluetoothDispatcherHost::set_adapter( | 436 void BluetoothDispatcherHost::set_adapter( |
436 scoped_refptr<device::BluetoothAdapter> adapter) { | 437 scoped_refptr<device::BluetoothAdapter> adapter) { |
437 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 438 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
438 connections_.clear(); | |
439 if (adapter_.get()) | 439 if (adapter_.get()) |
440 adapter_->RemoveObserver(this); | 440 adapter_->RemoveObserver(this); |
441 adapter_ = adapter; | 441 adapter_ = adapter; |
442 if (adapter_.get()) | 442 if (adapter_.get()) |
443 adapter_->AddObserver(this); | 443 adapter_->AddObserver(this); |
444 } | 444 } |
445 | 445 |
446 void BluetoothDispatcherHost::StartDeviceDiscovery( | 446 void BluetoothDispatcherHost::StartDeviceDiscovery( |
447 RequestDeviceSession* session, | 447 RequestDeviceSession* session, |
448 int chooser_id) { | 448 int chooser_id) { |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
728 const CacheQueryResult query_result = | 728 const CacheQueryResult query_result = |
729 QueryCacheForDevice(GetOrigin(frame_routing_id), device_id); | 729 QueryCacheForDevice(GetOrigin(frame_routing_id), device_id); |
730 | 730 |
731 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | 731 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { |
732 RecordConnectGATTOutcome(query_result.outcome); | 732 RecordConnectGATTOutcome(query_result.outcome); |
733 Send(new BluetoothMsg_ConnectGATTError(thread_id, request_id, | 733 Send(new BluetoothMsg_ConnectGATTError(thread_id, request_id, |
734 query_result.GetWebError())); | 734 query_result.GetWebError())); |
735 return; | 735 return; |
736 } | 736 } |
737 | 737 |
738 // If we are already connected no need to connect again. | |
739 auto connection_iter = device_id_to_connection_map_.find(device_id); | |
740 if (connection_iter != device_id_to_connection_map_.end()) { | |
741 if (connection_iter->second->IsConnected()) { | |
742 VLOG(1) << "Already connected."; | |
743 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, | |
744 device_id)); | |
745 return; | |
746 } | |
747 } | |
748 | |
738 query_result.device->CreateGattConnection( | 749 query_result.device->CreateGattConnection( |
739 base::Bind(&BluetoothDispatcherHost::OnGATTConnectionCreated, | 750 base::Bind(&BluetoothDispatcherHost::OnGATTConnectionCreated, |
740 weak_ptr_on_ui_thread_, thread_id, request_id, device_id, | 751 weak_ptr_on_ui_thread_, thread_id, request_id, |
741 start_time), | 752 frame_routing_id, device_id, start_time), |
742 base::Bind(&BluetoothDispatcherHost::OnCreateGATTConnectionError, | 753 base::Bind(&BluetoothDispatcherHost::OnCreateGATTConnectionError, |
743 weak_ptr_on_ui_thread_, thread_id, request_id, device_id, | 754 weak_ptr_on_ui_thread_, thread_id, request_id, device_id, |
744 start_time)); | 755 start_time)); |
745 } | 756 } |
746 | 757 |
758 void BluetoothDispatcherHost::OnDisconnect(int thread_id, | |
759 int frame_routing_id, | |
760 const std::string& device_id) { | |
761 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
762 RecordWebBluetoothFunctionCall( | |
763 UMAWebBluetoothFunction::REMOTE_GATT_SERVER_DISCONNECT); | |
764 | |
765 // Make sure the origin is allowed to access the device. We perform this check | |
766 // in case a hostile renderer is trying to disconnect a device that the | |
767 // renderer is not allowed to access. | |
768 if (allowed_devices_map_.GetDeviceAddress(GetOrigin(frame_routing_id), | |
769 device_id) | |
770 .empty()) { | |
771 bad_message::ReceivedBadMessage( | |
772 this, bad_message::BDH_DEVICE_NOT_ALLOWED_FOR_ORIGIN); | |
773 return; | |
774 } | |
775 | |
776 // The last BluetoothGattConnection for a device closes the connection when | |
777 // it's destroyed. | |
778 if (device_id_to_connection_map_.erase(device_id)) { | |
Jeffrey Yasskin
2016/01/14 22:13:18
What happens if the OnDisconnect comes in after Cr
ortuno
2016/01/15 01:00:08
As discussed, we should do this once we can cancel
Jeffrey Yasskin
2016/01/15 01:12:19
Yep.
| |
779 VLOG(1) << "Disconnecting device: " << device_id; | |
780 } | |
781 } | |
782 | |
747 void BluetoothDispatcherHost::OnGetPrimaryService( | 783 void BluetoothDispatcherHost::OnGetPrimaryService( |
748 int thread_id, | 784 int thread_id, |
749 int request_id, | 785 int request_id, |
750 int frame_routing_id, | 786 int frame_routing_id, |
751 const std::string& device_id, | 787 const std::string& device_id, |
752 const std::string& service_uuid) { | 788 const std::string& service_uuid) { |
753 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 789 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
754 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::GET_PRIMARY_SERVICE); | 790 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::GET_PRIMARY_SERVICE); |
755 RecordGetPrimaryServiceService(BluetoothUUID(service_uuid)); | 791 RecordGetPrimaryServiceService(BluetoothUUID(service_uuid)); |
756 | 792 |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1182 device->GetUUIDs())); // uuids | 1218 device->GetUUIDs())); // uuids |
1183 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::SUCCESS); | 1219 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::SUCCESS); |
1184 Send(new BluetoothMsg_RequestDeviceSuccess(session->thread_id, | 1220 Send(new BluetoothMsg_RequestDeviceSuccess(session->thread_id, |
1185 session->request_id, device_ipc)); | 1221 session->request_id, device_ipc)); |
1186 request_device_sessions_.Remove(chooser_id); | 1222 request_device_sessions_.Remove(chooser_id); |
1187 } | 1223 } |
1188 | 1224 |
1189 void BluetoothDispatcherHost::OnGATTConnectionCreated( | 1225 void BluetoothDispatcherHost::OnGATTConnectionCreated( |
1190 int thread_id, | 1226 int thread_id, |
1191 int request_id, | 1227 int request_id, |
1228 int frame_routing_id, | |
Jeffrey Yasskin
2016/01/14 22:13:18
It looks like you don't actually need this argumen
ortuno
2016/01/15 01:00:08
Done.
| |
1192 const std::string& device_id, | 1229 const std::string& device_id, |
1193 base::TimeTicks start_time, | 1230 base::TimeTicks start_time, |
1194 scoped_ptr<device::BluetoothGattConnection> connection) { | 1231 scoped_ptr<device::BluetoothGattConnection> connection) { |
1195 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1232 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1196 connections_.push_back(std::move(connection)); | 1233 device_id_to_connection_map_[device_id] = std::move(connection); |
1197 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time); | 1234 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time); |
1198 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS); | 1235 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS); |
1199 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, device_id)); | 1236 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, device_id)); |
1200 } | 1237 } |
1201 | 1238 |
1202 void BluetoothDispatcherHost::OnCreateGATTConnectionError( | 1239 void BluetoothDispatcherHost::OnCreateGATTConnectionError( |
1203 int thread_id, | 1240 int thread_id, |
1204 int request_id, | 1241 int request_id, |
1205 const std::string& device_id, | 1242 const std::string& device_id, |
1206 base::TimeTicks start_time, | 1243 base::TimeTicks start_time, |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1424 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1461 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1425 NOTIMPLEMENTED(); | 1462 NOTIMPLEMENTED(); |
1426 } | 1463 } |
1427 | 1464 |
1428 void BluetoothDispatcherHost::ShowNeedLocationLink() { | 1465 void BluetoothDispatcherHost::ShowNeedLocationLink() { |
1429 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1466 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1430 NOTIMPLEMENTED(); | 1467 NOTIMPLEMENTED(); |
1431 } | 1468 } |
1432 | 1469 |
1433 } // namespace content | 1470 } // namespace content |
OLD | NEW |