2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsIScriptElement_h___
|
|
|
|
#define nsIScriptElement_h___
|
|
|
|
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIScriptLoaderObserver.h"
|
2009-10-15 04:29:11 -07:00
|
|
|
#include "nsWeakPtr.h"
|
|
|
|
#include "nsIParser.h"
|
2010-09-01 04:41:12 -07:00
|
|
|
#include "nsContentCreatorFunctions.h"
|
2010-10-27 05:04:09 -07:00
|
|
|
#include "nsIDOMHTMLScriptElement.h"
|
2012-03-10 08:13:52 -08:00
|
|
|
#include "mozilla/CORSMode.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#define NS_ISCRIPTELEMENT_IID \
|
2012-09-18 20:24:27 -07:00
|
|
|
{ 0x491628bc, 0xce7c, 0x4db4, \
|
|
|
|
{ 0x93, 0x3f, 0xce, 0x1b, 0x75, 0xee, 0x75, 0xce } }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal interface implemented by script elements
|
|
|
|
*/
|
|
|
|
class nsIScriptElement : public nsIScriptLoaderObserver {
|
|
|
|
public:
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_ISCRIPTELEMENT_IID)
|
|
|
|
|
2010-10-25 05:17:38 -07:00
|
|
|
nsIScriptElement(mozilla::dom::FromParser aFromParser)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mLineNumber(0),
|
2011-10-17 07:59:28 -07:00
|
|
|
mAlreadyStarted(false),
|
|
|
|
mMalformed(false),
|
2010-11-01 03:50:01 -07:00
|
|
|
mDoneAddingChildren(aFromParser == mozilla::dom::NOT_FROM_PARSER ||
|
|
|
|
aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT),
|
2010-10-27 05:04:09 -07:00
|
|
|
mForceAsync(aFromParser == mozilla::dom::NOT_FROM_PARSER ||
|
|
|
|
aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT),
|
2011-10-17 07:59:28 -07:00
|
|
|
mFrozen(false),
|
|
|
|
mDefer(false),
|
|
|
|
mAsync(false),
|
|
|
|
mExternal(false),
|
2010-10-27 00:18:24 -07:00
|
|
|
mParserCreated(aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT ?
|
|
|
|
mozilla::dom::NOT_FROM_PARSER : aFromParser),
|
|
|
|
// Fragment parser-created scripts (if executable)
|
|
|
|
// behave like script-created scripts.
|
2012-07-30 07:20:58 -07:00
|
|
|
mCreatorParser(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Content type identifying the scripting language. Can be empty, in
|
|
|
|
* which case javascript will be assumed.
|
|
|
|
*/
|
|
|
|
virtual void GetScriptType(nsAString& type) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Location of script source text. Can return null, in which case
|
|
|
|
* this is assumed to be an inline script element.
|
|
|
|
*/
|
2009-11-17 00:52:30 -08:00
|
|
|
nsIURI* GetScriptURI()
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(mFrozen, "Not ready for this call yet!");
|
|
|
|
return mUri;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Script source text for inline script elements.
|
|
|
|
*/
|
|
|
|
virtual void GetScriptText(nsAString& text) = 0;
|
|
|
|
|
|
|
|
virtual void GetScriptCharset(nsAString& charset) = 0;
|
2008-07-25 19:42:12 -07:00
|
|
|
|
2009-11-17 00:52:30 -08:00
|
|
|
/**
|
|
|
|
* Freezes the return values of GetScriptDeferred(), GetScriptAsync() and
|
|
|
|
* GetScriptURI() so that subsequent modifications to the attributes don't
|
|
|
|
* change execution behavior.
|
|
|
|
*/
|
|
|
|
virtual void FreezeUriAsyncDefer() = 0;
|
|
|
|
|
2008-07-25 19:42:12 -07:00
|
|
|
/**
|
|
|
|
* Is the script deferred. Currently only supported by HTML scripts.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool GetScriptDeferred()
|
2009-11-17 00:52:30 -08:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mFrozen, "Not ready for this call yet!");
|
|
|
|
return mDefer;
|
|
|
|
}
|
2008-07-25 19:42:12 -07:00
|
|
|
|
2009-11-09 17:04:24 -08:00
|
|
|
/**
|
|
|
|
* Is the script async. Currently only supported by HTML scripts.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool GetScriptAsync()
|
2009-11-17 00:52:30 -08:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mFrozen, "Not ready for this call yet!");
|
|
|
|
return mAsync;
|
|
|
|
}
|
2009-11-09 17:04:24 -08:00
|
|
|
|
2010-10-27 00:03:10 -07:00
|
|
|
/**
|
|
|
|
* Is the script an external script?
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool GetScriptExternal()
|
2010-10-27 00:03:10 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mFrozen, "Not ready for this call yet!");
|
|
|
|
return mExternal;
|
|
|
|
}
|
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
/**
|
2010-10-25 05:17:38 -07:00
|
|
|
* Returns how the element was created.
|
2010-09-01 04:41:12 -07:00
|
|
|
*/
|
2010-10-25 05:17:38 -07:00
|
|
|
mozilla::dom::FromParser GetParserCreated()
|
2010-09-01 04:41:12 -07:00
|
|
|
{
|
|
|
|
return mParserCreated;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void SetScriptLineNumber(uint32_t aLineNumber)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mLineNumber = aLineNumber;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t GetScriptLineNumber()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mLineNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetIsMalformed()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mMalformed = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsMalformed()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mMalformed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreventExecution()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mAlreadyStarted = true;
|
2010-09-01 04:41:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoseParserInsertedness()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mFrozen = false;
|
2012-07-30 07:20:58 -07:00
|
|
|
mUri = nullptr;
|
|
|
|
mCreatorParser = nullptr;
|
2010-10-25 05:17:38 -07:00
|
|
|
mParserCreated = mozilla::dom::NOT_FROM_PARSER;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool async = false;
|
2010-10-27 05:04:09 -07:00
|
|
|
nsCOMPtr<nsIDOMHTMLScriptElement> htmlScript = do_QueryInterface(this);
|
|
|
|
if (htmlScript) {
|
|
|
|
htmlScript->GetAsync(&async);
|
|
|
|
}
|
|
|
|
mForceAsync = !async;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-10-15 04:29:11 -07:00
|
|
|
void SetCreatorParser(nsIParser* aParser)
|
|
|
|
{
|
|
|
|
mCreatorParser = getter_AddRefs(NS_GetWeakReference(aParser));
|
|
|
|
}
|
|
|
|
|
2012-01-20 03:16:27 -08:00
|
|
|
/**
|
|
|
|
* Unblocks the creator parser
|
|
|
|
*/
|
|
|
|
void UnblockParser()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
if (parser) {
|
|
|
|
parser->UnblockParser();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to resume parsing asynchronously
|
|
|
|
*/
|
|
|
|
void ContinueParserAsync()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
if (parser) {
|
|
|
|
parser->ContinueInterruptedParsingAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-15 04:29:11 -07:00
|
|
|
/**
|
|
|
|
* Informs the creator parser that the evaluation of this script is starting
|
|
|
|
*/
|
|
|
|
void BeginEvaluating()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
if (parser) {
|
|
|
|
parser->BeginEvaluatingParserInsertedScript();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Informs the creator parser that the evaluation of this script is ending
|
|
|
|
*/
|
|
|
|
void EndEvaluating()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
if (parser) {
|
|
|
|
parser->EndEvaluatingParserInsertedScript();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a pointer to the creator parser if this has one or null if not
|
|
|
|
*/
|
|
|
|
already_AddRefed<nsIParser> GetCreatorParser()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
return parser.forget();
|
|
|
|
}
|
|
|
|
|
2011-11-15 23:50:18 -08:00
|
|
|
/**
|
|
|
|
* This method is called when the parser finishes creating the script
|
|
|
|
* element's children, if any are present.
|
|
|
|
*
|
|
|
|
* @return whether the parser will be blocked while this script is being
|
|
|
|
* loaded
|
|
|
|
*/
|
|
|
|
bool AttemptToExecute()
|
|
|
|
{
|
|
|
|
mDoneAddingChildren = true;
|
2011-11-15 23:50:18 -08:00
|
|
|
bool block = MaybeProcessScript();
|
2011-11-15 23:50:18 -08:00
|
|
|
if (!mAlreadyStarted) {
|
|
|
|
// Need to lose parser-insertedness here to allow another script to cause
|
|
|
|
// execution later.
|
|
|
|
LoseParserInsertedness();
|
|
|
|
}
|
2011-11-15 23:50:18 -08:00
|
|
|
return block;
|
2011-11-15 23:50:18 -08:00
|
|
|
}
|
|
|
|
|
2012-03-10 08:13:52 -08:00
|
|
|
/**
|
|
|
|
* Get the CORS mode of the script element
|
|
|
|
*/
|
|
|
|
virtual mozilla::CORSMode GetCORSMode() const
|
|
|
|
{
|
|
|
|
/* Default to no CORS */
|
|
|
|
return mozilla::CORS_NONE;
|
|
|
|
}
|
|
|
|
|
2012-09-18 20:24:27 -07:00
|
|
|
/**
|
|
|
|
* Fire an error event
|
|
|
|
*/
|
|
|
|
virtual nsresult FireErrorEvent() = 0;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
protected:
|
2011-11-15 23:50:18 -08:00
|
|
|
/**
|
|
|
|
* Processes the script if it's in the document-tree and links to or
|
|
|
|
* contains a script. Once it has been evaluated there is no way to make it
|
|
|
|
* reevaluate the script, you'll have to create a new element. This also means
|
|
|
|
* that when adding a src attribute to an element that already contains an
|
|
|
|
* inline script, the script referenced by the src attribute will not be
|
|
|
|
* loaded.
|
|
|
|
*
|
|
|
|
* In order to be able to use multiple childNodes, or to use the
|
|
|
|
* fallback mechanism of using both inline script and linked script you have
|
|
|
|
* to add all attributes and childNodes before adding the element to the
|
|
|
|
* document-tree.
|
2011-11-15 23:50:18 -08:00
|
|
|
*
|
|
|
|
* @return whether the parser will be blocked while this script is being
|
|
|
|
* loaded
|
2011-11-15 23:50:18 -08:00
|
|
|
*/
|
2011-11-15 23:50:18 -08:00
|
|
|
virtual bool MaybeProcessScript() = 0;
|
2011-11-15 23:50:18 -08:00
|
|
|
|
2009-11-17 00:52:30 -08:00
|
|
|
/**
|
|
|
|
* The start line number of the script.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mLineNumber;
|
2009-11-17 00:52:30 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The "already started" flag per HTML5.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mAlreadyStarted;
|
2009-11-17 00:52:30 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The script didn't have an end tag.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mMalformed;
|
2009-11-17 00:52:30 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* False if parser-inserted but the parser hasn't triggered running yet.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mDoneAddingChildren;
|
2009-11-17 00:52:30 -08:00
|
|
|
|
2010-10-27 05:04:09 -07:00
|
|
|
/**
|
|
|
|
* If true, the .async property returns true instead of reflecting the
|
|
|
|
* content attribute.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mForceAsync;
|
2010-10-27 05:04:09 -07:00
|
|
|
|
2009-11-17 00:52:30 -08:00
|
|
|
/**
|
|
|
|
* Whether src, defer and async are frozen.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mFrozen;
|
2009-11-17 00:52:30 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The effective deferredness.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mDefer;
|
2009-11-17 00:52:30 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The effective asyncness.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mAsync;
|
2009-11-17 00:52:30 -08:00
|
|
|
|
2010-10-27 00:03:10 -07:00
|
|
|
/**
|
|
|
|
* The effective externalness. A script can be external with mUri being null
|
|
|
|
* if the src attribute contained an invalid URL string.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mExternal;
|
2010-10-27 00:03:10 -07:00
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
/**
|
|
|
|
* Whether this element was parser-created.
|
|
|
|
*/
|
2010-10-25 05:17:38 -07:00
|
|
|
mozilla::dom::FromParser mParserCreated;
|
2010-09-01 04:41:12 -07:00
|
|
|
|
2009-11-17 00:52:30 -08:00
|
|
|
/**
|
|
|
|
* The effective src (or null if no src).
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIURI> mUri;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The creator parser of a non-defer, non-async parser-inserted script.
|
|
|
|
*/
|
|
|
|
nsWeakPtr mCreatorParser;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(nsIScriptElement, NS_ISCRIPTELEMENT_IID)
|
|
|
|
|
|
|
|
#endif // nsIScriptElement_h___
|