forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsContentUtils.h
2376 lines (2089 loc) · 90.4 KB
/
nsContentUtils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* A namespace class for static content utilities. */
#ifndef nsContentUtils_h___
#define nsContentUtils_h___
#include <math.h>
#if defined(XP_WIN) || defined(XP_OS2)
#include <float.h>
#endif
#if defined(SOLARIS)
#include <ieeefp.h>
#endif
#include "js/RootingAPI.h"
#include "mozilla/Assertions.h"
#include "mozilla/AutoRestore.h"
#include "mozilla/GuardObjects.h"
#include "mozilla/TimeStamp.h"
#include "nsAString.h"
#include "nsCharSeparatedTokenizer.h"
#include "nsContentListDeclarations.h"
#include "nsMathUtils.h"
#include "nsReadableUtils.h"
class imgICache;
class imgIContainer;
class imgINotificationObserver;
class imgIRequest;
class imgLoader;
class imgRequestProxy;
class nsAutoScriptBlockerSuppressNodeRemoved;
class nsDragEvent;
class nsEvent;
class nsEventListenerManager;
class nsHtml5StringParser;
class nsIChannel;
class nsIConsoleService;
class nsIContent;
class nsIContentPolicy;
class nsIDocShell;
class nsIDocument;
class nsIDocumentLoaderFactory;
class nsIDocumentObserver;
class nsIDOMDocument;
class nsIDOMDocumentFragment;
class nsIDOMEvent;
class nsIDOMHTMLFormElement;
class nsIDOMHTMLInputElement;
class nsIDOMKeyEvent;
class nsIDOMNode;
class nsIDOMScriptObjectFactory;
class nsIDOMWindow;
class nsIDragSession;
class nsIEditor;
class nsIFragmentContentSink;
class nsIFrame;
class nsIImageLoadingContent;
class nsIInterfaceRequestor;
class nsIIOService;
class nsIJSRuntimeService;
class nsILineBreaker;
class nsIMIMEHeaderParam;
class nsINameSpaceManager;
class nsINodeInfo;
class nsIObserver;
class nsIParser;
class nsIParserService;
class nsIPresShell;
class nsIPrincipal;
class nsIRunnable;
class nsIScriptContext;
class nsIScriptGlobalObject;
class nsIScriptSecurityManager;
class nsIStringBundle;
class nsIStringBundleService;
class nsISupportsHashKey;
class nsIURI;
class nsIWidget;
class nsIWordBreaker;
class nsIXPConnect;
class nsIXPConnectJSObjectHolder;
class nsKeyEvent;
class nsNodeInfoManager;
class nsPIDOMWindow;
class nsPresContext;
class nsScriptObjectTracer;
class nsStringHashKey;
class nsTextFragment;
class nsViewportInfo;
class nsWrapperCache;
struct JSContext;
struct JSPropertyDescriptor;
struct JSRuntime;
struct nsIntMargin;
struct nsNativeKeyEvent; // Don't include nsINativeKeyBindings.h here: it will force strange compilation error!
template<class E> class nsCOMArray;
template<class E> class nsTArray;
template<class K, class V> class nsDataHashtable;
template<class K, class V> class nsRefPtrHashtable;
namespace JS {
class Value;
} // namespace JS
namespace mozilla {
class ErrorResult;
class Selection;
namespace dom {
class DocumentFragment;
class Element;
class EventTarget;
} // namespace dom
namespace layers {
class LayerManager;
} // namespace layers
// Called back from DeferredFinalize. Should add 'thing' to the array of smart
// pointers in 'pointers', creating the array if 'pointers' is null, and return
// the array.
typedef void* (*DeferredFinalizeAppendFunction)(void* pointers, void* thing);
// Called to finalize a number of objects. Slice is the number of objects
// to finalize, or if it's UINT32_MAX, all objects should be finalized.
// Return value indicates whether it finalized all objects in the buffer.
typedef bool (*DeferredFinalizeFunction)(uint32_t slice, void* data);
} // namespace mozilla
#ifdef IBMBIDI
class nsIBidiKeyboard;
#endif
extern const char kLoadAsData[];
enum EventNameType {
EventNameType_None = 0x0000,
EventNameType_HTML = 0x0001,
EventNameType_XUL = 0x0002,
EventNameType_SVGGraphic = 0x0004, // svg graphic elements
EventNameType_SVGSVG = 0x0008, // the svg element
EventNameType_SMIL = 0x0010, // smil elements
EventNameType_HTMLBodyOrFramesetOnly = 0x0020,
EventNameType_HTMLXUL = 0x0003,
EventNameType_All = 0xFFFF
};
struct EventNameMapping
{
nsIAtom* mAtom;
uint32_t mId;
int32_t mType;
uint32_t mStructType;
};
struct nsShortcutCandidate {
nsShortcutCandidate(uint32_t aCharCode, bool aIgnoreShift) :
mCharCode(aCharCode), mIgnoreShift(aIgnoreShift)
{
}
uint32_t mCharCode;
bool mIgnoreShift;
};
class nsContentUtils
{
friend class nsAutoScriptBlockerSuppressNodeRemoved;
typedef mozilla::dom::Element Element;
typedef mozilla::TimeDuration TimeDuration;
public:
static nsresult Init();
/**
* Get a JSContext from the document's scope object.
*/
static JSContext* GetContextFromDocument(nsIDocument *aDocument);
static bool IsCallerChrome();
static bool IsCallerXBL();
static bool IsImageSrcSetDisabled();
static bool LookupBindingMember(JSContext* aCx, nsIContent *aContent,
JS::HandleId aId, JSPropertyDescriptor* aDesc);
/**
* Returns the parent node of aChild crossing document boundaries.
*/
static nsINode* GetCrossDocParentNode(nsINode* aChild);
/**
* Do not ever pass null pointers to this method. If one of your
* nsIContents is null, you have to decide for yourself what
* "IsDescendantOf" really means.
*
* @param aPossibleDescendant node to test for being a descendant of
* aPossibleAncestor
* @param aPossibleAncestor node to test for being an ancestor of
* aPossibleDescendant
* @return true if aPossibleDescendant is a descendant of
* aPossibleAncestor (or is aPossibleAncestor). false
* otherwise.
*/
static bool ContentIsDescendantOf(const nsINode* aPossibleDescendant,
const nsINode* aPossibleAncestor);
/**
* Similar to ContentIsDescendantOf, except will treat an HTMLTemplateElement
* or ShadowRoot as an ancestor of things in the corresponding DocumentFragment.
* See the concept of "host-including inclusive ancestor" in the DOM
* specification.
*/
static bool ContentIsHostIncludingDescendantOf(
const nsINode* aPossibleDescendant, const nsINode* aPossibleAncestor);
/**
* Similar to ContentIsDescendantOf except it crosses document boundaries.
*/
static bool ContentIsCrossDocDescendantOf(nsINode* aPossibleDescendant,
nsINode* aPossibleAncestor);
/*
* This method fills the |aArray| with all ancestor nodes of |aNode|
* including |aNode| at the zero index.
*/
static nsresult GetAncestors(nsINode* aNode,
nsTArray<nsINode*>& aArray);
/*
* This method fills |aAncestorNodes| with all ancestor nodes of |aNode|
* including |aNode| (QI'd to nsIContent) at the zero index.
* For each ancestor, there is a corresponding element in |aAncestorOffsets|
* which is the IndexOf the child in relation to its parent.
*
* This method just sucks.
*/
static nsresult GetAncestorsAndOffsets(nsIDOMNode* aNode,
int32_t aOffset,
nsTArray<nsIContent*>* aAncestorNodes,
nsTArray<int32_t>* aAncestorOffsets);
/*
* The out parameter, |aCommonAncestor| will be the closest node, if any,
* to both |aNode| and |aOther| which is also an ancestor of each.
* Returns an error if the two nodes are disconnected and don't have
* a common ancestor.
*/
static nsresult GetCommonAncestor(nsIDOMNode *aNode,
nsIDOMNode *aOther,
nsIDOMNode** aCommonAncestor);
/**
* Returns the common ancestor, if any, for two nodes. Returns null if the
* nodes are disconnected.
*/
static nsINode* GetCommonAncestor(nsINode* aNode1,
nsINode* aNode2);
/**
* Returns true if aNode1 is before aNode2 in the same connected
* tree.
*/
static bool PositionIsBefore(nsINode* aNode1, nsINode* aNode2);
/**
* Utility routine to compare two "points", where a point is a
* node/offset pair
* Returns -1 if point1 < point2, 1, if point1 > point2,
* 0 if error or if point1 == point2.
* NOTE! If the two nodes aren't in the same connected subtree,
* the result is 1, and the optional aDisconnected parameter
* is set to true.
*/
static int32_t ComparePoints(nsINode* aParent1, int32_t aOffset1,
nsINode* aParent2, int32_t aOffset2,
bool* aDisconnected = nullptr);
static int32_t ComparePoints(nsIDOMNode* aParent1, int32_t aOffset1,
nsIDOMNode* aParent2, int32_t aOffset2,
bool* aDisconnected = nullptr);
/**
* Brute-force search of the element subtree rooted at aContent for
* an element with the given id. aId must be nonempty, otherwise
* this method may return nodes even if they have no id!
*/
static Element* MatchElementId(nsIContent *aContent, const nsAString& aId);
/**
* Similar to above, but to be used if one already has an atom for the ID
*/
static Element* MatchElementId(nsIContent *aContent, const nsIAtom* aId);
/**
* Reverses the document position flags passed in.
*
* @param aDocumentPosition The document position flags to be reversed.
*
* @return The reversed document position flags.
*
* @see nsIDOMNode
*/
static uint16_t ReverseDocumentPosition(uint16_t aDocumentPosition);
static uint32_t CopyNewlineNormalizedUnicodeTo(const nsAString& aSource,
uint32_t aSrcOffset,
PRUnichar* aDest,
uint32_t aLength,
bool& aLastCharCR);
static uint32_t CopyNewlineNormalizedUnicodeTo(nsReadingIterator<PRUnichar>& aSrcStart, const nsReadingIterator<PRUnichar>& aSrcEnd, nsAString& aDest);
static const nsDependentSubstring TrimCharsInSet(const char* aSet,
const nsAString& aValue);
template<bool IsWhitespace(PRUnichar)>
static const nsDependentSubstring TrimWhitespace(const nsAString& aStr,
bool aTrimTrailing = true);
/**
* Returns true if aChar is of class Ps, Pi, Po, Pf, or Pe.
*/
static bool IsFirstLetterPunctuation(uint32_t aChar);
static bool IsFirstLetterPunctuationAt(const nsTextFragment* aFrag, uint32_t aOffset);
/**
* Returns true if aChar is of class Lu, Ll, Lt, Lm, Lo, Nd, Nl or No
*/
static bool IsAlphanumeric(uint32_t aChar);
static bool IsAlphanumericAt(const nsTextFragment* aFrag, uint32_t aOffset);
/*
* Is the character an HTML whitespace character?
*
* We define whitespace using the list in HTML5 and css3-selectors:
* U+0009, U+000A, U+000C, U+000D, U+0020
*
* HTML 4.01 also lists U+200B (zero-width space).
*/
static bool IsHTMLWhitespace(PRUnichar aChar);
/*
* Returns whether the character is an HTML whitespace (see IsHTMLWhitespace)
* or a nbsp character (U+00A0).
*/
static bool IsHTMLWhitespaceOrNBSP(PRUnichar aChar);
/**
* Is the HTML local name a block element?
*/
static bool IsHTMLBlock(nsIAtom* aLocalName);
/**
* Is the HTML local name a void element?
*/
static bool IsHTMLVoid(nsIAtom* aLocalName);
/**
* Parse a margin string of format 'top, right, bottom, left' into
* an nsIntMargin.
*
* @param aString the string to parse
* @param aResult the resulting integer
* @return whether the value could be parsed
*/
static bool ParseIntMarginValue(const nsAString& aString, nsIntMargin& aResult);
/**
* Parse the value of the <font size=""> attribute according to the HTML5
* spec as of April 16, 2012.
*
* @param aValue the value to parse
* @return 1 to 7, or 0 if the value couldn't be parsed
*/
static int32_t ParseLegacyFontSize(const nsAString& aValue);
static void Shutdown();
/**
* Checks whether two nodes come from the same origin.
*/
static nsresult CheckSameOrigin(const nsINode* aTrustedNode,
nsIDOMNode* aUnTrustedNode);
static nsresult CheckSameOrigin(const nsINode* aTrustedNode,
const nsINode* unTrustedNode);
// Check if the (JS) caller can access aNode.
static bool CanCallerAccess(nsIDOMNode *aNode);
static bool CanCallerAccess(nsINode* aNode);
// Check if the (JS) caller can access aWindow.
// aWindow can be either outer or inner window.
static bool CanCallerAccess(nsPIDOMWindow* aWindow);
/**
* Get the window through the JS context that's currently on the stack.
* If there's no JS context currently on the stack, returns null.
*/
static nsPIDOMWindow *GetWindowFromCaller();
/**
* The two GetDocumentFrom* functions below allow a caller to get at a
* document that is relevant to the currently executing script.
*
* GetDocumentFromCaller gets its document by looking at the last called
* function and finding the document that the function itself relates to.
* For example, consider two windows A and B in the same origin. B has a
* function which does something that ends up needing the current document.
* If a script in window A were to call B's function, GetDocumentFromCaller
* would find that function (in B) and return B's document.
*
* GetDocumentFromContext gets its document by looking at the currently
* executing context's global object and returning its document. Thus,
* given the example above, GetDocumentFromCaller would see that the
* currently executing script was in window A, and return A's document.
*/
/**
* Get the document from the currently executing function. This will return
* the document that the currently executing function is in/from.
*
* @return The document or null if no JS Context.
*/
static nsIDocument* GetDocumentFromCaller();
/**
* Get the document through the JS context that's currently on the stack.
* If there's no JS context currently on the stack it will return null.
* This will return the document of the calling script.
*
* @return The document or null if no JS context
*/
static nsIDocument* GetDocumentFromContext();
// Check if a node is in the document prolog, i.e. before the document
// element.
static bool InProlog(nsINode *aNode);
static nsIParserService* GetParserService();
static nsINameSpaceManager* NameSpaceManager()
{
return sNameSpaceManager;
}
static nsIIOService* GetIOService()
{
return sIOService;
}
#ifdef IBMBIDI
static nsIBidiKeyboard* GetBidiKeyboard();
#endif
/**
* Get the cache security manager service. Can return null if the layout
* module has been shut down.
*/
static nsIScriptSecurityManager* GetSecurityManager()
{
return sSecurityManager;
}
// Returns the subject principal. Guaranteed to return non-null. May only
// be called when nsContentUtils is initialized.
static nsIPrincipal* GetSubjectPrincipal();
// Returns the principal of the given JS object. This should never be null
// for any object in the XPConnect runtime.
//
// In general, being interested in the principal of an object is enough to
// guarantee that the return value is non-null.
static nsIPrincipal* GetObjectPrincipal(JSObject* aObj);
static nsresult GenerateStateKey(nsIContent* aContent,
const nsIDocument* aDocument,
nsACString& aKey);
/**
* Create a new nsIURI from aSpec, using aBaseURI as the base. The
* origin charset of the new nsIURI will be the document charset of
* aDocument.
*/
static nsresult NewURIWithDocumentCharset(nsIURI** aResult,
const nsAString& aSpec,
nsIDocument* aDocument,
nsIURI* aBaseURI);
/**
* Convert aInput (in charset aCharset) to UTF16 in aOutput.
*
* @param aCharset the name of the charset; if empty, we assume UTF8
*/
static nsresult ConvertStringFromCharset(const nsACString& aCharset,
const nsACString& aInput,
nsAString& aOutput);
/**
* Determine whether a buffer begins with a BOM for UTF-8, UTF-16LE,
* UTF-16BE
*
* @param aBuffer the buffer to check
* @param aLength the length of the buffer
* @param aCharset empty if not found
* @return boolean indicating whether a BOM was detected.
*/
static bool CheckForBOM(const unsigned char* aBuffer, uint32_t aLength,
nsACString& aCharset);
static nsresult GuessCharset(const char *aData, uint32_t aDataLen,
nsACString &aCharset);
static nsresult CheckQName(const nsAString& aQualifiedName,
bool aNamespaceAware = true,
const PRUnichar** aColon = nullptr);
static nsresult SplitQName(const nsIContent* aNamespaceResolver,
const nsAFlatString& aQName,
int32_t *aNamespace, nsIAtom **aLocalName);
static nsresult GetNodeInfoFromQName(const nsAString& aNamespaceURI,
const nsAString& aQualifiedName,
nsNodeInfoManager* aNodeInfoManager,
uint16_t aNodeType,
nsINodeInfo** aNodeInfo);
static void SplitExpatName(const PRUnichar *aExpatName, nsIAtom **aPrefix,
nsIAtom **aTagName, int32_t *aNameSpaceID);
// Get a permission-manager setting for the given principal and type.
// If the pref doesn't exist or if it isn't ALLOW_ACTION, false is
// returned, otherwise true is returned. Always returns true for the
// system principal, and false for a null principal.
static bool IsSitePermAllow(nsIPrincipal* aPrincipal, const char* aType);
// Get a permission-manager setting for the given principal and type.
// If the pref doesn't exist or if it isn't DENY_ACTION, false is
// returned, otherwise true is returned. Always returns false for the
// system principal, and true for a null principal.
static bool IsSitePermDeny(nsIPrincipal* aPrincipal, const char* aType);
// Get a permission-manager setting for the given principal and type.
// If the pref doesn't exist or if it isn't ALLOW_ACTION, false is
// returned, otherwise true is returned. Always returns true for the
// system principal, and false for a null principal.
// This version checks the permission for an exact host match on
// the principal
static bool IsExactSitePermAllow(nsIPrincipal* aPrincipal, const char* aType);
// Get a permission-manager setting for the given principal and type.
// If the pref doesn't exist or if it isn't DENY_ACTION, false is
// returned, otherwise true is returned. Always returns false for the
// system principal, and true for a null principal.
// This version checks the permission for an exact host match on
// the principal
static bool IsExactSitePermDeny(nsIPrincipal* aPrincipal, const char* aType);
// Returns true if aDoc1 and aDoc2 have equal NodePrincipal()s.
static bool HaveEqualPrincipals(nsIDocument* aDoc1, nsIDocument* aDoc2);
static nsILineBreaker* LineBreaker()
{
return sLineBreaker;
}
static nsIWordBreaker* WordBreaker()
{
return sWordBreaker;
}
/**
* Regster aObserver as a shutdown observer. A strong reference is held
* to aObserver until UnregisterShutdownObserver is called.
*/
static void RegisterShutdownObserver(nsIObserver* aObserver);
static void UnregisterShutdownObserver(nsIObserver* aObserver);
/**
* @return true if aContent has an attribute aName in namespace aNameSpaceID,
* and the attribute value is non-empty.
*/
static bool HasNonEmptyAttr(const nsIContent* aContent, int32_t aNameSpaceID,
nsIAtom* aName);
/**
* Method that gets the primary presContext for the node.
*
* @param aContent The content node.
* @return the presContext, or nullptr if the content is not in a document
* (if GetCurrentDoc returns nullptr)
*/
static nsPresContext* GetContextForContent(const nsIContent* aContent);
/**
* Method to do security and content policy checks on the image URI
*
* @param aURI uri of the image to be loaded
* @param aContext the context the image is loaded in (eg an element)
* @param aLoadingDocument the document we belong to
* @param aLoadingPrincipal the principal doing the load
* @param aImageBlockingStatus the nsIContentPolicy blocking status for this
* image. This will be set even if a security check fails for the
* image, to some reasonable REJECT_* value. This out param will only
* be set if it's non-null.
* @return true if the load can proceed, or false if it is blocked.
* Note that aImageBlockingStatus, if set will always be an ACCEPT
* status if true is returned and always be a REJECT_* status if
* false is returned.
*/
static bool CanLoadImage(nsIURI* aURI,
nsISupports* aContext,
nsIDocument* aLoadingDocument,
nsIPrincipal* aLoadingPrincipal,
int16_t* aImageBlockingStatus = nullptr);
/**
* Method to start an image load. This does not do any security checks.
* This method will attempt to make aURI immutable; a caller that wants to
* keep a mutable version around should pass in a clone.
*
* @param aURI uri of the image to be loaded
* @param aLoadingDocument the document we belong to
* @param aLoadingPrincipal the principal doing the load
* @param aReferrer the referrer URI
* @param aObserver the observer for the image load
* @param aLoadFlags the load flags to use. See nsIRequest
* @return the imgIRequest for the image load
*/
static nsresult LoadImage(nsIURI* aURI,
nsIDocument* aLoadingDocument,
nsIPrincipal* aLoadingPrincipal,
nsIURI* aReferrer,
imgINotificationObserver* aObserver,
int32_t aLoadFlags,
imgRequestProxy** aRequest);
/**
* Obtain an image loader that respects the given document/channel's privacy status.
* Null document/channel arguments return the public image loader.
*/
static imgLoader* GetImgLoaderForDocument(nsIDocument* aDoc);
static imgLoader* GetImgLoaderForChannel(nsIChannel* aChannel);
/**
* Returns whether the given URI is in the image cache.
*/
static bool IsImageInCache(nsIURI* aURI, nsIDocument* aDocument);
/**
* Method to get an imgIContainer from an image loading content
*
* @param aContent The image loading content. Must not be null.
* @param aRequest The image request [out]
* @return the imgIContainer corresponding to the first frame of the image
*/
static already_AddRefed<imgIContainer> GetImageFromContent(nsIImageLoadingContent* aContent, imgIRequest **aRequest = nullptr);
/**
* Helper method to call imgIRequest::GetStaticRequest.
*/
static already_AddRefed<imgRequestProxy> GetStaticRequest(imgRequestProxy* aRequest);
/**
* Method that decides whether a content node is draggable
*
* @param aContent The content node to test.
* @return whether it's draggable
*/
static bool ContentIsDraggable(nsIContent* aContent);
/**
* Method that decides whether a content node is a draggable image
*
* @param aContent The content node to test.
* @return whether it's a draggable image
*/
static bool IsDraggableImage(nsIContent* aContent);
/**
* Method that decides whether a content node is a draggable link
*
* @param aContent The content node to test.
* @return whether it's a draggable link
*/
static bool IsDraggableLink(const nsIContent* aContent);
/**
* Convenience method to create a new nodeinfo that differs only by name
* from aNodeInfo.
*/
static nsresult NameChanged(nsINodeInfo* aNodeInfo, nsIAtom* aName,
nsINodeInfo** aResult);
/**
* Returns the appropriate event argument names for the specified
* namespace and event name. Added because we need to switch between
* SVG's "evt" and the rest of the world's "event", and because onerror
* takes 3 args.
*/
static void GetEventArgNames(int32_t aNameSpaceID, nsIAtom *aEventName,
uint32_t *aArgCount, const char*** aArgNames);
/**
* If aNode is not an element, return true exactly when aContent's binding
* parent is null.
*
* If aNode is an element, return true exactly when aContent's binding parent
* is the same as aNode's.
*
* This method is particularly useful for callers who are trying to ensure
* that they are working with a non-anonymous descendant of a given node. If
* aContent is a descendant of aNode, a return value of false from this
* method means that it's an anonymous descendant from aNode's point of view.
*
* Both arguments to this method must be non-null.
*/
static bool IsInSameAnonymousTree(const nsINode* aNode, const nsIContent* aContent);
/**
* Return the nsIXPConnect service.
*/
static nsIXPConnect *XPConnect()
{
return sXPConnect;
}
/**
* Report a non-localized error message to the error console.
* @param aErrorText the error message
* @param aErrorFlags See nsIScriptError.
* @param aCategory Name of module reporting error.
* @param aDocument Reference to the document which triggered the message.
* @param [aURI=nullptr] (Optional) URI of resource containing error.
* @param [aSourceLine=EmptyString()] (Optional) The text of the line that
contains the error (may be empty).
* @param [aLineNumber=0] (Optional) Line number within resource
containing error.
* @param [aColumnNumber=0] (Optional) Column number within resource
containing error.
If aURI is null, then aDocument->GetDocumentURI() is used.
*/
static nsresult ReportToConsoleNonLocalized(const nsAString& aErrorText,
uint32_t aErrorFlags,
const nsACString& aCategory,
nsIDocument* aDocument,
nsIURI* aURI = nullptr,
const nsAFlatString& aSourceLine
= EmptyString(),
uint32_t aLineNumber = 0,
uint32_t aColumnNumber = 0);
/**
* Report a localized error message to the error console.
* @param aErrorFlags See nsIScriptError.
* @param aCategory Name of module reporting error.
* @param aDocument Reference to the document which triggered the message.
* @param aFile Properties file containing localized message.
* @param aMessageName Name of localized message.
* @param [aParams=nullptr] (Optional) Parameters to be substituted into
localized message.
* @param [aParamsLength=0] (Optional) Length of aParams.
* @param [aURI=nullptr] (Optional) URI of resource containing error.
* @param [aSourceLine=EmptyString()] (Optional) The text of the line that
contains the error (may be empty).
* @param [aLineNumber=0] (Optional) Line number within resource
containing error.
* @param [aColumnNumber=0] (Optional) Column number within resource
containing error.
If aURI is null, then aDocument->GetDocumentURI() is used.
*/
enum PropertiesFile {
eCSS_PROPERTIES,
eXBL_PROPERTIES,
eXUL_PROPERTIES,
eLAYOUT_PROPERTIES,
eFORMS_PROPERTIES,
ePRINTING_PROPERTIES,
eDOM_PROPERTIES,
eHTMLPARSER_PROPERTIES,
eSVG_PROPERTIES,
eBRAND_PROPERTIES,
eCOMMON_DIALOG_PROPERTIES,
eMATHML_PROPERTIES,
eSECURITY_PROPERTIES,
PropertiesFile_COUNT
};
static nsresult ReportToConsole(uint32_t aErrorFlags,
const nsACString& aCategory,
nsIDocument* aDocument,
PropertiesFile aFile,
const char *aMessageName,
const PRUnichar **aParams = nullptr,
uint32_t aParamsLength = 0,
nsIURI* aURI = nullptr,
const nsAFlatString& aSourceLine
= EmptyString(),
uint32_t aLineNumber = 0,
uint32_t aColumnNumber = 0);
// This overload allows passing a literal string for aCategory.
template<uint32_t N>
static nsresult ReportToConsole(uint32_t aErrorFlags,
const char (&aCategory)[N],
nsIDocument* aDocument,
PropertiesFile aFile,
const char *aMessageName,
const PRUnichar **aParams = nullptr,
uint32_t aParamsLength = 0,
nsIURI* aURI = nullptr,
const nsAFlatString& aSourceLine
= EmptyString(),
uint32_t aLineNumber = 0,
uint32_t aColumnNumber = 0)
{
nsDependentCString category(aCategory, N - 1);
return ReportToConsole(aErrorFlags, category, aDocument, aFile,
aMessageName, aParams, aParamsLength, aURI,
aSourceLine, aLineNumber, aColumnNumber);
}
/**
* Get the localized string named |aKey| in properties file |aFile|.
*/
static nsresult GetLocalizedString(PropertiesFile aFile,
const char* aKey,
nsXPIDLString& aResult);
/**
* A helper function that parses a sandbox attribute (of an <iframe> or
* a CSP directive) and converts it to the set of flags used internally.
*
* @param aAttribute the value of the sandbox attribute
* @return the set of flags
*/
static uint32_t ParseSandboxAttributeToFlags(const nsAString& aSandboxAttr);
/**
* Fill (with the parameters given) the localized string named |aKey| in
* properties file |aFile|.
*/
private:
static nsresult FormatLocalizedString(PropertiesFile aFile,
const char* aKey,
const PRUnichar** aParams,
uint32_t aParamsLength,
nsXPIDLString& aResult);
public:
template<uint32_t N>
static nsresult FormatLocalizedString(PropertiesFile aFile,
const char* aKey,
const PRUnichar* (&aParams)[N],
nsXPIDLString& aResult)
{
return FormatLocalizedString(aFile, aKey, aParams, N, aResult);
}
/**
* Returns true if aDocument is a chrome document
*/
static bool IsChromeDoc(nsIDocument *aDocument);
/**
* Returns true if aDocument is in a docshell whose parent is the same type
*/
static bool IsChildOfSameType(nsIDocument* aDoc);
/**
'* Returns true if the content-type will be rendered as plain-text.
*/
static bool IsPlainTextType(const nsACString& aContentType);
/**
* Get the script file name to use when compiling the script
* referenced by aURI. In cases where there's no need for any extra
* security wrapper automation the script file name that's returned
* will be the spec in aURI, else it will be the spec in aDocument's
* URI followed by aURI's spec, separated by " -> ". Returns true
* if the script file name was modified, false if it's aURI's
* spec.
*/
static bool GetWrapperSafeScriptFilename(nsIDocument *aDocument,
nsIURI *aURI,
nsACString& aScriptURI);
/**
* Returns true if aDocument belongs to a chrome docshell for
* display purposes. Returns false for null documents or documents
* which do not belong to a docshell.
*/
static bool IsInChromeDocshell(nsIDocument *aDocument);
/**
* Return the content policy service
*/
static nsIContentPolicy *GetContentPolicy();
/**
* Quick helper to determine whether there are any mutation listeners
* of a given type that apply to this content or any of its ancestors.
* The method has the side effect to call document's MayDispatchMutationEvent
* using aTargetForSubtreeModified as the parameter.
*
* @param aNode The node to search for listeners
* @param aType The type of listener (NS_EVENT_BITS_MUTATION_*)
* @param aTargetForSubtreeModified The node which is the target of the
* possible DOMSubtreeModified event.
*
* @return true if there are mutation listeners of the specified type
*/
static bool HasMutationListeners(nsINode* aNode,
uint32_t aType,
nsINode* aTargetForSubtreeModified);
/**
* Quick helper to determine whether there are any mutation listeners
* of a given type that apply to any content in this document. It is valid
* to pass null for aDocument here, in which case this function always
* returns true.
*
* @param aDocument The document to search for listeners
* @param aType The type of listener (NS_EVENT_BITS_MUTATION_*)
*
* @return true if there are mutation listeners of the specified type
*/
static bool HasMutationListeners(nsIDocument* aDocument,
uint32_t aType);
/**
* Synchronously fire DOMNodeRemoved on aChild. Only fires the event if
* there really are listeners by checking using the HasMutationListeners
* function above. The function makes sure to hold the relevant objects alive
* for the duration of the event firing. However there are no guarantees
* that any of the objects are alive by the time the function returns.
* If you depend on that you need to hold references yourself.
*
* @param aChild The node to fire DOMNodeRemoved at.
* @param aParent The parent of aChild.
* @param aOwnerDoc The ownerDocument of aChild.
*/
static void MaybeFireNodeRemoved(nsINode* aChild, nsINode* aParent,
nsIDocument* aOwnerDoc);
/**
* This method creates and dispatches a trusted event.
* Works only with events which can be created by calling
* nsIDOMDocument::CreateEvent() with parameter "Events".
* @param aDoc The document which will be used to create the event.
* @param aTarget The target of the event, should be QIable to
* nsIDOMEventTarget.
* @param aEventName The name of the event.
* @param aCanBubble Whether the event can bubble.
* @param aCancelable Is the event cancelable.
* @param aDefaultAction Set to true if default action should be taken,
* see nsIDOMEventTarget::DispatchEvent.
*/
static nsresult DispatchTrustedEvent(nsIDocument* aDoc,
nsISupports* aTarget,
const nsAString& aEventName,
bool aCanBubble,
bool aCancelable,
bool *aDefaultAction = nullptr);
/**
* This method creates and dispatches a untrusted event.
* Works only with events which can be created by calling
* nsIDOMDocument::CreateEvent() with parameter "Events".
* @param aDoc The document which will be used to create the event.
* @param aTarget The target of the event, should be QIable to
* nsIDOMEventTarget.
* @param aEventName The name of the event.
* @param aCanBubble Whether the event can bubble.
* @param aCancelable Is the event cancelable.
* @param aDefaultAction Set to true if default action should be taken,
* see nsIDOMEventTarget::DispatchEvent.
*/
static nsresult DispatchUntrustedEvent(nsIDocument* aDoc,
nsISupports* aTarget,
const nsAString& aEventName,
bool aCanBubble,
bool aCancelable,
bool *aDefaultAction = nullptr);
/**
* This method creates and dispatches a trusted event to the chrome
* event handler.
* Works only with events which can be created by calling
* nsIDOMDocument::CreateEvent() with parameter "Events".
* @param aDocument The document which will be used to create the event,
* and whose window's chrome handler will be used to
* dispatch the event.