Skip to content
Permalink
Browse files
Add support for PermissionStatus.onchange in workers
https://bugs.webkit.org/show_bug.cgi?id=245090

Reviewed by Chris Dumez and Sihui Liu.

The onchange event of the PermissionStatus object is supposed to
be supported in both window and worker contexts. A previous patch
added support for window contexts and this patch adds support for
dedicated, service, and shared workers.

Since WebPermissionController::permissionChanged is only ever called
on the main thread, and it calls PermissionObserver::stateChanged,
the PermissionObserver it uses should be a main thread object.
Before this patch, when there was only support for window contexts,
all PermissionStatus objects were main thread objects. But now,
PermissionStatus objects can be created on worker threads as well.
When this happens, an associated MainThreadPermissionObserver is created
on the main thread and there is a one-to-one mapping between the worker
thread PermissionStatus object and the MainThreadPermissionObserver.
Then, when WebPermissionController::permissionChanged finds a
PermissionObserver to call stateChanged on, that PermissionObserver is
either a main thread PermissionStatus object (in the window case) or a
main thread MainThreadPermissionObserver, whose stateChanged function
will jump to the relevant worker thread and invoke the stateChanged
function of the associated worker thread PermissionStatus object.

* LayoutTests/TestExpectations:
* LayoutTests/http/tests/permissions/service-worker-permission-status-onchange-event-expected.txt: Added.
* LayoutTests/http/tests/permissions/service-worker-permission-status-onchange-event.html: Added.
* LayoutTests/http/tests/permissions/service-worker-permission-status-onchange-event.js: Added.
(self.onmessage):
* LayoutTests/http/tests/permissions/shared-worker-permission-status-onchange-event-expected.txt: Added.
* LayoutTests/http/tests/permissions/shared-worker-permission-status-onchange-event.html: Added.
* LayoutTests/http/tests/permissions/shared-worker-permission-status-onchange-event.js: Added.
(port.onmessage):
(onconnect):
* LayoutTests/http/tests/permissions/worker-permission-status-onchange-event-expected.txt: Added.
* LayoutTests/http/tests/permissions/worker-permission-status-onchange-event.html: Added.
* LayoutTests/http/tests/permissions/worker-permission-status-onchange-event.js: Added.
(onmessage):
* LayoutTests/imported/w3c/web-platform-tests/permissions/permissions-cg.https-expected.txt:
* LayoutTests/platform/wk2/TestExpectations:
* Source/WebCore/Headers.cmake:
* Source/WebCore/Modules/permissions/MainThreadPermissionObserver.cpp: Added.
(WebCore::MainThreadPermissionObserver::MainThreadPermissionObserver):
(WebCore::MainThreadPermissionObserver::~MainThreadPermissionObserver):
(WebCore::MainThreadPermissionObserver::stateChanged):
* Source/WebCore/Modules/permissions/MainThreadPermissionObserver.h: Copied from Source/WebCore/Modules/permissions/PermissionStatus.h.
* Source/WebCore/Modules/permissions/MainThreadPermissionObserverIdentifier.h: Copied from Source/WebCore/Modules/permissions/PermissionObserver.h.
* Source/WebCore/Modules/permissions/PermissionController.h:
* Source/WebCore/Modules/permissions/PermissionObserver.h:
* Source/WebCore/Modules/permissions/PermissionStatus.cpp:
(WebCore::allMainThreadPermissionObservers):
(WebCore::PermissionStatus::create):
(WebCore::PermissionStatus::PermissionStatus):
(WebCore::PermissionStatus::~PermissionStatus):
(WebCore::PermissionStatus::stateChanged):
* Source/WebCore/Modules/permissions/PermissionStatus.h:
* Source/WebCore/Modules/permissions/Permissions.cpp:
(WebCore::Permissions::query):
* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebKit/WebProcess/WebCoreSupport/WebPermissionController.cpp:
(WebKit::WebPermissionController::query):
(WebKit::WebPermissionController::permissionChanged):
* Source/WebKit/WebProcess/WebCoreSupport/WebPermissionController.h:
* Tools/WebKitTestRunner/TestController.cpp:
(WTR::TestController::handleQueryPermission):
(WTR::TestController::resetStateToConsistentValues):
(WTR::TestController::setGeolocationPermission):
* Tools/WebKitTestRunner/TestController.h:

Canonical link: https://commits.webkit.org/254490@main
  • Loading branch information
RupinMittal authored and szewai committed Sep 14, 2022
1 parent fc2b16c commit 65f6f1007c12d8371600f83bef65b52101a447f3
Show file tree
Hide file tree
Showing 27 changed files with 503 additions and 44 deletions.
@@ -216,6 +216,7 @@ fast/speechrecognition/start-second-recognition.html [ Skip ]
fast/speechrecognition/ios/ [ Skip ]
imported/w3c/web-platform-tests/permissions/ [ Skip ]
http/tests/notifications/permission/ [ Skip ]
http/tests/permissions/ [ Skip ]
imported/w3c/web-platform-tests/storage/ [ Skip ]
imported/w3c/web-platform-tests/file-system-access/ [ Skip ]
storage/filesystemaccess/ [ Skip ]
@@ -0,0 +1,15 @@
This test checks that the Permissions API change event works for service workers

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".


