Skip to content

Commit

Permalink
bluetooth: Allow variable test scan duration
Browse files Browse the repository at this point in the history
This change enables the scan duration to be set for a test. This
enables tests to have the ability to manipulate events during a scan.
The purpose for this change is to implement a scanning API for Web
Bluetooth Tests. The design for this API is found in the following
document:
https://docs.google.com/document/d/1XFl_4ZAgO8ddM6U53A9AfUuZeWgJnlYD5wtbXqEpzeg

BUG=719826

Change-Id: I6e060ee8d4d149134331c983a9cb884d9ae557f5
Reviewed-on: https://chromium-review.googlesource.com/930123
Commit-Queue: Ovidio Henriquez <odejesush@chromium.org>
Reviewed-by: Giovanni Ortuño Urquidi <ortuno@chromium.org>
Reviewed-by: Conley Owens <cco3@chromium.org>
Cr-Commit-Position: refs/heads/master@{#540287}
  • Loading branch information
odejesush authored and Commit Bot committed Mar 1, 2018
1 parent 885d708 commit 327e84d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
24 changes: 13 additions & 11 deletions content/browser/bluetooth/bluetooth_device_chooser_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,14 @@ const int k80thPercentileRSSI = -52;

namespace content {

bool BluetoothDeviceChooserController::use_test_scan_duration_ = false;
// Sets the default duration for a Bluetooth scan to 60 seconds.
int64_t BluetoothDeviceChooserController::scan_duration_ = 60;

namespace {
// Max length of device name in filter. Bluetooth 5.0 3.C.3.2.2.3 states that
// the maximum device name length is 248 bytes (UTF-8 encoded).
constexpr size_t kMaxLengthForDeviceName = 248;

// The duration of a Bluetooth Scan in seconds.
constexpr int kScanDuration = 60;
constexpr int kTestScanDuration = 0;

void LogRequestDeviceOptions(
const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) {
DVLOG(1) << "requestDevice called with the following filters: ";
Expand Down Expand Up @@ -281,10 +278,7 @@ BluetoothDeviceChooserController::BluetoothDeviceChooserController(
web_contents_(WebContents::FromRenderFrameHost(render_frame_host_)),
discovery_session_timer_(
FROM_HERE,
// TODO(jyasskin): Add a way for tests to control the dialog
// directly, and change this to a reasonable discovery timeout.
base::TimeDelta::FromSeconds(
use_test_scan_duration_ ? kTestScanDuration : kScanDuration),
base::TimeDelta::FromSeconds(scan_duration_),
base::Bind(&BluetoothDeviceChooserController::StopDeviceDiscovery,
// base::Timer guarantees it won't call back after its
// destructor starts.
Expand Down Expand Up @@ -490,8 +484,16 @@ int BluetoothDeviceChooserController::CalculateSignalStrengthLevel(
}
}

void BluetoothDeviceChooserController::SetTestScanDurationForTesting() {
BluetoothDeviceChooserController::use_test_scan_duration_ = true;
void BluetoothDeviceChooserController::SetTestScanDurationForTesting(
TestScanDurationSetting setting) {
switch (setting) {
case TestScanDurationSetting::IMMEDIATE_TIMEOUT:
scan_duration_ = 0;
break;
case TestScanDurationSetting::NEVER_TIMEOUT:
scan_duration_ = base::TimeDelta::Max().InSeconds();
break;
}
}

void BluetoothDeviceChooserController::PopulateConnectedDevices() {
Expand Down
18 changes: 13 additions & 5 deletions content/browser/bluetooth/bluetooth_device_chooser_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class CONTENT_EXPORT BluetoothDeviceChooserController final {
typedef base::Callback<void(blink::mojom::WebBluetoothResult result)>
ErrorCallback;

enum class TestScanDurationSetting { IMMEDIATE_TIMEOUT, NEVER_TIMEOUT };

// |web_bluetooth_service_| service that owns this class.
// |render_frame_host| should be the RenderFrameHost that owns the
// |web_bluetooth_service_|.
Expand Down Expand Up @@ -83,9 +85,14 @@ class CONTENT_EXPORT BluetoothDeviceChooserController final {
// present in a received radio signal.
static int CalculateSignalStrengthLevel(int8_t rssi);

// After this method is called any new instance of
// BluetoothDeviceChooserController will have a scan duration of 0.
static void SetTestScanDurationForTesting();
// After this method is called, any new instance of
// BluetoothDeviceChooserController will have a scan duration determined by
// the |setting| enum. The possible enumerations are described below:
// IMMEDIATE_TIMEOUT: Sets the scan duration to 0 seconds.
// NEVER_TIMEOUT: Sets the scan duration to INT_MAX seconds.
static void SetTestScanDurationForTesting(
TestScanDurationSetting setting =
TestScanDurationSetting::IMMEDIATE_TIMEOUT);

private:
// Populates the chooser with the GATT connected devices.
Expand Down Expand Up @@ -115,8 +122,9 @@ class CONTENT_EXPORT BluetoothDeviceChooserController final {
// Helper function to asynchronously run error_callback_.
void PostErrorCallback(blink::mojom::WebBluetoothResult result);

// If true all new instances of this class will have a scan duration of 0.
static bool use_test_scan_duration_;
// Stores the scan duration to use for the discovery session timer.
// The default value is 60 seconds.
static int64_t scan_duration_;

// The adapter used to get existing devices and start a discovery session.
device::BluetoothAdapter* adapter_;
Expand Down

0 comments on commit 327e84d

Please sign in to comment.