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.
|
|
|
|
*/
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
#ifndef __nsScriptLoader_h__
|
|
|
|
#define __nsScriptLoader_h__
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2004-07-07 02:52:03 +04:00
|
|
|
#include "nsIScriptElement.h"
|
2003-07-12 02:47:17 +04:00
|
|
|
#include "nsCOMArray.h"
|
2007-09-28 19:15:07 +04:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2001-05-17 09:20:51 +04:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIStreamLoader.h"
|
2015-04-09 05:50:45 +03:00
|
|
|
#include "mozilla/CORSMode.h"
|
2015-04-09 05:50:45 +03:00
|
|
|
#include "mozilla/LinkedList.h"
|
2014-11-18 16:46:43 +03:00
|
|
|
#include "mozilla/net/ReferrerPolicy.h"
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
class nsScriptLoadRequestList;
|
2013-03-18 19:38:19 +04:00
|
|
|
class nsIURI;
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2014-04-25 18:11:56 +04:00
|
|
|
namespace JS {
|
|
|
|
class SourceBufferHolder;
|
|
|
|
}
|
|
|
|
|
2014-07-03 22:12:03 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class AutoJSAPI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// Per-request data structure
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
class nsScriptLoadRequest final : public nsISupports,
|
|
|
|
private mozilla::LinkedListElement<nsScriptLoadRequest>
|
|
|
|
{
|
2015-04-09 05:50:45 +03:00
|
|
|
~nsScriptLoadRequest()
|
|
|
|
{
|
|
|
|
js_free(mScriptTextBuf);
|
|
|
|
}
|
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
typedef LinkedListElement<nsScriptLoadRequest> super;
|
|
|
|
|
|
|
|
// Allow LinkedListElement<nsScriptLoadRequest> to cast us to itself as needed.
|
|
|
|
friend class mozilla::LinkedListElement<nsScriptLoadRequest>;
|
|
|
|
friend class nsScriptLoadRequestList;
|
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
public:
|
|
|
|
nsScriptLoadRequest(nsIScriptElement* aElement,
|
|
|
|
uint32_t aVersion,
|
|
|
|
mozilla::CORSMode aCORSMode)
|
|
|
|
: mElement(aElement),
|
|
|
|
mLoading(true),
|
|
|
|
mIsInline(true),
|
|
|
|
mHasSourceMapURL(false),
|
2015-04-09 05:50:45 +03:00
|
|
|
mIsDefer(false),
|
|
|
|
mIsAsync(false),
|
|
|
|
mIsNonAsyncScriptInserted(false),
|
|
|
|
mIsXSLT(false),
|
2015-04-16 20:22:02 +03:00
|
|
|
mIsCanceled(false),
|
2015-04-09 05:50:45 +03:00
|
|
|
mScriptTextBuf(nullptr),
|
|
|
|
mScriptTextLength(0),
|
|
|
|
mJSVersion(aVersion),
|
|
|
|
mLineNo(1),
|
|
|
|
mCORSMode(aCORSMode),
|
|
|
|
mReferrerPolicy(mozilla::net::RP_Default)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
void FireScriptAvailable(nsresult aResult)
|
|
|
|
{
|
|
|
|
mElement->ScriptAvailable(aResult, mElement, mIsInline, mURI, mLineNo);
|
|
|
|
}
|
|
|
|
void FireScriptEvaluated(nsresult aResult)
|
|
|
|
{
|
|
|
|
mElement->ScriptEvaluated(aResult, mElement, mIsInline);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsPreload()
|
|
|
|
{
|
|
|
|
return mElement == nullptr;
|
|
|
|
}
|
|
|
|
|
2015-04-16 20:22:02 +03:00
|
|
|
void Cancel()
|
|
|
|
{
|
|
|
|
mIsCanceled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsCanceled() const
|
|
|
|
{
|
|
|
|
return mIsCanceled;
|
|
|
|
}
|
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
using super::getNext;
|
|
|
|
using super::isInList;
|
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
nsCOMPtr<nsIScriptElement> mElement;
|
|
|
|
bool mLoading; // Are we still waiting for a load to complete?
|
|
|
|
bool mIsInline; // Is the script inline or loaded?
|
|
|
|
bool mHasSourceMapURL; // Does the HTTP header have a source map url?
|
2015-04-09 05:50:45 +03:00
|
|
|
bool mIsDefer; // True if we live in mDeferRequests.
|
2015-04-09 05:50:46 +03:00
|
|
|
bool mIsAsync; // True if we live in mLoadingAsyncRequests or mLoadedAsyncRequests.
|
2015-04-09 05:50:45 +03:00
|
|
|
bool mIsNonAsyncScriptInserted; // True if we live in mNonAsyncExternalScriptInsertedRequests
|
|
|
|
bool mIsXSLT; // True if we live in mXSLTRequests.
|
2015-04-16 20:22:02 +03:00
|
|
|
bool mIsCanceled; // True if we have been explicitly canceled.
|
2015-04-09 05:50:45 +03:00
|
|
|
nsString mSourceMapURL; // Holds source map url for loaded scripts
|
|
|
|
char16_t* mScriptTextBuf; // Holds script text for non-inline scripts. Don't
|
|
|
|
size_t mScriptTextLength; // use nsString so we can give ownership to jsapi.
|
|
|
|
uint32_t mJSVersion;
|
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
nsCOMPtr<nsIPrincipal> mOriginPrincipal;
|
|
|
|
nsAutoCString mURL; // Keep the URI's filename alive during off thread parsing.
|
|
|
|
int32_t mLineNo;
|
|
|
|
const mozilla::CORSMode mCORSMode;
|
|
|
|
mozilla::net::ReferrerPolicy mReferrerPolicy;
|
|
|
|
};
|
|
|
|
|
2015-04-09 05:50:45 +03:00
|
|
|
class nsScriptLoadRequestList : private mozilla::LinkedList<nsScriptLoadRequest>
|
|
|
|
{
|
|
|
|
typedef mozilla::LinkedList<nsScriptLoadRequest> super;
|
|
|
|
|
|
|
|
public:
|
|
|
|
~nsScriptLoadRequestList();
|
|
|
|
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool Contains(nsScriptLoadRequest* aElem);
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
using super::getFirst;
|
|
|
|
using super::isEmpty;
|
|
|
|
|
|
|
|
void AppendElement(nsScriptLoadRequest* aElem)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aElem->isInList());
|
|
|
|
NS_ADDREF(aElem);
|
|
|
|
insertBack(aElem);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_WARN_UNUSED_RESULT
|
|
|
|
already_AddRefed<nsScriptLoadRequest> Steal(nsScriptLoadRequest* aElem)
|
|
|
|
{
|
|
|
|
aElem->removeFrom(*this);
|
|
|
|
return dont_AddRef(aElem);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_WARN_UNUSED_RESULT
|
|
|
|
already_AddRefed<nsScriptLoadRequest> StealFirst()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!isEmpty());
|
|
|
|
return Steal(getFirst());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// Script loader implementation
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsScriptLoader final : public nsIStreamLoaderObserver
|
2001-05-17 09:20:51 +04:00
|
|
|
{
|
2014-05-21 21:08:12 +04:00
|
|
|
class MOZ_STACK_CLASS AutoCurrentScriptUpdater
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoCurrentScriptUpdater(nsScriptLoader* aScriptLoader,
|
|
|
|
nsIScriptElement* aCurrentScript)
|
|
|
|
: mOldScript(aScriptLoader->mCurrentScript)
|
|
|
|
, mScriptLoader(aScriptLoader)
|
|
|
|
{
|
|
|
|
mScriptLoader->mCurrentScript = aCurrentScript;
|
|
|
|
}
|
|
|
|
~AutoCurrentScriptUpdater()
|
|
|
|
{
|
|
|
|
mScriptLoader->mCurrentScript.swap(mOldScript);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIScriptElement> mOldScript;
|
|
|
|
nsScriptLoader* mScriptLoader;
|
|
|
|
};
|
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
friend class nsScriptRequestProcessor;
|
2014-05-21 21:08:12 +04:00
|
|
|
friend class AutoCurrentScriptUpdater;
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
public:
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit nsScriptLoader(nsIDocument* aDocument);
|
2001-05-17 09:20:51 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISTREAMLOADEROBSERVER
|
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
/**
|
|
|
|
* The loader maintains a weak reference to the document with
|
|
|
|
* which it is initialized. This call forces the reference to
|
|
|
|
* be dropped.
|
|
|
|
*/
|
|
|
|
void DropDocumentReference()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mDocument = nullptr;
|
2006-11-04 08:45:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an observer for all scripts loaded through this loader.
|
|
|
|
*
|
|
|
|
* @param aObserver observer for all script processing.
|
|
|
|
*/
|
|
|
|
nsresult AddObserver(nsIScriptLoaderObserver* aObserver)
|
|
|
|
{
|
|
|
|
return mObservers.AppendObject(aObserver) ? NS_OK :
|
|
|
|
NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an observer.
|
|
|
|
*
|
|
|
|
* @param aObserver observer to be removed
|
|
|
|
*/
|
|
|
|
void RemoveObserver(nsIScriptLoaderObserver* aObserver)
|
|
|
|
{
|
|
|
|
mObservers.RemoveObject(aObserver);
|
|
|
|
}
|
2013-12-31 20:30:07 +04:00
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
/**
|
|
|
|
* Process a script element. This will include both loading the
|
|
|
|
* source of the element if it is not inline and evaluating
|
|
|
|
* the script itself.
|
|
|
|
*
|
|
|
|
* If the script is an inline script that can be executed immediately
|
|
|
|
* (i.e. there are no other scripts pending) then ScriptAvailable
|
|
|
|
* and ScriptEvaluated will be called before the function returns.
|
|
|
|
*
|
2011-11-16 11:50:18 +04:00
|
|
|
* If true is returned the script could not be executed immediately.
|
|
|
|
* In this case ScriptAvailable is guaranteed to be called at a later
|
|
|
|
* point (as well as possibly ScriptEvaluated).
|
2006-11-04 08:45:02 +03:00
|
|
|
*
|
|
|
|
* @param aElement The element representing the script to be loaded and
|
|
|
|
* evaluated.
|
|
|
|
*/
|
2011-11-16 11:50:18 +04:00
|
|
|
bool ProcessScriptElement(nsIScriptElement* aElement);
|
2006-11-04 08:45:02 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the currently executing script. This is useful if you want to
|
|
|
|
* generate a unique key based on the currently executing script.
|
|
|
|
*/
|
|
|
|
nsIScriptElement* GetCurrentScript()
|
|
|
|
{
|
|
|
|
return mCurrentScript;
|
|
|
|
}
|
|
|
|
|
2010-09-06 11:41:26 +04:00
|
|
|
nsIScriptElement* GetCurrentParserInsertedScript()
|
|
|
|
{
|
|
|
|
return mCurrentParserInsertedScript;
|
|
|
|
}
|
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
/**
|
|
|
|
* Whether the loader is enabled or not.
|
|
|
|
* When disabled, processing of new script elements is disabled.
|
2011-11-16 11:50:18 +04:00
|
|
|
* Any call to ProcessScriptElement() will return false. Note that
|
|
|
|
* this DOES NOT disable currently loading or executing scripts.
|
2006-11-04 08:45:02 +03:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool GetEnabled()
|
2006-11-04 08:45:02 +03:00
|
|
|
{
|
|
|
|
return mEnabled;
|
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
void SetEnabled(bool aEnabled)
|
2006-11-04 08:45:02 +03:00
|
|
|
{
|
2007-01-09 04:07:29 +03:00
|
|
|
if (!mEnabled && aEnabled) {
|
|
|
|
ProcessPendingRequestsAsync();
|
|
|
|
}
|
2006-11-04 08:45:02 +03:00
|
|
|
mEnabled = aEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add/remove blocker. Blockers will stop scripts from executing, but not
|
|
|
|
* from loading.
|
|
|
|
*/
|
|
|
|
void AddExecuteBlocker()
|
|
|
|
{
|
2008-03-15 02:08:57 +03:00
|
|
|
++mBlockerCount;
|
2006-11-04 08:45:02 +03:00
|
|
|
}
|
|
|
|
void RemoveExecuteBlocker()
|
|
|
|
{
|
|
|
|
if (!--mBlockerCount) {
|
2008-03-15 02:08:57 +03:00
|
|
|
ProcessPendingRequestsAsync();
|
2006-11-04 08:45:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 18:11:56 +04:00
|
|
|
/**
|
|
|
|
* Convert the given buffer to a UTF-16 string.
|
|
|
|
* @param aChannel Channel corresponding to the data. May be null.
|
|
|
|
* @param aData The data to convert
|
|
|
|
* @param aLength Length of the data
|
|
|
|
* @param aHintCharset Hint for the character set (e.g., from a charset
|
|
|
|
* attribute). May be the empty string.
|
|
|
|
* @param aDocument Document which the data is loaded for. Must not be
|
|
|
|
* null.
|
2014-07-22 08:43:21 +04:00
|
|
|
* @param aBufOut [out] char16_t array allocated by ConvertToUTF16 and
|
2014-04-25 18:11:56 +04:00
|
|
|
* containing data converted to unicode. Caller must
|
|
|
|
* js_free() this data when no longer needed.
|
|
|
|
* @param aLengthOut [out] Length of array returned in aBufOut in number
|
2014-07-22 08:43:21 +04:00
|
|
|
* of char16_t code units.
|
2014-04-25 18:11:56 +04:00
|
|
|
*/
|
|
|
|
static nsresult ConvertToUTF16(nsIChannel* aChannel, const uint8_t* aData,
|
|
|
|
uint32_t aLength,
|
|
|
|
const nsAString& aHintCharset,
|
|
|
|
nsIDocument* aDocument,
|
2014-07-22 08:43:21 +04:00
|
|
|
char16_t*& aBufOut, size_t& aLengthOut);
|
2014-04-25 18:11:56 +04:00
|
|
|
|
2007-01-09 04:07:29 +03:00
|
|
|
/**
|
|
|
|
* Processes any pending requests that are ready for processing.
|
|
|
|
*/
|
|
|
|
void ProcessPendingRequests();
|
|
|
|
|
2009-02-11 17:51:37 +03:00
|
|
|
/**
|
|
|
|
* Check whether it's OK to load a script from aURI in
|
|
|
|
* aDocument.
|
|
|
|
*/
|
|
|
|
static nsresult ShouldLoadScript(nsIDocument* aDocument,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsIURI* aURI,
|
|
|
|
const nsAString &aType);
|
|
|
|
|
2008-07-26 06:42:12 +04:00
|
|
|
/**
|
|
|
|
* Starts deferring deferred scripts and puts them in the mDeferredRequests
|
|
|
|
* queue instead.
|
|
|
|
*/
|
|
|
|
void BeginDeferringScripts()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mDeferEnabled = true;
|
2009-01-15 22:49:20 +03:00
|
|
|
if (mDocument) {
|
|
|
|
mDocument->BlockOnload();
|
|
|
|
}
|
2008-07-26 06:42:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-08 09:55:05 +04:00
|
|
|
* Notifies the script loader that parsing is done. If aTerminated is true,
|
|
|
|
* this will drop any pending scripts that haven't run yet. Otherwise, it
|
|
|
|
* will stops deferring scripts and immediately processes the
|
|
|
|
* mDeferredRequests queue.
|
2008-07-26 06:42:12 +04:00
|
|
|
*
|
2009-07-08 09:55:05 +04:00
|
|
|
* WARNING: This function will synchronously execute content scripts, so be
|
2008-07-26 06:42:12 +04:00
|
|
|
* prepared that the world might change around you.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
void ParsingComplete(bool aTerminated);
|
2008-07-26 06:42:12 +04:00
|
|
|
|
2009-01-15 04:25:21 +03:00
|
|
|
/**
|
|
|
|
* Returns the number of pending scripts, deferred or not.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t HasPendingOrCurrentScripts()
|
2009-01-15 04:25:21 +03:00
|
|
|
{
|
2010-09-01 15:41:12 +04:00
|
|
|
return mCurrentScript || mParserBlockingRequest;
|
2009-01-15 04:25:21 +03:00
|
|
|
}
|
|
|
|
|
2008-10-01 10:48:47 +04:00
|
|
|
/**
|
|
|
|
* Adds aURI to the preload list and starts loading it.
|
|
|
|
*
|
|
|
|
* @param aURI The URI of the external script.
|
|
|
|
* @param aCharset The charset parameter for the script.
|
|
|
|
* @param aType The type parameter for the script.
|
2012-03-10 20:13:52 +04:00
|
|
|
* @param aCrossOrigin The crossorigin attribute for the script.
|
|
|
|
* Void if not present.
|
2012-12-05 03:06:29 +04:00
|
|
|
* @param aScriptFromHead Whether or not the script was a child of head
|
2008-10-01 10:48:47 +04:00
|
|
|
*/
|
|
|
|
virtual void 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,
|
2014-11-18 16:46:43 +03:00
|
|
|
bool aScriptFromHead,
|
|
|
|
const mozilla::net::ReferrerPolicy aReferrerPolicy);
|
2008-10-01 10:48:47 +04:00
|
|
|
|
2013-09-12 03:42:09 +04:00
|
|
|
/**
|
|
|
|
* Process a request that was deferred so that the script could be compiled
|
|
|
|
* off thread.
|
|
|
|
*/
|
2013-12-06 00:55:31 +04:00
|
|
|
nsresult ProcessOffThreadRequest(nsScriptLoadRequest *aRequest,
|
|
|
|
void **aOffThreadToken);
|
2013-09-12 03:42:09 +04:00
|
|
|
|
2014-10-01 16:13:53 +04:00
|
|
|
bool AddPendingChildLoader(nsScriptLoader* aChild) {
|
|
|
|
return mPendingChildLoaders.AppendElement(aChild) != nullptr;
|
|
|
|
}
|
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
private:
|
2014-06-25 06:09:15 +04:00
|
|
|
virtual ~nsScriptLoader();
|
|
|
|
|
2012-01-20 15:16:27 +04:00
|
|
|
/**
|
|
|
|
* Unblocks the creator parser of the parser-blocking scripts.
|
|
|
|
*/
|
|
|
|
void UnblockParser(nsScriptLoadRequest* aParserBlockingRequest);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously resumes the creator parser of the parser-blocking scripts.
|
|
|
|
*/
|
|
|
|
void ContinueParserAsync(nsScriptLoadRequest* aParserBlockingRequest);
|
|
|
|
|
|
|
|
|
2008-11-19 10:05:05 +03:00
|
|
|
/**
|
|
|
|
* Helper function to check the content policy for a given request.
|
|
|
|
*/
|
2009-02-11 17:51:37 +03:00
|
|
|
static nsresult CheckContentPolicy(nsIDocument* aDocument,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIURI *aURI,
|
|
|
|
const nsAString &aType);
|
2008-11-19 10:05:05 +03:00
|
|
|
|
2008-10-01 10:48:47 +04:00
|
|
|
/**
|
|
|
|
* Start a load for aRequest's URI.
|
|
|
|
*/
|
2012-12-05 03:06:29 +04:00
|
|
|
nsresult StartLoad(nsScriptLoadRequest *aRequest, const nsAString &aType,
|
|
|
|
bool aScriptFromHead);
|
2008-10-01 10:48:47 +04:00
|
|
|
|
2007-01-09 04:07:29 +03:00
|
|
|
/**
|
2009-01-15 22:49:20 +03:00
|
|
|
* Process any pending requests asynchronously (i.e. off an event) if there
|
2007-01-09 04:07:29 +03:00
|
|
|
* are any. Note that this is a no-op if there aren't any currently pending
|
|
|
|
* requests.
|
2009-01-15 22:49:20 +03:00
|
|
|
*
|
|
|
|
* This function is virtual to allow cross-library calls to SetEnabled()
|
2007-01-09 04:07:29 +03:00
|
|
|
*/
|
2007-01-09 05:02:39 +03:00
|
|
|
virtual void ProcessPendingRequestsAsync();
|
2007-01-09 04:07:29 +03:00
|
|
|
|
2007-09-28 19:15:07 +04:00
|
|
|
/**
|
|
|
|
* If true, the loader is ready to execute scripts, and so are all its
|
|
|
|
* ancestors. If the loader itself is ready but some ancestor is not, this
|
|
|
|
* function will add an execute blocker and ask the ancestor to remove it
|
|
|
|
* once it becomes ready.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ReadyToExecuteScripts();
|
2007-09-28 19:15:07 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether just this loader is ready to execute scripts.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool SelfReadyToExecuteScripts()
|
2006-11-04 08:45:02 +03:00
|
|
|
{
|
2007-01-09 04:07:29 +03:00
|
|
|
return mEnabled && !mBlockerCount;
|
2006-11-04 08:45:02 +03:00
|
|
|
}
|
|
|
|
|
2013-09-12 03:42:09 +04:00
|
|
|
nsresult AttemptAsyncScriptParse(nsScriptLoadRequest* aRequest);
|
|
|
|
nsresult ProcessRequest(nsScriptLoadRequest* aRequest,
|
|
|
|
void **aOffThreadToken = nullptr);
|
2001-05-17 09:20:51 +04:00
|
|
|
void FireScriptAvailable(nsresult aResult,
|
2006-11-04 08:45:02 +03:00
|
|
|
nsScriptLoadRequest* aRequest);
|
2001-05-17 09:20:51 +04:00
|
|
|
void FireScriptEvaluated(nsresult aResult,
|
|
|
|
nsScriptLoadRequest* aRequest);
|
|
|
|
nsresult EvaluateScript(nsScriptLoadRequest* aRequest,
|
2014-04-25 18:11:56 +04:00
|
|
|
JS::SourceBufferHolder& aSrcBuf,
|
2013-09-12 03:42:09 +04:00
|
|
|
void **aOffThreadToken);
|
|
|
|
|
2013-12-31 20:30:07 +04:00
|
|
|
already_AddRefed<nsIScriptGlobalObject> GetScriptGlobalObject();
|
2014-07-03 22:12:03 +04:00
|
|
|
void FillCompileOptionsForRequest(const mozilla::dom::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);
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
nsresult PrepareLoadedRequest(nsScriptLoadRequest* aRequest,
|
|
|
|
nsIStreamLoader* aLoader,
|
|
|
|
nsresult aStatus,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aStringLen,
|
|
|
|
const uint8_t* aString);
|
2005-12-02 07:32:18 +03:00
|
|
|
|
2014-04-12 13:02:21 +04:00
|
|
|
void AddDeferRequest(nsScriptLoadRequest* aRequest);
|
|
|
|
bool MaybeRemovedDeferRequests();
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
nsIDocument* mDocument; // [WEAK]
|
2003-07-12 02:47:17 +04:00
|
|
|
nsCOMArray<nsIScriptLoaderObserver> mObservers;
|
2015-04-09 05:50:45 +03:00
|
|
|
nsScriptLoadRequestList mNonAsyncExternalScriptInsertedRequests;
|
2015-04-09 05:50:46 +03:00
|
|
|
// mLoadingAsyncRequests holds async requests while they're loading; when they
|
|
|
|
// have been loaded they are moved to mLoadedAsyncRequests.
|
|
|
|
nsScriptLoadRequestList mLoadingAsyncRequests;
|
|
|
|
nsScriptLoadRequestList mLoadedAsyncRequests;
|
2015-04-09 05:50:45 +03:00
|
|
|
nsScriptLoadRequestList mDeferRequests;
|
|
|
|
nsScriptLoadRequestList mXSLTRequests;
|
2010-09-20 16:12:06 +04:00
|
|
|
nsRefPtr<nsScriptLoadRequest> mParserBlockingRequest;
|
2008-10-01 10:48:47 +04:00
|
|
|
|
|
|
|
// In mRequests, the additional information here is stored by the element.
|
|
|
|
struct PreloadInfo {
|
|
|
|
nsRefPtr<nsScriptLoadRequest> mRequest;
|
|
|
|
nsString mCharset;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PreloadRequestComparator {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Equals(const PreloadInfo &aPi, nsScriptLoadRequest * const &aRequest)
|
2008-10-01 10:48:47 +04:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return aRequest == aPi.mRequest;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct PreloadURIComparator {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Equals(const PreloadInfo &aPi, nsIURI * const &aURI) const;
|
2008-10-01 10:48:47 +04:00
|
|
|
};
|
|
|
|
nsTArray<PreloadInfo> mPreloads;
|
|
|
|
|
2005-06-07 23:21:05 +04:00
|
|
|
nsCOMPtr<nsIScriptElement> mCurrentScript;
|
2010-09-06 11:41:26 +04:00
|
|
|
nsCOMPtr<nsIScriptElement> mCurrentParserInsertedScript;
|
2007-09-28 19:15:07 +04:00
|
|
|
// XXXbz do we want to cycle-collect these or something? Not sure.
|
|
|
|
nsTArray< nsRefPtr<nsScriptLoader> > mPendingChildLoaders;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mBlockerCount;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mEnabled;
|
|
|
|
bool mDeferEnabled;
|
|
|
|
bool mDocumentParsingDone;
|
2014-04-12 13:02:21 +04:00
|
|
|
bool mBlockingDOMContentLoaded;
|
2001-05-17 09:20:51 +04:00
|
|
|
};
|
|
|
|
|
2011-08-11 00:53:14 +04:00
|
|
|
class nsAutoScriptLoaderDisabler
|
|
|
|
{
|
|
|
|
public:
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit nsAutoScriptLoaderDisabler(nsIDocument* aDoc)
|
2011-08-11 00:53:14 +04:00
|
|
|
{
|
|
|
|
mLoader = aDoc->ScriptLoader();
|
|
|
|
mWasEnabled = mLoader->GetEnabled();
|
|
|
|
if (mWasEnabled) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mLoader->SetEnabled(false);
|
2011-08-11 00:53:14 +04:00
|
|
|
}
|
|
|
|
}
|
2013-12-31 20:30:07 +04:00
|
|
|
|
2011-08-11 00:53:14 +04:00
|
|
|
~nsAutoScriptLoaderDisabler()
|
|
|
|
{
|
|
|
|
if (mWasEnabled) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mLoader->SetEnabled(true);
|
2011-08-11 00:53:14 +04:00
|
|
|
}
|
|
|
|
}
|
2013-12-31 20:30:07 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mWasEnabled;
|
2011-08-11 00:53:14 +04:00
|
|
|
nsRefPtr<nsScriptLoader> mLoader;
|
|
|
|
};
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
#endif //__nsScriptLoader_h__
|