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 #include "content/browser/bluetooth/bluetooth_device_chooser_controller.h" | 5 #include "content/browser/bluetooth/bluetooth_device_chooser_controller.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <unordered_set> | 9 #include <unordered_set> |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 constexpr size_t kMaxLengthForDeviceName = | 45 constexpr size_t kMaxLengthForDeviceName = |
| 46 29; // max length of device name in filter. | 46 29; // max length of device name in filter. |
| 47 | 47 |
| 48 // The duration of a Bluetooth Scan in seconds. | 48 // The duration of a Bluetooth Scan in seconds. |
| 49 constexpr int kScanDuration = 10; | 49 constexpr int kScanDuration = 10; |
| 50 constexpr int kTestScanDuration = 0; | 50 constexpr int kTestScanDuration = 0; |
| 51 | 51 |
| 52 void LogRequestDeviceOptions( | 52 void LogRequestDeviceOptions( |
| 53 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { | 53 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
| 54 VLOG(1) << "requestDevice called with the following filters: "; | 54 VLOG(1) << "requestDevice called with the following filters: "; |
| 55 VLOG(1) << "Accept All Devices: " << options->accept_all_devices; | |
| 55 int i = 0; | 56 int i = 0; |
| 56 for (const auto& filter : options->filters) { | 57 for (const auto& filter : options->filters) { |
| 57 VLOG(1) << "Filter #" << ++i; | 58 VLOG(1) << "Filter #" << ++i; |
| 58 if (!filter->name.is_null()) | 59 if (!filter->name.is_null()) |
| 59 VLOG(1) << "Name: " << filter->name; | 60 VLOG(1) << "Name: " << filter->name; |
| 60 | 61 |
| 61 if (!filter->name_prefix.is_null()) | 62 if (!filter->name_prefix.is_null()) |
| 62 VLOG(1) << "Name Prefix: " << filter->name_prefix; | 63 VLOG(1) << "Name Prefix: " << filter->name_prefix; |
| 63 | 64 |
| 64 if (!filter->services.is_null()) { | 65 if (!filter->services.is_null()) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 84 return true; | 85 return true; |
| 85 if (!filter->name_prefix.is_null() && filter->name_prefix.size() == 0) | 86 if (!filter->name_prefix.is_null() && filter->name_prefix.size() == 0) |
| 86 return true; | 87 return true; |
| 87 if (!filter->name_prefix.is_null() && | 88 if (!filter->name_prefix.is_null() && |
| 88 filter->name_prefix.size() > kMaxLengthForDeviceName) | 89 filter->name_prefix.size() > kMaxLengthForDeviceName) |
| 89 return true; | 90 return true; |
| 90 | 91 |
| 91 return false; | 92 return false; |
| 92 } | 93 } |
| 93 | 94 |
| 94 bool HasEmptyOrInvalidFilter( | 95 bool HasEmptyOrInvalidFilter( |
|
ortuno
2016/10/31 00:00:02
Add the same check as the one on the renderer side
| |
| 95 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) { | 96 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) { |
| 96 return filters.empty() | 97 return filters.empty() |
| 97 ? true | 98 ? true |
| 98 : filters.end() != std::find_if(filters.begin(), filters.end(), | 99 : filters.end() != std::find_if(filters.begin(), filters.end(), |
| 99 IsEmptyOrInvalidFilter); | 100 IsEmptyOrInvalidFilter); |
| 100 } | 101 } |
| 101 | 102 |
| 102 bool MatchesFilter(const device::BluetoothDevice& device, | 103 bool MatchesFilter(const device::BluetoothDevice& device, |
| 103 const blink::mojom::WebBluetoothScanFilterPtr& filter) { | 104 const blink::mojom::WebBluetoothScanFilterPtr& filter) { |
| 104 if (!filter->name.is_null()) { | 105 if (!filter->name.is_null()) { |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 const ErrorCallback& error_callback) { | 236 const ErrorCallback& error_callback) { |
| 236 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 237 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 237 | 238 |
| 238 // GetDevice should only be called once. | 239 // GetDevice should only be called once. |
| 239 DCHECK(success_callback_.is_null()); | 240 DCHECK(success_callback_.is_null()); |
| 240 DCHECK(error_callback_.is_null()); | 241 DCHECK(error_callback_.is_null()); |
| 241 | 242 |
| 242 success_callback_ = success_callback; | 243 success_callback_ = success_callback; |
| 243 error_callback_ = error_callback; | 244 error_callback_ = error_callback; |
| 244 | 245 |
| 245 // The renderer should never send empty filters. | 246 // The renderer should never send empty filters if acceptAllDevices is false. |
| 246 if (HasEmptyOrInvalidFilter(options->filters)) { | 247 if (!options->accept_all_devices && |
|
ortuno
2016/10/31 00:00:02
You can remove the accept_all_devices check. See c
| |
| 248 HasEmptyOrInvalidFilter(options->filters)) { | |
| 247 web_bluetooth_service_->CrashRendererAndClosePipe( | 249 web_bluetooth_service_->CrashRendererAndClosePipe( |
| 248 bad_message::BDH_EMPTY_OR_INVALID_FILTERS); | 250 bad_message::BDH_EMPTY_OR_INVALID_FILTERS); |
| 249 return; | 251 return; |
| 250 } | 252 } |
| 251 options_ = std::move(options); | 253 options_ = std::move(options); |
| 252 LogRequestDeviceOptions(options_); | 254 LogRequestDeviceOptions(options_); |
| 253 | 255 |
| 254 // Check blacklist to reject invalid filters and adjust optional_services. | 256 // Check blacklist to reject invalid filters if acceptAllDevices is false. |
| 255 if (BluetoothBlacklist::Get().IsExcluded(options_->filters)) { | 257 if (!options_->accept_all_devices && |
|
ortuno
2016/10/31 00:00:02
!options_->filters.is_null()
| |
| 258 BluetoothBlacklist::Get().IsExcluded(options_->filters)) { | |
| 256 RecordRequestDeviceOutcome( | 259 RecordRequestDeviceOutcome( |
| 257 UMARequestDeviceOutcome::BLACKLISTED_SERVICE_IN_FILTER); | 260 UMARequestDeviceOutcome::BLACKLISTED_SERVICE_IN_FILTER); |
| 258 PostErrorCallback( | 261 PostErrorCallback( |
| 259 blink::mojom::WebBluetoothResult::REQUEST_DEVICE_WITH_BLACKLISTED_UUID); | 262 blink::mojom::WebBluetoothResult::REQUEST_DEVICE_WITH_BLACKLISTED_UUID); |
| 260 return; | 263 return; |
| 261 } | 264 } |
| 265 // Check blacklist to adjust optional_services. | |
| 262 BluetoothBlacklist::Get().RemoveExcludedUUIDs(options_.get()); | 266 BluetoothBlacklist::Get().RemoveExcludedUUIDs(options_.get()); |
| 263 | 267 |
| 264 const url::Origin requesting_origin = | 268 const url::Origin requesting_origin = |
| 265 render_frame_host_->GetLastCommittedOrigin(); | 269 render_frame_host_->GetLastCommittedOrigin(); |
| 266 const url::Origin embedding_origin = | 270 const url::Origin embedding_origin = |
| 267 web_contents_->GetMainFrame()->GetLastCommittedOrigin(); | 271 web_contents_->GetMainFrame()->GetLastCommittedOrigin(); |
| 268 | 272 |
| 269 // TODO(crbug.com/518042): Enforce correctly-delegated permissions instead of | 273 // TODO(crbug.com/518042): Enforce correctly-delegated permissions instead of |
| 270 // matching origins. When relaxing this, take care to handle non-sandboxed | 274 // matching origins. When relaxing this, take care to handle non-sandboxed |
| 271 // unique origins. | 275 // unique origins. |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 chooser_->SetAdapterPresence( | 349 chooser_->SetAdapterPresence( |
| 346 BluetoothChooser::AdapterPresence::POWERED_OFF); | 350 BluetoothChooser::AdapterPresence::POWERED_OFF); |
| 347 return; | 351 return; |
| 348 } | 352 } |
| 349 | 353 |
| 350 StartDeviceDiscovery(); | 354 StartDeviceDiscovery(); |
| 351 } | 355 } |
| 352 | 356 |
| 353 void BluetoothDeviceChooserController::AddFilteredDevice( | 357 void BluetoothDeviceChooserController::AddFilteredDevice( |
| 354 const device::BluetoothDevice& device) { | 358 const device::BluetoothDevice& device) { |
| 355 if (chooser_.get() && MatchesFilters(device, options_->filters)) { | 359 if (chooser_.get() && (options_->accept_all_devices || |
|
ortuno
2016/10/31 00:00:02
options_->filters.is_null() || ...
| |
| 360 MatchesFilters(device, options_->filters))) { | |
| 356 base::Optional<int8_t> rssi = device.GetInquiryRSSI(); | 361 base::Optional<int8_t> rssi = device.GetInquiryRSSI(); |
| 357 chooser_->AddOrUpdateDevice( | 362 chooser_->AddOrUpdateDevice( |
| 358 device.GetAddress(), !!device.GetName() /* should_update_name */, | 363 device.GetAddress(), !!device.GetName() /* should_update_name */, |
| 359 device.GetNameForDisplay(), device.IsGattConnected(), | 364 device.GetNameForDisplay(), device.IsGattConnected(), |
| 360 web_bluetooth_service_->IsDevicePaired(device.GetAddress()), | 365 web_bluetooth_service_->IsDevicePaired(device.GetAddress()), |
| 361 rssi ? CalculateSignalStrengthLevel(rssi.value()) : -1); | 366 rssi ? CalculateSignalStrengthLevel(rssi.value()) : -1); |
| 362 } | 367 } |
| 363 } | 368 } |
| 364 | 369 |
| 365 void BluetoothDeviceChooserController::AdapterPoweredChanged(bool powered) { | 370 void BluetoothDeviceChooserController::AdapterPoweredChanged(bool powered) { |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 512 | 517 |
| 513 void BluetoothDeviceChooserController::PostErrorCallback( | 518 void BluetoothDeviceChooserController::PostErrorCallback( |
| 514 blink::mojom::WebBluetoothResult error) { | 519 blink::mojom::WebBluetoothResult error) { |
| 515 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | 520 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 516 FROM_HERE, base::Bind(error_callback_, error))) { | 521 FROM_HERE, base::Bind(error_callback_, error))) { |
| 517 LOG(WARNING) << "No TaskRunner."; | 522 LOG(WARNING) << "No TaskRunner."; |
| 518 } | 523 } |
| 519 } | 524 } |
| 520 | 525 |
| 521 } // namespace content | 526 } // namespace content |
| OLD | NEW |