SimpleGlobalObject.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. * A simplere nsIGlobalObject implementation that can be used to set up a new
  7. * global without anything interesting in it other than the JS builtins. This
  8. * is safe to use on both mainthread and worker threads.
  9. */
  10. #ifndef mozilla_dom_SimpleGlobalObject_h__
  11. #define mozilla_dom_SimpleGlobalObject_h__
  12. #include "nsIGlobalObject.h"
  13. #include "nsWrapperCache.h"
  14. #include "js/TypeDecls.h"
  15. #include "nsISupportsImpl.h"
  16. #include "nsCycleCollectionParticipant.h"
  17. namespace mozilla {
  18. namespace dom {
  19. class SimpleGlobalObject : public nsIGlobalObject,
  20. public nsWrapperCache
  21. {
  22. public:
  23. enum class GlobalType {
  24. BindingDetail, // Should only be used by DOM bindings code.
  25. WorkerDebuggerSandbox,
  26. NotSimpleGlobal // Sentinel to be used by BasicGlobalType.
  27. };
  28. // Create a new JS global object that can be used to do some work. This
  29. // global will NOT have any DOM APIs exposed in it, will not be visible to the
  30. // debugger, and will not have a useful concept of principals, so don't try to
  31. // use it with any DOM objects. Apart from that, running code with
  32. // side-effects is safe in this global. Importantly, when you are first
  33. // handed this global it's guaranteed to have pristine built-ins. The
  34. // corresponding nsIGlobalObject* for this global object will be a
  35. // SimpleGlobalObject of the type provided; JS_GetPrivate on the returned
  36. // JSObject* will return the SimpleGlobalObject*.
  37. //
  38. // If the provided prototype value is undefined, it is ignored. If it's an
  39. // object or null, it's set as the prototype of the created global. If it's
  40. // anything else, this function returns null.
  41. //
  42. // Note that creating new globals is not cheap and should not be done
  43. // gratuitously. Please think carefully before you use this function.
  44. static JSObject* Create(GlobalType globalType,
  45. JS::Handle<JS::Value> proto =
  46. JS::UndefinedHandleValue);
  47. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  48. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(SimpleGlobalObject,
  49. nsIGlobalObject)
  50. // Gets the GlobalType of this SimpleGlobalObject.
  51. GlobalType Type() const
  52. {
  53. return mType;
  54. }
  55. // Gets the GlobalType of the SimpleGlobalObject for the given JSObject*, if
  56. // the given JSObject* is the global corresponding to a SimpleGlobalObject.
  57. // Oherwise, returns GlobalType::NotSimpleGlobal.
  58. static GlobalType SimpleGlobalType(JSObject* obj);
  59. virtual JSObject *GetGlobalJSObject() override
  60. {
  61. return GetWrapper();
  62. }
  63. virtual JSObject* WrapObject(JSContext* cx,
  64. JS::Handle<JSObject*> aGivenProto) override
  65. {
  66. MOZ_CRASH("SimpleGlobalObject doesn't use DOM bindings!");
  67. }
  68. private:
  69. SimpleGlobalObject(JSObject *global, GlobalType type)
  70. : mType(type)
  71. {
  72. SetWrapper(global);
  73. }
  74. virtual ~SimpleGlobalObject()
  75. {
  76. ClearWrapper();
  77. }
  78. const GlobalType mType;
  79. };
  80. } // namespace dom
  81. } // namespace mozilla
  82. #endif /* mozilla_dom_SimpleGlobalObject_h__ */