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 // 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. |
11 | 11 |
12 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" | 12 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" |
13 | 13 |
14 #include <stddef.h> | 14 #include <stddef.h> |
15 | 15 |
16 #include <utility> | 16 #include <utility> |
17 | 17 |
18 #include "base/bind.h" | 18 #include "base/bind.h" |
19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
21 #include "base/thread_task_runner_handle.h" | 21 #include "base/thread_task_runner_handle.h" |
22 #include "content/browser/bad_message.h" | 22 #include "content/browser/bad_message.h" |
23 #include "content/browser/bluetooth/bluetooth_metrics.h" | 23 #include "content/browser/bluetooth/bluetooth_metrics.h" |
24 #include "content/browser/bluetooth/first_device_bluetooth_chooser.h" | 24 #include "content/browser/bluetooth/first_device_bluetooth_chooser.h" |
25 #include "content/browser/frame_host/render_frame_host_impl.h" | 25 #include "content/browser/frame_host/render_frame_host_impl.h" |
26 #include "content/common/bluetooth/bluetooth_messages.h" | 26 #include "content/common/bluetooth/bluetooth_messages.h" |
| 27 #include "content/public/browser/browser_context.h" |
| 28 #include "content/public/browser/chooser_permission_manager.h" |
| 29 #include "content/public/browser/permission_type.h" |
27 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
28 #include "content/public/browser/web_contents_delegate.h" | 31 #include "content/public/browser/web_contents_delegate.h" |
29 #include "device/bluetooth/bluetooth_adapter.h" | 32 #include "device/bluetooth/bluetooth_adapter.h" |
30 #include "device/bluetooth/bluetooth_adapter_factory.h" | 33 #include "device/bluetooth/bluetooth_adapter_factory.h" |
31 #include "device/bluetooth/bluetooth_device.h" | 34 #include "device/bluetooth/bluetooth_device.h" |
32 #include "device/bluetooth/bluetooth_discovery_session.h" | 35 #include "device/bluetooth/bluetooth_discovery_session.h" |
33 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | 36 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
34 #include "device/bluetooth/bluetooth_gatt_service.h" | 37 #include "device/bluetooth/bluetooth_gatt_service.h" |
35 | 38 |
36 using blink::WebBluetoothError; | 39 using blink::WebBluetoothError; |
37 using device::BluetoothAdapter; | 40 using device::BluetoothAdapter; |
38 using device::BluetoothAdapterFactory; | 41 using device::BluetoothAdapterFactory; |
39 using device::BluetoothGattCharacteristic; | 42 using device::BluetoothGattCharacteristic; |
40 using device::BluetoothGattService; | 43 using device::BluetoothGattService; |
41 using device::BluetoothUUID; | 44 using device::BluetoothUUID; |
42 | 45 |
| 46 namespace { |
| 47 |
| 48 const char kDeviceAddressKey[] = "device-address"; |
| 49 const char kDeviceNameKey[] = "device-name"; |
| 50 const char kDeviceUUIDsKey[] = "device-uuids"; |
| 51 |
| 52 } // namespace |
| 53 |
43 namespace content { | 54 namespace content { |
44 | 55 |
45 namespace { | 56 namespace { |
46 | 57 |
47 // TODO(ortuno): Once we have a chooser for scanning, a way to control that | 58 // TODO(ortuno): Once we have a chooser for scanning, a way to control that |
48 // chooser from tests, and the right callback for discovered services we should | 59 // chooser from tests, and the right callback for discovered services we should |
49 // delete these constants. | 60 // delete these constants. |
50 // https://crbug.com/436280 and https://crbug.com/484504 | 61 // https://crbug.com/436280 and https://crbug.com/484504 |
51 const int kDelayTime = 5; // 5 seconds for scanning and discovering | 62 const int kDelayTime = 5; // 5 seconds for scanning and discovering |
52 const int kTestingDelayTime = 0; // No need to wait during tests | 63 const int kTestingDelayTime = 0; // No need to wait during tests |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 // Clear adapter, releasing observer references. | 344 // Clear adapter, releasing observer references. |
334 set_adapter(scoped_refptr<device::BluetoothAdapter>()); | 345 set_adapter(scoped_refptr<device::BluetoothAdapter>()); |
335 } | 346 } |
336 | 347 |
337 // Stores information associated with an in-progress requestDevice call. This | 348 // Stores information associated with an in-progress requestDevice call. This |
338 // will include the state of the active chooser dialog in a future patch. | 349 // will include the state of the active chooser dialog in a future patch. |
339 struct BluetoothDispatcherHost::RequestDeviceSession { | 350 struct BluetoothDispatcherHost::RequestDeviceSession { |
340 public: | 351 public: |
341 RequestDeviceSession(int thread_id, | 352 RequestDeviceSession(int thread_id, |
342 int request_id, | 353 int request_id, |
| 354 ChooserPermissionManager* chooser_permission_manager, |
343 url::Origin origin, | 355 url::Origin origin, |
| 356 url::Origin embedding_origin, |
344 const std::vector<BluetoothScanFilter>& filters, | 357 const std::vector<BluetoothScanFilter>& filters, |
345 const std::vector<BluetoothUUID>& optional_services) | 358 const std::vector<BluetoothUUID>& optional_services) |
346 : thread_id(thread_id), | 359 : thread_id(thread_id), |
347 request_id(request_id), | 360 request_id(request_id), |
| 361 chooser_permission_manager(chooser_permission_manager), |
348 origin(origin), | 362 origin(origin), |
| 363 embedding_origin(embedding_origin), |
349 filters(filters), | 364 filters(filters), |
350 optional_services(optional_services) {} | 365 optional_services(optional_services) {} |
351 | 366 |
352 void AddFilteredDevice(const device::BluetoothDevice& device) { | 367 void AddFilteredDevice(const device::BluetoothDevice& device) { |
353 if (chooser && MatchesFilters(device, filters)) { | 368 if (chooser && MatchesFilters(device, filters)) { |
354 chooser->AddDevice(device.GetAddress(), device.GetName()); | 369 chooser->AddDevice(device.GetAddress(), device.GetName()); |
355 } | 370 } |
356 } | 371 } |
357 | 372 |
358 scoped_ptr<device::BluetoothDiscoveryFilter> ComputeScanFilter() const { | 373 scoped_ptr<device::BluetoothDiscoveryFilter> ComputeScanFilter() const { |
359 std::set<BluetoothUUID> services; | 374 std::set<BluetoothUUID> services; |
360 for (const BluetoothScanFilter& filter : filters) { | 375 for (const BluetoothScanFilter& filter : filters) { |
361 services.insert(filter.services.begin(), filter.services.end()); | 376 services.insert(filter.services.begin(), filter.services.end()); |
362 } | 377 } |
363 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter( | 378 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter( |
364 new device::BluetoothDiscoveryFilter( | 379 new device::BluetoothDiscoveryFilter( |
365 device::BluetoothDiscoveryFilter::TRANSPORT_DUAL)); | 380 device::BluetoothDiscoveryFilter::TRANSPORT_DUAL)); |
366 for (const BluetoothUUID& service : services) { | 381 for (const BluetoothUUID& service : services) { |
367 discovery_filter->AddUUID(service); | 382 discovery_filter->AddUUID(service); |
368 } | 383 } |
369 return discovery_filter; | 384 return discovery_filter; |
370 } | 385 } |
371 | 386 |
372 const int thread_id; | 387 const int thread_id; |
373 const int request_id; | 388 const int request_id; |
| 389 ChooserPermissionManager* chooser_permission_manager; |
374 const url::Origin origin; | 390 const url::Origin origin; |
| 391 const url::Origin embedding_origin; |
375 const std::vector<BluetoothScanFilter> filters; | 392 const std::vector<BluetoothScanFilter> filters; |
376 const std::vector<BluetoothUUID> optional_services; | 393 const std::vector<BluetoothUUID> optional_services; |
377 scoped_ptr<BluetoothChooser> chooser; | 394 scoped_ptr<BluetoothChooser> chooser; |
378 scoped_ptr<device::BluetoothDiscoverySession> discovery_session; | 395 scoped_ptr<device::BluetoothDiscoverySession> discovery_session; |
379 }; | 396 }; |
380 | 397 |
381 struct BluetoothDispatcherHost::CacheQueryResult { | 398 struct BluetoothDispatcherHost::CacheQueryResult { |
382 CacheQueryResult() | 399 CacheQueryResult() |
383 : device(nullptr), | 400 : device(nullptr), |
384 service(nullptr), | 401 service(nullptr), |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 return; | 668 return; |
652 } | 669 } |
653 | 670 |
654 // The renderer should never send empty filters. | 671 // The renderer should never send empty filters. |
655 if (HasEmptyOrInvalidFilter(filters)) { | 672 if (HasEmptyOrInvalidFilter(filters)) { |
656 bad_message::ReceivedBadMessage(this, | 673 bad_message::ReceivedBadMessage(this, |
657 bad_message::BDH_EMPTY_OR_INVALID_FILTERS); | 674 bad_message::BDH_EMPTY_OR_INVALID_FILTERS); |
658 return; | 675 return; |
659 } | 676 } |
660 | 677 |
| 678 WebContents* web_contents = |
| 679 WebContents::FromRenderFrameHost(render_frame_host); |
| 680 |
| 681 url::Origin embedding_origin; |
| 682 ChooserPermissionManager* chooser_permission_manager = nullptr; |
| 683 |
| 684 if (web_contents) { |
| 685 embedding_origin = web_contents->GetMainFrame()->GetLastCommittedOrigin(); |
| 686 chooser_permission_manager = |
| 687 web_contents->GetBrowserContext()->GetChooserPermissionManager(); |
| 688 } |
| 689 |
661 // Create storage for the information that backs the chooser, and show the | 690 // Create storage for the information that backs the chooser, and show the |
662 // chooser. | 691 // chooser. |
663 RequestDeviceSession* const session = new RequestDeviceSession( | 692 RequestDeviceSession* const session = new RequestDeviceSession( |
664 thread_id, request_id, render_frame_host->GetLastCommittedOrigin(), | 693 thread_id, request_id, chooser_permission_manager, |
665 filters, optional_services); | 694 render_frame_host->GetLastCommittedOrigin(), embedding_origin, filters, |
| 695 optional_services); |
666 int chooser_id = request_device_sessions_.Add(session); | 696 int chooser_id = request_device_sessions_.Add(session); |
667 | 697 |
668 BluetoothChooser::EventHandler chooser_event_handler = | 698 BluetoothChooser::EventHandler chooser_event_handler = |
669 base::Bind(&BluetoothDispatcherHost::OnBluetoothChooserEvent, | 699 base::Bind(&BluetoothDispatcherHost::OnBluetoothChooserEvent, |
670 weak_ptr_on_ui_thread_, chooser_id); | 700 weak_ptr_on_ui_thread_, chooser_id); |
671 if (WebContents* web_contents = | 701 if (web_contents) { |
672 WebContents::FromRenderFrameHost(render_frame_host)) { | |
673 if (WebContentsDelegate* delegate = web_contents->GetDelegate()) { | 702 if (WebContentsDelegate* delegate = web_contents->GetDelegate()) { |
674 session->chooser = delegate->RunBluetoothChooser( | 703 session->chooser = delegate->RunBluetoothChooser( |
675 web_contents, chooser_event_handler, | 704 web_contents, chooser_event_handler, |
676 // TODO(ortuno): Replace with GetLastCommittedOrigin. | 705 // TODO(ortuno): Replace with GetLastCommittedOrigin. |
677 // http://crbug.com/577451 | 706 // http://crbug.com/577451 |
678 render_frame_host->GetLastCommittedURL().GetOrigin()); | 707 render_frame_host->GetLastCommittedURL().GetOrigin()); |
679 } | 708 } |
680 } | 709 } |
681 if (!session->chooser) { | 710 if (!session->chooser) { |
682 LOG(WARNING) | 711 LOG(WARNING) |
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1186 WebBluetoothError::ChosenDeviceVanished)); | 1215 WebBluetoothError::ChosenDeviceVanished)); |
1187 request_device_sessions_.Remove(chooser_id); | 1216 request_device_sessions_.Remove(chooser_id); |
1188 return; | 1217 return; |
1189 } | 1218 } |
1190 | 1219 |
1191 VLOG(1) << "Device: " << device->GetName(); | 1220 VLOG(1) << "Device: " << device->GetName(); |
1192 VLOG(1) << "UUIDs: "; | 1221 VLOG(1) << "UUIDs: "; |
1193 for (BluetoothUUID uuid : device->GetUUIDs()) | 1222 for (BluetoothUUID uuid : device->GetUUIDs()) |
1194 VLOG(1) << "\t" << uuid.canonical_value(); | 1223 VLOG(1) << "\t" << uuid.canonical_value(); |
1195 | 1224 |
| 1225 if (session->chooser_permission_manager) { |
| 1226 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue()); |
| 1227 device_dict->SetString(kDeviceAddressKey, device_id); |
| 1228 device_dict->SetString(kDeviceNameKey, device->GetName()); |
| 1229 |
| 1230 // Compute the union of session->optional_services and session->filters. |
| 1231 std::set<device::BluetoothUUID> services(session->optional_services.begin(), |
| 1232 session->optional_services.end()); |
| 1233 for (const content::BluetoothScanFilter& filter : session->filters) { |
| 1234 services.insert(filter.services.begin(), filter.services.end()); |
| 1235 } |
| 1236 scoped_ptr<base::ListValue> device_uuids_list(new base::ListValue()); |
| 1237 device::BluetoothDevice::UUIDList uuid_list = device->GetUUIDs(); |
| 1238 for (const auto& uuid : uuid_list) { |
| 1239 if (services.find(uuid) != services.end()) |
| 1240 device_uuids_list->AppendString(uuid.value()); |
| 1241 } |
| 1242 device_dict->Set(kDeviceUUIDsKey, std::move(device_uuids_list)); |
| 1243 |
| 1244 session->chooser_permission_manager->GrantPermission( |
| 1245 content::PermissionType::BLUETOOTH, session->origin, |
| 1246 session->embedding_origin, std::move(device_dict)); |
| 1247 } |
| 1248 |
1196 const std::string& device_id_for_origin = allowed_devices_map_.AddDevice( | 1249 const std::string& device_id_for_origin = allowed_devices_map_.AddDevice( |
1197 session->origin, device->GetAddress(), session->filters, | 1250 session->origin, device->GetAddress(), session->filters, |
1198 session->optional_services); | 1251 session->optional_services); |
1199 | 1252 |
1200 content::BluetoothDevice device_ipc( | 1253 content::BluetoothDevice device_ipc( |
1201 device_id_for_origin, // id | 1254 device_id_for_origin, // id |
1202 device->GetName(), // name | 1255 device->GetName(), // name |
1203 content::BluetoothDevice::ValidatePower( | 1256 content::BluetoothDevice::ValidatePower( |
1204 device->GetInquiryTxPower()), // tx_power | 1257 device->GetInquiryTxPower()), // tx_power |
1205 content::BluetoothDevice::ValidatePower( | 1258 content::BluetoothDevice::ValidatePower( |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1458 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1511 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1459 NOTIMPLEMENTED(); | 1512 NOTIMPLEMENTED(); |
1460 } | 1513 } |
1461 | 1514 |
1462 void BluetoothDispatcherHost::ShowNeedLocationLink() { | 1515 void BluetoothDispatcherHost::ShowNeedLocationLink() { |
1463 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1516 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1464 NOTIMPLEMENTED(); | 1517 NOTIMPLEMENTED(); |
1465 } | 1518 } |
1466 | 1519 |
1467 } // namespace content | 1520 } // namespace content |
OLD | NEW |