1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116

content / web_test / browser / fake_bluetooth_chooser.cc [blame]

// Copyright 2018 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/web_test/browser/fake_bluetooth_chooser.h"

#include <string>
#include <utility>

#include "content/browser/bluetooth/bluetooth_device_chooser_controller.h"
#include "content/public/browser/bluetooth_chooser.h"
#include "content/web_test/common/fake_bluetooth_chooser.mojom.h"

namespace content {

FakeBluetoothChooser::FakeBluetoothChooser(
    mojo::PendingReceiver<mojom::FakeBluetoothChooser> receiver,
    mojo::PendingAssociatedRemote<mojom::FakeBluetoothChooserClient> client)
    : receiver_(this, std::move(receiver)), client_(std::move(client)) {
  BluetoothDeviceChooserController::SetTestScanDurationForTesting(
      BluetoothDeviceChooserController::TestScanDurationSetting::NEVER_TIMEOUT);
}

FakeBluetoothChooser::~FakeBluetoothChooser() {
  BluetoothDeviceChooserController::SetTestScanDurationForTesting(
      BluetoothDeviceChooserController::TestScanDurationSetting::
          IMMEDIATE_TIMEOUT);

  client_->OnEvent(mojom::FakeBluetoothChooserEvent::New(
      mojom::ChooserEventType::CHOOSER_CLOSED, /*origin=*/std::nullopt,
      /*peripheral_address=*/std::nullopt));
}

void FakeBluetoothChooser::OnRunBluetoothChooser(
    const EventHandler& event_handler,
    const url::Origin& origin) {
  event_handler_ = event_handler;
  client_->OnEvent(mojom::FakeBluetoothChooserEvent::New(
      mojom::ChooserEventType::CHOOSER_OPENED, origin,
      /*peripheral_address=*/std::nullopt));
}

// mojom::FakeBluetoothChooser overrides
void FakeBluetoothChooser::SelectPeripheral(
    const std::string& peripheral_address) {
  DCHECK(event_handler_);
  event_handler_.Run(BluetoothChooserEvent::SELECTED, peripheral_address);
}

void FakeBluetoothChooser::Cancel() {
  DCHECK(event_handler_);
  event_handler_.Run(BluetoothChooserEvent::CANCELLED, std::string());
  client_->OnEvent(mojom::FakeBluetoothChooserEvent::New(
      mojom::ChooserEventType::CHOOSER_CLOSED, /*origin=*/std::nullopt,
      /*peripheral_address=*/std::nullopt));
}

void FakeBluetoothChooser::Rescan() {
  DCHECK(event_handler_);
  event_handler_.Run(BluetoothChooserEvent::RESCAN, std::string());
  client_->OnEvent(mojom::FakeBluetoothChooserEvent::New(
      mojom::ChooserEventType::DISCOVERING, /*origin=*/std::nullopt,
      /*peripheral_address=*/std::nullopt));
}

// BluetoothChooser overrides

void FakeBluetoothChooser::SetAdapterPresence(AdapterPresence presence) {
  mojom::FakeBluetoothChooserEventPtr event_ptr =
      mojom::FakeBluetoothChooserEvent::New();
  switch (presence) {
    case AdapterPresence::ABSENT:
      event_ptr->type = mojom::ChooserEventType::ADAPTER_REMOVED;
      break;
    case AdapterPresence::POWERED_OFF:
      event_ptr->type = mojom::ChooserEventType::ADAPTER_DISABLED;
      break;
    case AdapterPresence::POWERED_ON:
      event_ptr->type = mojom::ChooserEventType::ADAPTER_ENABLED;
      break;
    case AdapterPresence::UNAUTHORIZED:
      event_ptr->type = mojom::ChooserEventType::UNAUTHORIZED;
      break;
  }
  client_->OnEvent(std::move(event_ptr));
}

void FakeBluetoothChooser::ShowDiscoveryState(DiscoveryState state) {
  mojom::FakeBluetoothChooserEventPtr event_ptr =
      mojom::FakeBluetoothChooserEvent::New();
  switch (state) {
    case DiscoveryState::FAILED_TO_START:
      event_ptr->type = mojom::ChooserEventType::DISCOVERY_FAILED_TO_START;
      break;
    case DiscoveryState::DISCOVERING:
      event_ptr->type = mojom::ChooserEventType::DISCOVERING;
      break;
    case DiscoveryState::IDLE:
      event_ptr->type = mojom::ChooserEventType::DISCOVERY_IDLE;
      break;
  }
  client_->OnEvent(std::move(event_ptr));
}

void FakeBluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
                                             bool should_update_name,
                                             const std::u16string& device_name,
                                             bool is_gatt_connected,
                                             bool is_paired,
                                             int signal_strength_level) {
  client_->OnEvent(mojom::FakeBluetoothChooserEvent::New(
      mojom::ChooserEventType::ADD_OR_UPDATE_DEVICE,
      /*origin=*/std::nullopt, /*peripheral_address=*/device_id));
}

}  // namespace content