Bug 1850713: remove duplicated setting of early hint preloader id in `ScriptLoader...
[gecko.git] / dom / events / DragEvent.cpp
blobaa576e2f8e9f9e753608ba655c274b375fbdae56
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/DragEvent.h"
8 #include "mozilla/dom/MouseEventBinding.h"
9 #include "mozilla/MouseEvents.h"
10 #include "nsContentUtils.h"
11 #include "prtime.h"
13 namespace mozilla::dom {
15 DragEvent::DragEvent(EventTarget* aOwner, nsPresContext* aPresContext,
16 WidgetDragEvent* aEvent)
17 : MouseEvent(
18 aOwner, aPresContext,
19 aEvent ? aEvent : new WidgetDragEvent(false, eVoidEvent, nullptr)) {
20 if (aEvent) {
21 mEventIsInternal = false;
22 } else {
23 mEventIsInternal = true;
24 mEvent->mRefPoint = LayoutDeviceIntPoint(0, 0);
25 mEvent->AsMouseEvent()->mInputSource =
26 MouseEvent_Binding::MOZ_SOURCE_UNKNOWN;
30 void DragEvent::InitDragEvent(const nsAString& aType, bool aCanBubble,
31 bool aCancelable, nsGlobalWindowInner* aView,
32 int32_t aDetail, int32_t aScreenX,
33 int32_t aScreenY, int32_t aClientX,
34 int32_t aClientY, bool aCtrlKey, bool aAltKey,
35 bool aShiftKey, bool aMetaKey, uint16_t aButton,
36 EventTarget* aRelatedTarget,
37 DataTransfer* aDataTransfer) {
38 NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
40 MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable, aView, aDetail,
41 aScreenX, aScreenY, aClientX, aClientY, aCtrlKey,
42 aAltKey, aShiftKey, aMetaKey, aButton,
43 aRelatedTarget);
44 if (mEventIsInternal) {
45 mEvent->AsDragEvent()->mDataTransfer = aDataTransfer;
49 DataTransfer* DragEvent::GetDataTransfer() {
50 // the dataTransfer field of the event caches the DataTransfer associated
51 // with the drag. It is initialized when an attempt is made to retrieve it
52 // rather that when the event is created to avoid duplicating the data when
53 // no listener ever uses it.
54 if (!mEvent || mEvent->mClass != eDragEventClass) {
55 NS_WARNING("Tried to get dataTransfer from non-drag event!");
56 return nullptr;
59 WidgetDragEvent* dragEvent = mEvent->AsDragEvent();
60 // for synthetic events, just use the supplied data transfer object even if
61 // null
62 if (!mEventIsInternal) {
63 nsresult rv = nsContentUtils::SetDataTransferInEvent(dragEvent);
64 NS_ENSURE_SUCCESS(rv, nullptr);
67 return dragEvent->mDataTransfer;
70 // static
71 already_AddRefed<DragEvent> DragEvent::Constructor(
72 const GlobalObject& aGlobal, const nsAString& aType,
73 const DragEventInit& aParam) {
74 nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
75 RefPtr<DragEvent> e = new DragEvent(t, nullptr, nullptr);
76 bool trusted = e->Init(t);
77 e->InitDragEvent(aType, aParam.mBubbles, aParam.mCancelable, aParam.mView,
78 aParam.mDetail, aParam.mScreenX, aParam.mScreenY,
79 aParam.mClientX, aParam.mClientY, aParam.mCtrlKey,
80 aParam.mAltKey, aParam.mShiftKey, aParam.mMetaKey,
81 aParam.mButton, aParam.mRelatedTarget, aParam.mDataTransfer);
82 e->InitializeExtraMouseEventDictionaryMembers(aParam);
83 e->SetTrusted(trusted);
84 e->SetComposed(aParam.mComposed);
85 return e.forget();
88 } // namespace mozilla::dom
90 using namespace mozilla;
91 using namespace mozilla::dom;
93 already_AddRefed<DragEvent> NS_NewDOMDragEvent(EventTarget* aOwner,
94 nsPresContext* aPresContext,
95 WidgetDragEvent* aEvent) {
96 RefPtr<DragEvent> event = new DragEvent(aOwner, aPresContext, aEvent);
97 return event.forget();