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 // NETWORK_ERROR Note: | 5 // NETWORK_ERROR Note: |
6 // When a device can't be found in the BluetoothAdapter, that generally | 6 // When a device can't be found in the BluetoothAdapter, that generally |
7 // indicates that it's gone out of range. We reject with a NetworkError in that | 7 // indicates that it's gone out of range. We reject with a NetworkError in that |
8 // case. | 8 // case. |
9 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothdevice-conne
ctgatt | 9 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothdevice-conne
ctgatt |
10 | 10 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 current_delay_time_ = kTestingDelayTime; | 179 current_delay_time_ = kTestingDelayTime; |
180 set_adapter(mock_adapter.Pass()); | 180 set_adapter(mock_adapter.Pass()); |
181 } | 181 } |
182 | 182 |
183 BluetoothDispatcherHost::~BluetoothDispatcherHost() { | 183 BluetoothDispatcherHost::~BluetoothDispatcherHost() { |
184 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 184 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
185 // Clear adapter, releasing observer references. | 185 // Clear adapter, releasing observer references. |
186 set_adapter(scoped_refptr<device::BluetoothAdapter>()); | 186 set_adapter(scoped_refptr<device::BluetoothAdapter>()); |
187 } | 187 } |
188 | 188 |
189 struct BluetoothDispatcherHost::DiscoverySessionOptions { | 189 // Stores information associated with an in-progress requestDevice call. This |
190 DiscoverySessionOptions(const std::vector<BluetoothScanFilter>& filters, | 190 // will include the state of the active chooser dialog in a future patch. |
191 const std::vector<BluetoothUUID>& optional_services) | 191 struct BluetoothDispatcherHost::RequestDeviceSession { |
| 192 RequestDeviceSession(const std::vector<BluetoothScanFilter>& filters, |
| 193 const std::vector<BluetoothUUID>& optional_services) |
192 : filters(filters), optional_services(optional_services) {} | 194 : filters(filters), optional_services(optional_services) {} |
193 | 195 |
194 std::vector<BluetoothScanFilter> filters; | 196 std::vector<BluetoothScanFilter> filters; |
195 std::vector<BluetoothUUID> optional_services; | 197 std::vector<BluetoothUUID> optional_services; |
196 }; | 198 }; |
197 | 199 |
198 void BluetoothDispatcherHost::set_adapter( | 200 void BluetoothDispatcherHost::set_adapter( |
199 scoped_refptr<device::BluetoothAdapter> adapter) { | 201 scoped_refptr<device::BluetoothAdapter> adapter) { |
200 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 202 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
201 if (adapter_.get()) | 203 if (adapter_.get()) |
(...skipping 20 matching lines...) Expand all Loading... |
222 | 224 |
223 void BluetoothDispatcherHost::OnRequestDevice( | 225 void BluetoothDispatcherHost::OnRequestDevice( |
224 int thread_id, | 226 int thread_id, |
225 int request_id, | 227 int request_id, |
226 const std::vector<BluetoothScanFilter>& filters, | 228 const std::vector<BluetoothScanFilter>& filters, |
227 const std::vector<BluetoothUUID>& optional_services) { | 229 const std::vector<BluetoothUUID>& optional_services) { |
228 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 230 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
229 // TODO(scheib): Device selection UI: crbug.com/436280 | 231 // TODO(scheib): Device selection UI: crbug.com/436280 |
230 // TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed. | 232 // TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed. |
231 if (adapter_.get()) { | 233 if (adapter_.get()) { |
| 234 if (!request_device_sessions_ |
| 235 .insert(std::make_pair( |
| 236 std::make_pair(thread_id, request_id), |
| 237 RequestDeviceSession(filters, optional_services))) |
| 238 .second) { |
| 239 LOG(ERROR) << "2 requestDevice() calls with the same thread_id (" |
| 240 << thread_id << ") and request_id (" << request_id |
| 241 << ") shouldn't arrive at the same BluetoothDispatcherHost."; |
| 242 bad_message::ReceivedBadMessage( |
| 243 this, bad_message::BDH_DUPLICATE_REQUEST_DEVICE_ID); |
| 244 } |
232 adapter_->StartDiscoverySessionWithFilter( | 245 adapter_->StartDiscoverySessionWithFilter( |
233 ComputeScanFilter(filters), | 246 ComputeScanFilter(filters), |
234 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, | 247 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, |
235 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 248 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), |
236 base::Passed(make_scoped_ptr(new DiscoverySessionOptions( | |
237 filters, optional_services)))), | |
238 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError, | 249 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError, |
239 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); | 250 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
240 } else { | 251 } else { |
241 DLOG(WARNING) << "No BluetoothAdapter. Can't serve requestDevice."; | 252 DLOG(WARNING) << "No BluetoothAdapter. Can't serve requestDevice."; |
242 Send(new BluetoothMsg_RequestDeviceError( | 253 Send(new BluetoothMsg_RequestDeviceError( |
243 thread_id, request_id, WebBluetoothError::NoBluetoothAdapter)); | 254 thread_id, request_id, WebBluetoothError::NoBluetoothAdapter)); |
244 } | 255 } |
245 return; | 256 return; |
246 } | 257 } |
247 | 258 |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 characteristic->WriteRemoteCharacteristic( | 476 characteristic->WriteRemoteCharacteristic( |
466 value, base::Bind(&BluetoothDispatcherHost::OnWriteValueSuccess, | 477 value, base::Bind(&BluetoothDispatcherHost::OnWriteValueSuccess, |
467 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), | 478 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), |
468 base::Bind(&BluetoothDispatcherHost::OnWriteValueFailed, | 479 base::Bind(&BluetoothDispatcherHost::OnWriteValueFailed, |
469 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); | 480 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
470 } | 481 } |
471 | 482 |
472 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 483 void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
473 int thread_id, | 484 int thread_id, |
474 int request_id, | 485 int request_id, |
475 scoped_ptr<DiscoverySessionOptions> options, | |
476 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 486 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
477 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 487 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
478 BrowserThread::PostDelayedTask( | 488 BrowserThread::PostDelayedTask( |
479 BrowserThread::UI, FROM_HERE, | 489 BrowserThread::UI, FROM_HERE, |
480 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, | 490 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, |
481 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 491 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
482 base::Passed(&options), base::Passed(&discovery_session)), | 492 base::Passed(&discovery_session)), |
483 base::TimeDelta::FromSeconds(current_delay_time_)); | 493 base::TimeDelta::FromSeconds(current_delay_time_)); |
484 } | 494 } |
485 | 495 |
486 void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id, | 496 void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id, |
487 int request_id) { | 497 int request_id) { |
488 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 498 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
489 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStartedError"; | 499 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStartedError"; |
490 Send(new BluetoothMsg_RequestDeviceError( | 500 Send(new BluetoothMsg_RequestDeviceError( |
491 thread_id, request_id, WebBluetoothError::DiscoverySessionStartFailed)); | 501 thread_id, request_id, WebBluetoothError::DiscoverySessionStartFailed)); |
| 502 request_device_sessions_.erase(std::make_pair(thread_id, request_id)); |
492 } | 503 } |
493 | 504 |
494 void BluetoothDispatcherHost::StopDiscoverySession( | 505 void BluetoothDispatcherHost::StopDiscoverySession( |
495 int thread_id, | 506 int thread_id, |
496 int request_id, | 507 int request_id, |
497 scoped_ptr<DiscoverySessionOptions> options, | |
498 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 508 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
499 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 509 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
500 discovery_session->Stop( | 510 discovery_session->Stop( |
501 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStopped, | 511 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStopped, |
502 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 512 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), |
503 base::Passed(&options)), | |
504 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStoppedError, | 513 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStoppedError, |
505 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); | 514 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
506 } | 515 } |
507 | 516 |
508 void BluetoothDispatcherHost::OnDiscoverySessionStopped( | 517 void BluetoothDispatcherHost::OnDiscoverySessionStopped(int thread_id, |
509 int thread_id, | 518 int request_id) { |
510 int request_id, | |
511 scoped_ptr<DiscoverySessionOptions> options) { | |
512 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 519 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 520 auto session = |
| 521 request_device_sessions_.find(std::make_pair(thread_id, request_id)); |
| 522 CHECK(session != request_device_sessions_.end()); |
513 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | 523 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
514 for (device::BluetoothDevice* device : devices) { | 524 for (device::BluetoothDevice* device : devices) { |
515 if (MatchesFilters(*device, options->filters)) { | 525 if (MatchesFilters(*device, session->second.filters)) { |
516 content::BluetoothDevice device_ipc( | 526 content::BluetoothDevice device_ipc( |
517 device->GetAddress(), // instance_id | 527 device->GetAddress(), // instance_id |
518 device->GetName(), // name | 528 device->GetName(), // name |
519 device->GetBluetoothClass(), // device_class | 529 device->GetBluetoothClass(), // device_class |
520 device->GetVendorIDSource(), // vendor_id_source | 530 device->GetVendorIDSource(), // vendor_id_source |
521 device->GetVendorID(), // vendor_id | 531 device->GetVendorID(), // vendor_id |
522 device->GetProductID(), // product_id | 532 device->GetProductID(), // product_id |
523 device->GetDeviceID(), // product_version | 533 device->GetDeviceID(), // product_version |
524 device->IsPaired(), // paired | 534 device->IsPaired(), // paired |
525 content::BluetoothDevice::UUIDsFromBluetoothUUIDs( | 535 content::BluetoothDevice::UUIDsFromBluetoothUUIDs( |
526 device->GetUUIDs())); // uuids | 536 device->GetUUIDs())); // uuids |
527 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, | 537 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, |
528 device_ipc)); | 538 device_ipc)); |
| 539 request_device_sessions_.erase(session); |
529 return; | 540 return; |
530 } | 541 } |
531 } | 542 } |
532 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | 543 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
533 WebBluetoothError::NoDevicesFound)); | 544 WebBluetoothError::NoDevicesFound)); |
| 545 request_device_sessions_.erase(session); |
534 } | 546 } |
535 | 547 |
536 void BluetoothDispatcherHost::OnDiscoverySessionStoppedError(int thread_id, | 548 void BluetoothDispatcherHost::OnDiscoverySessionStoppedError(int thread_id, |
537 int request_id) { | 549 int request_id) { |
538 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 550 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
539 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStoppedError"; | 551 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStoppedError"; |
540 Send(new BluetoothMsg_RequestDeviceError( | 552 Send(new BluetoothMsg_RequestDeviceError( |
541 thread_id, request_id, WebBluetoothError::DiscoverySessionStopFailed)); | 553 thread_id, request_id, WebBluetoothError::DiscoverySessionStopFailed)); |
| 554 request_device_sessions_.erase(std::make_pair(thread_id, request_id)); |
542 } | 555 } |
543 | 556 |
544 void BluetoothDispatcherHost::OnGATTConnectionCreated( | 557 void BluetoothDispatcherHost::OnGATTConnectionCreated( |
545 int thread_id, | 558 int thread_id, |
546 int request_id, | 559 int request_id, |
547 const std::string& device_instance_id, | 560 const std::string& device_instance_id, |
548 scoped_ptr<device::BluetoothGattConnection> connection) { | 561 scoped_ptr<device::BluetoothGattConnection> connection) { |
549 // TODO(ortuno): Save the BluetoothGattConnection so we can disconnect | 562 // TODO(ortuno): Save the BluetoothGattConnection so we can disconnect |
550 // from it. | 563 // from it. |
551 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, | 564 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 | 634 |
622 void BluetoothDispatcherHost::OnWriteValueFailed( | 635 void BluetoothDispatcherHost::OnWriteValueFailed( |
623 int thread_id, | 636 int thread_id, |
624 int request_id, | 637 int request_id, |
625 device::BluetoothGattService::GattErrorCode error_code) { | 638 device::BluetoothGattService::GattErrorCode error_code) { |
626 Send(new BluetoothMsg_WriteCharacteristicValueError( | 639 Send(new BluetoothMsg_WriteCharacteristicValueError( |
627 thread_id, request_id, TranslateGATTError(error_code))); | 640 thread_id, request_id, TranslateGATTError(error_code))); |
628 } | 641 } |
629 | 642 |
630 } // namespace content | 643 } // namespace content |
OLD | NEW |