2020-09-28 16:22:03 +00:00
|
|
|
// Copyright 2019 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "shell/browser/serial/serial_chooser_context.h"
|
|
|
|
|
2021-06-01 01:34:44 +00:00
|
|
|
#include <string>
|
2020-09-28 16:22:03 +00:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "base/base64.h"
|
2021-06-03 08:05:04 +00:00
|
|
|
#include "base/containers/contains.h"
|
2020-09-28 16:22:03 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "base/values.h"
|
|
|
|
#include "content/public/browser/device_service.h"
|
2021-10-06 20:18:00 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2020-09-28 16:22:03 +00:00
|
|
|
#include "mojo/public/cpp/bindings/pending_remote.h"
|
2022-10-18 09:22:32 +00:00
|
|
|
#include "shell/browser/api/electron_api_session.h"
|
2022-06-27 20:50:08 +00:00
|
|
|
#include "shell/browser/electron_permission_manager.h"
|
2021-10-06 20:18:00 +00:00
|
|
|
#include "shell/browser/web_contents_permission_helper.h"
|
2022-10-18 09:22:32 +00:00
|
|
|
#include "shell/common/gin_converters/frame_converter.h"
|
|
|
|
#include "shell/common/gin_converters/serial_port_info_converter.h"
|
2020-09-28 16:22:03 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
constexpr char kPortNameKey[] = "name";
|
|
|
|
constexpr char kTokenKey[] = "token";
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2021-10-06 20:18:00 +00:00
|
|
|
const char kDeviceInstanceIdKey[] = "device_instance_id";
|
2020-10-16 01:30:41 +00:00
|
|
|
#else
|
2021-10-06 20:18:00 +00:00
|
|
|
const char kVendorIdKey[] = "vendor_id";
|
|
|
|
const char kProductIdKey[] = "product_id";
|
|
|
|
const char kSerialNumberKey[] = "serial_number";
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2021-10-06 20:18:00 +00:00
|
|
|
const char kUsbDriverKey[] = "usb_driver";
|
2022-02-10 02:58:52 +00:00
|
|
|
#endif // BUILDFLAG(IS_MAC)
|
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
2020-09-28 16:22:03 +00:00
|
|
|
|
|
|
|
std::string EncodeToken(const base::UnguessableToken& token) {
|
|
|
|
const uint64_t data[2] = {token.GetHighForSerialization(),
|
|
|
|
token.GetLowForSerialization()};
|
|
|
|
std::string buffer;
|
|
|
|
base::Base64Encode(
|
|
|
|
base::StringPiece(reinterpret_cast<const char*>(&data[0]), sizeof(data)),
|
|
|
|
&buffer);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::Value PortInfoToValue(const device::mojom::SerialPortInfo& port) {
|
2022-10-18 09:22:32 +00:00
|
|
|
base::Value::Dict value;
|
2020-09-28 16:22:03 +00:00
|
|
|
if (port.display_name && !port.display_name->empty())
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kPortNameKey, *port.display_name);
|
2020-09-28 16:22:03 +00:00
|
|
|
else
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kPortNameKey, port.path.LossyDisplayName());
|
2020-10-16 01:30:41 +00:00
|
|
|
|
|
|
|
if (!SerialChooserContext::CanStorePersistentEntry(port)) {
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kTokenKey, EncodeToken(port.token));
|
|
|
|
return base::Value(std::move(value));
|
2020-10-16 01:30:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2020-10-16 01:30:41 +00:00
|
|
|
// Windows provides a handy device identifier which we can rely on to be
|
|
|
|
// sufficiently stable for identifying devices across restarts.
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kDeviceInstanceIdKey, port.device_instance_id);
|
2020-10-16 01:30:41 +00:00
|
|
|
#else
|
|
|
|
DCHECK(port.has_vendor_id);
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kVendorIdKey, port.vendor_id);
|
2020-10-16 01:30:41 +00:00
|
|
|
DCHECK(port.has_product_id);
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kProductIdKey, port.product_id);
|
2020-10-16 01:30:41 +00:00
|
|
|
DCHECK(port.serial_number);
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kSerialNumberKey, *port.serial_number);
|
2020-10-16 01:30:41 +00:00
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-10-16 01:30:41 +00:00
|
|
|
DCHECK(port.usb_driver_name && !port.usb_driver_name->empty());
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kUsbDriverKey, *port.usb_driver_name);
|
2022-02-10 02:58:52 +00:00
|
|
|
#endif // BUILDFLAG(IS_MAC)
|
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
2022-10-18 09:22:32 +00:00
|
|
|
return base::Value(std::move(value));
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 20:50:08 +00:00
|
|
|
SerialChooserContext::SerialChooserContext(ElectronBrowserContext* context)
|
|
|
|
: browser_context_(context) {}
|
2021-07-26 17:10:57 +00:00
|
|
|
|
2022-07-25 14:50:19 +00:00
|
|
|
SerialChooserContext::~SerialChooserContext() {
|
|
|
|
// Notify observers that the chooser context is about to be destroyed.
|
|
|
|
// Observers must remove themselves from the observer lists.
|
|
|
|
for (auto& observer : port_observer_list_) {
|
|
|
|
observer.OnSerialChooserContextShutdown();
|
|
|
|
DCHECK(!port_observer_list_.HasObserver(&observer));
|
|
|
|
}
|
2022-05-17 16:48:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 16:22:03 +00:00
|
|
|
void SerialChooserContext::GrantPortPermission(
|
2021-07-26 17:10:57 +00:00
|
|
|
const url::Origin& origin,
|
2021-10-06 20:18:00 +00:00
|
|
|
const device::mojom::SerialPortInfo& port,
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2022-08-15 15:49:20 +00:00
|
|
|
port_info_.insert({port.token, port.Clone()});
|
|
|
|
|
2022-10-18 09:22:32 +00:00
|
|
|
if (CanStorePersistentEntry(port)) {
|
|
|
|
auto* permission_manager = static_cast<ElectronPermissionManager*>(
|
|
|
|
browser_context_->GetPermissionControllerDelegate());
|
|
|
|
permission_manager->GrantDevicePermission(
|
|
|
|
static_cast<blink::PermissionType>(
|
|
|
|
WebContentsPermissionHelper::PermissionType::SERIAL),
|
|
|
|
origin, PortInfoToValue(port), browser_context_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ephemeral_ports_[origin].insert(port.token);
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SerialChooserContext::HasPortPermission(
|
2021-07-26 17:10:57 +00:00
|
|
|
const url::Origin& origin,
|
2021-10-06 20:18:00 +00:00
|
|
|
const device::mojom::SerialPortInfo& port,
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2022-10-18 09:22:32 +00:00
|
|
|
auto it = ephemeral_ports_.find(origin);
|
|
|
|
if (it != ephemeral_ports_.end()) {
|
|
|
|
const std::set<base::UnguessableToken>& ports = it->second;
|
|
|
|
if (base::Contains(ports, port.token))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CanStorePersistentEntry(port))
|
|
|
|
return false;
|
|
|
|
|
2022-06-27 20:50:08 +00:00
|
|
|
auto* permission_manager = static_cast<ElectronPermissionManager*>(
|
|
|
|
browser_context_->GetPermissionControllerDelegate());
|
|
|
|
return permission_manager->CheckDevicePermission(
|
|
|
|
static_cast<blink::PermissionType>(
|
|
|
|
WebContentsPermissionHelper::PermissionType::SERIAL),
|
|
|
|
origin, PortInfoToValue(port), browser_context_);
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 16:48:40 +00:00
|
|
|
void SerialChooserContext::RevokePortPermissionWebInitiated(
|
|
|
|
const url::Origin& origin,
|
2022-10-18 09:22:32 +00:00
|
|
|
const base::UnguessableToken& token,
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2022-05-17 16:48:40 +00:00
|
|
|
auto it = port_info_.find(token);
|
2022-10-18 09:22:32 +00:00
|
|
|
if (it != port_info_.end()) {
|
|
|
|
auto* permission_manager = static_cast<ElectronPermissionManager*>(
|
|
|
|
browser_context_->GetPermissionControllerDelegate());
|
|
|
|
permission_manager->RevokeDevicePermission(
|
|
|
|
static_cast<blink::PermissionType>(
|
|
|
|
WebContentsPermissionHelper::PermissionType::SERIAL),
|
|
|
|
origin, PortInfoToValue(*it->second), browser_context_);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ephemeral = ephemeral_ports_.find(origin);
|
|
|
|
if (ephemeral != ephemeral_ports_.end()) {
|
|
|
|
std::set<base::UnguessableToken>& ports = ephemeral->second;
|
|
|
|
ports.erase(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* web_contents =
|
|
|
|
content::WebContents::FromRenderFrameHost(render_frame_host);
|
|
|
|
api::Session* session =
|
|
|
|
api::Session::FromBrowserContext(web_contents->GetBrowserContext());
|
|
|
|
|
|
|
|
if (session) {
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2023-10-10 10:45:44 +00:00
|
|
|
auto details = gin_helper::Dictionary::CreateEmpty(isolate);
|
2022-10-18 09:22:32 +00:00
|
|
|
details.Set("port", it->second);
|
|
|
|
details.SetGetter("frame", render_frame_host);
|
|
|
|
details.Set("origin", origin.Serialize());
|
|
|
|
session->Emit("serial-port-revoked", details);
|
|
|
|
}
|
2022-05-17 16:48:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 16:22:03 +00:00
|
|
|
// static
|
|
|
|
bool SerialChooserContext::CanStorePersistentEntry(
|
|
|
|
const device::mojom::SerialPortInfo& port) {
|
|
|
|
// If there is no display name then the path name will be used instead. The
|
|
|
|
// path name is not guaranteed to be stable. For example, on Linux the name
|
|
|
|
// "ttyUSB0" is reused for any USB serial device. A name like that would be
|
|
|
|
// confusing to show in settings when the device is disconnected.
|
|
|
|
if (!port.display_name || port.display_name->empty())
|
|
|
|
return false;
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2020-10-16 01:30:41 +00:00
|
|
|
return !port.device_instance_id.empty();
|
|
|
|
#else
|
|
|
|
if (!port.has_vendor_id || !port.has_product_id || !port.serial_number ||
|
|
|
|
port.serial_number->empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-10-16 01:30:41 +00:00
|
|
|
// The combination of the standard USB vendor ID, product ID and serial
|
|
|
|
// number properties should be enough to uniquely identify a device
|
|
|
|
// however recent versions of macOS include built-in drivers for common
|
|
|
|
// types of USB-to-serial adapters while their manufacturers still
|
|
|
|
// recommend installing their custom drivers. When both are loaded two
|
|
|
|
// IOSerialBSDClient instances are found for each device. Including the
|
|
|
|
// USB driver name allows us to distinguish between the two.
|
|
|
|
if (!port.usb_driver_name || port.usb_driver_name->empty())
|
|
|
|
return false;
|
2022-02-10 02:58:52 +00:00
|
|
|
#endif // BUILDFLAG(IS_MAC)
|
2020-10-16 01:30:41 +00:00
|
|
|
|
|
|
|
return true;
|
2022-02-10 02:58:52 +00:00
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 16:48:40 +00:00
|
|
|
const device::mojom::SerialPortInfo* SerialChooserContext::GetPortInfo(
|
|
|
|
const base::UnguessableToken& token) {
|
|
|
|
DCHECK(is_initialized_);
|
|
|
|
auto it = port_info_.find(token);
|
|
|
|
return it == port_info_.end() ? nullptr : it->second.get();
|
|
|
|
}
|
|
|
|
|
2020-09-28 16:22:03 +00:00
|
|
|
device::mojom::SerialPortManager* SerialChooserContext::GetPortManager() {
|
|
|
|
EnsurePortManagerConnection();
|
|
|
|
return port_manager_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::AddPortObserver(PortObserver* observer) {
|
|
|
|
port_observer_list_.AddObserver(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::RemovePortObserver(PortObserver* observer) {
|
|
|
|
port_observer_list_.RemoveObserver(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
base::WeakPtr<SerialChooserContext> SerialChooserContext::AsWeakPtr() {
|
|
|
|
return weak_factory_.GetWeakPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::OnPortAdded(device::mojom::SerialPortInfoPtr port) {
|
2022-08-15 15:49:20 +00:00
|
|
|
if (!base::Contains(port_info_, port->token))
|
|
|
|
port_info_.insert({port->token, port->Clone()});
|
|
|
|
|
2022-10-18 09:22:32 +00:00
|
|
|
for (auto& map_entry : ephemeral_ports_) {
|
|
|
|
std::set<base::UnguessableToken>& ports = map_entry.second;
|
|
|
|
ports.erase(port->token);
|
|
|
|
}
|
|
|
|
|
2020-09-28 16:22:03 +00:00
|
|
|
for (auto& observer : port_observer_list_)
|
|
|
|
observer.OnPortAdded(*port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::OnPortRemoved(
|
|
|
|
device::mojom::SerialPortInfoPtr port) {
|
|
|
|
for (auto& observer : port_observer_list_)
|
|
|
|
observer.OnPortRemoved(*port);
|
2022-08-15 15:49:20 +00:00
|
|
|
|
|
|
|
port_info_.erase(port->token);
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::EnsurePortManagerConnection() {
|
|
|
|
if (port_manager_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mojo::PendingRemote<device::mojom::SerialPortManager> manager;
|
|
|
|
content::GetDeviceService().BindSerialPortManager(
|
|
|
|
manager.InitWithNewPipeAndPassReceiver());
|
|
|
|
SetUpPortManagerConnection(std::move(manager));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::SetUpPortManagerConnection(
|
|
|
|
mojo::PendingRemote<device::mojom::SerialPortManager> manager) {
|
|
|
|
port_manager_.Bind(std::move(manager));
|
|
|
|
port_manager_.set_disconnect_handler(
|
|
|
|
base::BindOnce(&SerialChooserContext::OnPortManagerConnectionError,
|
|
|
|
base::Unretained(this)));
|
|
|
|
|
|
|
|
port_manager_->SetClient(client_receiver_.BindNewPipeAndPassRemote());
|
2022-08-15 15:49:20 +00:00
|
|
|
port_manager_->GetDevices(base::BindOnce(&SerialChooserContext::OnGetDevices,
|
|
|
|
weak_factory_.GetWeakPtr()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::OnGetDevices(
|
|
|
|
std::vector<device::mojom::SerialPortInfoPtr> ports) {
|
|
|
|
for (auto& port : ports)
|
|
|
|
port_info_.insert({port->token, std::move(port)});
|
|
|
|
is_initialized_ = true;
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::OnPortManagerConnectionError() {
|
|
|
|
port_manager_.reset();
|
|
|
|
client_receiver_.reset();
|
|
|
|
|
2022-10-18 09:22:32 +00:00
|
|
|
port_info_.clear();
|
|
|
|
ephemeral_ports_.clear();
|
|
|
|
}
|
2020-09-28 16:22:03 +00:00
|
|
|
} // namespace electron
|