Skip to content

Truly independent web browser

LibWeb: Send SharedWorker's extendedLifetime to the worker manager

Shannon Boothfe4938eb33ce
diff --git a/Libraries/LibWeb/HTML/SharedWorker.h b/Libraries/LibWeb/HTML/SharedWorker.h
index 7cfa9a52162..abd0bc78664 100644
--- a/Libraries/LibWeb/HTML/SharedWorker.h
+++ b/Libraries/LibWeb/HTML/SharedWorker.h
@@ -37,6 +37,8 @@ public:
return m_port;
}
+ Bindings::SharedWorkerOptions const& options() const { return m_options; }
+
void set_agent(WorkerAgentParent& agent) { m_agent = agent; }
private:
diff --git a/Libraries/LibWeb/HTML/Worker.cpp b/Libraries/LibWeb/HTML/Worker.cpp
index cf1ba5ead71..7e726235e34 100644
--- a/Libraries/LibWeb/HTML/Worker.cpp
+++ b/Libraries/LibWeb/HTML/Worker.cpp
@@ -113,8 +113,13 @@ void run_a_worker(Variant<GC::Ref<Worker>, GC::Ref<SharedWorker>> worker, URL::U
auto event_target = worker.visit([](auto& worker) -> GC::Ref<DOM::EventTarget> { return worker; });
+ // NB: Only SharedWorkerOptions has an extendedLifetime member.
+ auto extended_lifetime = worker.visit(
+ [](GC::Ref<Worker>) { return false; },
+ [](GC::Ref<SharedWorker> shared_worker) { return shared_worker->options().extended_lifetime; });
+
// Note: This spawns a new process to act as the 'agent' for the worker.
- auto agent = WorkerAgentParent::create(url, options, port, outside_settings, event_target, agent_type);
+ auto agent = WorkerAgentParent::create(url, options, extended_lifetime, port, outside_settings, event_target, agent_type);
worker.visit([&](auto worker) { worker->set_agent(agent); });
}
diff --git a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
index c0b7df4dc44..0ab246cf94a 100644
--- a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
+++ b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
@@ -52,19 +52,20 @@ static void report_worker_exception(WindowOrWorkerGlobalScopeMixin& window_or_wo
report_an_exception(relevant_global_object(window_or_worker), wrapped_error, move(error_info), OmitError::Yes);
}
-GC::Ref<WorkerAgentParent> WorkerAgentParent::create(URL::URL url, WorkerOptions const& options,
+GC::Ref<WorkerAgentParent> WorkerAgentParent::create(URL::URL url, WorkerOptions const& options, bool extended_lifetime,
GC::Ptr<MessagePort> outside_port, GC::Ref<EnvironmentSettingsObject> outside_settings,
GC::Ref<DOM::EventTarget> worker_event_target, AgentType agent_type)
{
- auto agent = GC::Heap::the().allocate<WorkerAgentParent>(move(url), options, outside_port, outside_settings, worker_event_target, agent_type);
+ auto agent = GC::Heap::the().allocate<WorkerAgentParent>(move(url), options, extended_lifetime, outside_port, outside_settings, worker_event_target, agent_type);
agent->start();
return agent;
}
-WorkerAgentParent::WorkerAgentParent(URL::URL url, WorkerOptions const& options, GC::Ptr<MessagePort> outside_port,
+WorkerAgentParent::WorkerAgentParent(URL::URL url, WorkerOptions const& options, bool extended_lifetime, GC::Ptr<MessagePort> outside_port,
GC::Ref<EnvironmentSettingsObject> outside_settings, GC::Ref<DOM::EventTarget> worker_event_target,
AgentType agent_type)
: m_worker_options(options)
+ , m_extended_lifetime(extended_lifetime)
, m_agent_type(agent_type)
, m_url(move(url))
, m_owner_token(next_owner_token())
@@ -113,6 +114,7 @@ void WorkerAgentParent::start()
.type = m_worker_options.type,
.credentials = m_worker_options.credentials,
.name = m_worker_options.name.to_utf8(),
+ .extended_lifetime = m_extended_lifetime,
.outside_port = move(data_holder),
.outside_settings = serialized_outside_settings,
.caller_is_secure_context = is_secure_context(*m_outside_settings),
diff --git a/Libraries/LibWeb/HTML/WorkerAgentParent.h b/Libraries/LibWeb/HTML/WorkerAgentParent.h
index 251d09e025e..1c4ec60c660 100644
--- a/Libraries/LibWeb/HTML/WorkerAgentParent.h
+++ b/Libraries/LibWeb/HTML/WorkerAgentParent.h
@@ -22,7 +22,7 @@ class WorkerAgentParent : public JS::Cell {
GC_DECLARE_ALLOCATOR(WorkerAgentParent);
public:
- static GC::Ref<WorkerAgentParent> create(URL::URL, WorkerOptions const&,
+ static GC::Ref<WorkerAgentParent> create(URL::URL, WorkerOptions const&, bool extended_lifetime,
GC::Ptr<MessagePort> outside_port, GC::Ref<EnvironmentSettingsObject> outside_settings,
GC::Ref<DOM::EventTarget> worker_event_target, AgentType);
@@ -39,7 +39,7 @@ protected:
virtual void finalize() override;
private:
- WorkerAgentParent(URL::URL, WorkerOptions const&, GC::Ptr<MessagePort> outside_port,
+ WorkerAgentParent(URL::URL, WorkerOptions const&, bool extended_lifetime, GC::Ptr<MessagePort> outside_port,
GC::Ref<EnvironmentSettingsObject> outside_settings, GC::Ref<DOM::EventTarget> worker_event_target,
AgentType);
@@ -52,6 +52,7 @@ private:
static WorkerAgentOwnerToken next_owner_token();
WorkerOptions m_worker_options;
+ bool m_extended_lifetime { false };
AgentType m_agent_type { AgentType::DedicatedWorker };
URL::URL m_url;
WorkerAgentId m_agent_id { 0 };
diff --git a/Libraries/LibWebCommon/HTML/WorkerAgentTypes.h b/Libraries/LibWebCommon/HTML/WorkerAgentTypes.h
index 9618e6ba387..a95fa799ef7 100644
--- a/Libraries/LibWebCommon/HTML/WorkerAgentTypes.h
+++ b/Libraries/LibWebCommon/HTML/WorkerAgentTypes.h
@@ -24,7 +24,6 @@ struct WEBCOMMON_API WorkerAgentStartRequest {
WorkerType type { WorkerType::Classic };
RequestCredentials credentials { RequestCredentials::SameOrigin };
String name;
- // FIXME: We don't implement SharedWorkerOptions/extendedLifetime yet.
bool extended_lifetime { false };
TransferDataEncoder outside_port;
SerializedEnvironmentSettingsObject outside_settings;
diff --git a/Libraries/LibWebView/WorkerProcessManager.cpp b/Libraries/LibWebView/WorkerProcessManager.cpp
index ac71dc0aacd..30f979dd331 100644
--- a/Libraries/LibWebView/WorkerProcessManager.cpp
+++ b/Libraries/LibWebView/WorkerProcessManager.cpp
@@ -135,7 +135,6 @@ Web::HTML::WorkerAgentId WorkerProcessManager::start_worker_agent(Owner owner, O
// AD-HOC: Error firing is routed back over IPC via notify_worker_script_load_failure;
// the spec's queue-global-task happens inside WorkerAgentParent in the
// requesting process.
- // FIXME: The extendedLifetime comparison becomes observable once SharedWorkerOptions is implemented.
if (agent.worker_type != request.type
|| agent.credentials != request.credentials
|| agent.extended_lifetime != request.extended_lifetime
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index cca0a78b130..96b9f84b822 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -1191,6 +1191,7 @@ Text/input/wpt-import/fullscreen/api/element-ready-check-allowed-cross-origin.su
Text/input/wpt-import/fullscreen/api/element-ready-check-not-allowed-cross-origin.sub.html
Text/input/wpt-import/fullscreen/api/element-request-fullscreen-cross-origin.sub.html
Text/input/wpt-import/fullscreen/api/navigate-iframe.sub.html
+Text/input/wpt-import/workers/shared-worker-options-mismatch.html
[CapFrameRate]
; Ends a smooth scroll once 20 frames see no change, which frames faster than the compositor's ticks do
diff --git a/Tests/LibWeb/Text/expected/wpt-import/workers/shared-worker-options-mismatch.txt b/Tests/LibWeb/Text/expected/wpt-import/workers/shared-worker-options-mismatch.txt
new file mode 100644
index 00000000000..620c50c3bca
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/workers/shared-worker-options-mismatch.txt
@@ -0,0 +1,37 @@
+Harness status: OK
+
+Found 32 tests
+
+32 Pass
+Pass Connecting to shared worker with different options should be blocked: default to module
+Pass Connecting to shared worker with different options should be blocked: module to default
+Pass Connecting to shared worker with different options should be blocked: classic to module
+Pass Connecting to shared worker with different options should be blocked: module to classic
+Pass Connecting to shared worker with different options should be blocked: default to omit in classic
+Pass Connecting to shared worker with different options should be blocked: default to include in classic
+Pass Connecting to shared worker with different options should be blocked: omit to default in classic
+Pass Connecting to shared worker with different options should be blocked: omit to same-origin in classic
+Pass Connecting to shared worker with different options should be blocked: omit to include in classic
+Pass Connecting to shared worker with different options should be blocked: same-origin to omit in classic
+Pass Connecting to shared worker with different options should be blocked: same-origin to include in classic
+Pass Connecting to shared worker with different options should be blocked: include to default in classic
+Pass Connecting to shared worker with different options should be blocked: include to omit in classic
+Pass Connecting to shared worker with different options should be blocked: include to same-origin in classic
+Pass Connecting to shared worker with different options should be blocked: default to extended in classic
+Pass Connecting to shared worker with different options should be blocked: extended to default in classic
+Pass Connecting to shared worker with different options should be blocked: true to false in classic
+Pass Connecting to shared worker with different options should be blocked: false to true in classic
+Pass Connecting to shared worker with different options should be blocked: default to omit in module
+Pass Connecting to shared worker with different options should be blocked: default to include in module
+Pass Connecting to shared worker with different options should be blocked: omit to default in module
+Pass Connecting to shared worker with different options should be blocked: omit to same-origin in module
+Pass Connecting to shared worker with different options should be blocked: omit to include in module
+Pass Connecting to shared worker with different options should be blocked: same-origin to omit in module
+Pass Connecting to shared worker with different options should be blocked: same-origin to include in module
+Pass Connecting to shared worker with different options should be blocked: include to default in module
+Pass Connecting to shared worker with different options should be blocked: include to omit in module
+Pass Connecting to shared worker with different options should be blocked: include to same-origin in module
+Pass Connecting to shared worker with different options should be blocked: default to extended in module
+Pass Connecting to shared worker with different options should be blocked: extended to default in module
+Pass Connecting to shared worker with different options should be blocked: true to false in module
+Pass Connecting to shared worker with different options should be blocked: false to true in module
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/shared-worker-options-mismatch.html b/Tests/LibWeb/Text/input/wpt-import/workers/shared-worker-options-mismatch.html
new file mode 100644
index 00000000000..eee73d349fa
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/workers/shared-worker-options-mismatch.html
@@ -0,0 +1,81 @@
+<!DOCTYPE html>
+<title>SharedWorker: type or credentials mismatch failure</title>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script>
+
+const mismatch_test = (testName, firstOptions, secondOptions) => {
+ promise_test(async () => {
+ firstOptions.name = testName;
+ secondOptions.name = testName;
+ const scriptURL = 'support/SharedWorker-common.js';
+ const firstWorker = new SharedWorker(scriptURL, firstOptions);
+ const secondWorker = new SharedWorker(scriptURL, secondOptions);
+ const result = new Promise((resolve, reject) => {
+ secondWorker.onerror = resolve;
+ secondWorker.port.onmessage = () => reject("Worker loaded succesfully");
+ });
+ secondWorker.port.postMessage("ping");
+ await result;
+ firstWorker.port.postMessage("close");
+ secondWorker.port.postMessage("close");
+ return result;
+ }, 'Connecting to shared worker with different options should be blocked: '
+ + testName);
+};
+
+// Tests different type.
+mismatch_test('default to module', {}, { type: 'module' });
+mismatch_test('module to default', { type: 'module' }, {});
+mismatch_test('classic to module', { type: 'classic' }, { type: 'module' });
+mismatch_test('module to classic', { type: 'module' }, { type: 'classic' });
+
+['classic', 'module'].forEach(type => {
+ // Tests different credentials.
+ mismatch_test('default to omit in ' + type,
+ { type },
+ { type, credentials: 'omit' });
+ mismatch_test('default to include in ' + type,
+ { type },
+ { type, credentials: 'include' });
+ mismatch_test('omit to default in ' + type,
+ { type, credentials: 'omit' },
+ { type });
+ mismatch_test('omit to same-origin in ' + type,
+ { type, credentials: 'omit' },
+ { type, credentials: 'same-origin' });
+ mismatch_test('omit to include in ' + type,
+ { type, credentials: 'omit' },
+ { type, credentials: 'include' });
+ mismatch_test('same-origin to omit in ' + type,
+ { type, credentials: 'same-origin'},
+ { type, credentials: 'omit' });
+ mismatch_test('same-origin to include in ' + type,
+ { type, credentials: 'same-origin'},
+ { type, credentials: 'include' });
+ mismatch_test('include to default in ' + type,
+ { type, credentials: 'include' },
+ { type });
+ mismatch_test('include to omit in ' + type,
+ { type, credentials: 'include' },
+ { type, credentials: 'omit' });
+ mismatch_test('include to same-origin in ' + type,
+ { type, credentials: 'include' },
+ { type, credentials: 'same-origin' });
+
+ // Tests different extendedLifetime.
+ mismatch_test('default to extended in ' + type,
+ { type },
+ { type, extendedLifetime: true });
+ mismatch_test('extended to default in ' + type,
+ { type, extendedLifetime: true },
+ { type });
+ mismatch_test('true to false in ' + type,
+ { type, extendedLifetime: true },
+ { type, extendedLifetime: false });
+ mismatch_test('false to true in ' + type,
+ { type, extendedLifetime: false },
+ { type, extendedLifetime: true });
+});
+
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/support/SharedWorker-common.js b/Tests/LibWeb/Text/input/wpt-import/workers/support/SharedWorker-common.js
new file mode 100644
index 00000000000..1c1dac39714
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/workers/support/SharedWorker-common.js
@@ -0,0 +1,32 @@
+function generateError()
+{
+ // Generate an exception by accessing an undefined variable.
+ foo.bar = 0;
+}
+
+onconnect = function(event) {
+ event.ports[0].onmessage = function(evt) { handleMessage(evt, event.ports[0]); };
+};
+
+function handleMessage(event, port) {
+ self.port = port;
+ if (event.data == "ping")
+ port.postMessage("PASS: Received ping message");
+ else if (event.data == "close")
+ close();
+ else if (event.data == "done")
+ port.postMessage("DONE");
+ else if (event.data == "throw")
+ generateError();
+ else if (event.data == "testingNameAttribute")
+ port.postMessage(self.name);
+ else if (/eval.+/.test(event.data)) {
+ try {
+ port.postMessage(event.data.substr(5) + ": " + eval(event.data.substr(5)));
+ } catch (ex) {
+ port.postMessage(event.data.substr(5) + ": " + ex);
+ }
+ }
+ else
+ port.postMessage("FAILURE: Received unknown message: " + event.data);
+}