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
|
/* -*- 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/. */
#include "AccessCheck.h"
#include "base/basictypes.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/ContentEvents.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/InternalMutationEvent.h"
#include "mozilla/dom/Performance.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/MiscEvents.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/PointerLockManager.h"
#include "mozilla/Preferences.h"
#include "mozilla/PresShell.h"
#include "mozilla/TextEvents.h"
#include "mozilla/TouchEvents.h"
#include "mozilla/ViewportUtils.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/DocumentInlines.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/ShadowRoot.h"
#include "mozilla/dom/WorkerScope.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/SVGUtils.h"
#include "mozilla/SVGOuterSVGFrame.h"
#include "nsContentUtils.h"
#include "nsCOMPtr.h"
#include "nsDeviceContext.h"
#include "nsError.h"
#include "nsGlobalWindowInner.h"
#include "nsIFrame.h"
#include "nsIContent.h"
#include "nsIContentInlines.h"
#include "nsIScrollableFrame.h"
#include "nsJSEnvironment.h"
#include "nsLayoutUtils.h"
#include "nsPIWindowRoot.h"
#include "nsRFPService.h"
namespace mozilla::dom {
Event::Event(EventTarget* aOwner, nsPresContext* aPresContext,
WidgetEvent* aEvent) {
ConstructorInit(aOwner, aPresContext, aEvent);
}
Event::Event(nsPIDOMWindowInner* aParent) {
ConstructorInit(nsGlobalWindowInner::Cast(aParent), nullptr, nullptr);
}
void Event::ConstructorInit(EventTarget* aOwner, nsPresContext* aPresContext,
WidgetEvent* aEvent) {
SetOwner(aOwner);
mIsMainThreadEvent = NS_IsMainThread();
if (mIsMainThreadEvent) {
mRefCnt.SetIsOnMainThread();
}
mPrivateDataDuplicated = false;
mWantsPopupControlCheck = false;
if (aEvent) {
mEvent = aEvent;
mEventIsInternal = false;
} else {
mEventIsInternal = true;
/*
A derived class might want to allocate its own type of aEvent
(derived from WidgetEvent). To do this, it should take care to pass
a non-nullptr aEvent to this ctor, e.g.:
FooEvent::FooEvent(..., WidgetEvent* aEvent)
: Event(..., aEvent ? aEvent : new WidgetEvent())
Then, to override the mEventIsInternal assignments done by the
base ctor, it should do this in its own ctor:
FooEvent::FooEvent(..., WidgetEvent* aEvent)
...
{
...
if (aEvent) {
mEventIsInternal = false;
}
else {
mEventIsInternal = true;
}
...
}
*/
mEvent = new WidgetEvent(false, eVoidEvent);
}
InitPresContextData(aPresContext);
}
void Event::InitPresContextData(nsPresContext* aPresContext) {
mPresContext = aPresContext;
// Get the explicit original target (if it's anonymous make it null)
{
nsCOMPtr<nsIContent> content = GetTargetFromFrame();
if (content && !content->IsInNativeAnonymousSubtree()) {
mExplicitOriginalTarget = std::move(content);
} else {
mExplicitOriginalTarget = nullptr;
}
}
}
Event::~Event() {
NS_ASSERT_OWNINGTHREAD(Event);
if (mEventIsInternal && mEvent) {
delete mEvent;
}
}
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Event)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_ENTRY(Event)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(Event)
NS_IMPL_CYCLE_COLLECTING_RELEASE(Event)
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Event)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Event)
if (tmp->mEventIsInternal) {
tmp->mEvent->mTarget = nullptr;
tmp->mEvent->mCurrentTarget = nullptr;
tmp->mEvent->mOriginalTarget = nullptr;
tmp->mEvent->mRelatedTarget = nullptr;
tmp->mEvent->mOriginalRelatedTarget = nullptr;
switch (tmp->mEvent->mClass) {
case eDragEventClass: {
WidgetDragEvent* dragEvent = tmp->mEvent->AsDragEvent();
dragEvent->mDataTransfer = nullptr;
break;
}
case eClipboardEventClass:
tmp->mEvent->AsClipboardEvent()->mClipboardData = nullptr;
break;
case eEditorInputEventClass: {
InternalEditorInputEvent* inputEvent =
tmp->mEvent->AsEditorInputEvent();
inputEvent->mDataTransfer = nullptr;
inputEvent->mTargetRanges.Clear();
break;
}
case eMutationEventClass:
tmp->mEvent->AsMutationEvent()->mRelatedNode = nullptr;
break;
default:
break;
}
if (WidgetMouseEvent* mouseEvent = tmp->mEvent->AsMouseEvent()) {
mouseEvent->mClickTarget = nullptr;
}
}
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPresContext);
NS_IMPL_CYCLE_COLLECTION_UNLINK(mExplicitOriginalTarget);
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner);
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Event)
if (tmp->mEventIsInternal) {
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEvent->mTarget)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEvent->mCurrentTarget)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEvent->mOriginalTarget)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEvent->mRelatedTarget)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEvent->mOriginalRelatedTarget);
switch (tmp->mEvent->mClass) {
case eDragEventClass: {
WidgetDragEvent* dragEvent = tmp->mEvent->AsDragEvent();
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mEvent->mDataTransfer");
cb.NoteXPCOMChild(dragEvent->mDataTransfer);
break;
}
case eClipboardEventClass:
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mEvent->mClipboardData");
cb.NoteXPCOMChild(tmp->mEvent->AsClipboardEvent()->mClipboardData);
break;
case eEditorInputEventClass:
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mEvent->mDataTransfer");
cb.NoteXPCOMChild(tmp->mEvent->AsEditorInputEvent()->mDataTransfer);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(
mEvent->AsEditorInputEvent()->mTargetRanges);
break;
case eMutationEventClass:
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mEvent->mRelatedNode");
cb.NoteXPCOMChild(tmp->mEvent->AsMutationEvent()->mRelatedNode);
break;
default:
break;
}
if (WidgetMouseEvent* mouseEvent = tmp->mEvent->AsMouseEvent()) {
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mEvent->mClickTarget");
cb.NoteXPCOMChild(mouseEvent->mClickTarget);
}
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPresContext)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mExplicitOriginalTarget)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
JSObject* Event::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
return WrapObjectInternal(aCx, aGivenProto);
}
JSObject* Event::WrapObjectInternal(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return Event_Binding::Wrap(aCx, this, aGivenProto);
}
void Event::GetType(nsAString& aType) const {
GetWidgetEventType(mEvent, aType);
}
EventTarget* Event::GetTarget() const { return mEvent->GetDOMEventTarget(); }
already_AddRefed<Document> Event::GetDocument() const {
nsCOMPtr<EventTarget> eventTarget = GetTarget();
if (!eventTarget) {
return nullptr;
}
nsIGlobalObject* global = eventTarget->GetOwnerGlobal();
if (!global) {
return nullptr;
}
nsPIDOMWindowInner* win = global->GetAsInnerWindow();
if (!win) {
return nullptr;
}
nsCOMPtr<Document> doc;
doc = win->GetExtantDoc();
return doc.forget();
}
EventTarget* Event::GetCurrentTarget() const {
return mEvent->GetCurrentDOMEventTarget();
}
void Event::ComposedPath(nsTArray<RefPtr<EventTarget>>& aPath) {
EventDispatcher::GetComposedPathFor(mEvent, aPath);
}
//
// Get the actual event target node (may have been retargeted for mouse events)
//
already_AddRefed<nsIContent> Event::GetTargetFromFrame() {
if (!mPresContext) {
return nullptr;
}
// Get the mTarget frame (have to get the ESM first)
nsIFrame* targetFrame = mPresContext->EventStateManager()->GetEventTarget();
if (!targetFrame) {
return nullptr;
}
// get the real content
nsCOMPtr<nsIContent> realEventContent;
targetFrame->GetContentForEvent(mEvent, getter_AddRefs(realEventContent));
return realEventContent.forget();
}
EventTarget* Event::GetExplicitOriginalTarget() const {
if (mExplicitOriginalTarget) {
return mExplicitOriginalTarget;
}
return GetTarget();
}
EventTarget* Event::GetOriginalTarget() const {
return mEvent->GetOriginalDOMEventTarget();
}
EventTarget* Event::GetComposedTarget() const {
EventTarget* et = GetOriginalTarget();
nsIContent* content = nsIContent::FromEventTargetOrNull(et);
if (!content) {
return et;
}
nsIContent* nonChrome = content->FindFirstNonChromeOnlyAccessContent();
return nonChrome ? static_cast<EventTarget*>(nonChrome)
: static_cast<EventTarget*>(content->GetComposedDoc());
}
void Event::SetTrusted(bool aTrusted) { mEvent->mFlags.mIsTrusted = aTrusted; }
bool Event::ShouldIgnoreChromeEventTargetListener() const {
MOZ_ASSERT(NS_IsMainThread());
if (!XRE_IsParentProcess()) {
return false;
}
if (EventTarget* currentTarget = GetCurrentTarget();
NS_WARN_IF(!currentTarget) || !currentTarget->IsRootWindow()) {
return false;
}
EventTarget* et = GetOriginalTarget();
if (NS_WARN_IF(!et)) {
return false;
}
nsIGlobalObject* global = et->GetOwnerGlobal();
if (NS_WARN_IF(!global)) {
return false;
}
nsPIDOMWindowInner* win = global->GetAsInnerWindow();
if (NS_WARN_IF(!win)) {
return false;
}
BrowsingContext* bc = win->GetBrowsingContext();
if (NS_WARN_IF(!bc)) {
return false;
}
// If this is a content event on an nsWindowRoot, then we also handle this in
// InProcessBrowserChildMessageManager, so we can ignore this event.
return bc->IsContent();
}
bool Event::Init(mozilla::dom::EventTarget* aGlobal) {
if (!mIsMainThreadEvent) {
return IsCurrentThreadRunningChromeWorker();
}
bool trusted = false;
if (aGlobal) {
if (nsPIDOMWindowInner* w = aGlobal->GetAsInnerWindow()) {
if (Document* d = w->GetExtantDoc()) {
trusted = nsContentUtils::IsChromeDoc(d);
if (nsPresContext* presContext = d->GetPresContext()) {
InitPresContextData(presContext);
}
}
}
}
return trusted;
}
// static
already_AddRefed<Event> Event::Constructor(const GlobalObject& aGlobal,
const nsAString& aType,
const EventInit& aParam) {
nsCOMPtr<mozilla::dom::EventTarget> t =
do_QueryInterface(aGlobal.GetAsSupports());
return Constructor(t, aType, aParam);
}
// static
already_AddRefed<Event> Event::Constructor(EventTarget* aEventTarget,
const nsAString& aType,
const EventInit& aParam) {
RefPtr<Event> e = new Event(aEventTarget, nullptr, nullptr);
bool trusted = e->Init(aEventTarget);
e->InitEvent(aType, aParam.mBubbles, aParam.mCancelable);
e->SetTrusted(trusted);
e->SetComposed(aParam.mComposed);
return e.forget();
}
uint16_t Event::EventPhase() const {
if ((mEvent->mCurrentTarget && mEvent->mCurrentTarget == mEvent->mTarget) ||
mEvent->mFlags.mInTargetPhase) {
return Event_Binding::AT_TARGET;
}
if (mEvent->mFlags.mInCapturePhase) {
return Event_Binding::CAPTURING_PHASE;
}
if (mEvent->mFlags.mInBubblingPhase) {
return Event_Binding::BUBBLING_PHASE;
}
return Event_Binding::NONE;
}
void Event::StopPropagation() { mEvent->StopPropagation(); }
void Event::StopImmediatePropagation() { mEvent->StopImmediatePropagation(); }
void Event::StopCrossProcessForwarding() {
mEvent->StopCrossProcessForwarding();
}
void Event::PreventDefault() {
// This method is called only from C++ code which must handle default action
// of this event. So, pass true always.
PreventDefaultInternal(true);
}
void Event::PreventDefault(JSContext* aCx, CallerType aCallerType) {
// Note that at handling default action, another event may be dispatched.
// Then, JS in content mey be call preventDefault()
// even in the event is in system event group. Therefore, don't refer
// mInSystemGroup here.
nsIPrincipal* principal =
mIsMainThreadEvent ? nsContentUtils::SubjectPrincipal(aCx) : nullptr;
PreventDefaultInternal(aCallerType == CallerType::System, principal);
}
void Event::PreventDefaultInternal(bool aCalledByDefaultHandler,
nsIPrincipal* aPrincipal) {
if (!mEvent->mFlags.mCancelable) {
return;
}
if (mEvent->mFlags.mInPassiveListener) {
if (nsPIDOMWindowInner* win = mOwner->GetAsInnerWindow()) {
if (Document* doc = win->GetExtantDoc()) {
if (!doc->HasWarnedAbout(
Document::ePreventDefaultFromPassiveListener)) {
AutoTArray<nsString, 1> params;
GetType(*params.AppendElement());
doc->WarnOnceAbout(Document::ePreventDefaultFromPassiveListener,
false, params);
}
}
}
return;
}
mEvent->PreventDefault(aCalledByDefaultHandler, aPrincipal);
if (!IsTrusted()) {
return;
}
if (mEvent->mClass == eDragEventClass) {
UpdateDefaultPreventedOnContentForDragEvent();
}
}
void Event::UpdateDefaultPreventedOnContentForDragEvent() {
WidgetDragEvent* dragEvent = mEvent->AsDragEvent();
if (!dragEvent) {
return;
}
nsIPrincipal* principal = nullptr;
// Since we now have HTMLEditorEventListener registered on nsWindowRoot,
// mCurrentTarget could be nsWindowRoot, so we need to use
// mTarget if that's the case.
MOZ_ASSERT_IF(dragEvent->mInHTMLEditorEventListener,
mEvent->mCurrentTarget->IsRootWindow());
EventTarget* target = dragEvent->mInHTMLEditorEventListener
? mEvent->mTarget
: mEvent->mCurrentTarget;
nsINode* node = nsINode::FromEventTargetOrNull(target);
if (node) {
principal = node->NodePrincipal();
} else {
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(target);
if (sop) {
principal = sop->GetPrincipal();
}
}
if (principal && !principal->IsSystemPrincipal()) {
dragEvent->mDefaultPreventedOnContent = true;
}
}
void Event::SetEventType(const nsAString& aEventTypeArg) {
mEvent->mSpecifiedEventTypeString.Truncate();
if (mIsMainThreadEvent) {
mEvent->mSpecifiedEventType = nsContentUtils::GetEventMessageAndAtom(
aEventTypeArg, mEvent->mClass, &(mEvent->mMessage));
mEvent->SetDefaultComposed();
} else {
mEvent->mSpecifiedEventType = NS_Atomize(u"on"_ns + aEventTypeArg);
mEvent->mMessage = eUnidentifiedEvent;
mEvent->SetComposed(aEventTypeArg);
}
mEvent->SetDefaultComposedInNativeAnonymousContent();
}
already_AddRefed<EventTarget> Event::EnsureWebAccessibleRelatedTarget(
EventTarget* aRelatedTarget) {
nsCOMPtr<EventTarget> relatedTarget = aRelatedTarget;
if (relatedTarget) {
nsIContent* content = nsIContent::FromEventTarget(relatedTarget);
if (content && content->ChromeOnlyAccess() &&
!nsContentUtils::CanAccessNativeAnon()) {
content = content->FindFirstNonChromeOnlyAccessContent();
relatedTarget = content;
}
if (relatedTarget) {
relatedTarget = relatedTarget->GetTargetForDOMEvent();
}
}
return relatedTarget.forget();
}
void Event::InitEvent(const nsAString& aEventTypeArg,
mozilla::CanBubble aCanBubbleArg,
mozilla::Cancelable aCancelableArg,
mozilla::Composed aComposedArg) {
// Make sure this event isn't already being dispatched.
NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
if (IsTrusted()) {
// Ensure the caller is permitted to dispatch trusted DOM events.
if (!nsContentUtils::ThreadsafeIsCallerChrome()) {
SetTrusted(false);
}
}
SetEventType(aEventTypeArg);
mEvent->mFlags.mBubbles = aCanBubbleArg == CanBubble::eYes;
mEvent->mFlags.mCancelable = aCancelableArg == Cancelable::eYes;
if (aComposedArg != Composed::eDefault) {
mEvent->mFlags.mComposed = aComposedArg == Composed::eYes;
}
mEvent->mFlags.mDefaultPrevented = false;
mEvent->mFlags.mDefaultPreventedByContent = false;
mEvent->mFlags.mDefaultPreventedByChrome = false;
mEvent->mFlags.mPropagationStopped = false;
mEvent->mFlags.mImmediatePropagationStopped = false;
// Clearing the old targets, so that the event is targeted correctly when
// re-dispatching it.
mEvent->mTarget = nullptr;
mEvent->mOriginalTarget = nullptr;
}
void Event::DuplicatePrivateData() {
NS_ASSERTION(mEvent, "No WidgetEvent for Event duplication!");
if (mEventIsInternal) {
return;
}
mEvent = mEvent->Duplicate();
mPresContext = nullptr;
mEventIsInternal = true;
mPrivateDataDuplicated = true;
}
void Event::SetTarget(EventTarget* aTarget) { mEvent->mTarget = aTarget; }
bool Event::IsDispatchStopped() { return mEvent->PropagationStopped(); }
WidgetEvent* Event::WidgetEventPtr() { return mEvent; }
// static
Maybe<CSSIntPoint> Event::GetScreenCoords(nsPresContext* aPresContext,
WidgetEvent* aEvent,
LayoutDeviceIntPoint aPoint) {
if (PointerLockManager::IsLocked()) {
return Some(EventStateManager::sLastScreenPoint);
}
if (!aEvent || (aEvent->mClass != eMouseEventClass &&
aEvent->mClass != eMouseScrollEventClass &&
aEvent->mClass != eWheelEventClass &&
aEvent->mClass != ePointerEventClass &&
aEvent->mClass != eTouchEventClass &&
aEvent->mClass != eDragEventClass &&
aEvent->mClass != eSimpleGestureEventClass)) {
return Nothing();
}
// Doing a straight conversion from LayoutDeviceIntPoint to CSSIntPoint
// seem incorrect, but it is needed to maintain legacy functionality.
WidgetGUIEvent* guiEvent = aEvent->AsGUIEvent();
if (!aPresContext || !(guiEvent && guiEvent->mWidget)) {
return Some(CSSIntPoint(aPoint.x, aPoint.y));
}
// (Potentially) transform the point from the coordinate space of an
// out-of-process iframe to the coordinate space of the native
// window. The transform can only be applied to a point whose components
// are floating-point values, so convert the integer point first, then
// transform, and then round the result back to an integer point.
LayoutDevicePoint floatPoint(aPoint);
LayoutDevicePoint topLevelPoint =
guiEvent->mWidget->WidgetToTopLevelWidgetTransform().TransformPoint(
floatPoint);
LayoutDeviceIntPoint rounded = RoundedToInt(topLevelPoint);
nsPoint pt = LayoutDevicePixel::ToAppUnits(
rounded, aPresContext->DeviceContext()->AppUnitsPerDevPixel());
pt += LayoutDevicePixel::ToAppUnits(
guiEvent->mWidget->TopLevelWidgetToScreenOffset(),
aPresContext->DeviceContext()->AppUnitsPerDevPixel());
return Some(CSSPixel::FromAppUnitsRounded(pt));
}
// static
CSSIntPoint Event::GetPageCoords(nsPresContext* aPresContext,
WidgetEvent* aEvent,
LayoutDeviceIntPoint aPoint,
CSSIntPoint aDefaultPoint) {
CSSIntPoint pagePoint =
Event::GetClientCoords(aPresContext, aEvent, aPoint, aDefaultPoint);
// If there is some scrolling, add scroll info to client point.
if (aPresContext && aPresContext->GetPresShell()) {
PresShell* presShell = aPresContext->PresShell();
nsIScrollableFrame* scrollframe =
presShell->GetRootScrollFrameAsScrollable();
if (scrollframe) {
pagePoint +=
CSSIntPoint::FromAppUnitsRounded(scrollframe->GetScrollPosition());
}
}
return pagePoint;
}
// static
CSSIntPoint Event::GetClientCoords(nsPresContext* aPresContext,
WidgetEvent* aEvent,
LayoutDeviceIntPoint aPoint,
CSSIntPoint aDefaultPoint) {
if (PointerLockManager::IsLocked()) {
return EventStateManager::sLastClientPoint;
}
if (!aEvent ||
(aEvent->mClass != eMouseEventClass &&
aEvent->mClass != eMouseScrollEventClass &&
aEvent->mClass != eWheelEventClass &&
aEvent->mClass != eTouchEventClass &&
aEvent->mClass != eDragEventClass &&
aEvent->mClass != ePointerEventClass &&
aEvent->mClass != eSimpleGestureEventClass) ||
!aPresContext || !aEvent->AsGUIEvent()->mWidget) {
return aDefaultPoint;
}
PresShell* presShell = aPresContext->GetPresShell();
if (!presShell) {
return CSSIntPoint(0, 0);
}
nsIFrame* rootFrame = presShell->GetRootFrame();
if (!rootFrame) {
return CSSIntPoint(0, 0);
}
nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(
aEvent, aPoint, RelativeTo{rootFrame});
return CSSIntPoint::FromAppUnitsRounded(pt);
}
// static
CSSIntPoint Event::GetOffsetCoords(nsPresContext* aPresContext,
WidgetEvent* aEvent,
LayoutDeviceIntPoint aPoint,
CSSIntPoint aDefaultPoint) {
if (!aEvent->mTarget) {
return GetPageCoords(aPresContext, aEvent, aPoint, aDefaultPoint);
}
nsCOMPtr<nsIContent> content = nsIContent::FromEventTarget(aEvent->mTarget);
if (!content || !aPresContext) {
return CSSIntPoint();
}
RefPtr<PresShell> presShell = aPresContext->GetPresShell();
if (!presShell) {
return CSSIntPoint();
}
presShell->FlushPendingNotifications(FlushType::Layout);
nsIFrame* frame = content->GetPrimaryFrame();
if (!frame) {
return CSSIntPoint();
}
// For compat, see https://github.com/w3c/csswg-drafts/issues/1508. In SVG we
// just return the coordinates of the outer SVG box. This is all kinda
// unfortunate.
if (frame->HasAnyStateBits(NS_FRAME_SVG_LAYOUT) &&
StaticPrefs::dom_events_offset_in_svg_relative_to_svg_root()) {
frame = SVGUtils::GetOuterSVGFrame(frame);
if (!frame) {
return CSSIntPoint();
}
}
nsIFrame* rootFrame = presShell->GetRootFrame();
if (!rootFrame) {
return CSSIntPoint();
}
CSSIntPoint clientCoords =
GetClientCoords(aPresContext, aEvent, aPoint, aDefaultPoint);
nsPoint pt = CSSPixel::ToAppUnits(clientCoords);
if (nsLayoutUtils::TransformPoint(RelativeTo{rootFrame}, RelativeTo{frame},
pt) == nsLayoutUtils::TRANSFORM_SUCCEEDED) {
pt -= frame->GetPaddingRectRelativeToSelf().TopLeft();
return CSSPixel::FromAppUnitsRounded(pt);
}
return CSSIntPoint();
}
// To be called ONLY by Event::GetType (which has the additional
// logic for handling user-defined events).
// static
const char16_t* Event::GetEventName(EventMessage aEventType) {
switch (aEventType) {
#define MESSAGE_TO_EVENT(name_, _message, _type, _struct) \
case _message: \
return u"" #name_;
#include "mozilla/EventNameList.h"
#undef MESSAGE_TO_EVENT
default:
break;
}
// XXXldb We can hit this case for WidgetEvent objects that we didn't
// create and that are not user defined events since this function and
// SetEventType are incomplete. (But fixing that requires fixing the
// arrays in nsEventListenerManager too, since the events for which
// this is a problem generally *are* created by Event.)
return nullptr;
}
bool Event::DefaultPrevented(CallerType aCallerType) const {
NS_ENSURE_TRUE(mEvent, false);
// If preventDefault() has never been called, just return false.
if (!mEvent->DefaultPrevented()) {
return false;
}
// If preventDefault() has been called by content, return true. Otherwise,
// i.e., preventDefault() has been called by chrome, return true only when
// this is called by chrome.
return mEvent->DefaultPreventedByContent() ||
aCallerType == CallerType::System;
}
bool Event::ReturnValue(CallerType aCallerType) const {
return !DefaultPrevented(aCallerType);
}
void Event::SetReturnValue(bool aReturnValue, CallerType aCallerType) {
if (!aReturnValue) {
PreventDefaultInternal(aCallerType == CallerType::System);
}
}
double Event::TimeStamp() {
if (mEvent->mTimeStamp.IsNull()) {
return 0.0;
}
if (mIsMainThreadEvent) {
if (NS_WARN_IF(!mOwner)) {
return 0.0;
}
nsPIDOMWindowInner* win = mOwner->GetAsInnerWindow();
if (NS_WARN_IF(!win)) {
return 0.0;
}
Performance* perf = win->GetPerformance();
if (NS_WARN_IF(!perf)) {
return 0.0;
}
double ret =
perf->GetDOMTiming()->TimeStampToDOMHighRes(mEvent->mTimeStamp);
MOZ_ASSERT(mOwner->PrincipalOrNull());
return nsRFPService::ReduceTimePrecisionAsMSecs(
ret, perf->GetRandomTimelineSeed(), perf->GetRTPCallerType());
}
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
double ret = workerPrivate->TimeStampToDOMHighRes(mEvent->mTimeStamp);
return nsRFPService::ReduceTimePrecisionAsMSecs(
ret, workerPrivate->GetRandomTimelineSeed(),
workerPrivate->GlobalScope()->GetRTPCallerType());
}
void Event::Serialize(IPC::MessageWriter* aWriter,
bool aSerializeInterfaceType) {
if (aSerializeInterfaceType) {
IPC::WriteParam(aWriter, u"event"_ns);
}
nsString type;
GetType(type);
IPC::WriteParam(aWriter, type);
IPC::WriteParam(aWriter, Bubbles());
IPC::WriteParam(aWriter, Cancelable());
IPC::WriteParam(aWriter, IsTrusted());
IPC::WriteParam(aWriter, Composed());
// No timestamp serialization for now!
}
bool Event::Deserialize(IPC::MessageReader* aReader) {
nsString type;
NS_ENSURE_TRUE(IPC::ReadParam(aReader, &type), false);
bool bubbles = false;
NS_ENSURE_TRUE(IPC::ReadParam(aReader, &bubbles), false);
bool cancelable = false;
NS_ENSURE_TRUE(IPC::ReadParam(aReader, &cancelable), false);
bool trusted = false;
NS_ENSURE_TRUE(IPC::ReadParam(aReader, &trusted), false);
bool composed = false;
NS_ENSURE_TRUE(IPC::ReadParam(aReader, &composed), false);
InitEvent(type, bubbles, cancelable);
SetTrusted(trusted);
SetComposed(composed);
return true;
}
void Event::SetOwner(EventTarget* aOwner) {
mOwner = nullptr;
if (!aOwner) {
return;
}
if (nsINode* n = aOwner->GetAsNode()) {
mOwner = n->OwnerDoc()->GetScopeObject();
return;
}
if (nsPIDOMWindowInner* w = aOwner->GetAsInnerWindow()) {
mOwner = w->AsGlobal();
return;
}
nsCOMPtr<DOMEventTargetHelper> eth = do_QueryInterface(aOwner);
if (eth) {
mOwner = eth->GetParentObject();
return;
}
#ifdef DEBUG
nsCOMPtr<nsPIWindowRoot> root = do_QueryInterface(aOwner);
MOZ_ASSERT(root, "Unexpected EventTarget!");
#endif
}
void Event::GetWidgetEventType(WidgetEvent* aEvent, nsAString& aType) {
if (!aEvent->mSpecifiedEventTypeString.IsEmpty()) {
aType = aEvent->mSpecifiedEventTypeString;
return;
}
const char16_t* name = GetEventName(aEvent->mMessage);
if (name) {
aType.AssignLiteral(name, nsString::char_traits::length(name));
return;
} else if (aEvent->mMessage == eUnidentifiedEvent &&
aEvent->mSpecifiedEventType) {
// Remove "on"
aType = Substring(nsDependentAtomString(aEvent->mSpecifiedEventType), 2);
aEvent->mSpecifiedEventTypeString = aType;
return;
}
aType.Truncate();
}
bool Event::IsDragExitEnabled(JSContext* aCx, JSObject* aGlobal) {
return StaticPrefs::dom_event_dragexit_enabled() ||
nsContentUtils::IsSystemCaller(aCx);
}
} // namespace mozilla::dom
using namespace mozilla;
using namespace mozilla::dom;
already_AddRefed<Event> NS_NewDOMEvent(EventTarget* aOwner,
nsPresContext* aPresContext,
WidgetEvent* aEvent) {
RefPtr<Event> it = new Event(aOwner, aPresContext, aEvent);
return it.forget();
}
|