Bug 1550519 - Show a translucent parent highlight when a subgrid is highlighted....
[gecko.git] / dom / workers / WorkerEventTarget.cpp
blob13c3c9dcc03a43e03e69f5ec07c277bd53dfd462
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 "WorkerEventTarget.h"
9 namespace mozilla {
10 namespace dom {
12 namespace {
14 class WrappedControlRunnable final : public WorkerControlRunnable {
15 nsCOMPtr<nsIRunnable> mInner;
17 ~WrappedControlRunnable() {}
19 public:
20 WrappedControlRunnable(WorkerPrivate* aWorkerPrivate,
21 already_AddRefed<nsIRunnable>&& aInner)
22 : WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
23 mInner(aInner) {}
25 virtual bool PreDispatch(WorkerPrivate* aWorkerPrivate) override {
26 // Silence bad assertions, this can be dispatched from any thread.
27 return true;
30 virtual void PostDispatch(WorkerPrivate* aWorkerPrivate,
31 bool aDispatchResult) override {
32 // Silence bad assertions, this can be dispatched from any thread.
35 bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
36 mInner->Run();
37 return true;
40 nsresult Cancel() override {
41 nsCOMPtr<nsICancelableRunnable> cr = do_QueryInterface(mInner);
43 // If the inner runnable is not cancellable, then just do the normal
44 // WorkerControlRunnable thing. This will end up calling Run().
45 if (!cr) {
46 WorkerControlRunnable::Cancel();
47 return NS_OK;
50 // Otherwise call the inner runnable's Cancel() and treat this like
51 // a WorkerRunnable cancel. We can't call WorkerControlRunnable::Cancel()
52 // in this case since that would result in both Run() and the inner
53 // Cancel() being called.
54 Unused << cr->Cancel();
55 return WorkerRunnable::Cancel();
59 } // anonymous namespace
61 NS_IMPL_ISUPPORTS(WorkerEventTarget, nsIEventTarget, nsISerialEventTarget)
63 WorkerEventTarget::WorkerEventTarget(WorkerPrivate* aWorkerPrivate,
64 Behavior aBehavior)
65 : mMutex("WorkerEventTarget"),
66 mWorkerPrivate(aWorkerPrivate),
67 mBehavior(aBehavior) {
68 MOZ_DIAGNOSTIC_ASSERT(mWorkerPrivate);
71 void WorkerEventTarget::ForgetWorkerPrivate(WorkerPrivate* aWorkerPrivate) {
72 MutexAutoLock lock(mMutex);
73 MOZ_DIAGNOSTIC_ASSERT(!mWorkerPrivate || mWorkerPrivate == aWorkerPrivate);
74 mWorkerPrivate = nullptr;
77 NS_IMETHODIMP
78 WorkerEventTarget::DispatchFromScript(nsIRunnable* aRunnable, uint32_t aFlags) {
79 nsCOMPtr<nsIRunnable> runnable(aRunnable);
80 return Dispatch(runnable.forget(), aFlags);
83 NS_IMETHODIMP
84 WorkerEventTarget::Dispatch(already_AddRefed<nsIRunnable> aRunnable,
85 uint32_t aFlags) {
86 nsCOMPtr<nsIRunnable> runnable(aRunnable);
88 MutexAutoLock lock(mMutex);
90 if (!mWorkerPrivate) {
91 return NS_ERROR_FAILURE;
94 if (mBehavior == Behavior::Hybrid) {
95 RefPtr<WorkerRunnable> r =
96 mWorkerPrivate->MaybeWrapAsWorkerRunnable(runnable.forget());
97 if (r->Dispatch()) {
98 return NS_OK;
101 runnable = r.forget();
104 RefPtr<WorkerControlRunnable> r =
105 new WrappedControlRunnable(mWorkerPrivate, runnable.forget());
106 if (!r->Dispatch()) {
107 return NS_ERROR_FAILURE;
110 return NS_OK;
113 NS_IMETHODIMP
114 WorkerEventTarget::DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t) {
115 return NS_ERROR_NOT_IMPLEMENTED;
118 NS_IMETHODIMP_(bool)
119 WorkerEventTarget::IsOnCurrentThreadInfallible() {
120 MutexAutoLock lock(mMutex);
122 if (!mWorkerPrivate) {
123 return false;
126 return mWorkerPrivate->IsOnCurrentThread();
129 NS_IMETHODIMP
130 WorkerEventTarget::IsOnCurrentThread(bool* aIsOnCurrentThread) {
131 MOZ_ASSERT(aIsOnCurrentThread);
132 *aIsOnCurrentThread = IsOnCurrentThreadInfallible();
133 return NS_OK;
136 } // namespace dom
137 } // namespace mozilla