| 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 10 matching lines...) Expand all Loading... |
| 21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/browser/web_contents_delegate.h" | 22 #include "content/public/browser/web_contents_delegate.h" |
| 23 #include "device/bluetooth/bluetooth_adapter.h" | 23 #include "device/bluetooth/bluetooth_adapter.h" |
| 24 #include "device/bluetooth/bluetooth_common.h" | 24 #include "device/bluetooth/bluetooth_common.h" |
| 25 #include "device/bluetooth/bluetooth_discovery_session.h" | 25 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 26 | 26 |
| 27 using device::BluetoothUUID; | 27 using device::BluetoothUUID; |
| 28 | 28 |
| 29 namespace content { | 29 namespace content { |
| 30 | 30 |
| 31 bool BluetoothDeviceChooserController::use_test_scan_duration_ = false; |
| 32 |
| 31 namespace { | 33 namespace { |
| 32 constexpr size_t kMaxLengthForDeviceName = | 34 constexpr size_t kMaxLengthForDeviceName = |
| 33 29; // max length of device name in filter. | 35 29; // max length of device name in filter. |
| 34 | 36 |
| 37 // The duration of a Bluetooth Scan in seconds. |
| 38 constexpr int kScanDuration = 10; |
| 39 constexpr int kTestScanDuration = 0; |
| 40 |
| 35 void LogRequestDeviceOptions( | 41 void LogRequestDeviceOptions( |
| 36 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { | 42 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
| 37 VLOG(1) << "requestDevice called with the following filters: "; | 43 VLOG(1) << "requestDevice called with the following filters: "; |
| 38 int i = 0; | 44 int i = 0; |
| 39 for (const auto& filter : options->filters) { | 45 for (const auto& filter : options->filters) { |
| 40 VLOG(1) << "Filter #" << ++i; | 46 VLOG(1) << "Filter #" << ++i; |
| 41 if (!filter->name.is_null()) | 47 if (!filter->name.is_null()) |
| 42 VLOG(1) << "Name: " << filter->name; | 48 VLOG(1) << "Name: " << filter->name; |
| 43 | 49 |
| 44 if (!filter->name_prefix.is_null()) | 50 if (!filter->name_prefix.is_null()) |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 } | 180 } |
| 175 NOTREACHED(); | 181 NOTREACHED(); |
| 176 return UMARequestDeviceOutcome::SUCCESS; | 182 return UMARequestDeviceOutcome::SUCCESS; |
| 177 } | 183 } |
| 178 | 184 |
| 179 } // namespace | 185 } // namespace |
| 180 | 186 |
| 181 BluetoothDeviceChooserController::BluetoothDeviceChooserController( | 187 BluetoothDeviceChooserController::BluetoothDeviceChooserController( |
| 182 WebBluetoothServiceImpl* web_bluetooth_service, | 188 WebBluetoothServiceImpl* web_bluetooth_service, |
| 183 RenderFrameHost* render_frame_host, | 189 RenderFrameHost* render_frame_host, |
| 184 device::BluetoothAdapter* adapter, | 190 device::BluetoothAdapter* adapter) |
| 185 base::TimeDelta scan_duration) | |
| 186 : adapter_(adapter), | 191 : adapter_(adapter), |
| 187 web_bluetooth_service_(web_bluetooth_service), | 192 web_bluetooth_service_(web_bluetooth_service), |
| 188 render_frame_host_(render_frame_host), | 193 render_frame_host_(render_frame_host), |
| 189 web_contents_(WebContents::FromRenderFrameHost(render_frame_host_)), | 194 web_contents_(WebContents::FromRenderFrameHost(render_frame_host_)), |
| 190 discovery_session_timer_( | 195 discovery_session_timer_( |
| 191 FROM_HERE, | 196 FROM_HERE, |
| 192 // TODO(jyasskin): Add a way for tests to control the dialog | 197 // TODO(jyasskin): Add a way for tests to control the dialog |
| 193 // directly, and change this to a reasonable discovery timeout. | 198 // directly, and change this to a reasonable discovery timeout. |
| 194 scan_duration, | 199 base::TimeDelta::FromSeconds( |
| 200 use_test_scan_duration_ ? kTestScanDuration : kScanDuration), |
| 195 base::Bind(&BluetoothDeviceChooserController::StopDeviceDiscovery, | 201 base::Bind(&BluetoothDeviceChooserController::StopDeviceDiscovery, |
| 196 // base::Timer guarantees it won't call back after its | 202 // base::Timer guarantees it won't call back after its |
| 197 // destructor starts. | 203 // destructor starts. |
| 198 base::Unretained(this)), | 204 base::Unretained(this)), |
| 199 /*is_repeating=*/false), | 205 /*is_repeating=*/false), |
| 200 weak_ptr_factory_(this) { | 206 weak_ptr_factory_(this) { |
| 201 CHECK(adapter_); | 207 CHECK(adapter_); |
| 202 } | 208 } |
| 203 | 209 |
| 204 BluetoothDeviceChooserController::~BluetoothDeviceChooserController() { | 210 BluetoothDeviceChooserController::~BluetoothDeviceChooserController() { |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 OnBluetoothChooserEvent(BluetoothChooser::Event::RESCAN, | 355 OnBluetoothChooserEvent(BluetoothChooser::Event::RESCAN, |
| 350 "" /* device_address */); | 356 "" /* device_address */); |
| 351 } | 357 } |
| 352 } | 358 } |
| 353 | 359 |
| 354 if (!powered) { | 360 if (!powered) { |
| 355 discovery_session_timer_.Stop(); | 361 discovery_session_timer_.Stop(); |
| 356 } | 362 } |
| 357 } | 363 } |
| 358 | 364 |
| 365 void BluetoothDeviceChooserController::SetTestScanDurationForTesting() { |
| 366 BluetoothDeviceChooserController::use_test_scan_duration_ = true; |
| 367 } |
| 368 |
| 359 void BluetoothDeviceChooserController::PopulateFoundDevices() { | 369 void BluetoothDeviceChooserController::PopulateFoundDevices() { |
| 360 VLOG(1) << "Populating " << adapter_->GetDevices().size() | 370 VLOG(1) << "Populating " << adapter_->GetDevices().size() |
| 361 << " devices in chooser."; | 371 << " devices in chooser."; |
| 362 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { | 372 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { |
| 363 AddFilteredDevice(*device); | 373 AddFilteredDevice(*device); |
| 364 } | 374 } |
| 365 } | 375 } |
| 366 | 376 |
| 367 void BluetoothDeviceChooserController::StartDeviceDiscovery() { | 377 void BluetoothDeviceChooserController::StartDeviceDiscovery() { |
| 368 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 378 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 | 479 |
| 470 void BluetoothDeviceChooserController::PostErrorCallback( | 480 void BluetoothDeviceChooserController::PostErrorCallback( |
| 471 blink::mojom::WebBluetoothError error) { | 481 blink::mojom::WebBluetoothError error) { |
| 472 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | 482 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 473 FROM_HERE, base::Bind(error_callback_, error))) { | 483 FROM_HERE, base::Bind(error_callback_, error))) { |
| 474 LOG(WARNING) << "No TaskRunner."; | 484 LOG(WARNING) << "No TaskRunner."; |
| 475 } | 485 } |
| 476 } | 486 } |
| 477 | 487 |
| 478 } // namespace content | 488 } // namespace content |
| OLD | NEW |