/dom/base/nsIScriptGlobalObject.h

http://github.com/zpao/v8monkey · C Header · 172 lines · 52 code · 27 blank · 93 comment · 3 complexity · 8e32137d1cbca9021275b1c7766257c0 MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=2 sw=2 et tw=80: */
  3. /* ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is mozilla.org code.
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Netscape Communications Corporation.
  20. * Portions created by the Initial Developer are Copyright (C) 1998
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either of the GNU General Public License Version 2 or later (the "GPL"),
  27. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. #ifndef nsIScriptGlobalObject_h__
  39. #define nsIScriptGlobalObject_h__
  40. #include "nsISupports.h"
  41. #include "nsEvent.h"
  42. #include "nsIProgrammingLanguage.h"
  43. class nsIScriptContext;
  44. class nsIDOMDocument;
  45. class nsIDOMEvent;
  46. class nsIScriptGlobalObjectOwner;
  47. class nsIArray;
  48. class nsScriptErrorEvent;
  49. class nsIScriptGlobalObject;
  50. struct JSObject; // until we finally remove GetGlobalJSObject...
  51. // Some helpers for working with integer "script type IDs", and specifically
  52. // for working with arrays of such objects. For example, it is common for
  53. // implementations supporting multiple script languages to keep each
  54. // language's nsIScriptContext in an array indexed by the language ID.
  55. // Implementation note: We always ignore nsIProgrammingLanguage::UNKNOWN and
  56. // nsIProgrammingLanguage::CPLUSPLUS - this gives javascript slot 0. An
  57. // attempted micro-optimization tried to avoid us going all the way to
  58. // nsIProgrammingLanguage::MAX; however:
  59. // * Someone is reportedly working on a PHP impl - that has value 9
  60. // * nsGenericElement therefore allows 4 bits for the value.
  61. // So there is no good reason for us to be more restrictive again...
  62. #define NS_STID_FIRST nsIProgrammingLanguage::JAVASCRIPT
  63. // like nsGenericElement, only 4 bits worth is valid...
  64. #define NS_STID_LAST (nsIProgrammingLanguage::MAX > 0x000FU ? \
  65. 0x000FU : nsIProgrammingLanguage::MAX)
  66. // Use to declare the array size
  67. #define NS_STID_ARRAY_UBOUND (NS_STID_LAST-NS_STID_FIRST+1)
  68. // Is a language ID valid?
  69. #define NS_STID_VALID(langID) (langID >= NS_STID_FIRST && langID <= NS_STID_LAST)
  70. // Return an index for a given ID.
  71. #define NS_STID_INDEX(langID) (langID-NS_STID_FIRST)
  72. // Create a 'for' loop iterating over all possible language IDs (*not* indexes)
  73. #define NS_STID_FOR_ID(varName) \
  74. for (varName=NS_STID_FIRST;varName<=NS_STID_LAST;varName++)
  75. // Create a 'for' loop iterating over all indexes (when you don't need to know
  76. // what language it is)
  77. #define NS_STID_FOR_INDEX(varName) \
  78. for (varName=0;varName<=NS_STID_INDEX(NS_STID_LAST);varName++)
  79. // A helper function for nsIScriptGlobalObject implementations to use
  80. // when handling a script error. Generally called by the global when a context
  81. // notifies it of an error via nsIScriptGlobalObject::HandleScriptError.
  82. // Returns true if HandleDOMEvent was actually called, in which case
  83. // aStatus will be filled in with the status.
  84. bool
  85. NS_HandleScriptError(nsIScriptGlobalObject *aScriptGlobal,
  86. nsScriptErrorEvent *aErrorEvent,
  87. nsEventStatus *aStatus);
  88. #define NS_ISCRIPTGLOBALOBJECT_IID \
  89. { 0x8f19a761, 0x0717, 0x4b3f, \
  90. { 0x80, 0xc5, 0xed, 0x7e, 0x9c, 0xbc, 0x40, 0xb1 } }
  91. /**
  92. * The global object which keeps a script context for each supported script
  93. * language. This often used to store per-window global state.
  94. */
  95. class nsIScriptGlobalObject : public nsISupports
  96. {
  97. public:
  98. NS_DECLARE_STATIC_IID_ACCESSOR(NS_ISCRIPTGLOBALOBJECT_IID)
  99. /**
  100. * Ensure that the script global object is initialized for working with the
  101. * specified script language ID. This will set up the nsIScriptContext
  102. * and 'script global' for that language, allowing these to be fetched
  103. * and manipulated.
  104. * @return NS_OK if successful; error conditions include that the language
  105. * has not been registered, as well as 'normal' errors, such as
  106. * out-of-memory
  107. */
  108. virtual nsresult EnsureScriptEnvironment(PRUint32 aLangID) = 0;
  109. /**
  110. * Get a script context (WITHOUT added reference) for the specified language.
  111. */
  112. virtual nsIScriptContext *GetScriptContext(PRUint32 lang) = 0;
  113. virtual JSObject* GetGlobalJSObject() = 0;
  114. virtual nsIScriptContext *GetContext() {
  115. return GetScriptContext(nsIProgrammingLanguage::JAVASCRIPT);
  116. }
  117. /**
  118. * Set a new language context for this global. The native global for the
  119. * context is created by the context's GetNativeGlobal() method.
  120. */
  121. virtual nsresult SetScriptContext(PRUint32 lang, nsIScriptContext *aContext) = 0;
  122. /**
  123. * Called when the global script for a language is finalized, typically as
  124. * part of its GC process. By the time this call is made, the
  125. * nsIScriptContext for the language has probably already been removed.
  126. * After this call, the passed object is dead - which should generally be the
  127. * same object the global is using for a global for that language.
  128. */
  129. virtual void OnFinalize(JSObject* aObject) = 0;
  130. /**
  131. * Called to enable/disable scripts.
  132. */
  133. virtual void SetScriptsEnabled(bool aEnabled, bool aFireTimeouts) = 0;
  134. /**
  135. * Handle a script error. Generally called by a script context.
  136. */
  137. virtual nsresult HandleScriptError(nsScriptErrorEvent *aErrorEvent,
  138. nsEventStatus *aEventStatus) {
  139. return NS_HandleScriptError(this, aErrorEvent, aEventStatus);
  140. }
  141. virtual bool IsBlackForCC() { return false; }
  142. };
  143. NS_DEFINE_STATIC_IID_ACCESSOR(nsIScriptGlobalObject,
  144. NS_ISCRIPTGLOBALOBJECT_IID)
  145. #endif