2001-09-25 22:43:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2008-01-30 02:11:48 +00:00
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
2001-09-25 22:43:09 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-17 21:52:36 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-12-03 00:33:00 +00:00
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1998-12-03 00:33:00 +00:00
|
|
|
*
|
2001-09-25 22:43:09 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-12-03 00:33:00 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-25 22:43:09 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:40:37 +00:00
|
|
|
*
|
2001-09-25 22:43:09 +00:00
|
|
|
* Contributor(s):
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
* Vidur Apparao <vidur@netscape.com>
|
2005-12-03 07:42:40 +00:00
|
|
|
* L. David Baron <dbaron@mozillafoundation.org>
|
2001-09-25 22:43:09 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-17 21:52:36 +00:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-25 22:43:09 +00:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-17 21:52:36 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-25 22:43:09 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-17 21:52:36 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-25 22:43:09 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1998-12-03 00:33:00 +00:00
|
|
|
|
|
|
|
/**
|
2003-11-18 01:58:43 +00:00
|
|
|
* This is not a generated file. It contains common utility functions
|
1998-12-03 00:33:00 +00:00
|
|
|
* invoked from the JavaScript code generated from IDL interfaces.
|
|
|
|
* The goal of the utility functions is to cut down on the size of
|
|
|
|
* the generated code itself.
|
|
|
|
*/
|
|
|
|
|
1998-12-22 22:03:20 +00:00
|
|
|
#include "nsJSUtils.h"
|
1998-12-03 00:33:00 +00:00
|
|
|
#include "jsapi.h"
|
1999-09-08 23:14:30 +00:00
|
|
|
#include "jsdbgapi.h"
|
|
|
|
#include "prprf.h"
|
1998-12-03 00:33:00 +00:00
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIScriptObjectOwner.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
1999-04-24 02:37:41 +00:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIXPConnect.h"
|
1999-09-08 23:14:30 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2004-09-01 16:50:12 +00:00
|
|
|
#include "nsContentUtils.h"
|
2008-02-13 03:52:43 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
1998-12-03 00:33:00 +00:00
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
#include "nsDOMJSUtils.h" // for GetScriptContextFromJSContext
|
1999-09-08 23:14:30 +00:00
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
JSBool
|
|
|
|
nsJSUtils::GetCallingLocation(JSContext* aContext, const char* *aFilename,
|
2008-03-06 22:52:58 +00:00
|
|
|
PRUint32* aLineno, nsIPrincipal* aPrincipal)
|
1999-09-08 23:14:30 +00:00
|
|
|
{
|
|
|
|
// Get the current filename and line number
|
|
|
|
JSStackFrame* frame = nsnull;
|
|
|
|
JSScript* script = nsnull;
|
|
|
|
do {
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
frame = ::JS_FrameIterator(aContext, &frame);
|
|
|
|
|
1999-09-08 23:14:30 +00:00
|
|
|
if (frame) {
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
script = ::JS_GetFrameScript(aContext, frame);
|
1999-09-08 23:14:30 +00:00
|
|
|
}
|
2003-06-24 21:43:01 +00:00
|
|
|
} while (frame && !script);
|
1999-09-08 23:14:30 +00:00
|
|
|
|
|
|
|
if (script) {
|
2008-01-30 02:11:48 +00:00
|
|
|
// If aPrincipals is non-null then our caller is asking us to ensure
|
|
|
|
// that the filename we return does not have elevated privileges.
|
2008-03-06 22:52:58 +00:00
|
|
|
if (aPrincipal) {
|
|
|
|
uint32 flags = JS_GetScriptFilenameFlags(script);
|
|
|
|
|
|
|
|
// Use the principal for the filename if it shouldn't be receiving
|
|
|
|
// implicit XPCNativeWrappers.
|
|
|
|
PRBool system;
|
|
|
|
if (flags & JSFILENAME_PROTECTED) {
|
2008-02-13 03:52:43 +00:00
|
|
|
nsIScriptSecurityManager *ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
|
2008-03-06 22:52:58 +00:00
|
|
|
if (NS_FAILED(ssm->IsSystemPrincipal(aPrincipal, &system)) || !system) {
|
|
|
|
JSPrincipals* jsprins;
|
|
|
|
aPrincipal->GetJSPrincipals(aContext, &jsprins);
|
2008-02-13 03:52:43 +00:00
|
|
|
|
2008-03-06 22:52:58 +00:00
|
|
|
*aFilename = jsprins->codebase;
|
|
|
|
*aLineno = 0;
|
|
|
|
JSPRINCIPALS_DROP(aContext, jsprins);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2008-01-30 02:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
const char* filename = ::JS_GetScriptFilename(aContext, script);
|
|
|
|
|
1999-09-08 23:14:30 +00:00
|
|
|
if (filename) {
|
(13163, r=alecf, scc, waterson, others; names available on request)
- Fix most of bug 13163 (see TODO for rest). This entails adding a version-string argument to nsIScriptContext::EvaluateString and passing it around lots of places in content sinks.
- Fix leaks and confusion about mSecurityManager and mNameSpaceManager in nsJSEnvironment.cpp. These still need to move from nsJSContext to nsGlobalWindow or thereabouts, jband and vidur are looking at that.
- Added comments and expanded tabs in nsJSEnvironment.cpp, esp. to EvaluateString. Also changed various nsresult vars to be named rv. Also restored brace/style conformity to nsJSProtocolHandler.cpp.
- Factored CompileFunction from AddScriptEventListener to pave the way for brutal sharing of compiled JS event handlers via JS_CloneFunctionObject.
- Lots of nsCOMPtr uses added. I'm using one for mNameSpaceManager. Hold mSecurityManager as a service explicitly, on the other hand (awaiting scc's fix to allow comptrs for services), and release in nsJSContext's dtor (fixing a leak). These two managers should be moved to the window object -- TODO item below.
- Hold JSRuntimeService along with JSRuntime for live of nsJSEnvironment, fix for shaver.
- Fix window.setTimeout etc. so the filename and line number of the timeout expr is propagated. This meant factoring nsJSUtils.cpp code.
- Fix all content sinks to use the same, and up-to-date JavaScript version parsing (whether for script type or for old language attribute); also fix SplitMimeType clones to strip whitespace.
- With waterson, fix bug in brutal-sharing version of XUL content sink: script src= should not evaluate the inline content of its tag.
1999-10-31 00:43:30 +00:00
|
|
|
PRUint32 lineno = 0;
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
jsbytecode* bytecode = ::JS_GetFramePC(aContext, frame);
|
|
|
|
|
1999-09-08 23:14:30 +00:00
|
|
|
if (bytecode) {
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
lineno = ::JS_PCToLineNumber(aContext, script, bytecode);
|
1999-09-08 23:14:30 +00:00
|
|
|
}
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
|
(13163, r=alecf, scc, waterson, others; names available on request)
- Fix most of bug 13163 (see TODO for rest). This entails adding a version-string argument to nsIScriptContext::EvaluateString and passing it around lots of places in content sinks.
- Fix leaks and confusion about mSecurityManager and mNameSpaceManager in nsJSEnvironment.cpp. These still need to move from nsJSContext to nsGlobalWindow or thereabouts, jband and vidur are looking at that.
- Added comments and expanded tabs in nsJSEnvironment.cpp, esp. to EvaluateString. Also changed various nsresult vars to be named rv. Also restored brace/style conformity to nsJSProtocolHandler.cpp.
- Factored CompileFunction from AddScriptEventListener to pave the way for brutal sharing of compiled JS event handlers via JS_CloneFunctionObject.
- Lots of nsCOMPtr uses added. I'm using one for mNameSpaceManager. Hold mSecurityManager as a service explicitly, on the other hand (awaiting scc's fix to allow comptrs for services), and release in nsJSContext's dtor (fixing a leak). These two managers should be moved to the window object -- TODO item below.
- Hold JSRuntimeService along with JSRuntime for live of nsJSEnvironment, fix for shaver.
- Fix window.setTimeout etc. so the filename and line number of the timeout expr is propagated. This meant factoring nsJSUtils.cpp code.
- Fix all content sinks to use the same, and up-to-date JavaScript version parsing (whether for script type or for old language attribute); also fix SplitMimeType clones to strip whitespace.
- With waterson, fix bug in brutal-sharing version of XUL content sink: script src= should not evaluate the inline content of its tag.
1999-10-31 00:43:30 +00:00
|
|
|
*aFilename = filename;
|
|
|
|
*aLineno = lineno;
|
|
|
|
return JS_TRUE;
|
1999-09-08 23:14:30 +00:00
|
|
|
}
|
|
|
|
}
|
(13163, r=alecf, scc, waterson, others; names available on request)
- Fix most of bug 13163 (see TODO for rest). This entails adding a version-string argument to nsIScriptContext::EvaluateString and passing it around lots of places in content sinks.
- Fix leaks and confusion about mSecurityManager and mNameSpaceManager in nsJSEnvironment.cpp. These still need to move from nsJSContext to nsGlobalWindow or thereabouts, jband and vidur are looking at that.
- Added comments and expanded tabs in nsJSEnvironment.cpp, esp. to EvaluateString. Also changed various nsresult vars to be named rv. Also restored brace/style conformity to nsJSProtocolHandler.cpp.
- Factored CompileFunction from AddScriptEventListener to pave the way for brutal sharing of compiled JS event handlers via JS_CloneFunctionObject.
- Lots of nsCOMPtr uses added. I'm using one for mNameSpaceManager. Hold mSecurityManager as a service explicitly, on the other hand (awaiting scc's fix to allow comptrs for services), and release in nsJSContext's dtor (fixing a leak). These two managers should be moved to the window object -- TODO item below.
- Hold JSRuntimeService along with JSRuntime for live of nsJSEnvironment, fix for shaver.
- Fix window.setTimeout etc. so the filename and line number of the timeout expr is propagated. This meant factoring nsJSUtils.cpp code.
- Fix all content sinks to use the same, and up-to-date JavaScript version parsing (whether for script type or for old language attribute); also fix SplitMimeType clones to strip whitespace.
- With waterson, fix bug in brutal-sharing version of XUL content sink: script src= should not evaluate the inline content of its tag.
1999-10-31 00:43:30 +00:00
|
|
|
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2004-02-09 22:48:53 +00:00
|
|
|
nsIScriptGlobalObject *
|
|
|
|
nsJSUtils::GetStaticScriptGlobal(JSContext* aContext, JSObject* aObj)
|
1999-12-18 20:29:29 +00:00
|
|
|
{
|
|
|
|
nsISupports* supports;
|
|
|
|
JSClass* clazz;
|
|
|
|
JSObject* parent;
|
|
|
|
JSObject* glob = aObj; // starting point for search
|
|
|
|
|
|
|
|
if (!glob)
|
2004-02-09 22:48:53 +00:00
|
|
|
return nsnull;
|
1999-12-18 20:29:29 +00:00
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
while ((parent = ::JS_GetParent(aContext, glob)))
|
1999-12-18 20:29:29 +00:00
|
|
|
glob = parent;
|
|
|
|
|
2001-11-27 09:46:38 +00:00
|
|
|
clazz = JS_GET_CLASS(aContext, glob);
|
1999-12-18 20:29:29 +00:00
|
|
|
|
|
|
|
if (!clazz ||
|
|
|
|
!(clazz->flags & JSCLASS_HAS_PRIVATE) ||
|
|
|
|
!(clazz->flags & JSCLASS_PRIVATE_IS_NSISUPPORTS) ||
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
!(supports = (nsISupports*)::JS_GetPrivate(aContext, glob))) {
|
2004-02-09 22:48:53 +00:00
|
|
|
return nsnull;
|
1999-12-18 20:29:29 +00:00
|
|
|
}
|
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrapper(do_QueryInterface(supports));
|
2004-02-09 22:48:53 +00:00
|
|
|
NS_ENSURE_TRUE(wrapper, nsnull);
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
|
2005-03-10 00:39:28 +00:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryWrappedNative(wrapper));
|
2004-02-09 22:48:53 +00:00
|
|
|
|
|
|
|
// We're returning a pointer to something that's about to be
|
|
|
|
// released, but that's ok here.
|
|
|
|
return sgo;
|
1999-12-18 20:29:29 +00:00
|
|
|
}
|
|
|
|
|
2004-02-09 22:48:53 +00:00
|
|
|
nsIScriptContext *
|
|
|
|
nsJSUtils::GetStaticScriptContext(JSContext* aContext, JSObject* aObj)
|
1999-12-18 20:29:29 +00:00
|
|
|
{
|
2004-02-09 22:48:53 +00:00
|
|
|
nsIScriptGlobalObject *nativeGlobal = GetStaticScriptGlobal(aContext, aObj);
|
2003-11-18 01:58:43 +00:00
|
|
|
if (!nativeGlobal)
|
2004-02-09 22:48:53 +00:00
|
|
|
return nsnull;
|
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
return nativeGlobal->GetScriptContext(nsIProgrammingLanguage::JAVASCRIPT);
|
2003-11-18 01:58:43 +00:00
|
|
|
}
|
1999-12-18 20:29:29 +00:00
|
|
|
|
2004-02-09 22:48:53 +00:00
|
|
|
nsIScriptGlobalObject *
|
|
|
|
nsJSUtils::GetDynamicScriptGlobal(JSContext* aContext)
|
1999-12-18 20:29:29 +00:00
|
|
|
{
|
2004-02-09 22:48:53 +00:00
|
|
|
nsIScriptContext *scriptCX = GetDynamicScriptContext(aContext);
|
2001-06-20 03:27:48 +00:00
|
|
|
if (!scriptCX)
|
2004-02-09 22:48:53 +00:00
|
|
|
return nsnull;
|
|
|
|
return scriptCX->GetGlobalObject();
|
2003-11-18 01:58:43 +00:00
|
|
|
}
|
1999-12-18 20:29:29 +00:00
|
|
|
|
2004-02-09 22:48:53 +00:00
|
|
|
nsIScriptContext *
|
|
|
|
nsJSUtils::GetDynamicScriptContext(JSContext *aContext)
|
1999-12-18 20:29:29 +00:00
|
|
|
{
|
2004-02-09 22:48:53 +00:00
|
|
|
return GetScriptContextFromJSContext(aContext);
|
1999-12-18 20:29:29 +00:00
|
|
|
}
|