| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_id_to_device_address_, | 7 // WebBluetoothServiceImpl map [service_id_to_device_address_, |
| 8 // characteristic_id_to_service_id_, descriptor_to_characteristic_] implies a | 8 // characteristic_id_to_service_id_, descriptor_to_characteristic_] implies a |
| 9 // hostile renderer because a renderer obtains the corresponding ID from this | 9 // hostile renderer because a renderer obtains the corresponding ID from this |
| 10 // class and it will be added to the map at that time. | 10 // class and it will be added to the map at that time. |
| 11 | 11 |
| 12 #include "content/browser/bluetooth/web_bluetooth_service_impl.h" | 12 #include "content/browser/bluetooth/web_bluetooth_service_impl.h" |
| 13 | 13 |
| 14 #include <algorithm> | 14 #include <algorithm> |
| 15 | 15 |
| 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "content/browser/bluetooth/bluetooth_adapter_factory_wrapper.h" |
| 17 #include "content/browser/bluetooth/bluetooth_blacklist.h" | 19 #include "content/browser/bluetooth/bluetooth_blacklist.h" |
| 18 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" | 20 #include "content/browser/bluetooth/bluetooth_device_chooser_controller.h" |
| 21 #include "content/browser/bluetooth/bluetooth_metrics.h" |
| 19 #include "content/browser/bluetooth/frame_connected_bluetooth_devices.h" | 22 #include "content/browser/bluetooth/frame_connected_bluetooth_devices.h" |
| 20 #include "content/browser/renderer_host/render_process_host_impl.h" | 23 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 24 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/navigation_handle.h" | 25 #include "content/public/browser/navigation_handle.h" |
| 22 #include "content/public/browser/render_frame_host.h" | 26 #include "content/public/browser/render_frame_host.h" |
| 23 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 24 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 28 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
| 25 | 29 |
| 26 namespace content { | 30 namespace content { |
| 27 | 31 |
| 28 namespace { | 32 namespace { |
| 29 | 33 |
| 30 blink::mojom::WebBluetoothError TranslateConnectErrorAndRecord( | 34 blink::mojom::WebBluetoothError TranslateConnectErrorAndRecord( |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 WebBluetoothServiceImpl::WebBluetoothServiceImpl( | 167 WebBluetoothServiceImpl::WebBluetoothServiceImpl( |
| 164 RenderFrameHost* render_frame_host, | 168 RenderFrameHost* render_frame_host, |
| 165 blink::mojom::WebBluetoothServiceRequest request) | 169 blink::mojom::WebBluetoothServiceRequest request) |
| 166 : WebContentsObserver(WebContents::FromRenderFrameHost(render_frame_host)), | 170 : WebContentsObserver(WebContents::FromRenderFrameHost(render_frame_host)), |
| 167 connected_devices_(new FrameConnectedBluetoothDevices(render_frame_host)), | 171 connected_devices_(new FrameConnectedBluetoothDevices(render_frame_host)), |
| 168 render_frame_host_(render_frame_host), | 172 render_frame_host_(render_frame_host), |
| 169 binding_(this, std::move(request)), | 173 binding_(this, std::move(request)), |
| 170 weak_ptr_factory_(this) { | 174 weak_ptr_factory_(this) { |
| 171 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 175 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 172 CHECK(web_contents()); | 176 CHECK(web_contents()); |
| 173 | |
| 174 GetBluetoothDispatcherHost()->AddAdapterObserver(this); | |
| 175 } | 177 } |
| 176 | 178 |
| 177 WebBluetoothServiceImpl::~WebBluetoothServiceImpl() { | 179 WebBluetoothServiceImpl::~WebBluetoothServiceImpl() { |
| 178 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 180 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 179 GetBluetoothDispatcherHost()->RemoveAdapterObserver(this); | 181 ClearState(); |
| 180 } | 182 } |
| 181 | 183 |
| 182 void WebBluetoothServiceImpl::SetClientConnectionErrorHandler( | 184 void WebBluetoothServiceImpl::SetClientConnectionErrorHandler( |
| 183 base::Closure closure) { | 185 base::Closure closure) { |
| 184 binding_.set_connection_error_handler(closure); | 186 binding_.set_connection_error_handler(closure); |
| 185 } | 187 } |
| 186 | 188 |
| 187 void WebBluetoothServiceImpl::DidFinishNavigation( | 189 void WebBluetoothServiceImpl::DidFinishNavigation( |
| 188 NavigationHandle* navigation_handle) { | 190 NavigationHandle* navigation_handle) { |
| 189 if (navigation_handle->HasCommitted() && | 191 if (navigation_handle->HasCommitted() && |
| 190 navigation_handle->GetRenderFrameHost() == render_frame_host_ && | 192 navigation_handle->GetRenderFrameHost() == render_frame_host_ && |
| 191 !navigation_handle->IsSamePage()) { | 193 !navigation_handle->IsSamePage()) { |
| 192 ClearState(); | 194 ClearState(); |
| 193 } | 195 } |
| 194 } | 196 } |
| 195 | 197 |
| 196 void WebBluetoothServiceImpl::AdapterPresentChanged( | 198 void WebBluetoothServiceImpl::AdapterPoweredChanged( |
| 197 device::BluetoothAdapter* adapter, | 199 device::BluetoothAdapter* adapter, |
| 198 bool present) { | 200 bool powered) { |
| 199 if (!present) { | 201 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 200 ClearState(); | 202 if (device_chooser_controller_.get()) { |
| 203 device_chooser_controller_->AdapterPoweredChanged(powered); |
| 201 } | 204 } |
| 202 } | 205 } |
| 203 | 206 |
| 207 void WebBluetoothServiceImpl::DeviceAdded(device::BluetoothAdapter* adapter, |
| 208 device::BluetoothDevice* device) { |
| 209 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 210 if (device_chooser_controller_.get()) { |
| 211 VLOG(1) << "Adding device to device chooser controller: " |
| 212 << device->GetAddress(); |
| 213 device_chooser_controller_->AddFilteredDevice(*device); |
| 214 } |
| 215 } |
| 216 |
| 204 void WebBluetoothServiceImpl::DeviceChanged(device::BluetoothAdapter* adapter, | 217 void WebBluetoothServiceImpl::DeviceChanged(device::BluetoothAdapter* adapter, |
| 205 device::BluetoothDevice* device) { | 218 device::BluetoothDevice* device) { |
| 206 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 219 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 207 if (!device->IsGattConnected() || !device->IsConnected()) { | 220 if (!device->IsGattConnected() || !device->IsConnected()) { |
| 208 std::string device_id = | 221 std::string device_id = |
| 209 connected_devices_->CloseConnectionToDeviceWithAddress( | 222 connected_devices_->CloseConnectionToDeviceWithAddress( |
| 210 device->GetAddress()); | 223 device->GetAddress()); |
| 211 if (!device_id.empty()) { | 224 if (!device_id.empty()) { |
| 212 if (client_) { | 225 if (client_) { |
| 213 client_->GattServerDisconnected(device_id); | 226 client_->GattServerDisconnected(device_id); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 characteristic_instance_id, mojo::Array<uint8_t>(std::move(value))); | 282 characteristic_instance_id, mojo::Array<uint8_t>(std::move(value))); |
| 270 } | 283 } |
| 271 } | 284 } |
| 272 | 285 |
| 273 void WebBluetoothServiceImpl::SetClient( | 286 void WebBluetoothServiceImpl::SetClient( |
| 274 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo client) { | 287 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo client) { |
| 275 DCHECK(!client_.get()); | 288 DCHECK(!client_.get()); |
| 276 client_.Bind(std::move(client)); | 289 client_.Bind(std::move(client)); |
| 277 } | 290 } |
| 278 | 291 |
| 292 void WebBluetoothServiceImpl::RequestDevice( |
| 293 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, |
| 294 const RequestDeviceCallback& callback) { |
| 295 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); |
| 296 RecordRequestDeviceOptions(options); |
| 297 |
| 298 if (!GetAdapter()) { |
| 299 if (GetBluetoothAdapterFactoryWrapper().IsBluetoothAdapterAvailable()) { |
| 300 GetBluetoothAdapterFactoryWrapper().AcquireAdapter( |
| 301 this, base::Bind(&WebBluetoothServiceImpl::RequestDeviceImpl, |
| 302 weak_ptr_factory_.GetWeakPtr(), |
| 303 base::Passed(std::move(options)), callback)); |
| 304 return; |
| 305 } |
| 306 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::NO_BLUETOOTH_ADAPTER); |
| 307 callback.Run(blink::mojom::WebBluetoothError::NO_BLUETOOTH_ADAPTER, |
| 308 nullptr /* device */); |
| 309 return; |
| 310 } |
| 311 RequestDeviceImpl(std::move(options), callback, GetAdapter()); |
| 312 } |
| 313 |
| 279 void WebBluetoothServiceImpl::RemoteServerConnect( | 314 void WebBluetoothServiceImpl::RemoteServerConnect( |
| 280 const mojo::String& device_id, | 315 const mojo::String& device_id, |
| 281 const RemoteServerConnectCallback& callback) { | 316 const RemoteServerConnectCallback& callback) { |
| 282 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 317 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 283 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::CONNECT_GATT); | 318 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::CONNECT_GATT); |
| 284 | 319 |
| 285 const CacheQueryResult query_result = | 320 const CacheQueryResult query_result = QueryCacheForDevice(device_id); |
| 286 GetBluetoothDispatcherHost()->QueryCacheForDevice(GetOrigin(), device_id); | |
| 287 | 321 |
| 288 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | 322 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { |
| 289 RecordConnectGATTOutcome(query_result.outcome); | 323 RecordConnectGATTOutcome(query_result.outcome); |
| 290 callback.Run(query_result.GetWebError()); | 324 callback.Run(query_result.GetWebError()); |
| 291 return; | 325 return; |
| 292 } | 326 } |
| 293 | 327 |
| 294 if (connected_devices_->IsConnectedToDeviceWithId(device_id)) { | 328 if (connected_devices_->IsConnectedToDeviceWithId(device_id)) { |
| 295 VLOG(1) << "Already connected."; | 329 VLOG(1) << "Already connected."; |
| 296 callback.Run(blink::mojom::WebBluetoothError::SUCCESS); | 330 callback.Run(blink::mojom::WebBluetoothError::SUCCESS); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 } | 363 } |
| 330 | 364 |
| 331 void WebBluetoothServiceImpl::RemoteServerGetPrimaryService( | 365 void WebBluetoothServiceImpl::RemoteServerGetPrimaryService( |
| 332 const mojo::String& device_id, | 366 const mojo::String& device_id, |
| 333 const mojo::String& service_uuid, | 367 const mojo::String& service_uuid, |
| 334 const RemoteServerGetPrimaryServiceCallback& callback) { | 368 const RemoteServerGetPrimaryServiceCallback& callback) { |
| 335 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 369 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 336 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::GET_PRIMARY_SERVICE); | 370 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::GET_PRIMARY_SERVICE); |
| 337 RecordGetPrimaryServiceService(device::BluetoothUUID(service_uuid)); | 371 RecordGetPrimaryServiceService(device::BluetoothUUID(service_uuid)); |
| 338 | 372 |
| 339 if (!GetBluetoothDispatcherHost() | 373 if (!allowed_devices_map_.IsOriginAllowedToAccessService( |
| 340 ->allowed_devices_map_.IsOriginAllowedToAccessService( | 374 GetOrigin(), device_id, service_uuid)) { |
| 341 GetOrigin(), device_id, service_uuid)) { | |
| 342 callback.Run(blink::mojom::WebBluetoothError::NOT_ALLOWED_TO_ACCESS_SERVICE, | 375 callback.Run(blink::mojom::WebBluetoothError::NOT_ALLOWED_TO_ACCESS_SERVICE, |
| 343 nullptr /* service */); | 376 nullptr /* service */); |
| 344 return; | 377 return; |
| 345 } | 378 } |
| 346 | 379 |
| 347 const CacheQueryResult query_result = | 380 const CacheQueryResult query_result = QueryCacheForDevice(device_id); |
| 348 GetBluetoothDispatcherHost()->QueryCacheForDevice(GetOrigin(), device_id); | |
| 349 | 381 |
| 350 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { | 382 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { |
| 351 binding_.Close(); | 383 binding_.Close(); |
| 352 return; | 384 return; |
| 353 } | 385 } |
| 354 | 386 |
| 355 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | 387 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { |
| 356 RecordGetPrimaryServiceOutcome(query_result.outcome); | 388 RecordGetPrimaryServiceOutcome(query_result.outcome); |
| 357 callback.Run(query_result.GetWebError(), nullptr /* service */); | 389 callback.Run(query_result.GetWebError(), nullptr /* service */); |
| 358 return; | 390 return; |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 // If the frame hasn't subscribed to notifications before we just | 639 // If the frame hasn't subscribed to notifications before we just |
| 608 // run the callback. | 640 // run the callback. |
| 609 callback.Run(); | 641 callback.Run(); |
| 610 return; | 642 return; |
| 611 } | 643 } |
| 612 notify_session_iter->second->Stop(base::Bind( | 644 notify_session_iter->second->Stop(base::Bind( |
| 613 &WebBluetoothServiceImpl::OnStopNotifySessionComplete, | 645 &WebBluetoothServiceImpl::OnStopNotifySessionComplete, |
| 614 weak_ptr_factory_.GetWeakPtr(), characteristic_instance_id, callback)); | 646 weak_ptr_factory_.GetWeakPtr(), characteristic_instance_id, callback)); |
| 615 } | 647 } |
| 616 | 648 |
| 649 void WebBluetoothServiceImpl::RequestDeviceImpl( |
| 650 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, |
| 651 const RequestDeviceCallback& callback, |
| 652 device::BluetoothAdapter* adapter) { |
| 653 // requestDevice() can only be called when processing a user-gesture and |
| 654 // any user gesture outside of a chooser should close the chooser so we should |
| 655 // never get a request with an open chooser. |
| 656 CHECK(!device_chooser_controller_.get()); |
| 657 |
| 658 device_chooser_controller_.reset(new BluetoothDeviceChooserController( |
| 659 this, render_frame_host_, adapter, |
| 660 GetBluetoothAdapterFactoryWrapper().GetScanDuration())); |
| 661 |
| 662 device_chooser_controller_->GetDevice( |
| 663 std::move(options), |
| 664 base::Bind(&WebBluetoothServiceImpl::OnGetDeviceSuccess, |
| 665 weak_ptr_factory_.GetWeakPtr(), callback), |
| 666 base::Bind(&WebBluetoothServiceImpl::OnGetDeviceFailed, |
| 667 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 668 } |
| 669 |
| 617 void WebBluetoothServiceImpl::RemoteServerGetPrimaryServiceImpl( | 670 void WebBluetoothServiceImpl::RemoteServerGetPrimaryServiceImpl( |
| 618 const std::string& service_uuid, | 671 const std::string& service_uuid, |
| 619 const RemoteServerGetPrimaryServiceCallback& callback, | 672 const RemoteServerGetPrimaryServiceCallback& callback, |
| 620 device::BluetoothDevice* device) { | 673 device::BluetoothDevice* device) { |
| 621 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 674 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 622 DCHECK(device->IsGattServicesDiscoveryComplete()); | 675 DCHECK(device->IsGattServicesDiscoveryComplete()); |
| 623 | 676 |
| 624 std::vector<device::BluetoothRemoteGattService*> services = | 677 std::vector<device::BluetoothRemoteGattService*> services = |
| 625 GetPrimaryServicesByUUID(device, service_uuid); | 678 GetPrimaryServicesByUUID(device, service_uuid); |
| 626 | 679 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 642 | 695 |
| 643 RecordGetPrimaryServiceOutcome(UMAGetPrimaryServiceOutcome::SUCCESS); | 696 RecordGetPrimaryServiceOutcome(UMAGetPrimaryServiceOutcome::SUCCESS); |
| 644 blink::mojom::WebBluetoothRemoteGATTServicePtr service_ptr = | 697 blink::mojom::WebBluetoothRemoteGATTServicePtr service_ptr = |
| 645 blink::mojom::WebBluetoothRemoteGATTService::New(); | 698 blink::mojom::WebBluetoothRemoteGATTService::New(); |
| 646 service_ptr->instance_id = services[0]->GetIdentifier(); | 699 service_ptr->instance_id = services[0]->GetIdentifier(); |
| 647 service_ptr->uuid = services[0]->GetUUID().canonical_value(); | 700 service_ptr->uuid = services[0]->GetUUID().canonical_value(); |
| 648 callback.Run(blink::mojom::WebBluetoothError::SUCCESS, | 701 callback.Run(blink::mojom::WebBluetoothError::SUCCESS, |
| 649 std::move(service_ptr)); | 702 std::move(service_ptr)); |
| 650 } | 703 } |
| 651 | 704 |
| 705 void WebBluetoothServiceImpl::OnGetDeviceSuccess( |
| 706 const RequestDeviceCallback& callback, |
| 707 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, |
| 708 const std::string& device_address) { |
| 709 device_chooser_controller_.reset(); |
| 710 |
| 711 const device::BluetoothDevice* const device = |
| 712 GetAdapter()->GetDevice(device_address); |
| 713 if (device == nullptr) { |
| 714 VLOG(1) << "Device " << device_address << " no longer in adapter"; |
| 715 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::CHOSEN_DEVICE_VANISHED); |
| 716 callback.Run(blink::mojom::WebBluetoothError::CHOSEN_DEVICE_VANISHED, |
| 717 nullptr /* device */); |
| 718 return; |
| 719 } |
| 720 |
| 721 const std::string device_id_for_origin = |
| 722 allowed_devices_map_.AddDevice(GetOrigin(), device_address, options); |
| 723 |
| 724 VLOG(1) << "Device: " << device->GetName(); |
| 725 VLOG(1) << "UUIDs: "; |
| 726 |
| 727 mojo::Array<mojo::String> filtered_uuids; |
| 728 for (const device::BluetoothUUID& uuid : device->GetUUIDs()) { |
| 729 if (allowed_devices_map_.IsOriginAllowedToAccessService( |
| 730 GetOrigin(), device_id_for_origin, uuid.canonical_value())) { |
| 731 VLOG(1) << "\t Allowed: " << uuid.canonical_value(); |
| 732 filtered_uuids.push_back(uuid.canonical_value()); |
| 733 } else { |
| 734 VLOG(1) << "\t Not Allowed: " << uuid.canonical_value(); |
| 735 } |
| 736 } |
| 737 |
| 738 blink::mojom::WebBluetoothDevicePtr device_ptr = |
| 739 blink::mojom::WebBluetoothDevice::New(); |
| 740 device_ptr->id = device_id_for_origin; |
| 741 device_ptr->name = base::UTF16ToUTF8(device->GetName()); |
| 742 device_ptr->uuids = std::move(filtered_uuids); |
| 743 |
| 744 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::SUCCESS); |
| 745 callback.Run(blink::mojom::WebBluetoothError::SUCCESS, std::move(device_ptr)); |
| 746 } |
| 747 |
| 748 void WebBluetoothServiceImpl::OnGetDeviceFailed( |
| 749 const RequestDeviceCallback& callback, |
| 750 blink::mojom::WebBluetoothError error) { |
| 751 // Errors are recorded by the *device_chooser_controller_. |
| 752 callback.Run(error, nullptr /* device */); |
| 753 device_chooser_controller_.reset(); |
| 754 } |
| 755 |
| 652 void WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess( | 756 void WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess( |
| 653 const std::string& device_id, | 757 const std::string& device_id, |
| 654 base::TimeTicks start_time, | 758 base::TimeTicks start_time, |
| 655 const RemoteServerConnectCallback& callback, | 759 const RemoteServerConnectCallback& callback, |
| 656 std::unique_ptr<device::BluetoothGattConnection> connection) { | 760 std::unique_ptr<device::BluetoothGattConnection> connection) { |
| 657 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 761 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 658 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time); | 762 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time); |
| 659 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS); | 763 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS); |
| 660 | 764 |
| 661 connected_devices_->Insert(device_id, std::move(connection)); | 765 connected_devices_->Insert(device_id, std::move(connection)); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 error_code, UMAGATTOperation::START_NOTIFICATIONS)); | 831 error_code, UMAGATTOperation::START_NOTIFICATIONS)); |
| 728 } | 832 } |
| 729 | 833 |
| 730 void WebBluetoothServiceImpl::OnStopNotifySessionComplete( | 834 void WebBluetoothServiceImpl::OnStopNotifySessionComplete( |
| 731 const std::string& characteristic_instance_id, | 835 const std::string& characteristic_instance_id, |
| 732 const RemoteCharacteristicStopNotificationsCallback& callback) { | 836 const RemoteCharacteristicStopNotificationsCallback& callback) { |
| 733 characteristic_id_to_notify_session_.erase(characteristic_instance_id); | 837 characteristic_id_to_notify_session_.erase(characteristic_instance_id); |
| 734 callback.Run(); | 838 callback.Run(); |
| 735 } | 839 } |
| 736 | 840 |
| 841 CacheQueryResult WebBluetoothServiceImpl::QueryCacheForDevice( |
| 842 const std::string& device_id) { |
| 843 const std::string& device_address = |
| 844 allowed_devices_map_.GetDeviceAddress(GetOrigin(), device_id); |
| 845 if (device_address.empty()) { |
| 846 CrashRendererAndClosePipe(bad_message::BDH_DEVICE_NOT_ALLOWED_FOR_ORIGIN); |
| 847 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); |
| 848 } |
| 849 |
| 850 CacheQueryResult result; |
| 851 result.device = GetAdapter()->GetDevice(device_address); |
| 852 |
| 853 // When a device can't be found in the BluetoothAdapter, that generally |
| 854 // indicates that it's gone out of range. We reject with a NetworkError in |
| 855 // that case. |
| 856 if (result.device == nullptr) { |
| 857 result.outcome = CacheQueryOutcome::NO_DEVICE; |
| 858 } |
| 859 return result; |
| 860 } |
| 861 |
| 737 CacheQueryResult WebBluetoothServiceImpl::QueryCacheForService( | 862 CacheQueryResult WebBluetoothServiceImpl::QueryCacheForService( |
| 738 const std::string& service_instance_id) { | 863 const std::string& service_instance_id) { |
| 739 auto device_iter = service_id_to_device_address_.find(service_instance_id); | 864 auto device_iter = service_id_to_device_address_.find(service_instance_id); |
| 740 | 865 |
| 741 // Kill the render, see "ID Not in Map Note" above. | 866 // Kill the render, see "ID Not in Map Note" above. |
| 742 if (device_iter == service_id_to_device_address_.end()) { | 867 if (device_iter == service_id_to_device_address_.end()) { |
| 743 CrashRendererAndClosePipe(bad_message::BDH_INVALID_SERVICE_ID); | 868 CrashRendererAndClosePipe(bad_message::BDH_INVALID_SERVICE_ID); |
| 744 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); | 869 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); |
| 745 } | 870 } |
| 746 | 871 |
| 747 const std::string& device_id = | 872 const std::string& device_id = |
| 748 GetBluetoothDispatcherHost()->allowed_devices_map_.GetDeviceId( | 873 allowed_devices_map_.GetDeviceId(GetOrigin(), device_iter->second); |
| 749 GetOrigin(), device_iter->second); | |
| 750 // Kill the renderer if origin is not allowed to access the device. | 874 // Kill the renderer if origin is not allowed to access the device. |
| 751 if (device_id.empty()) { | 875 if (device_id.empty()) { |
| 752 CrashRendererAndClosePipe(bad_message::BDH_DEVICE_NOT_ALLOWED_FOR_ORIGIN); | 876 CrashRendererAndClosePipe(bad_message::BDH_DEVICE_NOT_ALLOWED_FOR_ORIGIN); |
| 753 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); | 877 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); |
| 754 } | 878 } |
| 755 | 879 |
| 756 CacheQueryResult result = | 880 CacheQueryResult result = QueryCacheForDevice(device_id); |
| 757 GetBluetoothDispatcherHost()->QueryCacheForDevice(GetOrigin(), device_id); | |
| 758 | |
| 759 // TODO(ortuno): Remove once QueryCacheForDevice closes binding_. | |
| 760 // http://crbug.com/508771 | |
| 761 if (result.outcome == CacheQueryOutcome::BAD_RENDERER) { | |
| 762 binding_.Close(); | |
| 763 } | |
| 764 | |
| 765 if (result.outcome != CacheQueryOutcome::SUCCESS) { | 881 if (result.outcome != CacheQueryOutcome::SUCCESS) { |
| 766 return result; | 882 return result; |
| 767 } | 883 } |
| 768 | 884 |
| 769 result.service = result.device->GetGattService(service_instance_id); | 885 result.service = result.device->GetGattService(service_instance_id); |
| 770 if (result.service == nullptr) { | 886 if (result.service == nullptr) { |
| 771 result.outcome = CacheQueryOutcome::NO_SERVICE; | 887 result.outcome = CacheQueryOutcome::NO_SERVICE; |
| 772 } else if (!GetBluetoothDispatcherHost() | 888 } else if (!allowed_devices_map_.IsOriginAllowedToAccessService( |
| 773 ->allowed_devices_map_.IsOriginAllowedToAccessService( | 889 GetOrigin(), device_id, |
| 774 GetOrigin(), device_id, | 890 result.service->GetUUID().canonical_value())) { |
| 775 result.service->GetUUID().canonical_value())) { | |
| 776 CrashRendererAndClosePipe(bad_message::BDH_SERVICE_NOT_ALLOWED_FOR_ORIGIN); | 891 CrashRendererAndClosePipe(bad_message::BDH_SERVICE_NOT_ALLOWED_FOR_ORIGIN); |
| 777 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); | 892 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); |
| 778 } | 893 } |
| 779 return result; | 894 return result; |
| 780 } | 895 } |
| 781 | 896 |
| 782 CacheQueryResult WebBluetoothServiceImpl::QueryCacheForCharacteristic( | 897 CacheQueryResult WebBluetoothServiceImpl::QueryCacheForCharacteristic( |
| 783 const std::string& characteristic_instance_id) { | 898 const std::string& characteristic_instance_id) { |
| 784 auto characteristic_iter = | 899 auto characteristic_iter = |
| 785 characteristic_id_to_service_id_.find(characteristic_instance_id); | 900 characteristic_id_to_service_id_.find(characteristic_instance_id); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 803 result.outcome = CacheQueryOutcome::NO_CHARACTERISTIC; | 918 result.outcome = CacheQueryOutcome::NO_CHARACTERISTIC; |
| 804 } | 919 } |
| 805 | 920 |
| 806 return result; | 921 return result; |
| 807 } | 922 } |
| 808 | 923 |
| 809 RenderProcessHost* WebBluetoothServiceImpl::GetRenderProcessHost() { | 924 RenderProcessHost* WebBluetoothServiceImpl::GetRenderProcessHost() { |
| 810 return render_frame_host_->GetProcess(); | 925 return render_frame_host_->GetProcess(); |
| 811 } | 926 } |
| 812 | 927 |
| 813 BluetoothDispatcherHost* WebBluetoothServiceImpl::GetBluetoothDispatcherHost() { | 928 BluetoothAdapterFactoryWrapper& |
| 929 WebBluetoothServiceImpl::GetBluetoothAdapterFactoryWrapper() { |
| 814 RenderProcessHostImpl* render_process_host_impl = | 930 RenderProcessHostImpl* render_process_host_impl = |
| 815 static_cast<RenderProcessHostImpl*>(GetRenderProcessHost()); | 931 static_cast<RenderProcessHostImpl*>(GetRenderProcessHost()); |
| 816 return render_process_host_impl->GetBluetoothDispatcherHost(); | 932 return render_process_host_impl->GetBluetoothAdapterFactoryWrapper(); |
| 933 } |
| 934 |
| 935 device::BluetoothAdapter* WebBluetoothServiceImpl::GetAdapter() { |
| 936 return GetBluetoothAdapterFactoryWrapper().GetAdapter(this); |
| 817 } | 937 } |
| 818 | 938 |
| 819 void WebBluetoothServiceImpl::CrashRendererAndClosePipe( | 939 void WebBluetoothServiceImpl::CrashRendererAndClosePipe( |
| 820 bad_message::BadMessageReason reason) { | 940 bad_message::BadMessageReason reason) { |
| 821 bad_message::ReceivedBadMessage(GetRenderProcessHost(), reason); | 941 bad_message::ReceivedBadMessage(GetRenderProcessHost(), reason); |
| 822 binding_.Close(); | 942 binding_.Close(); |
| 823 } | 943 } |
| 824 | 944 |
| 825 url::Origin WebBluetoothServiceImpl::GetOrigin() { | 945 url::Origin WebBluetoothServiceImpl::GetOrigin() { |
| 826 return render_frame_host_->GetLastCommittedOrigin(); | 946 return render_frame_host_->GetLastCommittedOrigin(); |
| 827 } | 947 } |
| 828 | 948 |
| 829 void WebBluetoothServiceImpl::ClearState() { | 949 void WebBluetoothServiceImpl::ClearState() { |
| 830 characteristic_id_to_notify_session_.clear(); | 950 characteristic_id_to_notify_session_.clear(); |
| 831 pending_primary_services_requests_.clear(); | 951 pending_primary_services_requests_.clear(); |
| 832 characteristic_id_to_service_id_.clear(); | 952 characteristic_id_to_service_id_.clear(); |
| 833 service_id_to_device_address_.clear(); | 953 service_id_to_device_address_.clear(); |
| 834 connected_devices_.reset( | 954 connected_devices_.reset( |
| 835 new FrameConnectedBluetoothDevices(render_frame_host_)); | 955 new FrameConnectedBluetoothDevices(render_frame_host_)); |
| 956 allowed_devices_map_ = BluetoothAllowedDevicesMap(); |
| 957 device_chooser_controller_.reset(); |
| 958 GetBluetoothAdapterFactoryWrapper().ReleaseAdapter(this); |
| 836 } | 959 } |
| 837 | 960 |
| 838 } // namespace content | 961 } // namespace content |
| OLD | NEW |