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/. */
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 "nsIGlobalObject.h"
17 #include "nsWrapperCache.h"
18 #include "js/TypeDecls.h"
20 #include "nsISupportsImpl.h"
21 #include "nsCycleCollectionParticipant.h"
26 class SimpleGlobalObject
: public nsIGlobalObject
, public nsWrapperCache
{
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_SCRIPT_HOLDER_CLASS_AMBIGUOUS(SimpleGlobalObject
,
57 // Gets the GlobalType of this SimpleGlobalObject.
58 GlobalType
Type() const { return mType
; }
60 // Gets the GlobalType of the SimpleGlobalObject for the given JSObject*, if
61 // the given JSObject* is the global corresponding to a SimpleGlobalObject.
62 // Oherwise, returns GlobalType::NotSimpleGlobal.
63 static GlobalType
SimpleGlobalType(JSObject
* obj
);
65 JSObject
* GetGlobalJSObject() override
{ return GetWrapper(); }
66 JSObject
* GetGlobalJSObjectPreserveColor() const override
{
67 return GetWrapperPreserveColor();
70 virtual JSObject
* WrapObject(JSContext
* cx
,
71 JS::Handle
<JSObject
*> aGivenProto
) override
{
72 MOZ_CRASH("SimpleGlobalObject doesn't use DOM bindings!");
76 SimpleGlobalObject(JSObject
* global
, GlobalType type
) : mType(type
) {
80 virtual ~SimpleGlobalObject() { MOZ_ASSERT(!GetWrapperMaybeDead()); }
82 const GlobalType mType
;
86 } // namespace mozilla
88 #endif /* mozilla_dom_SimpleGlobalObject_h__ */