2015-05-03 19:32:37 +00: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 11:12:37 +00: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/. */
|
2004-07-06 22:52:03 +00:00
|
|
|
|
|
|
|
#ifndef nsIScriptElement_h___
|
|
|
|
#define nsIScriptElement_h___
|
|
|
|
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2006-11-04 05:45:02 +00:00
|
|
|
#include "nsIScriptLoaderObserver.h"
|
2009-10-15 11:29:11 +00:00
|
|
|
#include "nsWeakPtr.h"
|
|
|
|
#include "nsIParser.h"
|
2010-09-01 11:41:12 +00:00
|
|
|
#include "nsContentCreatorFunctions.h"
|
2010-10-27 12:04:09 +00:00
|
|
|
#include "nsIDOMHTMLScriptElement.h"
|
2012-03-10 16:13:52 +00:00
|
|
|
#include "mozilla/CORSMode.h"
|
2004-07-06 22:52:03 +00:00
|
|
|
|
2006-01-31 22:19:46 +00:00
|
|
|
#define NS_ISCRIPTELEMENT_IID \
|
2014-05-21 17:08:12 +00:00
|
|
|
{ 0xe60fca9b, 0x1b96, 0x4e4e, \
|
|
|
|
{ 0xa9, 0xb4, 0xdc, 0x98, 0x4f, 0x88, 0x3f, 0x9c } }
|
2004-07-06 22:52:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal interface implemented by script elements
|
|
|
|
*/
|
2006-11-04 05:45:02 +00:00
|
|
|
class nsIScriptElement : public nsIScriptLoaderObserver {
|
2004-07-06 22:52:03 +00:00
|
|
|
public:
|
2005-11-11 14:36:26 +00:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_ISCRIPTELEMENT_IID)
|
2004-07-06 22:52:03 +00:00
|
|
|
|
2014-09-02 00:49:25 +00:00
|
|
|
explicit nsIScriptElement(mozilla::dom::FromParser aFromParser)
|
2014-10-23 11:06:00 +00:00
|
|
|
: mLineNumber(1),
|
2011-10-17 14:59:28 +00:00
|
|
|
mAlreadyStarted(false),
|
|
|
|
mMalformed(false),
|
2010-11-01 10:50:01 +00:00
|
|
|
mDoneAddingChildren(aFromParser == mozilla::dom::NOT_FROM_PARSER ||
|
|
|
|
aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT),
|
2010-10-27 12:04:09 +00:00
|
|
|
mForceAsync(aFromParser == mozilla::dom::NOT_FROM_PARSER ||
|
|
|
|
aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT),
|
2011-10-17 14:59:28 +00:00
|
|
|
mFrozen(false),
|
|
|
|
mDefer(false),
|
|
|
|
mAsync(false),
|
|
|
|
mExternal(false),
|
2010-10-27 07:18:24 +00: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 14:20:58 +00:00
|
|
|
mCreatorParser(nullptr)
|
2006-11-04 05:45:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-07-06 22:52:03 +00: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 08:52:30 +00:00
|
|
|
nsIURI* GetScriptURI()
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(mFrozen, "Not ready for this call yet!");
|
|
|
|
return mUri;
|
|
|
|
}
|
2004-07-06 22:52:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Script source text for inline script elements.
|
|
|
|
*/
|
|
|
|
virtual void GetScriptText(nsAString& text) = 0;
|
|
|
|
|
|
|
|
virtual void GetScriptCharset(nsAString& charset) = 0;
|
2008-07-26 02:42:12 +00:00
|
|
|
|
2009-11-17 08:52:30 +00: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-26 02:42:12 +00:00
|
|
|
/**
|
|
|
|
* Is the script deferred. Currently only supported by HTML scripts.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool GetScriptDeferred()
|
2009-11-17 08:52:30 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mFrozen, "Not ready for this call yet!");
|
|
|
|
return mDefer;
|
|
|
|
}
|
2008-07-26 02:42:12 +00:00
|
|
|
|
2009-11-10 01:04:24 +00:00
|
|
|
/**
|
|
|
|
* Is the script async. Currently only supported by HTML scripts.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool GetScriptAsync()
|
2009-11-17 08:52:30 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mFrozen, "Not ready for this call yet!");
|
|
|
|
return mAsync;
|
|
|
|
}
|
2009-11-10 01:04:24 +00:00
|
|
|
|
2010-10-27 07:03:10 +00:00
|
|
|
/**
|
|
|
|
* Is the script an external script?
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool GetScriptExternal()
|
2010-10-27 07:03:10 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mFrozen, "Not ready for this call yet!");
|
|
|
|
return mExternal;
|
|
|
|
}
|
|
|
|
|
2010-09-01 11:41:12 +00:00
|
|
|
/**
|
2010-10-25 12:17:38 +00:00
|
|
|
* Returns how the element was created.
|
2010-09-01 11:41:12 +00:00
|
|
|
*/
|
2010-10-25 12:17:38 +00:00
|
|
|
mozilla::dom::FromParser GetParserCreated()
|
2010-09-01 11:41:12 +00:00
|
|
|
{
|
|
|
|
return mParserCreated;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
void SetScriptLineNumber(uint32_t aLineNumber)
|
2006-11-04 05:45:02 +00:00
|
|
|
{
|
|
|
|
mLineNumber = aLineNumber;
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t GetScriptLineNumber()
|
2006-11-04 05:45:02 +00:00
|
|
|
{
|
|
|
|
return mLineNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetIsMalformed()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mMalformed = true;
|
2006-11-04 05:45:02 +00:00
|
|
|
}
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsMalformed()
|
2006-11-04 05:45:02 +00:00
|
|
|
{
|
|
|
|
return mMalformed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreventExecution()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mAlreadyStarted = true;
|
2010-09-01 11:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoseParserInsertedness()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mFrozen = false;
|
2012-07-30 14:20:58 +00:00
|
|
|
mUri = nullptr;
|
|
|
|
mCreatorParser = nullptr;
|
2010-10-25 12:17:38 +00:00
|
|
|
mParserCreated = mozilla::dom::NOT_FROM_PARSER;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool async = false;
|
2010-10-27 12:04:09 +00:00
|
|
|
nsCOMPtr<nsIDOMHTMLScriptElement> htmlScript = do_QueryInterface(this);
|
|
|
|
if (htmlScript) {
|
|
|
|
htmlScript->GetAsync(&async);
|
|
|
|
}
|
|
|
|
mForceAsync = !async;
|
2006-11-04 05:45:02 +00:00
|
|
|
}
|
|
|
|
|
2009-10-15 11:29:11 +00:00
|
|
|
void SetCreatorParser(nsIParser* aParser)
|
|
|
|
{
|
2013-07-10 09:56:21 +00:00
|
|
|
mCreatorParser = do_GetWeakReference(aParser);
|
2009-10-15 11:29:11 +00:00
|
|
|
}
|
|
|
|
|
2012-01-20 11:16:27 +00: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 11:29:11 +00: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-16 07:50:18 +00: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-16 07:50:18 +00:00
|
|
|
bool block = MaybeProcessScript();
|
2011-11-16 07:50:18 +00:00
|
|
|
if (!mAlreadyStarted) {
|
|
|
|
// Need to lose parser-insertedness here to allow another script to cause
|
|
|
|
// execution later.
|
|
|
|
LoseParserInsertedness();
|
|
|
|
}
|
2011-11-16 07:50:18 +00:00
|
|
|
return block;
|
2011-11-16 07:50:18 +00:00
|
|
|
}
|
|
|
|
|
2012-03-10 16:13:52 +00:00
|
|
|
/**
|
|
|
|
* Get the CORS mode of the script element
|
|
|
|
*/
|
|
|
|
virtual mozilla::CORSMode GetCORSMode() const
|
|
|
|
{
|
|
|
|
/* Default to no CORS */
|
|
|
|
return mozilla::CORS_NONE;
|
|
|
|
}
|
|
|
|
|
2012-09-19 03:24:27 +00:00
|
|
|
/**
|
|
|
|
* Fire an error event
|
|
|
|
*/
|
|
|
|
virtual nsresult FireErrorEvent() = 0;
|
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
protected:
|
2011-11-16 07:50:18 +00: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-16 07:50:18 +00:00
|
|
|
*
|
|
|
|
* @return whether the parser will be blocked while this script is being
|
|
|
|
* loaded
|
2011-11-16 07:50:18 +00:00
|
|
|
*/
|
2011-11-16 07:50:18 +00:00
|
|
|
virtual bool MaybeProcessScript() = 0;
|
2011-11-16 07:50:18 +00:00
|
|
|
|
2009-11-17 08:52:30 +00:00
|
|
|
/**
|
|
|
|
* The start line number of the script.
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mLineNumber;
|
2009-11-17 08:52:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The "already started" flag per HTML5.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mAlreadyStarted;
|
2009-11-17 08:52:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The script didn't have an end tag.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mMalformed;
|
2009-11-17 08:52:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* False if parser-inserted but the parser hasn't triggered running yet.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mDoneAddingChildren;
|
2009-11-17 08:52:30 +00:00
|
|
|
|
2010-10-27 12:04:09 +00:00
|
|
|
/**
|
|
|
|
* If true, the .async property returns true instead of reflecting the
|
|
|
|
* content attribute.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mForceAsync;
|
2010-10-27 12:04:09 +00:00
|
|
|
|
2009-11-17 08:52:30 +00:00
|
|
|
/**
|
|
|
|
* Whether src, defer and async are frozen.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mFrozen;
|
2009-11-17 08:52:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The effective deferredness.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mDefer;
|
2009-11-17 08:52:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The effective asyncness.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mAsync;
|
2009-11-17 08:52:30 +00:00
|
|
|
|
2010-10-27 07:03:10 +00:00
|
|
|
/**
|
|
|
|
* The effective externalness. A script can be external with mUri being null
|
|
|
|
* if the src attribute contained an invalid URL string.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mExternal;
|
2010-10-27 07:03:10 +00:00
|
|
|
|
2010-09-01 11:41:12 +00:00
|
|
|
/**
|
|
|
|
* Whether this element was parser-created.
|
|
|
|
*/
|
2010-10-25 12:17:38 +00:00
|
|
|
mozilla::dom::FromParser mParserCreated;
|
2010-09-01 11:41:12 +00:00
|
|
|
|
2009-11-17 08:52:30 +00: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;
|
2004-07-06 22:52:03 +00:00
|
|
|
};
|
|
|
|
|
2005-11-11 14:36:26 +00:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(nsIScriptElement, NS_ISCRIPTELEMENT_IID)
|
|
|
|
|
2004-07-06 22:52:03 +00:00
|
|
|
#endif // nsIScriptElement_h___
|