content/base/public/nsContentUtils.h

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /* A namespace class for static content utilities. */
michael@0 8
michael@0 9 #ifndef nsContentUtils_h___
michael@0 10 #define nsContentUtils_h___
michael@0 11
michael@0 12 #if defined(XP_WIN)
michael@0 13 #include <float.h>
michael@0 14 #endif
michael@0 15
michael@0 16 #if defined(SOLARIS)
michael@0 17 #include <ieeefp.h>
michael@0 18 #endif
michael@0 19
michael@0 20 #include "js/TypeDecls.h"
michael@0 21 #include "js/Value.h"
michael@0 22 #include "js/RootingAPI.h"
michael@0 23 #include "mozilla/EventForwards.h"
michael@0 24 #include "mozilla/GuardObjects.h"
michael@0 25 #include "mozilla/TimeStamp.h"
michael@0 26 #include "nsContentListDeclarations.h"
michael@0 27 #include "nsMathUtils.h"
michael@0 28 #include "nsTArrayForwardDeclare.h"
michael@0 29 #include "Units.h"
michael@0 30
michael@0 31 #if defined(XP_WIN)
michael@0 32 // Undefine LoadImage to prevent naming conflict with Windows.
michael@0 33 #undef LoadImage
michael@0 34 #endif
michael@0 35
michael@0 36 class imgICache;
michael@0 37 class imgIContainer;
michael@0 38 class imgINotificationObserver;
michael@0 39 class imgIRequest;
michael@0 40 class imgLoader;
michael@0 41 class imgRequestProxy;
michael@0 42 class nsAutoScriptBlockerSuppressNodeRemoved;
michael@0 43 class nsHtml5StringParser;
michael@0 44 class nsIChannel;
michael@0 45 class nsIConsoleService;
michael@0 46 class nsIContent;
michael@0 47 class nsIContentPolicy;
michael@0 48 class nsIContentSecurityPolicy;
michael@0 49 class nsIDocShell;
michael@0 50 class nsIDocument;
michael@0 51 class nsIDocumentLoaderFactory;
michael@0 52 class nsIDocumentObserver;
michael@0 53 class nsIDOMDocument;
michael@0 54 class nsIDOMDocumentFragment;
michael@0 55 class nsIDOMEvent;
michael@0 56 class nsIDOMHTMLFormElement;
michael@0 57 class nsIDOMHTMLInputElement;
michael@0 58 class nsIDOMKeyEvent;
michael@0 59 class nsIDOMNode;
michael@0 60 class nsIDOMScriptObjectFactory;
michael@0 61 class nsIDOMWindow;
michael@0 62 class nsIDragSession;
michael@0 63 class nsIEditor;
michael@0 64 class nsIFragmentContentSink;
michael@0 65 class nsIFrame;
michael@0 66 class nsIImageLoadingContent;
michael@0 67 class nsIInterfaceRequestor;
michael@0 68 class nsIIOService;
michael@0 69 class nsIJSRuntimeService;
michael@0 70 class nsILineBreaker;
michael@0 71 class nsNameSpaceManager;
michael@0 72 class nsINodeInfo;
michael@0 73 class nsIObserver;
michael@0 74 class nsIParser;
michael@0 75 class nsIParserService;
michael@0 76 class nsIPresShell;
michael@0 77 class nsIPrincipal;
michael@0 78 class nsIRunnable;
michael@0 79 class nsIScriptContext;
michael@0 80 class nsIScriptGlobalObject;
michael@0 81 class nsIScriptSecurityManager;
michael@0 82 class nsIStringBundle;
michael@0 83 class nsIStringBundleService;
michael@0 84 class nsISupportsHashKey;
michael@0 85 class nsIURI;
michael@0 86 class nsIWidget;
michael@0 87 class nsIWordBreaker;
michael@0 88 class nsIXPConnect;
michael@0 89 class nsNodeInfoManager;
michael@0 90 class nsPIDOMWindow;
michael@0 91 class nsPresContext;
michael@0 92 class nsScriptObjectTracer;
michael@0 93 class nsStringBuffer;
michael@0 94 class nsStringHashKey;
michael@0 95 class nsTextFragment;
michael@0 96 class nsViewportInfo;
michael@0 97 class nsWrapperCache;
michael@0 98 class nsAttrValue;
michael@0 99
michael@0 100 struct JSPropertyDescriptor;
michael@0 101 struct JSRuntime;
michael@0 102 struct nsIntMargin;
michael@0 103
michael@0 104 template<class E> class nsCOMArray;
michael@0 105 template<class K, class V> class nsDataHashtable;
michael@0 106 template<class K, class V> class nsRefPtrHashtable;
michael@0 107 template<class T> class nsReadingIterator;
michael@0 108
michael@0 109 namespace mozilla {
michael@0 110 class ErrorResult;
michael@0 111 class EventListenerManager;
michael@0 112
michael@0 113 namespace dom {
michael@0 114 class DocumentFragment;
michael@0 115 class Element;
michael@0 116 class EventTarget;
michael@0 117 class Selection;
michael@0 118 } // namespace dom
michael@0 119
michael@0 120 namespace layers {
michael@0 121 class LayerManager;
michael@0 122 } // namespace layers
michael@0 123
michael@0 124 // Called back from DeferredFinalize. Should add 'thing' to the array of smart
michael@0 125 // pointers in 'pointers', creating the array if 'pointers' is null, and return
michael@0 126 // the array.
michael@0 127 typedef void* (*DeferredFinalizeAppendFunction)(void* pointers, void* thing);
michael@0 128
michael@0 129 // Called to finalize a number of objects. Slice is the number of objects
michael@0 130 // to finalize, or if it's UINT32_MAX, all objects should be finalized.
michael@0 131 // Return value indicates whether it finalized all objects in the buffer.
michael@0 132 typedef bool (*DeferredFinalizeFunction)(uint32_t slice, void* data);
michael@0 133
michael@0 134 } // namespace mozilla
michael@0 135
michael@0 136 class nsIBidiKeyboard;
michael@0 137
michael@0 138 extern const char kLoadAsData[];
michael@0 139
michael@0 140 // Stolen from nsReadableUtils, but that's OK, since we can declare the same
michael@0 141 // name multiple times.
michael@0 142 const nsAFlatString& EmptyString();
michael@0 143 const nsAFlatCString& EmptyCString();
michael@0 144
michael@0 145 enum EventNameType {
michael@0 146 EventNameType_None = 0x0000,
michael@0 147 EventNameType_HTML = 0x0001,
michael@0 148 EventNameType_XUL = 0x0002,
michael@0 149 EventNameType_SVGGraphic = 0x0004, // svg graphic elements
michael@0 150 EventNameType_SVGSVG = 0x0008, // the svg element
michael@0 151 EventNameType_SMIL = 0x0010, // smil elements
michael@0 152 EventNameType_HTMLBodyOrFramesetOnly = 0x0020,
michael@0 153
michael@0 154 EventNameType_HTMLXUL = 0x0003,
michael@0 155 EventNameType_All = 0xFFFF
michael@0 156 };
michael@0 157
michael@0 158 struct EventNameMapping
michael@0 159 {
michael@0 160 nsIAtom* mAtom;
michael@0 161 uint32_t mId;
michael@0 162 int32_t mType;
michael@0 163 uint32_t mStructType;
michael@0 164 };
michael@0 165
michael@0 166 struct nsShortcutCandidate {
michael@0 167 nsShortcutCandidate(uint32_t aCharCode, bool aIgnoreShift) :
michael@0 168 mCharCode(aCharCode), mIgnoreShift(aIgnoreShift)
michael@0 169 {
michael@0 170 }
michael@0 171 uint32_t mCharCode;
michael@0 172 bool mIgnoreShift;
michael@0 173 };
michael@0 174
michael@0 175 class nsContentUtils
michael@0 176 {
michael@0 177 friend class nsAutoScriptBlockerSuppressNodeRemoved;
michael@0 178 typedef mozilla::dom::Element Element;
michael@0 179 typedef mozilla::TimeDuration TimeDuration;
michael@0 180
michael@0 181 public:
michael@0 182 static nsresult Init();
michael@0 183
michael@0 184 /**
michael@0 185 * Get a JSContext from the document's scope object.
michael@0 186 */
michael@0 187 static JSContext* GetContextFromDocument(nsIDocument *aDocument);
michael@0 188
michael@0 189 static bool IsCallerChrome();
michael@0 190 static bool ThreadsafeIsCallerChrome();
michael@0 191 static bool IsCallerXBL();
michael@0 192
michael@0 193 static bool IsImageSrcSetDisabled();
michael@0 194
michael@0 195 static bool LookupBindingMember(JSContext* aCx, nsIContent *aContent,
michael@0 196 JS::Handle<jsid> aId,
michael@0 197 JS::MutableHandle<JSPropertyDescriptor> aDesc);
michael@0 198
michael@0 199 /**
michael@0 200 * Returns the parent node of aChild crossing document boundaries.
michael@0 201 */
michael@0 202 static nsINode* GetCrossDocParentNode(nsINode* aChild);
michael@0 203
michael@0 204 /**
michael@0 205 * Do not ever pass null pointers to this method. If one of your
michael@0 206 * nsIContents is null, you have to decide for yourself what
michael@0 207 * "IsDescendantOf" really means.
michael@0 208 *
michael@0 209 * @param aPossibleDescendant node to test for being a descendant of
michael@0 210 * aPossibleAncestor
michael@0 211 * @param aPossibleAncestor node to test for being an ancestor of
michael@0 212 * aPossibleDescendant
michael@0 213 * @return true if aPossibleDescendant is a descendant of
michael@0 214 * aPossibleAncestor (or is aPossibleAncestor). false
michael@0 215 * otherwise.
michael@0 216 */
michael@0 217 static bool ContentIsDescendantOf(const nsINode* aPossibleDescendant,
michael@0 218 const nsINode* aPossibleAncestor);
michael@0 219
michael@0 220 /**
michael@0 221 * Similar to ContentIsDescendantOf, except will treat an HTMLTemplateElement
michael@0 222 * or ShadowRoot as an ancestor of things in the corresponding DocumentFragment.
michael@0 223 * See the concept of "host-including inclusive ancestor" in the DOM
michael@0 224 * specification.
michael@0 225 */
michael@0 226 static bool ContentIsHostIncludingDescendantOf(
michael@0 227 const nsINode* aPossibleDescendant, const nsINode* aPossibleAncestor);
michael@0 228
michael@0 229 /**
michael@0 230 * Similar to ContentIsDescendantOf except it crosses document boundaries.
michael@0 231 */
michael@0 232 static bool ContentIsCrossDocDescendantOf(nsINode* aPossibleDescendant,
michael@0 233 nsINode* aPossibleAncestor);
michael@0 234
michael@0 235 /*
michael@0 236 * This method fills the |aArray| with all ancestor nodes of |aNode|
michael@0 237 * including |aNode| at the zero index.
michael@0 238 */
michael@0 239 static nsresult GetAncestors(nsINode* aNode,
michael@0 240 nsTArray<nsINode*>& aArray);
michael@0 241
michael@0 242 /*
michael@0 243 * This method fills |aAncestorNodes| with all ancestor nodes of |aNode|
michael@0 244 * including |aNode| (QI'd to nsIContent) at the zero index.
michael@0 245 * For each ancestor, there is a corresponding element in |aAncestorOffsets|
michael@0 246 * which is the IndexOf the child in relation to its parent.
michael@0 247 *
michael@0 248 * This method just sucks.
michael@0 249 */
michael@0 250 static nsresult GetAncestorsAndOffsets(nsIDOMNode* aNode,
michael@0 251 int32_t aOffset,
michael@0 252 nsTArray<nsIContent*>* aAncestorNodes,
michael@0 253 nsTArray<int32_t>* aAncestorOffsets);
michael@0 254
michael@0 255 /*
michael@0 256 * The out parameter, |aCommonAncestor| will be the closest node, if any,
michael@0 257 * to both |aNode| and |aOther| which is also an ancestor of each.
michael@0 258 * Returns an error if the two nodes are disconnected and don't have
michael@0 259 * a common ancestor.
michael@0 260 */
michael@0 261 static nsresult GetCommonAncestor(nsIDOMNode *aNode,
michael@0 262 nsIDOMNode *aOther,
michael@0 263 nsIDOMNode** aCommonAncestor);
michael@0 264
michael@0 265 /**
michael@0 266 * Returns the common ancestor, if any, for two nodes. Returns null if the
michael@0 267 * nodes are disconnected.
michael@0 268 */
michael@0 269 static nsINode* GetCommonAncestor(nsINode* aNode1,
michael@0 270 nsINode* aNode2);
michael@0 271
michael@0 272 /**
michael@0 273 * Returns true if aNode1 is before aNode2 in the same connected
michael@0 274 * tree.
michael@0 275 */
michael@0 276 static bool PositionIsBefore(nsINode* aNode1, nsINode* aNode2);
michael@0 277
michael@0 278 /**
michael@0 279 * Utility routine to compare two "points", where a point is a
michael@0 280 * node/offset pair
michael@0 281 * Returns -1 if point1 < point2, 1, if point1 > point2,
michael@0 282 * 0 if error or if point1 == point2.
michael@0 283 * NOTE! If the two nodes aren't in the same connected subtree,
michael@0 284 * the result is 1, and the optional aDisconnected parameter
michael@0 285 * is set to true.
michael@0 286 */
michael@0 287 static int32_t ComparePoints(nsINode* aParent1, int32_t aOffset1,
michael@0 288 nsINode* aParent2, int32_t aOffset2,
michael@0 289 bool* aDisconnected = nullptr);
michael@0 290 static int32_t ComparePoints(nsIDOMNode* aParent1, int32_t aOffset1,
michael@0 291 nsIDOMNode* aParent2, int32_t aOffset2,
michael@0 292 bool* aDisconnected = nullptr);
michael@0 293
michael@0 294 /**
michael@0 295 * Brute-force search of the element subtree rooted at aContent for
michael@0 296 * an element with the given id. aId must be nonempty, otherwise
michael@0 297 * this method may return nodes even if they have no id!
michael@0 298 */
michael@0 299 static Element* MatchElementId(nsIContent *aContent, const nsAString& aId);
michael@0 300
michael@0 301 /**
michael@0 302 * Similar to above, but to be used if one already has an atom for the ID
michael@0 303 */
michael@0 304 static Element* MatchElementId(nsIContent *aContent, const nsIAtom* aId);
michael@0 305
michael@0 306 /**
michael@0 307 * Reverses the document position flags passed in.
michael@0 308 *
michael@0 309 * @param aDocumentPosition The document position flags to be reversed.
michael@0 310 *
michael@0 311 * @return The reversed document position flags.
michael@0 312 *
michael@0 313 * @see nsIDOMNode
michael@0 314 */
michael@0 315 static uint16_t ReverseDocumentPosition(uint16_t aDocumentPosition);
michael@0 316
michael@0 317 static uint32_t CopyNewlineNormalizedUnicodeTo(const nsAString& aSource,
michael@0 318 uint32_t aSrcOffset,
michael@0 319 char16_t* aDest,
michael@0 320 uint32_t aLength,
michael@0 321 bool& aLastCharCR);
michael@0 322
michael@0 323 static uint32_t CopyNewlineNormalizedUnicodeTo(nsReadingIterator<char16_t>& aSrcStart, const nsReadingIterator<char16_t>& aSrcEnd, nsAString& aDest);
michael@0 324
michael@0 325 static const nsDependentSubstring TrimCharsInSet(const char* aSet,
michael@0 326 const nsAString& aValue);
michael@0 327
michael@0 328 template<bool IsWhitespace(char16_t)>
michael@0 329 static const nsDependentSubstring TrimWhitespace(const nsAString& aStr,
michael@0 330 bool aTrimTrailing = true);
michael@0 331
michael@0 332 /**
michael@0 333 * Returns true if aChar is of class Ps, Pi, Po, Pf, or Pe.
michael@0 334 */
michael@0 335 static bool IsFirstLetterPunctuation(uint32_t aChar);
michael@0 336 static bool IsFirstLetterPunctuationAt(const nsTextFragment* aFrag, uint32_t aOffset);
michael@0 337
michael@0 338 /**
michael@0 339 * Returns true if aChar is of class Lu, Ll, Lt, Lm, Lo, Nd, Nl or No
michael@0 340 */
michael@0 341 static bool IsAlphanumeric(uint32_t aChar);
michael@0 342 static bool IsAlphanumericAt(const nsTextFragment* aFrag, uint32_t aOffset);
michael@0 343
michael@0 344 /*
michael@0 345 * Is the character an HTML whitespace character?
michael@0 346 *
michael@0 347 * We define whitespace using the list in HTML5 and css3-selectors:
michael@0 348 * U+0009, U+000A, U+000C, U+000D, U+0020
michael@0 349 *
michael@0 350 * HTML 4.01 also lists U+200B (zero-width space).
michael@0 351 */
michael@0 352 static bool IsHTMLWhitespace(char16_t aChar);
michael@0 353
michael@0 354 /*
michael@0 355 * Returns whether the character is an HTML whitespace (see IsHTMLWhitespace)
michael@0 356 * or a nbsp character (U+00A0).
michael@0 357 */
michael@0 358 static bool IsHTMLWhitespaceOrNBSP(char16_t aChar);
michael@0 359
michael@0 360 /**
michael@0 361 * Is the HTML local name a block element?
michael@0 362 */
michael@0 363 static bool IsHTMLBlock(nsIAtom* aLocalName);
michael@0 364
michael@0 365 /**
michael@0 366 * Is the HTML local name a void element?
michael@0 367 */
michael@0 368 static bool IsHTMLVoid(nsIAtom* aLocalName);
michael@0 369
michael@0 370 /**
michael@0 371 * Parse a margin string of format 'top, right, bottom, left' into
michael@0 372 * an nsIntMargin.
michael@0 373 *
michael@0 374 * @param aString the string to parse
michael@0 375 * @param aResult the resulting integer
michael@0 376 * @return whether the value could be parsed
michael@0 377 */
michael@0 378 static bool ParseIntMarginValue(const nsAString& aString, nsIntMargin& aResult);
michael@0 379
michael@0 380 /**
michael@0 381 * Parse the value of the <font size=""> attribute according to the HTML5
michael@0 382 * spec as of April 16, 2012.
michael@0 383 *
michael@0 384 * @param aValue the value to parse
michael@0 385 * @return 1 to 7, or 0 if the value couldn't be parsed
michael@0 386 */
michael@0 387 static int32_t ParseLegacyFontSize(const nsAString& aValue);
michael@0 388
michael@0 389 static void Shutdown();
michael@0 390
michael@0 391 /**
michael@0 392 * Checks whether two nodes come from the same origin.
michael@0 393 */
michael@0 394 static nsresult CheckSameOrigin(const nsINode* aTrustedNode,
michael@0 395 nsIDOMNode* aUnTrustedNode);
michael@0 396 static nsresult CheckSameOrigin(const nsINode* aTrustedNode,
michael@0 397 const nsINode* unTrustedNode);
michael@0 398
michael@0 399 // Check if the (JS) caller can access aNode.
michael@0 400 static bool CanCallerAccess(nsIDOMNode *aNode);
michael@0 401 static bool CanCallerAccess(nsINode* aNode);
michael@0 402
michael@0 403 // Check if the (JS) caller can access aWindow.
michael@0 404 // aWindow can be either outer or inner window.
michael@0 405 static bool CanCallerAccess(nsPIDOMWindow* aWindow);
michael@0 406
michael@0 407 /**
michael@0 408 * Get the window through the JS context that's currently on the stack.
michael@0 409 * If there's no JS context currently on the stack, returns null.
michael@0 410 */
michael@0 411 static nsPIDOMWindow *GetWindowFromCaller();
michael@0 412
michael@0 413 /**
michael@0 414 * The two GetDocumentFrom* functions below allow a caller to get at a
michael@0 415 * document that is relevant to the currently executing script.
michael@0 416 *
michael@0 417 * GetDocumentFromCaller gets its document by looking at the last called
michael@0 418 * function and finding the document that the function itself relates to.
michael@0 419 * For example, consider two windows A and B in the same origin. B has a
michael@0 420 * function which does something that ends up needing the current document.
michael@0 421 * If a script in window A were to call B's function, GetDocumentFromCaller
michael@0 422 * would find that function (in B) and return B's document.
michael@0 423 *
michael@0 424 * GetDocumentFromContext gets its document by looking at the currently
michael@0 425 * executing context's global object and returning its document. Thus,
michael@0 426 * given the example above, GetDocumentFromCaller would see that the
michael@0 427 * currently executing script was in window A, and return A's document.
michael@0 428 */
michael@0 429 /**
michael@0 430 * Get the document from the currently executing function. This will return
michael@0 431 * the document that the currently executing function is in/from.
michael@0 432 *
michael@0 433 * @return The document or null if no JS Context.
michael@0 434 */
michael@0 435 static nsIDocument* GetDocumentFromCaller();
michael@0 436
michael@0 437 /**
michael@0 438 * Get the document through the JS context that's currently on the stack.
michael@0 439 * If there's no JS context currently on the stack it will return null.
michael@0 440 * This will return the document of the calling script.
michael@0 441 *
michael@0 442 * @return The document or null if no JS context
michael@0 443 */
michael@0 444 static nsIDocument* GetDocumentFromContext();
michael@0 445
michael@0 446 // Check if a node is in the document prolog, i.e. before the document
michael@0 447 // element.
michael@0 448 static bool InProlog(nsINode *aNode);
michael@0 449
michael@0 450 static nsIParserService* GetParserService();
michael@0 451
michael@0 452 static nsNameSpaceManager* NameSpaceManager()
michael@0 453 {
michael@0 454 return sNameSpaceManager;
michael@0 455 }
michael@0 456
michael@0 457 static nsIIOService* GetIOService()
michael@0 458 {
michael@0 459 return sIOService;
michael@0 460 }
michael@0 461
michael@0 462 static nsIBidiKeyboard* GetBidiKeyboard();
michael@0 463
michael@0 464 /**
michael@0 465 * Get the cache security manager service. Can return null if the layout
michael@0 466 * module has been shut down.
michael@0 467 */
michael@0 468 static nsIScriptSecurityManager* GetSecurityManager()
michael@0 469 {
michael@0 470 return sSecurityManager;
michael@0 471 }
michael@0 472
michael@0 473 /**
michael@0 474 * Get the ContentSecurityPolicy for a JS context.
michael@0 475 **/
michael@0 476 static bool GetContentSecurityPolicy(JSContext* aCx,
michael@0 477 nsIContentSecurityPolicy** aCSP);
michael@0 478
michael@0 479 // Returns the subject principal. Guaranteed to return non-null. May only
michael@0 480 // be called when nsContentUtils is initialized.
michael@0 481 static nsIPrincipal* GetSubjectPrincipal();
michael@0 482
michael@0 483 // Returns the principal of the given JS object. This should never be null
michael@0 484 // for any object in the XPConnect runtime.
michael@0 485 //
michael@0 486 // In general, being interested in the principal of an object is enough to
michael@0 487 // guarantee that the return value is non-null.
michael@0 488 static nsIPrincipal* GetObjectPrincipal(JSObject* aObj);
michael@0 489
michael@0 490 static nsresult GenerateStateKey(nsIContent* aContent,
michael@0 491 const nsIDocument* aDocument,
michael@0 492 nsACString& aKey);
michael@0 493
michael@0 494 /**
michael@0 495 * Create a new nsIURI from aSpec, using aBaseURI as the base. The
michael@0 496 * origin charset of the new nsIURI will be the document charset of
michael@0 497 * aDocument.
michael@0 498 */
michael@0 499 static nsresult NewURIWithDocumentCharset(nsIURI** aResult,
michael@0 500 const nsAString& aSpec,
michael@0 501 nsIDocument* aDocument,
michael@0 502 nsIURI* aBaseURI);
michael@0 503
michael@0 504 /**
michael@0 505 * Convert aInput (in encoding aEncoding) to UTF16 in aOutput.
michael@0 506 *
michael@0 507 * @param aEncoding the Gecko-canonical name of the encoding or the empty
michael@0 508 * string (meaning UTF-8)
michael@0 509 */
michael@0 510 static nsresult ConvertStringFromEncoding(const nsACString& aEncoding,
michael@0 511 const nsACString& aInput,
michael@0 512 nsAString& aOutput);
michael@0 513
michael@0 514 /**
michael@0 515 * Determine whether a buffer begins with a BOM for UTF-8, UTF-16LE,
michael@0 516 * UTF-16BE
michael@0 517 *
michael@0 518 * @param aBuffer the buffer to check
michael@0 519 * @param aLength the length of the buffer
michael@0 520 * @param aCharset empty if not found
michael@0 521 * @return boolean indicating whether a BOM was detected.
michael@0 522 */
michael@0 523 static bool CheckForBOM(const unsigned char* aBuffer, uint32_t aLength,
michael@0 524 nsACString& aCharset);
michael@0 525
michael@0 526 /**
michael@0 527 * Returns true if |aName| is a valid name to be registered via
michael@0 528 * document.registerElement.
michael@0 529 */
michael@0 530 static bool IsCustomElementName(nsIAtom* aName);
michael@0 531
michael@0 532 static nsresult CheckQName(const nsAString& aQualifiedName,
michael@0 533 bool aNamespaceAware = true,
michael@0 534 const char16_t** aColon = nullptr);
michael@0 535
michael@0 536 static nsresult SplitQName(const nsIContent* aNamespaceResolver,
michael@0 537 const nsAFlatString& aQName,
michael@0 538 int32_t *aNamespace, nsIAtom **aLocalName);
michael@0 539
michael@0 540 static nsresult GetNodeInfoFromQName(const nsAString& aNamespaceURI,
michael@0 541 const nsAString& aQualifiedName,
michael@0 542 nsNodeInfoManager* aNodeInfoManager,
michael@0 543 uint16_t aNodeType,
michael@0 544 nsINodeInfo** aNodeInfo);
michael@0 545
michael@0 546 static void SplitExpatName(const char16_t *aExpatName, nsIAtom **aPrefix,
michael@0 547 nsIAtom **aTagName, int32_t *aNameSpaceID);
michael@0 548
michael@0 549 // Get a permission-manager setting for the given principal and type.
michael@0 550 // If the pref doesn't exist or if it isn't ALLOW_ACTION, false is
michael@0 551 // returned, otherwise true is returned. Always returns true for the
michael@0 552 // system principal, and false for a null principal.
michael@0 553 static bool IsSitePermAllow(nsIPrincipal* aPrincipal, const char* aType);
michael@0 554
michael@0 555 // Get a permission-manager setting for the given principal and type.
michael@0 556 // If the pref doesn't exist or if it isn't DENY_ACTION, false is
michael@0 557 // returned, otherwise true is returned. Always returns false for the
michael@0 558 // system principal, and true for a null principal.
michael@0 559 static bool IsSitePermDeny(nsIPrincipal* aPrincipal, const char* aType);
michael@0 560
michael@0 561 // Get a permission-manager setting for the given principal and type.
michael@0 562 // If the pref doesn't exist or if it isn't ALLOW_ACTION, false is
michael@0 563 // returned, otherwise true is returned. Always returns true for the
michael@0 564 // system principal, and false for a null principal.
michael@0 565 // This version checks the permission for an exact host match on
michael@0 566 // the principal
michael@0 567 static bool IsExactSitePermAllow(nsIPrincipal* aPrincipal, const char* aType);
michael@0 568
michael@0 569 // Get a permission-manager setting for the given principal and type.
michael@0 570 // If the pref doesn't exist or if it isn't DENY_ACTION, false is
michael@0 571 // returned, otherwise true is returned. Always returns false for the
michael@0 572 // system principal, and true for a null principal.
michael@0 573 // This version checks the permission for an exact host match on
michael@0 574 // the principal
michael@0 575 static bool IsExactSitePermDeny(nsIPrincipal* aPrincipal, const char* aType);
michael@0 576
michael@0 577 // Returns true if aDoc1 and aDoc2 have equal NodePrincipal()s.
michael@0 578 static bool HaveEqualPrincipals(nsIDocument* aDoc1, nsIDocument* aDoc2);
michael@0 579
michael@0 580 static nsILineBreaker* LineBreaker()
michael@0 581 {
michael@0 582 return sLineBreaker;
michael@0 583 }
michael@0 584
michael@0 585 static nsIWordBreaker* WordBreaker()
michael@0 586 {
michael@0 587 return sWordBreaker;
michael@0 588 }
michael@0 589
michael@0 590 /**
michael@0 591 * Regster aObserver as a shutdown observer. A strong reference is held
michael@0 592 * to aObserver until UnregisterShutdownObserver is called.
michael@0 593 */
michael@0 594 static void RegisterShutdownObserver(nsIObserver* aObserver);
michael@0 595 static void UnregisterShutdownObserver(nsIObserver* aObserver);
michael@0 596
michael@0 597 /**
michael@0 598 * @return true if aContent has an attribute aName in namespace aNameSpaceID,
michael@0 599 * and the attribute value is non-empty.
michael@0 600 */
michael@0 601 static bool HasNonEmptyAttr(const nsIContent* aContent, int32_t aNameSpaceID,
michael@0 602 nsIAtom* aName);
michael@0 603
michael@0 604 /**
michael@0 605 * Method that gets the primary presContext for the node.
michael@0 606 *
michael@0 607 * @param aContent The content node.
michael@0 608 * @return the presContext, or nullptr if the content is not in a document
michael@0 609 * (if GetCurrentDoc returns nullptr)
michael@0 610 */
michael@0 611 static nsPresContext* GetContextForContent(const nsIContent* aContent);
michael@0 612
michael@0 613 /**
michael@0 614 * Method to do security and content policy checks on the image URI
michael@0 615 *
michael@0 616 * @param aURI uri of the image to be loaded
michael@0 617 * @param aContext the context the image is loaded in (eg an element)
michael@0 618 * @param aLoadingDocument the document we belong to
michael@0 619 * @param aLoadingPrincipal the principal doing the load
michael@0 620 * @param aImageBlockingStatus the nsIContentPolicy blocking status for this
michael@0 621 * image. This will be set even if a security check fails for the
michael@0 622 * image, to some reasonable REJECT_* value. This out param will only
michael@0 623 * be set if it's non-null.
michael@0 624 * @return true if the load can proceed, or false if it is blocked.
michael@0 625 * Note that aImageBlockingStatus, if set will always be an ACCEPT
michael@0 626 * status if true is returned and always be a REJECT_* status if
michael@0 627 * false is returned.
michael@0 628 */
michael@0 629 static bool CanLoadImage(nsIURI* aURI,
michael@0 630 nsISupports* aContext,
michael@0 631 nsIDocument* aLoadingDocument,
michael@0 632 nsIPrincipal* aLoadingPrincipal,
michael@0 633 int16_t* aImageBlockingStatus = nullptr);
michael@0 634 /**
michael@0 635 * Method to start an image load. This does not do any security checks.
michael@0 636 * This method will attempt to make aURI immutable; a caller that wants to
michael@0 637 * keep a mutable version around should pass in a clone.
michael@0 638 *
michael@0 639 * @param aURI uri of the image to be loaded
michael@0 640 * @param aLoadingDocument the document we belong to
michael@0 641 * @param aLoadingPrincipal the principal doing the load
michael@0 642 * @param aReferrer the referrer URI
michael@0 643 * @param aObserver the observer for the image load
michael@0 644 * @param aLoadFlags the load flags to use. See nsIRequest
michael@0 645 * @return the imgIRequest for the image load
michael@0 646 */
michael@0 647 static nsresult LoadImage(nsIURI* aURI,
michael@0 648 nsIDocument* aLoadingDocument,
michael@0 649 nsIPrincipal* aLoadingPrincipal,
michael@0 650 nsIURI* aReferrer,
michael@0 651 imgINotificationObserver* aObserver,
michael@0 652 int32_t aLoadFlags,
michael@0 653 const nsAString& initiatorType,
michael@0 654 imgRequestProxy** aRequest);
michael@0 655
michael@0 656 /**
michael@0 657 * Obtain an image loader that respects the given document/channel's privacy status.
michael@0 658 * Null document/channel arguments return the public image loader.
michael@0 659 */
michael@0 660 static imgLoader* GetImgLoaderForDocument(nsIDocument* aDoc);
michael@0 661 static imgLoader* GetImgLoaderForChannel(nsIChannel* aChannel);
michael@0 662
michael@0 663 /**
michael@0 664 * Returns whether the given URI is in the image cache.
michael@0 665 */
michael@0 666 static bool IsImageInCache(nsIURI* aURI, nsIDocument* aDocument);
michael@0 667
michael@0 668 /**
michael@0 669 * Method to get an imgIContainer from an image loading content
michael@0 670 *
michael@0 671 * @param aContent The image loading content. Must not be null.
michael@0 672 * @param aRequest The image request [out]
michael@0 673 * @return the imgIContainer corresponding to the first frame of the image
michael@0 674 */
michael@0 675 static already_AddRefed<imgIContainer> GetImageFromContent(nsIImageLoadingContent* aContent, imgIRequest **aRequest = nullptr);
michael@0 676
michael@0 677 /**
michael@0 678 * Helper method to call imgIRequest::GetStaticRequest.
michael@0 679 */
michael@0 680 static already_AddRefed<imgRequestProxy> GetStaticRequest(imgRequestProxy* aRequest);
michael@0 681
michael@0 682 /**
michael@0 683 * Method that decides whether a content node is draggable
michael@0 684 *
michael@0 685 * @param aContent The content node to test.
michael@0 686 * @return whether it's draggable
michael@0 687 */
michael@0 688 static bool ContentIsDraggable(nsIContent* aContent);
michael@0 689
michael@0 690 /**
michael@0 691 * Method that decides whether a content node is a draggable image
michael@0 692 *
michael@0 693 * @param aContent The content node to test.
michael@0 694 * @return whether it's a draggable image
michael@0 695 */
michael@0 696 static bool IsDraggableImage(nsIContent* aContent);
michael@0 697
michael@0 698 /**
michael@0 699 * Method that decides whether a content node is a draggable link
michael@0 700 *
michael@0 701 * @param aContent The content node to test.
michael@0 702 * @return whether it's a draggable link
michael@0 703 */
michael@0 704 static bool IsDraggableLink(const nsIContent* aContent);
michael@0 705
michael@0 706 /**
michael@0 707 * Convenience method to create a new nodeinfo that differs only by name
michael@0 708 * from aNodeInfo.
michael@0 709 */
michael@0 710 static nsresult NameChanged(nsINodeInfo* aNodeInfo, nsIAtom* aName,
michael@0 711 nsINodeInfo** aResult);
michael@0 712
michael@0 713 /**
michael@0 714 * Returns the appropriate event argument names for the specified
michael@0 715 * namespace and event name. Added because we need to switch between
michael@0 716 * SVG's "evt" and the rest of the world's "event", and because onerror
michael@0 717 * takes 3 args.
michael@0 718 */
michael@0 719 static void GetEventArgNames(int32_t aNameSpaceID, nsIAtom *aEventName,
michael@0 720 uint32_t *aArgCount, const char*** aArgNames);
michael@0 721
michael@0 722 /**
michael@0 723 * If aNode is not an element, return true exactly when aContent's binding
michael@0 724 * parent is null.
michael@0 725 *
michael@0 726 * If aNode is an element, return true exactly when aContent's binding parent
michael@0 727 * is the same as aNode's.
michael@0 728 *
michael@0 729 * This method is particularly useful for callers who are trying to ensure
michael@0 730 * that they are working with a non-anonymous descendant of a given node. If
michael@0 731 * aContent is a descendant of aNode, a return value of false from this
michael@0 732 * method means that it's an anonymous descendant from aNode's point of view.
michael@0 733 *
michael@0 734 * Both arguments to this method must be non-null.
michael@0 735 */
michael@0 736 static bool IsInSameAnonymousTree(const nsINode* aNode, const nsIContent* aContent);
michael@0 737
michael@0 738 /**
michael@0 739 * Return the nsIXPConnect service.
michael@0 740 */
michael@0 741 static nsIXPConnect *XPConnect()
michael@0 742 {
michael@0 743 return sXPConnect;
michael@0 744 }
michael@0 745
michael@0 746 /**
michael@0 747 * Report simple error message to the browser console
michael@0 748 * @param aErrorText the error message
michael@0 749 * @param classification Name of the module reporting error
michael@0 750 */
michael@0 751 static void LogSimpleConsoleError(const nsAString& aErrorText,
michael@0 752 const char * classification);
michael@0 753
michael@0 754 /**
michael@0 755 * Report a non-localized error message to the error console.
michael@0 756 * @param aErrorText the error message
michael@0 757 * @param aErrorFlags See nsIScriptError.
michael@0 758 * @param aCategory Name of module reporting error.
michael@0 759 * @param aDocument Reference to the document which triggered the message.
michael@0 760 * @param [aURI=nullptr] (Optional) URI of resource containing error.
michael@0 761 * @param [aSourceLine=EmptyString()] (Optional) The text of the line that
michael@0 762 contains the error (may be empty).
michael@0 763 * @param [aLineNumber=0] (Optional) Line number within resource
michael@0 764 containing error.
michael@0 765 * @param [aColumnNumber=0] (Optional) Column number within resource
michael@0 766 containing error.
michael@0 767 If aURI is null, then aDocument->GetDocumentURI() is used.
michael@0 768 */
michael@0 769 static nsresult ReportToConsoleNonLocalized(const nsAString& aErrorText,
michael@0 770 uint32_t aErrorFlags,
michael@0 771 const nsACString& aCategory,
michael@0 772 nsIDocument* aDocument,
michael@0 773 nsIURI* aURI = nullptr,
michael@0 774 const nsAFlatString& aSourceLine
michael@0 775 = EmptyString(),
michael@0 776 uint32_t aLineNumber = 0,
michael@0 777 uint32_t aColumnNumber = 0);
michael@0 778
michael@0 779 /**
michael@0 780 * Report a localized error message to the error console.
michael@0 781 * @param aErrorFlags See nsIScriptError.
michael@0 782 * @param aCategory Name of module reporting error.
michael@0 783 * @param aDocument Reference to the document which triggered the message.
michael@0 784 * @param aFile Properties file containing localized message.
michael@0 785 * @param aMessageName Name of localized message.
michael@0 786 * @param [aParams=nullptr] (Optional) Parameters to be substituted into
michael@0 787 localized message.
michael@0 788 * @param [aParamsLength=0] (Optional) Length of aParams.
michael@0 789 * @param [aURI=nullptr] (Optional) URI of resource containing error.
michael@0 790 * @param [aSourceLine=EmptyString()] (Optional) The text of the line that
michael@0 791 contains the error (may be empty).
michael@0 792 * @param [aLineNumber=0] (Optional) Line number within resource
michael@0 793 containing error.
michael@0 794 * @param [aColumnNumber=0] (Optional) Column number within resource
michael@0 795 containing error.
michael@0 796 If aURI is null, then aDocument->GetDocumentURI() is used.
michael@0 797 */
michael@0 798 enum PropertiesFile {
michael@0 799 eCSS_PROPERTIES,
michael@0 800 eXBL_PROPERTIES,
michael@0 801 eXUL_PROPERTIES,
michael@0 802 eLAYOUT_PROPERTIES,
michael@0 803 eFORMS_PROPERTIES,
michael@0 804 ePRINTING_PROPERTIES,
michael@0 805 eDOM_PROPERTIES,
michael@0 806 eHTMLPARSER_PROPERTIES,
michael@0 807 eSVG_PROPERTIES,
michael@0 808 eBRAND_PROPERTIES,
michael@0 809 eCOMMON_DIALOG_PROPERTIES,
michael@0 810 eMATHML_PROPERTIES,
michael@0 811 eSECURITY_PROPERTIES,
michael@0 812 PropertiesFile_COUNT
michael@0 813 };
michael@0 814 static nsresult ReportToConsole(uint32_t aErrorFlags,
michael@0 815 const nsACString& aCategory,
michael@0 816 nsIDocument* aDocument,
michael@0 817 PropertiesFile aFile,
michael@0 818 const char *aMessageName,
michael@0 819 const char16_t **aParams = nullptr,
michael@0 820 uint32_t aParamsLength = 0,
michael@0 821 nsIURI* aURI = nullptr,
michael@0 822 const nsAFlatString& aSourceLine
michael@0 823 = EmptyString(),
michael@0 824 uint32_t aLineNumber = 0,
michael@0 825 uint32_t aColumnNumber = 0);
michael@0 826
michael@0 827 static void LogMessageToConsole(const char* aMsg, ...);
michael@0 828
michael@0 829 /**
michael@0 830 * Get the localized string named |aKey| in properties file |aFile|.
michael@0 831 */
michael@0 832 static nsresult GetLocalizedString(PropertiesFile aFile,
michael@0 833 const char* aKey,
michael@0 834 nsXPIDLString& aResult);
michael@0 835
michael@0 836 /**
michael@0 837 * A helper function that parses a sandbox attribute (of an <iframe> or
michael@0 838 * a CSP directive) and converts it to the set of flags used internally.
michael@0 839 *
michael@0 840 * @param sandboxAttr the sandbox attribute
michael@0 841 * @return the set of flags (0 if sandboxAttr is null)
michael@0 842 */
michael@0 843 static uint32_t ParseSandboxAttributeToFlags(const nsAttrValue* sandboxAttr);
michael@0 844
michael@0 845
michael@0 846 /**
michael@0 847 * Fill (with the parameters given) the localized string named |aKey| in
michael@0 848 * properties file |aFile|.
michael@0 849 */
michael@0 850 private:
michael@0 851 static nsresult FormatLocalizedString(PropertiesFile aFile,
michael@0 852 const char* aKey,
michael@0 853 const char16_t** aParams,
michael@0 854 uint32_t aParamsLength,
michael@0 855 nsXPIDLString& aResult);
michael@0 856
michael@0 857 public:
michael@0 858 template<uint32_t N>
michael@0 859 static nsresult FormatLocalizedString(PropertiesFile aFile,
michael@0 860 const char* aKey,
michael@0 861 const char16_t* (&aParams)[N],
michael@0 862 nsXPIDLString& aResult)
michael@0 863 {
michael@0 864 return FormatLocalizedString(aFile, aKey, aParams, N, aResult);
michael@0 865 }
michael@0 866
michael@0 867 /**
michael@0 868 * Returns true if aDocument is a chrome document
michael@0 869 */
michael@0 870 static bool IsChromeDoc(nsIDocument *aDocument);
michael@0 871
michael@0 872 /**
michael@0 873 * Returns true if aDocument is in a docshell whose parent is the same type
michael@0 874 */
michael@0 875 static bool IsChildOfSameType(nsIDocument* aDoc);
michael@0 876
michael@0 877 /**
michael@0 878 '* Returns true if the content-type will be rendered as plain-text.
michael@0 879 */
michael@0 880 static bool IsPlainTextType(const nsACString& aContentType);
michael@0 881
michael@0 882 /**
michael@0 883 * Get the script file name to use when compiling the script
michael@0 884 * referenced by aURI. In cases where there's no need for any extra
michael@0 885 * security wrapper automation the script file name that's returned
michael@0 886 * will be the spec in aURI, else it will be the spec in aDocument's
michael@0 887 * URI followed by aURI's spec, separated by " -> ". Returns true
michael@0 888 * if the script file name was modified, false if it's aURI's
michael@0 889 * spec.
michael@0 890 */
michael@0 891 static bool GetWrapperSafeScriptFilename(nsIDocument *aDocument,
michael@0 892 nsIURI *aURI,
michael@0 893 nsACString& aScriptURI);
michael@0 894
michael@0 895
michael@0 896 /**
michael@0 897 * Returns true if aDocument belongs to a chrome docshell for
michael@0 898 * display purposes. Returns false for null documents or documents
michael@0 899 * which do not belong to a docshell.
michael@0 900 */
michael@0 901 static bool IsInChromeDocshell(nsIDocument *aDocument);
michael@0 902
michael@0 903 /**
michael@0 904 * Return the content policy service
michael@0 905 */
michael@0 906 static nsIContentPolicy *GetContentPolicy();
michael@0 907
michael@0 908 /**
michael@0 909 * Quick helper to determine whether there are any mutation listeners
michael@0 910 * of a given type that apply to this content or any of its ancestors.
michael@0 911 * The method has the side effect to call document's MayDispatchMutationEvent
michael@0 912 * using aTargetForSubtreeModified as the parameter.
michael@0 913 *
michael@0 914 * @param aNode The node to search for listeners
michael@0 915 * @param aType The type of listener (NS_EVENT_BITS_MUTATION_*)
michael@0 916 * @param aTargetForSubtreeModified The node which is the target of the
michael@0 917 * possible DOMSubtreeModified event.
michael@0 918 *
michael@0 919 * @return true if there are mutation listeners of the specified type
michael@0 920 */
michael@0 921 static bool HasMutationListeners(nsINode* aNode,
michael@0 922 uint32_t aType,
michael@0 923 nsINode* aTargetForSubtreeModified);
michael@0 924
michael@0 925 /**
michael@0 926 * Quick helper to determine whether there are any mutation listeners
michael@0 927 * of a given type that apply to any content in this document. It is valid
michael@0 928 * to pass null for aDocument here, in which case this function always
michael@0 929 * returns true.
michael@0 930 *
michael@0 931 * @param aDocument The document to search for listeners
michael@0 932 * @param aType The type of listener (NS_EVENT_BITS_MUTATION_*)
michael@0 933 *
michael@0 934 * @return true if there are mutation listeners of the specified type
michael@0 935 */
michael@0 936 static bool HasMutationListeners(nsIDocument* aDocument,
michael@0 937 uint32_t aType);
michael@0 938 /**
michael@0 939 * Synchronously fire DOMNodeRemoved on aChild. Only fires the event if
michael@0 940 * there really are listeners by checking using the HasMutationListeners
michael@0 941 * function above. The function makes sure to hold the relevant objects alive
michael@0 942 * for the duration of the event firing. However there are no guarantees
michael@0 943 * that any of the objects are alive by the time the function returns.
michael@0 944 * If you depend on that you need to hold references yourself.
michael@0 945 *
michael@0 946 * @param aChild The node to fire DOMNodeRemoved at.
michael@0 947 * @param aParent The parent of aChild.
michael@0 948 * @param aOwnerDoc The ownerDocument of aChild.
michael@0 949 */
michael@0 950 static void MaybeFireNodeRemoved(nsINode* aChild, nsINode* aParent,
michael@0 951 nsIDocument* aOwnerDoc);
michael@0 952
michael@0 953 /**
michael@0 954 * This method creates and dispatches a trusted event.
michael@0 955 * Works only with events which can be created by calling
michael@0 956 * nsIDOMDocument::CreateEvent() with parameter "Events".
michael@0 957 * @param aDoc The document which will be used to create the event.
michael@0 958 * @param aTarget The target of the event, should be QIable to
michael@0 959 * nsIDOMEventTarget.
michael@0 960 * @param aEventName The name of the event.
michael@0 961 * @param aCanBubble Whether the event can bubble.
michael@0 962 * @param aCancelable Is the event cancelable.
michael@0 963 * @param aDefaultAction Set to true if default action should be taken,
michael@0 964 * see nsIDOMEventTarget::DispatchEvent.
michael@0 965 */
michael@0 966 static nsresult DispatchTrustedEvent(nsIDocument* aDoc,
michael@0 967 nsISupports* aTarget,
michael@0 968 const nsAString& aEventName,
michael@0 969 bool aCanBubble,
michael@0 970 bool aCancelable,
michael@0 971 bool *aDefaultAction = nullptr);
michael@0 972
michael@0 973 /**
michael@0 974 * This method creates and dispatches a untrusted event.
michael@0 975 * Works only with events which can be created by calling
michael@0 976 * nsIDOMDocument::CreateEvent() with parameter "Events".
michael@0 977 * @param aDoc The document which will be used to create the event.
michael@0 978 * @param aTarget The target of the event, should be QIable to
michael@0 979 * nsIDOMEventTarget.
michael@0 980 * @param aEventName The name of the event.
michael@0 981 * @param aCanBubble Whether the event can bubble.
michael@0 982 * @param aCancelable Is the event cancelable.
michael@0 983 * @param aDefaultAction Set to true if default action should be taken,
michael@0 984 * see nsIDOMEventTarget::DispatchEvent.
michael@0 985 */
michael@0 986 static nsresult DispatchUntrustedEvent(nsIDocument* aDoc,
michael@0 987 nsISupports* aTarget,
michael@0 988 const nsAString& aEventName,
michael@0 989 bool aCanBubble,
michael@0 990 bool aCancelable,
michael@0 991 bool *aDefaultAction = nullptr);
michael@0 992
michael@0 993 /**
michael@0 994 * This method creates and dispatches a trusted event to the chrome
michael@0 995 * event handler.
michael@0 996 * Works only with events which can be created by calling
michael@0 997 * nsIDOMDocument::CreateEvent() with parameter "Events".
michael@0 998 * @param aDocument The document which will be used to create the event,
michael@0 999 * and whose window's chrome handler will be used to
michael@0 1000 * dispatch the event.
michael@0 1001 * @param aTarget The target of the event, used for event->SetTarget()
michael@0 1002 * @param aEventName The name of the event.
michael@0 1003 * @param aCanBubble Whether the event can bubble.
michael@0 1004 * @param aCancelable Is the event cancelable.
michael@0 1005 * @param aDefaultAction Set to true if default action should be taken,
michael@0 1006 * see nsIDOMEventTarget::DispatchEvent.
michael@0 1007 */
michael@0 1008 static nsresult DispatchChromeEvent(nsIDocument* aDoc,
michael@0 1009 nsISupports* aTarget,
michael@0 1010 const nsAString& aEventName,
michael@0 1011 bool aCanBubble,
michael@0 1012 bool aCancelable,
michael@0 1013 bool *aDefaultAction = nullptr);
michael@0 1014
michael@0 1015 /**
michael@0 1016 * Determines if an event attribute name (such as onclick) is valid for
michael@0 1017 * a given element type. Types are from the EventNameType enumeration
michael@0 1018 * defined above.
michael@0 1019 *
michael@0 1020 * @param aName the event name to look up
michael@0 1021 * @param aType the type of content
michael@0 1022 */
michael@0 1023 static bool IsEventAttributeName(nsIAtom* aName, int32_t aType);
michael@0 1024
michael@0 1025 /**
michael@0 1026 * Return the event id for the event with the given name. The name is the
michael@0 1027 * event name with the 'on' prefix. Returns NS_USER_DEFINED_EVENT if the
michael@0 1028 * event doesn't match a known event name.
michael@0 1029 *
michael@0 1030 * @param aName the event name to look up
michael@0 1031 */
michael@0 1032 static uint32_t GetEventId(nsIAtom* aName);
michael@0 1033
michael@0 1034 /**
michael@0 1035 * Return the category for the event with the given name. The name is the
michael@0 1036 * event name *without* the 'on' prefix. Returns NS_EVENT if the event
michael@0 1037 * is not known to be in any particular category.
michael@0 1038 *
michael@0 1039 * @param aName the event name to look up
michael@0 1040 */
michael@0 1041 static uint32_t GetEventCategory(const nsAString& aName);
michael@0 1042
michael@0 1043 /**
michael@0 1044 * Return the event id and atom for the event with the given name.
michael@0 1045 * The name is the event name *without* the 'on' prefix.
michael@0 1046 * Returns NS_USER_DEFINED_EVENT on the aEventID if the
michael@0 1047 * event doesn't match a known event name in the category.
michael@0 1048 *
michael@0 1049 * @param aName the event name to look up
michael@0 1050 * @param aEventStruct only return event id in aEventStruct category
michael@0 1051 */
michael@0 1052 static nsIAtom* GetEventIdAndAtom(const nsAString& aName,
michael@0 1053 uint32_t aEventStruct,
michael@0 1054 uint32_t* aEventID);
michael@0 1055
michael@0 1056 /**
michael@0 1057 * Used only during traversal of the XPCOM graph by the cycle
michael@0 1058 * collector: push a pointer to the listener manager onto the
michael@0 1059 * children deque, if it exists. Do nothing if there is no listener
michael@0 1060 * manager.
michael@0 1061 *
michael@0 1062 * Crucially: does not perform any refcounting operations.
michael@0 1063 *
michael@0 1064 * @param aNode The node to traverse.
michael@0 1065 * @param children The buffer to push a listener manager pointer into.
michael@0 1066 */
michael@0 1067 static void TraverseListenerManager(nsINode *aNode,
michael@0 1068 nsCycleCollectionTraversalCallback &cb);
michael@0 1069
michael@0 1070 /**
michael@0 1071 * Get the eventlistener manager for aNode, creating it if it does not
michael@0 1072 * already exist.
michael@0 1073 *
michael@0 1074 * @param aNode The node for which to get the eventlistener manager.
michael@0 1075 */
michael@0 1076 static mozilla::EventListenerManager*
michael@0 1077 GetListenerManagerForNode(nsINode* aNode);
michael@0 1078 /**
michael@0 1079 * Get the eventlistener manager for aNode, returning null if it does not
michael@0 1080 * already exist.
michael@0 1081 *
michael@0 1082 * @param aNode The node for which to get the eventlistener manager.
michael@0 1083 */
michael@0 1084 static mozilla::EventListenerManager*
michael@0 1085 GetExistingListenerManagerForNode(const nsINode* aNode);
michael@0 1086
michael@0 1087 static void UnmarkGrayJSListenersInCCGenerationDocuments(uint32_t aGeneration);
michael@0 1088
michael@0 1089 /**
michael@0 1090 * Remove the eventlistener manager for aNode.
michael@0 1091 *
michael@0 1092 * @param aNode The node for which to remove the eventlistener manager.
michael@0 1093 */
michael@0 1094 static void RemoveListenerManager(nsINode *aNode);
michael@0 1095
michael@0 1096 static bool IsInitialized()
michael@0 1097 {
michael@0 1098 return sInitialized;
michael@0 1099 }
michael@0 1100
michael@0 1101 /**
michael@0 1102 * Checks if the localname/prefix/namespace triple is valid wrt prefix
michael@0 1103 * and namespace according to the Namespaces in XML and DOM Code
michael@0 1104 * specfications.
michael@0 1105 *
michael@0 1106 * @param aLocalname localname of the node
michael@0 1107 * @param aPrefix prefix of the node
michael@0 1108 * @param aNamespaceID namespace of the node
michael@0 1109 */
michael@0 1110 static bool IsValidNodeName(nsIAtom *aLocalName, nsIAtom *aPrefix,
michael@0 1111 int32_t aNamespaceID);
michael@0 1112
michael@0 1113 /**
michael@0 1114 * Creates a DocumentFragment from text using a context node to resolve
michael@0 1115 * namespaces.
michael@0 1116 *
michael@0 1117 * Note! In the HTML case with the HTML5 parser enabled, this is only called
michael@0 1118 * from Range.createContextualFragment() and the implementation here is
michael@0 1119 * quirky accordingly (html context node behaves like a body context node).
michael@0 1120 * If you don't want that quirky behavior, don't use this method as-is!
michael@0 1121 *
michael@0 1122 * @param aContextNode the node which is used to resolve namespaces
michael@0 1123 * @param aFragment the string which is parsed to a DocumentFragment
michael@0 1124 * @param aReturn the resulting fragment
michael@0 1125 * @param aPreventScriptExecution whether to mark scripts as already started
michael@0 1126 */
michael@0 1127 static nsresult CreateContextualFragment(nsINode* aContextNode,
michael@0 1128 const nsAString& aFragment,
michael@0 1129 bool aPreventScriptExecution,
michael@0 1130 nsIDOMDocumentFragment** aReturn);
michael@0 1131 static already_AddRefed<mozilla::dom::DocumentFragment>
michael@0 1132 CreateContextualFragment(nsINode* aContextNode, const nsAString& aFragment,
michael@0 1133 bool aPreventScriptExecution,
michael@0 1134 mozilla::ErrorResult& aRv);
michael@0 1135
michael@0 1136 /**
michael@0 1137 * Invoke the fragment parsing algorithm (innerHTML) using the HTML parser.
michael@0 1138 *
michael@0 1139 * @param aSourceBuffer the string being set as innerHTML
michael@0 1140 * @param aTargetNode the target container
michael@0 1141 * @param aContextLocalName local name of context node
michael@0 1142 * @param aContextNamespace namespace of context node
michael@0 1143 * @param aQuirks true to make <table> not close <p>
michael@0 1144 * @param aPreventScriptExecution true to prevent scripts from executing;
michael@0 1145 * don't set to false when parsing into a target node that has been
michael@0 1146 * bound to tree.
michael@0 1147 * @return NS_ERROR_DOM_INVALID_STATE_ERR if a re-entrant attempt to parse
michael@0 1148 * fragments is made, NS_ERROR_OUT_OF_MEMORY if aSourceBuffer is too
michael@0 1149 * long and NS_OK otherwise.
michael@0 1150 */
michael@0 1151 static nsresult ParseFragmentHTML(const nsAString& aSourceBuffer,
michael@0 1152 nsIContent* aTargetNode,
michael@0 1153 nsIAtom* aContextLocalName,
michael@0 1154 int32_t aContextNamespace,
michael@0 1155 bool aQuirks,
michael@0 1156 bool aPreventScriptExecution);
michael@0 1157
michael@0 1158 /**
michael@0 1159 * Invoke the fragment parsing algorithm (innerHTML) using the XML parser.
michael@0 1160 *
michael@0 1161 * @param aSourceBuffer the string being set as innerHTML
michael@0 1162 * @param aTargetNode the target container
michael@0 1163 * @param aTagStack the namespace mapping context
michael@0 1164 * @param aPreventExecution whether to mark scripts as already started
michael@0 1165 * @param aReturn the result fragment
michael@0 1166 * @return NS_ERROR_DOM_INVALID_STATE_ERR if a re-entrant attempt to parse
michael@0 1167 * fragments is made, a return code from the XML parser.
michael@0 1168 */
michael@0 1169 static nsresult ParseFragmentXML(const nsAString& aSourceBuffer,
michael@0 1170 nsIDocument* aDocument,
michael@0 1171 nsTArray<nsString>& aTagStack,
michael@0 1172 bool aPreventScriptExecution,
michael@0 1173 nsIDOMDocumentFragment** aReturn);
michael@0 1174
michael@0 1175 /**
michael@0 1176 * Parse a string into a document using the HTML parser.
michael@0 1177 * Script elements are marked unexecutable.
michael@0 1178 *
michael@0 1179 * @param aSourceBuffer the string to parse as an HTML document
michael@0 1180 * @param aTargetDocument the document object to parse into. Must not have
michael@0 1181 * child nodes.
michael@0 1182 * @param aScriptingEnabledForNoscriptParsing whether <noscript> is parsed
michael@0 1183 * as if scripting was enabled
michael@0 1184 * @return NS_ERROR_DOM_INVALID_STATE_ERR if a re-entrant attempt to parse
michael@0 1185 * fragments is made, NS_ERROR_OUT_OF_MEMORY if aSourceBuffer is too
michael@0 1186 * long and NS_OK otherwise.
michael@0 1187 */
michael@0 1188 static nsresult ParseDocumentHTML(const nsAString& aSourceBuffer,
michael@0 1189 nsIDocument* aTargetDocument,
michael@0 1190 bool aScriptingEnabledForNoscriptParsing);
michael@0 1191
michael@0 1192 /**
michael@0 1193 * Converts HTML source to plain text by parsing the source and using the
michael@0 1194 * plain text serializer on the resulting tree.
michael@0 1195 *
michael@0 1196 * @param aSourceBuffer the string to parse as an HTML document
michael@0 1197 * @param aResultBuffer the string where the plain text result appears;
michael@0 1198 * may be the same string as aSourceBuffer
michael@0 1199 * @param aFlags Flags from nsIDocumentEncoder.
michael@0 1200 * @param aWrapCol Number of columns after which to line wrap; 0 for no
michael@0 1201 * auto-wrapping
michael@0 1202 * @return NS_ERROR_DOM_INVALID_STATE_ERR if a re-entrant attempt to parse
michael@0 1203 * fragments is made, NS_ERROR_OUT_OF_MEMORY if aSourceBuffer is too
michael@0 1204 * long and NS_OK otherwise.
michael@0 1205 */
michael@0 1206 static nsresult ConvertToPlainText(const nsAString& aSourceBuffer,
michael@0 1207 nsAString& aResultBuffer,
michael@0 1208 uint32_t aFlags,
michael@0 1209 uint32_t aWrapCol);
michael@0 1210
michael@0 1211 /**
michael@0 1212 * Sets the text contents of a node by replacing all existing children
michael@0 1213 * with a single text child.
michael@0 1214 *
michael@0 1215 * The function always notifies.
michael@0 1216 *
michael@0 1217 * Will reuse the first text child if one is available. Will not reuse
michael@0 1218 * existing cdata children.
michael@0 1219 *
michael@0 1220 * @param aContent Node to set contents of.
michael@0 1221 * @param aValue Value to set contents to.
michael@0 1222 * @param aTryReuse When true, the function will try to reuse an existing
michael@0 1223 * textnodes rather than always creating a new one.
michael@0 1224 */
michael@0 1225 static nsresult SetNodeTextContent(nsIContent* aContent,
michael@0 1226 const nsAString& aValue,
michael@0 1227 bool aTryReuse);
michael@0 1228
michael@0 1229 /**
michael@0 1230 * Get the textual contents of a node. This is a concatenation of all
michael@0 1231 * textnodes that are direct or (depending on aDeep) indirect children
michael@0 1232 * of the node.
michael@0 1233 *
michael@0 1234 * NOTE! No serialization takes place and <br> elements
michael@0 1235 * are not converted into newlines. Only textnodes and cdata nodes are
michael@0 1236 * added to the result.
michael@0 1237 *
michael@0 1238 * @param aNode Node to get textual contents of.
michael@0 1239 * @param aDeep If true child elements of aNode are recursivly descended
michael@0 1240 * into to find text children.
michael@0 1241 * @param aResult the result. Out param.
michael@0 1242 * @return false on out of memory errors, true otherwise.
michael@0 1243 */
michael@0 1244 static bool GetNodeTextContent(nsINode* aNode, bool aDeep,
michael@0 1245 nsAString& aResult) NS_WARN_UNUSED_RESULT;
michael@0 1246
michael@0 1247 /**
michael@0 1248 * Same as GetNodeTextContents but appends the result rather than sets it.
michael@0 1249 */
michael@0 1250 static bool AppendNodeTextContent(nsINode* aNode, bool aDeep,
michael@0 1251 nsAString& aResult, const mozilla::fallible_t&);
michael@0 1252
michael@0 1253 /**
michael@0 1254 * Utility method that checks if a given node has any non-empty
michael@0 1255 * children.
michael@0 1256 * NOTE! This method does not descend recursivly into elements.
michael@0 1257 * Though it would be easy to make it so if needed
michael@0 1258 */
michael@0 1259 static bool HasNonEmptyTextContent(nsINode* aNode);
michael@0 1260
michael@0 1261 /**
michael@0 1262 * Delete strings allocated for nsContentList matches
michael@0 1263 */
michael@0 1264 static void DestroyMatchString(void* aData);
michael@0 1265
michael@0 1266 /**
michael@0 1267 * Unbinds the content from the tree and nulls it out if it's not null.
michael@0 1268 */
michael@0 1269 static void DestroyAnonymousContent(nsCOMPtr<nsIContent>* aContent);
michael@0 1270 static void DestroyAnonymousContent(nsCOMPtr<Element>* aElement);
michael@0 1271
michael@0 1272 static void DeferredFinalize(nsISupports* aSupports);
michael@0 1273 static void DeferredFinalize(mozilla::DeferredFinalizeAppendFunction aAppendFunc,
michael@0 1274 mozilla::DeferredFinalizeFunction aFunc,
michael@0 1275 void* aThing);
michael@0 1276
michael@0 1277 /*
michael@0 1278 * Notify when the first XUL menu is opened and when the all XUL menus are
michael@0 1279 * closed. At opening, aInstalling should be TRUE, otherwise, it should be
michael@0 1280 * FALSE.
michael@0 1281 */
michael@0 1282 static void NotifyInstalledMenuKeyboardListener(bool aInstalling);
michael@0 1283
michael@0 1284 /**
michael@0 1285 * Do security checks before loading a resource. Does the following checks:
michael@0 1286 * nsIScriptSecurityManager::CheckLoadURIWithPrincipal
michael@0 1287 * NS_CheckContentLoadPolicy
michael@0 1288 * nsIScriptSecurityManager::CheckSameOriginURI
michael@0 1289 *
michael@0 1290 * You will still need to do at least SameOrigin checks before on redirects.
michael@0 1291 *
michael@0 1292 * @param aURIToLoad URI that is getting loaded.
michael@0 1293 * @param aLoadingPrincipal Principal of the resource that is initiating
michael@0 1294 * the load
michael@0 1295 * @param aCheckLoadFlags Flags to be passed to
michael@0 1296 * nsIScriptSecurityManager::CheckLoadURIWithPrincipal
michael@0 1297 * NOTE: If this contains ALLOW_CHROME the
michael@0 1298 * CheckSameOriginURI check will be skipped if
michael@0 1299 * aURIToLoad is a chrome uri.
michael@0 1300 * @param aAllowData Set to true to skip CheckSameOriginURI check when
michael@0 1301 aURIToLoad is a data uri.
michael@0 1302 * @param aContentPolicyType Type \
michael@0 1303 * @param aContext Context |- to be passed to
michael@0 1304 * @param aMimeGuess Mimetype | NS_CheckContentLoadPolicy
michael@0 1305 * @param aExtra Extra /
michael@0 1306 */
michael@0 1307 static nsresult CheckSecurityBeforeLoad(nsIURI* aURIToLoad,
michael@0 1308 nsIPrincipal* aLoadingPrincipal,
michael@0 1309 uint32_t aCheckLoadFlags,
michael@0 1310 bool aAllowData,
michael@0 1311 uint32_t aContentPolicyType,
michael@0 1312 nsISupports* aContext,
michael@0 1313 const nsAFlatCString& aMimeGuess = EmptyCString(),
michael@0 1314 nsISupports* aExtra = nullptr);
michael@0 1315
michael@0 1316 /**
michael@0 1317 * Returns true if aPrincipal is the system principal.
michael@0 1318 */
michael@0 1319 static bool IsSystemPrincipal(nsIPrincipal* aPrincipal);
michael@0 1320
michael@0 1321 /**
michael@0 1322 * Returns true if aPrincipal is an nsExpandedPrincipal.
michael@0 1323 */
michael@0 1324 static bool IsExpandedPrincipal(nsIPrincipal* aPrincipal);
michael@0 1325
michael@0 1326 /**
michael@0 1327 * Returns true if aPrincipal is the system or an nsExpandedPrincipal.
michael@0 1328 */
michael@0 1329 static bool IsSystemOrExpandedPrincipal(nsIPrincipal* aPrincipal)
michael@0 1330 {
michael@0 1331 return IsSystemPrincipal(aPrincipal) || IsExpandedPrincipal(aPrincipal);
michael@0 1332 }
michael@0 1333
michael@0 1334 /**
michael@0 1335 * Gets the system principal from the security manager.
michael@0 1336 */
michael@0 1337 static nsIPrincipal* GetSystemPrincipal();
michael@0 1338
michael@0 1339 /**
michael@0 1340 * *aResourcePrincipal is a principal describing who may access the contents
michael@0 1341 * of a resource. The resource can only be consumed by a principal that
michael@0 1342 * subsumes *aResourcePrincipal. MAKE SURE THAT NOTHING EVER ACTS WITH THE
michael@0 1343 * AUTHORITY OF *aResourcePrincipal.
michael@0 1344 * It may be null to indicate that the resource has no data from any origin
michael@0 1345 * in it yet and anything may access the resource.
michael@0 1346 * Additional data is being mixed into the resource from aExtraPrincipal
michael@0 1347 * (which may be null; if null, no data is being mixed in and this function
michael@0 1348 * will do nothing). Update *aResourcePrincipal to reflect the new data.
michael@0 1349 * If *aResourcePrincipal subsumes aExtraPrincipal, nothing needs to change,
michael@0 1350 * otherwise *aResourcePrincipal is replaced with the system principal.
michael@0 1351 * Returns true if *aResourcePrincipal changed.
michael@0 1352 */
michael@0 1353 static bool CombineResourcePrincipals(nsCOMPtr<nsIPrincipal>* aResourcePrincipal,
michael@0 1354 nsIPrincipal* aExtraPrincipal);
michael@0 1355
michael@0 1356 /**
michael@0 1357 * Trigger a link with uri aLinkURI. If aClick is false, this triggers a
michael@0 1358 * mouseover on the link, otherwise it triggers a load after doing a
michael@0 1359 * security check using aContent's principal.
michael@0 1360 *
michael@0 1361 * @param aContent the node on which a link was triggered.
michael@0 1362 * @param aPresContext the pres context, must be non-null.
michael@0 1363 * @param aLinkURI the URI of the link, must be non-null.
michael@0 1364 * @param aTargetSpec the target (like target=, may be empty).
michael@0 1365 * @param aClick whether this was a click or not (if false, this method
michael@0 1366 * assumes you just hovered over the link).
michael@0 1367 * @param aIsUserTriggered whether the user triggered the link. This would be
michael@0 1368 * false for loads from auto XLinks or from the
michael@0 1369 * click() method if we ever implement it.
michael@0 1370 * @param aIsTrusted If false, JS Context will be pushed to stack
michael@0 1371 * when the link is triggered.
michael@0 1372 */
michael@0 1373 static void TriggerLink(nsIContent *aContent, nsPresContext *aPresContext,
michael@0 1374 nsIURI *aLinkURI, const nsString& aTargetSpec,
michael@0 1375 bool aClick, bool aIsUserTriggered,
michael@0 1376 bool aIsTrusted);
michael@0 1377
michael@0 1378 /**
michael@0 1379 * Get the link location.
michael@0 1380 */
michael@0 1381 static void GetLinkLocation(mozilla::dom::Element* aElement,
michael@0 1382 nsString& aLocationString);
michael@0 1383
michael@0 1384 /**
michael@0 1385 * Return top-level widget in the parent chain.
michael@0 1386 */
michael@0 1387 static nsIWidget* GetTopLevelWidget(nsIWidget* aWidget);
michael@0 1388
michael@0 1389 /**
michael@0 1390 * Return the localized ellipsis for UI.
michael@0 1391 */
michael@0 1392 static const nsDependentString GetLocalizedEllipsis();
michael@0 1393
michael@0 1394 /**
michael@0 1395 * Get the candidates for accelkeys for aDOMKeyEvent.
michael@0 1396 *
michael@0 1397 * @param aDOMKeyEvent [in] the key event for accelkey handling.
michael@0 1398 * @param aCandidates [out] the candidate shortcut key combination list.
michael@0 1399 * the first item is most preferred.
michael@0 1400 */
michael@0 1401 static void GetAccelKeyCandidates(nsIDOMKeyEvent* aDOMKeyEvent,
michael@0 1402 nsTArray<nsShortcutCandidate>& aCandidates);
michael@0 1403
michael@0 1404 /**
michael@0 1405 * Get the candidates for accesskeys for aNativeKeyEvent.
michael@0 1406 *
michael@0 1407 * @param aNativeKeyEvent [in] the key event for accesskey handling.
michael@0 1408 * @param aCandidates [out] the candidate access key list.
michael@0 1409 * the first item is most preferred.
michael@0 1410 */
michael@0 1411 static void GetAccessKeyCandidates(
michael@0 1412 mozilla::WidgetKeyboardEvent* aNativeKeyEvent,
michael@0 1413 nsTArray<uint32_t>& aCandidates);
michael@0 1414
michael@0 1415 /**
michael@0 1416 * Hide any XUL popups associated with aDocument, including any documents
michael@0 1417 * displayed in child frames. Does nothing if aDocument is null.
michael@0 1418 */
michael@0 1419 static void HidePopupsInDocument(nsIDocument* aDocument);
michael@0 1420
michael@0 1421 /**
michael@0 1422 * Retrieve the current drag session, or null if no drag is currently occuring
michael@0 1423 */
michael@0 1424 static already_AddRefed<nsIDragSession> GetDragSession();
michael@0 1425
michael@0 1426 /*
michael@0 1427 * Initialize and set the dataTransfer field of an WidgetDragEvent.
michael@0 1428 */
michael@0 1429 static nsresult SetDataTransferInEvent(mozilla::WidgetDragEvent* aDragEvent);
michael@0 1430
michael@0 1431 // filters the drag and drop action to fit within the effects allowed and
michael@0 1432 // returns it.
michael@0 1433 static uint32_t FilterDropEffect(uint32_t aAction, uint32_t aEffectAllowed);
michael@0 1434
michael@0 1435 /*
michael@0 1436 * Return true if the target of a drop event is a content document that is
michael@0 1437 * an ancestor of the document for the source of the drag.
michael@0 1438 */
michael@0 1439 static bool CheckForSubFrameDrop(nsIDragSession* aDragSession,
michael@0 1440 mozilla::WidgetDragEvent* aDropEvent);
michael@0 1441
michael@0 1442 /**
michael@0 1443 * Return true if aURI is a local file URI (i.e. file://).
michael@0 1444 */
michael@0 1445 static bool URIIsLocalFile(nsIURI *aURI);
michael@0 1446
michael@0 1447 /**
michael@0 1448 * Given a URI, return set beforeHash to the part before the '#', and
michael@0 1449 * afterHash to the remainder of the URI, including the '#'.
michael@0 1450 */
michael@0 1451 static nsresult SplitURIAtHash(nsIURI *aURI,
michael@0 1452 nsACString &aBeforeHash,
michael@0 1453 nsACString &aAfterHash);
michael@0 1454
michael@0 1455 /**
michael@0 1456 * Get the application manifest URI for this document. The manifest URI
michael@0 1457 * is specified in the manifest= attribute of the root element of the
michael@0 1458 * document.
michael@0 1459 *
michael@0 1460 * @param aDocument The document that lists the manifest.
michael@0 1461 * @param aURI The manifest URI.
michael@0 1462 */
michael@0 1463 static void GetOfflineAppManifest(nsIDocument *aDocument, nsIURI **aURI);
michael@0 1464
michael@0 1465 /**
michael@0 1466 * Check whether an application should be allowed to use offline APIs.
michael@0 1467 */
michael@0 1468 static bool OfflineAppAllowed(nsIURI *aURI);
michael@0 1469
michael@0 1470 /**
michael@0 1471 * Check whether an application should be allowed to use offline APIs.
michael@0 1472 */
michael@0 1473 static bool OfflineAppAllowed(nsIPrincipal *aPrincipal);
michael@0 1474
michael@0 1475 /**
michael@0 1476 * If offline-apps.allow_by_default is true, we set offline-app permission
michael@0 1477 * for the principal and return true. Otherwise false.
michael@0 1478 */
michael@0 1479 static bool MaybeAllowOfflineAppByDefault(nsIPrincipal *aPrincipal, nsIDOMWindow *aWindow);
michael@0 1480
michael@0 1481 /**
michael@0 1482 * Increases the count of blockers preventing scripts from running.
michael@0 1483 * NOTE: You might want to use nsAutoScriptBlocker rather than calling
michael@0 1484 * this directly
michael@0 1485 */
michael@0 1486 static void AddScriptBlocker();
michael@0 1487
michael@0 1488 /**
michael@0 1489 * Decreases the count of blockers preventing scripts from running.
michael@0 1490 * NOTE: You might want to use nsAutoScriptBlocker rather than calling
michael@0 1491 * this directly
michael@0 1492 *
michael@0 1493 * WARNING! Calling this function could synchronously execute scripts.
michael@0 1494 */
michael@0 1495 static void RemoveScriptBlocker();
michael@0 1496
michael@0 1497 /**
michael@0 1498 * Add a runnable that is to be executed as soon as it's safe to execute
michael@0 1499 * scripts.
michael@0 1500 * NOTE: If it's currently safe to execute scripts, aRunnable will be run
michael@0 1501 * synchronously before the function returns.
michael@0 1502 *
michael@0 1503 * @param aRunnable The nsIRunnable to run as soon as it's safe to execute
michael@0 1504 * scripts. Passing null is allowed and results in nothing
michael@0 1505 * happening. It is also allowed to pass an object that
michael@0 1506 * has not yet been AddRefed.
michael@0 1507 * @return false on out of memory, true otherwise.
michael@0 1508 */
michael@0 1509 static bool AddScriptRunner(nsIRunnable* aRunnable);
michael@0 1510
michael@0 1511 /**
michael@0 1512 * Returns true if it's safe to execute content script and false otherwise.
michael@0 1513 *
michael@0 1514 * The only known case where this lies is mutation events. They run, and can
michael@0 1515 * run anything else, when this function returns false, but this is ok.
michael@0 1516 */
michael@0 1517 static bool IsSafeToRunScript() {
michael@0 1518 return sScriptBlockerCount == 0;
michael@0 1519 }
michael@0 1520
michael@0 1521 /**
michael@0 1522 * Retrieve information about the viewport as a data structure.
michael@0 1523 * This will return information in the viewport META data section
michael@0 1524 * of the document. This can be used in lieu of ProcessViewportInfo(),
michael@0 1525 * which places the viewport information in the document header instead
michael@0 1526 * of returning it directly.
michael@0 1527 *
michael@0 1528 * @param aDisplayWidth width of the on-screen display area for this
michael@0 1529 * document, in device pixels.
michael@0 1530 * @param aDisplayHeight height of the on-screen display area for this
michael@0 1531 * document, in device pixels.
michael@0 1532 *
michael@0 1533 * NOTE: If the site is optimized for mobile (via the doctype), this
michael@0 1534 * will return viewport information that specifies default information.
michael@0 1535 */
michael@0 1536 static nsViewportInfo GetViewportInfo(nsIDocument* aDocument,
michael@0 1537 const mozilla::ScreenIntSize& aDisplaySize);
michael@0 1538
michael@0 1539 // Call EnterMicroTask when you're entering JS execution.
michael@0 1540 // Usually the best way to do this is to use nsAutoMicroTask.
michael@0 1541 static void EnterMicroTask();
michael@0 1542 static void LeaveMicroTask();
michael@0 1543
michael@0 1544 static bool IsInMicroTask();
michael@0 1545 static uint32_t MicroTaskLevel();
michael@0 1546 static void SetMicroTaskLevel(uint32_t aLevel);
michael@0 1547
michael@0 1548 /* Process viewport META data. This gives us information for the scale
michael@0 1549 * and zoom of a page on mobile devices. We stick the information in
michael@0 1550 * the document header and use it later on after rendering.
michael@0 1551 *
michael@0 1552 * See Bug #436083
michael@0 1553 */
michael@0 1554 static nsresult ProcessViewportInfo(nsIDocument *aDocument,
michael@0 1555 const nsAString &viewportInfo);
michael@0 1556
michael@0 1557 static nsIScriptContext* GetContextForEventHandlers(nsINode* aNode,
michael@0 1558 nsresult* aRv);
michael@0 1559
michael@0 1560 static JSContext *GetCurrentJSContext();
michael@0 1561 static JSContext *GetSafeJSContext();
michael@0 1562 static JSContext *GetCurrentJSContextForThread();
michael@0 1563 static JSContext *GetDefaultJSContextForThread();
michael@0 1564
michael@0 1565 /**
michael@0 1566 * Case insensitive comparison between two strings. However it only ignores
michael@0 1567 * case for ASCII characters a-z.
michael@0 1568 */
michael@0 1569 static bool EqualsIgnoreASCIICase(const nsAString& aStr1,
michael@0 1570 const nsAString& aStr2);
michael@0 1571
michael@0 1572 /**
michael@0 1573 * Convert ASCII A-Z to a-z.
michael@0 1574 * @return NS_OK on success, or NS_ERROR_OUT_OF_MEMORY if making the string
michael@0 1575 * writable needs to allocate memory and that allocation fails.
michael@0 1576 */
michael@0 1577 static nsresult ASCIIToLower(nsAString& aStr);
michael@0 1578 static nsresult ASCIIToLower(const nsAString& aSource, nsAString& aDest);
michael@0 1579
michael@0 1580 /**
michael@0 1581 * Convert ASCII a-z to A-Z.
michael@0 1582 * @return NS_OK on success, or NS_ERROR_OUT_OF_MEMORY if making the string
michael@0 1583 * writable needs to allocate memory and that allocation fails.
michael@0 1584 */
michael@0 1585 static nsresult ASCIIToUpper(nsAString& aStr);
michael@0 1586 static nsresult ASCIIToUpper(const nsAString& aSource, nsAString& aDest);
michael@0 1587
michael@0 1588 /**
michael@0 1589 * Return whether aStr contains an ASCII uppercase character.
michael@0 1590 */
michael@0 1591 static bool StringContainsASCIIUpper(const nsAString& aStr);
michael@0 1592
michael@0 1593 // Returns NS_OK for same origin, error (NS_ERROR_DOM_BAD_URI) if not.
michael@0 1594 static nsresult CheckSameOrigin(nsIChannel *aOldChannel, nsIChannel *aNewChannel);
michael@0 1595 static nsIInterfaceRequestor* GetSameOriginChecker();
michael@0 1596
michael@0 1597 // Trace the safe JS context.
michael@0 1598 static void TraceSafeJSContext(JSTracer* aTrc);
michael@0 1599
michael@0 1600
michael@0 1601 /**
michael@0 1602 * Get the Origin of the passed in nsIPrincipal or nsIURI. If the passed in
michael@0 1603 * nsIURI or the URI of the passed in nsIPrincipal does not have a host, the
michael@0 1604 * origin is set to 'null'.
michael@0 1605 *
michael@0 1606 * The ASCII versions return a ASCII strings that are puny-code encoded,
michael@0 1607 * suitable for, for example, header values. The UTF versions return strings
michael@0 1608 * containing international characters.
michael@0 1609 *
michael@0 1610 * @pre aPrincipal/aOrigin must not be null.
michael@0 1611 *
michael@0 1612 * @note this should be used for HTML5 origin determination.
michael@0 1613 */
michael@0 1614 static nsresult GetASCIIOrigin(nsIPrincipal* aPrincipal,
michael@0 1615 nsCString& aOrigin);
michael@0 1616 static nsresult GetASCIIOrigin(nsIURI* aURI, nsCString& aOrigin);
michael@0 1617 static nsresult GetUTFOrigin(nsIPrincipal* aPrincipal,
michael@0 1618 nsString& aOrigin);
michael@0 1619 static nsresult GetUTFOrigin(nsIURI* aURI, nsString& aOrigin);
michael@0 1620 static void GetUTFNonNullOrigin(nsIURI* aURI, nsString& aOrigin);
michael@0 1621
michael@0 1622 /**
michael@0 1623 * This method creates and dispatches "command" event, which implements
michael@0 1624 * nsIDOMXULCommandEvent.
michael@0 1625 * If aShell is not null, dispatching goes via
michael@0 1626 * nsIPresShell::HandleDOMEventWithTarget.
michael@0 1627 */
michael@0 1628 static nsresult DispatchXULCommand(nsIContent* aTarget,
michael@0 1629 bool aTrusted,
michael@0 1630 nsIDOMEvent* aSourceEvent = nullptr,
michael@0 1631 nsIPresShell* aShell = nullptr,
michael@0 1632 bool aCtrl = false,
michael@0 1633 bool aAlt = false,
michael@0 1634 bool aShift = false,
michael@0 1635 bool aMeta = false);
michael@0 1636
michael@0 1637 /**
michael@0 1638 * Gets the nsIDocument given the script context. Will return nullptr on failure.
michael@0 1639 *
michael@0 1640 * @param aScriptContext the script context to get the document for; can be null
michael@0 1641 *
michael@0 1642 * @return the document associated with the script context
michael@0 1643 */
michael@0 1644 static nsIDocument*
michael@0 1645 GetDocumentFromScriptContext(nsIScriptContext* aScriptContext);
michael@0 1646
michael@0 1647 static bool CheckMayLoad(nsIPrincipal* aPrincipal, nsIChannel* aChannel, bool aAllowIfInheritsPrincipal);
michael@0 1648
michael@0 1649 /**
michael@0 1650 * The method checks whether the caller can access native anonymous content.
michael@0 1651 * If there is no JS in the stack or privileged JS is running, this
michael@0 1652 * method returns true, otherwise false.
michael@0 1653 */
michael@0 1654 static bool CanAccessNativeAnon();
michael@0 1655
michael@0 1656 MOZ_WARN_UNUSED_RESULT
michael@0 1657 static nsresult WrapNative(JSContext *cx, nsISupports *native,
michael@0 1658 const nsIID* aIID, JS::MutableHandle<JS::Value> vp,
michael@0 1659 bool aAllowWrapping = true)
michael@0 1660 {
michael@0 1661 return WrapNative(cx, native, nullptr, aIID, vp, aAllowWrapping);
michael@0 1662 }
michael@0 1663
michael@0 1664 // Same as the WrapNative above, but use this one if aIID is nsISupports' IID.
michael@0 1665 MOZ_WARN_UNUSED_RESULT
michael@0 1666 static nsresult WrapNative(JSContext *cx, nsISupports *native,
michael@0 1667 JS::MutableHandle<JS::Value> vp,
michael@0 1668 bool aAllowWrapping = true)
michael@0 1669 {
michael@0 1670 return WrapNative(cx, native, nullptr, nullptr, vp, aAllowWrapping);
michael@0 1671 }
michael@0 1672
michael@0 1673 MOZ_WARN_UNUSED_RESULT
michael@0 1674 static nsresult WrapNative(JSContext *cx, nsISupports *native,
michael@0 1675 nsWrapperCache *cache,
michael@0 1676 JS::MutableHandle<JS::Value> vp,
michael@0 1677 bool aAllowWrapping = true)
michael@0 1678 {
michael@0 1679 return WrapNative(cx, native, cache, nullptr, vp, aAllowWrapping);
michael@0 1680 }
michael@0 1681
michael@0 1682 /**
michael@0 1683 * Creates an arraybuffer from a binary string.
michael@0 1684 */
michael@0 1685 static nsresult CreateArrayBuffer(JSContext *aCx, const nsACString& aData,
michael@0 1686 JSObject** aResult);
michael@0 1687
michael@0 1688 static nsresult CreateBlobBuffer(JSContext* aCx,
michael@0 1689 const nsACString& aData,
michael@0 1690 JS::MutableHandle<JS::Value> aBlob);
michael@0 1691
michael@0 1692 static void StripNullChars(const nsAString& aInStr, nsAString& aOutStr);
michael@0 1693
michael@0 1694 /**
michael@0 1695 * Strip all \n, \r and nulls from the given string
michael@0 1696 * @param aString the string to remove newlines from [in/out]
michael@0 1697 */
michael@0 1698 static void RemoveNewlines(nsString &aString);
michael@0 1699
michael@0 1700 /**
michael@0 1701 * Convert Windows and Mac platform linebreaks to \n.
michael@0 1702 * @param aString the string to convert the newlines inside [in/out]
michael@0 1703 */
michael@0 1704 static void PlatformToDOMLineBreaks(nsString &aString);
michael@0 1705
michael@0 1706 /**
michael@0 1707 * Populates aResultString with the contents of the string-buffer aBuf, up
michael@0 1708 * to aBuf's null-terminator. aBuf must not be null. Ownership of the string
michael@0 1709 * is not transferred.
michael@0 1710 */
michael@0 1711 static void PopulateStringFromStringBuffer(nsStringBuffer* aBuf,
michael@0 1712 nsAString& aResultString);
michael@0 1713
michael@0 1714 static bool IsHandlingKeyBoardEvent()
michael@0 1715 {
michael@0 1716 return sIsHandlingKeyBoardEvent;
michael@0 1717 }
michael@0 1718
michael@0 1719 static void SetIsHandlingKeyBoardEvent(bool aHandling)
michael@0 1720 {
michael@0 1721 sIsHandlingKeyBoardEvent = aHandling;
michael@0 1722 }
michael@0 1723
michael@0 1724 /**
michael@0 1725 * Utility method for getElementsByClassName. aRootNode is the node (either
michael@0 1726 * document or element), which getElementsByClassName was called on.
michael@0 1727 */
michael@0 1728 static already_AddRefed<nsContentList>
michael@0 1729 GetElementsByClassName(nsINode* aRootNode, const nsAString& aClasses)
michael@0 1730 {
michael@0 1731 NS_PRECONDITION(aRootNode, "Must have root node");
michael@0 1732
michael@0 1733 return NS_GetFuncStringHTMLCollection(aRootNode, MatchClassNames,
michael@0 1734 DestroyClassNameArray,
michael@0 1735 AllocClassMatchingInfo,
michael@0 1736 aClasses);
michael@0 1737 }
michael@0 1738
michael@0 1739 /**
michael@0 1740 * Returns a presshell for this document, if there is one. This will be
michael@0 1741 * aDoc's direct presshell if there is one, otherwise we'll look at all
michael@0 1742 * ancestor documents to try to find a presshell, so for example this can
michael@0 1743 * still find a presshell for documents in display:none frames that have
michael@0 1744 * no presentation. So you have to be careful how you use this presshell ---
michael@0 1745 * getting generic data like a device context or widget from it is OK, but it
michael@0 1746 * might not be this document's actual presentation.
michael@0 1747 */
michael@0 1748 static nsIPresShell* FindPresShellForDocument(const nsIDocument* aDoc);
michael@0 1749
michael@0 1750 /**
michael@0 1751 * Returns the widget for this document if there is one. Looks at all ancestor
michael@0 1752 * documents to try to find a widget, so for example this can still find a
michael@0 1753 * widget for documents in display:none frames that have no presentation.
michael@0 1754 */
michael@0 1755 static nsIWidget* WidgetForDocument(const nsIDocument* aDoc);
michael@0 1756
michael@0 1757 /**
michael@0 1758 * Returns a layer manager to use for the given document. Basically we
michael@0 1759 * look up the document hierarchy for the first document which has
michael@0 1760 * a presentation with an associated widget, and use that widget's
michael@0 1761 * layer manager.
michael@0 1762 *
michael@0 1763 * @param aDoc the document for which to return a layer manager.
michael@0 1764 * @param aAllowRetaining an outparam that states whether the returned
michael@0 1765 * layer manager should be used for retained layers
michael@0 1766 */
michael@0 1767 static already_AddRefed<mozilla::layers::LayerManager>
michael@0 1768 LayerManagerForDocument(const nsIDocument *aDoc, bool *aAllowRetaining = nullptr);
michael@0 1769
michael@0 1770 /**
michael@0 1771 * Returns a layer manager to use for the given document. Basically we
michael@0 1772 * look up the document hierarchy for the first document which has
michael@0 1773 * a presentation with an associated widget, and use that widget's
michael@0 1774 * layer manager. In addition to the normal layer manager lookup this will
michael@0 1775 * specifically request a persistent layer manager. This means that the layer
michael@0 1776 * manager is expected to remain the layer manager for the document in the
michael@0 1777 * forseeable future. This function should be used carefully as it may change
michael@0 1778 * the document's layer manager.
michael@0 1779 *
michael@0 1780 * @param aDoc the document for which to return a layer manager.
michael@0 1781 * @param aAllowRetaining an outparam that states whether the returned
michael@0 1782 * layer manager should be used for retained layers
michael@0 1783 */
michael@0 1784 static already_AddRefed<mozilla::layers::LayerManager>
michael@0 1785 PersistentLayerManagerForDocument(nsIDocument *aDoc, bool *aAllowRetaining = nullptr);
michael@0 1786
michael@0 1787 /**
michael@0 1788 * Determine whether a content node is focused or not,
michael@0 1789 *
michael@0 1790 * @param aContent the content node to check
michael@0 1791 * @return true if the content node is focused, false otherwise.
michael@0 1792 */
michael@0 1793 static bool IsFocusedContent(const nsIContent *aContent);
michael@0 1794
michael@0 1795 /**
michael@0 1796 * Returns true if the DOM full-screen API is enabled.
michael@0 1797 */
michael@0 1798 static bool IsFullScreenApiEnabled();
michael@0 1799
michael@0 1800 /**
michael@0 1801 * Returns true if requests for full-screen are allowed in the current
michael@0 1802 * context. Requests are only allowed if the user initiated them (like with
michael@0 1803 * a mouse-click or key press), unless this check has been disabled by
michael@0 1804 * setting the pref "full-screen-api.allow-trusted-requests-only" to false.
michael@0 1805 */
michael@0 1806 static bool IsRequestFullScreenAllowed();
michael@0 1807
michael@0 1808 /**
michael@0 1809 * Returns true if the DOM fullscreen API is restricted to content only.
michael@0 1810 * This mirrors the pref "full-screen-api.content-only". If this is true,
michael@0 1811 * fullscreen requests in chrome are denied, and fullscreen requests in
michael@0 1812 * content stop percolating upwards before they reach chrome documents.
michael@0 1813 * That is, when an element in content requests fullscreen, only its
michael@0 1814 * containing frames that are in content are also made fullscreen, not
michael@0 1815 * the containing frame in the chrome document.
michael@0 1816 *
michael@0 1817 * Note if the fullscreen API is running in content only mode then multiple
michael@0 1818 * branches of a doctree can be fullscreen at the same time, but no fullscreen
michael@0 1819 * document will have a common ancestor with another fullscreen document
michael@0 1820 * that is also fullscreen (since the only common ancestor they can have
michael@0 1821 * is the chrome document, and that can't be fullscreen). i.e. multiple
michael@0 1822 * child documents of the chrome document can be fullscreen, but the chrome
michael@0 1823 * document won't be fullscreen.
michael@0 1824 *
michael@0 1825 * Making the fullscreen API content only is useful on platforms where we
michael@0 1826 * still want chrome to be visible or accessible while content is
michael@0 1827 * fullscreen, like on Windows 8 in Metro mode.
michael@0 1828 *
michael@0 1829 * Note that if the fullscreen API is content only, chrome can still go
michael@0 1830 * fullscreen by setting the "fullScreen" attribute on its XUL window.
michael@0 1831 */
michael@0 1832 static bool IsFullscreenApiContentOnly();
michael@0 1833
michael@0 1834 /**
michael@0 1835 * Returns true if the idle observers API is enabled.
michael@0 1836 */
michael@0 1837 static bool IsIdleObserverAPIEnabled() { return sIsIdleObserverAPIEnabled; }
michael@0 1838
michael@0 1839 /*
michael@0 1840 * Returns true if the performance timing APIs are enabled.
michael@0 1841 */
michael@0 1842 static bool IsPerformanceTimingEnabled()
michael@0 1843 {
michael@0 1844 return sIsPerformanceTimingEnabled;
michael@0 1845 }
michael@0 1846
michael@0 1847 /*
michael@0 1848 * Returns true if the performance timing APIs are enabled.
michael@0 1849 */
michael@0 1850 static bool IsResourceTimingEnabled()
michael@0 1851 {
michael@0 1852 return sIsResourceTimingEnabled;
michael@0 1853 }
michael@0 1854
michael@0 1855 /**
michael@0 1856 * Returns true if the doc tree branch which contains aDoc contains any
michael@0 1857 * plugins which we don't control event dispatch for, i.e. do any plugins
michael@0 1858 * in the same tab as this document receive key events outside of our
michael@0 1859 * control? This always returns false on MacOSX.
michael@0 1860 */
michael@0 1861 static bool HasPluginWithUncontrolledEventDispatch(nsIDocument* aDoc);
michael@0 1862
michael@0 1863 /**
michael@0 1864 * Fire mutation events for changes caused by parsing directly into a
michael@0 1865 * context node.
michael@0 1866 *
michael@0 1867 * @param aDoc the document of the node
michael@0 1868 * @param aDest the destination node that got stuff appended to it
michael@0 1869 * @param aOldChildCount the number of children the node had before parsing
michael@0 1870 */
michael@0 1871 static void FireMutationEventsForDirectParsing(nsIDocument* aDoc,
michael@0 1872 nsIContent* aDest,
michael@0 1873 int32_t aOldChildCount);
michael@0 1874
michael@0 1875 /**
michael@0 1876 * Returns true if the content is in a document and contains a plugin
michael@0 1877 * which we don't control event dispatch for, i.e. do any plugins in this
michael@0 1878 * doc tree receive key events outside of our control? This always returns
michael@0 1879 * false on MacOSX.
michael@0 1880 */
michael@0 1881 static bool HasPluginWithUncontrolledEventDispatch(nsIContent* aContent);
michael@0 1882
michael@0 1883 /**
michael@0 1884 * Returns the document that is the closest ancestor to aDoc that is
michael@0 1885 * fullscreen. If aDoc is fullscreen this returns aDoc. If aDoc is not
michael@0 1886 * fullscreen and none of aDoc's ancestors are fullscreen this returns
michael@0 1887 * nullptr.
michael@0 1888 */
michael@0 1889 static nsIDocument* GetFullscreenAncestor(nsIDocument* aDoc);
michael@0 1890
michael@0 1891 /**
michael@0 1892 * Returns true if aWin and the current pointer lock document
michael@0 1893 * have common scriptable top window.
michael@0 1894 */
michael@0 1895 static bool IsInPointerLockContext(nsIDOMWindow* aWin);
michael@0 1896
michael@0 1897 /**
michael@0 1898 * Returns the time limit on handling user input before
michael@0 1899 * EventStateManager::IsHandlingUserInput() stops returning true.
michael@0 1900 * This enables us to detect long running user-generated event handlers.
michael@0 1901 */
michael@0 1902 static TimeDuration HandlingUserInputTimeout();
michael@0 1903
michael@0 1904 static void GetShiftText(nsAString& text);
michael@0 1905 static void GetControlText(nsAString& text);
michael@0 1906 static void GetMetaText(nsAString& text);
michael@0 1907 static void GetOSText(nsAString& text);
michael@0 1908 static void GetAltText(nsAString& text);
michael@0 1909 static void GetModifierSeparatorText(nsAString& text);
michael@0 1910
michael@0 1911 /**
michael@0 1912 * Returns if aContent has a tabbable subdocument.
michael@0 1913 * A sub document isn't tabbable when it's a zombie document.
michael@0 1914 *
michael@0 1915 * @param aElement element to test.
michael@0 1916 *
michael@0 1917 * @return Whether the subdocument is tabbable.
michael@0 1918 */
michael@0 1919 static bool IsSubDocumentTabbable(nsIContent* aContent);
michael@0 1920
michael@0 1921 /**
michael@0 1922 * Returns if aNode ignores user focus.
michael@0 1923 *
michael@0 1924 * @param aNode node to test
michael@0 1925 *
michael@0 1926 * @return Whether the node ignores user focus.
michael@0 1927 */
michael@0 1928 static bool IsUserFocusIgnored(nsINode* aNode);
michael@0 1929
michael@0 1930 /**
michael@0 1931 * Returns if aContent has the 'scrollgrab' property.
michael@0 1932 * aContent may be null (in this case false is returned).
michael@0 1933 */
michael@0 1934 static bool HasScrollgrab(nsIContent* aContent);
michael@0 1935
michael@0 1936 /**
michael@0 1937 * Flushes the layout tree (recursively)
michael@0 1938 *
michael@0 1939 * @param aWindow the window the flush should start at
michael@0 1940 *
michael@0 1941 */
michael@0 1942 static void FlushLayoutForTree(nsIDOMWindow* aWindow);
michael@0 1943
michael@0 1944 /**
michael@0 1945 * Returns true if content with the given principal is allowed to use XUL
michael@0 1946 * and XBL and false otherwise.
michael@0 1947 */
michael@0 1948 static bool AllowXULXBLForPrincipal(nsIPrincipal* aPrincipal);
michael@0 1949
michael@0 1950 /**
michael@0 1951 * Perform cleanup that's appropriate for XPCOM shutdown.
michael@0 1952 */
michael@0 1953 static void XPCOMShutdown();
michael@0 1954
michael@0 1955 enum ContentViewerType
michael@0 1956 {
michael@0 1957 TYPE_UNSUPPORTED,
michael@0 1958 TYPE_CONTENT,
michael@0 1959 TYPE_PLUGIN,
michael@0 1960 TYPE_UNKNOWN
michael@0 1961 };
michael@0 1962
michael@0 1963 static already_AddRefed<nsIDocumentLoaderFactory>
michael@0 1964 FindInternalContentViewer(const char* aType,
michael@0 1965 ContentViewerType* aLoaderType = nullptr);
michael@0 1966
michael@0 1967 /**
michael@0 1968 * This helper method returns true if the aPattern pattern matches aValue.
michael@0 1969 * aPattern should not contain leading and trailing slashes (/).
michael@0 1970 * The pattern has to match the entire value not just a subset.
michael@0 1971 * aDocument must be a valid pointer (not null).
michael@0 1972 *
michael@0 1973 * This is following the HTML5 specification:
michael@0 1974 * http://dev.w3.org/html5/spec/forms.html#attr-input-pattern
michael@0 1975 *
michael@0 1976 * WARNING: This method mutates aPattern and aValue!
michael@0 1977 *
michael@0 1978 * @param aValue the string to check.
michael@0 1979 * @param aPattern the string defining the pattern.
michael@0 1980 * @param aDocument the owner document of the element.
michael@0 1981 * @result whether the given string is matches the pattern.
michael@0 1982 */
michael@0 1983 static bool IsPatternMatching(nsAString& aValue, nsAString& aPattern,
michael@0 1984 nsIDocument* aDocument);
michael@0 1985
michael@0 1986 /**
michael@0 1987 * Calling this adds support for
michael@0 1988 * ontouch* event handler DOM attributes.
michael@0 1989 */
michael@0 1990 static void InitializeTouchEventTable();
michael@0 1991
michael@0 1992 /**
michael@0 1993 * Test whether the given URI always inherits a security context
michael@0 1994 * from the document it comes from.
michael@0 1995 */
michael@0 1996 static nsresult URIInheritsSecurityContext(nsIURI *aURI, bool *aResult);
michael@0 1997
michael@0 1998 /**
michael@0 1999 * Set the given principal as the owner of the given channel, if
michael@0 2000 * needed. aURI must be the URI of aChannel. aPrincipal may be
michael@0 2001 * null. If aSetUpForAboutBlank is true, then about:blank will get
michael@0 2002 * the principal set up on it. If aForceOwner is true, the owner
michael@0 2003 * will be set on the channel, even if the principal can be determined
michael@0 2004 * from the channel.
michael@0 2005 * The return value is whether the principal was set up as the owner
michael@0 2006 * of the channel.
michael@0 2007 */
michael@0 2008 static bool SetUpChannelOwner(nsIPrincipal* aLoadingPrincipal,
michael@0 2009 nsIChannel* aChannel,
michael@0 2010 nsIURI* aURI,
michael@0 2011 bool aSetUpForAboutBlank,
michael@0 2012 bool aForceOwner = false);
michael@0 2013
michael@0 2014 static nsresult Btoa(const nsAString& aBinaryData,
michael@0 2015 nsAString& aAsciiBase64String);
michael@0 2016
michael@0 2017 static nsresult Atob(const nsAString& aAsciiString,
michael@0 2018 nsAString& aBinaryData);
michael@0 2019
michael@0 2020 /**
michael@0 2021 * Returns whether the input element passed in parameter has the autocomplete
michael@0 2022 * functionality enabled. It is taking into account the form owner.
michael@0 2023 * NOTE: the caller has to make sure autocomplete makes sense for the
michael@0 2024 * element's type.
michael@0 2025 *
michael@0 2026 * @param aInput the input element to check. NOTE: aInput can't be null.
michael@0 2027 * @return whether the input element has autocomplete enabled.
michael@0 2028 */
michael@0 2029 static bool IsAutocompleteEnabled(nsIDOMHTMLInputElement* aInput);
michael@0 2030
michael@0 2031 /**
michael@0 2032 * This will parse aSource, to extract the value of the pseudo attribute
michael@0 2033 * with the name specified in aName. See
michael@0 2034 * http://www.w3.org/TR/xml-stylesheet/#NT-StyleSheetPI for the specification
michael@0 2035 * which is used to parse aSource.
michael@0 2036 *
michael@0 2037 * @param aSource the string to parse
michael@0 2038 * @param aName the name of the attribute to get the value for
michael@0 2039 * @param aValue [out] the value for the attribute with name specified in
michael@0 2040 * aAttribute. Empty if the attribute isn't present.
michael@0 2041 * @return true if the attribute exists and was successfully parsed.
michael@0 2042 * false if the attribute doesn't exist, or has a malformed
michael@0 2043 * value, such as an unknown or unterminated entity.
michael@0 2044 */
michael@0 2045 static bool GetPseudoAttributeValue(const nsString& aSource, nsIAtom *aName,
michael@0 2046 nsAString& aValue);
michael@0 2047
michael@0 2048 /**
michael@0 2049 * Returns true if the language name is a version of JavaScript and
michael@0 2050 * false otherwise
michael@0 2051 */
michael@0 2052 static bool IsJavaScriptLanguage(const nsString& aName);
michael@0 2053
michael@0 2054 /**
michael@0 2055 * Returns the JSVersion for a string of the form '1.n', n = 0, ..., 8, and
michael@0 2056 * JSVERSION_UNKNOWN for other strings.
michael@0 2057 */
michael@0 2058 static JSVersion ParseJavascriptVersion(const nsAString& aVersionStr);
michael@0 2059
michael@0 2060 static bool IsJavascriptMIMEType(const nsAString& aMIMEType);
michael@0 2061
michael@0 2062 static void SplitMimeType(const nsAString& aValue, nsString& aType,
michael@0 2063 nsString& aParams);
michael@0 2064
michael@0 2065 /**
michael@0 2066 * Function checks if the user is idle.
michael@0 2067 *
michael@0 2068 * @param aRequestedIdleTimeInMS The idle observer's requested idle time.
michael@0 2069 * @param aUserIsIdle boolean indicating if the user
michael@0 2070 * is currently idle or not. *
michael@0 2071 * @return NS_OK NS_OK returned if the requested idle service and
michael@0 2072 * the current idle time were successfully obtained.
michael@0 2073 * NS_ERROR_FAILURE returned if the the requested
michael@0 2074 * idle service or the current idle were not obtained.
michael@0 2075 */
michael@0 2076 static nsresult IsUserIdle(uint32_t aRequestedIdleTimeInMS, bool* aUserIsIdle);
michael@0 2077
michael@0 2078 /**
michael@0 2079 * Takes a selection, and a text control element (<input> or <textarea>), and
michael@0 2080 * returns the offsets in the text content corresponding to the selection.
michael@0 2081 * The selection's anchor and focus must both be in the root node passed or a
michael@0 2082 * descendant.
michael@0 2083 *
michael@0 2084 * @param aSelection Selection to check
michael@0 2085 * @param aRoot Root <input> or <textarea> element
michael@0 2086 * @param aOutStartOffset Output start offset
michael@0 2087 * @param aOutEndOffset Output end offset
michael@0 2088 */
michael@0 2089 static void GetSelectionInTextControl(mozilla::dom::Selection* aSelection,
michael@0 2090 Element* aRoot,
michael@0 2091 int32_t& aOutStartOffset,
michael@0 2092 int32_t& aOutEndOffset);
michael@0 2093
michael@0 2094 /**
michael@0 2095 * Takes a frame for anonymous content within a text control (<input> or
michael@0 2096 * <textarea>), and returns an offset in the text content, adjusted for a
michael@0 2097 * trailing <br> frame.
michael@0 2098 *
michael@0 2099 * @param aOffsetFrame Frame for the text content in which the offset
michael@0 2100 * lies
michael@0 2101 * @param aOffset Offset as calculated by GetContentOffsetsFromPoint
michael@0 2102 * @param aOutOffset Output adjusted offset
michael@0 2103 *
michael@0 2104 * @see GetSelectionInTextControl for the original basis of this function.
michael@0 2105 */
michael@0 2106 static int32_t GetAdjustedOffsetInTextControl(nsIFrame* aOffsetFrame,
michael@0 2107 int32_t aOffset);
michael@0 2108
michael@0 2109 static nsIEditor* GetHTMLEditor(nsPresContext* aPresContext);
michael@0 2110
michael@0 2111 /**
michael@0 2112 * Check whether a spec feature/version is supported.
michael@0 2113 * @param aObject the object, which should support the feature,
michael@0 2114 * for example nsIDOMNode or nsIDOMDOMImplementation
michael@0 2115 * @param aFeature the feature ("Views", "Core", "HTML", "Range" ...)
michael@0 2116 * @param aVersion the version ("1.0", "2.0", ...)
michael@0 2117 * @return whether the feature is supported or not
michael@0 2118 */
michael@0 2119 static bool InternalIsSupported(nsISupports* aObject,
michael@0 2120 const nsAString& aFeature,
michael@0 2121 const nsAString& aVersion);
michael@0 2122
michael@0 2123 /**
michael@0 2124 * Return true if the browser.dom.window.dump.enabled pref is set.
michael@0 2125 */
michael@0 2126 static bool DOMWindowDumpEnabled();
michael@0 2127
michael@0 2128 /**
michael@0 2129 * Returns whether a content is an insertion point for XBL
michael@0 2130 * bindings or web components ShadowRoot. In web components,
michael@0 2131 * this corresponds to a <content> element that participates
michael@0 2132 * in node distribution. In XBL this corresponds to an
michael@0 2133 * <xbl:children> element in anonymous content.
michael@0 2134 *
michael@0 2135 * @param aContent The content to test for being an insertion point.
michael@0 2136 */
michael@0 2137 static bool IsContentInsertionPoint(const nsIContent* aContent);
michael@0 2138
michael@0 2139 private:
michael@0 2140 static bool InitializeEventTable();
michael@0 2141
michael@0 2142 static nsresult EnsureStringBundle(PropertiesFile aFile);
michael@0 2143
michael@0 2144 static bool CanCallerAccess(nsIPrincipal* aSubjectPrincipal,
michael@0 2145 nsIPrincipal* aPrincipal);
michael@0 2146
michael@0 2147 static nsresult WrapNative(JSContext *cx, nsISupports *native,
michael@0 2148 nsWrapperCache *cache, const nsIID* aIID,
michael@0 2149 JS::MutableHandle<JS::Value> vp,
michael@0 2150 bool aAllowWrapping);
michael@0 2151
michael@0 2152 static nsresult DispatchEvent(nsIDocument* aDoc,
michael@0 2153 nsISupports* aTarget,
michael@0 2154 const nsAString& aEventName,
michael@0 2155 bool aCanBubble,
michael@0 2156 bool aCancelable,
michael@0 2157 bool aTrusted,
michael@0 2158 bool *aDefaultAction = nullptr);
michael@0 2159
michael@0 2160 static void InitializeModifierStrings();
michael@0 2161
michael@0 2162 static void DropFragmentParsers();
michael@0 2163
michael@0 2164 static bool MatchClassNames(nsIContent* aContent, int32_t aNamespaceID,
michael@0 2165 nsIAtom* aAtom, void* aData);
michael@0 2166 static void DestroyClassNameArray(void* aData);
michael@0 2167 static void* AllocClassMatchingInfo(nsINode* aRootNode,
michael@0 2168 const nsString* aClasses);
michael@0 2169
michael@0 2170 static nsIXPConnect *sXPConnect;
michael@0 2171
michael@0 2172 static nsIScriptSecurityManager *sSecurityManager;
michael@0 2173
michael@0 2174 static nsIParserService *sParserService;
michael@0 2175
michael@0 2176 static nsNameSpaceManager *sNameSpaceManager;
michael@0 2177
michael@0 2178 static nsIIOService *sIOService;
michael@0 2179
michael@0 2180 static bool sImgLoaderInitialized;
michael@0 2181 static void InitImgLoader();
michael@0 2182
michael@0 2183 // The following four members are initialized lazily
michael@0 2184 static imgLoader* sImgLoader;
michael@0 2185 static imgLoader* sPrivateImgLoader;
michael@0 2186 static imgICache* sImgCache;
michael@0 2187 static imgICache* sPrivateImgCache;
michael@0 2188
michael@0 2189 static nsIConsoleService* sConsoleService;
michael@0 2190
michael@0 2191 static nsDataHashtable<nsISupportsHashKey, EventNameMapping>* sAtomEventTable;
michael@0 2192 static nsDataHashtable<nsStringHashKey, EventNameMapping>* sStringEventTable;
michael@0 2193 static nsCOMArray<nsIAtom>* sUserDefinedEvents;
michael@0 2194
michael@0 2195 static nsIStringBundleService* sStringBundleService;
michael@0 2196 static nsIStringBundle* sStringBundles[PropertiesFile_COUNT];
michael@0 2197
michael@0 2198 static nsIContentPolicy* sContentPolicyService;
michael@0 2199 static bool sTriedToGetContentPolicy;
michael@0 2200
michael@0 2201 static nsILineBreaker* sLineBreaker;
michael@0 2202 static nsIWordBreaker* sWordBreaker;
michael@0 2203
michael@0 2204 static nsIBidiKeyboard* sBidiKeyboard;
michael@0 2205
michael@0 2206 static bool sInitialized;
michael@0 2207 static uint32_t sScriptBlockerCount;
michael@0 2208 #ifdef DEBUG
michael@0 2209 static uint32_t sDOMNodeRemovedSuppressCount;
michael@0 2210 #endif
michael@0 2211 static uint32_t sMicroTaskLevel;
michael@0 2212 // Not an nsCOMArray because removing elements from those is slower
michael@0 2213 static nsTArray< nsCOMPtr<nsIRunnable> >* sBlockedScriptRunners;
michael@0 2214 static uint32_t sRunnersCountAtFirstBlocker;
michael@0 2215 static uint32_t sScriptBlockerCountWhereRunnersPrevented;
michael@0 2216
michael@0 2217 static nsIInterfaceRequestor* sSameOriginChecker;
michael@0 2218
michael@0 2219 static bool sIsHandlingKeyBoardEvent;
michael@0 2220 static bool sAllowXULXBL_for_file;
michael@0 2221 static bool sIsFullScreenApiEnabled;
michael@0 2222 static bool sTrustedFullScreenOnly;
michael@0 2223 static bool sFullscreenApiIsContentOnly;
michael@0 2224 static uint32_t sHandlingInputTimeout;
michael@0 2225 static bool sIsIdleObserverAPIEnabled;
michael@0 2226 static bool sIsPerformanceTimingEnabled;
michael@0 2227 static bool sIsResourceTimingEnabled;
michael@0 2228
michael@0 2229 static nsHtml5StringParser* sHTMLFragmentParser;
michael@0 2230 static nsIParser* sXMLFragmentParser;
michael@0 2231 static nsIFragmentContentSink* sXMLFragmentSink;
michael@0 2232
michael@0 2233 /**
michael@0 2234 * True if there's a fragment parser activation on the stack.
michael@0 2235 */
michael@0 2236 static bool sFragmentParsingActive;
michael@0 2237
michael@0 2238 static nsString* sShiftText;
michael@0 2239 static nsString* sControlText;
michael@0 2240 static nsString* sMetaText;
michael@0 2241 static nsString* sOSText;
michael@0 2242 static nsString* sAltText;
michael@0 2243 static nsString* sModifierSeparator;
michael@0 2244
michael@0 2245 #if !(defined(DEBUG) || defined(MOZ_ENABLE_JS_DUMP))
michael@0 2246 static bool sDOMWindowDumpEnabled;
michael@0 2247 #endif
michael@0 2248 };
michael@0 2249
michael@0 2250 class MOZ_STACK_CLASS nsAutoScriptBlocker {
michael@0 2251 public:
michael@0 2252 nsAutoScriptBlocker(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM) {
michael@0 2253 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
michael@0 2254 nsContentUtils::AddScriptBlocker();
michael@0 2255 }
michael@0 2256 ~nsAutoScriptBlocker() {
michael@0 2257 nsContentUtils::RemoveScriptBlocker();
michael@0 2258 }
michael@0 2259 private:
michael@0 2260 MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
michael@0 2261 };
michael@0 2262
michael@0 2263 class MOZ_STACK_CLASS nsAutoScriptBlockerSuppressNodeRemoved :
michael@0 2264 public nsAutoScriptBlocker {
michael@0 2265 public:
michael@0 2266 nsAutoScriptBlockerSuppressNodeRemoved() {
michael@0 2267 #ifdef DEBUG
michael@0 2268 ++nsContentUtils::sDOMNodeRemovedSuppressCount;
michael@0 2269 #endif
michael@0 2270 }
michael@0 2271 ~nsAutoScriptBlockerSuppressNodeRemoved() {
michael@0 2272 #ifdef DEBUG
michael@0 2273 --nsContentUtils::sDOMNodeRemovedSuppressCount;
michael@0 2274 #endif
michael@0 2275 }
michael@0 2276 };
michael@0 2277
michael@0 2278 class MOZ_STACK_CLASS nsAutoMicroTask
michael@0 2279 {
michael@0 2280 public:
michael@0 2281 nsAutoMicroTask()
michael@0 2282 {
michael@0 2283 nsContentUtils::EnterMicroTask();
michael@0 2284 }
michael@0 2285 ~nsAutoMicroTask()
michael@0 2286 {
michael@0 2287 nsContentUtils::LeaveMicroTask();
michael@0 2288 }
michael@0 2289 };
michael@0 2290
michael@0 2291 namespace mozilla {
michael@0 2292 namespace dom {
michael@0 2293
michael@0 2294 class TreeOrderComparator {
michael@0 2295 public:
michael@0 2296 bool Equals(nsINode* aElem1, nsINode* aElem2) const {
michael@0 2297 return aElem1 == aElem2;
michael@0 2298 }
michael@0 2299 bool LessThan(nsINode* aElem1, nsINode* aElem2) const {
michael@0 2300 return nsContentUtils::PositionIsBefore(aElem1, aElem2);
michael@0 2301 }
michael@0 2302 };
michael@0 2303
michael@0 2304 } // namespace dom
michael@0 2305 } // namespace mozilla
michael@0 2306
michael@0 2307 #define NS_INTERFACE_MAP_ENTRY_TEAROFF(_interface, _allocator) \
michael@0 2308 if (aIID.Equals(NS_GET_IID(_interface))) { \
michael@0 2309 foundInterface = static_cast<_interface *>(_allocator); \
michael@0 2310 if (!foundInterface) { \
michael@0 2311 *aInstancePtr = nullptr; \
michael@0 2312 return NS_ERROR_OUT_OF_MEMORY; \
michael@0 2313 } \
michael@0 2314 } else
michael@0 2315
michael@0 2316 /*
michael@0 2317 * In the following helper macros we exploit the fact that the result of a
michael@0 2318 * series of additions will not be finite if any one of the operands in the
michael@0 2319 * series is not finite.
michael@0 2320 */
michael@0 2321 #define NS_ENSURE_FINITE(f, rv) \
michael@0 2322 if (!NS_finite(f)) { \
michael@0 2323 return (rv); \
michael@0 2324 }
michael@0 2325
michael@0 2326 #define NS_ENSURE_FINITE2(f1, f2, rv) \
michael@0 2327 if (!NS_finite((f1)+(f2))) { \
michael@0 2328 return (rv); \
michael@0 2329 }
michael@0 2330
michael@0 2331 #define NS_ENSURE_FINITE4(f1, f2, f3, f4, rv) \
michael@0 2332 if (!NS_finite((f1)+(f2)+(f3)+(f4))) { \
michael@0 2333 return (rv); \
michael@0 2334 }
michael@0 2335
michael@0 2336 #define NS_ENSURE_FINITE5(f1, f2, f3, f4, f5, rv) \
michael@0 2337 if (!NS_finite((f1)+(f2)+(f3)+(f4)+(f5))) { \
michael@0 2338 return (rv); \
michael@0 2339 }
michael@0 2340
michael@0 2341 #define NS_ENSURE_FINITE6(f1, f2, f3, f4, f5, f6, rv) \
michael@0 2342 if (!NS_finite((f1)+(f2)+(f3)+(f4)+(f5)+(f6))) { \
michael@0 2343 return (rv); \
michael@0 2344 }
michael@0 2345
michael@0 2346 // Deletes a linked list iteratively to avoid blowing up the stack (bug 460444).
michael@0 2347 #define NS_CONTENT_DELETE_LIST_MEMBER(type_, ptr_, member_) \
michael@0 2348 { \
michael@0 2349 type_ *cur = (ptr_)->member_; \
michael@0 2350 (ptr_)->member_ = nullptr; \
michael@0 2351 while (cur) { \
michael@0 2352 type_ *next = cur->member_; \
michael@0 2353 cur->member_ = nullptr; \
michael@0 2354 delete cur; \
michael@0 2355 cur = next; \
michael@0 2356 } \
michael@0 2357 }
michael@0 2358
michael@0 2359 #endif /* nsContentUtils_h___ */

mercurial