2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2006-03-31 12:00:42 +04:00
|
|
|
/*
|
|
|
|
* A class that handles loading and evaluation of <script> elements.
|
|
|
|
*/
|
|
|
|
|
2014-03-19 15:38:02 +04:00
|
|
|
#include "nsScriptLoader.h"
|
|
|
|
|
2015-09-22 02:07:01 +03:00
|
|
|
#include "prsystem.h"
|
2012-01-15 12:13:08 +04:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsfriendapi.h"
|
2015-06-27 04:44:14 +03:00
|
|
|
#include "xpcpublic.h"
|
2001-05-17 09:20:51 +04:00
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsIContent.h"
|
2014-03-19 15:38:02 +04:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2010-05-05 22:18:05 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2001-05-17 09:20:51 +04:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
2001-07-16 06:40:48 +04:00
|
|
|
#include "nsIScriptContext.h"
|
2001-05-17 09:20:51 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIPrincipal.h"
|
2013-01-17 06:50:26 +04:00
|
|
|
#include "nsJSPrincipals.h"
|
2001-05-17 09:20:51 +04:00
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
2014-12-06 22:26:50 +03:00
|
|
|
#include "nsIClassOfService.h"
|
2013-10-16 05:35:44 +04:00
|
|
|
#include "nsITimedChannel.h"
|
2001-05-17 09:20:51 +04:00
|
|
|
#include "nsIScriptElement.h"
|
2004-07-07 02:52:03 +04:00
|
|
|
#include "nsIDOMHTMLScriptElement.h"
|
2001-05-17 09:20:51 +04:00
|
|
|
#include "nsIDocShell.h"
|
2003-03-04 15:12:48 +03:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
2003-07-12 02:47:17 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2006-03-22 21:36:36 +03:00
|
|
|
#include "nsIXPConnect.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2007-01-09 04:07:29 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2009-01-14 10:13:48 +03:00
|
|
|
#include "nsDocShellCID.h"
|
2010-02-24 00:20:46 +03:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2010-08-18 22:34:14 +04:00
|
|
|
#include "nsCRT.h"
|
2010-09-01 15:41:12 +04:00
|
|
|
#include "nsContentCreatorFunctions.h"
|
2014-12-31 02:54:59 +03:00
|
|
|
#include "nsCORSListenerProxy.h"
|
2015-02-19 16:52:01 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2012-08-20 22:34:32 +04:00
|
|
|
#include "nsSandboxFlags.h"
|
2013-08-21 23:28:26 +04:00
|
|
|
#include "nsContentTypeParser.h"
|
2014-06-04 00:37:46 +04:00
|
|
|
#include "nsINetworkPredictor.h"
|
2014-10-01 16:13:53 +04:00
|
|
|
#include "ImportManager.h"
|
2013-11-25 12:06:56 +04:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2010-02-24 00:20:46 +03:00
|
|
|
|
2012-06-19 06:30:09 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-04-18 09:03:03 +04:00
|
|
|
#include "mozilla/unused.h"
|
2015-08-13 06:19:11 +03:00
|
|
|
#include "mozilla/dom/SRICheck.h"
|
|
|
|
#include "nsIScriptError.h"
|
2010-05-20 03:22:19 +04:00
|
|
|
|
2010-02-24 00:20:46 +03:00
|
|
|
static PRLogModuleInfo* gCspPRLog;
|
|
|
|
|
2015-08-13 06:19:11 +03:00
|
|
|
static PRLogModuleInfo*
|
|
|
|
GetSriLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *gSriPRLog;
|
|
|
|
if (!gSriPRLog) {
|
|
|
|
gSriPRLog = PR_NewLogModule("SRI");
|
|
|
|
}
|
|
|
|
return gSriPRLog;
|
|
|
|
}
|
|
|
|
|
2012-03-10 20:13:52 +04:00
|
|
|
using namespace mozilla;
|
2010-04-30 17:12:05 +04:00
|
|
|
using namespace mozilla::dom;
|
2003-02-28 08:47:36 +03:00
|
|
|
|
2002-01-08 09:32:36 +03: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.
|
2013-07-19 06:21:19 +04:00
|
|
|
NS_IMPL_ISUPPORTS0(nsScriptLoadRequest)
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
nsScriptLoadRequestList::~nsScriptLoadRequestList()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoadRequestList::Clear()
|
|
|
|
{
|
|
|
|
while (!isEmpty()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoadRequest> first = StealFirst();
|
2015-04-16 20:22:02 +03:00
|
|
|
first->Cancel();
|
2015-04-09 05:50:45 +03:00
|
|
|
// And just let it go out of scope and die.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool
|
|
|
|
nsScriptLoadRequestList::Contains(nsScriptLoadRequest* aElem)
|
|
|
|
{
|
|
|
|
for (nsScriptLoadRequest* req = getFirst();
|
|
|
|
req; req = req->getNext()) {
|
|
|
|
if (req == aElem) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
nsScriptLoader::nsScriptLoader(nsIDocument *aDocument)
|
|
|
|
: mDocument(aDocument),
|
|
|
|
mBlockerCount(0),
|
2015-09-29 17:41:55 +03:00
|
|
|
mNumberOfProcessors(0),
|
2011-10-17 18:59:28 +04:00
|
|
|
mEnabled(true),
|
|
|
|
mDeferEnabled(false),
|
2014-04-12 13:02:21 +04:00
|
|
|
mDocumentParsingDone(false),
|
|
|
|
mBlockingDOMContentLoaded(false)
|
2001-05-17 09:20:51 +04:00
|
|
|
{
|
2010-02-24 00:20:46 +03:00
|
|
|
// enable logging for CSP
|
|
|
|
if (!gCspPRLog)
|
|
|
|
gCspPRLog = PR_NewLogModule("CSP");
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsScriptLoader::~nsScriptLoader()
|
|
|
|
{
|
|
|
|
mObservers.Clear();
|
2005-01-05 10:25:29 +03:00
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
if (mParserBlockingRequest) {
|
|
|
|
mParserBlockingRequest->FireScriptAvailable(NS_ERROR_ABORT);
|
|
|
|
}
|
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
for (nsScriptLoadRequest* req = mXSLTRequests.getFirst(); req;
|
|
|
|
req = req->getNext()) {
|
|
|
|
req->FireScriptAvailable(NS_ERROR_ABORT);
|
2010-10-27 11:18:24 +04:00
|
|
|
}
|
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
for (nsScriptLoadRequest* req = mDeferRequests.getFirst(); req;
|
|
|
|
req = req->getNext()) {
|
|
|
|
req->FireScriptAvailable(NS_ERROR_ABORT);
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
2007-09-28 19:15:07 +04:00
|
|
|
|
2015-04-09 05:50:46 +03:00
|
|
|
for (nsScriptLoadRequest* req = mLoadingAsyncRequests.getFirst(); req;
|
|
|
|
req = req->getNext()) {
|
|
|
|
req->FireScriptAvailable(NS_ERROR_ABORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (nsScriptLoadRequest* req = mLoadedAsyncRequests.getFirst(); req;
|
2015-04-09 05:50:45 +03:00
|
|
|
req = req->getNext()) {
|
|
|
|
req->FireScriptAvailable(NS_ERROR_ABORT);
|
2009-11-10 04:04:24 +03:00
|
|
|
}
|
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
for(nsScriptLoadRequest* req = mNonAsyncExternalScriptInsertedRequests.getFirst();
|
|
|
|
req;
|
|
|
|
req = req->getNext()) {
|
|
|
|
req->FireScriptAvailable(NS_ERROR_ABORT);
|
2010-10-13 11:12:55 +04:00
|
|
|
}
|
|
|
|
|
2007-09-28 19:15:07 +04: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.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t j = 0; j < mPendingChildLoaders.Length(); ++j) {
|
2007-09-28 19:15:07 +04:00
|
|
|
mPendingChildLoaders[j]->RemoveExecuteBlocker();
|
|
|
|
}
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsScriptLoader, nsIStreamLoaderObserver)
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2003-03-04 15:12:48 +03: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.
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2011-11-16 11:50:18 +04:00
|
|
|
IsScriptEventHandler(nsIContent* aScriptElement)
|
2003-03-04 15:12:48 +03:00
|
|
|
{
|
2015-03-03 14:08:59 +03:00
|
|
|
if (!aScriptElement->IsHTMLElement()) {
|
2011-11-16 11:50:18 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-28 15:25:24 +04:00
|
|
|
nsAutoString forAttr, eventAttr;
|
2011-11-16 11:50:18 +04:00
|
|
|
if (!aScriptElement->GetAttr(kNameSpaceID_None, nsGkAtoms::_for, forAttr) ||
|
|
|
|
!aScriptElement->GetAttr(kNameSpaceID_None, nsGkAtoms::event, eventAttr)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-10-28 15:25:24 +04:00
|
|
|
}
|
2003-03-04 15:12:48 +03:00
|
|
|
|
2010-08-18 22:34:14 +04:00
|
|
|
const nsAString& for_str =
|
|
|
|
nsContentUtils::TrimWhitespace<nsCRT::IsAsciiSpace>(forAttr);
|
2004-06-17 04:13:25 +04:00
|
|
|
if (!for_str.LowerCaseEqualsLiteral("window")) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2003-03-04 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We found for="window", now check for event="onload".
|
2010-08-18 22:34:14 +04:00
|
|
|
const nsAString& event_str =
|
2011-10-17 18:59:28 +04:00
|
|
|
nsContentUtils::TrimWhitespace<nsCRT::IsAsciiSpace>(eventAttr, false);
|
2003-07-14 11:37:39 +04:00
|
|
|
if (!StringBeginsWith(event_str, NS_LITERAL_STRING("onload"),
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
2003-03-04 15:12:48 +03:00
|
|
|
// It ain't "onload.*".
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2003-03-04 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2003-03-04 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2003-03-04 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2008-11-19 10:05:05 +03:00
|
|
|
nsresult
|
2009-02-11 17:51:37 +03:00
|
|
|
nsScriptLoader::CheckContentPolicy(nsIDocument* aDocument,
|
2008-11-19 10:05:05 +03:00
|
|
|
nsISupports *aContext,
|
2009-02-11 17:51:37 +03:00
|
|
|
nsIURI *aURI,
|
2015-09-21 00:56:21 +03:00
|
|
|
const nsAString &aType,
|
|
|
|
bool aIsPreLoad)
|
2008-11-19 10:05:05 +03:00
|
|
|
{
|
2015-09-21 00:56:21 +03:00
|
|
|
nsContentPolicyType contentPolicyType = aIsPreLoad
|
|
|
|
? nsIContentPolicy::TYPE_INTERNAL_SCRIPT_PRELOAD
|
|
|
|
: nsIContentPolicy::TYPE_INTERNAL_SCRIPT;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t shouldLoad = nsIContentPolicy::ACCEPT;
|
2015-09-21 00:56:21 +03:00
|
|
|
nsresult rv = NS_CheckContentLoadPolicy(contentPolicyType,
|
2009-02-11 17:51:37 +03:00
|
|
|
aURI,
|
|
|
|
aDocument->NodePrincipal(),
|
2008-11-19 10:05:05 +03:00
|
|
|
aContext,
|
|
|
|
NS_LossyConvertUTF16toASCII(aType),
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, //extra
|
2008-11-19 10:05:05 +03:00
|
|
|
&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-10-01 10:48:47 +04:00
|
|
|
nsresult
|
2009-02-11 17:51:37 +03:00
|
|
|
nsScriptLoader::ShouldLoadScript(nsIDocument* aDocument,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsIURI* aURI,
|
2015-09-21 00:56:21 +03:00
|
|
|
const nsAString &aType,
|
|
|
|
bool aIsPreLoad)
|
2008-10-01 10:48:47 +04:00
|
|
|
{
|
|
|
|
// Check that the containing page is allowed to load this URI.
|
|
|
|
nsresult rv = nsContentUtils::GetSecurityManager()->
|
2009-02-11 17:51:37 +03:00
|
|
|
CheckLoadURIWithPrincipal(aDocument->NodePrincipal(), aURI,
|
2008-10-01 10:48:47 +04:00
|
|
|
nsIScriptSecurityManager::ALLOW_CHROME);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// After the security manager, the content-policy stuff gets a veto
|
2015-09-21 00:56:21 +03:00
|
|
|
rv = CheckContentPolicy(aDocument, aContext, aURI, aType, aIsPreLoad);
|
2009-02-11 17:51:37 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-20 04:33:37 +03:00
|
|
|
class ContextMediator : public nsIStreamLoaderObserver
|
2009-02-11 17:51:37 +03:00
|
|
|
{
|
2015-10-20 04:33:37 +03:00
|
|
|
public:
|
|
|
|
explicit ContextMediator(nsScriptLoader *aScriptLoader, nsISupports *aContext)
|
|
|
|
: mScriptLoader(aScriptLoader)
|
|
|
|
, mContext(aContext) {}
|
2008-10-01 10:48:47 +04:00
|
|
|
|
2015-10-20 04:33:37 +03:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISTREAMLOADEROBSERVER
|
2014-10-14 16:02:15 +04:00
|
|
|
|
2015-10-20 04:33:37 +03:00
|
|
|
private:
|
|
|
|
virtual ~ContextMediator() {}
|
|
|
|
RefPtr<nsScriptLoader> mScriptLoader;
|
|
|
|
nsCOMPtr<nsISupports> mContext;
|
|
|
|
};
|
2009-10-24 01:50:40 +04:00
|
|
|
|
2015-10-20 04:33:37 +03:00
|
|
|
NS_IMPL_ISUPPORTS(ContextMediator, nsIStreamLoaderObserver)
|
2008-10-01 10:48:47 +04:00
|
|
|
|
2015-10-20 04:33:37 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ContextMediator::OnStreamComplete(nsIStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus,
|
|
|
|
uint32_t aStringLen,
|
|
|
|
const uint8_t* aString)
|
|
|
|
{
|
|
|
|
// pass arguments through except for the aContext,
|
|
|
|
// we have to mediate and use mContext instead.
|
|
|
|
return mScriptLoader->OnStreamComplete(aLoader, mContext, aStatus,
|
|
|
|
aStringLen, aString);
|
|
|
|
}
|
2008-10-01 10:48:47 +04:00
|
|
|
|
2015-10-20 04:33:37 +03:00
|
|
|
nsresult
|
|
|
|
nsScriptLoader::StartLoad(nsScriptLoadRequest *aRequest, const nsAString &aType,
|
|
|
|
bool aScriptFromHead)
|
|
|
|
{
|
2012-08-20 22:34:32 +04:00
|
|
|
// If this document is sandboxed without 'allow-scripts', abort.
|
|
|
|
if (mDocument->GetSandboxFlags() & SANDBOXED_SCRIPTS) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-21 00:56:21 +03:00
|
|
|
nsContentPolicyType contentPolicyType = aRequest->IsPreload()
|
|
|
|
? nsIContentPolicy::TYPE_INTERNAL_SCRIPT_PRELOAD
|
|
|
|
: nsIContentPolicy::TYPE_INTERNAL_SCRIPT;
|
2015-10-20 04:33:37 +03:00
|
|
|
nsCOMPtr<nsINode> context;
|
|
|
|
if (aRequest->mElement) {
|
|
|
|
context = do_QueryInterface(aRequest->mElement);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
context = mDocument;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = mDocument->GetDocumentLoadGroup();
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(mDocument->MasterDocument()->GetWindow()));
|
|
|
|
NS_ENSURE_TRUE(window, NS_ERROR_NULL_POINTER);
|
|
|
|
nsIDocShell *docshell = window->GetDocShell();
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> prompter(do_QueryInterface(docshell));
|
|
|
|
|
|
|
|
nsSecurityFlags securityFlags =
|
|
|
|
aRequest->mCORSMode == CORS_NONE
|
|
|
|
? nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL
|
|
|
|
: nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS;
|
|
|
|
if (aRequest->mCORSMode == CORS_USE_CREDENTIALS) {
|
|
|
|
securityFlags |= nsILoadInfo::SEC_REQUIRE_CORS_WITH_CREDENTIALS;
|
|
|
|
}
|
|
|
|
securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
|
2015-09-21 00:56:21 +03:00
|
|
|
|
2008-10-01 10:48:47 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2015-10-20 04:33:37 +03:00
|
|
|
nsresult rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
aRequest->mURI,
|
|
|
|
context,
|
|
|
|
securityFlags,
|
|
|
|
contentPolicyType,
|
|
|
|
loadGroup,
|
|
|
|
prompter,
|
|
|
|
nsIRequest::LOAD_NORMAL |
|
|
|
|
nsIChannel::LOAD_CLASSIFY_URI);
|
2014-09-21 20:36:25 +04:00
|
|
|
|
2008-10-01 10:48:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-12-05 03:06:29 +04:00
|
|
|
nsIScriptElement *script = aRequest->mElement;
|
2014-12-06 22:26:50 +03:00
|
|
|
nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(channel));
|
2014-07-24 22:31:49 +04:00
|
|
|
|
2014-12-06 22:26:50 +03:00
|
|
|
if (cos) {
|
2014-07-24 22:31:49 +04:00
|
|
|
if (aScriptFromHead &&
|
|
|
|
!(script && (script->GetScriptAsync() || script->GetScriptDeferred()))) {
|
|
|
|
// synchronous head scripts block lading of most other non js/css
|
|
|
|
// content such as images
|
2014-12-06 22:26:50 +03:00
|
|
|
cos->AddClassFlags(nsIClassOfService::Leader);
|
2014-07-24 22:31:49 +04:00
|
|
|
} else if (!(script && script->GetScriptDeferred())) {
|
|
|
|
// other scripts are neither blocked nor prioritized unless marked deferred
|
2014-12-06 22:26:50 +03:00
|
|
|
cos->AddClassFlags(nsIClassOfService::Unblocked);
|
2014-07-24 22:31:49 +04:00
|
|
|
}
|
2012-12-05 03:06:29 +04:00
|
|
|
}
|
|
|
|
|
2008-10-01 10:48:47 +04: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("*/*"),
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2014-11-18 16:46:43 +03:00
|
|
|
httpChannel->SetReferrerWithPolicy(mDocument->GetDocumentURI(),
|
|
|
|
aRequest->mReferrerPolicy);
|
2008-10-01 10:48:47 +04:00
|
|
|
}
|
|
|
|
|
2013-10-26 01:56:54 +04:00
|
|
|
nsCOMPtr<nsILoadContext> loadContext(do_QueryInterface(docshell));
|
2014-06-04 00:37:46 +04:00
|
|
|
mozilla::net::PredictorLearn(aRequest->mURI, mDocument->GetDocumentURI(),
|
|
|
|
nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE, loadContext);
|
2013-10-26 01:56:54 +04:00
|
|
|
|
2013-10-16 05:35:44 +04:00
|
|
|
// Set the initiator type
|
|
|
|
nsCOMPtr<nsITimedChannel> timedChannel(do_QueryInterface(httpChannel));
|
|
|
|
if (timedChannel) {
|
|
|
|
timedChannel->SetInitiatorType(NS_LITERAL_STRING("script"));
|
|
|
|
}
|
|
|
|
|
2015-10-20 04:33:37 +03:00
|
|
|
RefPtr<ContextMediator> mediator = new ContextMediator(this, aRequest);
|
2012-03-10 20:13:52 +04:00
|
|
|
|
2015-10-20 04:33:37 +03:00
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), mediator);
|
2009-01-14 10:13:48 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-10-20 04:33:37 +03:00
|
|
|
return channel->AsyncOpen2(loader);
|
2008-10-01 10:48:47 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2008-10-01 10:48:47 +04:00
|
|
|
nsScriptLoader::PreloadURIComparator::Equals(const PreloadInfo &aPi,
|
|
|
|
nsIURI * const &aURI) const
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool same;
|
2008-10-01 10:48:47 +04:00
|
|
|
return NS_SUCCEEDED(aPi.mRequest->mURI->Equals(aURI, &same)) &&
|
|
|
|
same;
|
|
|
|
}
|
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
class nsScriptRequestProcessor : public nsRunnable
|
|
|
|
{
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoader> mLoader;
|
|
|
|
RefPtr<nsScriptLoadRequest> mRequest;
|
2010-09-01 15:41:12 +04:00
|
|
|
public:
|
|
|
|
nsScriptRequestProcessor(nsScriptLoader* aLoader,
|
|
|
|
nsScriptLoadRequest* aRequest)
|
|
|
|
: mLoader(aLoader)
|
|
|
|
, mRequest(aRequest)
|
|
|
|
{}
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
return mLoader->ProcessRequest(mRequest);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-04 11:43:59 +04:00
|
|
|
static inline bool
|
|
|
|
ParseTypeAttribute(const nsAString& aType, JSVersion* aVersion)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aType.IsEmpty());
|
|
|
|
MOZ_ASSERT(aVersion);
|
|
|
|
MOZ_ASSERT(*aVersion == JSVERSION_DEFAULT);
|
|
|
|
|
|
|
|
nsContentTypeParser parser(aType);
|
|
|
|
|
|
|
|
nsAutoString mimeType;
|
|
|
|
nsresult rv = parser.GetType(mimeType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
if (!nsContentUtils::IsJavascriptMIMEType(mimeType)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the version string, and ensure the language supports it.
|
|
|
|
nsAutoString versionName;
|
|
|
|
rv = parser.GetParameter("version", versionName);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*aVersion = nsContentUtils::ParseJavascriptVersion(versionName);
|
|
|
|
} else if (rv != NS_ERROR_INVALID_ARG) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-24 09:27:13 +04:00
|
|
|
static bool
|
2013-11-12 02:25:55 +04:00
|
|
|
CSPAllowsInlineScript(nsIScriptElement *aElement, nsIDocument *aDocument)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
2014-12-11 15:18:10 +03:00
|
|
|
// Note: For imports NodePrincipal and the principal of the master are
|
|
|
|
// the same.
|
2013-11-12 02:25:55 +04:00
|
|
|
nsresult rv = aDocument->NodePrincipal()->GetCsp(getter_AddRefs(csp));
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
if (!csp) {
|
|
|
|
// no CSP --> allow
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-18 08:34:34 +03:00
|
|
|
// query the nonce
|
|
|
|
nsCOMPtr<nsIContent> scriptContent = do_QueryInterface(aElement);
|
2013-11-12 02:25:55 +04:00
|
|
|
nsAutoString nonce;
|
2015-09-18 08:34:34 +03:00
|
|
|
scriptContent->GetAttr(kNameSpaceID_None, nsGkAtoms::nonce, nonce);
|
|
|
|
|
|
|
|
// query the scripttext
|
|
|
|
nsAutoString scriptText;
|
|
|
|
aElement->GetScriptText(scriptText);
|
|
|
|
|
|
|
|
bool allowInlineScript = false;
|
|
|
|
rv = csp->GetAllowsInline(nsIContentPolicy::TYPE_SCRIPT,
|
|
|
|
nonce, scriptText,
|
|
|
|
aElement->GetScriptLineNumber(),
|
|
|
|
&allowInlineScript);
|
|
|
|
return allowInlineScript;
|
2013-11-12 02:25:55 +04:00
|
|
|
}
|
|
|
|
|
2011-11-16 11:50:18 +04:00
|
|
|
bool
|
2006-11-04 08:45:02 +03:00
|
|
|
nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
|
2005-12-02 07:32:18 +03:00
|
|
|
{
|
2001-05-17 09:20:51 +04:00
|
|
|
// We need a document to evaluate scripts.
|
2011-11-16 11:50:18 +04:00
|
|
|
NS_ENSURE_TRUE(mDocument, false);
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
// Check to see if scripts has been turned off.
|
|
|
|
if (!mEnabled || !mDocument->IsScriptEnabled()) {
|
2011-11-16 11:50:18 +04:00
|
|
|
return false;
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
NS_ASSERTION(!aElement->IsMalformed(), "Executing malformed script");
|
|
|
|
|
2011-11-16 11:50:18 +04:00
|
|
|
nsCOMPtr<nsIContent> scriptContent = do_QueryInterface(aElement);
|
|
|
|
|
2011-11-16 11:50:18 +04:00
|
|
|
// Step 12. Check that the script is not an eventhandler
|
2011-11-16 11:50:18 +04:00
|
|
|
if (IsScriptEventHandler(scriptContent)) {
|
2011-11-16 11:50:18 +04:00
|
|
|
return false;
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
2005-01-05 10:25:29 +03:00
|
|
|
|
2012-08-04 11:43:59 +04:00
|
|
|
JSVersion version = JSVERSION_DEFAULT;
|
2005-12-02 07:32:18 +03:00
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
// Check the type attribute to determine language and version.
|
2006-06-13 07:07:47 +04:00
|
|
|
// If type exists, it trumps the deprecated 'language='
|
2012-08-04 11:43:59 +04:00
|
|
|
nsAutoString type;
|
2004-07-07 02:52:03 +04:00
|
|
|
aElement->GetScriptType(type);
|
2001-05-17 09:20:51 +04:00
|
|
|
if (!type.IsEmpty()) {
|
2012-08-04 11:43:59 +04:00
|
|
|
NS_ENSURE_TRUE(ParseTypeAttribute(type, &version), false);
|
2006-06-13 07:07:47 +04:00
|
|
|
} else {
|
|
|
|
// no 'type=' element
|
|
|
|
// "language" is a deprecated attribute of HTML, so we check it only for
|
|
|
|
// HTML script elements.
|
2015-03-03 14:08:59 +03:00
|
|
|
if (scriptContent->IsHTMLElement()) {
|
2012-08-04 11:43:59 +04:00
|
|
|
nsAutoString language;
|
2011-11-16 11:50:18 +04:00
|
|
|
scriptContent->GetAttr(kNameSpaceID_None, nsGkAtoms::language, language);
|
2006-06-13 07:07:47 +04:00
|
|
|
if (!language.IsEmpty()) {
|
2013-02-05 01:37:21 +04:00
|
|
|
if (!nsContentUtils::IsJavaScriptLanguage(language)) {
|
2012-08-04 11:43:59 +04:00
|
|
|
return false;
|
|
|
|
}
|
2005-01-05 10:25:29 +03:00
|
|
|
}
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-16 11:50:18 +04:00
|
|
|
// Step 14. in the HTML5 spec
|
2012-08-04 11:43:59 +04:00
|
|
|
nsresult rv = NS_OK;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoadRequest> request;
|
2010-10-27 11:03:10 +04:00
|
|
|
if (aElement->GetScriptExternal()) {
|
2010-09-01 15:41:12 +04:00
|
|
|
// external script
|
2010-10-27 11:03:10 +04:00
|
|
|
nsCOMPtr<nsIURI> scriptURI = aElement->GetScriptURI();
|
|
|
|
if (!scriptURI) {
|
2012-09-19 07:24:27 +04:00
|
|
|
// Asynchronously report the failure to create a URI object
|
|
|
|
NS_DispatchToCurrentThread(
|
|
|
|
NS_NewRunnableMethod(aElement,
|
|
|
|
&nsIScriptElement::FireErrorEvent));
|
2011-11-16 11:50:18 +04:00
|
|
|
return false;
|
2010-10-27 11:03:10 +04:00
|
|
|
}
|
2014-11-18 16:46:43 +03:00
|
|
|
|
|
|
|
// Double-check that the preload matches what we're asked to load now.
|
|
|
|
mozilla::net::ReferrerPolicy ourRefPolicy = mDocument->GetReferrerPolicy();
|
2012-03-10 20:13:52 +04:00
|
|
|
CORSMode ourCORSMode = aElement->GetCORSMode();
|
2008-10-01 10:48:47 +04:00
|
|
|
nsTArray<PreloadInfo>::index_type i =
|
|
|
|
mPreloads.IndexOf(scriptURI.get(), 0, PreloadURIComparator());
|
|
|
|
if (i != nsTArray<PreloadInfo>::NoIndex) {
|
2010-09-01 15:41:12 +04:00
|
|
|
// preloaded
|
|
|
|
// note that a script-inserted script can steal a preload!
|
2008-10-01 10:48:47 +04:00
|
|
|
request = mPreloads[i].mRequest;
|
|
|
|
request->mElement = aElement;
|
2010-12-07 16:54:50 +03:00
|
|
|
nsString preloadCharset(mPreloads[i].mCharset);
|
2008-10-01 10:48:47 +04:00
|
|
|
mPreloads.RemoveElementAt(i);
|
2010-12-07 16:54:50 +03:00
|
|
|
|
|
|
|
// Double-check that the charset the preload used is the same as
|
|
|
|
// the charset we have now.
|
|
|
|
nsAutoString elementCharset;
|
|
|
|
aElement->GetScriptCharset(elementCharset);
|
2012-03-10 20:13:52 +04:00
|
|
|
if (elementCharset.Equals(preloadCharset) &&
|
2014-11-18 16:46:43 +03:00
|
|
|
ourCORSMode == request->mCORSMode &&
|
|
|
|
ourRefPolicy == request->mReferrerPolicy) {
|
2015-09-21 00:56:21 +03:00
|
|
|
rv = CheckContentPolicy(mDocument, aElement, request->mURI, type, false);
|
2011-11-16 11:50:18 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2010-12-07 16:54:50 +03:00
|
|
|
} else {
|
|
|
|
// Drop the preload
|
2012-07-30 18:20:58 +04:00
|
|
|
request = nullptr;
|
2010-12-07 16:54:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request) {
|
|
|
|
// no usable preload
|
2015-08-13 06:19:11 +03:00
|
|
|
|
|
|
|
SRIMetadata sriMetadata;
|
|
|
|
{
|
|
|
|
nsAutoString integrity;
|
|
|
|
scriptContent->GetAttr(kNameSpaceID_None, nsGkAtoms::integrity,
|
|
|
|
integrity);
|
|
|
|
if (!integrity.IsEmpty()) {
|
|
|
|
MOZ_LOG(GetSriLog(), mozilla::LogLevel::Debug,
|
|
|
|
("nsScriptLoader::ProcessScriptElement, integrity=%s",
|
|
|
|
NS_ConvertUTF16toUTF8(integrity).get()));
|
|
|
|
SRICheck::IntegrityMetadata(integrity, mDocument, &sriMetadata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request = new nsScriptLoadRequest(aElement, version, ourCORSMode,
|
|
|
|
sriMetadata);
|
2010-09-01 15:41:12 +04:00
|
|
|
request->mURI = scriptURI;
|
2011-10-17 18:59:28 +04:00
|
|
|
request->mIsInline = false;
|
2015-08-26 00:52:27 +03:00
|
|
|
request->mProgress = nsScriptLoadRequest::Progress_Loading;
|
2014-11-18 16:46:43 +03:00
|
|
|
request->mReferrerPolicy = ourRefPolicy;
|
2012-12-05 03:06:29 +04:00
|
|
|
|
|
|
|
// set aScriptFromHead to false so we don't treat non preloaded scripts as
|
|
|
|
// blockers for full page load. See bug 792438.
|
|
|
|
rv = StartLoad(request, type, false);
|
2012-09-19 07:24:27 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Asynchronously report the load failure
|
|
|
|
NS_DispatchToCurrentThread(
|
|
|
|
NS_NewRunnableMethod(aElement,
|
|
|
|
&nsIScriptElement::FireErrorEvent));
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-01 15:41:12 +04:00
|
|
|
}
|
2010-09-28 03:23:33 +04:00
|
|
|
|
2015-08-26 00:52:27 +03:00
|
|
|
// Should still be in loading stage of script.
|
|
|
|
NS_ASSERTION(!request->InCompilingStage(),
|
|
|
|
"Request should not yet be in compiling stage.");
|
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
request->mJSVersion = version;
|
|
|
|
|
2010-10-13 11:12:55 +04:00
|
|
|
if (aElement->GetScriptAsync()) {
|
2015-04-09 05:50:45 +03:00
|
|
|
request->mIsAsync = true;
|
2015-08-26 00:52:27 +03:00
|
|
|
if (request->IsDoneLoading()) {
|
2015-04-09 05:50:46 +03:00
|
|
|
mLoadedAsyncRequests.AppendElement(request);
|
2010-10-13 11:12:55 +04:00
|
|
|
// The script is available already. Run it ASAP when the event
|
|
|
|
// loop gets a chance to spin.
|
2015-08-26 00:52:27 +03:00
|
|
|
|
|
|
|
// KVKV TODO: Instead of processing immediately, try off-thread-parsing
|
2015-09-22 02:07:01 +03:00
|
|
|
// it and only schedule a pending ProcessRequest if that fails.
|
2010-10-13 11:12:55 +04:00
|
|
|
ProcessPendingRequestsAsync();
|
2015-04-09 05:50:46 +03:00
|
|
|
} else {
|
|
|
|
mLoadingAsyncRequests.AppendElement(request);
|
2010-10-13 11:12:55 +04:00
|
|
|
}
|
2011-11-16 11:50:18 +04:00
|
|
|
return false;
|
2010-10-13 11:12:55 +04:00
|
|
|
}
|
|
|
|
if (!aElement->GetParserCreated()) {
|
|
|
|
// Violate the HTML5 spec in order to make LABjs and the "order" plug-in
|
|
|
|
// for RequireJS work with their Gecko-sniffed code path. See
|
|
|
|
// http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html
|
2015-04-09 05:50:45 +03:00
|
|
|
request->mIsNonAsyncScriptInserted = true;
|
2010-10-13 11:12:55 +04:00
|
|
|
mNonAsyncExternalScriptInsertedRequests.AppendElement(request);
|
2015-08-26 00:52:27 +03:00
|
|
|
if (request->IsDoneLoading()) {
|
2010-10-13 11:12:55 +04:00
|
|
|
// The script is available already. Run it ASAP when the event
|
|
|
|
// loop gets a chance to spin.
|
|
|
|
ProcessPendingRequestsAsync();
|
|
|
|
}
|
2011-11-16 11:50:18 +04:00
|
|
|
return false;
|
2010-10-13 11:12:55 +04:00
|
|
|
}
|
|
|
|
// we now have a parser-inserted request that may or may not be still
|
|
|
|
// loading
|
|
|
|
if (aElement->GetScriptDeferred()) {
|
2010-09-01 15:41:12 +04:00
|
|
|
// 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.
|
2010-10-27 11:18:24 +04:00
|
|
|
NS_ASSERTION(mDocument->GetCurrentContentSink() ||
|
|
|
|
aElement->GetParserCreated() == FROM_PARSER_XSLT,
|
|
|
|
"Non-XSLT Defer script on a document without an active parser; bug 592366.");
|
2014-04-12 13:02:21 +04:00
|
|
|
AddDeferRequest(request);
|
2011-11-16 11:50:18 +04:00
|
|
|
return false;
|
2010-09-01 15:41:12 +04:00
|
|
|
}
|
2010-10-27 11:18:24 +04:00
|
|
|
|
|
|
|
if (aElement->GetParserCreated() == FROM_PARSER_XSLT) {
|
|
|
|
// Need to maintain order for XSLT-inserted scripts
|
|
|
|
NS_ASSERTION(!mParserBlockingRequest,
|
|
|
|
"Parser-blocking scripts and XSLT scripts in the same doc!");
|
2015-04-09 05:50:45 +03:00
|
|
|
request->mIsXSLT = true;
|
2010-10-27 11:18:24 +04:00
|
|
|
mXSLTRequests.AppendElement(request);
|
2015-08-26 00:52:27 +03:00
|
|
|
if (request->IsDoneLoading()) {
|
2010-10-27 11:18:24 +04:00
|
|
|
// The script is available already. Run it ASAP when the event
|
|
|
|
// loop gets a chance to spin.
|
|
|
|
ProcessPendingRequestsAsync();
|
|
|
|
}
|
2011-11-16 11:50:18 +04:00
|
|
|
return true;
|
2010-10-27 11:18:24 +04:00
|
|
|
}
|
2015-09-22 02:07:01 +03:00
|
|
|
|
2015-08-26 00:52:27 +03:00
|
|
|
if (request->IsDoneLoading() && ReadyToExecuteScripts()) {
|
2015-07-29 01:45:39 +03:00
|
|
|
// 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-10-25 16:17:38 +04:00
|
|
|
if (aElement->GetParserCreated() == FROM_PARSER_NETWORK) {
|
2011-11-16 11:50:18 +04:00
|
|
|
return ProcessRequest(request) == NS_ERROR_HTMLPARSER_BLOCK;
|
2010-09-28 03:23:33 +04:00
|
|
|
}
|
2015-07-29 01:45:39 +03: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.
|
2010-09-01 15:41:12 +04:00
|
|
|
NS_ASSERTION(!mParserBlockingRequest,
|
|
|
|
"There can be only one parser-blocking script at a time");
|
2015-04-09 05:50:45 +03:00
|
|
|
NS_ASSERTION(mXSLTRequests.isEmpty(),
|
2010-10-27 11:18:24 +04:00
|
|
|
"Parser-blocking scripts and XSLT scripts in the same doc!");
|
2010-09-01 15:41:12 +04:00
|
|
|
mParserBlockingRequest = request;
|
|
|
|
ProcessPendingRequestsAsync();
|
2011-11-16 11:50:18 +04:00
|
|
|
return true;
|
2007-04-12 00:18:11 +04:00
|
|
|
}
|
2015-09-22 02:07:01 +03:00
|
|
|
|
2015-07-29 01:45:39 +03: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 15:41:12 +04:00
|
|
|
NS_ASSERTION(!mParserBlockingRequest,
|
|
|
|
"There can be only one parser-blocking script at a time");
|
2015-04-09 05:50:45 +03:00
|
|
|
NS_ASSERTION(mXSLTRequests.isEmpty(),
|
2010-10-27 11:18:24 +04:00
|
|
|
"Parser-blocking scripts and XSLT scripts in the same doc!");
|
2010-09-01 15:41:12 +04:00
|
|
|
mParserBlockingRequest = request;
|
2011-11-16 11:50:18 +04:00
|
|
|
return true;
|
2008-10-01 10:48:47 +04:00
|
|
|
}
|
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
// inline script
|
2012-08-20 22:34:32 +04:00
|
|
|
// Is this document sandboxed without 'allow-scripts'?
|
|
|
|
if (mDocument->GetSandboxFlags() & SANDBOXED_SCRIPTS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-12 02:25:55 +04:00
|
|
|
// Does CSP allow this inline script to run?
|
|
|
|
if (!CSPAllowsInlineScript(aElement, mDocument)) {
|
|
|
|
return false;
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
2005-01-05 10:25:29 +03:00
|
|
|
|
2012-03-10 20:13:52 +04:00
|
|
|
// Inline scripts ignore ther CORS mode and are always CORS_NONE
|
2015-08-13 06:19:11 +03:00
|
|
|
request = new nsScriptLoadRequest(aElement, version, CORS_NONE,
|
|
|
|
SRIMetadata()); // SRI doesn't apply
|
2010-09-01 15:41:12 +04:00
|
|
|
request->mJSVersion = version;
|
2015-08-26 00:52:27 +03:00
|
|
|
request->mProgress = nsScriptLoadRequest::Progress_DoneLoading;
|
2011-10-17 18:59:28 +04:00
|
|
|
request->mIsInline = true;
|
2010-09-01 15:41:12 +04:00
|
|
|
request->mURI = mDocument->GetDocumentURI();
|
|
|
|
request->mLineNo = aElement->GetScriptLineNumber();
|
|
|
|
|
2010-10-27 11:18:24 +04:00
|
|
|
if (aElement->GetParserCreated() == FROM_PARSER_XSLT &&
|
2015-04-09 05:50:45 +03:00
|
|
|
(!ReadyToExecuteScripts() || !mXSLTRequests.isEmpty())) {
|
2010-10-27 11:18:24 +04:00
|
|
|
// Need to maintain order for XSLT-inserted scripts
|
|
|
|
NS_ASSERTION(!mParserBlockingRequest,
|
|
|
|
"Parser-blocking scripts and XSLT scripts in the same doc!");
|
|
|
|
mXSLTRequests.AppendElement(request);
|
2011-11-16 11:50:18 +04:00
|
|
|
return true;
|
2010-10-27 11:18:24 +04:00
|
|
|
}
|
2010-10-25 16:17:38 +04:00
|
|
|
if (aElement->GetParserCreated() == NOT_FROM_PARSER) {
|
2010-09-01 15:41:12 +04:00
|
|
|
NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(),
|
|
|
|
"A script-inserted script is inserted without an update batch?");
|
|
|
|
nsContentUtils::AddScriptRunner(new nsScriptRequestProcessor(this,
|
|
|
|
request));
|
2011-11-16 11:50:18 +04:00
|
|
|
return false;
|
2008-07-26 06:42:12 +04:00
|
|
|
}
|
2010-10-25 16:17:38 +04:00
|
|
|
if (aElement->GetParserCreated() == FROM_PARSER_NETWORK &&
|
2010-09-01 15:41:12 +04:00
|
|
|
!ReadyToExecuteScripts()) {
|
|
|
|
NS_ASSERTION(!mParserBlockingRequest,
|
|
|
|
"There can be only one parser-blocking script at a time");
|
|
|
|
mParserBlockingRequest = request;
|
2015-04-09 05:50:45 +03:00
|
|
|
NS_ASSERTION(mXSLTRequests.isEmpty(),
|
2010-10-27 11:18:24 +04:00
|
|
|
"Parser-blocking scripts and XSLT scripts in the same doc!");
|
2011-11-16 11:50:18 +04:00
|
|
|
return true;
|
2010-09-01 15:41:12 +04:00
|
|
|
}
|
|
|
|
// 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?");
|
2011-11-16 11:50:18 +04:00
|
|
|
return ProcessRequest(request) == NS_ERROR_HTMLPARSER_BLOCK;
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
2013-09-12 03:42:09 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class NotifyOffThreadScriptLoadCompletedRunnable : public nsRunnable
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoadRequest> mRequest;
|
|
|
|
RefPtr<nsScriptLoader> mLoader;
|
2013-12-06 00:55:31 +04:00
|
|
|
void *mToken;
|
2013-09-12 03:42:09 +04:00
|
|
|
|
|
|
|
public:
|
2013-12-06 00:55:31 +04:00
|
|
|
NotifyOffThreadScriptLoadCompletedRunnable(nsScriptLoadRequest* aRequest,
|
|
|
|
nsScriptLoader* aLoader)
|
2014-01-06 19:06:04 +04:00
|
|
|
: mRequest(aRequest), mLoader(aLoader), mToken(nullptr)
|
2013-12-06 00:55:31 +04:00
|
|
|
{}
|
|
|
|
|
2015-02-19 16:52:01 +03:00
|
|
|
virtual ~NotifyOffThreadScriptLoadCompletedRunnable();
|
|
|
|
|
2013-12-06 00:55:31 +04:00
|
|
|
void SetToken(void* aToken) {
|
|
|
|
MOZ_ASSERT(aToken && !mToken);
|
|
|
|
mToken = aToken;
|
|
|
|
}
|
2013-09-12 03:42:09 +04:00
|
|
|
|
2013-12-06 00:55:31 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2013-09-12 03:42:09 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* anonymous namespace */
|
|
|
|
|
|
|
|
nsresult
|
2015-08-26 00:52:27 +03:00
|
|
|
nsScriptLoader::ProcessOffThreadRequest(nsScriptLoadRequest* aRequest)
|
2013-09-12 03:42:09 +04:00
|
|
|
{
|
2015-08-26 00:52:27 +03:00
|
|
|
MOZ_ASSERT(aRequest->mProgress == nsScriptLoadRequest::Progress_Compiling);
|
|
|
|
aRequest->mProgress = nsScriptLoadRequest::Progress_DoneCompiling;
|
2015-09-22 02:07:01 +03:00
|
|
|
if (aRequest == mParserBlockingRequest) {
|
|
|
|
if (!ReadyToExecuteScripts()) {
|
|
|
|
// If not ready to execute scripts, schedule an async call to
|
|
|
|
// ProcessPendingRequests to handle it.
|
|
|
|
ProcessPendingRequestsAsync();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same logic as in top of ProcessPendingRequests.
|
|
|
|
mParserBlockingRequest = nullptr;
|
|
|
|
UnblockParser(aRequest);
|
|
|
|
ProcessRequest(aRequest);
|
|
|
|
mDocument->UnblockOnload(false);
|
|
|
|
ContinueParserAsync(aRequest);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-26 00:52:27 +03:00
|
|
|
nsresult rv = ProcessRequest(aRequest);
|
2013-12-06 00:55:31 +04:00
|
|
|
mDocument->UnblockOnload(false);
|
|
|
|
return rv;
|
2013-09-12 03:42:09 +04:00
|
|
|
}
|
|
|
|
|
2015-02-19 16:52:01 +03:00
|
|
|
NotifyOffThreadScriptLoadCompletedRunnable::~NotifyOffThreadScriptLoadCompletedRunnable()
|
|
|
|
{
|
|
|
|
if (MOZ_UNLIKELY(mRequest || mLoader) && !NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
if (mainThread) {
|
|
|
|
NS_ProxyRelease(mainThread, mRequest);
|
|
|
|
NS_ProxyRelease(mainThread, mLoader);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false, "We really shouldn't leak!");
|
|
|
|
// Better to leak than crash.
|
|
|
|
unused << mRequest.forget();
|
|
|
|
unused << mLoader.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 03:42:09 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
NotifyOffThreadScriptLoadCompletedRunnable::Run()
|
|
|
|
{
|
2013-12-06 00:55:31 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-09-15 01:06:56 +04:00
|
|
|
|
2014-12-20 05:16:17 +03:00
|
|
|
// We want these to be dropped on the main thread, once we return from this
|
|
|
|
// function.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoadRequest> request = mRequest.forget();
|
|
|
|
RefPtr<nsScriptLoader> loader = mLoader.forget();
|
2014-12-20 05:16:17 +03:00
|
|
|
|
2015-08-26 00:52:27 +03:00
|
|
|
request->mOffThreadToken = mToken;
|
|
|
|
nsresult rv = loader->ProcessOffThreadRequest(request);
|
2013-12-06 00:55:31 +04:00
|
|
|
|
|
|
|
return rv;
|
2013-09-12 03:42:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OffThreadScriptLoaderCallback(void *aToken, void *aCallbackData)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<NotifyOffThreadScriptLoadCompletedRunnable> aRunnable =
|
2014-04-18 09:03:03 +04:00
|
|
|
dont_AddRef(static_cast<NotifyOffThreadScriptLoadCompletedRunnable*>(aCallbackData));
|
2013-12-06 00:55:31 +04:00
|
|
|
aRunnable->SetToken(aToken);
|
|
|
|
NS_DispatchToMainThread(aRunnable);
|
2013-09-12 03:42:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-09-22 02:07:01 +03:00
|
|
|
nsScriptLoader::AttemptAsyncScriptCompile(nsScriptLoadRequest* aRequest)
|
2013-09-12 03:42:09 +04:00
|
|
|
{
|
2015-09-22 02:07:01 +03:00
|
|
|
// Don't off-thread compile inline scripts.
|
|
|
|
if (aRequest->mIsInline) {
|
2013-09-12 03:42:09 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-12-31 20:30:07 +04:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> globalObject = GetScriptGlobalObject();
|
|
|
|
if (!globalObject) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-06-19 11:21:14 +04:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (!jsapi.InitWithLegacyErrorReporting(globalObject)) {
|
2013-09-12 03:42:09 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-12-12 04:26:37 +04:00
|
|
|
|
2014-05-27 19:44:51 +04:00
|
|
|
JSContext* cx = jsapi.cx();
|
2013-12-31 20:30:07 +04:00
|
|
|
JS::Rooted<JSObject*> global(cx, globalObject->GetGlobalJSObject());
|
2013-09-12 03:42:09 +04:00
|
|
|
JS::CompileOptions options(cx);
|
2014-07-03 22:12:03 +04:00
|
|
|
FillCompileOptionsForRequest(jsapi, aRequest, global, &options);
|
2013-09-12 03:42:09 +04:00
|
|
|
|
2014-04-25 18:11:56 +04:00
|
|
|
if (!JS::CanCompileOffThread(cx, options, aRequest->mScriptTextLength)) {
|
2013-09-12 03:42:09 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<NotifyOffThreadScriptLoadCompletedRunnable> runnable =
|
2013-12-06 00:55:31 +04:00
|
|
|
new NotifyOffThreadScriptLoadCompletedRunnable(aRequest, this);
|
|
|
|
|
2014-04-11 19:47:43 +04:00
|
|
|
if (!JS::CompileOffThread(cx, options,
|
2014-04-25 18:11:56 +04:00
|
|
|
aRequest->mScriptTextBuf, aRequest->mScriptTextLength,
|
2013-09-12 03:42:09 +04:00
|
|
|
OffThreadScriptLoaderCallback,
|
2013-12-06 00:55:31 +04:00
|
|
|
static_cast<void*>(runnable))) {
|
2013-09-12 03:42:09 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDocument->BlockOnload();
|
2015-08-26 00:52:27 +03:00
|
|
|
aRequest->mProgress = nsScriptLoadRequest::Progress_Compiling;
|
2013-09-12 03:42:09 +04:00
|
|
|
|
2014-04-18 09:03:03 +04:00
|
|
|
unused << runnable.forget();
|
2013-09-12 03:42:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
nsresult
|
2015-09-22 02:07:01 +03:00
|
|
|
nsScriptLoader::CompileOffThreadOrProcessRequest(nsScriptLoadRequest* aRequest,
|
|
|
|
bool* oCompiledOffThread)
|
2001-05-17 09:20:51 +04:00
|
|
|
{
|
2010-09-01 15:41:12 +04:00
|
|
|
NS_ASSERTION(nsContentUtils::IsSafeToRunScript(),
|
|
|
|
"Processing requests when running scripts is unsafe.");
|
2015-08-26 00:52:27 +03:00
|
|
|
NS_ASSERTION(!aRequest->mOffThreadToken,
|
2015-08-26 00:52:27 +03:00
|
|
|
"Candidate for off-thread compile is already parsed off-thread");
|
|
|
|
NS_ASSERTION(!aRequest->InCompilingStage(),
|
|
|
|
"Candidate for off-thread compile is already in compiling stage.");
|
2015-07-29 00:03:56 +03:00
|
|
|
|
2015-09-22 02:07:01 +03:00
|
|
|
nsresult rv = AttemptAsyncScriptCompile(aRequest);
|
2015-08-26 00:52:27 +03:00
|
|
|
if (rv != NS_ERROR_FAILURE) {
|
2015-09-22 02:07:01 +03:00
|
|
|
if (oCompiledOffThread && rv == NS_OK) {
|
|
|
|
*oCompiledOffThread = true;
|
|
|
|
}
|
2015-08-26 00:52:27 +03:00
|
|
|
return rv;
|
2015-07-29 01:45:39 +03:00
|
|
|
}
|
2015-07-29 00:03:56 +03:00
|
|
|
|
2015-08-26 00:52:27 +03:00
|
|
|
return ProcessRequest(aRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptLoader::ProcessRequest(nsScriptLoadRequest* aRequest)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(nsContentUtils::IsSafeToRunScript(),
|
|
|
|
"Processing requests when running scripts is unsafe.");
|
2015-08-26 00:52:27 +03:00
|
|
|
NS_ASSERTION(aRequest->IsReadyToRun(),
|
|
|
|
"Processing a request that is not ready to run.");
|
2015-08-26 00:52:27 +03:00
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
NS_ENSURE_ARG(aRequest);
|
|
|
|
nsAutoString textData;
|
2014-07-22 08:43:21 +04:00
|
|
|
const char16_t* scriptBuf = nullptr;
|
2014-04-25 18:11:56 +04:00
|
|
|
size_t scriptLength = 0;
|
|
|
|
JS::SourceBufferHolder::Ownership giveScriptOwnership =
|
|
|
|
JS::SourceBufferHolder::NoOwnership;
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2010-06-19 10:38:41 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
|
2010-09-06 11:41:26 +04:00
|
|
|
nsCOMPtr<nsINode> scriptElem = do_QueryInterface(aRequest->mElement);
|
|
|
|
|
2001-05-17 09:20:51 +04: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.
|
2004-07-07 02:52:03 +04:00
|
|
|
aRequest->mElement->GetScriptText(textData);
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2014-04-25 18:11:56 +04:00
|
|
|
scriptBuf = textData.get();
|
|
|
|
scriptLength = textData.Length();
|
|
|
|
giveScriptOwnership = JS::SourceBufferHolder::NoOwnership;
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
else {
|
2014-04-25 18:11:56 +04:00
|
|
|
scriptBuf = aRequest->mScriptTextBuf;
|
|
|
|
scriptLength = aRequest->mScriptTextLength;
|
|
|
|
|
|
|
|
giveScriptOwnership = JS::SourceBufferHolder::GiveOwnership;
|
|
|
|
aRequest->mScriptTextBuf = nullptr;
|
|
|
|
aRequest->mScriptTextLength = 0;
|
2010-06-19 10:38:41 +04:00
|
|
|
|
2011-10-18 14:53:36 +04:00
|
|
|
doc = scriptElem->OwnerDoc();
|
2010-09-06 11:41:26 +04:00
|
|
|
}
|
|
|
|
|
2014-04-25 18:11:56 +04:00
|
|
|
JS::SourceBufferHolder srcBuf(scriptBuf, scriptLength, giveScriptOwnership);
|
|
|
|
|
2010-09-06 11:41:26 +04:00
|
|
|
nsCOMPtr<nsIScriptElement> oldParserInsertedScript;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t parserCreated = aRequest->mElement->GetParserCreated();
|
2010-09-06 11:41:26 +04:00
|
|
|
if (parserCreated) {
|
|
|
|
oldParserInsertedScript = mCurrentParserInsertedScript;
|
|
|
|
mCurrentParserInsertedScript = aRequest->mElement;
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
FireScriptAvailable(NS_OK, aRequest);
|
2010-09-16 12:48:47 +04:00
|
|
|
|
2013-06-16 06:06:27 +04:00
|
|
|
// The window may have gone away by this point, in which case there's no point
|
|
|
|
// in trying to run the script.
|
2014-05-21 21:08:12 +04:00
|
|
|
nsCOMPtr<nsIDocument> master = mDocument->MasterDocument();
|
|
|
|
nsPIDOMWindow *pwin = master->GetInnerWindow();
|
2013-06-16 06:06:27 +04:00
|
|
|
bool runScript = !!pwin;
|
|
|
|
if (runScript) {
|
|
|
|
nsContentUtils::DispatchTrustedEvent(scriptElem->OwnerDoc(),
|
|
|
|
scriptElem,
|
|
|
|
NS_LITERAL_STRING("beforescriptexecute"),
|
|
|
|
true, true, &runScript);
|
|
|
|
}
|
2010-09-16 12:48:47 +04:00
|
|
|
|
2013-08-02 21:55:50 +04:00
|
|
|
// Inner window could have gone away after firing beforescriptexecute
|
2014-05-21 21:08:12 +04:00
|
|
|
pwin = master->GetInnerWindow();
|
2013-08-02 21:55:50 +04:00
|
|
|
if (!pwin) {
|
|
|
|
runScript = false;
|
|
|
|
}
|
|
|
|
|
2010-09-16 12:48:47 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (runScript) {
|
|
|
|
if (doc) {
|
|
|
|
doc->BeginEvaluatingExternalScript();
|
|
|
|
}
|
2010-09-06 11:41:26 +04:00
|
|
|
aRequest->mElement->BeginEvaluating();
|
2015-08-26 00:52:27 +03:00
|
|
|
rv = EvaluateScript(aRequest, srcBuf);
|
2010-09-06 11:41:26 +04:00
|
|
|
aRequest->mElement->EndEvaluating();
|
2010-09-16 12:48:47 +04:00
|
|
|
if (doc) {
|
|
|
|
doc->EndEvaluatingExternalScript();
|
|
|
|
}
|
|
|
|
|
2011-10-18 14:53:36 +04:00
|
|
|
nsContentUtils::DispatchTrustedEvent(scriptElem->OwnerDoc(),
|
2010-09-16 12:48:47 +04:00
|
|
|
scriptElem,
|
|
|
|
NS_LITERAL_STRING("afterscriptexecute"),
|
2011-10-17 18:59:28 +04:00
|
|
|
true, false);
|
2010-06-19 10:38:41 +04:00
|
|
|
}
|
2010-09-16 12:48:47 +04:00
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
FireScriptEvaluated(rv, aRequest);
|
2003-11-13 07:15:25 +03:00
|
|
|
|
2010-09-06 11:41:26 +04:00
|
|
|
if (parserCreated) {
|
|
|
|
mCurrentParserInsertedScript = oldParserInsertedScript;
|
|
|
|
}
|
|
|
|
|
2015-08-26 00:52:27 +03:00
|
|
|
if (aRequest->mOffThreadToken) {
|
|
|
|
// The request was parsed off-main-thread, but the result of the off
|
|
|
|
// thread parse was not actually needed to process the request
|
|
|
|
// (disappearing window, some other error, ...). Finish the
|
|
|
|
// request to avoid leaks in the JS engine.
|
|
|
|
JS::FinishOffThreadScript(nullptr, xpc::GetJSRuntime(), aRequest->mOffThreadToken);
|
|
|
|
aRequest->mOffThreadToken = nullptr;
|
|
|
|
}
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::FireScriptAvailable(nsresult aResult,
|
2006-11-04 08:45:02 +03:00
|
|
|
nsScriptLoadRequest* aRequest)
|
2001-05-17 09:20:51 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < mObservers.Count(); i++) {
|
2006-11-04 08:45:02 +03:00
|
|
|
nsCOMPtr<nsIScriptLoaderObserver> obs = mObservers[i];
|
|
|
|
obs->ScriptAvailable(aResult, aRequest->mElement,
|
2007-03-01 11:11:40 +03:00
|
|
|
aRequest->mIsInline, aRequest->mURI,
|
|
|
|
aRequest->mLineNo);
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
aRequest->FireScriptAvailable(aResult);
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::FireScriptEvaluated(nsresult aResult,
|
|
|
|
nsScriptLoadRequest* aRequest)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < mObservers.Count(); i++) {
|
2006-11-04 08:45:02 +03:00
|
|
|
nsCOMPtr<nsIScriptLoaderObserver> obs = mObservers[i];
|
|
|
|
obs->ScriptEvaluated(aResult, aRequest->mElement,
|
2007-03-01 11:11:40 +03:00
|
|
|
aRequest->mIsInline);
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
2005-01-05 10:25:29 +03:00
|
|
|
aRequest->FireScriptEvaluated(aResult);
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
2013-12-31 20:30:07 +04:00
|
|
|
already_AddRefed<nsIScriptGlobalObject>
|
|
|
|
nsScriptLoader::GetScriptGlobalObject()
|
2013-09-12 03:42:09 +04:00
|
|
|
{
|
2014-05-21 21:08:12 +04:00
|
|
|
nsCOMPtr<nsIDocument> master = mDocument->MasterDocument();
|
|
|
|
nsPIDOMWindow *pwin = master->GetInnerWindow();
|
2013-09-13 03:11:17 +04:00
|
|
|
if (!pwin) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-09-12 03:42:09 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> globalObject = do_QueryInterface(pwin);
|
|
|
|
NS_ASSERTION(globalObject, "windows must be global objects");
|
|
|
|
|
|
|
|
// and make sure we are setup for this type of script.
|
|
|
|
nsresult rv = globalObject->EnsureScriptEnvironment();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-31 20:30:07 +04:00
|
|
|
return globalObject.forget();
|
2013-09-12 03:42:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-03 22:12:03 +04:00
|
|
|
nsScriptLoader::FillCompileOptionsForRequest(const AutoJSAPI &jsapi,
|
|
|
|
nsScriptLoadRequest *aRequest,
|
2013-11-26 20:47:52 +04:00
|
|
|
JS::Handle<JSObject *> aScopeChain,
|
2013-09-12 03:42:09 +04:00
|
|
|
JS::CompileOptions *aOptions)
|
|
|
|
{
|
|
|
|
// It's very important to use aRequest->mURI, not the final URI of the channel
|
|
|
|
// aRequest ended up getting script data from, as the script filename.
|
|
|
|
nsContentUtils::GetWrapperSafeScriptFilename(mDocument, aRequest->mURI, aRequest->mURL);
|
|
|
|
|
2014-03-13 00:10:02 +04:00
|
|
|
aOptions->setIntroductionType("scriptElement");
|
2013-09-12 03:42:09 +04:00
|
|
|
aOptions->setFileAndLine(aRequest->mURL.get(), aRequest->mLineNo);
|
|
|
|
aOptions->setVersion(JSVersion(aRequest->mJSVersion));
|
2015-04-01 19:05:28 +03:00
|
|
|
aOptions->setIsRunOnce(true);
|
2014-11-18 19:01:09 +03:00
|
|
|
// We only need the setNoScriptRval bit when compiling off-thread here, since
|
|
|
|
// otherwise nsJSUtils::EvaluateString will set it up for us.
|
|
|
|
aOptions->setNoScriptRval(true);
|
2013-09-17 13:43:23 +04:00
|
|
|
if (aRequest->mHasSourceMapURL) {
|
|
|
|
aOptions->setSourceMapURL(aRequest->mSourceMapURL.get());
|
|
|
|
}
|
2013-09-12 03:42:09 +04:00
|
|
|
if (aRequest->mOriginPrincipal) {
|
2014-09-29 12:44:30 +04:00
|
|
|
nsIPrincipal* scriptPrin = nsContentUtils::ObjectPrincipal(aScopeChain);
|
|
|
|
bool subsumes = scriptPrin->Subsumes(aRequest->mOriginPrincipal);
|
|
|
|
aOptions->setMutedErrors(!subsumes);
|
2013-09-12 03:42:09 +04:00
|
|
|
}
|
2013-11-26 20:47:52 +04:00
|
|
|
|
2014-07-03 22:12:03 +04:00
|
|
|
JSContext* cx = jsapi.cx();
|
2013-11-26 20:47:52 +04:00
|
|
|
JS::Rooted<JS::Value> elementVal(cx);
|
|
|
|
MOZ_ASSERT(aRequest->mElement);
|
2014-04-10 08:58:41 +04:00
|
|
|
if (NS_SUCCEEDED(nsContentUtils::WrapNative(cx, aRequest->mElement,
|
|
|
|
&elementVal,
|
2013-11-26 20:47:52 +04:00
|
|
|
/* aAllowWrapping = */ true))) {
|
|
|
|
MOZ_ASSERT(elementVal.isObject());
|
|
|
|
aOptions->setElement(&elementVal.toObject());
|
|
|
|
}
|
2013-09-12 03:42:09 +04:00
|
|
|
}
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
nsresult
|
|
|
|
nsScriptLoader::EvaluateScript(nsScriptLoadRequest* aRequest,
|
2015-08-26 00:52:27 +03:00
|
|
|
JS::SourceBufferHolder& aSrcBuf)
|
2001-05-17 09:20:51 +04:00
|
|
|
{
|
2001-09-26 02:16:41 +04:00
|
|
|
// We need a document to evaluate scripts.
|
|
|
|
if (!mDocument) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-12-08 15:37:19 +03:00
|
|
|
nsCOMPtr<nsIContent> scriptContent(do_QueryInterface(aRequest->mElement));
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* ownerDoc = scriptContent->OwnerDoc();
|
2010-12-08 15:37:19 +03:00
|
|
|
if (ownerDoc != mDocument) {
|
|
|
|
// Willful violation of HTML5 as of 2010-12-01
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2006-06-13 07:07:47 +04:00
|
|
|
// Get the script-type to be used by this element.
|
|
|
|
NS_ASSERTION(scriptContent, "no content - what is default script-type?");
|
2012-04-01 16:47:48 +04:00
|
|
|
|
2013-12-31 20:30:07 +04:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> globalObject = GetScriptGlobalObject();
|
|
|
|
if (!globalObject) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-02-26 05:35:30 +03: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.
|
2013-12-31 20:30:07 +04:00
|
|
|
nsCOMPtr<nsIScriptContext> context = globalObject->GetScriptContext();
|
2004-02-10 01:48:53 +03:00
|
|
|
if (!context) {
|
2001-05-17 09:20:51 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-12-12 04:26:37 +04:00
|
|
|
|
2014-03-28 00:05:15 +04:00
|
|
|
JSVersion version = JSVersion(aRequest->mJSVersion);
|
|
|
|
if (version == JSVERSION_UNKNOWN) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-19 15:38:02 +04:00
|
|
|
// New script entry point required, due to the "Create a script" sub-step of
|
|
|
|
// http://www.whatwg.org/specs/web-apps/current-work/#execute-the-script-block
|
2015-07-09 09:41:43 +03:00
|
|
|
nsAutoMicroTask mt;
|
2015-04-09 04:23:48 +03:00
|
|
|
AutoEntryScript entryScript(globalObject, "<script> element", true,
|
|
|
|
context->GetNativeContext());
|
2015-06-16 03:11:06 +03:00
|
|
|
entryScript.TakeOwnershipOfErrorReporting();
|
2014-03-19 15:38:02 +04:00
|
|
|
JS::Rooted<JSObject*> global(entryScript.cx(),
|
|
|
|
globalObject->GetGlobalJSObject());
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2013-01-15 15:46:35 +04:00
|
|
|
bool oldProcessingScriptTag = context->GetProcessingScriptTag();
|
|
|
|
context->SetProcessingScriptTag(true);
|
2014-05-21 21:08:12 +04:00
|
|
|
nsresult rv;
|
|
|
|
{
|
|
|
|
// Update our current script.
|
|
|
|
AutoCurrentScriptUpdater scriptUpdater(this, aRequest->mElement);
|
|
|
|
Maybe<AutoCurrentScriptUpdater> masterScriptUpdater;
|
|
|
|
nsCOMPtr<nsIDocument> master = mDocument->MasterDocument();
|
|
|
|
if (master != mDocument) {
|
|
|
|
// If this script belongs to an import document, it will be
|
|
|
|
// executed in the context of the master document. During the
|
|
|
|
// execution currentScript of the master should refer to this
|
|
|
|
// script. So let's update the mCurrentScript of the ScriptLoader
|
|
|
|
// of the master document too.
|
2014-08-14 02:39:41 +04:00
|
|
|
masterScriptUpdater.emplace(master->ScriptLoader(),
|
|
|
|
aRequest->mElement);
|
2014-05-21 21:08:12 +04:00
|
|
|
}
|
2013-01-15 15:46:35 +04:00
|
|
|
|
2014-05-21 21:08:12 +04:00
|
|
|
JS::CompileOptions options(entryScript.cx());
|
2014-07-03 22:12:03 +04:00
|
|
|
FillCompileOptionsForRequest(entryScript, aRequest, global, &options);
|
2014-05-21 21:08:12 +04:00
|
|
|
rv = nsJSUtils::EvaluateString(entryScript.cx(), aSrcBuf, global, options,
|
2015-08-26 00:52:27 +03:00
|
|
|
aRequest->OffThreadTokenPtr());
|
2014-05-21 21:08:12 +04:00
|
|
|
}
|
2005-06-07 23:21:05 +04:00
|
|
|
|
2013-01-15 15:46:35 +04:00
|
|
|
context->SetProcessingScriptTag(oldProcessingScriptTag);
|
2001-05-17 09:20:51 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-01-09 04:07:29 +03:00
|
|
|
void
|
|
|
|
nsScriptLoader::ProcessPendingRequestsAsync()
|
|
|
|
{
|
2010-09-01 15:41:12 +04:00
|
|
|
if (mParserBlockingRequest || !mPendingChildLoaders.IsEmpty()) {
|
2010-04-21 03:21:35 +04:00
|
|
|
nsCOMPtr<nsIRunnable> ev = NS_NewRunnableMethod(this,
|
2007-01-09 04:07:29 +03:00
|
|
|
&nsScriptLoader::ProcessPendingRequests);
|
|
|
|
|
|
|
|
NS_DispatchToCurrentThread(ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
void
|
2006-11-04 08:45:02 +03:00
|
|
|
nsScriptLoader::ProcessPendingRequests()
|
2001-05-17 09:20:51 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoadRequest> request;
|
2015-08-26 00:52:27 +03:00
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
if (mParserBlockingRequest &&
|
2015-08-26 00:52:27 +03:00
|
|
|
mParserBlockingRequest->IsReadyToRun() &&
|
2010-09-01 15:41:12 +04:00
|
|
|
ReadyToExecuteScripts()) {
|
|
|
|
request.swap(mParserBlockingRequest);
|
2015-09-22 02:07:01 +03:00
|
|
|
bool offThreadCompiled = request->mProgress == nsScriptLoadRequest::Progress_DoneCompiling;
|
2012-01-20 15:16:27 +04:00
|
|
|
UnblockParser(request);
|
2010-09-01 15:41:12 +04:00
|
|
|
ProcessRequest(request);
|
2015-09-22 02:07:01 +03:00
|
|
|
if (offThreadCompiled) {
|
|
|
|
mDocument->UnblockOnload(false);
|
|
|
|
}
|
2012-01-20 15:16:27 +04:00
|
|
|
ContinueParserAsync(request);
|
2010-09-01 15:41:12 +04:00
|
|
|
}
|
2010-09-21 02:47:57 +04:00
|
|
|
|
2010-10-27 11:18:24 +04:00
|
|
|
while (ReadyToExecuteScripts() &&
|
2015-04-09 05:50:45 +03:00
|
|
|
!mXSLTRequests.isEmpty() &&
|
2015-08-26 00:52:27 +03:00
|
|
|
mXSLTRequests.getFirst()->IsReadyToRun()) {
|
2015-04-09 05:50:45 +03:00
|
|
|
request = mXSLTRequests.StealFirst();
|
2010-10-27 11:18:24 +04:00
|
|
|
ProcessRequest(request);
|
|
|
|
}
|
|
|
|
|
2015-04-09 05:50:46 +03:00
|
|
|
while (mEnabled && !mLoadedAsyncRequests.isEmpty()) {
|
|
|
|
request = mLoadedAsyncRequests.StealFirst();
|
2015-08-26 00:52:27 +03:00
|
|
|
CompileOffThreadOrProcessRequest(request);
|
2010-09-01 15:41:12 +04:00
|
|
|
}
|
2010-09-28 03:23:33 +04:00
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
while (mEnabled && !mNonAsyncExternalScriptInsertedRequests.isEmpty() &&
|
2015-08-26 00:52:27 +03:00
|
|
|
mNonAsyncExternalScriptInsertedRequests.getFirst()->IsReadyToRun()) {
|
2010-10-13 11:12:55 +04:00
|
|
|
// Violate the HTML5 spec and execute these in the insertion order in
|
|
|
|
// order to make LABjs and the "order" plug-in for RequireJS work with
|
|
|
|
// their Gecko-sniffed code path. See
|
|
|
|
// http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html
|
2015-04-09 05:50:45 +03:00
|
|
|
request = mNonAsyncExternalScriptInsertedRequests.StealFirst();
|
2010-10-13 11:12:55 +04:00
|
|
|
ProcessRequest(request);
|
|
|
|
}
|
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
if (mDocumentParsingDone && mXSLTRequests.isEmpty()) {
|
2015-08-26 00:52:27 +03:00
|
|
|
while (!mDeferRequests.isEmpty() && mDeferRequests.getFirst()->IsReadyToRun()) {
|
2015-04-09 05:50:45 +03:00
|
|
|
request = mDeferRequests.StealFirst();
|
2010-09-01 15:41:12 +04:00
|
|
|
ProcessRequest(request);
|
|
|
|
}
|
2009-11-10 04:04:24 +03:00
|
|
|
}
|
|
|
|
|
2007-10-07 22:23:52 +04:00
|
|
|
while (!mPendingChildLoaders.IsEmpty() && ReadyToExecuteScripts()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoader> child = mPendingChildLoaders[0];
|
2007-09-28 19:15:07 +04:00
|
|
|
mPendingChildLoaders.RemoveElementAt(0);
|
|
|
|
child->RemoveExecuteBlocker();
|
|
|
|
}
|
2009-01-15 22:49:20 +03:00
|
|
|
|
2015-05-28 15:40:07 +03:00
|
|
|
if (mDocumentParsingDone && mDocument && !mParserBlockingRequest &&
|
|
|
|
mNonAsyncExternalScriptInsertedRequests.isEmpty() &&
|
|
|
|
mXSLTRequests.isEmpty() && mDeferRequests.isEmpty() &&
|
|
|
|
MaybeRemovedDeferRequests()) {
|
|
|
|
return ProcessPendingRequests();
|
|
|
|
}
|
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
if (mDocumentParsingDone && mDocument &&
|
2015-04-09 05:50:46 +03:00
|
|
|
!mParserBlockingRequest && mLoadingAsyncRequests.isEmpty() &&
|
|
|
|
mLoadedAsyncRequests.isEmpty() &&
|
2015-04-09 05:50:45 +03:00
|
|
|
mNonAsyncExternalScriptInsertedRequests.isEmpty() &&
|
|
|
|
mXSLTRequests.isEmpty() && mDeferRequests.isEmpty()) {
|
2009-01-15 22:49:20 +03: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.
|
2011-10-17 18:59:28 +04:00
|
|
|
mDocumentParsingDone = false;
|
|
|
|
mDocument->UnblockOnload(true);
|
2009-01-15 22:49:20 +03:00
|
|
|
}
|
2007-09-28 19:15:07 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2007-09-28 19:15:07 +04:00
|
|
|
nsScriptLoader::ReadyToExecuteScripts()
|
|
|
|
{
|
|
|
|
// Make sure the SelfReadyToExecuteScripts check is first, so that
|
|
|
|
// we don't block twice on an ancestor.
|
|
|
|
if (!SelfReadyToExecuteScripts()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-09-28 19:15:07 +04:00
|
|
|
}
|
2014-10-01 16:13:53 +04:00
|
|
|
|
2007-09-28 19:15:07 +04:00
|
|
|
for (nsIDocument* doc = mDocument; doc; doc = doc->GetParentDocument()) {
|
|
|
|
nsScriptLoader* ancestor = doc->ScriptLoader();
|
|
|
|
if (!ancestor->SelfReadyToExecuteScripts() &&
|
|
|
|
ancestor->AddPendingChildLoader(this)) {
|
|
|
|
AddExecuteBlocker();
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-09-28 19:15:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 14:02:41 +04:00
|
|
|
if (mDocument && !mDocument->IsMasterDocument()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImportManager> im = mDocument->ImportManager();
|
|
|
|
RefPtr<ImportLoader> loader = im->Find(mDocument);
|
2014-10-01 16:13:53 +04:00
|
|
|
MOZ_ASSERT(loader, "How can we have an import document without a loader?");
|
|
|
|
|
|
|
|
// The referring link that counts in the execution order calculation
|
|
|
|
// (in spec: flagged as branch)
|
|
|
|
nsCOMPtr<nsINode> referrer = loader->GetMainReferrer();
|
|
|
|
MOZ_ASSERT(referrer, "There has to be a main referring link for each imports");
|
|
|
|
|
|
|
|
// Import documents are blocked by their import predecessors. We need to
|
|
|
|
// wait with script execution until all the predecessors are done.
|
|
|
|
// Technically it means we have to wait for the last one to finish,
|
|
|
|
// which is the neares one to us in the order.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImportLoader> lastPred = im->GetNearestPredecessor(referrer);
|
2014-10-01 16:13:53 +04:00
|
|
|
if (!lastPred) {
|
|
|
|
// If there is no predecessor we can run.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = lastPred->GetDocument();
|
|
|
|
if (lastPred->IsBlocking() || !doc || (doc && !doc->ScriptLoader()->SelfReadyToExecuteScripts())) {
|
|
|
|
// Document has not been created yet or it was created but not ready.
|
|
|
|
// Either case we are blocked by it. The ImportLoader will take care
|
|
|
|
// of blocking us, and adding the pending child loader to the blocking
|
|
|
|
// ScriptLoader when it's possible (at this point the blocking loader
|
|
|
|
// might not have created the document/ScriptLoader)
|
|
|
|
lastPred->AddBlockedScriptLoader(this);
|
|
|
|
// As more imports are parsed, this can change, let's cache what we
|
|
|
|
// blocked, so it can be later updated if needed (see: ImportLoader::Updater).
|
|
|
|
loader->SetBlockingPredecessor(lastPred);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
2005-01-05 10:25:29 +03:00
|
|
|
// This function was copied from nsParser.cpp. It was simplified a bit.
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2012-08-22 19:56:38 +04:00
|
|
|
DetectByteOrderMark(const unsigned char* aBytes, int32_t aLen, nsCString& oCharset)
|
2002-09-18 23:44:19 +04:00
|
|
|
{
|
|
|
|
if (aLen < 2)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-09-18 23:44:19 +04:00
|
|
|
|
|
|
|
switch(aBytes[0]) {
|
2005-01-05 10:25:29 +03:00
|
|
|
case 0xEF:
|
|
|
|
if (aLen >= 3 && 0xBB == aBytes[1] && 0xBF == aBytes[2]) {
|
|
|
|
// EF BB BF
|
|
|
|
// Win2K UTF-8 BOM
|
2014-05-22 07:48:51 +04:00
|
|
|
oCharset.AssignLiteral("UTF-8");
|
2002-09-18 23:44:19 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xFE:
|
2005-01-05 10:25:29 +03:00
|
|
|
if (0xFF == aBytes[1]) {
|
2002-09-18 23:44:19 +04:00
|
|
|
// FE FF
|
2005-01-05 10:25:29 +03:00
|
|
|
// UTF-16, big-endian
|
2014-05-22 07:48:51 +04:00
|
|
|
oCharset.AssignLiteral("UTF-16BE");
|
2002-09-18 23:44:19 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xFF:
|
2005-01-05 10:25:29 +03:00
|
|
|
if (0xFE == aBytes[1]) {
|
2002-09-18 23:44:19 +04:00
|
|
|
// FF FE
|
2005-01-05 10:25:29 +03:00
|
|
|
// UTF-16, little-endian
|
2014-05-22 07:48:51 +04:00
|
|
|
oCharset.AssignLiteral("UTF-16LE");
|
2002-09-18 23:44:19 +04:00
|
|
|
}
|
|
|
|
break;
|
2005-01-05 10:25:29 +03:00
|
|
|
}
|
2003-05-24 01:34:47 +04:00
|
|
|
return !oCharset.IsEmpty();
|
2002-09-18 23:44:19 +04:00
|
|
|
}
|
|
|
|
|
2005-06-12 16:32:05 +04:00
|
|
|
/* static */ nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsScriptLoader::ConvertToUTF16(nsIChannel* aChannel, const uint8_t* aData,
|
|
|
|
uint32_t aLength, const nsAString& aHintCharset,
|
2014-04-25 18:11:56 +04:00
|
|
|
nsIDocument* aDocument,
|
2014-07-22 08:43:21 +04:00
|
|
|
char16_t*& aBufOut, size_t& aLengthOut)
|
2005-06-12 16:32:05 +04:00
|
|
|
{
|
|
|
|
if (!aLength) {
|
2014-04-25 18:11:56 +04:00
|
|
|
aBufOut = nullptr;
|
|
|
|
aLengthOut = 0;
|
2005-06-12 16:32:05 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-23 10:26:34 +04:00
|
|
|
// The encoding info precedence is as follows from high to low:
|
|
|
|
// The BOM
|
|
|
|
// HTTP Content-Type (if name recognized)
|
|
|
|
// charset attribute (if name recognized)
|
|
|
|
// The encoding of the document
|
2005-06-12 16:32:05 +04:00
|
|
|
|
2012-10-23 10:26:34 +04:00
|
|
|
nsAutoCString charset;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;
|
|
|
|
|
|
|
|
if (DetectByteOrderMark(aData, aLength, charset)) {
|
|
|
|
// charset is now "UTF-8" or "UTF-16". The UTF-16 decoder will re-sniff
|
|
|
|
// the BOM for endianness. Both the UTF-16 and the UTF-8 decoder will
|
|
|
|
// take care of swallowing the BOM.
|
2013-11-26 11:31:52 +04:00
|
|
|
unicodeDecoder = EncodingUtils::DecoderForEncoding(charset);
|
2005-06-12 16:32:05 +04:00
|
|
|
}
|
|
|
|
|
2012-10-23 10:26:34 +04:00
|
|
|
if (!unicodeDecoder &&
|
|
|
|
aChannel &&
|
2013-02-25 19:56:19 +04:00
|
|
|
NS_SUCCEEDED(aChannel->GetContentCharset(charset)) &&
|
2013-11-25 12:06:56 +04:00
|
|
|
EncodingUtils::FindEncodingForLabel(charset, charset)) {
|
2013-11-26 11:31:52 +04:00
|
|
|
unicodeDecoder = EncodingUtils::DecoderForEncoding(charset);
|
2005-06-12 16:32:05 +04:00
|
|
|
}
|
|
|
|
|
2013-11-25 12:06:56 +04:00
|
|
|
if (!unicodeDecoder &&
|
|
|
|
EncodingUtils::FindEncodingForLabel(aHintCharset, charset)) {
|
2013-11-26 11:31:52 +04:00
|
|
|
unicodeDecoder = EncodingUtils::DecoderForEncoding(charset);
|
2005-06-12 16:32:05 +04:00
|
|
|
}
|
|
|
|
|
2012-10-23 10:26:34 +04:00
|
|
|
if (!unicodeDecoder && aDocument) {
|
|
|
|
charset = aDocument->GetDocumentCharacterSet();
|
2013-11-26 11:31:52 +04:00
|
|
|
unicodeDecoder = EncodingUtils::DecoderForEncoding(charset);
|
2005-06-12 16:32:05 +04:00
|
|
|
}
|
|
|
|
|
2012-10-23 10:26:34 +04:00
|
|
|
if (!unicodeDecoder) {
|
|
|
|
// Curiously, there are various callers that don't pass aDocument. The
|
|
|
|
// fallback in the old code was ISO-8859-1, which behaved like
|
|
|
|
// windows-1252. Saying windows-1252 for clarity and for compliance
|
|
|
|
// with the Encoding Standard.
|
2013-11-26 11:31:52 +04:00
|
|
|
unicodeDecoder = EncodingUtils::DecoderForEncoding("windows-1252");
|
2005-06-12 16:32:05 +04:00
|
|
|
}
|
|
|
|
|
2012-10-23 10:26:34 +04:00
|
|
|
int32_t unicodeLength = 0;
|
2005-06-12 16:32:05 +04:00
|
|
|
|
2012-10-23 10:26:34 +04:00
|
|
|
nsresult rv =
|
|
|
|
unicodeDecoder->GetMaxLength(reinterpret_cast<const char*>(aData),
|
|
|
|
aLength, &unicodeLength);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-06-12 16:32:05 +04:00
|
|
|
|
2014-07-22 08:43:21 +04:00
|
|
|
aBufOut = static_cast<char16_t*>(js_malloc(unicodeLength * sizeof(char16_t)));
|
2014-04-25 18:11:56 +04:00
|
|
|
if (!aBufOut) {
|
|
|
|
aLengthOut = 0;
|
2012-10-23 10:26:34 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2005-06-12 16:32:05 +04:00
|
|
|
}
|
2014-04-25 18:11:56 +04:00
|
|
|
aLengthOut = unicodeLength;
|
2012-10-23 10:26:34 +04:00
|
|
|
|
2012-12-10 18:11:15 +04:00
|
|
|
rv = unicodeDecoder->Convert(reinterpret_cast<const char*>(aData),
|
2014-04-25 18:11:56 +04:00
|
|
|
(int32_t *) &aLength, aBufOut,
|
2012-12-10 18:11:15 +04:00
|
|
|
&unicodeLength);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2014-04-25 18:11:56 +04:00
|
|
|
aLengthOut = unicodeLength;
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
js_free(aBufOut);
|
|
|
|
aBufOut = nullptr;
|
|
|
|
aLengthOut = 0;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptLoader::OnStreamComplete(nsIStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aStringLen,
|
|
|
|
const uint8_t* aString)
|
2001-05-17 09:20:51 +04:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsScriptLoadRequest* request = static_cast<nsScriptLoadRequest*>(aContext);
|
2001-05-17 09:20:51 +04:00
|
|
|
NS_ASSERTION(request, "null request in stream complete handler");
|
2006-11-04 08:45:02 +03:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_FAILURE);
|
|
|
|
|
2015-08-13 06:19:11 +03:00
|
|
|
nsresult rv = NS_ERROR_SRI_CORRUPT;
|
|
|
|
if (request->mIntegrity.IsEmpty() ||
|
2015-10-07 21:27:19 +03:00
|
|
|
NS_SUCCEEDED(SRICheck::VerifyIntegrity(request->mIntegrity, aLoader,
|
2015-08-13 06:19:11 +03:00
|
|
|
request->mCORSMode, aStringLen,
|
|
|
|
aString, mDocument))) {
|
|
|
|
rv = PrepareLoadedRequest(request, aLoader, aStatus, aStringLen, aString);
|
|
|
|
}
|
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-06-25 04:37:03 +04:00
|
|
|
/*
|
|
|
|
* Handle script not loading error because source was a tracking URL.
|
2014-08-08 00:35:03 +04:00
|
|
|
* We make a note of this script node by including it in a dedicated
|
|
|
|
* array of blocked tracking nodes under its parent document.
|
2014-06-25 04:37:03 +04:00
|
|
|
*/
|
|
|
|
if (rv == NS_ERROR_TRACKING_URI) {
|
|
|
|
nsCOMPtr<nsIContent> cont = do_QueryInterface(request->mElement);
|
|
|
|
mDocument->AddBlockedTrackingNode(cont);
|
|
|
|
}
|
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
if (request->mIsDefer) {
|
|
|
|
if (request->isInList()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoadRequest> req = mDeferRequests.Steal(request);
|
2015-04-09 05:50:45 +03:00
|
|
|
FireScriptAvailable(rv, req);
|
|
|
|
}
|
|
|
|
} else if (request->mIsAsync) {
|
|
|
|
if (request->isInList()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoadRequest> req = mLoadingAsyncRequests.Steal(request);
|
2015-04-09 05:50:45 +03:00
|
|
|
FireScriptAvailable(rv, req);
|
|
|
|
}
|
|
|
|
} else if (request->mIsNonAsyncScriptInserted) {
|
|
|
|
if (request->isInList()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoadRequest> req =
|
2015-04-09 05:50:45 +03:00
|
|
|
mNonAsyncExternalScriptInsertedRequests.Steal(request);
|
|
|
|
FireScriptAvailable(rv, req);
|
|
|
|
}
|
|
|
|
} else if (request->mIsXSLT) {
|
|
|
|
if (request->isInList()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoadRequest> req = mXSLTRequests.Steal(request);
|
2015-04-09 05:50:45 +03:00
|
|
|
FireScriptAvailable(rv, req);
|
|
|
|
}
|
2010-09-01 15:41:12 +04:00
|
|
|
} else if (mParserBlockingRequest == request) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mParserBlockingRequest = nullptr;
|
2012-01-20 15:16:27 +04:00
|
|
|
UnblockParser(request);
|
2010-09-01 15:41:12 +04:00
|
|
|
FireScriptAvailable(rv, request);
|
2012-01-20 15:16:27 +04:00
|
|
|
ContinueParserAsync(request);
|
2009-11-10 04:04:24 +03:00
|
|
|
} else {
|
|
|
|
mPreloads.RemoveElement(request, PreloadRequestComparator());
|
2008-10-01 10:48:47 +04:00
|
|
|
}
|
2014-04-09 03:40:26 +04:00
|
|
|
rv = NS_OK;
|
|
|
|
} else {
|
2015-02-19 07:51:06 +03:00
|
|
|
free(const_cast<uint8_t *>(aString));
|
2014-04-09 03:40:26 +04:00
|
|
|
rv = NS_SUCCESS_ADOPTED_DATA;
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
// Process our request and/or any pending ones
|
|
|
|
ProcessPendingRequests();
|
|
|
|
|
2014-04-09 03:40:26 +04:00
|
|
|
return rv;
|
2006-11-04 08:45:02 +03:00
|
|
|
}
|
|
|
|
|
2012-01-20 15:16:27 +04:00
|
|
|
void
|
|
|
|
nsScriptLoader::UnblockParser(nsScriptLoadRequest* aParserBlockingRequest)
|
|
|
|
{
|
|
|
|
aParserBlockingRequest->mElement->UnblockParser();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::ContinueParserAsync(nsScriptLoadRequest* aParserBlockingRequest)
|
|
|
|
{
|
|
|
|
aParserBlockingRequest->mElement->ContinueParserAsync();
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:41:55 +03:00
|
|
|
uint32_t
|
|
|
|
nsScriptLoader::NumberOfProcessors()
|
|
|
|
{
|
|
|
|
if (mNumberOfProcessors > 0)
|
|
|
|
return mNumberOfProcessors;
|
|
|
|
|
|
|
|
int32_t numProcs = PR_GetNumberOfProcessors();
|
|
|
|
if (numProcs > 0)
|
|
|
|
mNumberOfProcessors = numProcs;
|
|
|
|
return mNumberOfProcessors;
|
|
|
|
}
|
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
nsresult
|
|
|
|
nsScriptLoader::PrepareLoadedRequest(nsScriptLoadRequest* aRequest,
|
|
|
|
nsIStreamLoader* aLoader,
|
|
|
|
nsresult aStatus,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aStringLen,
|
|
|
|
const uint8_t* aString)
|
2006-11-04 08:45:02 +03:00
|
|
|
{
|
2001-05-17 09:20:51 +04:00
|
|
|
if (NS_FAILED(aStatus)) {
|
2006-11-04 08:45:02 +03:00
|
|
|
return aStatus;
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
2015-04-16 20:22:02 +03:00
|
|
|
if (aRequest->IsCanceled()) {
|
|
|
|
return NS_BINDING_ABORTED;
|
|
|
|
}
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
// If we don't have a document, then we need to abort further
|
|
|
|
// evaluation.
|
|
|
|
if (!mDocument) {
|
2006-11-04 08:45:02 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
2002-07-02 03:12:13 +04:00
|
|
|
// If the load returned an error page, then we need to abort
|
|
|
|
nsCOMPtr<nsIRequest> req;
|
2006-11-04 08:45:02 +03:00
|
|
|
nsresult rv = aLoader->GetRequest(getter_AddRefs(req));
|
2002-07-02 03:12:13 +04:00
|
|
|
NS_ASSERTION(req, "StreamLoader's request went away prematurely");
|
2006-11-04 08:45:02 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(req);
|
2002-07-02 03:12:13 +04:00
|
|
|
if (httpChannel) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool requestSucceeded;
|
2002-07-02 03:12:13 +04:00
|
|
|
rv = httpChannel->GetRequestSucceeded(&requestSucceeded);
|
|
|
|
if (NS_SUCCEEDED(rv) && !requestSucceeded) {
|
2006-11-04 08:45:02 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2002-07-02 03:12:13 +04:00
|
|
|
}
|
2013-09-17 13:43:23 +04:00
|
|
|
|
|
|
|
nsAutoCString sourceMapURL;
|
2014-07-10 22:43:18 +04:00
|
|
|
rv = httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("X-SourceMap"), sourceMapURL);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
aRequest->mHasSourceMapURL = true;
|
|
|
|
aRequest->mSourceMapURL = NS_ConvertUTF8toUTF16(sourceMapURL);
|
|
|
|
}
|
2002-07-02 03:12:13 +04:00
|
|
|
}
|
|
|
|
|
2005-06-12 16:32:05 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(req);
|
2012-03-10 20:13:52 +04:00
|
|
|
// If this load was subject to a CORS check; don't flag it with a
|
|
|
|
// separate origin principal, so that it will treat our document's
|
|
|
|
// principal as the origin principal
|
|
|
|
if (aRequest->mCORSMode == CORS_NONE) {
|
|
|
|
rv = nsContentUtils::GetSecurityManager()->
|
2014-08-07 03:05:40 +04:00
|
|
|
GetChannelResultPrincipal(channel, getter_AddRefs(aRequest->mOriginPrincipal));
|
2012-03-10 20:13:52 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2011-12-19 21:48:12 +04:00
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
if (aStringLen) {
|
2005-06-12 16:32:05 +04:00
|
|
|
// Check the charset attribute to determine script charset.
|
|
|
|
nsAutoString hintCharset;
|
2008-10-01 10:48:47 +04: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;
|
|
|
|
}
|
2006-11-04 08:45:02 +03:00
|
|
|
rv = ConvertToUTF16(channel, aString, aStringLen, hintCharset, mDocument,
|
2014-04-25 18:11:56 +04:00
|
|
|
aRequest->mScriptTextBuf, aRequest->mScriptTextLength);
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-05-17 09:20:51 +04:00
|
|
|
}
|
|
|
|
|
2006-11-04 08:45:02 +03: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.
|
2012-03-22 01:06:10 +04:00
|
|
|
NS_ASSERTION(mDeferRequests.Contains(aRequest) ||
|
2015-04-09 05:50:46 +03:00
|
|
|
mLoadingAsyncRequests.Contains(aRequest) ||
|
2012-03-22 01:06:10 +04:00
|
|
|
mNonAsyncExternalScriptInsertedRequests.Contains(aRequest) ||
|
|
|
|
mXSLTRequests.Contains(aRequest) ||
|
2010-09-01 15:41:12 +04:00
|
|
|
mPreloads.Contains(aRequest, PreloadRequestComparator()) ||
|
|
|
|
mParserBlockingRequest,
|
2006-11-04 08:45:02 +03:00
|
|
|
"aRequest should be pending!");
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
// Mark this as loaded
|
2015-08-26 00:52:27 +03:00
|
|
|
aRequest->mProgress = nsScriptLoadRequest::Progress_DoneLoading;
|
2015-07-29 00:03:56 +03:00
|
|
|
|
2015-09-22 02:07:01 +03:00
|
|
|
// If this is currently blocking the parser, attempt to compile it off-main-thread.
|
2015-09-29 17:41:55 +03:00
|
|
|
if (aRequest == mParserBlockingRequest && (NumberOfProcessors() > 1)) {
|
2015-09-22 02:07:01 +03:00
|
|
|
nsresult rv = AttemptAsyncScriptCompile(aRequest);
|
|
|
|
if (rv == NS_OK) {
|
|
|
|
NS_ASSERTION(aRequest->mProgress == nsScriptLoadRequest::Progress_Compiling,
|
|
|
|
"Request should be off-thread compiling now.");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If off-thread compile errored, return the error.
|
|
|
|
if (rv != NS_ERROR_FAILURE) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If off-thread compile was rejected, continue with regular processing.
|
|
|
|
}
|
|
|
|
|
2015-04-16 20:22:02 +03:00
|
|
|
// And if it's async, move it to the loaded list. aRequest->mIsAsync really
|
|
|
|
// _should_ be in a list, but the consequences if it's not are bad enough we
|
|
|
|
// want to avoid trying to move it if it's not.
|
2015-04-09 05:50:46 +03:00
|
|
|
if (aRequest->mIsAsync) {
|
2015-04-16 20:22:02 +03:00
|
|
|
MOZ_ASSERT(aRequest->isInList());
|
|
|
|
if (aRequest->isInList()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoadRequest> req = mLoadingAsyncRequests.Steal(aRequest);
|
2015-04-16 20:22:02 +03:00
|
|
|
mLoadedAsyncRequests.AppendElement(req);
|
|
|
|
}
|
2015-04-09 05:50:46 +03:00
|
|
|
}
|
|
|
|
|
2001-05-25 11:03:04 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-03-22 08:03:57 +03:00
|
|
|
|
2008-07-26 06:42:12 +04:00
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsScriptLoader::ParsingComplete(bool aTerminated)
|
2008-07-26 06:42:12 +04:00
|
|
|
{
|
2009-01-15 22:49:20 +03:00
|
|
|
if (mDeferEnabled) {
|
2009-07-08 09:55:05 +04:00
|
|
|
// Have to check because we apparently get ParsingComplete
|
2009-01-15 22:49:20 +03:00
|
|
|
// without BeginDeferringScripts in some cases
|
2011-10-17 18:59:28 +04:00
|
|
|
mDocumentParsingDone = true;
|
2009-01-15 22:49:20 +03:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mDeferEnabled = false;
|
2009-07-08 09:55:05 +04:00
|
|
|
if (aTerminated) {
|
2010-09-01 15:41:12 +04:00
|
|
|
mDeferRequests.Clear();
|
2015-04-09 05:50:46 +03:00
|
|
|
mLoadingAsyncRequests.Clear();
|
|
|
|
mLoadedAsyncRequests.Clear();
|
2010-10-13 11:12:55 +04:00
|
|
|
mNonAsyncExternalScriptInsertedRequests.Clear();
|
2010-10-27 11:18:24 +04:00
|
|
|
mXSLTRequests.Clear();
|
2015-04-16 20:22:02 +03:00
|
|
|
if (mParserBlockingRequest) {
|
|
|
|
mParserBlockingRequest->Cancel();
|
|
|
|
mParserBlockingRequest = nullptr;
|
|
|
|
}
|
2008-07-26 06:42:12 +04:00
|
|
|
}
|
|
|
|
|
2009-07-08 09:55:05 +04:00
|
|
|
// Have to call this even if aTerminated so we'll correctly unblock
|
|
|
|
// onload and all.
|
2008-07-26 06:42:12 +04:00
|
|
|
ProcessPendingRequests();
|
|
|
|
}
|
2008-10-01 10:48:47 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::PreloadURI(nsIURI *aURI, const nsAString &aCharset,
|
2012-03-10 20:13:52 +04:00
|
|
|
const nsAString &aType,
|
2012-12-05 03:06:29 +04:00
|
|
|
const nsAString &aCrossOrigin,
|
2015-08-13 06:19:11 +03:00
|
|
|
const nsAString& aIntegrity,
|
2014-11-18 16:46:43 +03:00
|
|
|
bool aScriptFromHead,
|
|
|
|
const mozilla::net::ReferrerPolicy aReferrerPolicy)
|
2008-10-01 10:48:47 +04:00
|
|
|
{
|
2011-07-06 01:33:03 +04:00
|
|
|
// Check to see if scripts has been turned off.
|
|
|
|
if (!mEnabled || !mDocument->IsScriptEnabled()) {
|
2008-10-01 10:48:47 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-13 06:19:11 +03:00
|
|
|
SRIMetadata sriMetadata;
|
|
|
|
if (!aIntegrity.IsEmpty()) {
|
|
|
|
MOZ_LOG(GetSriLog(), mozilla::LogLevel::Debug,
|
|
|
|
("nsScriptLoader::PreloadURI, integrity=%s",
|
|
|
|
NS_ConvertUTF16toUTF8(aIntegrity).get()));
|
|
|
|
SRICheck::IntegrityMetadata(aIntegrity, mDocument, &sriMetadata);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptLoadRequest> request =
|
2012-07-30 18:20:58 +04:00
|
|
|
new nsScriptLoadRequest(nullptr, 0,
|
2015-08-13 06:19:11 +03:00
|
|
|
Element::StringToCORSMode(aCrossOrigin),
|
|
|
|
sriMetadata);
|
2008-10-01 10:48:47 +04:00
|
|
|
request->mURI = aURI;
|
2011-10-17 18:59:28 +04:00
|
|
|
request->mIsInline = false;
|
2015-08-26 00:52:27 +03:00
|
|
|
request->mProgress = nsScriptLoadRequest::Progress_Loading;
|
2014-11-18 16:46:43 +03:00
|
|
|
request->mReferrerPolicy = aReferrerPolicy;
|
|
|
|
|
2012-12-05 03:06:29 +04:00
|
|
|
nsresult rv = StartLoad(request, aType, aScriptFromHead);
|
2008-10-01 10:48:47 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PreloadInfo *pi = mPreloads.AppendElement();
|
|
|
|
pi->mRequest = request;
|
|
|
|
pi->mCharset = aCharset;
|
|
|
|
}
|
2014-04-12 13:02:21 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::AddDeferRequest(nsScriptLoadRequest* aRequest)
|
|
|
|
{
|
2015-04-09 05:50:45 +03:00
|
|
|
aRequest->mIsDefer = true;
|
2014-04-12 13:02:21 +04:00
|
|
|
mDeferRequests.AppendElement(aRequest);
|
2015-04-09 05:50:45 +03:00
|
|
|
if (mDeferEnabled && aRequest == mDeferRequests.getFirst() &&
|
|
|
|
mDocument && !mBlockingDOMContentLoaded) {
|
2014-04-12 13:02:21 +04:00
|
|
|
MOZ_ASSERT(mDocument->GetReadyStateEnum() == nsIDocument::READYSTATE_LOADING);
|
|
|
|
mBlockingDOMContentLoaded = true;
|
|
|
|
mDocument->BlockDOMContentLoaded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsScriptLoader::MaybeRemovedDeferRequests()
|
|
|
|
{
|
2015-04-09 05:50:45 +03:00
|
|
|
if (mDeferRequests.isEmpty() && mDocument &&
|
2014-04-12 13:02:21 +04:00
|
|
|
mBlockingDOMContentLoaded) {
|
|
|
|
mBlockingDOMContentLoaded = false;
|
|
|
|
mDocument->UnblockDOMContentLoaded();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|