1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=78: */
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.
24 * Vidur Apparao <vidur@netscape.com>
25 * L. David Baron <dbaron@mozillafoundation.org>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 * This is not a generated file. It contains common utility functions
43 * invoked from the JavaScript code generated from IDL interfaces.
44 * The goal of the utility functions is to cut down on the size of
45 * the generated code itself.
48 #include "nsJSUtils.h"
52 #include "nsIScriptContext.h"
53 #include "nsIScriptObjectOwner.h"
54 #include "nsIScriptGlobalObject.h"
55 #include "nsIServiceManager.h"
56 #include "nsIXPConnect.h"
58 #include "nsContentUtils.h"
59 #include "nsIScriptSecurityManager.h"
61 #include "nsDOMJSUtils.h" // for GetScriptContextFromJSContext
64 nsJSUtils::GetCallingLocation(JSContext
* aContext
, const char* *aFilename
,
65 PRUint32
* aLineno
, nsIPrincipal
* aPrincipal
)
67 // Get the current filename and line number
68 JSStackFrame
* frame
= nsnull
;
69 JSScript
* script
= nsnull
;
71 frame
= ::JS_FrameIterator(aContext
, &frame
);
74 script
= ::JS_GetFrameScript(aContext
, frame
);
76 } while (frame
&& !script
);
79 // If aPrincipals is non-null then our caller is asking us to ensure
80 // that the filename we return does not have elevated privileges.
82 uint32 flags
= JS_GetScriptFilenameFlags(script
);
84 // Use the principal for the filename if it shouldn't be receiving
85 // implicit XPCNativeWrappers.
87 if (flags
& JSFILENAME_PROTECTED
) {
88 nsIScriptSecurityManager
*ssm
= nsContentUtils::GetSecurityManager();
90 if (NS_FAILED(ssm
->IsSystemPrincipal(aPrincipal
, &system
)) || !system
) {
91 JSPrincipals
* jsprins
;
92 aPrincipal
->GetJSPrincipals(aContext
, &jsprins
);
94 *aFilename
= jsprins
->codebase
;
96 JSPRINCIPALS_DROP(aContext
, jsprins
);
102 const char* filename
= ::JS_GetScriptFilename(aContext
, script
);
106 jsbytecode
* bytecode
= ::JS_GetFramePC(aContext
, frame
);
109 lineno
= ::JS_PCToLineNumber(aContext
, script
, bytecode
);
112 *aFilename
= filename
;
121 nsIScriptGlobalObject
*
122 nsJSUtils::GetStaticScriptGlobal(JSContext
* aContext
, JSObject
* aObj
)
124 nsISupports
* supports
;
127 JSObject
* glob
= aObj
; // starting point for search
132 while ((parent
= ::JS_GetParent(aContext
, glob
)))
135 clazz
= JS_GET_CLASS(aContext
, glob
);
138 !(clazz
->flags
& JSCLASS_HAS_PRIVATE
) ||
139 !(clazz
->flags
& JSCLASS_PRIVATE_IS_NSISUPPORTS
) ||
140 !(supports
= (nsISupports
*)::JS_GetPrivate(aContext
, glob
))) {
144 nsCOMPtr
<nsIXPConnectWrappedNative
> wrapper(do_QueryInterface(supports
));
145 NS_ENSURE_TRUE(wrapper
, nsnull
);
147 nsCOMPtr
<nsIScriptGlobalObject
> sgo(do_QueryWrappedNative(wrapper
));
149 // We're returning a pointer to something that's about to be
150 // released, but that's ok here.
155 nsJSUtils::GetStaticScriptContext(JSContext
* aContext
, JSObject
* aObj
)
157 nsIScriptGlobalObject
*nativeGlobal
= GetStaticScriptGlobal(aContext
, aObj
);
161 return nativeGlobal
->GetScriptContext(nsIProgrammingLanguage::JAVASCRIPT
);
164 nsIScriptGlobalObject
*
165 nsJSUtils::GetDynamicScriptGlobal(JSContext
* aContext
)
167 nsIScriptContext
*scriptCX
= GetDynamicScriptContext(aContext
);
170 return scriptCX
->GetGlobalObject();
174 nsJSUtils::GetDynamicScriptContext(JSContext
*aContext
)
176 return GetScriptContextFromJSContext(aContext
);