1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/public/nsIScriptElement.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,327 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsIScriptElement_h___ 1.10 +#define nsIScriptElement_h___ 1.11 + 1.12 +#include "nsISupports.h" 1.13 +#include "nsIURI.h" 1.14 +#include "nsCOMPtr.h" 1.15 +#include "nsIScriptLoaderObserver.h" 1.16 +#include "nsWeakPtr.h" 1.17 +#include "nsIParser.h" 1.18 +#include "nsContentCreatorFunctions.h" 1.19 +#include "nsIDOMHTMLScriptElement.h" 1.20 +#include "mozilla/CORSMode.h" 1.21 + 1.22 +#define NS_ISCRIPTELEMENT_IID \ 1.23 +{ 0x491628bc, 0xce7c, 0x4db4, \ 1.24 + { 0x93, 0x3f, 0xce, 0x1b, 0x75, 0xee, 0x75, 0xce } } 1.25 + 1.26 +/** 1.27 + * Internal interface implemented by script elements 1.28 + */ 1.29 +class nsIScriptElement : public nsIScriptLoaderObserver { 1.30 +public: 1.31 + NS_DECLARE_STATIC_IID_ACCESSOR(NS_ISCRIPTELEMENT_IID) 1.32 + 1.33 + nsIScriptElement(mozilla::dom::FromParser aFromParser) 1.34 + : mLineNumber(0), 1.35 + mAlreadyStarted(false), 1.36 + mMalformed(false), 1.37 + mDoneAddingChildren(aFromParser == mozilla::dom::NOT_FROM_PARSER || 1.38 + aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT), 1.39 + mForceAsync(aFromParser == mozilla::dom::NOT_FROM_PARSER || 1.40 + aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT), 1.41 + mFrozen(false), 1.42 + mDefer(false), 1.43 + mAsync(false), 1.44 + mExternal(false), 1.45 + mParserCreated(aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT ? 1.46 + mozilla::dom::NOT_FROM_PARSER : aFromParser), 1.47 + // Fragment parser-created scripts (if executable) 1.48 + // behave like script-created scripts. 1.49 + mCreatorParser(nullptr) 1.50 + { 1.51 + } 1.52 + 1.53 + /** 1.54 + * Content type identifying the scripting language. Can be empty, in 1.55 + * which case javascript will be assumed. 1.56 + */ 1.57 + virtual void GetScriptType(nsAString& type) = 0; 1.58 + 1.59 + /** 1.60 + * Location of script source text. Can return null, in which case 1.61 + * this is assumed to be an inline script element. 1.62 + */ 1.63 + nsIURI* GetScriptURI() 1.64 + { 1.65 + NS_PRECONDITION(mFrozen, "Not ready for this call yet!"); 1.66 + return mUri; 1.67 + } 1.68 + 1.69 + /** 1.70 + * Script source text for inline script elements. 1.71 + */ 1.72 + virtual void GetScriptText(nsAString& text) = 0; 1.73 + 1.74 + virtual void GetScriptCharset(nsAString& charset) = 0; 1.75 + 1.76 + /** 1.77 + * Freezes the return values of GetScriptDeferred(), GetScriptAsync() and 1.78 + * GetScriptURI() so that subsequent modifications to the attributes don't 1.79 + * change execution behavior. 1.80 + */ 1.81 + virtual void FreezeUriAsyncDefer() = 0; 1.82 + 1.83 + /** 1.84 + * Is the script deferred. Currently only supported by HTML scripts. 1.85 + */ 1.86 + bool GetScriptDeferred() 1.87 + { 1.88 + NS_PRECONDITION(mFrozen, "Not ready for this call yet!"); 1.89 + return mDefer; 1.90 + } 1.91 + 1.92 + /** 1.93 + * Is the script async. Currently only supported by HTML scripts. 1.94 + */ 1.95 + bool GetScriptAsync() 1.96 + { 1.97 + NS_PRECONDITION(mFrozen, "Not ready for this call yet!"); 1.98 + return mAsync; 1.99 + } 1.100 + 1.101 + /** 1.102 + * Is the script an external script? 1.103 + */ 1.104 + bool GetScriptExternal() 1.105 + { 1.106 + NS_PRECONDITION(mFrozen, "Not ready for this call yet!"); 1.107 + return mExternal; 1.108 + } 1.109 + 1.110 + /** 1.111 + * Returns how the element was created. 1.112 + */ 1.113 + mozilla::dom::FromParser GetParserCreated() 1.114 + { 1.115 + return mParserCreated; 1.116 + } 1.117 + 1.118 + void SetScriptLineNumber(uint32_t aLineNumber) 1.119 + { 1.120 + mLineNumber = aLineNumber; 1.121 + } 1.122 + uint32_t GetScriptLineNumber() 1.123 + { 1.124 + return mLineNumber; 1.125 + } 1.126 + 1.127 + void SetIsMalformed() 1.128 + { 1.129 + mMalformed = true; 1.130 + } 1.131 + bool IsMalformed() 1.132 + { 1.133 + return mMalformed; 1.134 + } 1.135 + 1.136 + void PreventExecution() 1.137 + { 1.138 + mAlreadyStarted = true; 1.139 + } 1.140 + 1.141 + void LoseParserInsertedness() 1.142 + { 1.143 + mFrozen = false; 1.144 + mUri = nullptr; 1.145 + mCreatorParser = nullptr; 1.146 + mParserCreated = mozilla::dom::NOT_FROM_PARSER; 1.147 + bool async = false; 1.148 + nsCOMPtr<nsIDOMHTMLScriptElement> htmlScript = do_QueryInterface(this); 1.149 + if (htmlScript) { 1.150 + htmlScript->GetAsync(&async); 1.151 + } 1.152 + mForceAsync = !async; 1.153 + } 1.154 + 1.155 + void SetCreatorParser(nsIParser* aParser) 1.156 + { 1.157 + mCreatorParser = do_GetWeakReference(aParser); 1.158 + } 1.159 + 1.160 + /** 1.161 + * Unblocks the creator parser 1.162 + */ 1.163 + void UnblockParser() 1.164 + { 1.165 + nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser); 1.166 + if (parser) { 1.167 + parser->UnblockParser(); 1.168 + } 1.169 + } 1.170 + 1.171 + /** 1.172 + * Attempts to resume parsing asynchronously 1.173 + */ 1.174 + void ContinueParserAsync() 1.175 + { 1.176 + nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser); 1.177 + if (parser) { 1.178 + parser->ContinueInterruptedParsingAsync(); 1.179 + } 1.180 + } 1.181 + 1.182 + /** 1.183 + * Informs the creator parser that the evaluation of this script is starting 1.184 + */ 1.185 + void BeginEvaluating() 1.186 + { 1.187 + nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser); 1.188 + if (parser) { 1.189 + parser->BeginEvaluatingParserInsertedScript(); 1.190 + } 1.191 + } 1.192 + 1.193 + /** 1.194 + * Informs the creator parser that the evaluation of this script is ending 1.195 + */ 1.196 + void EndEvaluating() 1.197 + { 1.198 + nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser); 1.199 + if (parser) { 1.200 + parser->EndEvaluatingParserInsertedScript(); 1.201 + } 1.202 + } 1.203 + 1.204 + /** 1.205 + * Retrieves a pointer to the creator parser if this has one or null if not 1.206 + */ 1.207 + already_AddRefed<nsIParser> GetCreatorParser() 1.208 + { 1.209 + nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser); 1.210 + return parser.forget(); 1.211 + } 1.212 + 1.213 + /** 1.214 + * This method is called when the parser finishes creating the script 1.215 + * element's children, if any are present. 1.216 + * 1.217 + * @return whether the parser will be blocked while this script is being 1.218 + * loaded 1.219 + */ 1.220 + bool AttemptToExecute() 1.221 + { 1.222 + mDoneAddingChildren = true; 1.223 + bool block = MaybeProcessScript(); 1.224 + if (!mAlreadyStarted) { 1.225 + // Need to lose parser-insertedness here to allow another script to cause 1.226 + // execution later. 1.227 + LoseParserInsertedness(); 1.228 + } 1.229 + return block; 1.230 + } 1.231 + 1.232 + /** 1.233 + * Get the CORS mode of the script element 1.234 + */ 1.235 + virtual mozilla::CORSMode GetCORSMode() const 1.236 + { 1.237 + /* Default to no CORS */ 1.238 + return mozilla::CORS_NONE; 1.239 + } 1.240 + 1.241 + /** 1.242 + * Fire an error event 1.243 + */ 1.244 + virtual nsresult FireErrorEvent() = 0; 1.245 + 1.246 +protected: 1.247 + /** 1.248 + * Processes the script if it's in the document-tree and links to or 1.249 + * contains a script. Once it has been evaluated there is no way to make it 1.250 + * reevaluate the script, you'll have to create a new element. This also means 1.251 + * that when adding a src attribute to an element that already contains an 1.252 + * inline script, the script referenced by the src attribute will not be 1.253 + * loaded. 1.254 + * 1.255 + * In order to be able to use multiple childNodes, or to use the 1.256 + * fallback mechanism of using both inline script and linked script you have 1.257 + * to add all attributes and childNodes before adding the element to the 1.258 + * document-tree. 1.259 + * 1.260 + * @return whether the parser will be blocked while this script is being 1.261 + * loaded 1.262 + */ 1.263 + virtual bool MaybeProcessScript() = 0; 1.264 + 1.265 + /** 1.266 + * The start line number of the script. 1.267 + */ 1.268 + uint32_t mLineNumber; 1.269 + 1.270 + /** 1.271 + * The "already started" flag per HTML5. 1.272 + */ 1.273 + bool mAlreadyStarted; 1.274 + 1.275 + /** 1.276 + * The script didn't have an end tag. 1.277 + */ 1.278 + bool mMalformed; 1.279 + 1.280 + /** 1.281 + * False if parser-inserted but the parser hasn't triggered running yet. 1.282 + */ 1.283 + bool mDoneAddingChildren; 1.284 + 1.285 + /** 1.286 + * If true, the .async property returns true instead of reflecting the 1.287 + * content attribute. 1.288 + */ 1.289 + bool mForceAsync; 1.290 + 1.291 + /** 1.292 + * Whether src, defer and async are frozen. 1.293 + */ 1.294 + bool mFrozen; 1.295 + 1.296 + /** 1.297 + * The effective deferredness. 1.298 + */ 1.299 + bool mDefer; 1.300 + 1.301 + /** 1.302 + * The effective asyncness. 1.303 + */ 1.304 + bool mAsync; 1.305 + 1.306 + /** 1.307 + * The effective externalness. A script can be external with mUri being null 1.308 + * if the src attribute contained an invalid URL string. 1.309 + */ 1.310 + bool mExternal; 1.311 + 1.312 + /** 1.313 + * Whether this element was parser-created. 1.314 + */ 1.315 + mozilla::dom::FromParser mParserCreated; 1.316 + 1.317 + /** 1.318 + * The effective src (or null if no src). 1.319 + */ 1.320 + nsCOMPtr<nsIURI> mUri; 1.321 + 1.322 + /** 1.323 + * The creator parser of a non-defer, non-async parser-inserted script. 1.324 + */ 1.325 + nsWeakPtr mCreatorParser; 1.326 +}; 1.327 + 1.328 +NS_DEFINE_STATIC_IID_ACCESSOR(nsIScriptElement, NS_ISCRIPTELEMENT_IID) 1.329 + 1.330 +#endif // nsIScriptElement_h___