This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathGeckoBindings.cpp
More file actions
2848 lines (2449 loc) · 83.1 KB
/
Copy pathGeckoBindings.cpp
File metadata and controls
2848 lines (2449 loc) · 83.1 KB
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: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
/* FFI functions for Servo to call into Gecko */
#include "mozilla/GeckoBindings.h"
#include "ChildIterator.h"
#include "ErrorReporter.h"
#include "GeckoProfiler.h"
#include "gfxFontFamilyList.h"
#include "gfxFontFeatures.h"
#include "nsAnimationManager.h"
#include "nsAttrValueInlines.h"
#include "nsCSSFrameConstructor.h"
#include "nsCSSProps.h"
#include "nsCSSPseudoElements.h"
#include "nsContentUtils.h"
#include "nsDOMTokenList.h"
#include "nsDeviceContext.h"
#include "nsIContentInlines.h"
#include "nsICrashReporter.h"
#include "nsIDocumentInlines.h"
#include "nsILoadContext.h"
#include "nsIFrame.h"
#include "nsIMemoryReporter.h"
#include "nsIMozBrowserFrame.h"
#include "nsINode.h"
#include "nsIPresShell.h"
#include "nsIPresShellInlines.h"
#include "nsIPrincipal.h"
#include "nsIURI.h"
#include "nsFontMetrics.h"
#include "nsHTMLStyleSheet.h"
#include "nsMappedAttributes.h"
#include "nsMediaFeatures.h"
#include "nsNameSpaceManager.h"
#include "nsNetUtil.h"
#include "nsProxyRelease.h"
#include "nsString.h"
#include "nsStyleStruct.h"
#include "nsStyleUtil.h"
#include "nsSVGElement.h"
#include "nsTArray.h"
#include "nsTransitionManager.h"
#include "nsWindowSizes.h"
#include "mozilla/CORSMode.h"
#include "mozilla/DeclarationBlock.h"
#include "mozilla/EffectCompositor.h"
#include "mozilla/EffectSet.h"
#include "mozilla/EventStates.h"
#include "mozilla/FontPropertyTypes.h"
#include "mozilla/Keyframe.h"
#include "mozilla/Mutex.h"
#include "mozilla/Preferences.h"
#include "mozilla/ServoElementSnapshot.h"
#include "mozilla/StaticPrefs.h"
#include "mozilla/RestyleManager.h"
#include "mozilla/SizeOfState.h"
#include "mozilla/StyleAnimationValue.h"
#include "mozilla/SystemGroup.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/ServoTraversalStatistics.h"
#include "mozilla/Telemetry.h"
#include "mozilla/RWLock.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/ElementInlines.h"
#include "mozilla/dom/HTMLTableCellElement.h"
#include "mozilla/dom/HTMLBodyElement.h"
#include "mozilla/dom/HTMLSlotElement.h"
#include "mozilla/dom/MediaList.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/URLExtraData.h"
#include "mozilla/dom/CSSMozDocumentRule.h"
#if defined(MOZ_MEMORY)
# include "mozmemory.h"
#endif
using namespace mozilla;
using namespace mozilla::css;
using namespace mozilla::dom;
#define SERVO_ARC_TYPE(name_, type_) \
already_AddRefed<type_> \
type_##Strong::Consume() { \
RefPtr<type_> result; \
result.swap(mPtr); \
return result.forget(); \
}
#include "mozilla/ServoArcTypeList.h"
SERVO_ARC_TYPE(ComputedStyle, ComputedStyle)
#undef SERVO_ARC_TYPE
// Definitions of the global traversal stats.
bool ServoTraversalStatistics::sActive = false;
ServoTraversalStatistics ServoTraversalStatistics::sSingleton;
static RWLock* sServoFFILock = nullptr;
static
const nsFont*
ThreadSafeGetDefaultFontHelper(const nsPresContext* aPresContext,
nsAtom* aLanguage, uint8_t aGenericId)
{
bool needsCache = false;
const nsFont* retval;
{
AutoReadLock guard(*sServoFFILock);
retval = aPresContext->GetDefaultFont(aGenericId, aLanguage, &needsCache);
}
if (!needsCache) {
return retval;
}
{
AutoWriteLock guard(*sServoFFILock);
retval = aPresContext->GetDefaultFont(aGenericId, aLanguage, nullptr);
}
return retval;
}
/*
* Does this child count as significant for selector matching?
*
* See nsStyleUtil::IsSignificantChild for details.
*/
bool
Gecko_IsSignificantChild(RawGeckoNodeBorrowed aNode,
bool aWhitespaceIsSignificant)
{
return nsStyleUtil::ThreadSafeIsSignificantChild(aNode->AsContent(),
aWhitespaceIsSignificant);
}
RawGeckoNodeBorrowedOrNull
Gecko_GetLastChild(RawGeckoNodeBorrowed aNode)
{
return aNode->GetLastChild();
}
RawGeckoNodeBorrowedOrNull
Gecko_GetPreviousSibling(RawGeckoNodeBorrowed aNode)
{
return aNode->GetPreviousSibling();
}
RawGeckoNodeBorrowedOrNull
Gecko_GetFlattenedTreeParentNode(RawGeckoNodeBorrowed aNode)
{
return aNode->GetFlattenedTreeParentNodeForStyle();
}
RawGeckoElementBorrowedOrNull
Gecko_GetBeforeOrAfterPseudo(RawGeckoElementBorrowed aElement, bool aIsBefore)
{
MOZ_ASSERT(aElement);
MOZ_ASSERT(aElement->HasProperties());
return aIsBefore
? nsLayoutUtils::GetBeforePseudo(aElement)
: nsLayoutUtils::GetAfterPseudo(aElement);
}
nsTArray<nsIContent*>*
Gecko_GetAnonymousContentForElement(RawGeckoElementBorrowed aElement)
{
nsIAnonymousContentCreator* ac = do_QueryFrame(aElement->GetPrimaryFrame());
if (!ac) {
return nullptr;
}
auto* array = new nsTArray<nsIContent*>();
nsContentUtils::AppendNativeAnonymousChildren(aElement, *array, 0);
return array;
}
void
Gecko_DestroyAnonymousContentList(nsTArray<nsIContent*>* aAnonContent)
{
MOZ_ASSERT(aAnonContent);
delete aAnonContent;
}
const nsTArray<RefPtr<nsINode>>*
Gecko_GetAssignedNodes(RawGeckoElementBorrowed aElement)
{
MOZ_ASSERT(HTMLSlotElement::FromNode(aElement));
return &static_cast<const HTMLSlotElement*>(aElement)->AssignedNodes();
}
void
Gecko_ComputedStyle_Init(mozilla::ComputedStyle* aStyle,
RawGeckoPresContextBorrowed aPresContext,
const ServoComputedData* aValues,
mozilla::CSSPseudoElementType aPseudoType,
nsAtom* aPseudoTag)
{
auto* presContext = const_cast<nsPresContext*>(aPresContext);
new (KnownNotNull, aStyle) mozilla::ComputedStyle(
presContext, aPseudoTag, aPseudoType,
ServoComputedDataForgotten(aValues));
}
ServoComputedData::ServoComputedData(
const ServoComputedDataForgotten aValue)
{
PodAssign(this, aValue.mPtr);
}
MOZ_DEFINE_MALLOC_ENCLOSING_SIZE_OF(ServoStyleStructsMallocEnclosingSizeOf)
void
ServoComputedData::AddSizeOfExcludingThis(nsWindowSizes& aSizes) const
{
// Note: GetStyleFoo() returns a pointer to an nsStyleFoo that sits within a
// servo_arc::Arc, i.e. it is preceded by a word-sized refcount. So we need
// to measure it with a function that can handle an interior pointer. We use
// ServoStyleStructsEnclosingMallocSizeOf to clearly identify in DMD's
// output the memory measured here.
#define STYLE_STRUCT(name_) \
static_assert(alignof(nsStyle##name_) <= sizeof(size_t), \
"alignment will break AddSizeOfExcludingThis()"); \
const void* p##name_ = GetStyle##name_(); \
if (!aSizes.mState.HaveSeenPtr(p##name_)) { \
aSizes.mStyleSizes.NS_STYLE_SIZES_FIELD(name_) += \
ServoStyleStructsMallocEnclosingSizeOf(p##name_); \
}
#include "nsStyleStructList.h"
#undef STYLE_STRUCT
if (visited_style.mPtr && !aSizes.mState.HaveSeenPtr(visited_style.mPtr)) {
visited_style.mPtr->AddSizeOfIncludingThis(
aSizes, &aSizes.mLayoutComputedValuesVisited);
}
// Measurement of the following members may be added later if DMD finds it is
// worthwhile:
// - custom_properties
// - writing_mode
// - rules
// - font_computation_data
}
void
Gecko_ComputedStyle_Destroy(mozilla::ComputedStyle* aStyle)
{
aStyle->~ComputedStyle();
}
void
Gecko_ConstructStyleChildrenIterator(
RawGeckoElementBorrowed aElement,
RawGeckoStyleChildrenIteratorBorrowedMut aIterator)
{
MOZ_ASSERT(aElement);
MOZ_ASSERT(aIterator);
new (aIterator) StyleChildrenIterator(aElement);
}
void
Gecko_DestroyStyleChildrenIterator(
RawGeckoStyleChildrenIteratorBorrowedMut aIterator)
{
MOZ_ASSERT(aIterator);
aIterator->~StyleChildrenIterator();
}
RawGeckoNodeBorrowed
Gecko_GetNextStyleChild(RawGeckoStyleChildrenIteratorBorrowedMut aIterator)
{
MOZ_ASSERT(aIterator);
return aIterator->GetNextChild();
}
bool
Gecko_VisitedStylesEnabled(const nsIDocument* aDoc)
{
MOZ_ASSERT(aDoc);
MOZ_ASSERT(NS_IsMainThread());
if (!StaticPrefs::layout_css_visited_links_enabled()) {
return false;
}
if (aDoc->IsBeingUsedAsImage()) {
return false;
}
nsILoadContext* loadContext = aDoc->GetLoadContext();
if (loadContext && loadContext->UsePrivateBrowsing()) {
return false;
}
return true;
}
EventStates::ServoType
Gecko_ElementState(RawGeckoElementBorrowed aElement)
{
return aElement->StyleState().ServoValue();
}
bool
Gecko_IsRootElement(RawGeckoElementBorrowed aElement)
{
return aElement->OwnerDoc()->GetRootElement() == aElement;
}
// Dirtiness tracking.
void
Gecko_SetNodeFlags(RawGeckoNodeBorrowed aNode, uint32_t aFlags)
{
const_cast<nsINode*>(aNode)->SetFlags(aFlags);
}
void
Gecko_UnsetNodeFlags(RawGeckoNodeBorrowed aNode, uint32_t aFlags)
{
const_cast<nsINode*>(aNode)->UnsetFlags(aFlags);
}
void
Gecko_NoteDirtyElement(RawGeckoElementBorrowed aElement)
{
MOZ_ASSERT(NS_IsMainThread());
const_cast<Element*>(aElement)->NoteDirtyForServo();
}
void
Gecko_NoteDirtySubtreeForInvalidation(RawGeckoElementBorrowed aElement)
{
MOZ_ASSERT(NS_IsMainThread());
const_cast<Element*>(aElement)->NoteDirtySubtreeForServo();
}
void
Gecko_NoteAnimationOnlyDirtyElement(RawGeckoElementBorrowed aElement)
{
MOZ_ASSERT(NS_IsMainThread());
const_cast<Element*>(aElement)->NoteAnimationOnlyDirtyForServo();
}
bool Gecko_AnimationNameMayBeReferencedFromStyle(
RawGeckoPresContextBorrowed aPresContext,
nsAtom* aName)
{
MOZ_ASSERT(aPresContext);
return aPresContext->AnimationManager()->AnimationMayBeReferenced(aName);
}
CSSPseudoElementType
Gecko_GetImplementedPseudo(RawGeckoElementBorrowed aElement)
{
return aElement->GetPseudoElementType();
}
uint32_t
Gecko_CalcStyleDifference(ComputedStyleBorrowed aOldStyle,
ComputedStyleBorrowed aNewStyle,
bool* aAnyStyleStructChanged,
bool* aOnlyResetStructsChanged)
{
MOZ_ASSERT(aOldStyle);
MOZ_ASSERT(aNewStyle);
uint32_t equalStructs;
nsChangeHint result = const_cast<ComputedStyle*>(aOldStyle)->
CalcStyleDifference(const_cast<ComputedStyle*>(aNewStyle), &equalStructs);
*aAnyStyleStructChanged =
equalStructs != StyleStructConstants::kAllStructsMask;
const auto kInheritedStructsMask = StyleStructConstants::kInheritedStructsMask;
*aOnlyResetStructsChanged =
(equalStructs & kInheritedStructsMask) == kInheritedStructsMask;
return result;
}
const ServoElementSnapshot*
Gecko_GetElementSnapshot(const ServoElementSnapshotTable* aTable,
const Element* aElement)
{
MOZ_ASSERT(aTable);
MOZ_ASSERT(aElement);
return aTable->Get(const_cast<Element*>(aElement));
}
bool
Gecko_HaveSeenPtr(SeenPtrs* aTable, const void* aPtr)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aTable);
// Empty Rust allocations are indicated by small values up to the alignment
// of the relevant type. We shouldn't see anything like that here.
MOZ_ASSERT(uintptr_t(aPtr) > 16);
return aTable->HaveSeenPtr(aPtr);
}
RawServoDeclarationBlockStrongBorrowedOrNull
Gecko_GetStyleAttrDeclarationBlock(RawGeckoElementBorrowed aElement)
{
DeclarationBlock* decl = aElement->GetInlineStyleDeclaration();
if (!decl) {
return nullptr;
}
return decl->RefRawStrong();
}
void
Gecko_UnsetDirtyStyleAttr(RawGeckoElementBorrowed aElement)
{
DeclarationBlock* decl = aElement->GetInlineStyleDeclaration();
if (!decl) {
return;
}
decl->UnsetDirty();
}
static const RawServoDeclarationBlockStrong*
AsRefRawStrong(const RefPtr<RawServoDeclarationBlock>& aDecl)
{
static_assert(sizeof(RefPtr<RawServoDeclarationBlock>) ==
sizeof(RawServoDeclarationBlockStrong),
"RefPtr should just be a pointer");
return reinterpret_cast<const RawServoDeclarationBlockStrong*>(&aDecl);
}
RawServoDeclarationBlockStrongBorrowedOrNull
Gecko_GetHTMLPresentationAttrDeclarationBlock(RawGeckoElementBorrowed aElement)
{
const nsMappedAttributes* attrs = aElement->GetMappedAttributes();
if (!attrs) {
auto* svg = nsSVGElement::FromNodeOrNull(aElement);
if (svg) {
if (auto decl = svg->GetContentDeclarationBlock()) {
return decl->RefRawStrong();
}
}
return nullptr;
}
return AsRefRawStrong(attrs->GetServoStyle());
}
RawServoDeclarationBlockStrongBorrowedOrNull
Gecko_GetExtraContentStyleDeclarations(RawGeckoElementBorrowed aElement)
{
if (!aElement->IsAnyOfHTMLElements(nsGkAtoms::td, nsGkAtoms::th)) {
return nullptr;
}
const HTMLTableCellElement* cell = static_cast<const HTMLTableCellElement*>(aElement);
if (nsMappedAttributes* attrs = cell->GetMappedAttributesInheritedFromTable()) {
return AsRefRawStrong(attrs->GetServoStyle());
}
return nullptr;
}
RawServoDeclarationBlockStrongBorrowedOrNull
Gecko_GetUnvisitedLinkAttrDeclarationBlock(RawGeckoElementBorrowed aElement)
{
nsHTMLStyleSheet* sheet = aElement->OwnerDoc()->GetAttributeStyleSheet();
if (!sheet) {
return nullptr;
}
return AsRefRawStrong(sheet->GetServoUnvisitedLinkDecl());
}
StyleSheet* Gecko_StyleSheet_Clone(
const StyleSheet* aSheet,
const StyleSheet* aNewParentSheet)
{
MOZ_ASSERT(aSheet);
MOZ_ASSERT(aSheet->GetParentSheet(), "Should only be used for @import");
MOZ_ASSERT(aNewParentSheet, "Wat");
RefPtr<StyleSheet> newSheet =
aSheet->Clone(nullptr, nullptr, nullptr, nullptr);
// NOTE(emilio): This code runs in the StylesheetInner constructor, which
// means that the inner pointer of `aNewParentSheet` still points to the old
// one.
//
// So we _don't_ update neither the parent pointer of the stylesheet, nor the
// child list (yet). This is fixed up in that same constructor.
return static_cast<StyleSheet*>(newSheet.forget().take());
}
void
Gecko_StyleSheet_AddRef(const StyleSheet* aSheet)
{
MOZ_ASSERT(NS_IsMainThread());
const_cast<StyleSheet*>(aSheet)->AddRef();
}
void
Gecko_StyleSheet_Release(const StyleSheet* aSheet)
{
MOZ_ASSERT(NS_IsMainThread());
const_cast<StyleSheet*>(aSheet)->Release();
}
RawServoDeclarationBlockStrongBorrowedOrNull
Gecko_GetVisitedLinkAttrDeclarationBlock(RawGeckoElementBorrowed aElement)
{
nsHTMLStyleSheet* sheet = aElement->OwnerDoc()->GetAttributeStyleSheet();
if (!sheet) {
return nullptr;
}
return AsRefRawStrong(sheet->GetServoVisitedLinkDecl());
}
RawServoDeclarationBlockStrongBorrowedOrNull
Gecko_GetActiveLinkAttrDeclarationBlock(RawGeckoElementBorrowed aElement)
{
nsHTMLStyleSheet* sheet = aElement->OwnerDoc()->GetAttributeStyleSheet();
if (!sheet) {
return nullptr;
}
return AsRefRawStrong(sheet->GetServoActiveLinkDecl());
}
static CSSPseudoElementType
GetPseudoTypeFromElementForAnimation(const Element*& aElementOrPseudo) {
if (aElementOrPseudo->IsGeneratedContentContainerForBefore()) {
aElementOrPseudo = aElementOrPseudo->GetParent()->AsElement();
return CSSPseudoElementType::before;
}
if (aElementOrPseudo->IsGeneratedContentContainerForAfter()) {
aElementOrPseudo = aElementOrPseudo->GetParent()->AsElement();
return CSSPseudoElementType::after;
}
return CSSPseudoElementType::NotPseudo;
}
bool
Gecko_GetAnimationRule(RawGeckoElementBorrowed aElement,
EffectCompositor::CascadeLevel aCascadeLevel,
RawServoAnimationValueMapBorrowedMut aAnimationValues)
{
MOZ_ASSERT(aElement);
nsIDocument* doc = aElement->GetComposedDoc();
if (!doc) {
return false;
}
nsPresContext* presContext = doc->GetPresContext();
if (!presContext || !presContext->IsDynamic()) {
// For print or print preview, ignore animations.
return false;
}
CSSPseudoElementType pseudoType =
GetPseudoTypeFromElementForAnimation(aElement);
return presContext->EffectCompositor()
->GetServoAnimationRule(aElement,
pseudoType,
aCascadeLevel,
aAnimationValues);
}
bool
Gecko_StyleAnimationsEquals(RawGeckoStyleAnimationListBorrowed aA,
RawGeckoStyleAnimationListBorrowed aB)
{
return *aA == *aB;
}
void
Gecko_CopyAnimationNames(RawGeckoStyleAnimationListBorrowedMut aDest,
RawGeckoStyleAnimationListBorrowed aSrc)
{
size_t srcLength = aSrc->Length();
aDest->EnsureLengthAtLeast(srcLength);
for (size_t index = 0; index < srcLength; index++) {
(*aDest)[index].SetName((*aSrc)[index].GetName());
}
}
void
Gecko_SetAnimationName(StyleAnimation* aStyleAnimation,
nsAtom* aAtom)
{
MOZ_ASSERT(aStyleAnimation);
aStyleAnimation->SetName(already_AddRefed<nsAtom>(aAtom));
}
void
Gecko_UpdateAnimations(RawGeckoElementBorrowed aElement,
ComputedStyleBorrowedOrNull aOldComputedData,
ComputedStyleBorrowedOrNull aComputedData,
UpdateAnimationsTasks aTasks)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aElement);
if (!aElement->IsInComposedDoc()) {
return;
}
nsPresContext* presContext = nsContentUtils::GetContextForContent(aElement);
if (!presContext || !presContext->IsDynamic()) {
return;
}
nsAutoAnimationMutationBatch mb(aElement->OwnerDoc());
CSSPseudoElementType pseudoType =
GetPseudoTypeFromElementForAnimation(aElement);
if (aTasks & UpdateAnimationsTasks::CSSAnimations) {
presContext->AnimationManager()->
UpdateAnimations(const_cast<dom::Element*>(aElement), pseudoType,
aComputedData);
}
// aComputedData might be nullptr if the target element is now in a
// display:none subtree. We still call Gecko_UpdateAnimations in this case
// because we need to stop CSS animations in the display:none subtree.
// However, we don't need to update transitions since they are stopped by
// RestyleManager::AnimationsWithDestroyedFrame so we just return early
// here.
if (!aComputedData) {
return;
}
if (aTasks & UpdateAnimationsTasks::CSSTransitions) {
MOZ_ASSERT(aOldComputedData);
presContext->TransitionManager()->
UpdateTransitions(const_cast<dom::Element*>(aElement), pseudoType,
*aOldComputedData,
*aComputedData);
}
if (aTasks & UpdateAnimationsTasks::EffectProperties) {
presContext->EffectCompositor()->UpdateEffectProperties(
aComputedData, const_cast<dom::Element*>(aElement), pseudoType);
}
if (aTasks & UpdateAnimationsTasks::CascadeResults) {
EffectSet* effectSet = EffectSet::GetEffectSet(aElement, pseudoType);
// CSS animations/transitions might have been destroyed as part of the above
// steps so before updating cascade results, we check if there are still any
// animations to update.
if (effectSet) {
// We call UpdateCascadeResults directly (intead of
// MaybeUpdateCascadeResults) since we know for sure that the cascade has
// changed, but we were unable to call MarkCascadeUpdated when we noticed
// it since we avoid mutating state as part of the Servo parallel
// traversal.
presContext->EffectCompositor()
->UpdateCascadeResults(*effectSet,
const_cast<Element*>(aElement),
pseudoType);
}
}
if (aTasks & UpdateAnimationsTasks::DisplayChangedFromNone) {
presContext->EffectCompositor()
->RequestRestyle(const_cast<Element*>(aElement),
pseudoType,
EffectCompositor::RestyleType::Standard,
EffectCompositor::CascadeLevel::Animations);
}
}
size_t
Gecko_GetAnimationEffectCount(RawGeckoElementBorrowed aElementOrPseudo)
{
CSSPseudoElementType pseudoType =
GetPseudoTypeFromElementForAnimation(aElementOrPseudo);
EffectSet* effectSet = EffectSet::GetEffectSet(aElementOrPseudo, pseudoType);
return effectSet ? effectSet->Count() : 0;
}
bool
Gecko_ElementHasAnimations(RawGeckoElementBorrowed aElement)
{
CSSPseudoElementType pseudoType =
GetPseudoTypeFromElementForAnimation(aElement);
return !!EffectSet::GetEffectSet(aElement, pseudoType);
}
bool
Gecko_ElementHasCSSAnimations(RawGeckoElementBorrowed aElement)
{
CSSPseudoElementType pseudoType =
GetPseudoTypeFromElementForAnimation(aElement);
nsAnimationManager::CSSAnimationCollection* collection =
nsAnimationManager::CSSAnimationCollection
::GetAnimationCollection(aElement, pseudoType);
return collection && !collection->mAnimations.IsEmpty();
}
bool
Gecko_ElementHasCSSTransitions(RawGeckoElementBorrowed aElement)
{
CSSPseudoElementType pseudoType =
GetPseudoTypeFromElementForAnimation(aElement);
nsTransitionManager::CSSTransitionCollection* collection =
nsTransitionManager::CSSTransitionCollection
::GetAnimationCollection(aElement, pseudoType);
return collection && !collection->mAnimations.IsEmpty();
}
size_t
Gecko_ElementTransitions_Length(RawGeckoElementBorrowed aElement)
{
CSSPseudoElementType pseudoType =
GetPseudoTypeFromElementForAnimation(aElement);
nsTransitionManager::CSSTransitionCollection* collection =
nsTransitionManager::CSSTransitionCollection
::GetAnimationCollection(aElement, pseudoType);
return collection ? collection->mAnimations.Length() : 0;
}
static CSSTransition*
GetCurrentTransitionAt(RawGeckoElementBorrowed aElement, size_t aIndex)
{
CSSPseudoElementType pseudoType =
GetPseudoTypeFromElementForAnimation(aElement);
nsTransitionManager::CSSTransitionCollection* collection =
nsTransitionManager::CSSTransitionCollection
::GetAnimationCollection(aElement, pseudoType);
if (!collection) {
return nullptr;
}
nsTArray<RefPtr<CSSTransition>>& transitions = collection->mAnimations;
return aIndex < transitions.Length()
? transitions[aIndex].get()
: nullptr;
}
nsCSSPropertyID
Gecko_ElementTransitions_PropertyAt(RawGeckoElementBorrowed aElement,
size_t aIndex)
{
CSSTransition* transition = GetCurrentTransitionAt(aElement, aIndex);
return transition ? transition->TransitionProperty()
: nsCSSPropertyID::eCSSProperty_UNKNOWN;
}
RawServoAnimationValueBorrowedOrNull
Gecko_ElementTransitions_EndValueAt(RawGeckoElementBorrowed aElement,
size_t aIndex)
{
CSSTransition* transition = GetCurrentTransitionAt(aElement,
aIndex);
return transition ? transition->ToValue().mServo.get() : nullptr;
}
double
Gecko_GetProgressFromComputedTiming(RawGeckoComputedTimingBorrowed aComputedTiming)
{
return aComputedTiming->mProgress.Value();
}
double
Gecko_GetPositionInSegment(RawGeckoAnimationPropertySegmentBorrowed aSegment,
double aProgress,
ComputedTimingFunction::BeforeFlag aBeforeFlag)
{
MOZ_ASSERT(aSegment->mFromKey < aSegment->mToKey,
"The segment from key should be less than to key");
double positionInSegment = (aProgress - aSegment->mFromKey) /
// To avoid floating precision inaccuracies, make
// sure we calculate both the numerator and
// denominator using double precision.
(double(aSegment->mToKey) - aSegment->mFromKey);
return ComputedTimingFunction::GetPortion(aSegment->mTimingFunction,
positionInSegment,
aBeforeFlag);
}
RawServoAnimationValueBorrowedOrNull
Gecko_AnimationGetBaseStyle(void* aBaseStyles, nsCSSPropertyID aProperty)
{
auto base =
static_cast<nsRefPtrHashtable<nsUint32HashKey, RawServoAnimationValue>*>
(aBaseStyles);
return base->GetWeak(aProperty);
}
void
Gecko_FillAllImageLayers(nsStyleImageLayers* aLayers, uint32_t aMaxLen)
{
aLayers->FillAllLayers(aMaxLen);
}
bool
Gecko_IsDocumentBody(RawGeckoElementBorrowed aElement)
{
nsIDocument* doc = aElement->GetUncomposedDoc();
return doc && doc->GetBodyElement() == aElement;
}
nscolor
Gecko_GetLookAndFeelSystemColor(int32_t aId,
RawGeckoPresContextBorrowed aPresContext)
{
bool useStandinsForNativeColors = aPresContext && !aPresContext->IsChrome();
nscolor result;
LookAndFeel::ColorID colorId = static_cast<LookAndFeel::ColorID>(aId);
AutoWriteLock guard(*sServoFFILock);
LookAndFeel::GetColor(colorId, useStandinsForNativeColors, &result);
return result;
}
bool
Gecko_MatchLang(RawGeckoElementBorrowed aElement,
nsAtom* aOverrideLang,
bool aHasOverrideLang,
const char16_t* aValue)
{
MOZ_ASSERT(!(aOverrideLang && !aHasOverrideLang),
"aHasOverrideLang should only be set when aOverrideLang is null");
MOZ_ASSERT(aValue, "null lang parameter");
if (!aValue || !*aValue) {
return false;
}
// We have to determine the language of the current element. Since
// this is currently no property and since the language is inherited
// from the parent we have to be prepared to look at all parent
// nodes. The language itself is encoded in the LANG attribute.
if (auto* language = aHasOverrideLang ? aOverrideLang : aElement->GetLang()) {
return nsStyleUtil::DashMatchCompare(nsDependentAtomString(language),
nsDependentString(aValue),
nsASCIICaseInsensitiveStringComparator());
}
// Try to get the language from the HTTP header or if this
// is missing as well from the preferences.
// The content language can be a comma-separated list of
// language codes.
nsAutoString language;
aElement->OwnerDoc()->GetContentLanguage(language);
nsDependentString langString(aValue);
language.StripWhitespace();
for (auto const& lang : language.Split(char16_t(','))) {
if (nsStyleUtil::DashMatchCompare(lang,
langString,
nsASCIICaseInsensitiveStringComparator())) {
return true;
}
}
return false;
}
nsAtom*
Gecko_GetXMLLangValue(RawGeckoElementBorrowed aElement)
{
const nsAttrValue* attr =
aElement->GetParsedAttr(nsGkAtoms::lang, kNameSpaceID_XML);
if (!attr) {
return nullptr;
}
MOZ_ASSERT(attr->Type() == nsAttrValue::eAtom);
RefPtr<nsAtom> atom = attr->GetAtomValue();
return atom.forget().take();
}
nsIDocument::DocumentTheme
Gecko_GetDocumentLWTheme(const nsIDocument* aDocument)
{
return aDocument->ThreadSafeGetDocumentLWTheme();
}
bool
Gecko_IsTableBorderNonzero(RawGeckoElementBorrowed aElement)
{
if (!aElement->IsHTMLElement(nsGkAtoms::table)) {
return false;
}
const nsAttrValue *val = aElement->GetParsedAttr(nsGkAtoms::border);
return val && (val->Type() != nsAttrValue::eInteger ||
val->GetIntegerValue() != 0);
}
bool
Gecko_IsBrowserFrame(RawGeckoElementBorrowed aElement)
{
nsIMozBrowserFrame* browserFrame =
const_cast<Element*>(aElement)->GetAsMozBrowserFrame();
return browserFrame && browserFrame->GetReallyIsBrowser();
}
template <typename Implementor>
static nsAtom*
LangValue(Implementor* aElement)
{
// TODO(emilio): Deduplicate a bit with nsIContent::GetLang().
const nsAttrValue* attr =
aElement->GetParsedAttr(nsGkAtoms::lang, kNameSpaceID_XML);
if (!attr && aElement->SupportsLangAttr()) {
attr = aElement->GetParsedAttr(nsGkAtoms::lang);
}
if (!attr) {
return nullptr;
}
MOZ_ASSERT(attr->Type() == nsAttrValue::eAtom);
RefPtr<nsAtom> atom = attr->GetAtomValue();
return atom.forget().take();
}
template <typename Implementor, typename MatchFn>
static bool
DoMatch(Implementor* aElement, nsAtom* aNS, nsAtom* aName, MatchFn aMatch)
{
if (MOZ_LIKELY(aNS)) {
int32_t ns = aNS == nsGkAtoms::_empty
? kNameSpaceID_None
: nsContentUtils::NameSpaceManager()->GetNameSpaceID(
aNS, aElement->IsInChromeDocument());
MOZ_ASSERT(ns == nsContentUtils::NameSpaceManager()->GetNameSpaceID(
aNS, aElement->IsInChromeDocument()));
NS_ENSURE_TRUE(ns != kNameSpaceID_Unknown, false);
const nsAttrValue* value = aElement->GetParsedAttr(aName, ns);
return value && aMatch(value);
}
// No namespace means any namespace - we have to check them all. :-(
BorrowedAttrInfo attrInfo;
for (uint32_t i = 0; (attrInfo = aElement->GetAttrInfoAt(i)); ++i) {
if (attrInfo.mName->LocalName() != aName) {
continue;
}
if (aMatch(attrInfo.mValue)) {
return true;
}
}
return false;
}
template <typename Implementor>
static bool
HasAttr(Implementor* aElement, nsAtom* aNS, nsAtom* aName)
{
auto match = [](const nsAttrValue* aValue) { return true; };
return DoMatch(aElement, aNS, aName, match);
}
template <typename Implementor>
static bool
AttrEquals(Implementor* aElement, nsAtom* aNS, nsAtom* aName, nsAtom* aStr,
bool aIgnoreCase)
{
auto match = [aStr, aIgnoreCase](const nsAttrValue* aValue) {
return aValue->Equals(aStr, aIgnoreCase ? eIgnoreCase : eCaseMatters);
};
return DoMatch(aElement, aNS, aName, match);
}
#define WITH_COMPARATOR(ignore_case_, c_, expr_) \
if (ignore_case_) { \
const nsCaseInsensitiveStringComparator c_; \
return expr_; \
} else { \
const nsDefaultStringComparator c_; \
return expr_; \
}
template <typename Implementor>
static bool
AttrDashEquals(Implementor* aElement, nsAtom* aNS, nsAtom* aName,
nsAtom* aStr, bool aIgnoreCase)
{
auto match = [aStr, aIgnoreCase](const nsAttrValue* aValue) {
nsAutoString str;