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 struct BluetoothDispatcherHost::RequestDeviceSession { |
190 DiscoverySessionOptions(const std::vector<BluetoothScanFilter>& filters, | 190 RequestDeviceSession(const std::vector<BluetoothScanFilter>& filters, |
191 const std::vector<BluetoothUUID>& optional_services) | 191 const std::vector<BluetoothUUID>& optional_services) |
192 : filters(filters), optional_services(optional_services) {} | 192 : filters(filters), optional_services(optional_services) {} |
193 | 193 |
194 std::vector<BluetoothScanFilter> filters; | 194 std::vector<BluetoothScanFilter> filters; |
195 std::vector<BluetoothUUID> optional_services; | 195 std::vector<BluetoothUUID> optional_services; |
196 }; | 196 }; |
197 | 197 |
198 void BluetoothDispatcherHost::set_adapter( | 198 void BluetoothDispatcherHost::set_adapter( |
199 scoped_refptr<device::BluetoothAdapter> adapter) { | 199 scoped_refptr<device::BluetoothAdapter> adapter) { |
200 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 200 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
201 if (adapter_.get()) | 201 if (adapter_.get()) |
(...skipping 20 matching lines...) Expand all Loading... | |
222 | 222 |
223 void BluetoothDispatcherHost::OnRequestDevice( | 223 void BluetoothDispatcherHost::OnRequestDevice( |
224 int thread_id, | 224 int thread_id, |
225 int request_id, | 225 int request_id, |
226 const std::vector<BluetoothScanFilter>& filters, | 226 const std::vector<BluetoothScanFilter>& filters, |
227 const std::vector<BluetoothUUID>& optional_services) { | 227 const std::vector<BluetoothUUID>& optional_services) { |
228 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 228 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
229 // TODO(scheib): Device selection UI: crbug.com/436280 | 229 // TODO(scheib): Device selection UI: crbug.com/436280 |
230 // TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed. | 230 // TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed. |
231 if (adapter_.get()) { | 231 if (adapter_.get()) { |
232 if (!request_device_sessions_ | |
233 .insert(std::make_pair( | |
234 std::make_pair(thread_id, request_id), | |
ortuno
2015/07/15 22:39:12
Would an iframe and it's parent use the same web_b
Jeffrey Yasskin
2015/07/17 01:07:23
They'll use the same BluetoothDispatcher (since it
| |
235 RequestDeviceSession(filters, optional_services))) | |
236 .second) { | |
237 LOG(ERROR) << "2 requestDevice() calls with the same thread_id (" | |
238 << thread_id << ") and request_id (" << request_id | |
239 << ") shouldn't arrive at the same BluetoothDispatcherHost."; | |
240 bad_message::ReceivedBadMessage( | |
241 this, bad_message::BDH_DUPLICATE_REQUEST_DEVICE_ID); | |
242 } | |
232 adapter_->StartDiscoverySessionWithFilter( | 243 adapter_->StartDiscoverySessionWithFilter( |
233 ComputeScanFilter(filters), | 244 ComputeScanFilter(filters), |
234 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, | 245 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, |
235 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 246 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, | 247 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError, |
239 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); | 248 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
240 } else { | 249 } else { |
241 DLOG(WARNING) << "No BluetoothAdapter. Can't serve requestDevice."; | 250 DLOG(WARNING) << "No BluetoothAdapter. Can't serve requestDevice."; |
242 Send(new BluetoothMsg_RequestDeviceError( | 251 Send(new BluetoothMsg_RequestDeviceError( |
243 thread_id, request_id, WebBluetoothError::NoBluetoothAdapter)); | 252 thread_id, request_id, WebBluetoothError::NoBluetoothAdapter)); |
244 } | 253 } |
245 return; | 254 return; |
246 } | 255 } |
247 | 256 |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
465 characteristic->WriteRemoteCharacteristic( | 474 characteristic->WriteRemoteCharacteristic( |
466 value, base::Bind(&BluetoothDispatcherHost::OnWriteValueSuccess, | 475 value, base::Bind(&BluetoothDispatcherHost::OnWriteValueSuccess, |
467 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), | 476 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), |
468 base::Bind(&BluetoothDispatcherHost::OnWriteValueFailed, | 477 base::Bind(&BluetoothDispatcherHost::OnWriteValueFailed, |
469 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); | 478 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
470 } | 479 } |
471 | 480 |
472 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 481 void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
473 int thread_id, | 482 int thread_id, |
474 int request_id, | 483 int request_id, |
475 scoped_ptr<DiscoverySessionOptions> options, | |
476 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 484 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
477 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 485 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
478 BrowserThread::PostDelayedTask( | 486 BrowserThread::PostDelayedTask( |
479 BrowserThread::UI, FROM_HERE, | 487 BrowserThread::UI, FROM_HERE, |
480 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, | 488 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, |
481 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 489 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
482 base::Passed(&options), base::Passed(&discovery_session)), | 490 base::Passed(&discovery_session)), |
483 base::TimeDelta::FromSeconds(current_delay_time_)); | 491 base::TimeDelta::FromSeconds(current_delay_time_)); |
484 } | 492 } |
485 | 493 |
486 void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id, | 494 void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id, |
487 int request_id) { | 495 int request_id) { |
488 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 496 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
489 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStartedError"; | 497 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStartedError"; |
490 Send(new BluetoothMsg_RequestDeviceError( | 498 Send(new BluetoothMsg_RequestDeviceError( |
491 thread_id, request_id, WebBluetoothError::DiscoverySessionStartFailed)); | 499 thread_id, request_id, WebBluetoothError::DiscoverySessionStartFailed)); |
500 request_device_sessions_.erase(std::make_pair(thread_id, request_id)); | |
492 } | 501 } |
493 | 502 |
494 void BluetoothDispatcherHost::StopDiscoverySession( | 503 void BluetoothDispatcherHost::StopDiscoverySession( |
495 int thread_id, | 504 int thread_id, |
496 int request_id, | 505 int request_id, |
497 scoped_ptr<DiscoverySessionOptions> options, | |
498 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 506 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
499 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 507 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
500 discovery_session->Stop( | 508 discovery_session->Stop( |
501 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStopped, | 509 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStopped, |
502 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 510 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), |
503 base::Passed(&options)), | |
504 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStoppedError, | 511 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStoppedError, |
505 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); | 512 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
506 } | 513 } |
507 | 514 |
508 void BluetoothDispatcherHost::OnDiscoverySessionStopped( | 515 void BluetoothDispatcherHost::OnDiscoverySessionStopped(int thread_id, |
509 int thread_id, | 516 int request_id) { |
510 int request_id, | |
511 scoped_ptr<DiscoverySessionOptions> options) { | |
512 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 517 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
518 auto session = | |
519 request_device_sessions_.find(std::make_pair(thread_id, request_id)); | |
ortuno
2015/07/15 22:39:12
We use thread_id + request_id everywhere. What do
Jeffrey Yasskin
2015/07/17 01:07:23
It'd make sense to do that, but I'd rather do it a
| |
520 CHECK(session != request_device_sessions_.end()); | |
513 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | 521 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
514 for (device::BluetoothDevice* device : devices) { | 522 for (device::BluetoothDevice* device : devices) { |
515 if (MatchesFilters(*device, options->filters)) { | 523 if (MatchesFilters(*device, session->second.filters)) { |
516 content::BluetoothDevice device_ipc( | 524 content::BluetoothDevice device_ipc( |
517 device->GetAddress(), // instance_id | 525 device->GetAddress(), // instance_id |
518 device->GetName(), // name | 526 device->GetName(), // name |
519 device->GetBluetoothClass(), // device_class | 527 device->GetBluetoothClass(), // device_class |
520 device->GetVendorIDSource(), // vendor_id_source | 528 device->GetVendorIDSource(), // vendor_id_source |
521 device->GetVendorID(), // vendor_id | 529 device->GetVendorID(), // vendor_id |
522 device->GetProductID(), // product_id | 530 device->GetProductID(), // product_id |
523 device->GetDeviceID(), // product_version | 531 device->GetDeviceID(), // product_version |
524 device->IsPaired(), // paired | 532 device->IsPaired(), // paired |
525 content::BluetoothDevice::UUIDsFromBluetoothUUIDs( | 533 content::BluetoothDevice::UUIDsFromBluetoothUUIDs( |
526 device->GetUUIDs())); // uuids | 534 device->GetUUIDs())); // uuids |
527 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, | 535 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, |
528 device_ipc)); | 536 device_ipc)); |
537 request_device_sessions_.erase(session); | |
529 return; | 538 return; |
530 } | 539 } |
531 } | 540 } |
532 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | 541 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
533 WebBluetoothError::NoDevicesFound)); | 542 WebBluetoothError::NoDevicesFound)); |
543 request_device_sessions_.erase(session); | |
534 } | 544 } |
535 | 545 |
536 void BluetoothDispatcherHost::OnDiscoverySessionStoppedError(int thread_id, | 546 void BluetoothDispatcherHost::OnDiscoverySessionStoppedError(int thread_id, |
537 int request_id) { | 547 int request_id) { |
538 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 548 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
539 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStoppedError"; | 549 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStoppedError"; |
540 Send(new BluetoothMsg_RequestDeviceError( | 550 Send(new BluetoothMsg_RequestDeviceError( |
541 thread_id, request_id, WebBluetoothError::DiscoverySessionStopFailed)); | 551 thread_id, request_id, WebBluetoothError::DiscoverySessionStopFailed)); |
552 request_device_sessions_.erase(std::make_pair(thread_id, request_id)); | |
542 } | 553 } |
543 | 554 |
544 void BluetoothDispatcherHost::OnGATTConnectionCreated( | 555 void BluetoothDispatcherHost::OnGATTConnectionCreated( |
545 int thread_id, | 556 int thread_id, |
546 int request_id, | 557 int request_id, |
547 const std::string& device_instance_id, | 558 const std::string& device_instance_id, |
548 scoped_ptr<device::BluetoothGattConnection> connection) { | 559 scoped_ptr<device::BluetoothGattConnection> connection) { |
549 // TODO(ortuno): Save the BluetoothGattConnection so we can disconnect | 560 // TODO(ortuno): Save the BluetoothGattConnection so we can disconnect |
550 // from it. | 561 // from it. |
551 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, | 562 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
621 | 632 |
622 void BluetoothDispatcherHost::OnWriteValueFailed( | 633 void BluetoothDispatcherHost::OnWriteValueFailed( |
623 int thread_id, | 634 int thread_id, |
624 int request_id, | 635 int request_id, |
625 device::BluetoothGattService::GattErrorCode error_code) { | 636 device::BluetoothGattService::GattErrorCode error_code) { |
626 Send(new BluetoothMsg_WriteCharacteristicValueError( | 637 Send(new BluetoothMsg_WriteCharacteristicValueError( |
627 thread_id, request_id, TranslateGATTError(error_code))); | 638 thread_id, request_id, TranslateGATTError(error_code))); |
628 } | 639 } |
629 | 640 |
630 } // namespace content | 641 } // namespace content |
OLD | NEW |