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 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 request_id(request_id), | 236 request_id(request_id), |
237 filters(filters), | 237 filters(filters), |
238 optional_services(optional_services) {} | 238 optional_services(optional_services) {} |
239 | 239 |
240 void AddFilteredDevice(const device::BluetoothDevice& device) { | 240 void AddFilteredDevice(const device::BluetoothDevice& device) { |
241 if (chooser && MatchesFilters(device, filters)) { | 241 if (chooser && MatchesFilters(device, filters)) { |
242 chooser->AddDevice(device.GetAddress(), device.GetName()); | 242 chooser->AddDevice(device.GetAddress(), device.GetName()); |
243 } | 243 } |
244 } | 244 } |
245 | 245 |
| 246 scoped_ptr<device::BluetoothDiscoveryFilter> ComputeScanFilter() const { |
| 247 std::set<BluetoothUUID> services; |
| 248 for (const BluetoothScanFilter& filter : filters) { |
| 249 services.insert(filter.services.begin(), filter.services.end()); |
| 250 } |
| 251 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter( |
| 252 new device::BluetoothDiscoveryFilter( |
| 253 device::BluetoothDiscoveryFilter::TRANSPORT_DUAL)); |
| 254 for (const BluetoothUUID& service : services) { |
| 255 discovery_filter->AddUUID(service); |
| 256 } |
| 257 return discovery_filter.Pass(); |
| 258 } |
| 259 |
246 const int thread_id; | 260 const int thread_id; |
247 const int request_id; | 261 const int request_id; |
248 const std::vector<BluetoothScanFilter> filters; | 262 const std::vector<BluetoothScanFilter> filters; |
249 const std::vector<BluetoothUUID> optional_services; | 263 const std::vector<BluetoothUUID> optional_services; |
250 scoped_ptr<BluetoothChooser> chooser; | 264 scoped_ptr<BluetoothChooser> chooser; |
251 scoped_ptr<device::BluetoothDiscoverySession> discovery_session; | 265 scoped_ptr<device::BluetoothDiscoverySession> discovery_session; |
252 }; | 266 }; |
253 | 267 |
254 void BluetoothDispatcherHost::set_adapter( | 268 void BluetoothDispatcherHost::set_adapter( |
255 scoped_refptr<device::BluetoothAdapter> adapter) { | 269 scoped_refptr<device::BluetoothAdapter> adapter) { |
256 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 270 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
257 if (adapter_.get()) | 271 if (adapter_.get()) |
258 adapter_->RemoveObserver(this); | 272 adapter_->RemoveObserver(this); |
259 adapter_ = adapter; | 273 adapter_ = adapter; |
260 if (adapter_.get()) | 274 if (adapter_.get()) |
261 adapter_->AddObserver(this); | 275 adapter_->AddObserver(this); |
262 } | 276 } |
263 | 277 |
| 278 void BluetoothDispatcherHost::StartDeviceDiscovery( |
| 279 RequestDeviceSession* session, |
| 280 int chooser_id) { |
| 281 if (session->discovery_session) { |
| 282 // Already running; just increase the timeout. |
| 283 discovery_session_timer_.Reset(); |
| 284 } else { |
| 285 session->chooser->ShowDiscoveryState( |
| 286 BluetoothChooser::DiscoveryState::DISCOVERING); |
| 287 adapter_->StartDiscoverySessionWithFilter( |
| 288 session->ComputeScanFilter(), |
| 289 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, |
| 290 weak_ptr_factory_.GetWeakPtr(), chooser_id), |
| 291 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError, |
| 292 weak_ptr_factory_.GetWeakPtr(), chooser_id)); |
| 293 } |
| 294 } |
| 295 |
264 void BluetoothDispatcherHost::StopDeviceDiscovery() { | 296 void BluetoothDispatcherHost::StopDeviceDiscovery() { |
265 for (IDMap<RequestDeviceSession, IDMapOwnPointer>::iterator iter( | 297 for (IDMap<RequestDeviceSession, IDMapOwnPointer>::iterator iter( |
266 &request_device_sessions_); | 298 &request_device_sessions_); |
267 !iter.IsAtEnd(); iter.Advance()) { | 299 !iter.IsAtEnd(); iter.Advance()) { |
268 RequestDeviceSession* session = iter.GetCurrentValue(); | 300 RequestDeviceSession* session = iter.GetCurrentValue(); |
269 if (session->discovery_session) { | 301 if (session->discovery_session) { |
270 StopDiscoverySession(session->discovery_session.Pass()); | 302 StopDiscoverySession(session->discovery_session.Pass()); |
271 } | 303 } |
272 if (session->chooser) { | 304 if (session->chooser) { |
273 session->chooser->ShowDiscoveryState( | 305 session->chooser->ShowDiscoveryState( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 for (IDMap<RequestDeviceSession, IDMapOwnPointer>::iterator iter( | 340 for (IDMap<RequestDeviceSession, IDMapOwnPointer>::iterator iter( |
309 &request_device_sessions_); | 341 &request_device_sessions_); |
310 !iter.IsAtEnd(); iter.Advance()) { | 342 !iter.IsAtEnd(); iter.Advance()) { |
311 RequestDeviceSession* session = iter.GetCurrentValue(); | 343 RequestDeviceSession* session = iter.GetCurrentValue(); |
312 if (session->chooser) { | 344 if (session->chooser) { |
313 session->chooser->RemoveDevice(device->GetAddress()); | 345 session->chooser->RemoveDevice(device->GetAddress()); |
314 } | 346 } |
315 } | 347 } |
316 } | 348 } |
317 | 349 |
318 static scoped_ptr<device::BluetoothDiscoveryFilter> ComputeScanFilter( | |
319 const std::vector<BluetoothScanFilter>& filters) { | |
320 std::set<BluetoothUUID> services; | |
321 for (const BluetoothScanFilter& filter : filters) { | |
322 services.insert(filter.services.begin(), filter.services.end()); | |
323 } | |
324 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter( | |
325 new device::BluetoothDiscoveryFilter( | |
326 device::BluetoothDiscoveryFilter::TRANSPORT_DUAL)); | |
327 for (const BluetoothUUID& service : services) { | |
328 discovery_filter->AddUUID(service); | |
329 } | |
330 return discovery_filter.Pass(); | |
331 } | |
332 | |
333 void BluetoothDispatcherHost::OnRequestDevice( | 350 void BluetoothDispatcherHost::OnRequestDevice( |
334 int thread_id, | 351 int thread_id, |
335 int request_id, | 352 int request_id, |
336 int frame_routing_id, | 353 int frame_routing_id, |
337 const std::vector<BluetoothScanFilter>& filters, | 354 const std::vector<BluetoothScanFilter>& filters, |
338 const std::vector<BluetoothUUID>& optional_services) { | 355 const std::vector<BluetoothUUID>& optional_services) { |
339 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 356 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
340 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); | 357 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); |
341 RecordRequestDeviceArguments(filters, optional_services); | 358 RecordRequestDeviceArguments(filters, optional_services); |
342 | 359 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 // If the dialog's closing, no need to do any of the rest of this. | 435 // If the dialog's closing, no need to do any of the rest of this. |
419 return; | 436 return; |
420 } | 437 } |
421 | 438 |
422 if (!adapter_->IsPowered()) { | 439 if (!adapter_->IsPowered()) { |
423 session->chooser->SetAdapterPresence( | 440 session->chooser->SetAdapterPresence( |
424 BluetoothChooser::AdapterPresence::POWERED_OFF); | 441 BluetoothChooser::AdapterPresence::POWERED_OFF); |
425 return; | 442 return; |
426 } | 443 } |
427 | 444 |
428 // Redundant with the chooser's default; just to be clear: | 445 StartDeviceDiscovery(session, chooser_id); |
429 session->chooser->ShowDiscoveryState( | |
430 BluetoothChooser::DiscoveryState::DISCOVERING); | |
431 adapter_->StartDiscoverySessionWithFilter( | |
432 ComputeScanFilter(filters), | |
433 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, | |
434 weak_ptr_factory_.GetWeakPtr(), chooser_id), | |
435 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError, | |
436 weak_ptr_factory_.GetWeakPtr(), chooser_id)); | |
437 } | 446 } |
438 | 447 |
439 void BluetoothDispatcherHost::OnConnectGATT( | 448 void BluetoothDispatcherHost::OnConnectGATT( |
440 int thread_id, | 449 int thread_id, |
441 int request_id, | 450 int request_id, |
442 const std::string& device_instance_id) { | 451 const std::string& device_instance_id) { |
443 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 452 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
444 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::CONNECT_GATT); | 453 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::CONNECT_GATT); |
445 const base::TimeTicks start_time = base::TimeTicks::Now(); | 454 const base::TimeTicks start_time = base::TimeTicks::Now(); |
446 | 455 |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
714 } | 723 } |
715 } | 724 } |
716 // Ignore discovery session start errors when the dialog was already closed by | 725 // Ignore discovery session start errors when the dialog was already closed by |
717 // the time they happen. | 726 // the time they happen. |
718 } | 727 } |
719 | 728 |
720 void BluetoothDispatcherHost::OnBluetoothChooserEvent( | 729 void BluetoothDispatcherHost::OnBluetoothChooserEvent( |
721 int chooser_id, | 730 int chooser_id, |
722 BluetoothChooser::Event event, | 731 BluetoothChooser::Event event, |
723 const std::string& device_id) { | 732 const std::string& device_id) { |
| 733 RequestDeviceSession* session = request_device_sessions_.Lookup(chooser_id); |
| 734 DCHECK(session) << "Shouldn't receive an event (" << static_cast<int>(event) |
| 735 << ") from a closed chooser."; |
| 736 CHECK(session->chooser) << "Shouldn't receive an event (" |
| 737 << static_cast<int>(event) |
| 738 << ") from a closed chooser."; |
724 switch (event) { | 739 switch (event) { |
725 case BluetoothChooser::Event::RESCAN: | 740 case BluetoothChooser::Event::RESCAN: |
726 // TODO(jyasskin): Implement starting a new Bluetooth discovery session. | 741 StartDeviceDiscovery(session, chooser_id); |
727 NOTIMPLEMENTED(); | |
728 break; | 742 break; |
729 case BluetoothChooser::Event::CANCELLED: | 743 case BluetoothChooser::Event::CANCELLED: |
730 case BluetoothChooser::Event::SELECTED: { | 744 case BluetoothChooser::Event::SELECTED: { |
731 RequestDeviceSession* session = | |
732 request_device_sessions_.Lookup(chooser_id); | |
733 DCHECK(session) << "Shouldn't close the dialog twice."; | |
734 CHECK(session->chooser) << "Shouldn't close the dialog twice."; | |
735 | |
736 // Synchronously ensure nothing else calls into the chooser after it has | 745 // Synchronously ensure nothing else calls into the chooser after it has |
737 // asked to be closed. | 746 // asked to be closed. |
738 session->chooser.reset(); | 747 session->chooser.reset(); |
739 | 748 |
740 // Yield to the event loop to make sure we don't destroy the session | 749 // Yield to the event loop to make sure we don't destroy the session |
741 // within a BluetoothDispatcherHost stack frame. | 750 // within a BluetoothDispatcherHost stack frame. |
742 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | 751 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
743 FROM_HERE, | 752 FROM_HERE, |
744 base::Bind(&BluetoothDispatcherHost::FinishClosingChooser, | 753 base::Bind(&BluetoothDispatcherHost::FinishClosingChooser, |
745 weak_ptr_factory_.GetWeakPtr(), chooser_id, event, | 754 weak_ptr_factory_.GetWeakPtr(), chooser_id, event, |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
920 | 929 |
921 void BluetoothDispatcherHost::ShowBluetoothPairingLink() { | 930 void BluetoothDispatcherHost::ShowBluetoothPairingLink() { |
922 NOTIMPLEMENTED(); | 931 NOTIMPLEMENTED(); |
923 } | 932 } |
924 | 933 |
925 void BluetoothDispatcherHost::ShowBluetoothAdapterOffLink() { | 934 void BluetoothDispatcherHost::ShowBluetoothAdapterOffLink() { |
926 NOTIMPLEMENTED(); | 935 NOTIMPLEMENTED(); |
927 } | 936 } |
928 | 937 |
929 } // namespace content | 938 } // namespace content |
OLD | NEW |