No permission state is set
PASS message.data is "prompt"
PASS receivedPostMessageResponse became true
Permission has been granted
PASS message.data is "granted"
PASS receivedPostMessageResponse became true
PASS successfullyParsed is true

TEST COMPLETE

@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/js-test-pre.js"></script>
<script src="/resources/notifications-test-pre.js"></script>
</head>
<body>
<script>

description("This test checks that the Permissions API change event works for service workers")

jsTestIsAsync = true;

(async function() {
expectedData = null;
receivedPostMessageResponse = false;

navigator.serviceWorker.register('service-worker-permission-status-onchange-event.js');
navigator.serviceWorker.addEventListener('message', (message) => {
window.message = message;
shouldBeEqualToString("message.data", expectedData);
receivedPostMessageResponse = true;
});

debug("No permission state is set");
receivedPostMessageResponse = false;
expectedData = "prompt";
navigator.serviceWorker.ready.then((registration) => {
registration.active.postMessage(1);
});
await shouldBecomeEqual("receivedPostMessageResponse", "true");

receivedPostMessageResponse = false;
expectedData = "granted";
testRunner.setGeolocationPermission(true);
debug("Permission has been granted");
await shouldBecomeEqual("receivedPostMessageResponse", "true");
finishJSTest();
})();

</script>
<script src="/resources/js-test-post.js"></script>
</body>
</html>
@@ -0,0 +1,11 @@
var permissionStatus;

self.onmessage = (event) => {
navigator.permissions.query({ name: "geolocation" }).then((result)=>{
permissionStatus = result;

permissionStatus.onchange = () => event.source.postMessage(permissionStatus.state);

event.source.postMessage(permissionStatus.state);
});
};
@@ -0,0 +1,15 @@
This test checks that the Permissions API change event works for shared workers

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".


No permission state is set
PASS message.data is "prompt"
PASS receivedPostMessageResponse became true
Permission has been granted
PASS message.data is "granted"
PASS receivedPostMessageResponse became true
PASS successfullyParsed is true

TEST COMPLETE

@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/js-test-pre.js"></script>
<script src="/resources/notifications-test-pre.js"></script>
</head>
<body>
<script>

description("This test checks that the Permissions API change event works for shared workers")

jsTestIsAsync = true;

(async function() {
expectedData = null;
receivedPostMessageResponse = false;

var worker = new SharedWorker('shared-worker-permission-status-onchange-event.js');
worker.port.onmessage = function(message) {
window.message = message;
shouldBeEqualToString("message.data", expectedData);
receivedPostMessageResponse = true;
}

debug("No permission state is set");
receivedPostMessageResponse = false;
expectedData = "prompt";
worker.port.postMessage(1);
await shouldBecomeEqual("receivedPostMessageResponse", "true");

receivedPostMessageResponse = false;
expectedData = "granted";
testRunner.setGeolocationPermission(true);
debug("Permission has been granted");
await shouldBecomeEqual("receivedPostMessageResponse", "true");
finishJSTest();
})();

</script>
<script src="/resources/js-test-post.js"></script>
</body>
</html>
@@ -0,0 +1,16 @@
var permissionStatus;

onconnect = function (e) {
var port = e.ports[0];

port.onmessage = function(e) {
navigator.permissions.query({ name: "geolocation" }).then((result)=>{
permissionStatus = result;

permissionStatus.onchange = () => port.postMessage(permissionStatus.state);

port.postMessage(permissionStatus.state);
});
}

}
@@ -0,0 +1,15 @@
This test checks that the Permissions API change event works for dedicated workers

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".


No permission state is set
PASS message.data is "prompt"
PASS receivedPostMessageResponse became true
Permission has been granted
PASS message.data is "granted"
PASS receivedPostMessageResponse became true
PASS successfullyParsed is true

TEST COMPLETE

@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/js-test-pre.js"></script>
<script src="/resources/notifications-test-pre.js"></script>
</head>
<body>
<script>

description("This test checks that the Permissions API change event works for dedicated workers")

jsTestIsAsync = true;

(async function() {
expectedData = null;
receivedPostMessageResponse = false;

var worker = new Worker('worker-permission-status-onchange-event.js');
worker.onmessage = function(message) {
window.message = message;
shouldBeEqualToString("message.data", expectedData);
receivedPostMessageResponse = true;
}

debug("No permission state is set");
receivedPostMessageResponse = false;
expectedData = "prompt";
worker.postMessage(1);
await shouldBecomeEqual("receivedPostMessageResponse", "true");

receivedPostMessageResponse = false;
expectedData = "granted";
testRunner.setGeolocationPermission(true);
debug("Permission has been granted");
await shouldBecomeEqual("receivedPostMessageResponse", "true");
finishJSTest();
})();

