2001-05-17 05:20:51 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2004-05-13 18:34:18 +00:00
|
|
|
// vim: ft=cpp tw=78 sw=2 et ts=2
|
2004-04-17 21:52:36 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* 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/
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2001-05-17 05:20:51 +00:00
|
|
|
* The Original Code is Mozilla.
|
2004-04-17 21:52:36 +00:00
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2001-05-17 05:20:51 +00:00
|
|
|
* Vidur Apparao <vidur@netscape.com> (original author)
|
2004-04-17 21:52:36 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* 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"),
|
|
|
|
* 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
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* 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
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2006-03-31 08:00:42 +00:00
|
|
|
/*
|
|
|
|
* A class that handles loading and evaluation of <script> elements.
|
|
|
|
*/
|
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
#include "nsScriptLoader.h"
|
|
|
|
#include "nsIDOMCharacterData.h"
|
|
|
|
#include "nsParserUtils.h"
|
2005-01-05 07:25:29 +00:00
|
|
|
#include "nsIMIMEHeaderParam.h"
|
2001-05-17 05:20:51 +00:00
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsIContent.h"
|
2006-12-26 17:47:52 +00:00
|
|
|
#include "nsGkAtoms.h"
|
2001-05-17 05:20:51 +00:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
2001-07-16 02:40:48 +00:00
|
|
|
#include "nsIScriptContext.h"
|
2006-06-13 03:07:47 +00:00
|
|
|
#include "nsIScriptRuntime.h"
|
2001-05-17 05:20:51 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsContentPolicyUtils.h"
|
2001-05-21 22:40:10 +00:00
|
|
|
#include "nsIDOMWindow.h"
|
2001-05-17 05:20:51 +00:00
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsIScriptElement.h"
|
2004-07-06 22:52:03 +00:00
|
|
|
#include "nsIDOMHTMLScriptElement.h"
|
2001-05-17 05:20:51 +00:00
|
|
|
#include "nsIDocShell.h"
|
2006-06-13 03:07:47 +00:00
|
|
|
#include "jscntxt.h"
|
2003-03-04 12:12:48 +00:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
2003-07-11 22:47:17 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2006-03-22 18:36:36 +00:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsContentErrors.h"
|
2006-11-04 05:45:02 +00:00
|
|
|
#include "nsIParser.h"
|
2007-01-09 01:07:29 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2003-02-28 05:47:36 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2006-02-14 02:04:20 +00:00
|
|
|
// If aMaybeCertPrincipal is a cert principal and aNewPrincipal is not the same
|
|
|
|
// as aMaybeCertPrincipal, downgrade aMaybeCertPrincipal to a codebase
|
|
|
|
// principal. Return the downgraded principal, or aMaybeCertPrincipal if no
|
|
|
|
// downgrade was needed.
|
2003-10-21 22:11:49 +00:00
|
|
|
static already_AddRefed<nsIPrincipal>
|
2006-02-14 02:04:20 +00:00
|
|
|
MaybeDowngradeToCodebase(nsIPrincipal *aMaybeCertPrincipal,
|
|
|
|
nsIPrincipal *aNewPrincipal)
|
2003-10-21 22:11:49 +00:00
|
|
|
{
|
2006-02-14 02:04:20 +00:00
|
|
|
NS_PRECONDITION(aMaybeCertPrincipal, "Null old principal!");
|
|
|
|
NS_PRECONDITION(aNewPrincipal, "Null new principal!");
|
2003-10-21 22:11:49 +00:00
|
|
|
|
2006-02-14 02:04:20 +00:00
|
|
|
nsIPrincipal *principal = aMaybeCertPrincipal;
|
2003-10-21 22:11:49 +00:00
|
|
|
|
|
|
|
PRBool hasCert;
|
2006-02-14 02:04:20 +00:00
|
|
|
aMaybeCertPrincipal->GetHasCertificate(&hasCert);
|
2003-10-21 22:11:49 +00:00
|
|
|
if (hasCert) {
|
|
|
|
PRBool equal;
|
2006-02-14 02:04:20 +00:00
|
|
|
aMaybeCertPrincipal->Equals(aNewPrincipal, &equal);
|
2003-10-21 22:11:49 +00:00
|
|
|
if (!equal) {
|
|
|
|
nsCOMPtr<nsIURI> uri, domain;
|
2006-02-14 02:04:20 +00:00
|
|
|
aMaybeCertPrincipal->GetURI(getter_AddRefs(uri));
|
|
|
|
aMaybeCertPrincipal->GetDomain(getter_AddRefs(domain));
|
2003-10-21 22:11:49 +00:00
|
|
|
|
2006-02-14 02:04:20 +00:00
|
|
|
nsContentUtils::GetSecurityManager()->GetCodebasePrincipal(uri,
|
|
|
|
&principal);
|
2003-10-21 22:11:49 +00:00
|
|
|
if (principal && domain) {
|
|
|
|
principal->SetDomain(domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
return principal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(principal);
|
|
|
|
|
|
|
|
return principal;
|
|
|
|
}
|
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// Per-request data structure
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class nsScriptLoadRequest : public nsISupports {
|
|
|
|
public:
|
2004-07-06 22:52:03 +00:00
|
|
|
nsScriptLoadRequest(nsIScriptElement* aElement,
|
2006-11-04 05:45:02 +00:00
|
|
|
PRUint32 aVersion)
|
|
|
|
: mElement(aElement),
|
2007-03-01 08:11:40 +00:00
|
|
|
mLoading(PR_TRUE),
|
2006-11-04 05:45:02 +00:00
|
|
|
mIsInline(PR_TRUE),
|
|
|
|
mJSVersion(aVersion), mLineNo(1)
|
|
|
|
{
|
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
void FireScriptAvailable(nsresult aResult)
|
|
|
|
{
|
2007-03-01 08:11:40 +00:00
|
|
|
mElement->ScriptAvailable(aResult, mElement, mIsInline, mURI, mLineNo);
|
2006-11-04 05:45:02 +00:00
|
|
|
}
|
|
|
|
void FireScriptEvaluated(nsresult aResult)
|
|
|
|
{
|
2007-03-01 08:11:40 +00:00
|
|
|
mElement->ScriptEvaluated(aResult, mElement, mIsInline);
|
2006-11-04 05:45:02 +00:00
|
|
|
}
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2004-07-06 22:52:03 +00:00
|
|
|
nsCOMPtr<nsIScriptElement> mElement;
|
2002-10-11 00:38:29 +00:00
|
|
|
PRPackedBool mLoading; // Are we still waiting for a load to complete?
|
|
|
|
PRPackedBool mIsInline; // Is the script inline or loaded?
|
2004-02-19 02:44:03 +00:00
|
|
|
nsString mScriptText; // Holds script for loaded scripts
|
2006-06-13 03:07:47 +00:00
|
|
|
PRUint32 mJSVersion;
|
2001-05-17 05:20:51 +00:00
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
PRInt32 mLineNo;
|
|
|
|
};
|
|
|
|
|
2002-01-08 06:32:36 +00:00
|
|
|
// The nsScriptLoadRequest is passed as the context to necko, and thus
|
|
|
|
// it needs to be threadsafe. Necko won't do anything with this
|
|
|
|
// context, but it will AddRef and Release it on other threads.
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS0(nsScriptLoadRequest)
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
nsScriptLoader::nsScriptLoader(nsIDocument *aDocument)
|
|
|
|
: mDocument(aDocument),
|
|
|
|
mBlockerCount(0),
|
2007-03-01 08:11:40 +00:00
|
|
|
mEnabled(PR_TRUE),
|
|
|
|
mHadPendingScripts(PR_FALSE)
|
2001-05-17 05:20:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsScriptLoader::~nsScriptLoader()
|
|
|
|
{
|
|
|
|
mObservers.Clear();
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
for (PRInt32 i = 0; i < mPendingRequests.Count(); i++) {
|
|
|
|
mPendingRequests[i]->FireScriptAvailable(NS_ERROR_ABORT);
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
2007-09-28 15:15:07 +00:00
|
|
|
|
|
|
|
// Unblock the kids, in case any of them moved to a different document
|
|
|
|
// subtree in the meantime and therefore aren't actually going away.
|
|
|
|
for (PRUint32 j = 0; j < mPendingChildLoaders.Length(); ++j) {
|
|
|
|
mPendingChildLoaders[j]->RemoveExecuteBlocker();
|
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsScriptLoader, nsIStreamLoaderObserver)
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2003-03-04 12:12:48 +00:00
|
|
|
// Helper method for checking if the script element is an event-handler
|
|
|
|
// This means that it has both a for-attribute and a event-attribute.
|
|
|
|
// Also, if the for-attribute has a value that matches "\s*window\s*",
|
|
|
|
// and the event-attribute matches "\s*onload([ \(].*)?" then it isn't an
|
|
|
|
// eventhandler. (both matches are case insensitive).
|
|
|
|
// This is how IE seems to filter out a window's onload handler from a
|
|
|
|
// <script for=... event=...> element.
|
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
static PRBool
|
|
|
|
IsScriptEventHandler(nsIScriptElement *aScriptElement)
|
2003-03-04 12:12:48 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> contElement = do_QueryInterface(aScriptElement);
|
2005-10-28 11:25:24 +00:00
|
|
|
NS_ASSERTION(contElement, "nsIScriptElement isn't nsIContent");
|
2003-03-04 12:12:48 +00:00
|
|
|
|
2005-10-28 11:25:24 +00:00
|
|
|
nsAutoString forAttr, eventAttr;
|
2006-12-26 17:47:52 +00:00
|
|
|
if (!contElement->GetAttr(kNameSpaceID_None, nsGkAtoms::_for, forAttr) ||
|
|
|
|
!contElement->GetAttr(kNameSpaceID_None, nsGkAtoms::event, eventAttr)) {
|
2005-10-28 11:25:24 +00:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2003-03-04 12:12:48 +00:00
|
|
|
|
2005-10-28 11:25:24 +00:00
|
|
|
const nsAString& for_str = nsContentUtils::TrimWhitespace(forAttr);
|
2004-06-17 00:13:25 +00:00
|
|
|
if (!for_str.LowerCaseEqualsLiteral("window")) {
|
2003-03-04 12:12:48 +00:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We found for="window", now check for event="onload".
|
2005-10-28 11:25:24 +00:00
|
|
|
const nsAString& event_str = nsContentUtils::TrimWhitespace(eventAttr, PR_FALSE);
|
2003-07-14 07:37:39 +00:00
|
|
|
if (!StringBeginsWith(event_str, NS_LITERAL_STRING("onload"),
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
2003-03-04 12:12:48 +00:00
|
|
|
// It ain't "onload.*".
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString::const_iterator start, end;
|
|
|
|
event_str.BeginReading(start);
|
|
|
|
event_str.EndReading(end);
|
|
|
|
|
|
|
|
start.advance(6); // advance past "onload"
|
|
|
|
|
|
|
|
if (start != end && *start != '(' && *start != ' ') {
|
|
|
|
// We got onload followed by something other than space or
|
|
|
|
// '('. Not good enough.
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2005-12-02 04:32:18 +00:00
|
|
|
nsresult
|
2006-11-04 05:45:02 +00:00
|
|
|
nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
|
2005-12-02 04:32:18 +00:00
|
|
|
{
|
2001-05-17 05:20:51 +00:00
|
|
|
// We need a document to evaluate scripts.
|
2005-12-02 04:32:18 +00:00
|
|
|
NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
// Check to see if scripts has been turned off.
|
|
|
|
if (!mEnabled || !mDocument->IsScriptEnabled()) {
|
2005-12-02 04:32:18 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
NS_ASSERTION(!aElement->IsMalformed(), "Executing malformed script");
|
|
|
|
|
2003-03-04 12:12:48 +00:00
|
|
|
// Check that the script is not an eventhandler
|
|
|
|
if (IsScriptEventHandler(aElement)) {
|
2005-12-02 04:32:18 +00:00
|
|
|
return NS_CONTENT_SCRIPT_IS_EVENTHANDLER;
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2003-08-11 18:13:16 +00:00
|
|
|
// Script evaluation can also be disabled in the current script
|
|
|
|
// context even though it's enabled in the document.
|
2006-06-13 03:07:47 +00:00
|
|
|
// XXX - still hard-coded for JS here, even though another language
|
|
|
|
// may be specified. Should this check be made *after* we examine
|
|
|
|
// the attributes to locate the script-type?
|
|
|
|
// For now though, if JS is disabled we assume every language is
|
|
|
|
// disabled.
|
2006-11-04 05:45:02 +00:00
|
|
|
// XXX is this different from the mDocument->IsScriptEnabled() call?
|
2003-10-22 06:09:48 +00:00
|
|
|
nsIScriptGlobalObject *globalObject = mDocument->GetScriptGlobalObject();
|
2007-04-11 20:18:11 +00:00
|
|
|
if (!globalObject) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptContext *context = globalObject->GetScriptContext(
|
|
|
|
nsIProgrammingLanguage::JAVASCRIPT);
|
2004-02-09 22:48:53 +00:00
|
|
|
|
2007-04-11 20:18:11 +00:00
|
|
|
// If scripts aren't enabled in the current context, there's no
|
|
|
|
// point in going on.
|
|
|
|
if (!context || !context->GetScriptsEnabled()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2003-08-11 18:13:16 +00:00
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
// Default script language is whatever the root content specifies
|
|
|
|
// (which may come from a header or http-meta tag)
|
|
|
|
PRUint32 typeID = mDocument->GetRootContent()->GetScriptTypeID();
|
|
|
|
PRUint32 version = 0;
|
2002-05-06 22:59:42 +00:00
|
|
|
nsAutoString language, type, src;
|
2005-12-02 04:32:18 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
// Check the type attribute to determine language and version.
|
2006-06-13 03:07:47 +00:00
|
|
|
// If type exists, it trumps the deprecated 'language='
|
2004-07-06 22:52:03 +00:00
|
|
|
aElement->GetScriptType(type);
|
2001-05-17 05:20:51 +00:00
|
|
|
if (!type.IsEmpty()) {
|
2005-01-05 07:25:29 +00:00
|
|
|
nsCOMPtr<nsIMIMEHeaderParam> mimeHdrParser =
|
|
|
|
do_GetService("@mozilla.org/network/mime-hdrparam;1");
|
|
|
|
NS_ENSURE_TRUE(mimeHdrParser, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 typeAndParams(type);
|
|
|
|
|
|
|
|
nsAutoString mimeType;
|
|
|
|
rv = mimeHdrParser->GetParameter(typeAndParams, nsnull,
|
|
|
|
EmptyCString(), PR_FALSE, nsnull,
|
|
|
|
mimeType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
// Javascript keeps the fast path, optimized for most-likely type
|
2005-06-14 16:08:05 +00:00
|
|
|
// Table ordered from most to least likely JS MIME types.
|
|
|
|
// See bug 62485, feel free to add <script type="..."> survey data to it,
|
|
|
|
// or to a new bug once 62485 is closed.
|
|
|
|
static const char *jsTypes[] = {
|
|
|
|
"text/javascript",
|
|
|
|
"text/ecmascript",
|
|
|
|
"application/javascript",
|
|
|
|
"application/ecmascript",
|
|
|
|
"application/x-javascript",
|
|
|
|
nsnull
|
|
|
|
};
|
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
PRBool isJavaScript = PR_FALSE;
|
2005-06-14 16:08:05 +00:00
|
|
|
for (PRInt32 i = 0; jsTypes[i]; i++) {
|
|
|
|
if (mimeType.LowerCaseEqualsASCII(jsTypes[i])) {
|
|
|
|
isJavaScript = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-06-13 03:07:47 +00:00
|
|
|
if (isJavaScript)
|
|
|
|
typeID = nsIProgrammingLanguage::JAVASCRIPT;
|
|
|
|
else {
|
|
|
|
// Use the object factory to locate a matching language.
|
|
|
|
nsCOMPtr<nsIScriptRuntime> runtime;
|
|
|
|
rv = NS_GetScriptRuntime(mimeType, getter_AddRefs(runtime));
|
|
|
|
if (NS_FAILED(rv) || runtime == nsnull) {
|
|
|
|
// Failed to get the explicitly specified language
|
|
|
|
NS_WARNING("Failed to find a scripting language");
|
|
|
|
typeID = nsIProgrammingLanguage::UNKNOWN;
|
|
|
|
} else
|
|
|
|
typeID = runtime->GetScriptTypeID();
|
|
|
|
}
|
|
|
|
if (typeID != nsIProgrammingLanguage::UNKNOWN) {
|
|
|
|
// Get the version string, and ensure the language supports it.
|
|
|
|
nsAutoString versionName;
|
2005-01-05 07:25:29 +00:00
|
|
|
rv = mimeHdrParser->GetParameter(typeAndParams, "version",
|
|
|
|
EmptyCString(), PR_FALSE, nsnull,
|
2006-06-13 03:07:47 +00:00
|
|
|
versionName);
|
2005-01-05 07:25:29 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2006-06-13 03:07:47 +00:00
|
|
|
// no version attribute - version remains 0.
|
2005-01-05 07:25:29 +00:00
|
|
|
if (rv != NS_ERROR_INVALID_ARG)
|
|
|
|
return rv;
|
|
|
|
} else {
|
2006-06-13 03:07:47 +00:00
|
|
|
nsCOMPtr<nsIScriptRuntime> runtime;
|
|
|
|
rv = NS_GetScriptRuntimeByID(typeID, getter_AddRefs(runtime));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("Failed to locate the language with this ID");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = runtime->ParseVersion(versionName, &version);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("This script language version is not supported - ignored");
|
|
|
|
typeID = nsIProgrammingLanguage::UNKNOWN;
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-13 03:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some js specifics yet to be abstracted.
|
|
|
|
if (typeID == nsIProgrammingLanguage::JAVASCRIPT) {
|
|
|
|
nsAutoString value;
|
2005-01-05 07:25:29 +00:00
|
|
|
|
|
|
|
rv = mimeHdrParser->GetParameter(typeAndParams, "e4x",
|
|
|
|
EmptyCString(), PR_FALSE, nsnull,
|
|
|
|
value);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (rv != NS_ERROR_INVALID_ARG)
|
|
|
|
return rv;
|
|
|
|
} else {
|
|
|
|
if (value.Length() == 1 && value[0] == '1')
|
2006-06-13 03:07:47 +00:00
|
|
|
// This means that we need to set JSOPTION_XML in the JS options.
|
|
|
|
// We re-use our knowledge of the implementation to reuse
|
|
|
|
// JSVERSION_HAS_XML as a safe version flag.
|
|
|
|
// If version has JSVERSION_UNKNOWN (-1), then this is still OK.
|
|
|
|
version |= JSVERSION_HAS_XML;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no 'type=' element
|
|
|
|
// "language" is a deprecated attribute of HTML, so we check it only for
|
|
|
|
// HTML script elements.
|
|
|
|
nsCOMPtr<nsIDOMHTMLScriptElement> htmlScriptElement =
|
|
|
|
do_QueryInterface(aElement);
|
|
|
|
if (htmlScriptElement) {
|
|
|
|
htmlScriptElement->GetAttribute(NS_LITERAL_STRING("language"), language);
|
|
|
|
if (!language.IsEmpty()) {
|
|
|
|
if (nsParserUtils::IsJavaScriptLanguage(language, &version))
|
|
|
|
typeID = nsIProgrammingLanguage::JAVASCRIPT;
|
|
|
|
else
|
|
|
|
typeID = nsIProgrammingLanguage::UNKNOWN;
|
|
|
|
// IE, Opera, etc. do not respect language version, so neither should
|
|
|
|
// we at this late date in the browser wars saga. Note that this change
|
|
|
|
// affects HTML but not XUL or SVG (but note also that XUL has its own
|
|
|
|
// code to check nsParserUtils::IsJavaScriptLanguage -- that's probably
|
|
|
|
// a separate bug, one we may not be able to fix short of XUL2). See
|
|
|
|
// bug 255895 (https://bugzilla.mozilla.org/show_bug.cgi?id=255895).
|
|
|
|
NS_ASSERTION(JSVERSION_DEFAULT == 0,
|
|
|
|
"We rely on all languages having 0 as a version default");
|
|
|
|
version = 0;
|
2005-01-05 07:25:29 +00:00
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
// If we don't know the language, we don't know how to evaluate
|
|
|
|
if (typeID == nsIProgrammingLanguage::UNKNOWN) {
|
2005-12-02 04:32:18 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
2006-06-13 03:07:47 +00:00
|
|
|
// If not from a chrome document (which is always trusted), we need some way
|
|
|
|
// of checking the language is "safe". Currently the only other language
|
|
|
|
// impl is Python, and that is *not* safe in untrusted code - so fixing
|
|
|
|
// this isn't a priority.!
|
|
|
|
// See also similar code in nsXULContentSink.cpp
|
|
|
|
if (typeID != nsIProgrammingLanguage::JAVASCRIPT &&
|
|
|
|
!nsContentUtils::IsChromeDoc(mDocument)) {
|
|
|
|
NS_WARNING("Untrusted language called from non-chrome - ignored");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> eltContent(do_QueryInterface(aElement));
|
2006-11-04 05:45:02 +00:00
|
|
|
eltContent->SetScriptTypeID(typeID);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
// Create a request object for this script
|
2006-11-04 05:45:02 +00:00
|
|
|
nsRefPtr<nsScriptLoadRequest> request = new nsScriptLoadRequest(aElement, version);
|
2005-12-02 04:32:18 +00:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_OUT_OF_MEMORY);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2004-07-06 22:52:03 +00:00
|
|
|
// First check to see if this is an external script
|
|
|
|
nsCOMPtr<nsIURI> scriptURI = aElement->GetScriptURI();
|
|
|
|
if (scriptURI) {
|
2001-05-17 05:20:51 +00:00
|
|
|
// Check that the containing page is allowed to load this URI.
|
2005-10-14 09:07:29 +00:00
|
|
|
rv = nsContentUtils::GetSecurityManager()->
|
2006-04-27 18:21:11 +00:00
|
|
|
CheckLoadURIWithPrincipal(mDocument->NodePrincipal(), scriptURI,
|
2004-04-25 16:55:27 +00:00
|
|
|
nsIScriptSecurityManager::ALLOW_CHROME);
|
2003-10-21 22:11:49 +00:00
|
|
|
|
2005-12-02 04:32:18 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
// After the security manager, the content-policy stuff gets a veto
|
2007-04-11 20:18:11 +00:00
|
|
|
PRInt16 shouldLoad = nsIContentPolicy::ACCEPT;
|
|
|
|
rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_SCRIPT,
|
|
|
|
scriptURI,
|
2007-08-08 01:16:09 +00:00
|
|
|
mDocument->NodePrincipal(),
|
2007-04-11 20:18:11 +00:00
|
|
|
aElement,
|
|
|
|
NS_LossyConvertUTF16toASCII(type),
|
|
|
|
nsnull, //extra
|
|
|
|
&shouldLoad,
|
2007-08-08 01:16:09 +00:00
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
nsContentUtils::GetSecurityManager());
|
2007-04-11 20:18:11 +00:00
|
|
|
if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
|
|
|
|
if (NS_FAILED(rv) || shouldLoad != nsIContentPolicy::REJECT_TYPE) {
|
|
|
|
return NS_ERROR_CONTENT_BLOCKED;
|
2001-05-21 22:40:10 +00:00
|
|
|
}
|
2007-04-11 20:18:11 +00:00
|
|
|
return NS_ERROR_CONTENT_BLOCKED_SHOW_ALT;
|
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2007-04-11 20:18:11 +00:00
|
|
|
request->mURI = scriptURI;
|
|
|
|
request->mIsInline = PR_FALSE;
|
|
|
|
request->mLoading = PR_TRUE;
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2007-04-11 20:18:11 +00:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = mDocument->GetDocumentLoadGroup();
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2007-04-11 20:18:11 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(globalObject));
|
|
|
|
nsIDocShell *docshell = window->GetDocShell();
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2007-04-11 20:18:11 +00:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> prompter(do_QueryInterface(docshell));
|
2001-08-10 01:46:41 +00:00
|
|
|
|
2007-04-11 20:18:11 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
scriptURI, nsnull, loadGroup,
|
|
|
|
prompter, nsIRequest::LOAD_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-11-04 05:45:02 +00:00
|
|
|
|
2007-04-11 20:18:11 +00:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
|
|
|
|
if (httpChannel) {
|
|
|
|
// HTTP content negotation has little value in this context.
|
|
|
|
httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
|
|
|
|
NS_LITERAL_CSTRING("*/*"),
|
|
|
|
PR_FALSE);
|
|
|
|
httpChannel->SetReferrer(mDocument->GetDocumentURI());
|
|
|
|
}
|
2005-12-02 04:32:18 +00:00
|
|
|
|
2007-04-11 20:18:11 +00:00
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), this);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-11-04 05:45:02 +00:00
|
|
|
|
2007-04-11 20:18:11 +00:00
|
|
|
rv = channel->AsyncOpen(loader, request);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-08-10 01:46:41 +00:00
|
|
|
} else {
|
2001-05-17 05:20:51 +00:00
|
|
|
request->mLoading = PR_FALSE;
|
|
|
|
request->mIsInline = PR_TRUE;
|
2004-01-09 23:54:21 +00:00
|
|
|
request->mURI = mDocument->GetDocumentURI();
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2004-07-06 22:52:03 +00:00
|
|
|
request->mLineNo = aElement->GetScriptLineNumber();
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
// If we've got existing pending requests, add ourselves
|
|
|
|
// to this list.
|
2007-10-07 18:23:52 +00:00
|
|
|
if (mPendingRequests.Count() == 0 && ReadyToExecuteScripts()) {
|
2006-11-04 05:45:02 +00:00
|
|
|
return ProcessRequest(request);
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
// Add the request to our pending requests list
|
|
|
|
NS_ENSURE_TRUE(mPendingRequests.AppendObject(request),
|
|
|
|
NS_ERROR_OUT_OF_MEMORY);
|
2005-06-07 19:21:05 +00:00
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
// Added as pending request, now we can send blocking back
|
|
|
|
return NS_ERROR_HTMLPARSER_BLOCK;
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptLoader::ProcessRequest(nsScriptLoadRequest* aRequest)
|
|
|
|
{
|
2006-11-04 05:45:02 +00:00
|
|
|
NS_ASSERTION(ReadyToExecuteScripts(),
|
|
|
|
"Caller forgot to check ReadyToExecuteScripts()");
|
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
NS_ENSURE_ARG(aRequest);
|
|
|
|
nsAFlatString* script;
|
|
|
|
nsAutoString textData;
|
|
|
|
|
|
|
|
// If there's no script text, we try to get it from the element
|
|
|
|
if (aRequest->mIsInline) {
|
|
|
|
// XXX This is inefficient - GetText makes multiple
|
|
|
|
// copies.
|
2004-07-06 22:52:03 +00:00
|
|
|
aRequest->mElement->GetScriptText(textData);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
script = &textData;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
script = &aRequest->mScriptText;
|
|
|
|
}
|
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
FireScriptAvailable(NS_OK, aRequest);
|
2001-05-17 05:20:51 +00:00
|
|
|
nsresult rv = EvaluateScript(aRequest, *script);
|
|
|
|
FireScriptEvaluated(rv, aRequest);
|
2003-11-13 04:15:25 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::FireScriptAvailable(nsresult aResult,
|
2006-11-04 05:45:02 +00:00
|
|
|
nsScriptLoadRequest* aRequest)
|
2001-05-17 05:20:51 +00:00
|
|
|
{
|
2006-11-04 05:45:02 +00:00
|
|
|
for (PRInt32 i = 0; i < mObservers.Count(); i++) {
|
|
|
|
nsCOMPtr<nsIScriptLoaderObserver> obs = mObservers[i];
|
|
|
|
obs->ScriptAvailable(aResult, aRequest->mElement,
|
2007-03-01 08:11:40 +00:00
|
|
|
aRequest->mIsInline, aRequest->mURI,
|
|
|
|
aRequest->mLineNo);
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
aRequest->FireScriptAvailable(aResult);
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::FireScriptEvaluated(nsresult aResult,
|
|
|
|
nsScriptLoadRequest* aRequest)
|
|
|
|
{
|
2006-11-04 05:45:02 +00:00
|
|
|
for (PRInt32 i = 0; i < mObservers.Count(); i++) {
|
|
|
|
nsCOMPtr<nsIScriptLoaderObserver> obs = mObservers[i];
|
|
|
|
obs->ScriptEvaluated(aResult, aRequest->mElement,
|
2007-03-01 08:11:40 +00:00
|
|
|
aRequest->mIsInline);
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
2005-01-05 07:25:29 +00:00
|
|
|
aRequest->FireScriptEvaluated(aResult);
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptLoader::EvaluateScript(nsScriptLoadRequest* aRequest,
|
|
|
|
const nsAFlatString& aScript)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2001-09-25 22:16:41 +00:00
|
|
|
// We need a document to evaluate scripts.
|
|
|
|
if (!mDocument) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2003-10-22 06:09:48 +00:00
|
|
|
nsIScriptGlobalObject *globalObject = mDocument->GetScriptGlobalObject();
|
2001-05-17 05:20:51 +00:00
|
|
|
NS_ENSURE_TRUE(globalObject, NS_ERROR_FAILURE);
|
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
// Get the script-type to be used by this element.
|
|
|
|
nsCOMPtr<nsIContent> scriptContent(do_QueryInterface(aRequest->mElement));
|
|
|
|
NS_ASSERTION(scriptContent, "no content - what is default script-type?");
|
|
|
|
PRUint32 stid = scriptContent ? scriptContent->GetScriptTypeID() :
|
|
|
|
nsIProgrammingLanguage::JAVASCRIPT;
|
|
|
|
// and make sure we are setup for this type of script.
|
|
|
|
rv = globalObject->EnsureScriptEnvironment(stid);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2004-02-26 02:35:30 +00:00
|
|
|
// Make sure context is a strong reference since we access it after
|
|
|
|
// we've executed a script, which may cause all other references to
|
|
|
|
// the context to go away.
|
2006-06-13 03:07:47 +00:00
|
|
|
nsCOMPtr<nsIScriptContext> context = globalObject->GetScriptContext(stid);
|
2004-02-09 22:48:53 +00:00
|
|
|
if (!context) {
|
2001-05-17 05:20:51 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCAutoString url;
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
if (aRequest->mURI) {
|
2002-03-06 07:48:55 +00:00
|
|
|
rv = aRequest->mURI->GetSpec(url);
|
2001-05-17 05:20:51 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2004-02-26 02:35:30 +00:00
|
|
|
|
2004-09-01 16:50:12 +00:00
|
|
|
PRBool oldProcessingScriptTag = context->GetProcessingScriptTag();
|
2001-05-17 05:20:51 +00:00
|
|
|
context->SetProcessingScriptTag(PR_TRUE);
|
|
|
|
|
2005-06-07 19:21:05 +00:00
|
|
|
// Update our current script.
|
|
|
|
nsCOMPtr<nsIScriptElement> oldCurrent = mCurrentScript;
|
|
|
|
mCurrentScript = aRequest->mElement;
|
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
PRBool isUndefined;
|
2006-06-13 03:07:47 +00:00
|
|
|
rv = context->EvaluateString(aScript,
|
|
|
|
globalObject->GetScriptGlobal(stid),
|
|
|
|
mDocument->NodePrincipal(), url.get(),
|
|
|
|
aRequest->mLineNo, aRequest->mJSVersion, nsnull,
|
|
|
|
&isUndefined);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2005-06-07 19:21:05 +00:00
|
|
|
// Put the old script back in case it wants to do anything else.
|
|
|
|
mCurrentScript = oldCurrent;
|
|
|
|
|
2006-07-05 21:43:46 +00:00
|
|
|
JSContext *cx = nsnull; // Initialize this to keep GCC happy.
|
2006-06-13 03:07:47 +00:00
|
|
|
if (stid == nsIProgrammingLanguage::JAVASCRIPT) {
|
2006-07-05 21:43:46 +00:00
|
|
|
cx = (JSContext *)context->GetNativeContext();
|
|
|
|
::JS_BeginRequest(cx);
|
|
|
|
::JS_ReportPendingException(cx);
|
2005-01-05 07:25:29 +00:00
|
|
|
}
|
2004-09-01 16:50:12 +00:00
|
|
|
|
|
|
|
context->SetProcessingScriptTag(oldProcessingScriptTag);
|
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
if (stid == nsIProgrammingLanguage::JAVASCRIPT) {
|
|
|
|
nsCOMPtr<nsIXPCNativeCallContext> ncc;
|
|
|
|
nsContentUtils::XPConnect()->
|
|
|
|
GetCurrentNativeCallContext(getter_AddRefs(ncc));
|
2004-09-01 16:50:12 +00:00
|
|
|
|
2006-06-13 03:07:47 +00:00
|
|
|
if (ncc) {
|
2006-07-05 21:43:46 +00:00
|
|
|
NS_ASSERTION(!::JS_IsExceptionPending(cx),
|
|
|
|
"JS_ReportPendingException wasn't called");
|
2006-06-13 03:07:47 +00:00
|
|
|
ncc->SetExceptionWasThrown(PR_FALSE);
|
|
|
|
}
|
2006-07-05 21:43:46 +00:00
|
|
|
::JS_EndRequest(cx);
|
2004-09-01 16:50:12 +00:00
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-01-09 01:07:29 +00:00
|
|
|
void
|
|
|
|
nsScriptLoader::ProcessPendingRequestsAsync()
|
|
|
|
{
|
2007-09-28 15:15:07 +00:00
|
|
|
if (mPendingRequests.Count() || !mPendingChildLoaders.IsEmpty()) {
|
2007-01-09 01:07:29 +00:00
|
|
|
nsCOMPtr<nsIRunnable> ev = new nsRunnableMethod<nsScriptLoader>(this,
|
|
|
|
&nsScriptLoader::ProcessPendingRequests);
|
|
|
|
|
|
|
|
NS_DispatchToCurrentThread(ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
void
|
2006-11-04 05:45:02 +00:00
|
|
|
nsScriptLoader::ProcessPendingRequests()
|
2001-05-17 05:20:51 +00:00
|
|
|
{
|
2006-11-04 05:45:02 +00:00
|
|
|
nsRefPtr<nsScriptLoadRequest> request;
|
2007-10-07 18:23:52 +00:00
|
|
|
while (mPendingRequests.Count() && ReadyToExecuteScripts() &&
|
2006-11-04 05:45:02 +00:00
|
|
|
!(request = mPendingRequests[0])->mLoading) {
|
2003-07-11 22:47:17 +00:00
|
|
|
mPendingRequests.RemoveObjectAt(0);
|
2001-05-17 05:20:51 +00:00
|
|
|
ProcessRequest(request);
|
|
|
|
}
|
2007-09-28 15:15:07 +00:00
|
|
|
|
2007-10-07 18:23:52 +00:00
|
|
|
while (!mPendingChildLoaders.IsEmpty() && ReadyToExecuteScripts()) {
|
2007-09-28 15:15:07 +00:00
|
|
|
nsRefPtr<nsScriptLoader> child = mPendingChildLoaders[0];
|
|
|
|
mPendingChildLoaders.RemoveElementAt(0);
|
|
|
|
child->RemoveExecuteBlocker();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsScriptLoader::ReadyToExecuteScripts()
|
|
|
|
{
|
|
|
|
// Make sure the SelfReadyToExecuteScripts check is first, so that
|
|
|
|
// we don't block twice on an ancestor.
|
|
|
|
if (!SelfReadyToExecuteScripts()) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (nsIDocument* doc = mDocument; doc; doc = doc->GetParentDocument()) {
|
|
|
|
nsScriptLoader* ancestor = doc->ScriptLoader();
|
|
|
|
if (!ancestor->SelfReadyToExecuteScripts() &&
|
|
|
|
ancestor->AddPendingChildLoader(this)) {
|
|
|
|
AddExecuteBlocker();
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
2002-09-18 19:44:19 +00:00
|
|
|
|
2005-01-05 07:25:29 +00:00
|
|
|
// This function was copied from nsParser.cpp. It was simplified a bit.
|
|
|
|
static PRBool
|
|
|
|
DetectByteOrderMark(const unsigned char* aBytes, PRInt32 aLen, nsCString& oCharset)
|
2002-09-18 19:44:19 +00:00
|
|
|
{
|
|
|
|
if (aLen < 2)
|
2002-09-18 22:05:09 +00:00
|
|
|
return PR_FALSE;
|
2002-09-18 19:44:19 +00:00
|
|
|
|
|
|
|
switch(aBytes[0]) {
|
2005-01-05 07:25:29 +00:00
|
|
|
case 0xEF:
|
|
|
|
if (aLen >= 3 && 0xBB == aBytes[1] && 0xBF == aBytes[2]) {
|
|
|
|
// EF BB BF
|
|
|
|
// Win2K UTF-8 BOM
|
|
|
|
oCharset.Assign("UTF-8");
|
2002-09-18 19:44:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xFE:
|
2005-01-05 07:25:29 +00:00
|
|
|
if (0xFF == aBytes[1]) {
|
2002-09-18 19:44:19 +00:00
|
|
|
// FE FF
|
2005-01-05 07:25:29 +00:00
|
|
|
// UTF-16, big-endian
|
|
|
|
oCharset.Assign("UTF-16BE");
|
2002-09-18 19:44:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xFF:
|
2005-01-05 07:25:29 +00:00
|
|
|
if (0xFE == aBytes[1]) {
|
2002-09-18 19:44:19 +00:00
|
|
|
// FF FE
|
2005-01-05 07:25:29 +00:00
|
|
|
// UTF-16, little-endian
|
|
|
|
oCharset.Assign("UTF-16LE");
|
2002-09-18 19:44:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2005-01-05 07:25:29 +00:00
|
|
|
}
|
2003-05-23 21:34:47 +00:00
|
|
|
return !oCharset.IsEmpty();
|
2002-09-18 19:44:19 +00:00
|
|
|
}
|
|
|
|
|
2005-06-12 12:32:05 +00:00
|
|
|
/* static */ nsresult
|
|
|
|
nsScriptLoader::ConvertToUTF16(nsIChannel* aChannel, const PRUint8* aData,
|
|
|
|
PRUint32 aLength, const nsString& aHintCharset,
|
|
|
|
nsIDocument* aDocument, nsString& aString)
|
|
|
|
{
|
|
|
|
if (!aLength) {
|
|
|
|
aString.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString characterSet;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (aChannel) {
|
|
|
|
rv = aChannel->GetContentCharset(characterSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aHintCharset.IsEmpty() && (NS_FAILED(rv) || characterSet.IsEmpty())) {
|
|
|
|
// charset name is always ASCII.
|
|
|
|
LossyCopyUTF16toASCII(aHintCharset, characterSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || characterSet.IsEmpty()) {
|
|
|
|
DetectByteOrderMark(aData, aLength, characterSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (characterSet.IsEmpty()) {
|
|
|
|
// charset from document default
|
|
|
|
characterSet = aDocument->GetDocumentCharacterSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (characterSet.IsEmpty()) {
|
|
|
|
// fall back to ISO-8859-1, see bug 118404
|
|
|
|
characterSet.AssignLiteral("ISO-8859-1");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> charsetConv =
|
2006-06-03 23:35:59 +00:00
|
|
|
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
2005-06-12 12:32:05 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && charsetConv) {
|
|
|
|
rv = charsetConv->GetUnicodeDecoder(characterSet.get(),
|
|
|
|
getter_AddRefs(unicodeDecoder));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// fall back to ISO-8859-1 if charset is not supported. (bug 230104)
|
|
|
|
rv = charsetConv->GetUnicodeDecoderRaw("ISO-8859-1",
|
|
|
|
getter_AddRefs(unicodeDecoder));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// converts from the charset to unicode
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRInt32 unicodeLength = 0;
|
|
|
|
|
2007-07-08 07:08:04 +00:00
|
|
|
rv = unicodeDecoder->GetMaxLength(reinterpret_cast<const char*>(aData),
|
2005-06-12 12:32:05 +00:00
|
|
|
aLength, &unicodeLength);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2006-06-22 19:07:30 +00:00
|
|
|
if (!EnsureStringLength(aString, unicodeLength))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2005-06-12 12:32:05 +00:00
|
|
|
PRUnichar *ustr = aString.BeginWriting();
|
|
|
|
|
|
|
|
PRInt32 consumedLength = 0;
|
|
|
|
PRInt32 originalLength = aLength;
|
|
|
|
PRInt32 convertedLength = 0;
|
|
|
|
PRInt32 bufferLength = unicodeLength;
|
|
|
|
do {
|
2007-07-08 07:08:04 +00:00
|
|
|
rv = unicodeDecoder->Convert(reinterpret_cast<const char*>(aData),
|
2005-06-12 12:32:05 +00:00
|
|
|
(PRInt32 *) &aLength, ustr,
|
|
|
|
&unicodeLength);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// if we failed, we consume one byte, replace it with U+FFFD
|
|
|
|
// and try the conversion again.
|
|
|
|
ustr[unicodeLength++] = (PRUnichar)0xFFFD;
|
|
|
|
ustr += unicodeLength;
|
|
|
|
|
|
|
|
unicodeDecoder->Reset();
|
|
|
|
}
|
|
|
|
aData += ++aLength;
|
|
|
|
consumedLength += aLength;
|
|
|
|
aLength = originalLength - consumedLength;
|
|
|
|
convertedLength += unicodeLength;
|
|
|
|
unicodeLength = bufferLength - convertedLength;
|
|
|
|
} while (NS_FAILED(rv) && (originalLength > consumedLength) && (bufferLength > convertedLength));
|
|
|
|
aString.SetLength(convertedLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2002-09-18 19:44:19 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptLoader::OnStreamComplete(nsIStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus,
|
2006-11-04 05:45:02 +00:00
|
|
|
PRUint32 aStringLen,
|
|
|
|
const PRUint8* aString)
|
2001-05-17 05:20:51 +00:00
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
nsScriptLoadRequest* request = static_cast<nsScriptLoadRequest*>(aContext);
|
2001-05-17 05:20:51 +00:00
|
|
|
NS_ASSERTION(request, "null request in stream complete handler");
|
2006-11-04 05:45:02 +00:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsresult rv = PrepareLoadedRequest(request, aLoader, aStatus, aStringLen,
|
|
|
|
aString);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mPendingRequests.RemoveObject(request);
|
|
|
|
FireScriptAvailable(rv, request);
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
// Process our request and/or any pending ones
|
|
|
|
ProcessPendingRequests();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptLoader::PrepareLoadedRequest(nsScriptLoadRequest* aRequest,
|
|
|
|
nsIStreamLoader* aLoader,
|
|
|
|
nsresult aStatus,
|
|
|
|
PRUint32 aStringLen,
|
|
|
|
const PRUint8* aString)
|
|
|
|
{
|
2001-05-17 05:20:51 +00:00
|
|
|
if (NS_FAILED(aStatus)) {
|
2006-11-04 05:45:02 +00:00
|
|
|
return aStatus;
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have a document, then we need to abort further
|
|
|
|
// evaluation.
|
|
|
|
if (!mDocument) {
|
2006-11-04 05:45:02 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
2002-07-01 23:12:13 +00:00
|
|
|
// If the load returned an error page, then we need to abort
|
|
|
|
nsCOMPtr<nsIRequest> req;
|
2006-11-04 05:45:02 +00:00
|
|
|
nsresult rv = aLoader->GetRequest(getter_AddRefs(req));
|
2002-07-01 23:12:13 +00:00
|
|
|
NS_ASSERTION(req, "StreamLoader's request went away prematurely");
|
2006-11-04 05:45:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(req);
|
2002-07-01 23:12:13 +00:00
|
|
|
if (httpChannel) {
|
|
|
|
PRBool requestSucceeded;
|
|
|
|
rv = httpChannel->GetRequestSucceeded(&requestSucceeded);
|
|
|
|
if (NS_SUCCEEDED(rv) && !requestSucceeded) {
|
2006-11-04 05:45:02 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2002-07-01 23:12:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-12 12:32:05 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(req);
|
2006-11-04 05:45:02 +00:00
|
|
|
if (aStringLen) {
|
2005-06-12 12:32:05 +00:00
|
|
|
// Check the charset attribute to determine script charset.
|
|
|
|
nsAutoString hintCharset;
|
2006-11-04 05:45:02 +00:00
|
|
|
aRequest->mElement->GetScriptCharset(hintCharset);
|
|
|
|
rv = ConvertToUTF16(channel, aString, aStringLen, hintCharset, mDocument,
|
|
|
|
aRequest->mScriptText);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Could not convert external JavaScript to Unicode!");
|
2006-11-04 05:45:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2006-02-14 02:04:20 +00:00
|
|
|
// -- Merge the principal of the script file with that of the document; if
|
|
|
|
// the script has a non-cert principal, the document's principal should be
|
|
|
|
// downgraded.
|
2001-05-17 05:20:51 +00:00
|
|
|
if (channel) {
|
2007-09-26 16:52:33 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> channelPrincipal;
|
|
|
|
nsContentUtils::GetSecurityManager()->
|
|
|
|
GetChannelPrincipal(channel, getter_AddRefs(channelPrincipal));
|
|
|
|
if (channelPrincipal) {
|
2006-04-27 18:21:11 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> newPrincipal =
|
2007-09-26 16:52:33 +00:00
|
|
|
MaybeDowngradeToCodebase(mDocument->NodePrincipal(),
|
|
|
|
channelPrincipal);
|
2006-04-27 18:21:11 +00:00
|
|
|
|
|
|
|
mDocument->SetPrincipal(newPrincipal);
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
// This assertion could fire errorously if we ran out of memory when
|
|
|
|
// inserting the request in the array. However it's an unlikely case
|
|
|
|
// so if you see this assertion it is likely something else that is
|
|
|
|
// wrong, especially if you see it more than once.
|
|
|
|
NS_ASSERTION(mPendingRequests.IndexOf(aRequest) >= 0,
|
|
|
|
"aRequest should be pending!");
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
// Mark this as loaded
|
|
|
|
aRequest->mLoading = PR_FALSE;
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2001-05-25 07:03:04 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|