2001-09-26 02:43:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2008-01-30 05:11:48 +03:00
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
2001-09-26 02:43:09 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 01:52:36 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-12-03 03:33:00 +03:00
|
|
|
*
|
2004-04-18 01:52:36 +04: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 03:33:00 +03:00
|
|
|
*
|
2001-09-26 02:43:09 +04: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 03:33:00 +03:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 01:52:36 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-26 02:43:09 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:40:37 +03:00
|
|
|
*
|
2001-09-26 02:43:09 +04: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 21:42:36 +04:00
|
|
|
* Vidur Apparao <vidur@netscape.com>
|
2005-12-03 10:42:40 +03:00
|
|
|
* L. David Baron <dbaron@mozillafoundation.org>
|
2001-09-26 02:43:09 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 01:52:36 +04: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-26 02:43:09 +04: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-18 01:52:36 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-26 02:43:09 +04: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-18 01:52:36 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-26 02:43:09 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1998-12-03 03:33:00 +03:00
|
|
|
|
|
|
|
/**
|
2003-11-18 04:58:43 +03:00
|
|
|
* This is not a generated file. It contains common utility functions
|
1998-12-03 03:33:00 +03: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-23 01:03:20 +03:00
|
|
|
#include "nsJSUtils.h"
|
1998-12-03 03:33:00 +03:00
|
|
|
#include "jsapi.h"
|
1999-09-09 03:14:30 +04:00
|
|
|
#include "jsdbgapi.h"
|
|
|
|
#include "prprf.h"
|
1998-12-03 03:33:00 +03:00
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIScriptObjectOwner.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
1999-04-24 06:37:41 +04:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIXPConnect.h"
|
1999-09-09 03:14:30 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2004-09-01 20:50:12 +04:00
|
|
|
#include "nsContentUtils.h"
|
2008-02-13 06:52:43 +03:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
1998-12-03 03:33:00 +03:00
|
|
|
|
2006-06-13 07:07:47 +04:00
|
|
|
#include "nsDOMJSUtils.h" // for GetScriptContextFromJSContext
|
1999-09-09 03:14:30 +04: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 21:42:36 +04:00
|
|
|
JSBool
|
|
|
|
nsJSUtils::GetCallingLocation(JSContext* aContext, const char* *aFilename,
|
2008-03-07 01:52:58 +03:00
|
|
|
PRUint32* aLineno, nsIPrincipal* aPrincipal)
|
1999-09-09 03:14:30 +04: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 21:42:36 +04:00
|
|
|
frame = ::JS_FrameIterator(aContext, &frame);
|
|
|
|
|
1999-09-09 03:14:30 +04: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 21:42:36 +04:00
|
|
|
script = ::JS_GetFrameScript(aContext, frame);
|
1999-09-09 03:14:30 +04:00
|
|
|
}
|
2003-06-25 01:43:01 +04:00
|
|
|
} while (frame && !script);
|
1999-09-09 03:14:30 +04:00
|
|
|
|
|
|
|
if (script) {
|
2008-01-30 05:11:48 +03: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-07 01:52:58 +03: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 06:52:43 +03:00
|
|
|
nsIScriptSecurityManager *ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
|
2008-03-07 01:52:58 +03:00
|
|
|
if (NS_FAILED(ssm->IsSystemPrincipal(aPrincipal, &system)) || !system) {
|
|
|
|
JSPrincipals* jsprins;
|
|
|
|
aPrincipal->GetJSPrincipals(aContext, &jsprins);
|
2008-02-13 06:52:43 +03:00
|
|
|
|
2008-03-07 01:52:58 +03:00
|
|
|
*aFilename = jsprins->codebase;
|
|
|
|
*aLineno = 0;
|
|
|
|
JSPRINCIPALS_DROP(aContext, jsprins);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2008-01-30 05:11:48 +03: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 21:42:36 +04:00
|
|
|
const char* filename = ::JS_GetScriptFilename(aContext, script);
|
|
|
|
|
1999-09-09 03:14:30 +04: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 03:43:30 +03: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 21:42:36 +04:00
|
|
|
jsbytecode* bytecode = ::JS_GetFramePC(aContext, frame);
|
|
|
|
|
1999-09-09 03:14:30 +04: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 21:42:36 +04:00
|
|
|
lineno = ::JS_PCToLineNumber(aContext, script, bytecode);
|
1999-09-09 03:14:30 +04: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 21:42:36 +04: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 03:43:30 +03:00
|
|
|
*aFilename = filename;
|
|
|
|
*aLineno = lineno;
|
|
|
|
return JS_TRUE;
|
1999-09-09 03:14:30 +04: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 03:43:30 +03:00
|
|
|
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2004-02-10 01:48:53 +03:00
|
|
|
nsIScriptGlobalObject *
|
|
|
|
nsJSUtils::GetStaticScriptGlobal(JSContext* aContext, JSObject* aObj)
|
1999-12-18 23:29:29 +03:00
|
|
|
{
|
|
|
|
nsISupports* supports;
|
|
|
|
JSClass* clazz;
|
|
|
|
JSObject* parent;
|
|
|
|
JSObject* glob = aObj; // starting point for search
|
|
|
|
|
|
|
|
if (!glob)
|
2004-02-10 01:48:53 +03:00
|
|
|
return nsnull;
|
1999-12-18 23:29:29 +03: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 21:42:36 +04:00
|
|
|
while ((parent = ::JS_GetParent(aContext, glob)))
|
1999-12-18 23:29:29 +03:00
|
|
|
glob = parent;
|
|
|
|
|
2001-11-27 12:46:38 +03:00
|
|
|
clazz = JS_GET_CLASS(aContext, glob);
|
1999-12-18 23:29:29 +03: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 21:42:36 +04:00
|
|
|
!(supports = (nsISupports*)::JS_GetPrivate(aContext, glob))) {
|
2004-02-10 01:48:53 +03:00
|
|
|
return nsnull;
|
1999-12-18 23:29:29 +03:00
|
|
|
}
|
|
|
|
|
2010-12-22 04:41:23 +03:00
|
|
|
// We might either have a window directly (e.g. if the global is a
|
|
|
|
// sandbox whose script object principal pointer is a window), or an
|
|
|
|
// XPCWrappedNative for a window. We could also have other
|
|
|
|
// sandbox-related script object principals, but we can't do much
|
|
|
|
// about those short of trying to walk the proto chain of |glob|
|
|
|
|
// looking for a window or something.
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryInterface(supports));
|
|
|
|
if (!sgo) {
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrapper(do_QueryInterface(supports));
|
|
|
|
NS_ENSURE_TRUE(wrapper, nsnull);
|
|
|
|
sgo = do_QueryWrappedNative(wrapper);
|
|
|
|
}
|
2004-02-10 01:48:53 +03:00
|
|
|
|
|
|
|
// We're returning a pointer to something that's about to be
|
|
|
|
// released, but that's ok here.
|
|
|
|
return sgo;
|
1999-12-18 23:29:29 +03:00
|
|
|
}
|
|
|
|
|
2004-02-10 01:48:53 +03:00
|
|
|
nsIScriptContext *
|
|
|
|
nsJSUtils::GetStaticScriptContext(JSContext* aContext, JSObject* aObj)
|
1999-12-18 23:29:29 +03:00
|
|
|
{
|
2004-02-10 01:48:53 +03:00
|
|
|
nsIScriptGlobalObject *nativeGlobal = GetStaticScriptGlobal(aContext, aObj);
|
2003-11-18 04:58:43 +03:00
|
|
|
if (!nativeGlobal)
|
2004-02-10 01:48:53 +03:00
|
|
|
return nsnull;
|
|
|
|
|
2006-06-13 07:07:47 +04:00
|
|
|
return nativeGlobal->GetScriptContext(nsIProgrammingLanguage::JAVASCRIPT);
|
2003-11-18 04:58:43 +03:00
|
|
|
}
|
1999-12-18 23:29:29 +03:00
|
|
|
|
2004-02-10 01:48:53 +03:00
|
|
|
nsIScriptGlobalObject *
|
|
|
|
nsJSUtils::GetDynamicScriptGlobal(JSContext* aContext)
|
1999-12-18 23:29:29 +03:00
|
|
|
{
|
2004-02-10 01:48:53 +03:00
|
|
|
nsIScriptContext *scriptCX = GetDynamicScriptContext(aContext);
|
2001-06-20 07:27:48 +04:00
|
|
|
if (!scriptCX)
|
2004-02-10 01:48:53 +03:00
|
|
|
return nsnull;
|
|
|
|
return scriptCX->GetGlobalObject();
|
2003-11-18 04:58:43 +03:00
|
|
|
}
|
1999-12-18 23:29:29 +03:00
|
|
|
|
2004-02-10 01:48:53 +03:00
|
|
|
nsIScriptContext *
|
|
|
|
nsJSUtils::GetDynamicScriptContext(JSContext *aContext)
|
1999-12-18 23:29:29 +03:00
|
|
|
{
|
2004-02-10 01:48:53 +03:00
|
|
|
return GetScriptContextFromJSContext(aContext);
|
1999-12-18 23:29:29 +03:00
|
|
|
}
|
2010-12-20 19:21:58 +03:00
|
|
|
|
|
|
|
PRUint64
|
|
|
|
nsJSUtils::GetCurrentlyRunningCodeWindowID(JSContext *aContext)
|
|
|
|
{
|
|
|
|
if (!aContext)
|
|
|
|
return 0;
|
|
|
|
|
2011-02-09 04:20:06 +03:00
|
|
|
JSObject *jsGlobal;
|
|
|
|
if (!JS_GetGlobalForCallingScript(aContext, &jsGlobal) || !jsGlobal)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(aContext, jsGlobal))
|
|
|
|
return 0;
|
|
|
|
nsIScriptGlobalObject *sg = GetStaticScriptGlobal(aContext, jsGlobal);
|
|
|
|
if (!sg)
|
|
|
|
return 0;
|
2010-12-20 19:21:58 +03:00
|
|
|
|
2011-02-09 04:20:06 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(sg);
|
|
|
|
return win ? win->GetOuterWindow()->WindowID() : 0;
|
2010-12-20 19:21:58 +03:00
|
|
|
}
|
|
|
|
|