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/. */
6 
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"
14 
15 using namespace mozilla::dom;
16 
17 namespace {
18 
19 class WorkerCSPEventRunnable final : public MainThreadWorkerRunnable {
20  public:
WorkerCSPEventRunnable(WorkerPrivate * aWorkerPrivate,const nsAString & aJSON)21   WorkerCSPEventRunnable(WorkerPrivate* aWorkerPrivate, const nsAString& aJSON)
22       : MainThreadWorkerRunnable(aWorkerPrivate), mJSON(aJSON) {}
23 
24  private:
WorkerRun(JSContext * aCx,WorkerPrivate * aWorkerPrivate)25   bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
26     SecurityPolicyViolationEventInit violationEventInit;
27     if (NS_WARN_IF(!violationEventInit.Init(mJSON))) {
28       return true;
29     }
30 
31     RefPtr<mozilla::dom::Event> event =
32         mozilla::dom::SecurityPolicyViolationEvent::Constructor(
33             aWorkerPrivate->GlobalScope(), u"securitypolicyviolation"_ns,
34             violationEventInit);
35     event->SetTrusted(true);
36 
37     aWorkerPrivate->GlobalScope()->DispatchEvent(*event);
38     return true;
39   }
40 
41   const nsString mJSON;
42 };
43 
44 }  // namespace
45 
NS_IMPL_ISUPPORTS(WorkerCSPEventListener,nsICSPEventListener)46 NS_IMPL_ISUPPORTS(WorkerCSPEventListener, nsICSPEventListener)
47 
48 /* static */
49 already_AddRefed<WorkerCSPEventListener> WorkerCSPEventListener::Create(
50     WorkerPrivate* aWorkerPrivate) {
51   MOZ_ASSERT(aWorkerPrivate);
52   aWorkerPrivate->AssertIsOnWorkerThread();
53 
54   RefPtr<WorkerCSPEventListener> listener = new WorkerCSPEventListener();
55 
56   MutexAutoLock lock(listener->mMutex);
57   listener->mWorkerRef = WeakWorkerRef::Create(aWorkerPrivate, [listener]() {
58     MutexAutoLock lock(listener->mMutex);
59     listener->mWorkerRef = nullptr;
60   });
61 
62   if (NS_WARN_IF(!listener->mWorkerRef)) {
63     return nullptr;
64   }
65 
66   return listener.forget();
67 }
68 
WorkerCSPEventListener()69 WorkerCSPEventListener::WorkerCSPEventListener()
70     : mMutex("WorkerCSPEventListener::mMutex") {}
71 
72 NS_IMETHODIMP
OnCSPViolationEvent(const nsAString & aJSON)73 WorkerCSPEventListener::OnCSPViolationEvent(const nsAString& aJSON) {
74   MOZ_ASSERT(NS_IsMainThread());
75 
76   MutexAutoLock lock(mMutex);
77   if (!mWorkerRef) {
78     return NS_OK;
79   }
80 
81   WorkerPrivate* workerPrivate = mWorkerRef->GetUnsafePrivate();
82   MOZ_ASSERT(workerPrivate);
83 
84   RefPtr<WorkerCSPEventRunnable> runnable =
85       new WorkerCSPEventRunnable(workerPrivate, aJSON);
86   runnable->Dispatch();
87 
88   return NS_OK;
89 }
90