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 "ChromeWorker.h"
9 #include "mozilla/dom/BindingUtils.h"
10 #include "mozilla/dom/WorkerBinding.h"
11 #include "nsContentUtils.h"
12 #include "WorkerPrivate.h"
14 namespace mozilla::dom
{
17 already_AddRefed
<ChromeWorker
> ChromeWorker::Constructor(
18 const GlobalObject
& aGlobal
, const nsAString
& aScriptURL
,
20 JSContext
* cx
= aGlobal
.Context();
22 RefPtr
<WorkerPrivate
> workerPrivate
= WorkerPrivate::Constructor(
23 cx
, aScriptURL
, true /* aIsChromeWorker */, WorkerKindDedicated
, u
""_ns
,
24 VoidCString(), nullptr /*aLoadInfo */, aRv
);
25 if (NS_WARN_IF(aRv
.Failed())) {
29 nsCOMPtr
<nsIGlobalObject
> globalObject
=
30 do_QueryInterface(aGlobal
.GetAsSupports());
32 RefPtr
<ChromeWorker
> worker
=
33 new ChromeWorker(globalObject
, workerPrivate
.forget());
34 return worker
.forget();
38 bool ChromeWorker::WorkerAvailable(JSContext
* aCx
, JSObject
* /* unused */) {
39 // Chrome is always allowed to use workers, and content is never
40 // allowed to use ChromeWorker, so all we have to check is the
41 // caller. However, chrome workers apparently might not have a
42 // system principal, so we have to check for them manually.
43 if (NS_IsMainThread()) {
44 return nsContentUtils::IsSystemCaller(aCx
);
47 return GetWorkerPrivateFromContext(aCx
)->IsChromeWorker();
50 ChromeWorker::ChromeWorker(nsIGlobalObject
* aGlobalObject
,
51 already_AddRefed
<WorkerPrivate
> aWorkerPrivate
)
52 : Worker(aGlobalObject
, std::move(aWorkerPrivate
)) {}
54 ChromeWorker::~ChromeWorker() = default;
56 JSObject
* ChromeWorker::WrapObject(JSContext
* aCx
,
57 JS::Handle
<JSObject
*> aGivenProto
) {
58 JS::Rooted
<JSObject
*> wrapper(
59 aCx
, ChromeWorker_Binding::Wrap(aCx
, this, aGivenProto
));
61 // Most DOM objects don't assume they have a reflector. If they don't have
62 // one and need one, they create it. But in workers code, we assume that the
63 // reflector is always present. In order to guarantee that it's always
64 // present, we have to preserve it. Otherwise the GC will happily collect it
66 MOZ_ALWAYS_TRUE(TryPreserveWrapper(wrapper
));
72 } // namespace mozilla::dom