Skip to content

Truly independent web browser

LibWeb: Set a PermissionStatus's state before queueing its change event

Shannon Boothe87dae2b5611
diff --git a/Libraries/LibWeb/Bindings/Rust/src/permissions_api/permission_status.rs b/Libraries/LibWeb/Bindings/Rust/src/permissions_api/permission_status.rs
index a5ac2a161fd..5dc930bab41 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/permissions_api/permission_status.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/permissions_api/permission_status.rs
@@ -22,7 +22,7 @@ use crate::bindings::promise::{TaskSource, queue_a_global_task};
use crate::bindings::{CallContext, WebIdlResult};
use crate::permissions::permissions::PermissionName;
use crate::permissions_api::permission_store::register_status;
-use crate::permissions_api::permissions::{PermissionDescriptor, permission_state};
+use crate::permissions_api::permissions::{PermissionDescriptor, permission_query_algorithm, permission_state};
unsafe extern "C" {
fn web_html_window_or_worker_global_scope_register_permission_status(
@@ -131,30 +131,33 @@ impl PermissionStatus {
&self.query
}
- // Reads the state again from the permission store, and fires change at this status if it moved.
+ // https://w3c.github.io/permissions/#PermissionStatus-update
pub(crate) fn update_from_store(this: Ref<Self>, vm: &Vm) {
let global = this.realm.global_object();
- // A document that isn't fully active sees no change until it is again.
- if let Some(window) = window_from_global_object(global)
- && !window.associated_document().is_fully_active()
- {
- return;
+ // 1. If this's relevant global object is a Window object, then:
+ if let Some(window) = window_from_global_object(global) {
+ // 1. Let document be status's relevant global object's associated Document.
+ // 2. If document is null or document is not fully active, terminate this algorithm.
+ if !window.associated_document().is_fully_active() {
+ return;
+ }
}
- let state = permission_state(this.permission_name, &relevant_settings_object(global));
- if state == this.state.get() {
+ // NB: The store tells every status of a change, and only those whose state it moved run these steps.
+ let settings = relevant_settings_object(global);
+ if permission_state(this.permission_name, &settings) == this.state.get() {
return;
}
- // NB: The state is read again when the task runs, so a status that heard of several changes ends with the last.
+ // 2. Let query be status's [[query]] internal slot.
+ let query = this.query();
+
+ // 3. Run query's name's permission query algorithm, passing query and status.
+ permission_query_algorithm(query, &this, &settings);
+
+ // 4. Queue a task on the permissions task source to fire an event named change at status.
queue_a_global_task(vm, global, TaskSource::Permissions, this, |_, this| {
- let global = this.realm.global_object();
- let state = permission_state(this.permission_name, &relevant_settings_object(global));
- if state == this.state.get() {
- return;
- }
- this.state.set(state);
let event = CppEvent::create(this.realm, "change", false, false, false);
this.event_target.dispatch_event(&event);
});
diff --git a/Libraries/LibWeb/Bindings/Rust/src/permissions_api/permissions.rs b/Libraries/LibWeb/Bindings/Rust/src/permissions_api/permissions.rs
index 720b167f1cd..172dbfc793d 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/permissions_api/permissions.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/permissions_api/permissions.rs
@@ -273,7 +273,7 @@ impl Permissions {
}
// https://w3c.github.io/permissions/#dfn-permission-query-algorithm
-fn permission_query_algorithm(
+pub(crate) fn permission_query_algorithm(
permission_desc: &PermissionDescriptor,
status: &PermissionStatus,
settings: &CppEnvironmentSettingsObject,
diff --git a/Tests/LibWeb/Text/expected/PermissionsAPI/permission-status-change-undone-before-its-event.txt b/Tests/LibWeb/Text/expected/PermissionsAPI/permission-status-change-undone-before-its-event.txt
new file mode 100644
index 00000000000..76ead229a32
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/PermissionsAPI/permission-status-change-undone-before-its-event.txt
@@ -0,0 +1,3 @@
+change: granted
+change: granted
+state: granted
diff --git a/Tests/LibWeb/Text/input/PermissionsAPI/permission-status-change-undone-before-its-event.html b/Tests/LibWeb/Text/input/PermissionsAPI/permission-status-change-undone-before-its-event.html
new file mode 100644
index 00000000000..263d78240e1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/PermissionsAPI/permission-status-change-undone-before-its-event.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ await internals.setPermission("geolocation", "granted");
+ const status = await navigator.permissions.query({ name: "geolocation" });
+ status.onchange = () => println(`change: ${status.state}`);
+ // The status hears of a change, and of the change that undoes it, before the first change event fires.
+ internals.setPermission("geolocation", "denied");
+ await internals.setPermission("geolocation", "granted");
+ await new Promise(resolve => setTimeout(resolve, 0));
+ println(`state: ${status.state}`);
+ done();
+ });
+</script>