Bug 1771337 [wpt PR 34217] - Convert `popup=popup` to `popup=auto` or just `popup...
[gecko.git] / dom / workers / WorkerCSPEventListener.cpp
blob4fbb7e74ca66c4a036c1a16d811a22119d4646ed
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "WorkerCSPEventListener.h"
8 #include "WorkerRef.h"
9 #include "WorkerRunnable.h"
10 #include "WorkerScope.h"
11 #include "mozilla/dom/SecurityPolicyViolationEvent.h"
12 #include "mozilla/dom/SecurityPolicyViolationEventBinding.h"
13 #include "mozilla/dom/WorkerRunnable.h"
15 using namespace mozilla::dom;
17 namespace {
19 class WorkerCSPEventRunnable final : public MainThreadWorkerRunnable {
20 public:
21 WorkerCSPEventRunnable(WorkerPrivate* aWorkerPrivate, const nsAString& aJSON)
22 : MainThreadWorkerRunnable(aWorkerPrivate), mJSON(aJSON) {}
24 private:
25 bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
26 SecurityPolicyViolationEventInit violationEventInit;
27 if (NS_WARN_IF(!violationEventInit.Init(mJSON))) {
28 return true;
31 RefPtr<mozilla::dom::Event> event =
32 mozilla::dom::SecurityPolicyViolationEvent::Constructor(
33 aWorkerPrivate->GlobalScope(), u"securitypolicyviolation"_ns,
34 violationEventInit);
35 event->SetTrusted(true);
37 aWorkerPrivate->GlobalScope()->DispatchEvent(*event);
38 return true;
41 const nsString mJSON;
44 } // namespace
46 NS_IMPL_ISUPPORTS(WorkerCSPEventListener, nsICSPEventListener)
48 /* static */
49 already_AddRefed<WorkerCSPEventListener> WorkerCSPEventListener::Create(
50 WorkerPrivate* aWorkerPrivate) {
51 MOZ_ASSERT(aWorkerPrivate);
52 aWorkerPrivate->AssertIsOnWorkerThread();
54 RefPtr<WorkerCSPEventListener> listener = new WorkerCSPEventListener();
56 MutexAutoLock lock(listener->mMutex);
57 listener->mWorkerRef = WeakWorkerRef::Create(aWorkerPrivate, [listener]() {
58 MutexAutoLock lock(listener->mMutex);
59 listener->mWorkerRef = nullptr;
60 });
62 if (NS_WARN_IF(!listener->mWorkerRef)) {
63 return nullptr;
66 return listener.forget();
69 WorkerCSPEventListener::WorkerCSPEventListener()
70 : mMutex("WorkerCSPEventListener::mMutex") {}
72 NS_IMETHODIMP
73 WorkerCSPEventListener::OnCSPViolationEvent(const nsAString& aJSON) {
74 MOZ_ASSERT(NS_IsMainThread());
76 MutexAutoLock lock(mMutex);
77 if (!mWorkerRef) {
78 return NS_OK;
81 WorkerPrivate* workerPrivate = mWorkerRef->GetUnsafePrivate();
82 MOZ_ASSERT(workerPrivate);
84 RefPtr<WorkerCSPEventRunnable> runnable =
85 new WorkerCSPEventRunnable(workerPrivate, aJSON);
86 runnable->Dispatch();
88 return NS_OK;