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/. */
|
2004-07-07 02:52:03 +04:00
|
|
|
|
|
|
|
#ifndef nsIScriptElement_h___
|
|
|
|
#define nsIScriptElement_h___
|
|
|
|
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2006-11-04 08:45:02 +03:00
|
|
|
#include "nsIScriptLoaderObserver.h"
|
2018-10-31 23:39:03 +03:00
|
|
|
#include "nsIWeakReferenceUtils.h"
|
2009-10-15 15:29:11 +04:00
|
|
|
#include "nsIParser.h"
|
2017-11-09 06:02:14 +03:00
|
|
|
#include "nsIContent.h"
|
2010-09-01 15:41:12 +04:00
|
|
|
#include "nsContentCreatorFunctions.h"
|
2012-03-10 20:13:52 +04:00
|
|
|
#include "mozilla/CORSMode.h"
|
2018-11-13 17:33:02 +03:00
|
|
|
#include "mozilla/net/ReferrerPolicy.h"
|
2004-07-07 02:52:03 +04:00
|
|
|
|
2018-01-05 01:32:15 +03:00
|
|
|
// Must be kept in sync with xpcom/rust/xpcom/src/interfaces/nonidl.rs
|
2006-02-01 01:19:46 +03:00
|
|
|
#define NS_ISCRIPTELEMENT_IID \
|
2018-11-30 13:46:48 +03:00
|
|
|
{ \
|
2014-05-21 21:08:12 +04:00
|
|
|
0xe60fca9b, 0x1b96, 0x4e4e, { \
|
|
|
|
0xa9, 0xb4, 0xdc, 0x98, 0x4f, 0x88, 0x3f, 0x9c \
|
|
|
|
} \
|
|
|
|
}
|
2004-07-07 02:52:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal interface implemented by script elements
|
|
|
|
*/
|
2017-05-08 09:25:03 +03:00
|
|
|
class nsIScriptElement : public nsIScriptLoaderObserver {
|
2004-07-07 02:52:03 +04:00
|
|
|
public:
|
2005-11-11 17:36:26 +03:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_ISCRIPTELEMENT_IID)
|
2004-07-07 02:52:03 +04:00
|
|
|
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit nsIScriptElement(mozilla::dom::FromParser aFromParser)
|
2014-10-23 15:06:00 +04:00
|
|
|
: mLineNumber(1),
|
2018-07-05 09:21:04 +03:00
|
|
|
mColumnNumber(1),
|
2011-10-17 18:59:28 +04:00
|
|
|
mAlreadyStarted(false),
|
|
|
|
mMalformed(false),
|
2010-11-01 13:50:01 +03:00
|
|
|
mDoneAddingChildren(aFromParser == mozilla::dom::NOT_FROM_PARSER ||
|
|
|
|
aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT),
|
2010-10-27 16:04:09 +04:00
|
|
|
mForceAsync(aFromParser == mozilla::dom::NOT_FROM_PARSER ||
|
|
|
|
aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT),
|
2011-10-17 18:59:28 +04:00
|
|
|
mFrozen(false),
|
2018-01-08 18:17:34 +03:00
|
|
|
mIsModule(false),
|
2011-10-17 18:59:28 +04:00
|
|
|
mDefer(false),
|
|
|
|
mAsync(false),
|
|
|
|
mExternal(false),
|
2010-10-27 11:18:24 +04: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 18:20:58 +04:00
|
|
|
mCreatorParser(nullptr) {}
|
2006-11-04 08:45:02 +03:00
|
|
|
|
2004-07-07 02:52:03 +04:00
|
|
|
/**
|
|
|
|
* Content type identifying the scripting language. Can be empty, in
|
|
|
|
* which case javascript will be assumed.
|
2016-07-13 19:10:13 +03:00
|
|
|
* Return false if type attribute is not found.
|
2004-07-07 02:52:03 +04:00
|
|
|
*/
|
2016-07-13 19:10:13 +03:00
|
|
|
virtual bool GetScriptType(nsAString& type) = 0;
|
|
|
|
|
2004-07-07 02:52:03 +04:00
|
|
|
/**
|
|
|
|
* Location of script source text. Can return null, in which case
|
|
|
|
* this is assumed to be an inline script element.
|
|
|
|
*/
|
2009-11-17 11:52:30 +03:00
|
|
|
nsIURI* GetScriptURI() {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mFrozen, "Not ready for this call yet!");
|
2009-11-17 11:52:30 +03:00
|
|
|
return mUri;
|
|
|
|
}
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2017-10-05 08:16:32 +03:00
|
|
|
nsIPrincipal* GetScriptURITriggeringPrincipal() {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mFrozen, "Not ready for this call yet!");
|
2017-10-05 08:16:32 +03:00
|
|
|
return mSrcTriggeringPrincipal;
|
|
|
|
}
|
|
|
|
|
2004-07-07 02:52:03 +04:00
|
|
|
/**
|
|
|
|
* Script source text for inline script elements.
|
|
|
|
*/
|
|
|
|
virtual void GetScriptText(nsAString& text) = 0;
|
|
|
|
|
|
|
|
virtual void GetScriptCharset(nsAString& charset) = 0;
|
2008-07-26 06:42:12 +04:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
2018-01-08 18:17:34 +03:00
|
|
|
* Freezes the return values of the following methods so that subsequent
|
|
|
|
* modifications to the attributes don't change execution behavior:
|
|
|
|
* - GetScriptIsModule()
|
|
|
|
* - GetScriptDeferred()
|
|
|
|
* - GetScriptAsync()
|
|
|
|
* - GetScriptURI()
|
|
|
|
* - GetScriptExternal()
|
2009-11-17 11:52:30 +03:00
|
|
|
*/
|
2019-01-02 16:05:23 +03:00
|
|
|
virtual void FreezeExecutionAttrs(mozilla::dom::Document*) = 0;
|
2018-01-08 18:17:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the script a module script. Currently only supported by HTML scripts.
|
|
|
|
*/
|
|
|
|
bool GetScriptIsModule() {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mFrozen, "Not ready for this call yet!");
|
2018-01-08 18:17:34 +03:00
|
|
|
return mIsModule;
|
|
|
|
}
|
2009-11-17 11:52:30 +03:00
|
|
|
|
2008-07-26 06:42:12 +04:00
|
|
|
/**
|
|
|
|
* Is the script deferred. Currently only supported by HTML scripts.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool GetScriptDeferred() {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mFrozen, "Not ready for this call yet!");
|
2009-11-17 11:52:30 +03:00
|
|
|
return mDefer;
|
|
|
|
}
|
2008-07-26 06:42:12 +04:00
|
|
|
|
2009-11-10 04:04:24 +03:00
|
|
|
/**
|
|
|
|
* Is the script async. Currently only supported by HTML scripts.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool GetScriptAsync() {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mFrozen, "Not ready for this call yet!");
|
2016-07-13 19:10:13 +03:00
|
|
|
return mAsync;
|
2009-11-17 11:52:30 +03:00
|
|
|
}
|
2009-11-10 04:04:24 +03:00
|
|
|
|
2010-10-27 11:03:10 +04:00
|
|
|
/**
|
|
|
|
* Is the script an external script?
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool GetScriptExternal() {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mFrozen, "Not ready for this call yet!");
|
2010-10-27 11:03:10 +04:00
|
|
|
return mExternal;
|
|
|
|
}
|
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
/**
|
2010-10-25 16:17:38 +04:00
|
|
|
* Returns how the element was created.
|
2010-09-01 15:41:12 +04:00
|
|
|
*/
|
2010-10-25 16:17:38 +04:00
|
|
|
mozilla::dom::FromParser GetParserCreated() { return mParserCreated; }
|
2010-09-01 15:41:12 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetScriptLineNumber(uint32_t aLineNumber) { mLineNumber = aLineNumber; }
|
2017-05-08 09:25:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t GetScriptLineNumber() { return mLineNumber; }
|
2006-11-04 08:45:02 +03:00
|
|
|
|
2018-07-05 09:21:04 +03:00
|
|
|
void SetScriptColumnNumber(uint32_t aColumnNumber) {
|
|
|
|
mColumnNumber = aColumnNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GetScriptColumnNumber() { return mColumnNumber; }
|
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
void SetIsMalformed() { mMalformed = true; }
|
2017-05-08 09:25:03 +03:00
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
bool IsMalformed() { return mMalformed; }
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
void PreventExecution() { mAlreadyStarted = true; }
|
2010-09-01 15:41:12 +04:00
|
|
|
|
|
|
|
void LoseParserInsertedness() {
|
2012-07-30 18:20:58 +04:00
|
|
|
mUri = nullptr;
|
|
|
|
mCreatorParser = nullptr;
|
2010-10-25 16:17:38 +04:00
|
|
|
mParserCreated = mozilla::dom::NOT_FROM_PARSER;
|
2017-11-09 06:02:14 +03:00
|
|
|
mForceAsync = !GetAsyncState();
|
2018-01-12 13:36:23 +03:00
|
|
|
|
|
|
|
// Reset state set by FreezeExecutionAttrs().
|
|
|
|
mFrozen = false;
|
|
|
|
mIsModule = false;
|
|
|
|
mExternal = false;
|
|
|
|
mAsync = false;
|
|
|
|
mDefer = false;
|
2006-11-04 08:45:02 +03:00
|
|
|
}
|
|
|
|
|
2009-10-15 15:29:11 +04:00
|
|
|
void SetCreatorParser(nsIParser* aParser) {
|
2013-07-10 13:56:21 +04:00
|
|
|
mCreatorParser = do_GetWeakReference(aParser);
|
2009-10-15 15:29:11 +04:00
|
|
|
}
|
|
|
|
|
2012-01-20 15:16:27 +04: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 15:29:11 +04:00
|
|
|
/**
|
|
|
|
* Informs the creator parser that the evaluation of this script is starting
|
|
|
|
*/
|
|
|
|
void BeginEvaluating() {
|
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
if (parser) {
|
2016-09-03 00:55:38 +03:00
|
|
|
parser->PushDefinedInsertionPoint();
|
2009-10-15 15:29:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Informs the creator parser that the evaluation of this script is ending
|
|
|
|
*/
|
|
|
|
void EndEvaluating() {
|
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
if (parser) {
|
2016-09-03 00:55:38 +03:00
|
|
|
parser->PopDefinedInsertionPoint();
|
2009-10-15 15:29:11 +04:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2009-10-15 15:29:11 +04:00
|
|
|
/**
|
|
|
|
* 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 11:50:18 +04: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 11:50:18 +04:00
|
|
|
bool block = MaybeProcessScript();
|
2011-11-16 11:50:18 +04:00
|
|
|
if (!mAlreadyStarted) {
|
|
|
|
// Need to lose parser-insertedness here to allow another script to cause
|
|
|
|
// execution later.
|
|
|
|
LoseParserInsertedness();
|
|
|
|
}
|
2011-11-16 11:50:18 +04:00
|
|
|
return block;
|
2011-11-16 11:50:18 +04:00
|
|
|
}
|
|
|
|
|
2012-03-10 20:13:52 +04:00
|
|
|
/**
|
|
|
|
* Get the CORS mode of the script element
|
|
|
|
*/
|
|
|
|
virtual mozilla::CORSMode GetCORSMode() const {
|
|
|
|
/* Default to no CORS */
|
|
|
|
return mozilla::CORS_NONE;
|
|
|
|
}
|
|
|
|
|
2018-11-13 17:33:02 +03:00
|
|
|
/**
|
|
|
|
* Get referrer policy of the script element
|
|
|
|
*/
|
|
|
|
virtual mozilla::net::ReferrerPolicy GetReferrerPolicy() {
|
|
|
|
return mozilla::net::RP_Unset;
|
|
|
|
}
|
|
|
|
|
2012-09-19 07:24:27 +04:00
|
|
|
/**
|
|
|
|
* Fire an error event
|
|
|
|
*/
|
|
|
|
virtual nsresult FireErrorEvent() = 0;
|
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
protected:
|
2011-11-16 11:50:18 +04: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 11:50:18 +04:00
|
|
|
*
|
|
|
|
* @return whether the parser will be blocked while this script is being
|
|
|
|
* loaded
|
2011-11-16 11:50:18 +04:00
|
|
|
*/
|
2011-11-16 11:50:18 +04:00
|
|
|
virtual bool MaybeProcessScript() = 0;
|
2011-11-16 11:50:18 +04:00
|
|
|
|
2017-11-09 06:02:14 +03:00
|
|
|
/**
|
|
|
|
* Since we've removed the XPCOM interface to HTML elements, we need a way to
|
|
|
|
* retreive async state from script elements without bringing the type in.
|
|
|
|
*/
|
|
|
|
virtual bool GetAsyncState() = 0;
|
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The start line number of the script.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mLineNumber;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2018-07-05 09:21:04 +03:00
|
|
|
/**
|
|
|
|
* The start column number of the script.
|
|
|
|
*/
|
|
|
|
uint32_t mColumnNumber;
|
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The "already started" flag per HTML5.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mAlreadyStarted;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The script didn't have an end tag.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mMalformed;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* False if parser-inserted but the parser hasn't triggered running yet.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mDoneAddingChildren;
|
2009-11-17 11:52:30 +03:00
|
|
|
|
2010-10-27 16:04:09 +04:00
|
|
|
/**
|
|
|
|
* If true, the .async property returns true instead of reflecting the
|
|
|
|
* content attribute.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mForceAsync;
|
2010-10-27 16:04:09 +04:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* Whether src, defer and async are frozen.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mFrozen;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2018-01-08 18:17:34 +03:00
|
|
|
/**
|
|
|
|
* The effective moduleness.
|
|
|
|
*/
|
|
|
|
bool mIsModule;
|
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The effective deferredness.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mDefer;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The effective asyncness.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mAsync;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2010-10-27 11:03:10 +04: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 10:19:26 +04:00
|
|
|
bool mExternal;
|
2010-10-27 11:03:10 +04:00
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
/**
|
|
|
|
* Whether this element was parser-created.
|
|
|
|
*/
|
2010-10-25 16:17:38 +04:00
|
|
|
mozilla::dom::FromParser mParserCreated;
|
2010-09-01 15:41:12 +04:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The effective src (or null if no src).
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIURI> mUri;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2017-10-05 08:16:32 +03:00
|
|
|
/**
|
|
|
|
* The triggering principal for the src URL.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIPrincipal> mSrcTriggeringPrincipal;
|
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The creator parser of a non-defer, non-async parser-inserted script.
|
|
|
|
*/
|
|
|
|
nsWeakPtr mCreatorParser;
|
2004-07-07 02:52:03 +04:00
|
|
|
};
|
|
|
|
|
2005-11-11 17:36:26 +03:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(nsIScriptElement, NS_ISCRIPTELEMENT_IID)
|
|
|
|
|
2004-07-07 02:52:03 +04:00
|
|
|
#endif // nsIScriptElement_h___
|