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
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
#ifndef mozilla_dom_ScriptLoader_h
|
|
|
|
#define mozilla_dom_ScriptLoader_h
|
2001-05-17 09:20:51 +04:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2016-05-07 14:46:38 +03:00
|
|
|
#include "nsRefPtrHashtable.h"
|
2017-04-27 13:27:03 +03:00
|
|
|
#include "mozilla/Encoding.h"
|
2004-07-07 02:52:03 +04:00
|
|
|
#include "nsIScriptElement.h"
|
2003-07-12 02:47:17 +04:00
|
|
|
#include "nsCOMArray.h"
|
2016-05-07 14:46:38 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2007-09-28 19:15:07 +04:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2017-04-21 19:57:58 +03:00
|
|
|
#include "nsICacheInfoChannel.h"
|
2001-05-17 09:20:51 +04:00
|
|
|
#include "nsIDocument.h"
|
2015-11-30 17:54:11 +03:00
|
|
|
#include "nsIIncrementalStreamLoader.h"
|
2016-05-07 14:46:38 +03:00
|
|
|
#include "nsURIHashKey.h"
|
2015-04-09 05:50:45 +03:00
|
|
|
#include "mozilla/CORSMode.h"
|
2017-05-08 09:24:44 +03:00
|
|
|
#include "mozilla/dom/ScriptLoadRequest.h"
|
2015-08-13 06:19:11 +03:00
|
|
|
#include "mozilla/dom/SRIMetadata.h"
|
2015-11-30 17:54:52 +03:00
|
|
|
#include "mozilla/dom/SRICheck.h"
|
2016-05-07 14:46:38 +03:00
|
|
|
#include "mozilla/MozPromise.h"
|
2014-11-18 16:46:43 +03:00
|
|
|
#include "mozilla/net/ReferrerPolicy.h"
|
2015-11-30 17:54:52 +03:00
|
|
|
#include "mozilla/Vector.h"
|
2001-05-17 09:20:51 +04:00
|
|
|
|
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;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace JS
|
2014-04-25 18:11:56 +04:00
|
|
|
|
2014-07-03 22:12:03 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2017-05-08 09:24:22 +03:00
|
|
|
|
2014-07-03 22:12:03 +04:00
|
|
|
class AutoJSAPI;
|
2017-05-08 09:24:22 +03:00
|
|
|
class ModuleLoadRequest;
|
|
|
|
class ModuleScript;
|
2017-05-08 09:24:44 +03:00
|
|
|
class ScriptLoadHandler;
|
|
|
|
class ScriptRequestProcessor;
|
2015-04-09 05:50:45 +03:00
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// Script loader implementation
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
class ScriptLoader final : public nsISupports
|
2001-05-17 09:20:51 +04:00
|
|
|
{
|
2014-05-21 21:08:12 +04:00
|
|
|
class MOZ_STACK_CLASS AutoCurrentScriptUpdater
|
|
|
|
{
|
|
|
|
public:
|
2017-05-08 09:24:22 +03:00
|
|
|
AutoCurrentScriptUpdater(ScriptLoader* aScriptLoader,
|
2014-05-21 21:08:12 +04:00
|
|
|
nsIScriptElement* aCurrentScript)
|
|
|
|
: mOldScript(aScriptLoader->mCurrentScript)
|
|
|
|
, mScriptLoader(aScriptLoader)
|
|
|
|
{
|
|
|
|
mScriptLoader->mCurrentScript = aCurrentScript;
|
|
|
|
}
|
2017-05-08 09:25:03 +03:00
|
|
|
|
2014-05-21 21:08:12 +04:00
|
|
|
~AutoCurrentScriptUpdater()
|
|
|
|
{
|
|
|
|
mScriptLoader->mCurrentScript.swap(mOldScript);
|
|
|
|
}
|
2017-05-08 09:25:03 +03:00
|
|
|
|
2014-05-21 21:08:12 +04:00
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIScriptElement> mOldScript;
|
2017-05-08 09:24:22 +03:00
|
|
|
ScriptLoader* mScriptLoader;
|
2014-05-21 21:08:12 +04:00
|
|
|
};
|
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
friend class ModuleLoadRequest;
|
|
|
|
friend class ScriptRequestProcessor;
|
|
|
|
friend class ScriptLoadHandler;
|
2014-05-21 21:08:12 +04:00
|
|
|
friend class AutoCurrentScriptUpdater;
|
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
public:
|
2017-05-08 09:24:22 +03:00
|
|
|
explicit ScriptLoader(nsIDocument* aDocument);
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2016-05-07 14:46:38 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2017-05-08 09:24:22 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(ScriptLoader)
|
2001-05-17 09:20:51 +04:00
|
|
|
|
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
|
|
|
/**
|
2017-01-31 23:03:57 +03:00
|
|
|
* Process a script element. This will include both loading the
|
2006-11-04 08:45:02 +03:00
|
|
|
* 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.
|
2017-01-31 23:03:57 +03:00
|
|
|
* 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;
|
|
|
|
}
|
2017-05-08 09:25:03 +03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-21 06:13:18 +03:00
|
|
|
* Add/remove a blocker for parser-blocking scripts (and XSLT
|
|
|
|
* scripts). Blockers will stop such scripts from executing, but not from
|
|
|
|
* loading.
|
2006-11-04 08:45:02 +03:00
|
|
|
*/
|
2016-05-21 06:13:18 +03:00
|
|
|
void AddParserBlockingScriptExecutionBlocker()
|
2006-11-04 08:45:02 +03:00
|
|
|
{
|
2016-05-21 06:13:18 +03:00
|
|
|
++mParserBlockingBlockerCount;
|
2006-11-04 08:45:02 +03:00
|
|
|
}
|
2017-05-08 09:25:03 +03:00
|
|
|
|
2016-05-21 06:13:18 +03:00
|
|
|
void RemoveParserBlockingScriptExecutionBlocker()
|
2006-11-04 08:45:02 +03:00
|
|
|
{
|
2016-05-21 06:13:18 +03:00
|
|
|
if (!--mParserBlockingBlockerCount && ReadyToExecuteScripts()) {
|
|
|
|
ProcessPendingRequestsAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add/remove a blocker for execution of all scripts. Blockers will stop
|
|
|
|
* scripts from executing, but not from loading.
|
|
|
|
*/
|
|
|
|
void AddExecuteBlocker()
|
|
|
|
{
|
|
|
|
++mBlockerCount;
|
|
|
|
}
|
2017-05-08 09:25:03 +03:00
|
|
|
|
2016-05-21 06:13:18 +03:00
|
|
|
void RemoveExecuteBlocker()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mBlockerCount);
|
|
|
|
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
|
|
|
|
2017-03-17 05:10:40 +03:00
|
|
|
static inline nsresult
|
|
|
|
ConvertToUTF16(nsIChannel* aChannel, const uint8_t* aData,
|
|
|
|
uint32_t aLength, const nsAString& aHintCharset,
|
|
|
|
nsIDocument* aDocument,
|
|
|
|
JS::UniqueTwoByteChars& aBufOut, size_t& aLengthOut)
|
|
|
|
{
|
|
|
|
char16_t* bufOut;
|
2017-05-08 09:25:03 +03:00
|
|
|
nsresult rv = ConvertToUTF16(aChannel, aData, aLength, aHintCharset,
|
|
|
|
aDocument, bufOut, aLengthOut);
|
2017-03-17 05:10:40 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
aBufOut.reset(bufOut);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
};
|
|
|
|
|
2015-12-01 17:00:58 +03:00
|
|
|
/**
|
|
|
|
* Handle the completion of a stream. This is called by the
|
2017-05-08 09:24:22 +03:00
|
|
|
* ScriptLoadHandler object which observes the IncrementalStreamLoader
|
2017-01-31 23:03:57 +03:00
|
|
|
* loading the script. The streamed content is expected to be stored on the
|
2017-01-31 23:03:57 +03:00
|
|
|
* aRequest argument.
|
2015-12-01 17:00:58 +03:00
|
|
|
*/
|
2016-12-17 19:31:13 +03:00
|
|
|
nsresult OnStreamComplete(nsIIncrementalStreamLoader* aLoader,
|
2017-05-08 09:24:22 +03:00
|
|
|
ScriptLoadRequest* aRequest,
|
2015-11-30 17:54:52 +03:00
|
|
|
nsresult aChannelStatus,
|
|
|
|
nsresult aSRIStatus,
|
|
|
|
mozilla::dom::SRICheckDataVerifier* aSRIDataVerifier);
|
2015-12-01 17:00:58 +03:00
|
|
|
|
2017-04-21 19:57:58 +03:00
|
|
|
/**
|
|
|
|
* Returns wether any request is queued, and not executed yet.
|
|
|
|
*/
|
|
|
|
bool HasPendingRequests();
|
|
|
|
|
2007-01-09 04:07:29 +03:00
|
|
|
/**
|
|
|
|
* Processes any pending requests that are ready for processing.
|
|
|
|
*/
|
|
|
|
void ProcessPendingRequests();
|
|
|
|
|
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.
|
2015-08-13 06:19:11 +03:00
|
|
|
* @param aIntegrity The expect hash url, if avail, of the request
|
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
|
|
|
*/
|
2017-05-08 09:25:03 +03:00
|
|
|
virtual void PreloadURI(nsIURI* aURI, const nsAString& aCharset,
|
|
|
|
const nsAString& aType,
|
|
|
|
const nsAString& aCrossOrigin,
|
2015-08-13 06:19:11 +03:00
|
|
|
const nsAString& aIntegrity,
|
2017-08-19 12:35:00 +03:00
|
|
|
bool aScriptFromHead, bool aAsync, bool aDefer,
|
2014-11-18 16:46:43 +03:00
|
|
|
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.
|
|
|
|
*/
|
2017-05-08 09:25:03 +03:00
|
|
|
nsresult ProcessOffThreadRequest(ScriptLoadRequest* aRequest);
|
2013-09-12 03:42:09 +04:00
|
|
|
|
2017-05-08 09:25:03 +03:00
|
|
|
bool AddPendingChildLoader(ScriptLoader* aChild)
|
|
|
|
{
|
2014-10-01 16:13:53 +04:00
|
|
|
return mPendingChildLoaders.AppendElement(aChild) != nullptr;
|
|
|
|
}
|
|
|
|
|
2016-09-19 21:59:54 +03:00
|
|
|
mozilla::dom::DocGroup* GetDocGroup() const
|
|
|
|
{
|
|
|
|
return mDocument->GetDocGroup();
|
|
|
|
}
|
|
|
|
|
2017-04-21 19:57:58 +03:00
|
|
|
/**
|
|
|
|
* Register the fact that we saw the load event, and that we need to save the
|
|
|
|
* bytecode at the next loop cycle unless new scripts are waiting in the
|
|
|
|
* pipeline.
|
|
|
|
*/
|
|
|
|
void LoadEventFired();
|
|
|
|
|
2017-08-22 21:06:37 +03:00
|
|
|
/**
|
|
|
|
* Destroy and prevent the ScriptLoader or the ScriptLoadRequests from owning
|
|
|
|
* any references to the JSScript or to the Request which might be used for
|
|
|
|
* caching the encoded bytecode.
|
|
|
|
*/
|
|
|
|
void Destroy()
|
|
|
|
{
|
|
|
|
GiveUpBytecodeEncoding();
|
|
|
|
}
|
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
private:
|
2017-05-08 09:24:22 +03:00
|
|
|
virtual ~ScriptLoader();
|
2014-06-25 06:09:15 +04:00
|
|
|
|
2017-05-08 09:25:03 +03:00
|
|
|
ScriptLoadRequest* CreateLoadRequest(ScriptKind aKind,
|
|
|
|
nsIScriptElement* aElement,
|
2017-11-19 01:16:09 +03:00
|
|
|
ValidJSVersion aValidJSVersion,
|
2017-05-08 09:25:03 +03:00
|
|
|
mozilla::CORSMode aCORSMode,
|
|
|
|
const mozilla::dom::SRIMetadata& aIntegrity);
|
2016-05-07 14:46:38 +03:00
|
|
|
|
2012-01-20 15:16:27 +04:00
|
|
|
/**
|
|
|
|
* Unblocks the creator parser of the parser-blocking scripts.
|
|
|
|
*/
|
2017-05-08 09:24:22 +03:00
|
|
|
void UnblockParser(ScriptLoadRequest* aParserBlockingRequest);
|
2012-01-20 15:16:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously resumes the creator parser of the parser-blocking scripts.
|
|
|
|
*/
|
2017-05-08 09:24:22 +03:00
|
|
|
void ContinueParserAsync(ScriptLoadRequest* aParserBlockingRequest);
|
2012-01-20 15:16:27 +04:00
|
|
|
|
|
|
|
|
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,
|
2017-05-08 09:25:03 +03:00
|
|
|
nsISupports* aContext,
|
|
|
|
nsIURI* aURI,
|
|
|
|
const nsAString& aType,
|
2015-09-21 00:56:21 +03:00
|
|
|
bool aIsPreLoad);
|
2008-11-19 10:05:05 +03:00
|
|
|
|
2008-10-01 10:48:47 +04:00
|
|
|
/**
|
|
|
|
* Start a load for aRequest's URI.
|
|
|
|
*/
|
2017-05-08 09:25:03 +03:00
|
|
|
nsresult StartLoad(ScriptLoadRequest* aRequest);
|
2008-10-01 10:48:47 +04:00
|
|
|
|
2017-04-21 19:57:57 +03:00
|
|
|
/**
|
|
|
|
* Abort the current stream, and re-start with a new load request from scratch
|
|
|
|
* without requesting any alternate data. Returns NS_BINDING_RETARGETED on
|
|
|
|
* success, as this error code is used to abort the input stream.
|
|
|
|
*/
|
2017-05-08 09:25:03 +03:00
|
|
|
nsresult RestartLoad(ScriptLoadRequest* aRequest);
|
2017-04-21 19:57:57 +03: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
|
|
|
/**
|
2016-05-21 06:13:18 +03:00
|
|
|
* If true, the loader is ready to execute parser-blocking 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
|
2007-09-28 19:15:07 +04:00
|
|
|
* once it becomes ready.
|
|
|
|
*/
|
2016-05-21 06:13:18 +03:00
|
|
|
bool ReadyToExecuteParserBlockingScripts();
|
2007-09-28 19:15:07 +04:00
|
|
|
|
|
|
|
/**
|
2016-05-21 06:13:18 +03:00
|
|
|
* Return whether just this loader is ready to execute parser-blocking
|
|
|
|
* scripts.
|
2007-09-28 19:15:07 +04:00
|
|
|
*/
|
2016-05-21 06:13:18 +03:00
|
|
|
bool SelfReadyToExecuteParserBlockingScripts()
|
2006-11-04 08:45:02 +03:00
|
|
|
{
|
2016-05-21 06:13:18 +03:00
|
|
|
return ReadyToExecuteScripts() && !mParserBlockingBlockerCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether this loader is ready to execute scripts in general.
|
|
|
|
*/
|
|
|
|
bool ReadyToExecuteScripts()
|
|
|
|
{
|
|
|
|
return mEnabled && !mBlockerCount;
|
2006-11-04 08:45:02 +03:00
|
|
|
}
|
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
nsresult AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest);
|
|
|
|
nsresult ProcessRequest(ScriptLoadRequest* aRequest);
|
|
|
|
nsresult CompileOffThreadOrProcessRequest(ScriptLoadRequest* aRequest);
|
2001-05-17 09:20:51 +04:00
|
|
|
void FireScriptAvailable(nsresult aResult,
|
2017-05-08 09:24:22 +03:00
|
|
|
ScriptLoadRequest* aRequest);
|
2001-05-17 09:20:51 +04:00
|
|
|
void FireScriptEvaluated(nsresult aResult,
|
2017-05-08 09:24:22 +03:00
|
|
|
ScriptLoadRequest* aRequest);
|
|
|
|
nsresult EvaluateScript(ScriptLoadRequest* aRequest);
|
2013-09-12 03:42:09 +04:00
|
|
|
|
2017-04-21 19:57:58 +03:00
|
|
|
/**
|
|
|
|
* Queue the current script load request to be saved, when the page
|
|
|
|
* initialization ends. The page initialization end is defined as being the
|
|
|
|
* time when the load event got received, and when no more scripts are waiting
|
|
|
|
* to be executed.
|
|
|
|
*/
|
2017-05-08 09:24:22 +03:00
|
|
|
void RegisterForBytecodeEncoding(ScriptLoadRequest* aRequest);
|
2017-04-21 19:57:58 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if all conditions are met, i-e that the onLoad event fired and that
|
|
|
|
* no more script have to be processed. If all conditions are met, queue an
|
|
|
|
* event to encode all the bytecode and save them on the cache.
|
|
|
|
*/
|
|
|
|
void MaybeTriggerBytecodeEncoding();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterate over all script load request and save the bytecode of executed
|
|
|
|
* functions on the cache provided by the channel.
|
|
|
|
*/
|
|
|
|
void EncodeBytecode();
|
2017-05-08 09:24:22 +03:00
|
|
|
void EncodeRequestBytecode(JSContext* aCx, ScriptLoadRequest* aRequest);
|
2017-04-21 19:57:58 +03:00
|
|
|
|
|
|
|
void GiveUpBytecodeEncoding();
|
|
|
|
|
2013-12-31 20:30:07 +04:00
|
|
|
already_AddRefed<nsIScriptGlobalObject> GetScriptGlobalObject();
|
2016-09-19 16:50:22 +03:00
|
|
|
nsresult FillCompileOptionsForRequest(const mozilla::dom::AutoJSAPI& jsapi,
|
2017-05-08 09:24:22 +03:00
|
|
|
ScriptLoadRequest* aRequest,
|
2016-09-19 16:50:22 +03:00
|
|
|
JS::Handle<JSObject*> aScopeChain,
|
|
|
|
JS::CompileOptions* aOptions);
|
2001-05-17 09:20:51 +04:00
|
|
|
|
2015-09-29 17:41:55 +03:00
|
|
|
uint32_t NumberOfProcessors();
|
2017-05-08 09:24:22 +03:00
|
|
|
nsresult PrepareLoadedRequest(ScriptLoadRequest* aRequest,
|
2016-12-17 19:31:13 +03:00
|
|
|
nsIIncrementalStreamLoader* aLoader,
|
2017-01-31 23:03:57 +03:00
|
|
|
nsresult aStatus);
|
2005-12-02 07:32:18 +03:00
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
void AddDeferRequest(ScriptLoadRequest* aRequest);
|
2014-04-12 13:02:21 +04:00
|
|
|
bool MaybeRemovedDeferRequests();
|
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
void MaybeMoveToLoadedList(ScriptLoadRequest* aRequest);
|
2016-05-07 14:46:38 +03:00
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
JS::SourceBufferHolder GetScriptSource(ScriptLoadRequest* aRequest,
|
2016-05-07 14:46:38 +03:00
|
|
|
nsAutoString& inlineData);
|
|
|
|
|
2017-02-08 20:08:33 +03:00
|
|
|
bool ModuleScriptsEnabled();
|
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
void SetModuleFetchStarted(ModuleLoadRequest *aRequest);
|
2017-05-08 09:25:03 +03:00
|
|
|
void SetModuleFetchFinishedAndResumeWaitingRequests(ModuleLoadRequest* aRequest,
|
2016-05-07 14:46:38 +03:00
|
|
|
nsresult aResult);
|
|
|
|
|
2017-05-08 09:25:03 +03:00
|
|
|
bool IsFetchingModule(ModuleLoadRequest* aRequest) const;
|
2016-05-07 14:46:38 +03:00
|
|
|
|
2017-12-15 00:13:57 +03:00
|
|
|
bool ModuleMapContainsURL(nsIURI* aURL) const;
|
|
|
|
RefPtr<mozilla::GenericPromise> WaitForModuleFetch(nsIURI* aURL);
|
2017-05-08 09:24:22 +03:00
|
|
|
ModuleScript* GetFetchedModule(nsIURI* aURL) const;
|
2016-05-07 14:46:38 +03:00
|
|
|
|
|
|
|
friend bool
|
|
|
|
HostResolveImportedModule(JSContext* aCx, unsigned argc, JS::Value* vp);
|
|
|
|
|
2017-06-05 15:06:20 +03:00
|
|
|
// Returns wether we should save the bytecode of this script after the
|
|
|
|
// execution of the script.
|
2017-05-08 09:24:44 +03:00
|
|
|
static bool
|
2017-06-05 15:06:20 +03:00
|
|
|
ShouldCacheBytecode(ScriptLoadRequest* aRequest);
|
2017-05-18 17:12:15 +03:00
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
nsresult CreateModuleScript(ModuleLoadRequest* aRequest);
|
|
|
|
nsresult ProcessFetchedModuleSource(ModuleLoadRequest* aRequest);
|
2017-08-22 12:34:14 +03:00
|
|
|
void CheckModuleDependenciesLoaded(ModuleLoadRequest* aRequest);
|
2017-05-08 09:24:22 +03:00
|
|
|
void ProcessLoadedModuleTree(ModuleLoadRequest* aRequest);
|
2017-08-22 12:34:14 +03:00
|
|
|
bool InstantiateModuleTree(ModuleLoadRequest* aRequest);
|
2017-12-06 17:54:58 +03:00
|
|
|
JS::Value FindFirstParseError(ModuleLoadRequest* aRequest);
|
2017-05-08 09:24:22 +03:00
|
|
|
void StartFetchingModuleDependencies(ModuleLoadRequest* aRequest);
|
2016-05-07 14:46:38 +03:00
|
|
|
|
|
|
|
RefPtr<mozilla::GenericPromise>
|
2017-05-08 09:24:22 +03:00
|
|
|
StartFetchingModuleAndDependencies(ModuleLoadRequest* aRequest, nsIURI* aURI);
|
2016-05-07 14:46:38 +03:00
|
|
|
|
2001-05-17 09:20:51 +04:00
|
|
|
nsIDocument* mDocument; // [WEAK]
|
2003-07-12 02:47:17 +04:00
|
|
|
nsCOMArray<nsIScriptLoaderObserver> mObservers;
|
2017-05-08 09:24:22 +03:00
|
|
|
ScriptLoadRequestList 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.
|
2017-05-08 09:24:22 +03:00
|
|
|
ScriptLoadRequestList mLoadingAsyncRequests;
|
|
|
|
ScriptLoadRequestList mLoadedAsyncRequests;
|
|
|
|
ScriptLoadRequestList mDeferRequests;
|
|
|
|
ScriptLoadRequestList mXSLTRequests;
|
|
|
|
RefPtr<ScriptLoadRequest> mParserBlockingRequest;
|
2008-10-01 10:48:47 +04:00
|
|
|
|
2017-04-21 19:57:58 +03:00
|
|
|
// List of script load request that are holding a buffer which has to be saved
|
|
|
|
// on the cache.
|
2017-05-08 09:24:22 +03:00
|
|
|
ScriptLoadRequestList mBytecodeEncodingQueue;
|
2017-04-21 19:57:58 +03:00
|
|
|
|
2008-10-01 10:48:47 +04:00
|
|
|
// In mRequests, the additional information here is stored by the element.
|
2017-05-08 09:25:03 +03:00
|
|
|
struct PreloadInfo
|
|
|
|
{
|
2017-05-08 09:24:22 +03:00
|
|
|
RefPtr<ScriptLoadRequest> mRequest;
|
2008-10-01 10:48:47 +04:00
|
|
|
nsString mCharset;
|
|
|
|
};
|
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
friend void ImplCycleCollectionUnlink(ScriptLoader::PreloadInfo& aField);
|
2016-05-07 14:46:38 +03:00
|
|
|
friend void ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
2017-05-08 09:24:22 +03:00
|
|
|
ScriptLoader::PreloadInfo& aField,
|
2016-05-07 14:46:38 +03:00
|
|
|
const char* aName, uint32_t aFlags);
|
|
|
|
|
2017-05-08 09:25:03 +03:00
|
|
|
struct PreloadRequestComparator
|
|
|
|
{
|
|
|
|
bool Equals(const PreloadInfo& aPi, ScriptLoadRequest* const& aRequest) const
|
2008-10-01 10:48:47 +04:00
|
|
|
{
|
|
|
|
return aRequest == aPi.mRequest;
|
|
|
|
}
|
|
|
|
};
|
2017-05-08 09:25:03 +03:00
|
|
|
|
|
|
|
struct PreloadURIComparator
|
|
|
|
{
|
|
|
|
bool Equals(const PreloadInfo& aPi, nsIURI* const &aURI) const;
|
2008-10-01 10:48:47 +04:00
|
|
|
};
|
2017-05-08 09:25:03 +03:00
|
|
|
|
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;
|
2017-05-08 09:24:22 +03:00
|
|
|
nsTArray< RefPtr<ScriptLoader> > mPendingChildLoaders;
|
2016-05-21 06:13:18 +03:00
|
|
|
uint32_t mParserBlockingBlockerCount;
|
2016-05-21 06:13:18 +03:00
|
|
|
uint32_t mBlockerCount;
|
2015-09-29 17:41:55 +03:00
|
|
|
uint32_t mNumberOfProcessors;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mEnabled;
|
|
|
|
bool mDeferEnabled;
|
|
|
|
bool mDocumentParsingDone;
|
2014-04-12 13:02:21 +04:00
|
|
|
bool mBlockingDOMContentLoaded;
|
2017-04-21 19:57:58 +03:00
|
|
|
bool mLoadEventFired;
|
2017-08-22 21:06:37 +03:00
|
|
|
bool mGiveUpEncoding;
|
2016-05-07 14:46:38 +03:00
|
|
|
|
|
|
|
// Module map
|
|
|
|
nsRefPtrHashtable<nsURIHashKey, mozilla::GenericPromise::Private> mFetchingModules;
|
2017-05-08 09:24:22 +03:00
|
|
|
nsRefPtrHashtable<nsURIHashKey, ModuleScript> mFetchedModules;
|
2016-09-08 04:59:40 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIConsoleReportCollector> mReporter;
|
2015-11-30 17:54:52 +03:00
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
// Logging
|
|
|
|
static LazyLogModule gCspPRLog;
|
|
|
|
static LazyLogModule gScriptLoaderLog;
|
2015-12-01 17:00:58 +03: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;
|
2017-05-08 09:24:22 +03:00
|
|
|
RefPtr<ScriptLoader> mLoader;
|
2011-08-11 00:53:14 +04:00
|
|
|
};
|
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_ScriptLoader_h
|