Chromium Code Reviews| 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 // WebBluetoothServiceImpl 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. |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 std::vector<device::BluetoothRemoteGattCharacteristic*> | 135 std::vector<device::BluetoothRemoteGattCharacteristic*> |
| 136 GetCharacteristicsByUUID(device::BluetoothRemoteGattService* service, | 136 GetCharacteristicsByUUID(device::BluetoothRemoteGattService* service, |
| 137 const BluetoothUUID& characteristic_uuid) { | 137 const BluetoothUUID& characteristic_uuid) { |
| 138 std::vector<device::BluetoothRemoteGattCharacteristic*> characteristics; | 138 std::vector<device::BluetoothRemoteGattCharacteristic*> characteristics; |
| 139 VLOG(1) << "Looking for characteristic: " | 139 VLOG(1) << "Looking for characteristic: " |
| 140 << characteristic_uuid.canonical_value(); | 140 << characteristic_uuid.canonical_value(); |
| 141 for (device::BluetoothRemoteGattCharacteristic* characteristic : | 141 for (device::BluetoothRemoteGattCharacteristic* characteristic : |
| 142 service->GetCharacteristics()) { | 142 service->GetCharacteristics()) { |
| 143 VLOG(1) << "Characteristic in cache: " | 143 VLOG(1) << "Characteristic in cache: " |
| 144 << characteristic->GetUUID().canonical_value(); | 144 << characteristic->GetUUID().canonical_value(); |
| 145 if (characteristic->GetUUID() == characteristic_uuid) { | 145 if (characteristic->GetUUID() == characteristic_uuid) |
|
ortuno
2016/11/22 02:32:21
nit: Please leave these. Owners (me) prefer to hav
juncai
2016/11/23 20:54:22
Done.
| |
| 146 characteristics.push_back(characteristic); | 146 characteristics.push_back(characteristic); |
| 147 } | |
| 148 } | 147 } |
| 149 return characteristics; | 148 return characteristics; |
| 150 } | 149 } |
| 151 | 150 |
| 152 // TODO(ortuno): This should really be a BluetoothDevice method. | 151 // TODO(ortuno): This should really be a BluetoothDevice method. |
| 153 // Replace when implemented. http://crbug.com/552022 | 152 // Replace when implemented. http://crbug.com/552022 |
| 154 std::vector<device::BluetoothRemoteGattService*> GetPrimaryServicesByUUID( | 153 std::vector<device::BluetoothRemoteGattService*> GetPrimaryServicesByUUID( |
| 155 device::BluetoothDevice* device, | 154 device::BluetoothDevice* device, |
| 156 const BluetoothUUID& service_uuid) { | 155 const BluetoothUUID& service_uuid) { |
| 157 std::vector<device::BluetoothRemoteGattService*> services; | 156 std::vector<device::BluetoothRemoteGattService*> services; |
| 158 VLOG(1) << "Looking for service: " << service_uuid.canonical_value(); | 157 VLOG(1) << "Looking for service: " << service_uuid.canonical_value(); |
| 159 for (device::BluetoothRemoteGattService* service : | 158 for (device::BluetoothRemoteGattService* service : |
| 160 device->GetGattServices()) { | 159 device->GetGattServices()) { |
| 161 VLOG(1) << "Service in cache: " << service->GetUUID().canonical_value(); | 160 VLOG(1) << "Service in cache: " << service->GetUUID().canonical_value(); |
| 162 if (service->GetUUID() == service_uuid && service->IsPrimary()) { | 161 if (service->GetUUID() == service_uuid && service->IsPrimary()) |
| 163 services.push_back(service); | 162 services.push_back(service); |
| 164 } | |
| 165 } | 163 } |
| 166 return services; | 164 return services; |
| 167 } | 165 } |
| 168 | 166 |
| 169 // TODO(ortuno): This should really be a BluetoothDevice method. | 167 // TODO(ortuno): This should really be a BluetoothDevice method. |
| 170 // Replace when implemented. http://crbug.com/552022 | 168 // Replace when implemented. http://crbug.com/552022 |
| 171 std::vector<device::BluetoothRemoteGattService*> GetPrimaryServices( | 169 std::vector<device::BluetoothRemoteGattService*> GetPrimaryServices( |
| 172 device::BluetoothDevice* device) { | 170 device::BluetoothDevice* device) { |
| 173 std::vector<device::BluetoothRemoteGattService*> services; | 171 std::vector<device::BluetoothRemoteGattService*> services; |
| 174 VLOG(1) << "Looking for services."; | 172 VLOG(1) << "Looking for services."; |
| 175 for (device::BluetoothRemoteGattService* service : | 173 for (device::BluetoothRemoteGattService* service : |
| 176 device->GetGattServices()) { | 174 device->GetGattServices()) { |
| 177 VLOG(1) << "Service in cache: " << service->GetUUID().canonical_value(); | 175 VLOG(1) << "Service in cache: " << service->GetUUID().canonical_value(); |
| 178 if (service->IsPrimary()) { | 176 if (service->IsPrimary()) |
| 179 services.push_back(service); | 177 services.push_back(service); |
| 180 } | |
| 181 } | 178 } |
| 182 return services; | 179 return services; |
| 183 } | 180 } |
| 184 | 181 |
| 185 } // namespace | 182 } // namespace |
| 186 | 183 |
| 187 WebBluetoothServiceImpl::WebBluetoothServiceImpl( | 184 WebBluetoothServiceImpl::WebBluetoothServiceImpl( |
| 188 RenderFrameHost* render_frame_host, | 185 RenderFrameHost* render_frame_host, |
| 189 blink::mojom::WebBluetoothServiceRequest request) | 186 blink::mojom::WebBluetoothServiceRequest request) |
| 190 : WebContentsObserver(WebContents::FromRenderFrameHost(render_frame_host)), | 187 : WebContentsObserver(WebContents::FromRenderFrameHost(render_frame_host)), |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 218 navigation_handle->GetRenderFrameHost() == render_frame_host_ && | 215 navigation_handle->GetRenderFrameHost() == render_frame_host_ && |
| 219 !navigation_handle->IsSamePage()) { | 216 !navigation_handle->IsSamePage()) { |
| 220 ClearState(); | 217 ClearState(); |
| 221 } | 218 } |
| 222 } | 219 } |
| 223 | 220 |
| 224 void WebBluetoothServiceImpl::AdapterPoweredChanged( | 221 void WebBluetoothServiceImpl::AdapterPoweredChanged( |
| 225 device::BluetoothAdapter* adapter, | 222 device::BluetoothAdapter* adapter, |
| 226 bool powered) { | 223 bool powered) { |
| 227 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 224 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 228 if (device_chooser_controller_.get()) { | 225 if (device_chooser_controller_.get()) |
| 229 device_chooser_controller_->AdapterPoweredChanged(powered); | 226 device_chooser_controller_->AdapterPoweredChanged(powered); |
| 230 } | |
| 231 } | 227 } |
| 232 | 228 |
| 233 void WebBluetoothServiceImpl::DeviceAdded(device::BluetoothAdapter* adapter, | 229 void WebBluetoothServiceImpl::DeviceAdded(device::BluetoothAdapter* adapter, |
| 234 device::BluetoothDevice* device) { | 230 device::BluetoothDevice* device) { |
| 235 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 231 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 236 if (device_chooser_controller_.get()) { | 232 if (device_chooser_controller_.get()) |
| 237 device_chooser_controller_->AddFilteredDevice(*device); | 233 device_chooser_controller_->AddFilteredDevice(*device); |
| 238 } | |
| 239 } | 234 } |
| 240 | 235 |
| 241 void WebBluetoothServiceImpl::DeviceChanged(device::BluetoothAdapter* adapter, | 236 void WebBluetoothServiceImpl::DeviceChanged(device::BluetoothAdapter* adapter, |
| 242 device::BluetoothDevice* device) { | 237 device::BluetoothDevice* device) { |
| 243 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 238 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 244 | 239 |
| 245 if (device_chooser_controller_.get()) { | 240 if (device_chooser_controller_.get()) |
| 246 device_chooser_controller_->AddFilteredDevice(*device); | 241 device_chooser_controller_->AddFilteredDevice(*device); |
| 247 } | |
| 248 | 242 |
| 249 if (!device->IsGattConnected()) { | 243 if (!device->IsGattConnected()) { |
| 250 base::Optional<WebBluetoothDeviceId> device_id = | 244 base::Optional<WebBluetoothDeviceId> device_id = |
| 251 connected_devices_->CloseConnectionToDeviceWithAddress( | 245 connected_devices_->CloseConnectionToDeviceWithAddress( |
| 252 device->GetAddress()); | 246 device->GetAddress()); |
| 253 if (device_id && client_) { | 247 if (device_id && client_) |
| 254 client_->GattServerDisconnected(device_id.value()); | 248 client_->GattServerDisconnected(device_id.value()); |
| 255 } | |
| 256 } | 249 } |
| 257 } | 250 } |
| 258 | 251 |
| 259 void WebBluetoothServiceImpl::GattServicesDiscovered( | 252 void WebBluetoothServiceImpl::GattServicesDiscovered( |
| 260 device::BluetoothAdapter* adapter, | 253 device::BluetoothAdapter* adapter, |
| 261 device::BluetoothDevice* device) { | 254 device::BluetoothDevice* device) { |
| 262 if (device_chooser_controller_.get()) { | 255 if (device_chooser_controller_.get()) |
| 263 device_chooser_controller_->AddFilteredDevice(*device); | 256 device_chooser_controller_->AddFilteredDevice(*device); |
| 264 } | |
| 265 | 257 |
| 266 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 258 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 267 const std::string& device_address = device->GetAddress(); | 259 const std::string& device_address = device->GetAddress(); |
| 268 VLOG(1) << "Services discovered for device: " << device_address; | 260 VLOG(1) << "Services discovered for device: " << device_address; |
| 269 | 261 |
| 270 auto iter = pending_primary_services_requests_.find(device_address); | 262 auto iter = pending_primary_services_requests_.find(device_address); |
| 271 if (iter == pending_primary_services_requests_.end()) { | 263 if (iter == pending_primary_services_requests_.end()) |
| 272 return; | 264 return; |
| 273 } | |
| 274 std::vector<PrimaryServicesRequestCallback> requests = | 265 std::vector<PrimaryServicesRequestCallback> requests = |
| 275 std::move(iter->second); | 266 std::move(iter->second); |
| 276 pending_primary_services_requests_.erase(iter); | 267 pending_primary_services_requests_.erase(iter); |
| 277 | 268 |
| 278 for (const PrimaryServicesRequestCallback& request : requests) { | 269 for (const PrimaryServicesRequestCallback& request : requests) |
| 279 request.Run(device); | 270 request.Run(device); |
| 280 } | |
| 281 | 271 |
| 282 // Sending get-service responses unexpectedly queued another request. | 272 // Sending get-service responses unexpectedly queued another request. |
| 283 DCHECK( | 273 DCHECK( |
| 284 !base::ContainsKey(pending_primary_services_requests_, device_address)); | 274 !base::ContainsKey(pending_primary_services_requests_, device_address)); |
| 285 } | 275 } |
| 286 | 276 |
| 287 void WebBluetoothServiceImpl::GattCharacteristicValueChanged( | 277 void WebBluetoothServiceImpl::GattCharacteristicValueChanged( |
| 288 device::BluetoothAdapter* adapter, | 278 device::BluetoothAdapter* adapter, |
| 289 device::BluetoothRemoteGattCharacteristic* characteristic, | 279 device::BluetoothRemoteGattCharacteristic* characteristic, |
| 290 const std::vector<uint8_t>& value) { | 280 const std::vector<uint8_t>& value) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 303 weak_ptr_factory_.GetWeakPtr(), | 293 weak_ptr_factory_.GetWeakPtr(), |
| 304 characteristic->GetIdentifier(), value))) { | 294 characteristic->GetIdentifier(), value))) { |
| 305 LOG(WARNING) << "No TaskRunner."; | 295 LOG(WARNING) << "No TaskRunner."; |
| 306 } | 296 } |
| 307 } | 297 } |
| 308 | 298 |
| 309 void WebBluetoothServiceImpl::NotifyCharacteristicValueChanged( | 299 void WebBluetoothServiceImpl::NotifyCharacteristicValueChanged( |
| 310 const std::string& characteristic_instance_id, | 300 const std::string& characteristic_instance_id, |
| 311 std::vector<uint8_t> value) { | 301 std::vector<uint8_t> value) { |
| 312 if (client_) { | 302 if (client_) { |
| 313 client_->RemoteCharacteristicValueChanged( | 303 client_->RemoteCharacteristicValueChanged(characteristic_instance_id, |
| 314 characteristic_instance_id, mojo::Array<uint8_t>(std::move(value))); | 304 std::move(value)); |
| 315 } | 305 } |
| 316 } | 306 } |
| 317 | 307 |
| 318 void WebBluetoothServiceImpl::SetClient( | 308 void WebBluetoothServiceImpl::SetClient( |
| 319 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo client) { | 309 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo client) { |
| 320 DCHECK(!client_.get()); | 310 DCHECK(!client_.get()); |
| 321 client_.Bind(std::move(client)); | 311 client_.Bind(std::move(client)); |
| 322 } | 312 } |
| 323 | 313 |
| 324 void WebBluetoothServiceImpl::RequestDevice( | 314 void WebBluetoothServiceImpl::RequestDevice( |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 404 RecordWebBluetoothFunctionCall( | 394 RecordWebBluetoothFunctionCall( |
| 405 quantity == blink::mojom::WebBluetoothGATTQueryQuantity::SINGLE | 395 quantity == blink::mojom::WebBluetoothGATTQueryQuantity::SINGLE |
| 406 ? UMAWebBluetoothFunction::GET_PRIMARY_SERVICE | 396 ? UMAWebBluetoothFunction::GET_PRIMARY_SERVICE |
| 407 : UMAWebBluetoothFunction::GET_PRIMARY_SERVICES); | 397 : UMAWebBluetoothFunction::GET_PRIMARY_SERVICES); |
| 408 RecordGetPrimaryServicesServices(quantity, services_uuid); | 398 RecordGetPrimaryServicesServices(quantity, services_uuid); |
| 409 | 399 |
| 410 if (!allowed_devices_map_.IsOriginAllowedToAccessAtLeastOneService( | 400 if (!allowed_devices_map_.IsOriginAllowedToAccessAtLeastOneService( |
| 411 GetOrigin(), device_id)) { | 401 GetOrigin(), device_id)) { |
| 412 callback.Run( | 402 callback.Run( |
| 413 blink::mojom::WebBluetoothResult::NOT_ALLOWED_TO_ACCESS_ANY_SERVICE, | 403 blink::mojom::WebBluetoothResult::NOT_ALLOWED_TO_ACCESS_ANY_SERVICE, |
| 414 nullptr /* service */); | 404 base::nullopt /* service */); |
| 415 return; | 405 return; |
| 416 } | 406 } |
| 417 | 407 |
| 418 if (services_uuid && | 408 if (services_uuid && |
| 419 !allowed_devices_map_.IsOriginAllowedToAccessService( | 409 !allowed_devices_map_.IsOriginAllowedToAccessService( |
| 420 GetOrigin(), device_id, services_uuid.value())) { | 410 GetOrigin(), device_id, services_uuid.value())) { |
| 421 callback.Run( | 411 callback.Run( |
| 422 blink::mojom::WebBluetoothResult::NOT_ALLOWED_TO_ACCESS_SERVICE, | 412 blink::mojom::WebBluetoothResult::NOT_ALLOWED_TO_ACCESS_SERVICE, |
| 423 nullptr /* service */); | 413 base::nullopt /* service */); |
| 424 return; | 414 return; |
| 425 } | 415 } |
| 426 | 416 |
| 427 const CacheQueryResult query_result = QueryCacheForDevice(device_id); | 417 const CacheQueryResult query_result = QueryCacheForDevice(device_id); |
| 428 | 418 |
| 429 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { | 419 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) |
| 420 return; | |
| 421 | |
| 422 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | |
| 423 RecordGetPrimaryServicesOutcome(quantity, query_result.outcome); | |
| 424 callback.Run(query_result.GetWebResult(), base::nullopt /* service */); | |
| 430 return; | 425 return; |
| 431 } | 426 } |
| 432 | 427 |
| 433 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | |
| 434 RecordGetPrimaryServicesOutcome(quantity, query_result.outcome); | |
| 435 callback.Run(query_result.GetWebResult(), nullptr /* service */); | |
| 436 return; | |
| 437 } | |
| 438 | |
| 439 const std::string& device_address = query_result.device->GetAddress(); | 428 const std::string& device_address = query_result.device->GetAddress(); |
| 440 | 429 |
| 441 // We can't know if a service is present or not until GATT service discovery | 430 // We can't know if a service is present or not until GATT service discovery |
| 442 // is complete for the device. | 431 // is complete for the device. |
| 443 if (query_result.device->IsGattServicesDiscoveryComplete()) { | 432 if (query_result.device->IsGattServicesDiscoveryComplete()) { |
| 444 RemoteServerGetPrimaryServicesImpl(device_id, quantity, services_uuid, | 433 RemoteServerGetPrimaryServicesImpl(device_id, quantity, services_uuid, |
| 445 callback, query_result.device); | 434 callback, query_result.device); |
| 446 return; | 435 return; |
| 447 } | 436 } |
| 448 | 437 |
| 449 VLOG(1) << "Services not yet discovered."; | 438 VLOG(1) << "Services not yet discovered."; |
| 450 pending_primary_services_requests_[device_address].push_back(base::Bind( | 439 pending_primary_services_requests_[device_address].push_back(base::Bind( |
| 451 &WebBluetoothServiceImpl::RemoteServerGetPrimaryServicesImpl, | 440 &WebBluetoothServiceImpl::RemoteServerGetPrimaryServicesImpl, |
| 452 base::Unretained(this), device_id, quantity, services_uuid, callback)); | 441 base::Unretained(this), device_id, quantity, services_uuid, callback)); |
| 453 } | 442 } |
| 454 | 443 |
| 455 void WebBluetoothServiceImpl::RemoteServiceGetCharacteristics( | 444 void WebBluetoothServiceImpl::RemoteServiceGetCharacteristics( |
| 456 const mojo::String& service_instance_id, | 445 const std::string& service_instance_id, |
| 457 blink::mojom::WebBluetoothGATTQueryQuantity quantity, | 446 blink::mojom::WebBluetoothGATTQueryQuantity quantity, |
| 458 const base::Optional<BluetoothUUID>& characteristics_uuid, | 447 const base::Optional<BluetoothUUID>& characteristics_uuid, |
| 459 const RemoteServiceGetCharacteristicsCallback& callback) { | 448 const RemoteServiceGetCharacteristicsCallback& callback) { |
| 460 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 449 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 461 | 450 |
| 462 RecordWebBluetoothFunctionCall( | 451 RecordWebBluetoothFunctionCall( |
| 463 quantity == blink::mojom::WebBluetoothGATTQueryQuantity::SINGLE | 452 quantity == blink::mojom::WebBluetoothGATTQueryQuantity::SINGLE |
| 464 ? UMAWebBluetoothFunction::SERVICE_GET_CHARACTERISTIC | 453 ? UMAWebBluetoothFunction::SERVICE_GET_CHARACTERISTIC |
| 465 : UMAWebBluetoothFunction::SERVICE_GET_CHARACTERISTICS); | 454 : UMAWebBluetoothFunction::SERVICE_GET_CHARACTERISTICS); |
| 466 RecordGetCharacteristicsCharacteristic(quantity, characteristics_uuid); | 455 RecordGetCharacteristicsCharacteristic(quantity, characteristics_uuid); |
| 467 | 456 |
| 468 if (characteristics_uuid && | 457 if (characteristics_uuid && |
| 469 BluetoothBlacklist::Get().IsExcluded(characteristics_uuid.value())) { | 458 BluetoothBlacklist::Get().IsExcluded(characteristics_uuid.value())) { |
| 470 RecordGetCharacteristicsOutcome(quantity, | 459 RecordGetCharacteristicsOutcome(quantity, |
| 471 UMAGetCharacteristicOutcome::BLACKLISTED); | 460 UMAGetCharacteristicOutcome::BLACKLISTED); |
| 472 callback.Run( | 461 callback.Run( |
| 473 blink::mojom::WebBluetoothResult::BLACKLISTED_CHARACTERISTIC_UUID, | 462 blink::mojom::WebBluetoothResult::BLACKLISTED_CHARACTERISTIC_UUID, |
| 474 nullptr /* characteristics */); | 463 base::nullopt /* characteristics */); |
| 475 return; | 464 return; |
| 476 } | 465 } |
| 477 | 466 |
| 478 const CacheQueryResult query_result = | 467 const CacheQueryResult query_result = |
| 479 QueryCacheForService(service_instance_id); | 468 QueryCacheForService(service_instance_id); |
| 480 | 469 |
| 481 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { | 470 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) |
| 471 return; | |
| 472 | |
| 473 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | |
| 474 RecordGetCharacteristicsOutcome(quantity, query_result.outcome); | |
| 475 callback.Run(query_result.GetWebResult(), | |
| 476 base::nullopt /* characteristics */); | |
| 482 return; | 477 return; |
| 483 } | 478 } |
| 484 | 479 |
| 485 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | |
| 486 RecordGetCharacteristicsOutcome(quantity, query_result.outcome); | |
| 487 callback.Run(query_result.GetWebResult(), nullptr /* characteristics */); | |
| 488 return; | |
| 489 } | |
| 490 | |
| 491 std::vector<device::BluetoothRemoteGattCharacteristic*> characteristics = | 480 std::vector<device::BluetoothRemoteGattCharacteristic*> characteristics = |
| 492 characteristics_uuid | 481 characteristics_uuid |
| 493 ? GetCharacteristicsByUUID(query_result.service, | 482 ? GetCharacteristicsByUUID(query_result.service, |
| 494 characteristics_uuid.value()) | 483 characteristics_uuid.value()) |
| 495 : query_result.service->GetCharacteristics(); | 484 : query_result.service->GetCharacteristics(); |
| 496 | 485 |
| 497 mojo::Array<blink::mojom::WebBluetoothRemoteGATTCharacteristicPtr> | 486 std::vector<blink::mojom::WebBluetoothRemoteGATTCharacteristicPtr> |
| 498 response_characteristics; | 487 response_characteristics; |
| 499 for (device::BluetoothRemoteGattCharacteristic* characteristic : | 488 for (device::BluetoothRemoteGattCharacteristic* characteristic : |
| 500 characteristics) { | 489 characteristics) { |
| 501 if (BluetoothBlacklist::Get().IsExcluded(characteristic->GetUUID())) { | 490 if (BluetoothBlacklist::Get().IsExcluded(characteristic->GetUUID())) |
| 502 continue; | 491 continue; |
| 503 } | |
| 504 std::string characteristic_instance_id = characteristic->GetIdentifier(); | 492 std::string characteristic_instance_id = characteristic->GetIdentifier(); |
| 505 auto insert_result = characteristic_id_to_service_id_.insert( | 493 auto insert_result = characteristic_id_to_service_id_.insert( |
| 506 std::make_pair(characteristic_instance_id, service_instance_id)); | 494 std::make_pair(characteristic_instance_id, service_instance_id)); |
| 507 // If value is already in map, DCHECK it's valid. | 495 // If value is already in map, DCHECK it's valid. |
| 508 if (!insert_result.second) | 496 if (!insert_result.second) |
| 509 DCHECK(insert_result.first->second == service_instance_id); | 497 DCHECK(insert_result.first->second == service_instance_id); |
| 510 | 498 |
| 511 blink::mojom::WebBluetoothRemoteGATTCharacteristicPtr characteristic_ptr = | 499 blink::mojom::WebBluetoothRemoteGATTCharacteristicPtr characteristic_ptr = |
| 512 blink::mojom::WebBluetoothRemoteGATTCharacteristic::New(); | 500 blink::mojom::WebBluetoothRemoteGATTCharacteristic::New(); |
| 513 characteristic_ptr->instance_id = characteristic_instance_id; | 501 characteristic_ptr->instance_id = characteristic_instance_id; |
| 514 characteristic_ptr->uuid = characteristic->GetUUID().canonical_value(); | 502 characteristic_ptr->uuid = characteristic->GetUUID().canonical_value(); |
| 515 characteristic_ptr->properties = | 503 characteristic_ptr->properties = |
| 516 static_cast<uint32_t>(characteristic->GetProperties()); | 504 static_cast<uint32_t>(characteristic->GetProperties()); |
| 517 response_characteristics.push_back(std::move(characteristic_ptr)); | 505 response_characteristics.push_back(std::move(characteristic_ptr)); |
| 518 | 506 |
| 519 if (quantity == blink::mojom::WebBluetoothGATTQueryQuantity::SINGLE) { | 507 if (quantity == blink::mojom::WebBluetoothGATTQueryQuantity::SINGLE) |
| 520 break; | 508 break; |
| 521 } | |
| 522 } | 509 } |
| 523 | 510 |
| 524 if (!response_characteristics.empty()) { | 511 if (!response_characteristics.empty()) { |
| 525 RecordGetCharacteristicsOutcome(quantity, | 512 RecordGetCharacteristicsOutcome(quantity, |
| 526 UMAGetCharacteristicOutcome::SUCCESS); | 513 UMAGetCharacteristicOutcome::SUCCESS); |
| 527 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, | 514 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, |
| 528 std::move(response_characteristics)); | 515 std::move(response_characteristics)); |
| 529 return; | 516 return; |
| 530 } | 517 } |
| 531 | 518 |
| 532 RecordGetCharacteristicsOutcome( | 519 RecordGetCharacteristicsOutcome( |
| 533 quantity, characteristics_uuid | 520 quantity, characteristics_uuid |
| 534 ? UMAGetCharacteristicOutcome::NOT_FOUND | 521 ? UMAGetCharacteristicOutcome::NOT_FOUND |
| 535 : UMAGetCharacteristicOutcome::NO_CHARACTERISTICS); | 522 : UMAGetCharacteristicOutcome::NO_CHARACTERISTICS); |
| 536 callback.Run(characteristics_uuid | 523 callback.Run(characteristics_uuid |
| 537 ? blink::mojom::WebBluetoothResult::CHARACTERISTIC_NOT_FOUND | 524 ? blink::mojom::WebBluetoothResult::CHARACTERISTIC_NOT_FOUND |
| 538 : blink::mojom::WebBluetoothResult::NO_CHARACTERISTICS_FOUND, | 525 : blink::mojom::WebBluetoothResult::NO_CHARACTERISTICS_FOUND, |
| 539 nullptr /* characteristics */); | 526 base::nullopt /* characteristics */); |
| 540 } | 527 } |
| 541 | 528 |
| 542 void WebBluetoothServiceImpl::RemoteCharacteristicReadValue( | 529 void WebBluetoothServiceImpl::RemoteCharacteristicReadValue( |
| 543 const mojo::String& characteristic_instance_id, | 530 const std::string& characteristic_instance_id, |
| 544 const RemoteCharacteristicReadValueCallback& callback) { | 531 const RemoteCharacteristicReadValueCallback& callback) { |
| 545 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 532 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 546 RecordWebBluetoothFunctionCall( | 533 RecordWebBluetoothFunctionCall( |
| 547 UMAWebBluetoothFunction::CHARACTERISTIC_READ_VALUE); | 534 UMAWebBluetoothFunction::CHARACTERISTIC_READ_VALUE); |
| 548 | 535 |
| 549 const CacheQueryResult query_result = | 536 const CacheQueryResult query_result = |
| 550 QueryCacheForCharacteristic(characteristic_instance_id); | 537 QueryCacheForCharacteristic(characteristic_instance_id); |
| 551 | 538 |
| 552 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { | 539 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) |
| 540 return; | |
| 541 | |
| 542 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | |
| 543 RecordCharacteristicReadValueOutcome(query_result.outcome); | |
| 544 callback.Run(query_result.GetWebResult(), base::nullopt /* value */); | |
| 553 return; | 545 return; |
| 554 } | 546 } |
| 555 | 547 |
| 556 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | |
| 557 RecordCharacteristicReadValueOutcome(query_result.outcome); | |
| 558 callback.Run(query_result.GetWebResult(), nullptr /* value */); | |
| 559 return; | |
| 560 } | |
| 561 | |
| 562 if (BluetoothBlacklist::Get().IsExcludedFromReads( | 548 if (BluetoothBlacklist::Get().IsExcludedFromReads( |
| 563 query_result.characteristic->GetUUID())) { | 549 query_result.characteristic->GetUUID())) { |
| 564 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::BLACKLISTED); | 550 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::BLACKLISTED); |
| 565 callback.Run(blink::mojom::WebBluetoothResult::BLACKLISTED_READ, | 551 callback.Run(blink::mojom::WebBluetoothResult::BLACKLISTED_READ, |
| 566 nullptr /* value */); | 552 base::nullopt /* value */); |
| 567 return; | 553 return; |
| 568 } | 554 } |
| 569 | 555 |
| 570 query_result.characteristic->ReadRemoteCharacteristic( | 556 query_result.characteristic->ReadRemoteCharacteristic( |
| 571 base::Bind(&WebBluetoothServiceImpl::OnReadValueSuccess, | 557 base::Bind(&WebBluetoothServiceImpl::OnReadValueSuccess, |
| 572 weak_ptr_factory_.GetWeakPtr(), callback), | 558 weak_ptr_factory_.GetWeakPtr(), callback), |
| 573 base::Bind(&WebBluetoothServiceImpl::OnReadValueFailed, | 559 base::Bind(&WebBluetoothServiceImpl::OnReadValueFailed, |
| 574 weak_ptr_factory_.GetWeakPtr(), callback)); | 560 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 575 } | 561 } |
| 576 | 562 |
| 577 void WebBluetoothServiceImpl::RemoteCharacteristicWriteValue( | 563 void WebBluetoothServiceImpl::RemoteCharacteristicWriteValue( |
| 578 const mojo::String& characteristic_instance_id, | 564 const std::string& characteristic_instance_id, |
| 579 mojo::Array<uint8_t> value, | 565 const std::vector<uint8_t>& value, |
| 580 const RemoteCharacteristicWriteValueCallback& callback) { | 566 const RemoteCharacteristicWriteValueCallback& callback) { |
| 581 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 567 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 582 RecordWebBluetoothFunctionCall( | 568 RecordWebBluetoothFunctionCall( |
| 583 UMAWebBluetoothFunction::CHARACTERISTIC_WRITE_VALUE); | 569 UMAWebBluetoothFunction::CHARACTERISTIC_WRITE_VALUE); |
| 584 | 570 |
| 585 // We perform the length check on the renderer side. So if we | 571 // We perform the length check on the renderer side. So if we |
| 586 // get a value with length > 512, we can assume it's a hostile | 572 // get a value with length > 512, we can assume it's a hostile |
| 587 // renderer and kill it. | 573 // renderer and kill it. |
| 588 if (value.size() > 512) { | 574 if (value.size() > 512) { |
| 589 CrashRendererAndClosePipe(bad_message::BDH_INVALID_WRITE_VALUE_LENGTH); | 575 CrashRendererAndClosePipe(bad_message::BDH_INVALID_WRITE_VALUE_LENGTH); |
| 590 return; | 576 return; |
| 591 } | 577 } |
| 592 | 578 |
| 593 const CacheQueryResult query_result = | 579 const CacheQueryResult query_result = |
| 594 QueryCacheForCharacteristic(characteristic_instance_id); | 580 QueryCacheForCharacteristic(characteristic_instance_id); |
| 595 | 581 |
| 596 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { | 582 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) |
| 597 return; | 583 return; |
| 598 } | |
| 599 | 584 |
| 600 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | 585 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { |
| 601 RecordCharacteristicWriteValueOutcome(query_result.outcome); | 586 RecordCharacteristicWriteValueOutcome(query_result.outcome); |
| 602 callback.Run(query_result.GetWebResult()); | 587 callback.Run(query_result.GetWebResult()); |
| 603 return; | 588 return; |
| 604 } | 589 } |
| 605 | 590 |
| 606 if (BluetoothBlacklist::Get().IsExcludedFromWrites( | 591 if (BluetoothBlacklist::Get().IsExcludedFromWrites( |
| 607 query_result.characteristic->GetUUID())) { | 592 query_result.characteristic->GetUUID())) { |
| 608 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::BLACKLISTED); | 593 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::BLACKLISTED); |
| 609 callback.Run(blink::mojom::WebBluetoothResult::BLACKLISTED_WRITE); | 594 callback.Run(blink::mojom::WebBluetoothResult::BLACKLISTED_WRITE); |
| 610 return; | 595 return; |
| 611 } | 596 } |
| 612 | 597 |
| 613 query_result.characteristic->WriteRemoteCharacteristic( | 598 query_result.characteristic->WriteRemoteCharacteristic( |
| 614 value.To<std::vector<uint8_t>>(), | 599 value, base::Bind(&WebBluetoothServiceImpl::OnWriteValueSuccess, |
| 615 base::Bind(&WebBluetoothServiceImpl::OnWriteValueSuccess, | 600 weak_ptr_factory_.GetWeakPtr(), callback), |
| 616 weak_ptr_factory_.GetWeakPtr(), callback), | |
| 617 base::Bind(&WebBluetoothServiceImpl::OnWriteValueFailed, | 601 base::Bind(&WebBluetoothServiceImpl::OnWriteValueFailed, |
| 618 weak_ptr_factory_.GetWeakPtr(), callback)); | 602 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 619 } | 603 } |
| 620 | 604 |
| 621 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications( | 605 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications( |
| 622 const mojo::String& characteristic_instance_id, | 606 const std::string& characteristic_instance_id, |
| 623 const RemoteCharacteristicStartNotificationsCallback& callback) { | 607 const RemoteCharacteristicStartNotificationsCallback& callback) { |
| 624 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 608 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 625 RecordWebBluetoothFunctionCall( | 609 RecordWebBluetoothFunctionCall( |
| 626 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS); | 610 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS); |
| 627 | 611 |
| 628 auto iter = | 612 auto iter = |
| 629 characteristic_id_to_notify_session_.find(characteristic_instance_id); | 613 characteristic_id_to_notify_session_.find(characteristic_instance_id); |
| 630 if (iter != characteristic_id_to_notify_session_.end() && | 614 if (iter != characteristic_id_to_notify_session_.end() && |
| 631 iter->second->IsActive()) { | 615 iter->second->IsActive()) { |
| 632 // If the frame has already started notifications and the notifications | 616 // If the frame has already started notifications and the notifications |
| 633 // are active we return SUCCESS. | 617 // are active we return SUCCESS. |
| 634 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); | 618 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); |
| 635 return; | 619 return; |
| 636 } | 620 } |
| 637 | 621 |
| 638 const CacheQueryResult query_result = | 622 const CacheQueryResult query_result = |
| 639 QueryCacheForCharacteristic(characteristic_instance_id); | 623 QueryCacheForCharacteristic(characteristic_instance_id); |
| 640 | 624 |
| 641 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { | 625 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) |
| 642 return; | 626 return; |
| 643 } | |
| 644 | 627 |
| 645 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | 628 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { |
| 646 RecordStartNotificationsOutcome(query_result.outcome); | 629 RecordStartNotificationsOutcome(query_result.outcome); |
| 647 callback.Run(query_result.GetWebResult()); | 630 callback.Run(query_result.GetWebResult()); |
| 648 return; | 631 return; |
| 649 } | 632 } |
| 650 | 633 |
| 651 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate = | 634 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate = |
| 652 query_result.characteristic->GetProperties() & | 635 query_result.characteristic->GetProperties() & |
| 653 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY | | 636 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY | |
| 654 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE); | 637 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE); |
| 655 if (!notify_or_indicate) { | 638 if (!notify_or_indicate) { |
| 656 callback.Run(blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED); | 639 callback.Run(blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED); |
| 657 return; | 640 return; |
| 658 } | 641 } |
| 659 | 642 |
| 660 query_result.characteristic->StartNotifySession( | 643 query_result.characteristic->StartNotifySession( |
| 661 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess, | 644 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess, |
| 662 weak_ptr_factory_.GetWeakPtr(), callback), | 645 weak_ptr_factory_.GetWeakPtr(), callback), |
| 663 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed, | 646 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed, |
| 664 weak_ptr_factory_.GetWeakPtr(), callback)); | 647 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 665 } | 648 } |
| 666 | 649 |
| 667 void WebBluetoothServiceImpl::RemoteCharacteristicStopNotifications( | 650 void WebBluetoothServiceImpl::RemoteCharacteristicStopNotifications( |
| 668 const mojo::String& characteristic_instance_id, | 651 const std::string& characteristic_instance_id, |
| 669 const RemoteCharacteristicStopNotificationsCallback& callback) { | 652 const RemoteCharacteristicStopNotificationsCallback& callback) { |
| 670 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 653 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 671 RecordWebBluetoothFunctionCall( | 654 RecordWebBluetoothFunctionCall( |
| 672 UMAWebBluetoothFunction::CHARACTERISTIC_STOP_NOTIFICATIONS); | 655 UMAWebBluetoothFunction::CHARACTERISTIC_STOP_NOTIFICATIONS); |
| 673 | 656 |
| 674 const CacheQueryResult query_result = | 657 const CacheQueryResult query_result = |
| 675 QueryCacheForCharacteristic(characteristic_instance_id); | 658 QueryCacheForCharacteristic(characteristic_instance_id); |
| 676 | 659 |
| 677 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { | 660 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) |
| 678 return; | 661 return; |
| 679 } | |
| 680 | 662 |
| 681 auto notify_session_iter = | 663 auto notify_session_iter = |
| 682 characteristic_id_to_notify_session_.find(characteristic_instance_id); | 664 characteristic_id_to_notify_session_.find(characteristic_instance_id); |
| 683 if (notify_session_iter == characteristic_id_to_notify_session_.end()) { | 665 if (notify_session_iter == characteristic_id_to_notify_session_.end()) { |
| 684 // If the frame hasn't subscribed to notifications before we just | 666 // If the frame hasn't subscribed to notifications before we just |
| 685 // run the callback. | 667 // run the callback. |
| 686 callback.Run(); | 668 callback.Run(); |
| 687 return; | 669 return; |
| 688 } | 670 } |
| 689 notify_session_iter->second->Stop(base::Bind( | 671 notify_session_iter->second->Stop(base::Bind( |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 719 const base::Optional<BluetoothUUID>& services_uuid, | 701 const base::Optional<BluetoothUUID>& services_uuid, |
| 720 const RemoteServerGetPrimaryServicesCallback& callback, | 702 const RemoteServerGetPrimaryServicesCallback& callback, |
| 721 device::BluetoothDevice* device) { | 703 device::BluetoothDevice* device) { |
| 722 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 704 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 723 DCHECK(device->IsGattServicesDiscoveryComplete()); | 705 DCHECK(device->IsGattServicesDiscoveryComplete()); |
| 724 | 706 |
| 725 std::vector<device::BluetoothRemoteGattService*> services = | 707 std::vector<device::BluetoothRemoteGattService*> services = |
| 726 services_uuid ? GetPrimaryServicesByUUID(device, services_uuid.value()) | 708 services_uuid ? GetPrimaryServicesByUUID(device, services_uuid.value()) |
| 727 : GetPrimaryServices(device); | 709 : GetPrimaryServices(device); |
| 728 | 710 |
| 729 mojo::Array<blink::mojom::WebBluetoothRemoteGATTServicePtr> response_services; | 711 std::vector<blink::mojom::WebBluetoothRemoteGATTServicePtr> response_services; |
| 730 for (device::BluetoothRemoteGattService* service : services) { | 712 for (device::BluetoothRemoteGattService* service : services) { |
| 731 if (!allowed_devices_map_.IsOriginAllowedToAccessService( | 713 if (!allowed_devices_map_.IsOriginAllowedToAccessService( |
| 732 GetOrigin(), device_id, service->GetUUID())) { | 714 GetOrigin(), device_id, service->GetUUID())) { |
| 733 continue; | 715 continue; |
| 734 } | 716 } |
| 735 std::string service_instance_id = service->GetIdentifier(); | 717 std::string service_instance_id = service->GetIdentifier(); |
| 736 const std::string& device_address = device->GetAddress(); | 718 const std::string& device_address = device->GetAddress(); |
| 737 auto insert_result = service_id_to_device_address_.insert( | 719 auto insert_result = service_id_to_device_address_.insert( |
| 738 make_pair(service_instance_id, device_address)); | 720 make_pair(service_instance_id, device_address)); |
| 739 // If value is already in map, DCHECK it's valid. | 721 // If value is already in map, DCHECK it's valid. |
| 740 if (!insert_result.second) | 722 if (!insert_result.second) |
| 741 DCHECK_EQ(insert_result.first->second, device_address); | 723 DCHECK_EQ(insert_result.first->second, device_address); |
| 742 | 724 |
| 743 blink::mojom::WebBluetoothRemoteGATTServicePtr service_ptr = | 725 blink::mojom::WebBluetoothRemoteGATTServicePtr service_ptr = |
| 744 blink::mojom::WebBluetoothRemoteGATTService::New(); | 726 blink::mojom::WebBluetoothRemoteGATTService::New(); |
| 745 service_ptr->instance_id = service_instance_id; | 727 service_ptr->instance_id = service_instance_id; |
| 746 service_ptr->uuid = service->GetUUID().canonical_value(); | 728 service_ptr->uuid = service->GetUUID().canonical_value(); |
| 747 response_services.push_back(std::move(service_ptr)); | 729 response_services.push_back(std::move(service_ptr)); |
| 748 | 730 |
| 749 if (quantity == blink::mojom::WebBluetoothGATTQueryQuantity::SINGLE) { | 731 if (quantity == blink::mojom::WebBluetoothGATTQueryQuantity::SINGLE) |
| 750 break; | 732 break; |
| 751 } | |
| 752 } | 733 } |
| 753 | 734 |
| 754 if (!response_services.empty()) { | 735 if (!response_services.empty()) { |
| 755 VLOG(1) << "Services found in device."; | 736 VLOG(1) << "Services found in device."; |
| 756 RecordGetPrimaryServicesOutcome(quantity, | 737 RecordGetPrimaryServicesOutcome(quantity, |
| 757 UMAGetPrimaryServiceOutcome::SUCCESS); | 738 UMAGetPrimaryServiceOutcome::SUCCESS); |
| 758 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, | 739 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, |
| 759 std::move(response_services)); | 740 std::move(response_services)); |
| 760 return; | 741 return; |
| 761 } | 742 } |
| 762 | 743 |
| 763 VLOG(1) << "Services not found in device."; | 744 VLOG(1) << "Services not found in device."; |
| 764 RecordGetPrimaryServicesOutcome( | 745 RecordGetPrimaryServicesOutcome( |
| 765 quantity, services_uuid ? UMAGetPrimaryServiceOutcome::NOT_FOUND | 746 quantity, services_uuid ? UMAGetPrimaryServiceOutcome::NOT_FOUND |
| 766 : UMAGetPrimaryServiceOutcome::NO_SERVICES); | 747 : UMAGetPrimaryServiceOutcome::NO_SERVICES); |
| 767 callback.Run(services_uuid | 748 callback.Run(services_uuid |
| 768 ? blink::mojom::WebBluetoothResult::SERVICE_NOT_FOUND | 749 ? blink::mojom::WebBluetoothResult::SERVICE_NOT_FOUND |
| 769 : blink::mojom::WebBluetoothResult::NO_SERVICES_FOUND, | 750 : blink::mojom::WebBluetoothResult::NO_SERVICES_FOUND, |
| 770 nullptr /* services */); | 751 base::nullopt /* services */); |
| 771 } | 752 } |
| 772 | 753 |
| 773 void WebBluetoothServiceImpl::OnGetDeviceSuccess( | 754 void WebBluetoothServiceImpl::OnGetDeviceSuccess( |
| 774 const RequestDeviceCallback& callback, | 755 const RequestDeviceCallback& callback, |
| 775 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, | 756 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, |
| 776 const std::string& device_address) { | 757 const std::string& device_address) { |
| 777 device_chooser_controller_.reset(); | 758 device_chooser_controller_.reset(); |
| 778 | 759 |
| 779 const device::BluetoothDevice* const device = | 760 const device::BluetoothDevice* const device = |
| 780 GetAdapter()->GetDevice(device_address); | 761 GetAdapter()->GetDevice(device_address); |
| 781 if (device == nullptr) { | 762 if (device == nullptr) { |
| 782 VLOG(1) << "Device " << device_address << " no longer in adapter"; | 763 VLOG(1) << "Device " << device_address << " no longer in adapter"; |
| 783 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::CHOSEN_DEVICE_VANISHED); | 764 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::CHOSEN_DEVICE_VANISHED); |
| 784 callback.Run(blink::mojom::WebBluetoothResult::CHOSEN_DEVICE_VANISHED, | 765 callback.Run(blink::mojom::WebBluetoothResult::CHOSEN_DEVICE_VANISHED, |
| 785 nullptr /* device */); | 766 nullptr /* device */); |
| 786 return; | 767 return; |
| 787 } | 768 } |
| 788 | 769 |
| 789 const WebBluetoothDeviceId device_id_for_origin = | 770 const WebBluetoothDeviceId device_id_for_origin = |
| 790 allowed_devices_map_.AddDevice(GetOrigin(), device_address, options); | 771 allowed_devices_map_.AddDevice(GetOrigin(), device_address, options); |
| 791 | 772 |
| 792 VLOG(1) << "Device: " << device->GetNameForDisplay(); | 773 VLOG(1) << "Device: " << device->GetNameForDisplay(); |
| 793 | 774 |
| 794 blink::mojom::WebBluetoothDevicePtr device_ptr = | 775 blink::mojom::WebBluetoothDevicePtr device_ptr = |
| 795 blink::mojom::WebBluetoothDevice::New(); | 776 blink::mojom::WebBluetoothDevice::New(); |
| 796 device_ptr->id = device_id_for_origin; | 777 device_ptr->id = device_id_for_origin; |
| 797 device_ptr->name = device->GetName() ? mojo::String(device->GetName().value()) | 778 device_ptr->name = device->GetName(); |
| 798 : mojo::String(nullptr); | |
| 799 | 779 |
| 800 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::SUCCESS); | 780 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::SUCCESS); |
| 801 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, | 781 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, |
| 802 std::move(device_ptr)); | 782 std::move(device_ptr)); |
| 803 } | 783 } |
| 804 | 784 |
| 805 void WebBluetoothServiceImpl::OnGetDeviceFailed( | 785 void WebBluetoothServiceImpl::OnGetDeviceFailed( |
| 806 const RequestDeviceCallback& callback, | 786 const RequestDeviceCallback& callback, |
| 807 blink::mojom::WebBluetoothResult result) { | 787 blink::mojom::WebBluetoothResult result) { |
| 808 // Errors are recorded by the *device_chooser_controller_. | 788 // Errors are recorded by the *device_chooser_controller_. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 830 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 810 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 831 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time); | 811 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time); |
| 832 callback.Run(TranslateConnectErrorAndRecord(error_code)); | 812 callback.Run(TranslateConnectErrorAndRecord(error_code)); |
| 833 } | 813 } |
| 834 | 814 |
| 835 void WebBluetoothServiceImpl::OnReadValueSuccess( | 815 void WebBluetoothServiceImpl::OnReadValueSuccess( |
| 836 const RemoteCharacteristicReadValueCallback& callback, | 816 const RemoteCharacteristicReadValueCallback& callback, |
| 837 const std::vector<uint8_t>& value) { | 817 const std::vector<uint8_t>& value) { |
| 838 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 818 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 839 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS); | 819 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS); |
| 840 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, | 820 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, value); |
| 841 mojo::Array<uint8_t>::From(value)); | |
| 842 } | 821 } |
| 843 | 822 |
| 844 void WebBluetoothServiceImpl::OnReadValueFailed( | 823 void WebBluetoothServiceImpl::OnReadValueFailed( |
| 845 const RemoteCharacteristicReadValueCallback& callback, | 824 const RemoteCharacteristicReadValueCallback& callback, |
| 846 device::BluetoothRemoteGattService::GattErrorCode error_code) { | 825 device::BluetoothRemoteGattService::GattErrorCode error_code) { |
| 847 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 826 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 848 callback.Run(TranslateGATTErrorAndRecord( | 827 callback.Run(TranslateGATTErrorAndRecord( |
| 849 error_code, UMAGATTOperation::CHARACTERISTIC_READ), | 828 error_code, UMAGATTOperation::CHARACTERISTIC_READ), |
| 850 nullptr /* value */); | 829 base::nullopt /* value */); |
| 851 } | 830 } |
| 852 | 831 |
| 853 void WebBluetoothServiceImpl::OnWriteValueSuccess( | 832 void WebBluetoothServiceImpl::OnWriteValueSuccess( |
| 854 const RemoteCharacteristicWriteValueCallback& callback) { | 833 const RemoteCharacteristicWriteValueCallback& callback) { |
| 855 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 834 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 856 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::SUCCESS); | 835 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::SUCCESS); |
| 857 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); | 836 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); |
| 858 } | 837 } |
| 859 | 838 |
| 860 void WebBluetoothServiceImpl::OnWriteValueFailed( | 839 void WebBluetoothServiceImpl::OnWriteValueFailed( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 902 CrashRendererAndClosePipe(bad_message::BDH_DEVICE_NOT_ALLOWED_FOR_ORIGIN); | 881 CrashRendererAndClosePipe(bad_message::BDH_DEVICE_NOT_ALLOWED_FOR_ORIGIN); |
| 903 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); | 882 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); |
| 904 } | 883 } |
| 905 | 884 |
| 906 CacheQueryResult result; | 885 CacheQueryResult result; |
| 907 result.device = GetAdapter()->GetDevice(device_address); | 886 result.device = GetAdapter()->GetDevice(device_address); |
| 908 | 887 |
| 909 // When a device can't be found in the BluetoothAdapter, that generally | 888 // When a device can't be found in the BluetoothAdapter, that generally |
| 910 // indicates that it's gone out of range. We reject with a NetworkError in | 889 // indicates that it's gone out of range. We reject with a NetworkError in |
| 911 // that case. | 890 // that case. |
| 912 if (result.device == nullptr) { | 891 if (result.device == nullptr) |
| 913 result.outcome = CacheQueryOutcome::NO_DEVICE; | 892 result.outcome = CacheQueryOutcome::NO_DEVICE; |
| 914 } | |
| 915 return result; | 893 return result; |
| 916 } | 894 } |
| 917 | 895 |
| 918 CacheQueryResult WebBluetoothServiceImpl::QueryCacheForService( | 896 CacheQueryResult WebBluetoothServiceImpl::QueryCacheForService( |
| 919 const std::string& service_instance_id) { | 897 const std::string& service_instance_id) { |
| 920 auto device_iter = service_id_to_device_address_.find(service_instance_id); | 898 auto device_iter = service_id_to_device_address_.find(service_instance_id); |
| 921 | 899 |
| 922 // Kill the render, see "ID Not in Map Note" above. | 900 // Kill the render, see "ID Not in Map Note" above. |
| 923 if (device_iter == service_id_to_device_address_.end()) { | 901 if (device_iter == service_id_to_device_address_.end()) { |
| 924 CrashRendererAndClosePipe(bad_message::BDH_INVALID_SERVICE_ID); | 902 CrashRendererAndClosePipe(bad_message::BDH_INVALID_SERVICE_ID); |
| 925 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); | 903 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); |
| 926 } | 904 } |
| 927 | 905 |
| 928 const WebBluetoothDeviceId* device_id = | 906 const WebBluetoothDeviceId* device_id = |
| 929 allowed_devices_map_.GetDeviceId(GetOrigin(), device_iter->second); | 907 allowed_devices_map_.GetDeviceId(GetOrigin(), device_iter->second); |
| 930 // Kill the renderer if origin is not allowed to access the device. | 908 // Kill the renderer if origin is not allowed to access the device. |
| 931 if (device_id == nullptr) { | 909 if (device_id == nullptr) { |
| 932 CrashRendererAndClosePipe(bad_message::BDH_DEVICE_NOT_ALLOWED_FOR_ORIGIN); | 910 CrashRendererAndClosePipe(bad_message::BDH_DEVICE_NOT_ALLOWED_FOR_ORIGIN); |
| 933 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); | 911 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); |
| 934 } | 912 } |
| 935 | 913 |
| 936 CacheQueryResult result = QueryCacheForDevice(*device_id); | 914 CacheQueryResult result = QueryCacheForDevice(*device_id); |
| 937 if (result.outcome != CacheQueryOutcome::SUCCESS) { | 915 if (result.outcome != CacheQueryOutcome::SUCCESS) |
| 938 return result; | 916 return result; |
| 939 } | |
| 940 | 917 |
| 941 result.service = result.device->GetGattService(service_instance_id); | 918 result.service = result.device->GetGattService(service_instance_id); |
| 942 if (result.service == nullptr) { | 919 if (result.service == nullptr) { |
| 943 result.outcome = CacheQueryOutcome::NO_SERVICE; | 920 result.outcome = CacheQueryOutcome::NO_SERVICE; |
| 944 } else if (!allowed_devices_map_.IsOriginAllowedToAccessService( | 921 } else if (!allowed_devices_map_.IsOriginAllowedToAccessService( |
| 945 GetOrigin(), *device_id, result.service->GetUUID())) { | 922 GetOrigin(), *device_id, result.service->GetUUID())) { |
| 946 CrashRendererAndClosePipe(bad_message::BDH_SERVICE_NOT_ALLOWED_FOR_ORIGIN); | 923 CrashRendererAndClosePipe(bad_message::BDH_SERVICE_NOT_ALLOWED_FOR_ORIGIN); |
| 947 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); | 924 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); |
| 948 } | 925 } |
| 949 return result; | 926 return result; |
| 950 } | 927 } |
| 951 | 928 |
| 952 CacheQueryResult WebBluetoothServiceImpl::QueryCacheForCharacteristic( | 929 CacheQueryResult WebBluetoothServiceImpl::QueryCacheForCharacteristic( |
| 953 const std::string& characteristic_instance_id) { | 930 const std::string& characteristic_instance_id) { |
| 954 auto characteristic_iter = | 931 auto characteristic_iter = |
| 955 characteristic_id_to_service_id_.find(characteristic_instance_id); | 932 characteristic_id_to_service_id_.find(characteristic_instance_id); |
| 956 | 933 |
| 957 // Kill the render, see "ID Not in Map Note" above. | 934 // Kill the render, see "ID Not in Map Note" above. |
| 958 if (characteristic_iter == characteristic_id_to_service_id_.end()) { | 935 if (characteristic_iter == characteristic_id_to_service_id_.end()) { |
| 959 CrashRendererAndClosePipe(bad_message::BDH_INVALID_CHARACTERISTIC_ID); | 936 CrashRendererAndClosePipe(bad_message::BDH_INVALID_CHARACTERISTIC_ID); |
| 960 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); | 937 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER); |
| 961 } | 938 } |
| 962 | 939 |
| 963 CacheQueryResult result = QueryCacheForService(characteristic_iter->second); | 940 CacheQueryResult result = QueryCacheForService(characteristic_iter->second); |
| 964 | 941 |
| 965 if (result.outcome != CacheQueryOutcome::SUCCESS) { | 942 if (result.outcome != CacheQueryOutcome::SUCCESS) |
| 966 return result; | 943 return result; |
| 967 } | |
| 968 | 944 |
| 969 result.characteristic = | 945 result.characteristic = |
| 970 result.service->GetCharacteristic(characteristic_instance_id); | 946 result.service->GetCharacteristic(characteristic_instance_id); |
| 971 | 947 |
| 972 if (result.characteristic == nullptr) { | 948 if (result.characteristic == nullptr) |
| 973 result.outcome = CacheQueryOutcome::NO_CHARACTERISTIC; | 949 result.outcome = CacheQueryOutcome::NO_CHARACTERISTIC; |
| 974 } | |
| 975 | 950 |
| 976 return result; | 951 return result; |
| 977 } | 952 } |
| 978 | 953 |
| 979 RenderProcessHost* WebBluetoothServiceImpl::GetRenderProcessHost() { | 954 RenderProcessHost* WebBluetoothServiceImpl::GetRenderProcessHost() { |
| 980 return render_frame_host_->GetProcess(); | 955 return render_frame_host_->GetProcess(); |
| 981 } | 956 } |
| 982 | 957 |
| 983 device::BluetoothAdapter* WebBluetoothServiceImpl::GetAdapter() { | 958 device::BluetoothAdapter* WebBluetoothServiceImpl::GetAdapter() { |
| 984 return BluetoothAdapterFactoryWrapper::Get().GetAdapter(this); | 959 return BluetoothAdapterFactoryWrapper::Get().GetAdapter(this); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 1000 characteristic_id_to_service_id_.clear(); | 975 characteristic_id_to_service_id_.clear(); |
| 1001 service_id_to_device_address_.clear(); | 976 service_id_to_device_address_.clear(); |
| 1002 connected_devices_.reset( | 977 connected_devices_.reset( |
| 1003 new FrameConnectedBluetoothDevices(render_frame_host_)); | 978 new FrameConnectedBluetoothDevices(render_frame_host_)); |
| 1004 allowed_devices_map_ = BluetoothAllowedDevicesMap(); | 979 allowed_devices_map_ = BluetoothAllowedDevicesMap(); |
| 1005 device_chooser_controller_.reset(); | 980 device_chooser_controller_.reset(); |
| 1006 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this); | 981 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this); |
| 1007 } | 982 } |
| 1008 | 983 |
| 1009 } // namespace content | 984 } // namespace content |
| OLD | NEW |