feat: support Web Serial & WebUSB blocklists (#46600)
This commit is contained in:
parent
352a403efd
commit
a29e1170b9
10 changed files with 106 additions and 4 deletions
|
@ -9,7 +9,9 @@
|
|||
#include <utility>
|
||||
|
||||
#include "base/base64.h"
|
||||
#include "base/command_line.h"
|
||||
#include "base/values.h"
|
||||
#include "chrome/browser/serial/serial_blocklist.h"
|
||||
#include "content/public/browser/device_service.h"
|
||||
#include "content/public/browser/web_contents.h"
|
||||
#include "mojo/public/cpp/bindings/pending_remote.h"
|
||||
|
@ -104,6 +106,12 @@ bool SerialChooserContext::HasPortPermission(
|
|||
const url::Origin& origin,
|
||||
const device::mojom::SerialPortInfo& port,
|
||||
content::RenderFrameHost* render_frame_host) {
|
||||
bool blocklist_disabled = base::CommandLine::ForCurrentProcess()->HasSwitch(
|
||||
kDisableSerialBlocklist);
|
||||
if (!blocklist_disabled && SerialBlocklist::Get().IsExcluded(port)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto it = ephemeral_ports_.find(origin);
|
||||
if (it != ephemeral_ports_.end()) {
|
||||
const std::set<base::UnguessableToken>& ports = it->second;
|
||||
|
|
|
@ -36,6 +36,8 @@ namespace electron {
|
|||
|
||||
class ElectronBrowserContext;
|
||||
|
||||
const char kDisableSerialBlocklist[] = "disable-serial-blocklist";
|
||||
|
||||
inline constexpr std::string_view kPortNameKey = "name";
|
||||
inline constexpr std::string_view kTokenKey = "token";
|
||||
inline constexpr std::string_view kBluetoothDevicePathKey =
|
||||
|
|
|
@ -7,9 +7,12 @@
|
|||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
#include "base/command_line.h"
|
||||
#include "base/containers/contains.h"
|
||||
#include "base/functional/bind.h"
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
#include "chrome/browser/serial/serial_blocklist.h"
|
||||
#include "content/public/browser/console_message.h"
|
||||
#include "content/public/browser/web_contents.h"
|
||||
#include "device/bluetooth/bluetooth_adapter.h"
|
||||
#include "device/bluetooth/bluetooth_adapter_factory.h"
|
||||
|
@ -120,8 +123,7 @@ SerialChooserController::SerialChooserController(
|
|||
allowed_bluetooth_service_class_ids_(
|
||||
std::move(allowed_bluetooth_service_class_ids)),
|
||||
callback_(std::move(callback)),
|
||||
serial_delegate_(serial_delegate),
|
||||
render_frame_host_id_(render_frame_host->GetGlobalId()) {
|
||||
initiator_document_(render_frame_host->GetWeakDocumentPtr()) {
|
||||
origin_ = web_contents_->GetPrimaryMainFrame()->GetLastCommittedOrigin();
|
||||
|
||||
chooser_context_ = SerialChooserContextFactory::GetForBrowserContext(
|
||||
|
@ -210,7 +212,7 @@ void SerialChooserController::OnDeviceChosen(const std::string& port_id) {
|
|||
return ptr->token.ToString() == port_id;
|
||||
});
|
||||
if (it != ports_.end()) {
|
||||
auto* rfh = content::RenderFrameHost::FromID(render_frame_host_id_);
|
||||
auto* rfh = initiator_document_.AsRenderFrameHostIfValid();
|
||||
chooser_context_->GrantPortPermission(origin_, *it->get(), rfh);
|
||||
RunCallback(it->Clone());
|
||||
} else {
|
||||
|
@ -246,6 +248,34 @@ void SerialChooserController::OnGetDevices(
|
|||
|
||||
bool SerialChooserController::DisplayDevice(
|
||||
const device::mojom::SerialPortInfo& port) const {
|
||||
bool blocklist_disabled = base::CommandLine::ForCurrentProcess()->HasSwitch(
|
||||
kDisableSerialBlocklist);
|
||||
if (!blocklist_disabled && SerialBlocklist::Get().IsExcluded(port)) {
|
||||
if (port.has_vendor_id && port.has_product_id) {
|
||||
AddMessageToConsole(
|
||||
blink::mojom::ConsoleMessageLevel::kInfo,
|
||||
base::StringPrintf(
|
||||
"Skipping a port blocked by "
|
||||
"the Serial blocklist: vendorId=%d, "
|
||||
"productId=%d, name='%s', serial='%s'",
|
||||
port.vendor_id, port.product_id,
|
||||
port.display_name ? port.display_name.value().c_str() : "",
|
||||
port.serial_number ? port.serial_number.value().c_str() : ""));
|
||||
} else if (port.bluetooth_service_class_id) {
|
||||
AddMessageToConsole(
|
||||
blink::mojom::ConsoleMessageLevel::kInfo,
|
||||
base::StringPrintf(
|
||||
"Skipping a port blocked by "
|
||||
"the Serial blocklist: bluetoothServiceClassId=%s, "
|
||||
"name='%s'",
|
||||
port.bluetooth_service_class_id->value().c_str(),
|
||||
port.display_name ? port.display_name.value().c_str() : ""));
|
||||
} else {
|
||||
NOTREACHED();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (filters_.empty()) {
|
||||
return BluetoothPortIsAllowed(allowed_bluetooth_service_class_ids_, port);
|
||||
}
|
||||
|
@ -260,6 +290,15 @@ bool SerialChooserController::DisplayDevice(
|
|||
return false;
|
||||
}
|
||||
|
||||
void SerialChooserController::AddMessageToConsole(
|
||||
blink::mojom::ConsoleMessageLevel level,
|
||||
const std::string& message) const {
|
||||
if (content::RenderFrameHost* rfh =
|
||||
initiator_document_.AsRenderFrameHostIfValid()) {
|
||||
rfh->AddMessageToConsole(level, message);
|
||||
}
|
||||
}
|
||||
|
||||
void SerialChooserController::RunCallback(
|
||||
device::mojom::SerialPortInfoPtr port) {
|
||||
if (callback_) {
|
||||
|
|
|
@ -12,9 +12,11 @@
|
|||
#include "base/scoped_observation.h"
|
||||
#include "content/public/browser/global_routing_id.h"
|
||||
#include "content/public/browser/serial_chooser.h"
|
||||
#include "content/public/browser/weak_document_ptr.h"
|
||||
#include "device/bluetooth/bluetooth_adapter.h"
|
||||
#include "services/device/public/mojom/serial.mojom-forward.h"
|
||||
#include "shell/browser/serial/serial_chooser_context.h"
|
||||
#include "third_party/blink/public/mojom/devtools/console_message.mojom.h"
|
||||
#include "third_party/blink/public/mojom/serial/serial.mojom-forward.h"
|
||||
|
||||
namespace content {
|
||||
|
@ -66,6 +68,8 @@ class SerialChooserController final
|
|||
void GetDevices();
|
||||
void OnGetDevices(std::vector<device::mojom::SerialPortInfoPtr> ports);
|
||||
bool DisplayDevice(const device::mojom::SerialPortInfo& port) const;
|
||||
void AddMessageToConsole(blink::mojom::ConsoleMessageLevel level,
|
||||
const std::string& message) const;
|
||||
void RunCallback(device::mojom::SerialPortInfoPtr port);
|
||||
void OnDeviceChosen(const std::string& port_id);
|
||||
void OnGetAdapter(base::OnceClosure callback,
|
||||
|
@ -78,6 +82,7 @@ class SerialChooserController final
|
|||
std::vector<blink::mojom::SerialPortFilterPtr> filters_;
|
||||
std::vector<::device::BluetoothUUID> allowed_bluetooth_service_class_ids_;
|
||||
content::SerialChooser::Callback callback_;
|
||||
content::WeakDocumentPtr initiator_document_;
|
||||
url::Origin origin_;
|
||||
|
||||
base::WeakPtr<SerialChooserContext> chooser_context_;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue