chromium/content/browser/bluetooth/bluetooth_device_chooser_controller.cc

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/bluetooth/bluetooth_device_chooser_controller.h"

#include <set>
#include <string>
#include <unordered_set>

#include "base/containers/contains.h"
#include "base/containers/flat_set.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/types/optional_util.h"
#include "content/browser/bluetooth/bluetooth_blocklist.h"
#include "content/browser/bluetooth/bluetooth_metrics.h"
#include "content/browser/bluetooth/bluetooth_util.h"
#include "content/browser/bluetooth/web_bluetooth_service_impl.h"
#include "content/browser/devtools/devtools_instrumentation.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/public/browser/bluetooth_delegate.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_client.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_discovery_session.h"

BluetoothUUID;
UUIDSet;
ManufacturerDataMap;
WebBluetoothResult;

namespace {

// Signal Strength Display Notes:
//
// RSSI values are displayed by the chooser to empower a user to differentiate
// between multiple devices with the same name, comparing devices with different
// names is a secondary goal. It is important that a user is able to move closer
// and farther away from a device and have it transition between two different
// signal strength levels, thus we want to spread RSSI values out evenly accross
// displayed levels.
//
// Real-world RSSI values from UMA in RecordRSSISignalStrength are used to
// select 4 threshold points equally spaced through the CDF.
//
// CDF: Cumulative Distribution Function:
// https://en.wikipedia.org/wiki/Cumulative_distribution_function
//
// This data was last updated using a 28-day average of metrics recorded on
// 2024-03-10.
const int k20thPercentileRSSI =;
const int k40thPercentileRSSI =;
const int k60thPercentileRSSI =;
const int k80thPercentileRSSI =;

// Client name for logging in BLE scanning.
constexpr char kScanClientName[] =;

}  // namespace

namespace content {

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

namespace {

#if DCHECK_IS_ON()
void LogRequestDeviceOptions(
    const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) {}
#else
void LogRequestDeviceOptions(
    const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) {
  DVLOG(1) << "requestDevice called with the following filters: ";
  DVLOG(1) << "acceptAllDevices: " << options->accept_all_devices;

  if (!options->filters)
    return;

  int i = 0;
  for (const auto& filter : options->filters.value()) {
    DVLOG(1) << "Filter #" << ++i;
    if (filter->name)
      DVLOG(1) << "Name: " << filter->name.value();

    if (filter->name_prefix)
      DVLOG(1) << "Name Prefix: " << filter->name_prefix.value();

    if (filter->services) {
      base::Value::List services_list;
      for (const auto& service : filter->services.value())
        services_list.Append(service.canonical_value());
      DVLOG(1) << "Services: " << services_list;
    }

    if (filter->manufacturer_data) {
      base::Value::List manufacturer_data_list;
      for (const auto& manufacturer_data : filter->manufacturer_data.value()) {
        base::Value::List filter_data_list;
        base::Value::List filter_mask_list;
        for (const auto& data_filter : manufacturer_data.second) {
          filter_data_list.Append(data_filter->data);
          filter_mask_list.Append(data_filter->mask);
        }
        base::Value::Dict data_filter_dict;
        data_filter_dict.Set("Company Identifier", manufacturer_data.first->id);
        data_filter_dict.Set("Data", std::move(filter_data_list));
        data_filter_dict.Set("Mask", std::move(filter_mask_list));
        manufacturer_data_list.Append(std::move(data_filter_dict));
      }
      DVLOG(1) << "Manufacturer Data: " << manufacturer_data_list;
    }
  }
}
#endif

bool MatchesFilter(const std::string* device_name,
                   const UUIDSet& device_uuids,
                   const ManufacturerDataMap& device_manufacturer_data,
                   const blink::mojom::WebBluetoothLeScanFilterPtr& filter) {}

bool MatchesFilters(
    const std::string* device_name,
    const UUIDSet& device_uuids,
    const ManufacturerDataMap& device_manufacturer_data,
    const std::optional<std::vector<blink::mojom::WebBluetoothLeScanFilterPtr>>&
        filters) {}

void StopDiscoverySession(
    std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) {}

}  // namespace

BluetoothDeviceChooserController::BluetoothDeviceRequestPromptInfo::
    BluetoothDeviceRequestPromptInfo(
        BluetoothDeviceChooserController& controller)
    :{}

BluetoothDeviceChooserController::BluetoothDeviceRequestPromptInfo::
    ~BluetoothDeviceRequestPromptInfo() = default;

std::vector<DevtoolsDeviceRequestPromptDevice>
BluetoothDeviceChooserController::BluetoothDeviceRequestPromptInfo::
    GetDevices() {}

bool BluetoothDeviceChooserController::BluetoothDeviceRequestPromptInfo::
    SelectDevice(const std::string& select_device_id) {}

void BluetoothDeviceChooserController::BluetoothDeviceRequestPromptInfo::
    Cancel() {}

BluetoothDeviceChooserController::BluetoothDeviceChooserController(
    WebBluetoothServiceImpl* web_bluetooth_service,
    RenderFrameHost& render_frame_host,
    scoped_refptr<device::BluetoothAdapter> adapter)
    :{}

BluetoothDeviceChooserController::~BluetoothDeviceChooserController() {}

void BluetoothDeviceChooserController::GetDevice(
    blink::mojom::WebBluetoothRequestDeviceOptionsPtr options,
    Callback callback) {}

void BluetoothDeviceChooserController::AddFilteredDevice(
    const device::BluetoothDevice& device) {}

void BluetoothDeviceChooserController::AdapterPoweredChanged(bool powered) {}

int BluetoothDeviceChooserController::CalculateSignalStrengthLevel(
    int8_t rssi) {}

void BluetoothDeviceChooserController::SetTestScanDurationForTesting(
    TestScanDurationSetting setting) {}

void BluetoothDeviceChooserController::PopulateConnectedDevices() {}

void BluetoothDeviceChooserController::StartDeviceDiscovery() {}

void BluetoothDeviceChooserController::StopDeviceDiscovery() {}

void BluetoothDeviceChooserController::OnStartDiscoverySessionSuccess(
    std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) {}

void BluetoothDeviceChooserController::OnStartDiscoverySessionFailed() {}

void BluetoothDeviceChooserController::OnBluetoothChooserEvent(
    BluetoothChooserEvent event,
    const std::string& device_address) {}

void BluetoothDeviceChooserController::PostSuccessCallback(
    const std::string& device_address) {}

void BluetoothDeviceChooserController::PostErrorCallback(
    WebBluetoothResult error) {}

// static
std::unique_ptr<device::BluetoothDiscoveryFilter>
BluetoothDeviceChooserController::ComputeScanFilter(
    const std::optional<std::vector<blink::mojom::WebBluetoothLeScanFilterPtr>>&
        filters) {}

}  // namespace content