Bug 1856663 - Add more chunks for Android mochitest-plain. r=jmaher,taskgraph-reviewe...
[gecko.git] / dom / bindings / SimpleGlobalObject.h
blob342869abaf2ecf00e79744098117364928d996ff
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /**
8 * A simplere nsIGlobalObject implementation that can be used to set up a new
9 * global without anything interesting in it other than the JS builtins. This
10 * is safe to use on both mainthread and worker threads.
13 #ifndef mozilla_dom_SimpleGlobalObject_h__
14 #define mozilla_dom_SimpleGlobalObject_h__
16 #include "nsContentUtils.h"
17 #include "nsIGlobalObject.h"
18 #include "nsWrapperCache.h"
19 #include "js/TypeDecls.h"
20 #include "js/Value.h"
21 #include "nsISupportsImpl.h"
22 #include "nsCycleCollectionParticipant.h"
24 namespace mozilla::dom {
26 class SimpleGlobalObject : public nsIGlobalObject, public nsWrapperCache {
27 public:
28 enum class GlobalType {
29 BindingDetail, // Should only be used by DOM bindings code.
30 WorkerDebuggerSandbox,
31 NotSimpleGlobal // Sentinel to be used by BasicGlobalType.
34 // Create a new JS global object that can be used to do some work. This
35 // global will NOT have any DOM APIs exposed in it, will not be visible to the
36 // debugger, and will not have a useful concept of principals, so don't try to
37 // use it with any DOM objects. Apart from that, running code with
38 // side-effects is safe in this global. Importantly, when you are first
39 // handed this global it's guaranteed to have pristine built-ins. The
40 // corresponding nsIGlobalObject* for this global object will be a
41 // SimpleGlobalObject of the type provided; JS::GetPrivate on the returned
42 // JSObject* will return the SimpleGlobalObject*.
44 // If the provided prototype value is undefined, it is ignored. If it's an
45 // object or null, it's set as the prototype of the created global. If it's
46 // anything else, this function returns null.
48 // Note that creating new globals is not cheap and should not be done
49 // gratuitously. Please think carefully before you use this function.
50 static JSObject* Create(GlobalType globalType, JS::Handle<JS::Value> proto =
51 JS::UndefinedHandleValue);
53 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
54 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(SimpleGlobalObject)
56 // Gets the GlobalType of this SimpleGlobalObject.
57 GlobalType Type() const { return mType; }
59 // Gets the GlobalType of the SimpleGlobalObject for the given JSObject*, if
60 // the given JSObject* is the global corresponding to a SimpleGlobalObject.
61 // Oherwise, returns GlobalType::NotSimpleGlobal.
62 static GlobalType SimpleGlobalType(JSObject* obj);
64 JSObject* GetGlobalJSObject() override { return GetWrapper(); }
65 JSObject* GetGlobalJSObjectPreserveColor() const override {
66 return GetWrapperPreserveColor();
69 OriginTrials Trials() const override { return {}; }
71 JSObject* WrapObject(JSContext* cx,
72 JS::Handle<JSObject*> aGivenProto) override {
73 MOZ_CRASH("SimpleGlobalObject doesn't use DOM bindings!");
76 bool ShouldResistFingerprinting(RFPTarget aTarget) const override {
77 return nsContentUtils::ShouldResistFingerprinting(
78 "Presently we don't have enough context to make an informed decision"
79 "on JS Sandboxes. See 1782853",
80 aTarget);
83 private:
84 SimpleGlobalObject(JSObject* global, GlobalType type) : mType(type) {
85 SetWrapper(global);
88 virtual ~SimpleGlobalObject() { MOZ_ASSERT(!GetWrapperMaybeDead()); }
90 const GlobalType mType;
93 } // namespace mozilla::dom
95 #endif /* mozilla_dom_SimpleGlobalObject_h__ */