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
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294

content / browser / serial / serial_service.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/browser/serial/serial_service.h"

#include <map>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/renderer_host/back_forward_cache_disable.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/serial_chooser.h"
#include "content/public/browser/serial_delegate.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_client.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/device/public/mojom/serial.mojom.h"
#include "third_party/blink/public/mojom/permissions_policy/permissions_policy.mojom.h"

namespace content {

SerialService::SerialService(RenderFrameHost* rfh)
    : DocumentUserData<SerialService>(rfh) {
  DCHECK(render_frame_host().IsFeatureEnabled(
      blink::mojom::PermissionsPolicyFeature::kSerial));
  // Serial API is not supported for back-forward cache for now because we
  // don't have support for closing/freezing ports when the frame is added to
  // the back-forward cache, so we mark frames that use this API as disabled
  // for back-forward cache.
  BackForwardCache::DisableForRenderFrameHost(
      &render_frame_host(),
      BackForwardCacheDisable::DisabledReason(
          BackForwardCacheDisable::DisabledReasonId::kSerial));

  watchers_.set_disconnect_handler(base::BindRepeating(
      &SerialService::OnWatcherConnectionError, base::Unretained(this)));

  SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
  if (delegate)
    delegate->AddObserver(&render_frame_host(), this);
}

SerialService::~SerialService() {
  SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
  if (delegate)
    delegate->RemoveObserver(&render_frame_host(), this);

  // The remaining watchers will be closed from this end.
  if (!watchers_.empty())
    DecrementActiveFrameCount();
}

void SerialService::Bind(
    mojo::PendingReceiver<blink::mojom::SerialService> receiver) {
  receivers_.Add(this, std::move(receiver));
}

void SerialService::SetClient(
    mojo::PendingRemote<blink::mojom::SerialServiceClient> client) {
  clients_.Add(std::move(client));
}

void SerialService::GetPorts(GetPortsCallback callback) {
  SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
  if (!delegate) {
    std::move(callback).Run(std::vector<blink::mojom::SerialPortInfoPtr>());
    return;
  }

  delegate->GetPortManager(&render_frame_host())
      ->GetDevices(base::BindOnce(&SerialService::FinishGetPorts,
                                  weak_factory_.GetWeakPtr(),
                                  std::move(callback)));
}

void SerialService::RequestPort(
    std::vector<blink::mojom::SerialPortFilterPtr> filters,
    const std::vector<::device::BluetoothUUID>&
        allowed_bluetooth_service_class_ids,
    RequestPortCallback callback) {
  SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
  if (!delegate) {
    std::move(callback).Run(nullptr);
    return;
  }

  if (!delegate->CanRequestPortPermission(&render_frame_host())) {
    std::move(callback).Run(nullptr);
    return;
  }

  chooser_ = delegate->RunChooser(
      &render_frame_host(), std::move(filters),
      allowed_bluetooth_service_class_ids,
      base::BindOnce(&SerialService::FinishRequestPort,
                     weak_factory_.GetWeakPtr(), std::move(callback)));
}

void SerialService::OpenPort(
    const base::UnguessableToken& token,
    device::mojom::SerialConnectionOptionsPtr options,
    mojo::PendingRemote<device::mojom::SerialPortClient> client,
    OpenPortCallback callback) {
  SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
  if (!delegate) {
    std::move(callback).Run(mojo::NullRemote());
    return;
  }

  const auto* port = delegate->GetPortInfo(&render_frame_host(), token);
  if (!port || !delegate->HasPortPermission(&render_frame_host(), *port)) {
    std::move(callback).Run(mojo::NullRemote());
    return;
  }

  if (watchers_.empty()) {
    auto* web_contents_impl = static_cast<WebContentsImpl*>(
        WebContents::FromRenderFrameHost(&render_frame_host()));
    web_contents_impl->IncrementSerialActiveFrameCount();
  }

  mojo::PendingRemote<device::mojom::SerialPortConnectionWatcher> watcher;
  mojo::ReceiverId receiver_id =
      watchers_.Add(this, watcher.InitWithNewPipeAndPassReceiver());
  watcher_ids_.insert({token, receiver_id});

  delegate->GetPortManager(&render_frame_host())
      ->OpenPort(token, /*use_alternate_path=*/false, std::move(options),
                 std::move(client), std::move(watcher), std::move(callback));
}

void SerialService::ForgetPort(const base::UnguessableToken& token,
                               ForgetPortCallback callback) {
  SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
  if (delegate) {
    delegate->RevokePortPermissionWebInitiated(&render_frame_host(), token);
  }
  std::move(callback).Run();
}

void SerialService::OnPortAdded(const device::mojom::SerialPortInfo& port) {
  // Notify clients that a connect event should be dispatched for an added port.
  //
  // In some cases `port` may be disconnected. Wired serial ports are always
  // connected when first added. Bluetooth serial ports are added in the
  // disconnected state if the underlying Bluetooth device is paired with the
  // system but the system has no open connections to the device.
  //
  // Do not notify clients if `port` is disconnected since it would cause a
  // disconnect event to be dispatched for a port that did not previously
  // receive a 'connect' event and hasn't been returned by getPorts().
  if (port.connected) {
    OnPortConnectedStateChanged(port);
  }
}

void SerialService::OnPortRemoved(const device::mojom::SerialPortInfo& port) {
  OnPortConnectedStateChanged(port);
}

void SerialService::OnPortConnectedStateChanged(
    const device::mojom::SerialPortInfo& port) {
  SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
  if (!delegate || !delegate->HasPortPermission(&render_frame_host(), port)) {
    return;
  }

  for (const auto& client : clients_) {
    client->OnPortConnectedStateChanged(ToBlinkType(port));
  }
}

void SerialService::OnPortManagerConnectionError() {
  // Reflect the loss of the SerialPortManager connection into the renderer
  // in order to force caches to be cleared and connections to be
  // re-established.
  receivers_.Clear();
  clients_.Clear();
  chooser_.reset();

  if (!watchers_.empty()) {
    watchers_.Clear();
    DecrementActiveFrameCount();
  }
}

void SerialService::OnPermissionRevoked(const url::Origin& origin) {
  if (origin != render_frame_host().GetMainFrame()->GetLastCommittedOrigin())
    return;

  SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
  size_t watchers_removed =
      std::erase_if(watcher_ids_, [&](const auto& watcher_entry) {
        const auto* port =
            delegate->GetPortInfo(&render_frame_host(), watcher_entry.first);
        if (port && delegate->HasPortPermission(&render_frame_host(), *port))
          return false;

        watchers_.Remove(watcher_entry.second);
        return true;
      });

  if (watchers_removed > 0 && watchers_.empty())
    DecrementActiveFrameCount();
}

void SerialService::FinishGetPorts(
    GetPortsCallback callback,
    std::vector<device::mojom::SerialPortInfoPtr> ports) {
  std::vector<blink::mojom::SerialPortInfoPtr> result;
  SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
  if (!delegate) {
    std::move(callback).Run(std::move(result));
    return;
  }

  for (const auto& port : ports) {
    if (delegate->HasPortPermission(&render_frame_host(), *port))
      result.push_back(ToBlinkType(*port));
  }

  std::move(callback).Run(std::move(result));
}

void SerialService::FinishRequestPort(RequestPortCallback callback,
                                      device::mojom::SerialPortInfoPtr port) {
  SerialDelegate* delegate = GetContentClient()->browser()->GetSerialDelegate();
  if (!delegate || !port) {
    std::move(callback).Run(nullptr);
    return;
  }

  std::move(callback).Run(ToBlinkType(*port));
}

void SerialService::OnWatcherConnectionError() {
  if (watchers_.empty())
    DecrementActiveFrameCount();

  // Clean up any associated |watcher_ids_| entries.
  std::erase_if(watcher_ids_, [&](const auto& watcher_entry) {
    return watcher_entry.second == watchers_.current_receiver();
  });
}

void SerialService::DecrementActiveFrameCount() {
  auto* web_contents_impl = static_cast<WebContentsImpl*>(
      WebContents::FromRenderFrameHost(&render_frame_host()));
  web_contents_impl->DecrementSerialActiveFrameCount();
}

blink::mojom::SerialPortInfoPtr SerialService::ToBlinkType(
    const device::mojom::SerialPortInfo& port) {
  auto info = blink::mojom::SerialPortInfo::New();
  std::optional<std::string> persistent_identifier;

  info->has_usb_vendor_id = port.has_vendor_id;
  if (port.has_vendor_id) {
    info->usb_vendor_id = port.vendor_id;
  }
  info->has_usb_product_id = port.has_product_id;
  if (port.has_product_id) {
    info->usb_product_id = port.product_id;
  }
  if (port.bluetooth_service_class_id) {
    info->bluetooth_service_class_id = port.bluetooth_service_class_id;
    // Mac address + service uuid can persistently identify a serial port.
    persistent_identifier = base::UTF16ToUTF8(port.path.LossyDisplayName()) +
                            info->bluetooth_service_class_id->value();
  }
  info->connected = port.connected;
  if (persistent_identifier) {
    auto it = token_map_.find(*persistent_identifier);
    if (it == token_map_.end()) {
      auto result = token_map_.insert({*persistent_identifier, port.token});
      CHECK(result.second);
      it = result.first;
    }
    info->token = it->second;
  } else {
    info->token = port.token;
  }
  return info;
}

DOCUMENT_USER_DATA_KEY_IMPL(SerialService);

}  // namespace content