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 "js/Value.h" 20 #include "nsISupportsImpl.h" 21 #include "nsCycleCollectionParticipant.h" 22 23 namespace mozilla { 24 namespace dom { 25 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. 32 }; 33 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*. 43 // 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. 47 // 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); 52 53 NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(SimpleGlobalObject,nsIGlobalObject)54 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(SimpleGlobalObject, 55 nsIGlobalObject) 56 57 // Gets the GlobalType of this SimpleGlobalObject. 58 GlobalType Type() const { return mType; } 59 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); 64 GetGlobalJSObject()65 JSObject* GetGlobalJSObject() override { return GetWrapper(); } GetGlobalJSObjectPreserveColor()66 JSObject* GetGlobalJSObjectPreserveColor() const override { 67 return GetWrapperPreserveColor(); 68 } 69 WrapObject(JSContext * cx,JS::Handle<JSObject * > aGivenProto)70 virtual JSObject* WrapObject(JSContext* cx, 71 JS::Handle<JSObject*> aGivenProto) override { 72 MOZ_CRASH("SimpleGlobalObject doesn't use DOM bindings!"); 73 } 74 75 private: SimpleGlobalObject(JSObject * global,GlobalType type)76 SimpleGlobalObject(JSObject* global, GlobalType type) : mType(type) { 77 SetWrapper(global); 78 } 79 ~SimpleGlobalObject()80 virtual ~SimpleGlobalObject() { MOZ_ASSERT(!GetWrapperMaybeDead()); } 81 82 const GlobalType mType; 83 }; 84 85 } // namespace dom 86 } // namespace mozilla 87 88 #endif /* mozilla_dom_SimpleGlobalObject_h__ */ 89