2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
// vim: ft=cpp tw=78 sw=2 et ts=2
|
|
|
|
/* ***** 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.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla.
|
|
|
|
*
|
|
|
|
* 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):
|
|
|
|
* Vidur Apparao <vidur@netscape.com> (original author)
|
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A class that handles loading and evaluation of <script> elements.
|
|
|
|
*/
|
|
|
|
|
2010-03-03 21:02:55 -08:00
|
|
|
#include "jscntxt.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsScriptLoader.h"
|
|
|
|
#include "nsIDOMCharacterData.h"
|
|
|
|
#include "nsParserUtils.h"
|
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsIContent.h"
|
2010-05-05 11:18:05 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIScriptRuntime.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsIScriptElement.h"
|
|
|
|
#include "nsIDOMHTMLScriptElement.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsContentErrors.h"
|
|
|
|
#include "nsIParser.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2009-01-13 23:13:48 -08:00
|
|
|
#include "nsDocShellCID.h"
|
2010-02-23 13:20:46 -08:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
|
|
|
#include "prlog.h"
|
2010-04-23 12:49:48 -07:00
|
|
|
#include "nsIChannelPolicy.h"
|
|
|
|
#include "nsChannelPolicy.h"
|
2010-08-18 11:34:14 -07:00
|
|
|
#include "nsCRT.h"
|
2010-09-01 04:41:12 -07:00
|
|
|
#include "nsContentCreatorFunctions.h"
|
2010-02-23 13:20:46 -08:00
|
|
|
|
2010-05-19 16:22:19 -07:00
|
|
|
#include "mozilla/FunctionTimer.h"
|
|
|
|
|
2010-02-23 13:20:46 -08:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
static PRLogModuleInfo* gCspPRLog;
|
|
|
|
#endif
|
|
|
|
|
2010-04-30 06:12:05 -07:00
|
|
|
using namespace mozilla::dom;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// Per-request data structure
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class nsScriptLoadRequest : public nsISupports {
|
|
|
|
public:
|
|
|
|
nsScriptLoadRequest(nsIScriptElement* aElement,
|
|
|
|
PRUint32 aVersion)
|
|
|
|
: mElement(aElement),
|
|
|
|
mLoading(PR_TRUE),
|
|
|
|
mIsInline(PR_TRUE),
|
|
|
|
mJSVersion(aVersion), mLineNo(1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
void FireScriptAvailable(nsresult aResult)
|
|
|
|
{
|
|
|
|
mElement->ScriptAvailable(aResult, mElement, mIsInline, mURI, mLineNo);
|
|
|
|
}
|
|
|
|
void FireScriptEvaluated(nsresult aResult)
|
|
|
|
{
|
|
|
|
mElement->ScriptEvaluated(aResult, mElement, mIsInline);
|
|
|
|
}
|
|
|
|
|
2008-09-30 23:48:47 -07:00
|
|
|
PRBool IsPreload()
|
|
|
|
{
|
|
|
|
return mElement == nsnull;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIScriptElement> mElement;
|
|
|
|
PRPackedBool mLoading; // Are we still waiting for a load to complete?
|
|
|
|
PRPackedBool mIsInline; // Is the script inline or loaded?
|
|
|
|
nsString mScriptText; // Holds script for loaded scripts
|
|
|
|
PRUint32 mJSVersion;
|
|
|
|
nsCOMPtr<nsIURI> mURI;
|
2008-11-10 13:09:30 -08:00
|
|
|
nsCOMPtr<nsIURI> mFinalURI;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 mLineNo;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsScriptLoader::nsScriptLoader(nsIDocument *aDocument)
|
|
|
|
: mDocument(aDocument),
|
|
|
|
mBlockerCount(0),
|
2009-01-15 11:49:20 -08:00
|
|
|
mEnabled(PR_TRUE),
|
|
|
|
mDeferEnabled(PR_FALSE),
|
2010-09-01 04:41:12 -07:00
|
|
|
mDocumentParsingDone(PR_FALSE)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-02-23 13:20:46 -08:00
|
|
|
// enable logging for CSP
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gCspPRLog)
|
|
|
|
gCspPRLog = PR_NewLogModule("CSP");
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsScriptLoader::~nsScriptLoader()
|
|
|
|
{
|
|
|
|
mObservers.Clear();
|
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
if (mParserBlockingRequest) {
|
|
|
|
mParserBlockingRequest->FireScriptAvailable(NS_ERROR_ABORT);
|
|
|
|
}
|
|
|
|
|
2010-09-20 05:12:06 -07:00
|
|
|
for (PRUint32 i = 0; i < mDeferRequests.Length(); i++) {
|
2010-09-01 04:41:12 -07:00
|
|
|
mDeferRequests[i]->FireScriptAvailable(NS_ERROR_ABORT);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-09-28 08:15:07 -07:00
|
|
|
|
2010-09-20 05:12:06 -07:00
|
|
|
for (PRUint32 i = 0; i < mAsyncRequests.Length(); i++) {
|
2009-11-09 17:04:24 -08:00
|
|
|
mAsyncRequests[i]->FireScriptAvailable(NS_ERROR_ABORT);
|
|
|
|
}
|
|
|
|
|
2007-09-28 08:15:07 -07: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();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsScriptLoader, nsIStreamLoaderObserver)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
static PRBool
|
|
|
|
IsScriptEventHandler(nsIScriptElement *aScriptElement)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> contElement = do_QueryInterface(aScriptElement);
|
|
|
|
NS_ASSERTION(contElement, "nsIScriptElement isn't nsIContent");
|
|
|
|
|
|
|
|
nsAutoString forAttr, eventAttr;
|
|
|
|
if (!contElement->GetAttr(kNameSpaceID_None, nsGkAtoms::_for, forAttr) ||
|
|
|
|
!contElement->GetAttr(kNameSpaceID_None, nsGkAtoms::event, eventAttr)) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-08-18 11:34:14 -07:00
|
|
|
const nsAString& for_str =
|
|
|
|
nsContentUtils::TrimWhitespace<nsCRT::IsAsciiSpace>(forAttr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!for_str.LowerCaseEqualsLiteral("window")) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We found for="window", now check for event="onload".
|
2010-08-18 11:34:14 -07:00
|
|
|
const nsAString& event_str =
|
|
|
|
nsContentUtils::TrimWhitespace<nsCRT::IsAsciiSpace>(eventAttr, PR_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!StringBeginsWith(event_str, NS_LITERAL_STRING("onload"),
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2008-11-18 23:05:05 -08:00
|
|
|
nsresult
|
2009-02-11 06:51:37 -08:00
|
|
|
nsScriptLoader::CheckContentPolicy(nsIDocument* aDocument,
|
2008-11-18 23:05:05 -08:00
|
|
|
nsISupports *aContext,
|
2009-02-11 06:51:37 -08:00
|
|
|
nsIURI *aURI,
|
2008-11-18 23:05:05 -08:00
|
|
|
const nsAString &aType)
|
|
|
|
{
|
|
|
|
PRInt16 shouldLoad = nsIContentPolicy::ACCEPT;
|
|
|
|
nsresult rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_SCRIPT,
|
2009-02-11 06:51:37 -08:00
|
|
|
aURI,
|
|
|
|
aDocument->NodePrincipal(),
|
2008-11-18 23:05:05 -08:00
|
|
|
aContext,
|
|
|
|
NS_LossyConvertUTF16toASCII(aType),
|
|
|
|
nsnull, //extra
|
|
|
|
&shouldLoad,
|
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
nsContentUtils::GetSecurityManager());
|
|
|
|
if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
|
|
|
|
if (NS_FAILED(rv) || shouldLoad != nsIContentPolicy::REJECT_TYPE) {
|
|
|
|
return NS_ERROR_CONTENT_BLOCKED;
|
|
|
|
}
|
|
|
|
return NS_ERROR_CONTENT_BLOCKED_SHOW_ALT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-30 23:48:47 -07:00
|
|
|
nsresult
|
2009-02-11 06:51:37 -08:00
|
|
|
nsScriptLoader::ShouldLoadScript(nsIDocument* aDocument,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsIURI* aURI,
|
|
|
|
const nsAString &aType)
|
2008-09-30 23:48:47 -07:00
|
|
|
{
|
|
|
|
// Check that the containing page is allowed to load this URI.
|
|
|
|
nsresult rv = nsContentUtils::GetSecurityManager()->
|
2009-02-11 06:51:37 -08:00
|
|
|
CheckLoadURIWithPrincipal(aDocument->NodePrincipal(), aURI,
|
2008-09-30 23:48:47 -07:00
|
|
|
nsIScriptSecurityManager::ALLOW_CHROME);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// After the security manager, the content-policy stuff gets a veto
|
2009-02-11 06:51:37 -08:00
|
|
|
rv = CheckContentPolicy(aDocument, aContext, aURI, aType);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptLoader::StartLoad(nsScriptLoadRequest *aRequest, const nsAString &aType)
|
|
|
|
{
|
2008-11-18 23:05:05 -08:00
|
|
|
nsISupports *context = aRequest->mElement.get()
|
|
|
|
? static_cast<nsISupports *>(aRequest->mElement.get())
|
|
|
|
: static_cast<nsISupports *>(mDocument);
|
2009-02-11 06:51:37 -08:00
|
|
|
nsresult rv = ShouldLoadScript(mDocument, context, aRequest->mURI, aType);
|
2008-11-18 23:05:05 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2008-09-30 23:48:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = mDocument->GetDocumentLoadGroup();
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(mDocument->GetScriptGlobalObject()));
|
2009-10-23 14:50:40 -07:00
|
|
|
if (!window) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2008-09-30 23:48:47 -07:00
|
|
|
nsIDocShell *docshell = window->GetDocShell();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> prompter(do_QueryInterface(docshell));
|
|
|
|
|
2010-04-23 12:49:48 -07:00
|
|
|
// check for a Content Security Policy to pass down to the channel
|
|
|
|
// that will be created to load the script
|
|
|
|
nsCOMPtr<nsIChannelPolicy> channelPolicy;
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
rv = mDocument->NodePrincipal()->GetCsp(getter_AddRefs(csp));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (csp) {
|
|
|
|
channelPolicy = do_CreateInstance("@mozilla.org/nschannelpolicy;1");
|
|
|
|
channelPolicy->SetContentSecurityPolicy(csp);
|
|
|
|
channelPolicy->SetLoadType(nsIContentPolicy::TYPE_SCRIPT);
|
|
|
|
}
|
|
|
|
|
2008-09-30 23:48:47 -07:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
2010-05-21 14:03:02 -07:00
|
|
|
aRequest->mURI, nsnull, loadGroup, prompter,
|
|
|
|
nsIRequest::LOAD_NORMAL | nsIChannel::LOAD_CLASSIFY_URI,
|
2010-04-23 12:49:48 -07:00
|
|
|
channelPolicy);
|
2008-09-30 23:48:47 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), this);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-01-13 23:13:48 -08:00
|
|
|
rv = channel->AsyncOpen(loader, aRequest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-09-30 23:48:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsScriptLoader::PreloadURIComparator::Equals(const PreloadInfo &aPi,
|
|
|
|
nsIURI * const &aURI) const
|
|
|
|
{
|
|
|
|
PRBool same;
|
|
|
|
return NS_SUCCEEDED(aPi.mRequest->mURI->Equals(aURI, &same)) &&
|
|
|
|
same;
|
|
|
|
}
|
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
class nsScriptRequestProcessor : public nsRunnable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsScriptLoader> mLoader;
|
|
|
|
nsRefPtr<nsScriptLoadRequest> mRequest;
|
|
|
|
public:
|
|
|
|
nsScriptRequestProcessor(nsScriptLoader* aLoader,
|
|
|
|
nsScriptLoadRequest* aRequest)
|
|
|
|
: mLoader(aLoader)
|
|
|
|
, mRequest(aRequest)
|
|
|
|
{}
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
return mLoader->ProcessRequest(mRequest);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
|
|
|
|
{
|
|
|
|
// We need a document to evaluate scripts.
|
|
|
|
NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// Check to see if scripts has been turned off.
|
|
|
|
if (!mEnabled || !mDocument->IsScriptEnabled()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!aElement->IsMalformed(), "Executing malformed script");
|
|
|
|
|
|
|
|
// Check that the script is not an eventhandler
|
|
|
|
if (IsScriptEventHandler(aElement)) {
|
|
|
|
return NS_CONTENT_SCRIPT_IS_EVENTHANDLER;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Script evaluation can also be disabled in the current script
|
|
|
|
// context even though it's enabled in the document.
|
|
|
|
// 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.
|
|
|
|
// XXX is this different from the mDocument->IsScriptEnabled() call?
|
|
|
|
nsIScriptGlobalObject *globalObject = mDocument->GetScriptGlobalObject();
|
2007-04-11 13:18:11 -07:00
|
|
|
if (!globalObject) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptContext *context = globalObject->GetScriptContext(
|
|
|
|
nsIProgrammingLanguage::JAVASCRIPT);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-11 13:18:11 -07: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;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-04-30 06:12:05 -07:00
|
|
|
// Default script language is whatever the root element specifies
|
2007-12-01 03:10:12 -08:00
|
|
|
// (which may come from a header or http-meta tag), or if there
|
2010-04-30 06:12:05 -07:00
|
|
|
// is no root element, from the script global object.
|
|
|
|
Element* rootElement = mDocument->GetRootElement();
|
|
|
|
PRUint32 typeID = rootElement ? rootElement->GetScriptTypeID() :
|
2007-12-01 03:10:12 -08:00
|
|
|
context->GetScriptTypeID();
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 version = 0;
|
|
|
|
nsAutoString language, type, src;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// Check the type attribute to determine language and version.
|
|
|
|
// If type exists, it trumps the deprecated 'language='
|
|
|
|
aElement->GetScriptType(type);
|
|
|
|
if (!type.IsEmpty()) {
|
2008-12-16 18:11:07 -08:00
|
|
|
nsContentTypeParser parser(type);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsAutoString mimeType;
|
2008-12-16 18:11:07 -08:00
|
|
|
rv = parser.GetType(mimeType);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Javascript keeps the fast path, optimized for most-likely type
|
|
|
|
// 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
|
|
|
|
};
|
|
|
|
|
|
|
|
PRBool isJavaScript = PR_FALSE;
|
|
|
|
for (PRInt32 i = 0; jsTypes[i]; i++) {
|
|
|
|
if (mimeType.LowerCaseEqualsASCII(jsTypes[i])) {
|
|
|
|
isJavaScript = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
2008-12-16 18:11:07 -08:00
|
|
|
rv = parser.GetParameter("version", versionName);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// no version attribute - version remains 0.
|
|
|
|
if (rv != NS_ERROR_INVALID_ARG)
|
|
|
|
return rv;
|
|
|
|
} else {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some js specifics yet to be abstracted.
|
|
|
|
if (typeID == nsIProgrammingLanguage::JAVASCRIPT) {
|
|
|
|
nsAutoString value;
|
2008-12-16 18:11:07 -08:00
|
|
|
rv = parser.GetParameter("e4x", value);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (rv != NS_ERROR_INVALID_ARG)
|
|
|
|
return rv;
|
|
|
|
} else {
|
|
|
|
if (value.Length() == 1 && value[0] == '1')
|
|
|
|
// 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.
|
2010-09-13 09:38:22 -07:00
|
|
|
version |= js::VersionFlags::HAS_XML;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't know the language, we don't know how to evaluate
|
|
|
|
if (typeID == nsIProgrammingLanguage::UNKNOWN) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
// 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));
|
|
|
|
eltContent->SetScriptTypeID(typeID);
|
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
// Step 9. in the HTML5 spec
|
2008-07-25 19:42:12 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIURI> scriptURI = aElement->GetScriptURI();
|
2008-09-30 23:48:47 -07:00
|
|
|
nsRefPtr<nsScriptLoadRequest> request;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (scriptURI) {
|
2010-09-01 04:41:12 -07:00
|
|
|
// external script
|
2008-09-30 23:48:47 -07:00
|
|
|
nsTArray<PreloadInfo>::index_type i =
|
|
|
|
mPreloads.IndexOf(scriptURI.get(), 0, PreloadURIComparator());
|
|
|
|
if (i != nsTArray<PreloadInfo>::NoIndex) {
|
2010-09-01 04:41:12 -07:00
|
|
|
// preloaded
|
|
|
|
// note that a script-inserted script can steal a preload!
|
2008-09-30 23:48:47 -07:00
|
|
|
request = mPreloads[i].mRequest;
|
|
|
|
request->mElement = aElement;
|
2010-09-01 04:41:12 -07:00
|
|
|
// XXX what if the charset attribute of the element and the charset
|
|
|
|
// of the preload don't match?
|
2008-09-30 23:48:47 -07:00
|
|
|
mPreloads.RemoveElementAt(i);
|
2010-09-27 16:23:33 -07:00
|
|
|
rv = CheckContentPolicy(mDocument, aElement, request->mURI, type);
|
2010-09-01 04:41:12 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
// not preloaded
|
|
|
|
request = new nsScriptLoadRequest(aElement, version);
|
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
request->mURI = scriptURI;
|
|
|
|
request->mIsInline = PR_FALSE;
|
|
|
|
request->mLoading = PR_TRUE;
|
|
|
|
rv = StartLoad(request, type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2010-09-27 16:23:33 -07:00
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
request->mJSVersion = version;
|
|
|
|
|
|
|
|
PRBool async = !aElement->GetParserCreated() || aElement->GetScriptAsync();
|
|
|
|
|
|
|
|
// we now have a request that may or may not be still loading
|
|
|
|
if (!async && aElement->GetScriptDeferred()) {
|
|
|
|
// We don't want to run this yet.
|
|
|
|
// If we come here, the script is a parser-created script and it has
|
|
|
|
// the defer attribute but not the async attribute. Since a
|
|
|
|
// a parser-inserted script is being run, we came here by the parser
|
|
|
|
// running the script, which means the parser is still alive and the
|
|
|
|
// parse is ongoing.
|
|
|
|
NS_ASSERTION(mDocument->GetCurrentContentSink(),
|
|
|
|
"Defer script on a document without an active parser; bug 592366.");
|
2010-09-20 05:12:06 -07:00
|
|
|
mDeferRequests.AppendElement(request);
|
2010-09-01 04:41:12 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (async) {
|
2010-09-20 05:12:06 -07:00
|
|
|
mAsyncRequests.AppendElement(request);
|
2010-09-01 04:41:12 -07:00
|
|
|
if (!request->mLoading) {
|
|
|
|
// The script is available already. Run it ASAP when the event
|
|
|
|
// loop gets a chance to spin.
|
|
|
|
ProcessPendingRequestsAsync();
|
2010-09-27 16:23:33 -07:00
|
|
|
}
|
2010-09-01 04:41:12 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-10-18 06:29:37 -07:00
|
|
|
if (!request->mLoading && ReadyToExecuteScripts()) {
|
|
|
|
// The request has already been loaded and there are no pending style
|
|
|
|
// sheets. If the script comes from the network stream, cheat for
|
|
|
|
// performance reasons and avoid a trip through the event loop.
|
2010-09-01 04:41:12 -07:00
|
|
|
if (aElement->GetParserCreated() == NS_FROM_PARSER_NETWORK) {
|
|
|
|
return ProcessRequest(request);
|
2010-09-27 16:23:33 -07:00
|
|
|
}
|
2010-09-01 04:41:12 -07:00
|
|
|
// Otherwise, we've got a document.written script, make a trip through
|
|
|
|
// the event loop to hide the preload effects from the scripts on the
|
|
|
|
// Web page.
|
|
|
|
NS_ASSERTION(!mParserBlockingRequest,
|
|
|
|
"There can be only one parser-blocking script at a time");
|
|
|
|
mParserBlockingRequest = request;
|
|
|
|
ProcessPendingRequestsAsync();
|
|
|
|
return NS_ERROR_HTMLPARSER_BLOCK;
|
2007-04-11 13:18:11 -07:00
|
|
|
}
|
2010-10-18 06:29:37 -07:00
|
|
|
// The script hasn't loaded yet or there's a style sheet blocking it.
|
|
|
|
// The script will be run when it loads or the style sheet loads.
|
2010-09-01 04:41:12 -07:00
|
|
|
NS_ASSERTION(!mParserBlockingRequest,
|
|
|
|
"There can be only one parser-blocking script at a time");
|
|
|
|
mParserBlockingRequest = request;
|
|
|
|
return NS_ERROR_HTMLPARSER_BLOCK;
|
2008-09-30 23:48:47 -07:00
|
|
|
}
|
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
// inline script
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
rv = mDocument->NodePrincipal()->GetCsp(getter_AddRefs(csp));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-09-27 16:23:33 -07:00
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
if (csp) {
|
|
|
|
PR_LOG(gCspPRLog, PR_LOG_DEBUG, ("New ScriptLoader i ****with CSP****"));
|
|
|
|
PRBool inlineOK;
|
|
|
|
// this call will send violation reports when necessary
|
|
|
|
rv = csp->GetAllowsInlineScript(&inlineOK);
|
2010-02-23 13:20:46 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
if (!inlineOK) {
|
|
|
|
PR_LOG(gCspPRLog, PR_LOG_DEBUG, ("CSP blocked inline scripts (2)"));
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
request = new nsScriptLoadRequest(aElement, version);
|
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
request->mJSVersion = version;
|
|
|
|
request->mLoading = PR_FALSE;
|
|
|
|
request->mIsInline = PR_TRUE;
|
|
|
|
request->mURI = mDocument->GetDocumentURI();
|
|
|
|
request->mLineNo = aElement->GetScriptLineNumber();
|
|
|
|
|
|
|
|
if (aElement->GetParserCreated() == NS_NOT_FROM_PARSER) {
|
|
|
|
NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(),
|
|
|
|
"A script-inserted script is inserted without an update batch?");
|
|
|
|
nsContentUtils::AddScriptRunner(new nsScriptRequestProcessor(this,
|
|
|
|
request));
|
2008-07-25 19:42:12 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-09-01 04:41:12 -07:00
|
|
|
if (aElement->GetParserCreated() == NS_FROM_PARSER_NETWORK &&
|
|
|
|
!ReadyToExecuteScripts()) {
|
|
|
|
NS_ASSERTION(!mParserBlockingRequest,
|
|
|
|
"There can be only one parser-blocking script at a time");
|
|
|
|
mParserBlockingRequest = request;
|
|
|
|
return NS_ERROR_HTMLPARSER_BLOCK;
|
|
|
|
}
|
|
|
|
// We now have a document.written inline script or we have an inline script
|
|
|
|
// from the network but there is no style sheet that is blocking scripts.
|
|
|
|
// Don't check for style sheets blocking scripts in the document.write
|
|
|
|
// case to avoid style sheet network activity affecting when
|
|
|
|
// document.write returns. It's not really necessary to do this if
|
|
|
|
// there's no document.write currently on the call stack. However,
|
|
|
|
// this way matches IE more closely than checking if document.write
|
|
|
|
// is on the call stack.
|
|
|
|
NS_ASSERTION(nsContentUtils::IsSafeToRunScript(),
|
|
|
|
"Not safe to run a parser-inserted script?");
|
|
|
|
return ProcessRequest(request);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptLoader::ProcessRequest(nsScriptLoadRequest* aRequest)
|
|
|
|
{
|
2010-09-01 04:41:12 -07:00
|
|
|
NS_ASSERTION(nsContentUtils::IsSafeToRunScript(),
|
|
|
|
"Processing requests when running scripts is unsafe.");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ENSURE_ARG(aRequest);
|
|
|
|
nsAFlatString* script;
|
|
|
|
nsAutoString textData;
|
|
|
|
|
2010-05-19 16:22:19 -07:00
|
|
|
NS_TIME_FUNCTION;
|
|
|
|
|
2010-06-18 23:38:41 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// 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.
|
|
|
|
aRequest->mElement->GetScriptText(textData);
|
|
|
|
|
|
|
|
script = &textData;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
script = &aRequest->mScriptText;
|
2010-06-18 23:38:41 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> eltAsContent = do_QueryInterface(aRequest->mElement);
|
|
|
|
NS_ASSERTION(eltAsContent, "Script should QI to nsIContent.");
|
|
|
|
doc = eltAsContent->GetOwnerDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
FireScriptAvailable(NS_OK, aRequest);
|
2010-09-16 01:48:47 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> scriptElem = do_QueryInterface(aRequest->mElement);
|
|
|
|
PRBool runScript = PR_TRUE;
|
|
|
|
nsContentUtils::DispatchTrustedEvent(scriptElem->GetOwnerDoc(),
|
|
|
|
scriptElem,
|
|
|
|
NS_LITERAL_STRING("beforescriptexecute"),
|
|
|
|
PR_TRUE, PR_TRUE, &runScript);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (runScript) {
|
|
|
|
aRequest->mElement->BeginEvaluating();
|
|
|
|
if (doc) {
|
|
|
|
doc->BeginEvaluatingExternalScript();
|
|
|
|
}
|
|
|
|
rv = EvaluateScript(aRequest, *script);
|
|
|
|
if (doc) {
|
|
|
|
doc->EndEvaluatingExternalScript();
|
|
|
|
}
|
|
|
|
aRequest->mElement->EndEvaluating();
|
|
|
|
|
|
|
|
nsContentUtils::DispatchTrustedEvent(scriptElem->GetOwnerDoc(),
|
|
|
|
scriptElem,
|
|
|
|
NS_LITERAL_STRING("afterscriptexecute"),
|
|
|
|
PR_TRUE, PR_FALSE);
|
2010-06-18 23:38:41 -07:00
|
|
|
}
|
2010-09-16 01:48:47 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
FireScriptEvaluated(rv, aRequest);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::FireScriptAvailable(nsresult aResult,
|
|
|
|
nsScriptLoadRequest* aRequest)
|
|
|
|
{
|
|
|
|
for (PRInt32 i = 0; i < mObservers.Count(); i++) {
|
|
|
|
nsCOMPtr<nsIScriptLoaderObserver> obs = mObservers[i];
|
|
|
|
obs->ScriptAvailable(aResult, aRequest->mElement,
|
|
|
|
aRequest->mIsInline, aRequest->mURI,
|
|
|
|
aRequest->mLineNo);
|
|
|
|
}
|
|
|
|
|
|
|
|
aRequest->FireScriptAvailable(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::FireScriptEvaluated(nsresult aResult,
|
|
|
|
nsScriptLoadRequest* aRequest)
|
|
|
|
{
|
|
|
|
for (PRInt32 i = 0; i < mObservers.Count(); i++) {
|
|
|
|
nsCOMPtr<nsIScriptLoaderObserver> obs = mObservers[i];
|
|
|
|
obs->ScriptEvaluated(aResult, aRequest->mElement,
|
|
|
|
aRequest->mIsInline);
|
|
|
|
}
|
|
|
|
|
|
|
|
aRequest->FireScriptEvaluated(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptLoader::EvaluateScript(nsScriptLoadRequest* aRequest,
|
|
|
|
const nsAFlatString& aScript)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// We need a document to evaluate scripts.
|
|
|
|
if (!mDocument) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2008-05-28 12:03:04 -07:00
|
|
|
nsPIDOMWindow *pwin = mDocument->GetInnerWindow();
|
|
|
|
if (!pwin || !pwin->IsInnerWindow()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> globalObject = do_QueryInterface(pwin);
|
|
|
|
NS_ASSERTION(globalObject, "windows must be global objects");
|
2007-03-22 10:30:00 -07: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;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
nsCOMPtr<nsIScriptContext> context = globalObject->GetScriptContext(stid);
|
|
|
|
if (!context) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2008-11-10 13:09:30 -08:00
|
|
|
nsIURI* uri = aRequest->mFinalURI ? aRequest->mFinalURI : aRequest->mURI;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PRBool oldProcessingScriptTag = context->GetProcessingScriptTag();
|
|
|
|
context->SetProcessingScriptTag(PR_TRUE);
|
|
|
|
|
|
|
|
// Update our current script.
|
|
|
|
nsCOMPtr<nsIScriptElement> oldCurrent = mCurrentScript;
|
|
|
|
mCurrentScript = aRequest->mElement;
|
|
|
|
|
2008-11-13 16:00:11 -08:00
|
|
|
nsCAutoString url;
|
|
|
|
nsContentUtils::GetWrapperSafeScriptFilename(mDocument, uri, url);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PRBool isUndefined;
|
|
|
|
rv = context->EvaluateString(aScript,
|
|
|
|
globalObject->GetScriptGlobal(stid),
|
|
|
|
mDocument->NodePrincipal(), url.get(),
|
|
|
|
aRequest->mLineNo, aRequest->mJSVersion, nsnull,
|
|
|
|
&isUndefined);
|
|
|
|
|
|
|
|
// Put the old script back in case it wants to do anything else.
|
|
|
|
mCurrentScript = oldCurrent;
|
|
|
|
|
|
|
|
JSContext *cx = nsnull; // Initialize this to keep GCC happy.
|
|
|
|
if (stid == nsIProgrammingLanguage::JAVASCRIPT) {
|
|
|
|
cx = (JSContext *)context->GetNativeContext();
|
|
|
|
::JS_BeginRequest(cx);
|
2009-05-19 19:11:01 -07:00
|
|
|
NS_ASSERTION(!::JS_IsExceptionPending(cx),
|
|
|
|
"JS_ReportPendingException wasn't called in EvaluateString");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
context->SetProcessingScriptTag(oldProcessingScriptTag);
|
|
|
|
|
|
|
|
if (stid == nsIProgrammingLanguage::JAVASCRIPT) {
|
2008-08-30 16:58:36 -07:00
|
|
|
NS_ASSERTION(!::JS_IsExceptionPending(cx),
|
|
|
|
"JS_ReportPendingException wasn't called");
|
2007-03-22 10:30:00 -07:00
|
|
|
::JS_EndRequest(cx);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::ProcessPendingRequestsAsync()
|
|
|
|
{
|
2010-09-01 04:41:12 -07:00
|
|
|
if (mParserBlockingRequest || !mPendingChildLoaders.IsEmpty()) {
|
2010-04-20 16:21:35 -07:00
|
|
|
nsCOMPtr<nsIRunnable> ev = NS_NewRunnableMethod(this,
|
2007-03-22 10:30:00 -07:00
|
|
|
&nsScriptLoader::ProcessPendingRequests);
|
|
|
|
|
|
|
|
NS_DispatchToCurrentThread(ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::ProcessPendingRequests()
|
|
|
|
{
|
2010-09-20 05:12:06 -07:00
|
|
|
nsRefPtr<nsScriptLoadRequest> request;
|
2010-09-01 04:41:12 -07:00
|
|
|
if (mParserBlockingRequest &&
|
|
|
|
!mParserBlockingRequest->mLoading &&
|
|
|
|
ReadyToExecuteScripts()) {
|
|
|
|
request.swap(mParserBlockingRequest);
|
|
|
|
// nsContentSink::ScriptAvailable unblocks the parser
|
|
|
|
ProcessRequest(request);
|
|
|
|
}
|
2010-09-20 15:47:57 -07:00
|
|
|
|
2010-09-20 05:12:06 -07:00
|
|
|
PRUint32 i = 0;
|
|
|
|
while (mEnabled && i < mAsyncRequests.Length()) {
|
2010-09-01 04:41:12 -07:00
|
|
|
if (!mAsyncRequests[i]->mLoading) {
|
2010-09-20 05:12:06 -07:00
|
|
|
request.swap(mAsyncRequests[i]);
|
|
|
|
mAsyncRequests.RemoveElementAt(i);
|
2010-09-01 04:41:12 -07:00
|
|
|
ProcessRequest(request);
|
|
|
|
continue;
|
2010-09-21 12:48:40 -07:00
|
|
|
}
|
2010-09-01 04:41:12 -07:00
|
|
|
++i;
|
|
|
|
}
|
2010-09-27 16:23:33 -07:00
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
if (mDocumentParsingDone) {
|
2010-09-20 05:12:06 -07:00
|
|
|
while (!mDeferRequests.IsEmpty() && !mDeferRequests[0]->mLoading) {
|
|
|
|
request.swap(mDeferRequests[0]);
|
|
|
|
mDeferRequests.RemoveElementAt(0);
|
2010-09-01 04:41:12 -07:00
|
|
|
ProcessRequest(request);
|
|
|
|
}
|
2009-11-09 17:04:24 -08:00
|
|
|
}
|
|
|
|
|
2007-10-07 11:23:52 -07:00
|
|
|
while (!mPendingChildLoaders.IsEmpty() && ReadyToExecuteScripts()) {
|
2007-09-28 08:15:07 -07:00
|
|
|
nsRefPtr<nsScriptLoader> child = mPendingChildLoaders[0];
|
|
|
|
mPendingChildLoaders.RemoveElementAt(0);
|
|
|
|
child->RemoveExecuteBlocker();
|
|
|
|
}
|
2009-01-15 11:49:20 -08:00
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
if (mDocumentParsingDone && mDocument &&
|
2010-09-20 05:12:06 -07:00
|
|
|
!mParserBlockingRequest && mAsyncRequests.IsEmpty() &&
|
|
|
|
mDeferRequests.IsEmpty()) {
|
2009-01-15 11:49:20 -08:00
|
|
|
// No more pending scripts; time to unblock onload.
|
|
|
|
// OK to unblock onload synchronously here, since callers must be
|
|
|
|
// prepared for the world changing anyway.
|
2010-09-01 04:41:12 -07:00
|
|
|
mDocumentParsingDone = PR_FALSE;
|
2009-01-15 11:49:20 -08:00
|
|
|
mDocument->UnblockOnload(PR_TRUE);
|
|
|
|
}
|
2007-09-28 08:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This function was copied from nsParser.cpp. It was simplified a bit.
|
|
|
|
static PRBool
|
|
|
|
DetectByteOrderMark(const unsigned char* aBytes, PRInt32 aLen, nsCString& oCharset)
|
|
|
|
{
|
|
|
|
if (aLen < 2)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
switch(aBytes[0]) {
|
|
|
|
case 0xEF:
|
|
|
|
if (aLen >= 3 && 0xBB == aBytes[1] && 0xBF == aBytes[2]) {
|
|
|
|
// EF BB BF
|
|
|
|
// Win2K UTF-8 BOM
|
|
|
|
oCharset.Assign("UTF-8");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xFE:
|
|
|
|
if (0xFF == aBytes[1]) {
|
|
|
|
// FE FF
|
|
|
|
// UTF-16, big-endian
|
2008-12-06 11:08:26 -08:00
|
|
|
oCharset.Assign("UTF-16");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xFF:
|
|
|
|
if (0xFE == aBytes[1]) {
|
|
|
|
// FF FE
|
|
|
|
// UTF-16, little-endian
|
2008-12-06 11:08:26 -08:00
|
|
|
oCharset.Assign("UTF-16");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return !oCharset.IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2010-05-18 05:28:37 -07:00
|
|
|
if (characterSet.IsEmpty() && aDocument) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// 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 =
|
|
|
|
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
|
|
|
|
|
|
|
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 00:08:04 -07:00
|
|
|
rv = unicodeDecoder->GetMaxLength(reinterpret_cast<const char*>(aData),
|
2007-03-22 10:30:00 -07:00
|
|
|
aLength, &unicodeLength);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (!EnsureStringLength(aString, unicodeLength))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
PRUnichar *ustr = aString.BeginWriting();
|
|
|
|
|
|
|
|
PRInt32 consumedLength = 0;
|
|
|
|
PRInt32 originalLength = aLength;
|
|
|
|
PRInt32 convertedLength = 0;
|
|
|
|
PRInt32 bufferLength = unicodeLength;
|
|
|
|
do {
|
2007-07-08 00:08:04 -07:00
|
|
|
rv = unicodeDecoder->Convert(reinterpret_cast<const char*>(aData),
|
2007-03-22 10:30:00 -07: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptLoader::OnStreamComplete(nsIStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus,
|
|
|
|
PRUint32 aStringLen,
|
|
|
|
const PRUint8* aString)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
nsScriptLoadRequest* request = static_cast<nsScriptLoadRequest*>(aContext);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(request, "null request in stream complete handler");
|
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsresult rv = PrepareLoadedRequest(request, aLoader, aStatus, aStringLen,
|
|
|
|
aString);
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-09-20 05:12:06 -07:00
|
|
|
if (mDeferRequests.RemoveElement(request) ||
|
|
|
|
mAsyncRequests.RemoveElement(request)) {
|
2008-09-30 23:48:47 -07:00
|
|
|
FireScriptAvailable(rv, request);
|
2010-09-01 04:41:12 -07:00
|
|
|
} else if (mParserBlockingRequest == request) {
|
|
|
|
mParserBlockingRequest = nsnull;
|
|
|
|
// nsContentSink::ScriptAvailable unblocks the parser
|
|
|
|
FireScriptAvailable(rv, request);
|
2009-11-09 17:04:24 -08:00
|
|
|
} else {
|
|
|
|
mPreloads.RemoveElement(request, PreloadRequestComparator());
|
2008-09-30 23:48:47 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07: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)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
return aStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have a document, then we need to abort further
|
|
|
|
// evaluation.
|
|
|
|
if (!mDocument) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the load returned an error page, then we need to abort
|
|
|
|
nsCOMPtr<nsIRequest> req;
|
|
|
|
nsresult rv = aLoader->GetRequest(getter_AddRefs(req));
|
|
|
|
NS_ASSERTION(req, "StreamLoader's request went away prematurely");
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(req);
|
|
|
|
if (httpChannel) {
|
|
|
|
PRBool requestSucceeded;
|
|
|
|
rv = httpChannel->GetRequestSucceeded(&requestSucceeded);
|
|
|
|
if (NS_SUCCEEDED(rv) && !requestSucceeded) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(req);
|
2008-11-10 13:09:30 -08:00
|
|
|
NS_GetFinalChannelURI(channel, getter_AddRefs(aRequest->mFinalURI));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aStringLen) {
|
|
|
|
// Check the charset attribute to determine script charset.
|
|
|
|
nsAutoString hintCharset;
|
2008-09-30 23:48:47 -07:00
|
|
|
if (!aRequest->IsPreload()) {
|
|
|
|
aRequest->mElement->GetScriptCharset(hintCharset);
|
|
|
|
} else {
|
|
|
|
nsTArray<PreloadInfo>::index_type i =
|
|
|
|
mPreloads.IndexOf(aRequest, 0, PreloadRequestComparator());
|
|
|
|
NS_ASSERTION(i != mPreloads.NoIndex, "Incorrect preload bookkeeping");
|
|
|
|
hintCharset = mPreloads[i].mCharset;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = ConvertToUTF16(channel, aString, aStringLen, hintCharset, mDocument,
|
|
|
|
aRequest->mScriptText);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-03-24 16:58:42 -07:00
|
|
|
if (!ShouldExecuteScript(mDocument, channel)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07: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.
|
2010-09-01 04:41:12 -07:00
|
|
|
NS_ASSERTION(mDeferRequests.IndexOf(aRequest) >= 0 ||
|
2009-11-09 17:04:24 -08:00
|
|
|
mAsyncRequests.IndexOf(aRequest) >= 0 ||
|
2010-09-01 04:41:12 -07:00
|
|
|
mPreloads.Contains(aRequest, PreloadRequestComparator()) ||
|
|
|
|
mParserBlockingRequest,
|
2007-03-22 10:30:00 -07:00
|
|
|
"aRequest should be pending!");
|
|
|
|
|
|
|
|
// Mark this as loaded
|
|
|
|
aRequest->mLoading = PR_FALSE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-03-21 22:03:57 -07:00
|
|
|
|
|
|
|
/* static */
|
2008-03-24 16:58:42 -07:00
|
|
|
PRBool
|
|
|
|
nsScriptLoader::ShouldExecuteScript(nsIDocument* aDocument,
|
|
|
|
nsIChannel* aChannel)
|
2008-03-21 22:03:57 -07:00
|
|
|
{
|
|
|
|
if (!aChannel) {
|
2008-03-24 16:58:42 -07:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool hasCert;
|
|
|
|
nsIPrincipal* docPrincipal = aDocument->NodePrincipal();
|
|
|
|
docPrincipal->GetHasCertificate(&hasCert);
|
|
|
|
if (!hasCert) {
|
|
|
|
return PR_TRUE;
|
2008-03-21 22:03:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> channelPrincipal;
|
|
|
|
nsresult rv = nsContentUtils::GetSecurityManager()->
|
|
|
|
GetChannelPrincipal(aChannel, getter_AddRefs(channelPrincipal));
|
2008-03-24 16:58:42 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, PR_FALSE);
|
2008-03-21 22:03:57 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(channelPrincipal, "Gotta have a principal here!");
|
|
|
|
|
2008-04-16 09:03:23 -07:00
|
|
|
// If the channel principal isn't at least as powerful as the
|
|
|
|
// document principal, then we don't execute the script.
|
|
|
|
PRBool subsumes;
|
|
|
|
rv = channelPrincipal->Subsumes(docPrincipal, &subsumes);
|
|
|
|
return NS_SUCCEEDED(rv) && subsumes;
|
2008-03-21 22:03:57 -07:00
|
|
|
}
|
2008-07-25 19:42:12 -07:00
|
|
|
|
|
|
|
void
|
2009-07-07 22:55:05 -07:00
|
|
|
nsScriptLoader::ParsingComplete(PRBool aTerminated)
|
2008-07-25 19:42:12 -07:00
|
|
|
{
|
2009-01-15 11:49:20 -08:00
|
|
|
if (mDeferEnabled) {
|
2009-07-07 22:55:05 -07:00
|
|
|
// Have to check because we apparently get ParsingComplete
|
2009-01-15 11:49:20 -08:00
|
|
|
// without BeginDeferringScripts in some cases
|
2010-09-01 04:41:12 -07:00
|
|
|
mDocumentParsingDone = PR_TRUE;
|
2009-01-15 11:49:20 -08:00
|
|
|
}
|
2008-07-25 19:42:12 -07:00
|
|
|
mDeferEnabled = PR_FALSE;
|
2009-07-07 22:55:05 -07:00
|
|
|
if (aTerminated) {
|
2010-09-01 04:41:12 -07:00
|
|
|
mDeferRequests.Clear();
|
|
|
|
mAsyncRequests.Clear();
|
|
|
|
mParserBlockingRequest = nsnull;
|
2008-07-25 19:42:12 -07:00
|
|
|
}
|
|
|
|
|
2009-07-07 22:55:05 -07:00
|
|
|
// Have to call this even if aTerminated so we'll correctly unblock
|
|
|
|
// onload and all.
|
2008-07-25 19:42:12 -07:00
|
|
|
ProcessPendingRequests();
|
|
|
|
}
|
2008-09-30 23:48:47 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::PreloadURI(nsIURI *aURI, const nsAString &aCharset,
|
|
|
|
const nsAString &aType)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsScriptLoadRequest> request = new nsScriptLoadRequest(nsnull, 0);
|
|
|
|
if (!request) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->mURI = aURI;
|
|
|
|
request->mIsInline = PR_FALSE;
|
|
|
|
request->mLoading = PR_TRUE;
|
|
|
|
nsresult rv = StartLoad(request, aType);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PreloadInfo *pi = mPreloads.AppendElement();
|
|
|
|
pi->mRequest = request;
|
|
|
|
pi->mCharset = aCharset;
|
|
|
|
}
|