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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/webusb/usb.h"
#include <utility>
#include "base/notreached.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/device/public/mojom/usb_device.mojom-blink.h"
#include "services/device/public/mojom/usb_enumeration_options.mojom-blink.h"
#include "services/network/public/mojom/permissions_policy/permissions_policy_feature.mojom-blink.h"
#include "third_party/blink/public/mojom/service_worker/service_worker.mojom-blink.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_usb_device_filter.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_usb_device_request_options.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/navigator_base.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/modules/event_target_modules.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope.h"
#include "third_party/blink/renderer/modules/webusb/usb_connection_event.h"
#include "third_party/blink/renderer/modules/webusb/usb_device.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
using device::mojom::blink::UsbDevice;
using device::mojom::blink::UsbDeviceFilterPtr;
using device::mojom::blink::UsbDeviceInfoPtr;
namespace blink {
namespace {
const char kFeaturePolicyBlocked[] =
"Access to the feature \"usb\" is disallowed by permissions policy.";
const char kNoDeviceSelected[] = "No device selected.";
void RejectWithTypeError(const String& error_details,
ScriptPromiseResolverBase* resolver) {
ScriptState::Scope scope(resolver->GetScriptState());
v8::Isolate* isolate = resolver->GetScriptState()->GetIsolate();
resolver->Reject(V8ThrowException::CreateTypeError(isolate, error_details));
}
UsbDeviceFilterPtr ConvertDeviceFilter(const USBDeviceFilter* filter,
ScriptPromiseResolverBase* resolver) {
auto mojo_filter = device::mojom::blink::UsbDeviceFilter::New();
mojo_filter->has_vendor_id = filter->hasVendorId();
if (mojo_filter->has_vendor_id)
mojo_filter->vendor_id = filter->vendorId();
mojo_filter->has_product_id = filter->hasProductId();
if (mojo_filter->has_product_id) {
if (!mojo_filter->has_vendor_id) {
RejectWithTypeError(
"A filter containing a productId must also contain a vendorId.",
resolver);
return nullptr;
}
mojo_filter->product_id = filter->productId();
}
mojo_filter->has_class_code = filter->hasClassCode();
if (mojo_filter->has_class_code)
mojo_filter->class_code = filter->classCode();
mojo_filter->has_subclass_code = filter->hasSubclassCode();
if (mojo_filter->has_subclass_code) {
if (!mojo_filter->has_class_code) {
RejectWithTypeError(
"A filter containing a subclassCode must also contain a classCode.",
resolver);
return nullptr;
}
mojo_filter->subclass_code = filter->subclassCode();
}
mojo_filter->has_protocol_code = filter->hasProtocolCode();
if (mojo_filter->has_protocol_code) {
if (!mojo_filter->has_subclass_code) {
RejectWithTypeError(
"A filter containing a protocolCode must also contain a "
"subclassCode.",
resolver);
return nullptr;
}
mojo_filter->protocol_code = filter->protocolCode();
}
if (filter->hasSerialNumber())
mojo_filter->serial_number = filter->serialNumber();
return mojo_filter;
}
bool IsContextSupported(ExecutionContext* context) {
// Since WebUSB on Web Workers is in the process of being implemented, we
// check here if the runtime flag for the appropriate worker is enabled.
// TODO(https://crbug.com/837406): Remove this check once the feature has
// shipped.
if (!context) {
return false;
}
DCHECK(context->IsWindow() || context->IsDedicatedWorkerGlobalScope() ||
context->IsServiceWorkerGlobalScope());
DCHECK(!context->IsDedicatedWorkerGlobalScope() ||
RuntimeEnabledFeatures::WebUSBOnDedicatedWorkersEnabled());
DCHECK(!context->IsServiceWorkerGlobalScope() ||
RuntimeEnabledFeatures::WebUSBOnServiceWorkersEnabled());
return true;
}
// Carries out basic checks for the web-exposed APIs, to make sure the minimum
// requirements for them to be served are met. Returns true if any conditions
// fail to be met, generating an appropriate exception as well. Otherwise,
// returns false to indicate the call should be allowed.
bool ShouldBlockUsbServiceCall(LocalDOMWindow* window,
ExecutionContext* context,
ExceptionState* exception_state) {
if (!IsContextSupported(context)) {
if (exception_state) {
exception_state->ThrowDOMException(
DOMExceptionCode::kNotSupportedError,
"The implementation did not support the requested type of object or "
"operation.");
}
return true;
}
// For window and dedicated workers, reject the request if the top-level frame
// has an opaque origin. For Service Workers, we use their security origin
// directly as they do not use delegated permissions.
const SecurityOrigin* security_origin = nullptr;
if (context->IsWindow()) {
security_origin =
window->GetFrame()->Top()->GetSecurityContext()->GetSecurityOrigin();
} else if (context->IsDedicatedWorkerGlobalScope()) {
security_origin = static_cast<WorkerGlobalScope*>(context)
->top_level_frame_security_origin();
} else if (context->IsServiceWorkerGlobalScope()) {
security_origin = context->GetSecurityOrigin();
} else {
NOTREACHED();
}
if (security_origin->IsOpaque()) {
if (exception_state) {
exception_state->ThrowSecurityError(
"Access to the WebUSB API is denied from contexts where the "
"top-level document has an opaque origin.");
}
return true;
}
if (!context->IsFeatureEnabled(network::mojom::PermissionsPolicyFeature::kUsb,
ReportOptions::kReportOnFailure)) {
if (exception_state) {
exception_state->ThrowSecurityError(kFeaturePolicyBlocked);
}
return true;
}
return false;
}
} // namespace
const char USB::kSupplementName[] = "USB";
USB* USB::usb(NavigatorBase& navigator) {
USB* usb = Supplement<NavigatorBase>::From<USB>(navigator);
if (!usb) {
usb = MakeGarbageCollected<USB>(navigator);
ProvideTo(navigator, usb);
}
return usb;
}
USB::USB(NavigatorBase& navigator)
: Supplement<NavigatorBase>(navigator),
ExecutionContextLifecycleObserver(navigator.GetExecutionContext()),
service_(navigator.GetExecutionContext()),
client_receiver_(this, navigator.GetExecutionContext()) {}
USB::~USB() {
// |service_| may still be valid but there should be no more outstanding
// requests to them because each holds a persistent handle to this object.
DCHECK(get_devices_requests_.empty());
DCHECK(get_permission_requests_.empty());
}
ScriptPromise<IDLSequence<USBDevice>> USB::getDevices(
ScriptState* script_state,
ExceptionState& exception_state) {
if (ShouldBlockUsbServiceCall(GetSupplementable()->DomWindow(),
GetExecutionContext(), &exception_state)) {
return ScriptPromise<IDLSequence<USBDevice>>();
}
EnsureServiceConnection();
auto* resolver =
MakeGarbageCollected<ScriptPromiseResolver<IDLSequence<USBDevice>>>(
script_state, exception_state.GetContext());
get_devices_requests_.insert(resolver);
service_->GetDevices(WTF::BindOnce(&USB::OnGetDevices, WrapPersistent(this),
WrapPersistent(resolver)));
return resolver->Promise();
}
ScriptPromise<USBDevice> USB::requestDevice(
ScriptState* script_state,
const USBDeviceRequestOptions* options,
ExceptionState& exception_state) {
if (!DomWindow()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kNotSupportedError,
"The implementation did not support the requested type of object or "
"operation.");
return EmptyPromise();
}
if (ShouldBlockUsbServiceCall(GetSupplementable()->DomWindow(),
GetExecutionContext(), &exception_state)) {
return EmptyPromise();
}
EnsureServiceConnection();
if (!LocalFrame::HasTransientUserActivation(DomWindow()->GetFrame())) {
exception_state.ThrowSecurityError(
"Must be handling a user gesture to show a permission request.");
return EmptyPromise();
}
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver<USBDevice>>(
script_state, exception_state.GetContext());
auto promise = resolver->Promise();
auto mojo_options = mojom::blink::WebUsbRequestDeviceOptions::New();
if (options->hasFilters()) {
mojo_options->filters.reserve(options->filters().size());
for (const auto& filter : options->filters()) {
UsbDeviceFilterPtr converted_filter =
ConvertDeviceFilter(filter, resolver);
if (!converted_filter)
return promise;
mojo_options->filters.push_back(std::move(converted_filter));
}
}
mojo_options->exclusion_filters.reserve(options->exclusionFilters().size());
for (const auto& filter : options->exclusionFilters()) {
UsbDeviceFilterPtr converted_filter = ConvertDeviceFilter(filter, resolver);
if (!converted_filter) {
return promise;
}
mojo_options->exclusion_filters.push_back(std::move(converted_filter));
}
DCHECK(options->filters().size() == mojo_options->filters.size());
DCHECK(options->exclusionFilters().size() ==
mojo_options->exclusion_filters.size());
get_permission_requests_.insert(resolver);
service_->GetPermission(std::move(mojo_options),
resolver->WrapCallbackInScriptScope(WTF::BindOnce(
&USB::OnGetPermission, WrapPersistent(this))));
return promise;
}
ExecutionContext* USB::GetExecutionContext() const {
return ExecutionContextLifecycleObserver::GetExecutionContext();
}
const AtomicString& USB::InterfaceName() const {
return event_target_names::kUSB;
}
void USB::ContextDestroyed() {
get_devices_requests_.clear();
get_permission_requests_.clear();
}
USBDevice* USB::GetOrCreateDevice(UsbDeviceInfoPtr device_info) {
auto it = device_cache_.find(device_info->guid);
if (it != device_cache_.end()) {
return it->value.Get();
}
String guid = device_info->guid;
mojo::PendingRemote<UsbDevice> pipe;
service_->GetDevice(guid, pipe.InitWithNewPipeAndPassReceiver());
USBDevice* device = MakeGarbageCollected<USBDevice>(
this, std::move(device_info), std::move(pipe), GetExecutionContext());
device_cache_.insert(guid, device);
return device;
}
void USB::ForgetDevice(
const String& device_guid,
mojom::blink::WebUsbService::ForgetDeviceCallback callback) {
EnsureServiceConnection();
service_->ForgetDevice(device_guid, std::move(callback));
}
void USB::OnGetDevices(ScriptPromiseResolver<IDLSequence<USBDevice>>* resolver,
Vector<UsbDeviceInfoPtr> device_infos) {
DCHECK(get_devices_requests_.Contains(resolver));
HeapVector<Member<USBDevice>> devices;
for (auto& device_info : device_infos)
devices.push_back(GetOrCreateDevice(std::move(device_info)));
resolver->Resolve(devices);
get_devices_requests_.erase(resolver);
}
void USB::OnGetPermission(ScriptPromiseResolver<USBDevice>* resolver,
UsbDeviceInfoPtr device_info) {
DCHECK(get_permission_requests_.Contains(resolver));
EnsureServiceConnection();
if (service_.is_bound() && device_info) {
resolver->Resolve(GetOrCreateDevice(std::move(device_info)));
} else {
resolver->RejectWithDOMException(DOMExceptionCode::kNotFoundError,
kNoDeviceSelected);
}
get_permission_requests_.erase(resolver);
}
void USB::OnDeviceAdded(UsbDeviceInfoPtr device_info) {
if (!service_.is_bound())
return;
DispatchEvent(*USBConnectionEvent::Create(
event_type_names::kConnect, GetOrCreateDevice(std::move(device_info))));
}
void USB::OnDeviceRemoved(UsbDeviceInfoPtr device_info) {
String guid = device_info->guid;
USBDevice* device = nullptr;
const auto it = device_cache_.find(guid);
if (it != device_cache_.end()) {
device = it->value;
} else {
device = MakeGarbageCollected<USBDevice>(this, std::move(device_info),
mojo::NullRemote(),
GetExecutionContext());
}
DispatchEvent(
*USBConnectionEvent::Create(event_type_names::kDisconnect, device));
device_cache_.erase(guid);
}
void USB::OnServiceConnectionError() {
service_.reset();
client_receiver_.reset();
// This loop is resolving promises with a value and so it is possible for
// script to be executed in the process of determining if the value is a
// thenable. Move the set to a local variable to prevent such execution from
// invalidating the iterator used by the loop.
HeapHashSet<Member<ScriptPromiseResolver<IDLSequence<USBDevice>>>>
get_devices_requests;
get_devices_requests.swap(get_devices_requests_);
for (auto& resolver : get_devices_requests)
resolver->Resolve(HeapVector<Member<USBDevice>>(0));
// Similar protection is unnecessary when rejecting a promise.
for (auto& resolver : get_permission_requests_) {
ScriptState* resolver_script_state = resolver->GetScriptState();
if (!IsInParallelAlgorithmRunnable(resolver->GetExecutionContext(),
resolver_script_state)) {
continue;
}
ScriptState::Scope script_state_scope(resolver_script_state);
resolver->RejectWithDOMException(DOMExceptionCode::kNotFoundError,
kNoDeviceSelected);
}
}
void USB::AddedEventListener(const AtomicString& event_type,
RegisteredEventListener& listener) {
EventTarget::AddedEventListener(event_type, listener);
if (event_type != event_type_names::kConnect &&
event_type != event_type_names::kDisconnect) {
return;
}
auto* context = GetExecutionContext();
if (ShouldBlockUsbServiceCall(GetSupplementable()->DomWindow(), context,
nullptr)) {
return;
}
if (context->IsServiceWorkerGlobalScope()) {
auto* service_worker_global_scope =
static_cast<ServiceWorkerGlobalScope*>(context);
if (service_worker_global_scope->did_evaluate_script()) {
String message = String::Format(
"Event handler of '%s' event must be added on the initial evaluation "
"of worker script. More info: "
"https://developer.chrome.com/docs/extensions/mv3/service_workers/"
"events/",
event_type.Utf8().c_str());
GetExecutionContext()->AddConsoleMessage(
mojom::blink::ConsoleMessageSource::kJavaScript,
mojom::blink::ConsoleMessageLevel::kWarning, message);
}
}
EnsureServiceConnection();
}
void USB::EnsureServiceConnection() {
if (service_.is_bound())
return;
DCHECK(IsContextSupported(GetExecutionContext()));
DCHECK(IsFeatureEnabled(ReportOptions::kDoNotReport));
// See https://bit.ly/2S0zRAS for task types.
auto task_runner =
GetExecutionContext()->GetTaskRunner(TaskType::kMiscPlatformAPI);
GetExecutionContext()->GetBrowserInterfaceBroker().GetInterface(
service_.BindNewPipeAndPassReceiver(task_runner));
service_.set_disconnect_handler(
WTF::BindOnce(&USB::OnServiceConnectionError, WrapWeakPersistent(this)));
DCHECK(!client_receiver_.is_bound());
service_->SetClient(
client_receiver_.BindNewEndpointAndPassRemote(task_runner));
}
bool USB::IsFeatureEnabled(ReportOptions report_options) const {
return GetExecutionContext()->IsFeatureEnabled(
network::mojom::PermissionsPolicyFeature::kUsb, report_options);
}
void USB::Trace(Visitor* visitor) const {
visitor->Trace(service_);
visitor->Trace(get_devices_requests_);
visitor->Trace(get_permission_requests_);
visitor->Trace(client_receiver_);
visitor->Trace(device_cache_);
EventTarget::Trace(visitor);
Supplement<NavigatorBase>::Trace(visitor);
ExecutionContextLifecycleObserver::Trace(visitor);
}
} // namespace blink
|