</script>
<script src="/resources/js-test-post.js"></script>
</body>
</html>
@@ -0,0 +1,11 @@
var permissionStatus;

onmessage = function(e) {
navigator.permissions.query({ name: "geolocation" }).then((result)=>{
permissionStatus = result;

permissionStatus.onchange = () => postMessage(permissionStatus.state);

postMessage(permissionStatus.state);
});
}
@@ -1,5 +1,3 @@

Harness Error (TIMEOUT), message = null

TIMEOUT status is not garbage collected when it goes out of scope Test timed out
PASS status is not garbage collected when it goes out of scope

@@ -874,6 +874,7 @@ fast/speechrecognition/start-second-recognition.html [ Pass ]
fullscreen/full-screen-enter-while-exiting.html [ Pass ]
imported/w3c/web-platform-tests/permissions/ [ Pass ]
http/tests/notifications/permission/ [ Pass ]
http/tests/permissions/ [ Pass ]
imported/w3c/web-platform-tests/storage/ [ Pass ]
imported/w3c/web-platform-tests/file-system-access/ [ Pass ]
storage/filesystemaccess/ [ Pass ]
@@ -305,6 +305,8 @@ set(WebCore_PRIVATE_FRAMEWORK_HEADERS
Modules/notifications/NotificationPermissionCallback.h
Modules/notifications/NotificationResources.h

Modules/permissions/MainThreadPermissionObserver.h
Modules/permissions/MainThreadPermissionObserverIdentifier.h
Modules/permissions/PermissionController.h
Modules/permissions/PermissionDescriptor.h
Modules/permissions/PermissionName.h
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include "MainThreadPermissionObserver.h"

#include "ClientOrigin.h"
#include "Document.h"
#include "PermissionController.h"
#include "PermissionState.h"
#include "ScriptExecutionContext.h"

namespace WebCore {

MainThreadPermissionObserver::MainThreadPermissionObserver(WeakPtr<PermissionObserver>&& permissionObserver, ScriptExecutionContextIdentifier contextIdentifier, PermissionState state, PermissionDescriptor descriptor, PermissionQuerySource source, WeakPtr<Page>&& page, ClientOrigin&& origin)
: m_permissionObserver(WTFMove(permissionObserver))
, m_contextIdentifier(contextIdentifier)
, m_state(state)
, m_descriptor(descriptor)
, m_source(source)
, m_page(WTFMove(page))
, m_origin(WTFMove(origin))
{
ASSERT(isMainThread());
PermissionController::shared().addObserver(*this);
}

MainThreadPermissionObserver::~MainThreadPermissionObserver()
{
ASSERT(isMainThread());
PermissionController::shared().removeObserver(*this);
}

void MainThreadPermissionObserver::stateChanged(PermissionState newPermissionState)
{
m_state = newPermissionState;

ScriptExecutionContext::postTaskTo(m_contextIdentifier, [permissionObserver = m_permissionObserver, newPermissionState](auto&) {
if (permissionObserver)
permissionObserver->stateChanged(newPermissionState);
});
}

}
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include "ClientOrigin.h"
#include "MainThreadPermissionObserverIdentifier.h"
#include "PermissionDescriptor.h"
#include "PermissionObserver.h"
#include "PermissionQuerySource.h"
#include "PermissionState.h"
#include "ScriptExecutionContextIdentifier.h"

namespace WebCore {

class Page;

class MainThreadPermissionObserver final : public PermissionObserver {
WTF_MAKE_NONCOPYABLE(MainThreadPermissionObserver);
WTF_MAKE_FAST_ALLOCATED;
public:
MainThreadPermissionObserver(WeakPtr<PermissionObserver>&&, ScriptExecutionContextIdentifier, PermissionState, PermissionDescriptor, PermissionQuerySource, WeakPtr<Page>&&, ClientOrigin&&);
~MainThreadPermissionObserver();

private:
// PermissionObserver
PermissionState currentState() const final { return m_state; }
void stateChanged(PermissionState) final;
const ClientOrigin& origin() const final { return m_origin; }
PermissionDescriptor descriptor() const final { return m_descriptor; }
PermissionQuerySource source() const final { return m_source; }
const WeakPtr<Page>& page() const final { return m_page; }

WeakPtr<PermissionObserver> m_permissionObserver;
ScriptExecutionContextIdentifier m_contextIdentifier;
PermissionState m_state;
PermissionDescriptor m_descriptor;
PermissionQuerySource m_source;
WeakPtr<Page> m_page;
ClientOrigin m_origin;
};

} // namespace WebCore

0 comments on commit 65f6f10

Please sign in to comment.