Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: content/browser/bluetooth/bluetooth_dispatcher_host.cc

Issue 1527853002: bluetooth: Add disconnect function (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-device-identifier
Patch Set: Fix test that fails after upstream change. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 } 326 }
326 327
327 set_adapter(std::move(mock_adapter)); 328 set_adapter(std::move(mock_adapter));
328 } 329 }
329 330
330 BluetoothDispatcherHost::~BluetoothDispatcherHost() { 331 BluetoothDispatcherHost::~BluetoothDispatcherHost() {
331 DCHECK_CURRENTLY_ON(BrowserThread::UI); 332 DCHECK_CURRENTLY_ON(BrowserThread::UI);
332 // Clear adapter, releasing observer references. 333 // Clear adapter, releasing observer references.
333 set_adapter(scoped_refptr<device::BluetoothAdapter>()); 334 set_adapter(scoped_refptr<device::BluetoothAdapter>());
334 } 335 }
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 428
428 int thread_id; 429 int thread_id;
429 int request_id; 430 int request_id;
430 std::string service_uuid; 431 std::string service_uuid;
431 CallingFunction func; 432 CallingFunction func;
432 }; 433 };
433 434
434 void BluetoothDispatcherHost::set_adapter( 435 void BluetoothDispatcherHost::set_adapter(
435 scoped_refptr<device::BluetoothAdapter> adapter) { 436 scoped_refptr<device::BluetoothAdapter> adapter) {
436 DCHECK_CURRENTLY_ON(BrowserThread::UI); 437 DCHECK_CURRENTLY_ON(BrowserThread::UI);
437 connections_.clear();
438 if (adapter_.get()) 438 if (adapter_.get())
439 adapter_->RemoveObserver(this); 439 adapter_->RemoveObserver(this);
440 adapter_ = adapter; 440 adapter_ = adapter;
441 if (adapter_.get()) 441 if (adapter_.get())
442 adapter_->AddObserver(this); 442 adapter_->AddObserver(this);
443 } 443 }
444 444
445 void BluetoothDispatcherHost::StartDeviceDiscovery( 445 void BluetoothDispatcherHost::StartDeviceDiscovery(
446 RequestDeviceSession* session, 446 RequestDeviceSession* session,
447 int chooser_id) { 447 int chooser_id) {
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 const CacheQueryResult query_result = 725 const CacheQueryResult query_result =
726 QueryCacheForDevice(GetOrigin(frame_routing_id), device_id); 726 QueryCacheForDevice(GetOrigin(frame_routing_id), device_id);
727 727
728 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { 728 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
729 RecordConnectGATTOutcome(query_result.outcome); 729 RecordConnectGATTOutcome(query_result.outcome);
730 Send(new BluetoothMsg_ConnectGATTError(thread_id, request_id, 730 Send(new BluetoothMsg_ConnectGATTError(thread_id, request_id,
731 query_result.GetWebError())); 731 query_result.GetWebError()));
732 return; 732 return;
733 } 733 }
734 734
735 // If we are already connected no need to connect again.
736 auto connection_iter = device_id_to_connection_map_.find(device_id);
737 if (connection_iter != device_id_to_connection_map_.end()) {
738 if (connection_iter->second->IsConnected()) {
739 VLOG(1) << "Already connected.";
740 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id,
741 device_id));
742 return;
743 }
744 }
745
735 query_result.device->CreateGattConnection( 746 query_result.device->CreateGattConnection(
736 base::Bind(&BluetoothDispatcherHost::OnGATTConnectionCreated, 747 base::Bind(&BluetoothDispatcherHost::OnGATTConnectionCreated,
737 weak_ptr_on_ui_thread_, thread_id, request_id, device_id, 748 weak_ptr_on_ui_thread_, thread_id, request_id, device_id,
738 start_time), 749 start_time),
739 base::Bind(&BluetoothDispatcherHost::OnCreateGATTConnectionError, 750 base::Bind(&BluetoothDispatcherHost::OnCreateGATTConnectionError,
740 weak_ptr_on_ui_thread_, thread_id, request_id, device_id, 751 weak_ptr_on_ui_thread_, thread_id, request_id, device_id,
741 start_time)); 752 start_time));
742 } 753 }
743 754
755 void BluetoothDispatcherHost::OnDisconnect(int thread_id,
756 int frame_routing_id,
757 const std::string& device_id) {
758 DCHECK_CURRENTLY_ON(BrowserThread::UI);
759 RecordWebBluetoothFunctionCall(
760 UMAWebBluetoothFunction::REMOTE_GATT_SERVER_DISCONNECT);
761
762 // Make sure the origin is allowed to access the device. We perform this check
763 // in case a hostile renderer is trying to disconnect a device that the
764 // renderer is not allowed to access.
765 if (allowed_devices_map_.GetDeviceAddress(GetOrigin(frame_routing_id),
766 device_id)
767 .empty()) {
768 bad_message::ReceivedBadMessage(
769 this, bad_message::BDH_DEVICE_NOT_ALLOWED_FOR_ORIGIN);
770 return;
771 }
772
773 // The last BluetoothGattConnection for a device closes the connection when
774 // it's destroyed.
775 if (device_id_to_connection_map_.erase(device_id)) {
776 VLOG(1) << "Disconnecting device: " << device_id;
777 }
778 }
779
744 void BluetoothDispatcherHost::OnGetPrimaryService( 780 void BluetoothDispatcherHost::OnGetPrimaryService(
745 int thread_id, 781 int thread_id,
746 int request_id, 782 int request_id,
747 int frame_routing_id, 783 int frame_routing_id,
748 const std::string& device_id, 784 const std::string& device_id,
749 const std::string& service_uuid) { 785 const std::string& service_uuid) {
750 DCHECK_CURRENTLY_ON(BrowserThread::UI); 786 DCHECK_CURRENTLY_ON(BrowserThread::UI);
751 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::GET_PRIMARY_SERVICE); 787 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::GET_PRIMARY_SERVICE);
752 RecordGetPrimaryServiceService(BluetoothUUID(service_uuid)); 788 RecordGetPrimaryServiceService(BluetoothUUID(service_uuid));
753 789
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
1181 request_device_sessions_.Remove(chooser_id); 1217 request_device_sessions_.Remove(chooser_id);
1182 } 1218 }
1183 1219
1184 void BluetoothDispatcherHost::OnGATTConnectionCreated( 1220 void BluetoothDispatcherHost::OnGATTConnectionCreated(
1185 int thread_id, 1221 int thread_id,
1186 int request_id, 1222 int request_id,
1187 const std::string& device_id, 1223 const std::string& device_id,
1188 base::TimeTicks start_time, 1224 base::TimeTicks start_time,
1189 scoped_ptr<device::BluetoothGattConnection> connection) { 1225 scoped_ptr<device::BluetoothGattConnection> connection) {
1190 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1226 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1191 connections_.push_back(std::move(connection)); 1227 device_id_to_connection_map_[device_id] = std::move(connection);
1192 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time); 1228 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time);
1193 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS); 1229 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS);
1194 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, device_id)); 1230 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, device_id));
1195 } 1231 }
1196 1232
1197 void BluetoothDispatcherHost::OnCreateGATTConnectionError( 1233 void BluetoothDispatcherHost::OnCreateGATTConnectionError(
1198 int thread_id, 1234 int thread_id,
1199 int request_id, 1235 int request_id,
1200 const std::string& device_id, 1236 const std::string& device_id,
1201 base::TimeTicks start_time, 1237 base::TimeTicks start_time,
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
1422 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1458 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1423 NOTIMPLEMENTED(); 1459 NOTIMPLEMENTED();
1424 } 1460 }
1425 1461
1426 void BluetoothDispatcherHost::ShowNeedLocationLink() { 1462 void BluetoothDispatcherHost::ShowNeedLocationLink() {
1427 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1463 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1428 NOTIMPLEMENTED(); 1464 NOTIMPLEMENTED();
1429 } 1465 }
1430 1466
1431 } // namespace content 1467 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/bluetooth/bluetooth_dispatcher_host.h ('k') | content/browser/bluetooth/bluetooth_metrics.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698