Bug 1874684 - Part 38: Enable now passing tests. r=allstarschh
[gecko.git] / dom / workers / WorkerCSPEventListener.cpp
blobc693928486a48d0a1362d7ac65aa74cf09daf366
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, "WorkerCSPEventRunnable"),
23 mJSON(aJSON) {}
25 private:
26 bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
27 SecurityPolicyViolationEventInit violationEventInit;
28 if (NS_WARN_IF(!violationEventInit.Init(mJSON))) {
29 return true;
32 RefPtr<mozilla::dom::Event> event =
33 mozilla::dom::SecurityPolicyViolationEvent::Constructor(
34 aWorkerPrivate->GlobalScope(), u"securitypolicyviolation"_ns,
35 violationEventInit);
36 event->SetTrusted(true);
38 aWorkerPrivate->GlobalScope()->DispatchEvent(*event);
39 return true;
42 const nsString mJSON;
45 } // namespace
47 NS_IMPL_ISUPPORTS(WorkerCSPEventListener, nsICSPEventListener)
49 /* static */
50 already_AddRefed<WorkerCSPEventListener> WorkerCSPEventListener::Create(
51 WorkerPrivate* aWorkerPrivate) {
52 MOZ_ASSERT(aWorkerPrivate);
53 aWorkerPrivate->AssertIsOnWorkerThread();
55 RefPtr<WorkerCSPEventListener> listener = new WorkerCSPEventListener();
57 MutexAutoLock lock(listener->mMutex);
58 listener->mWorkerRef = WeakWorkerRef::Create(aWorkerPrivate, [listener]() {
59 MutexAutoLock lock(listener->mMutex);
60 listener->mWorkerRef = nullptr;
61 });
63 if (NS_WARN_IF(!listener->mWorkerRef)) {
64 return nullptr;
67 return listener.forget();
70 WorkerCSPEventListener::WorkerCSPEventListener()
71 : mMutex("WorkerCSPEventListener::mMutex") {}
73 NS_IMETHODIMP
74 WorkerCSPEventListener::OnCSPViolationEvent(const nsAString& aJSON) {
75 MutexAutoLock lock(mMutex);
76 if (!mWorkerRef) {
77 return NS_OK;
80 WorkerPrivate* workerPrivate = mWorkerRef->GetUnsafePrivate();
81 MOZ_ASSERT(workerPrivate);
83 if (NS_IsMainThread()) {
84 RefPtr<WorkerCSPEventRunnable> runnable =
85 new WorkerCSPEventRunnable(workerPrivate, aJSON);
86 runnable->Dispatch();
88 return NS_OK;
91 SecurityPolicyViolationEventInit violationEventInit;
92 if (NS_WARN_IF(!violationEventInit.Init(aJSON))) {
93 return NS_ERROR_UNEXPECTED;
96 RefPtr<mozilla::dom::Event> event =
97 mozilla::dom::SecurityPolicyViolationEvent::Constructor(
98 workerPrivate->GlobalScope(), u"securitypolicyviolation"_ns,
99 violationEventInit);
100 event->SetTrusted(true);
102 workerPrivate->GlobalScope()->DispatchEvent(*event);
104 return NS_OK;