Implement initial version of ReportingObserver (#37905)

The specification moved around lately with how it defines its reports
and report bodies. They became dictionaries, but are currently missing
some fields [1].

Most tests won't be passing yet, since the `Reporting-Endpoints` header
isn't used yet. In fact, the specification leaves it up to the browser
to figure out when to run this task [2]. I am not sure if there some
background scheduling we can do here.

Confirmed with content-security-policy/reporting-api/
report-to-directive-allowed-in-meta.https.sub.html that the callback is
invoked. The test doesn't pass, since
the `describe_scripted_caller` is empty for HTML elements. Thus the
`source_file` is empty, whereas it should be equivalent to the current
document URL.

Part of #37328

Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com>

[1]: https://github.com/w3c/reporting/issues/286
[2]: https://w3c.github.io/reporting/#report-delivery
This commit is contained in:
Tim van der Lippe 2025-07-07 12:43:30 +02:00 committed by GitHub
parent 3d4868592a
commit fcb2a4cd95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 551 additions and 101 deletions

View file

@ -76,6 +76,7 @@ use crate::dom::bindings::codegen::Bindings::NotificationBinding::NotificationPe
use crate::dom::bindings::codegen::Bindings::PermissionStatusBinding::{
PermissionName, PermissionState,
};
use crate::dom::bindings::codegen::Bindings::ReportingObserverBinding::Report;
use crate::dom::bindings::codegen::Bindings::VoidFunctionBinding::VoidFunction;
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use crate::dom::bindings::codegen::Bindings::WorkerGlobalScopeBinding::WorkerGlobalScopeMethods;
@ -109,6 +110,7 @@ use crate::dom::performance::Performance;
use crate::dom::performanceobserver::VALID_ENTRY_TYPES;
use crate::dom::promise::Promise;
use crate::dom::readablestream::{CrossRealmTransformReadable, ReadableStream};
use crate::dom::reportingobserver::ReportingObserver;
use crate::dom::serviceworker::ServiceWorker;
use crate::dom::serviceworkerregistration::ServiceWorkerRegistration;
use crate::dom::trustedtypepolicyfactory::TrustedTypePolicyFactory;
@ -3332,6 +3334,56 @@ impl GlobalScope {
unreachable!();
}
pub(crate) fn append_reporting_observer(&self, reporting_observer: &ReportingObserver) {
if let Some(window) = self.downcast::<Window>() {
return window.append_reporting_observer(DomRoot::from_ref(reporting_observer));
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.append_reporting_observer(DomRoot::from_ref(reporting_observer));
}
unreachable!();
}
pub(crate) fn remove_reporting_observer(&self, reporting_observer: &ReportingObserver) {
if let Some(window) = self.downcast::<Window>() {
return window.remove_reporting_observer(reporting_observer);
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.remove_reporting_observer(reporting_observer);
}
unreachable!();
}
pub(crate) fn registered_reporting_observers(&self) -> Vec<DomRoot<ReportingObserver>> {
if let Some(window) = self.downcast::<Window>() {
return window.registered_reporting_observers();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.registered_reporting_observers();
}
unreachable!();
}
pub(crate) fn append_report(&self, report: Report) {
if let Some(window) = self.downcast::<Window>() {
return window.append_report(report);
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.append_report(report);
}
unreachable!();
}
pub(crate) fn buffered_reports(&self) -> Vec<Report> {
if let Some(window) = self.downcast::<Window>() {
return window.buffered_reports();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.buffered_reports();
}
unreachable!();
}
pub(crate) fn import_map(&self) -> Ref<'_, ImportMap> {
self.import_map.borrow()
}