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::StartDiscovery(RequestDeviceSession* session, |
| 279 int chooser_id) { |
| 280 if (session->discovery_session) { |
| 281 // Already running; just increase the timeout. |
| 282 discovery_session_timer_.Reset(); |
| 283 } else { |
| 284 session->chooser->ShowDiscoveryState( |
| 285 BluetoothChooser::DiscoveryState::DISCOVERING); |
| 286 adapter_->StartDiscoverySessionWithFilter( |
| 287 session->ComputeScanFilter(), |
| 288 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, |
| 289 weak_ptr_factory_.GetWeakPtr(), chooser_id), |
| 290 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError, |
| 291 weak_ptr_factory_.GetWeakPtr(), chooser_id)); |
| 292 } |
| 293 } |
| 294 |
264 void BluetoothDispatcherHost::StopDeviceDiscovery() { | 295 void BluetoothDispatcherHost::StopDeviceDiscovery() { |
265 for (IDMap<RequestDeviceSession, IDMapOwnPointer>::iterator iter( | 296 for (IDMap<RequestDeviceSession, IDMapOwnPointer>::iterator iter( |
266 &request_device_sessions_); | 297 &request_device_sessions_); |
267 !iter.IsAtEnd(); iter.Advance()) { | 298 !iter.IsAtEnd(); iter.Advance()) { |
268 RequestDeviceSession* session = iter.GetCurrentValue(); | 299 RequestDeviceSession* session = iter.GetCurrentValue(); |
269 if (session->discovery_session) { | 300 if (session->discovery_session) { |
270 StopDiscoverySession(session->discovery_session.Pass()); | 301 StopDiscoverySession(session->discovery_session.Pass()); |
271 } | 302 } |
272 if (session->chooser) { | 303 if (session->chooser) { |
273 session->chooser->ShowDiscoveryState( | 304 session->chooser->ShowDiscoveryState( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 for (IDMap<RequestDeviceSession, IDMapOwnPointer>::iterator iter( | 339 for (IDMap<RequestDeviceSession, IDMapOwnPointer>::iterator iter( |
309 &request_device_sessions_); | 340 &request_device_sessions_); |
310 !iter.IsAtEnd(); iter.Advance()) { | 341 !iter.IsAtEnd(); iter.Advance()) { |
311 RequestDeviceSession* session = iter.GetCurrentValue(); | 342 RequestDeviceSession* session = iter.GetCurrentValue(); |
312 if (session->chooser) { | 343 if (session->chooser) { |
313 session->chooser->RemoveDevice(device->GetAddress()); | 344 session->chooser->RemoveDevice(device->GetAddress()); |
314 } | 345 } |
315 } | 346 } |
316 } | 347 } |
317 | 348 |
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( | 349 void BluetoothDispatcherHost::OnRequestDevice( |
334 int thread_id, | 350 int thread_id, |
335 int request_id, | 351 int request_id, |
336 int frame_routing_id, | 352 int frame_routing_id, |
337 const std::vector<BluetoothScanFilter>& filters, | 353 const std::vector<BluetoothScanFilter>& filters, |
338 const std::vector<BluetoothUUID>& optional_services) { | 354 const std::vector<BluetoothUUID>& optional_services) { |
339 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 355 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
340 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); | 356 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); |
341 RecordRequestDeviceArguments(filters, optional_services); | 357 RecordRequestDeviceArguments(filters, optional_services); |
342 | 358 |
(...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. | 434 // If the dialog's closing, no need to do any of the rest of this. |
419 return; | 435 return; |
420 } | 436 } |
421 | 437 |
422 if (!adapter_->IsPowered()) { | 438 if (!adapter_->IsPowered()) { |
423 session->chooser->SetAdapterPresence( | 439 session->chooser->SetAdapterPresence( |
424 BluetoothChooser::AdapterPresence::POWERED_OFF); | 440 BluetoothChooser::AdapterPresence::POWERED_OFF); |
425 return; | 441 return; |
426 } | 442 } |
427 | 443 |
428 // Redundant with the chooser's default; just to be clear: | 444 StartDiscovery(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 } | 445 } |
438 | 446 |
439 void BluetoothDispatcherHost::OnConnectGATT( | 447 void BluetoothDispatcherHost::OnConnectGATT( |
440 int thread_id, | 448 int thread_id, |
441 int request_id, | 449 int request_id, |
442 const std::string& device_instance_id) { | 450 const std::string& device_instance_id) { |
443 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 451 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
444 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::CONNECT_GATT); | 452 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::CONNECT_GATT); |
445 const base::TimeTicks start_time = base::TimeTicks::Now(); | 453 const base::TimeTicks start_time = base::TimeTicks::Now(); |
446 | 454 |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
714 } | 722 } |
715 } | 723 } |
716 // Ignore discovery session start errors when the dialog was already closed by | 724 // Ignore discovery session start errors when the dialog was already closed by |
717 // the time they happen. | 725 // the time they happen. |
718 } | 726 } |
719 | 727 |
720 void BluetoothDispatcherHost::OnBluetoothChooserEvent( | 728 void BluetoothDispatcherHost::OnBluetoothChooserEvent( |
721 int chooser_id, | 729 int chooser_id, |
722 BluetoothChooser::Event event, | 730 BluetoothChooser::Event event, |
723 const std::string& device_id) { | 731 const std::string& device_id) { |
| 732 RequestDeviceSession* session = request_device_sessions_.Lookup(chooser_id); |
| 733 DCHECK(session) << "Shouldn't receive an event (" << static_cast<int>(event) |
| 734 << ") from a closed chooser."; |
| 735 CHECK(session->chooser) << "Shouldn't receive an event (" |
| 736 << static_cast<int>(event) |
| 737 << ") from a closed chooser."; |
724 switch (event) { | 738 switch (event) { |
725 case BluetoothChooser::Event::RESCAN: | 739 case BluetoothChooser::Event::RESCAN: |
726 // TODO(jyasskin): Implement starting a new Bluetooth discovery session. | 740 StartDiscovery(session, chooser_id); |
727 NOTIMPLEMENTED(); | |
728 break; | 741 break; |
729 case BluetoothChooser::Event::CANCELLED: | 742 case BluetoothChooser::Event::CANCELLED: |
730 case BluetoothChooser::Event::SELECTED: { | 743 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 | 744 // Synchronously ensure nothing else calls into the chooser after it has |
737 // asked to be closed. | 745 // asked to be closed. |
738 session->chooser.reset(); | 746 session->chooser.reset(); |
739 | 747 |
740 // Yield to the event loop to make sure we don't destroy the session | 748 // Yield to the event loop to make sure we don't destroy the session |
741 // within a BluetoothDispatcherHost stack frame. | 749 // within a BluetoothDispatcherHost stack frame. |
742 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | 750 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
743 FROM_HERE, | 751 FROM_HERE, |
744 base::Bind(&BluetoothDispatcherHost::FinishClosingChooser, | 752 base::Bind(&BluetoothDispatcherHost::FinishClosingChooser, |
745 weak_ptr_factory_.GetWeakPtr(), chooser_id, event, | 753 weak_ptr_factory_.GetWeakPtr(), chooser_id, event, |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
920 | 928 |
921 void BluetoothDispatcherHost::ShowBluetoothPairingLink() { | 929 void BluetoothDispatcherHost::ShowBluetoothPairingLink() { |
922 NOTIMPLEMENTED(); | 930 NOTIMPLEMENTED(); |
923 } | 931 } |
924 | 932 |
925 void BluetoothDispatcherHost::ShowBluetoothAdapterOffLink() { | 933 void BluetoothDispatcherHost::ShowBluetoothAdapterOffLink() { |
926 NOTIMPLEMENTED(); | 934 NOTIMPLEMENTED(); |
927 } | 935 } |
928 | 936 |
929 } // namespace content | 937 } // namespace content |
OLD | NEW |