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
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/
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
16 * The Original Code is mozilla.org code.
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.
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.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsIScriptGlobalObject_h__
40 #define nsIScriptGlobalObject_h__
42 #include "nsISupports.h"
44 #include "nsIProgrammingLanguage.h"
46 class nsIScriptContext
;
50 class nsIScriptGlobalObjectOwner
;
52 class nsScriptErrorEvent
;
53 class nsIScriptGlobalObject
;
54 struct JSObject
; // until we finally remove GetGlobalJSObject...
56 // Some helpers for working with integer "script type IDs", and specifically
57 // for working with arrays of such objects. For example, it is common for
58 // implementations supporting multiple script languages to keep each
59 // language's nsIScriptContext in an array indexed by the language ID.
61 // Implementation note: We always ignore nsIProgrammingLanguage::UNKNOWN and
62 // nsIProgrammingLanguage::CPLUSPLUS - this gives javascript slot 0. An
63 // attempted micro-optimization tried to avoid us going all the way to
64 // nsIProgrammingLanguage::MAX; however:
65 // * Someone is reportedly working on a PHP impl - that has value 9
66 // * nsGenericElement therefore allows 4 bits for the value.
67 // So there is no good reason for us to be more restrictive again...
69 #define NS_STID_FIRST nsIProgrammingLanguage::JAVASCRIPT
70 // like nsGenericElement, only 4 bits worth is valid...
71 #define NS_STID_LAST (nsIProgrammingLanguage::MAX > 0x000FU ? \
72 0x000FU : nsIProgrammingLanguage::MAX)
74 // Use to declare the array size
75 #define NS_STID_ARRAY_UBOUND (NS_STID_LAST-NS_STID_FIRST+1)
77 // Is a language ID valid?
78 #define NS_STID_VALID(langID) (langID >= NS_STID_FIRST && langID <= NS_STID_LAST)
80 // Return an index for a given ID.
81 #define NS_STID_INDEX(langID) (langID-NS_STID_FIRST)
83 // Create a 'for' loop iterating over all possible language IDs (*not* indexes)
84 #define NS_STID_FOR_ID(varName) \
85 for (varName=NS_STID_FIRST;varName<=NS_STID_LAST;varName++)
87 // Create a 'for' loop iterating over all indexes (when you don't need to know
88 // what language it is)
89 #define NS_STID_FOR_INDEX(varName) \
90 for (varName=0;varName<=NS_STID_INDEX(NS_STID_LAST);varName++)
92 // A helper function for nsIScriptGlobalObject implementations to use
93 // when handling a script error. Generally called by the global when a context
94 // notifies it of an error via nsIScriptGlobalObject::HandleScriptError.
95 // Returns PR_TRUE if HandleDOMEvent was actually called, in which case
96 // aStatus will be filled in with the status.
98 NS_HandleScriptError(nsIScriptGlobalObject
*aScriptGlobal
,
99 nsScriptErrorEvent
*aErrorEvent
,
100 nsEventStatus
*aStatus
);
103 #define NS_ISCRIPTGLOBALOBJECT_IID \
104 { /* {6afecd40-0b9a-4cfd-8c42-0f645cd91829} */ \
105 0x6afecd40, 0x0b9a, 0x4cfd, \
106 { 0x8c, 0x42, 0x0f, 0x64, 0x5c, 0xd9, 0x18, 0x29 } }
109 + * The global object which keeps a script context for each supported script
110 + * language. This often used to store per-window global state.
113 class nsIScriptGlobalObject
: public nsISupports
116 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ISCRIPTGLOBALOBJECT_IID
)
119 * Ensure that the script global object is initialized for working with the
120 * specified script language ID. This will set up the nsIScriptContext
121 * and 'script global' for that language, allowing these to be fetched
123 * @return NS_OK if successful; error conditions include that the language
124 * has not been registered, as well as 'normal' errors, such as
127 virtual nsresult
EnsureScriptEnvironment(PRUint32 aLangID
) = 0;
129 * Get a script context (WITHOUT added reference) for the specified language.
131 virtual nsIScriptContext
*GetScriptContext(PRUint32 lang
) = 0;
134 * Get the opaque "global" object for the specified lang.
136 virtual void *GetScriptGlobal(PRUint32 lang
) = 0;
138 // Set/GetContext deprecated methods - use GetScriptContext/Global
139 virtual JSObject
*GetGlobalJSObject() {
140 return (JSObject
*)GetScriptGlobal(nsIProgrammingLanguage::JAVASCRIPT
);
143 virtual nsIScriptContext
*GetContext() {
144 return GetScriptContext(nsIProgrammingLanguage::JAVASCRIPT
);
148 * Set a new language context for this global. The native global for the
149 * context is created by the context's GetNativeGlobal() method.
152 virtual nsresult
SetScriptContext(PRUint32 lang
, nsIScriptContext
*aContext
) = 0;
155 * Called when the global script for a language is finalized, typically as
156 * part of its GC process. By the time this call is made, the
157 * nsIScriptContext for the language has probably already been removed.
158 * After this call, the passed object is dead - which should generally be the
159 * same object the global is using for a global for that language.
162 virtual void OnFinalize(PRUint32 aLangID
, void *aScriptGlobal
) = 0;
165 * Called to enable/disable scripts.
167 virtual void SetScriptsEnabled(PRBool aEnabled
, PRBool aFireTimeouts
) = 0;
169 /** Set a new arguments object for this window. This will be set on
170 * the window right away (if there's an existing document) and it
171 * will also be installed on the window when the next document is
172 * loaded. Each language impl is responsible for converting to
173 * an array of args as appropriate for that language.
175 virtual nsresult
SetNewArguments(nsIArray
*aArguments
) = 0;
177 /** Handle a script error. Generally called by a script context.
179 virtual nsresult
HandleScriptError(nsScriptErrorEvent
*aErrorEvent
,
180 nsEventStatus
*aEventStatus
) {
181 return NS_HandleScriptError(this, aErrorEvent
, aEventStatus
);
185 NS_DEFINE_STATIC_IID_ACCESSOR(nsIScriptGlobalObject
,
186 NS_ISCRIPTGLOBALOBJECT_IID
)