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/. */
6 
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.
11  */
12 
13 #ifndef mozilla_dom_SimpleGlobalObject_h__
14 #define mozilla_dom_SimpleGlobalObject_h__
15 
16 #include "nsIGlobalObject.h"
17 #include "nsWrapperCache.h"
18 #include "js/TypeDecls.h"
19 #include "nsISupportsImpl.h"
20 #include "nsCycleCollectionParticipant.h"
21 
22 namespace mozilla {
23 namespace dom {
24 
25 class SimpleGlobalObject : public nsIGlobalObject,
26                            public nsWrapperCache
27 {
28 public:
29   enum class GlobalType {
30     BindingDetail, // Should only be used by DOM bindings code.
31     WorkerDebuggerSandbox,
32     NotSimpleGlobal // Sentinel to be used by BasicGlobalType.
33   };
34 
35   // Create a new JS global object that can be used to do some work.  This
36   // global will NOT have any DOM APIs exposed in it, will not be visible to the
37   // debugger, and will not have a useful concept of principals, so don't try to
38   // use it with any DOM objects.  Apart from that, running code with
39   // side-effects is safe in this global.  Importantly, when you are first
40   // handed this global it's guaranteed to have pristine built-ins.  The
41   // corresponding nsIGlobalObject* for this global object will be a
42   // SimpleGlobalObject of the type provided; JS_GetPrivate on the returned
43   // JSObject* will return the SimpleGlobalObject*.
44   //
45   // If the provided prototype value is undefined, it is ignored.  If it's an
46   // object or null, it's set as the prototype of the created global.  If it's
47   // anything else, this function returns null.
48   //
49   // Note that creating new globals is not cheap and should not be done
50   // gratuitously.  Please think carefully before you use this function.
51   static JSObject* Create(GlobalType globalType,
52                           JS::Handle<JS::Value> proto =
53                             JS::UndefinedHandleValue);
54 
55   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(SimpleGlobalObject,nsIGlobalObject)56   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(SimpleGlobalObject,
57                                                          nsIGlobalObject)
58 
59   // Gets the GlobalType of this SimpleGlobalObject.
60   GlobalType Type() const
61   {
62     return mType;
63   }
64 
65   // Gets the GlobalType of the SimpleGlobalObject for the given JSObject*, if
66   // the given JSObject* is the global corresponding to a SimpleGlobalObject.
67   // Oherwise, returns GlobalType::NotSimpleGlobal.
68   static GlobalType SimpleGlobalType(JSObject* obj);
69 
GetGlobalJSObject()70   virtual JSObject *GetGlobalJSObject() override
71   {
72     return GetWrapper();
73   }
74 
WrapObject(JSContext * cx,JS::Handle<JSObject * > aGivenProto)75   virtual JSObject* WrapObject(JSContext* cx,
76                                JS::Handle<JSObject*> aGivenProto) override
77   {
78     MOZ_CRASH("SimpleGlobalObject doesn't use DOM bindings!");
79   }
80 
81 private:
SimpleGlobalObject(JSObject * global,GlobalType type)82   SimpleGlobalObject(JSObject *global, GlobalType type)
83     : mType(type)
84   {
85     SetWrapper(global);
86   }
87 
~SimpleGlobalObject()88   virtual ~SimpleGlobalObject()
89   {
90     ClearWrapper();
91   }
92 
93   const GlobalType mType;
94 };
95 
96 } // namespace dom
97 } // namespace mozilla
98 
99 #endif /* mozilla_dom_SimpleGlobalObject_h__ */
100