Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/events/DragEvent.cpp
Line
Count
Source (jump to first uncovered line)
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/. */
6
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"
12
13
namespace mozilla {
14
namespace dom {
15
16
DragEvent::DragEvent(EventTarget* aOwner,
17
                     nsPresContext* aPresContext,
18
                     WidgetDragEvent* aEvent)
19
  : MouseEvent(aOwner, aPresContext,
20
               aEvent ? aEvent :
21
                        new WidgetDragEvent(false, eVoidEvent, nullptr))
22
0
{
23
0
  if (aEvent) {
24
0
    mEventIsInternal = false;
25
0
  }
26
0
  else {
27
0
    mEventIsInternal = true;
28
0
    mEvent->mTime = PR_Now();
29
0
    mEvent->mRefPoint = LayoutDeviceIntPoint(0, 0);
30
0
    mEvent->AsMouseEvent()->inputSource = MouseEvent_Binding::MOZ_SOURCE_UNKNOWN;
31
0
  }
32
0
}
33
34
void
35
DragEvent::InitDragEvent(const nsAString& aType,
36
                         bool aCanBubble,
37
                         bool aCancelable,
38
                         nsGlobalWindowInner* aView,
39
                         int32_t aDetail,
40
                         int32_t aScreenX,
41
                         int32_t aScreenY,
42
                         int32_t aClientX,
43
                         int32_t aClientY,
44
                         bool aCtrlKey,
45
                         bool aAltKey,
46
                         bool aShiftKey,
47
                         bool aMetaKey,
48
                         uint16_t aButton,
49
                         EventTarget* aRelatedTarget,
50
                         DataTransfer* aDataTransfer)
51
0
{
52
0
  NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
53
0
54
0
  MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable,
55
0
                             aView, aDetail, aScreenX, aScreenY,
56
0
                             aClientX, aClientY, aCtrlKey, aAltKey,
57
0
                             aShiftKey, aMetaKey, aButton, aRelatedTarget);
58
0
  if (mEventIsInternal) {
59
0
    mEvent->AsDragEvent()->mDataTransfer = aDataTransfer;
60
0
  }
61
0
}
62
63
DataTransfer*
64
DragEvent::GetDataTransfer()
65
0
{
66
0
  // the dataTransfer field of the event caches the DataTransfer associated
67
0
  // with the drag. It is initialized when an attempt is made to retrieve it
68
0
  // rather that when the event is created to avoid duplicating the data when
69
0
  // no listener ever uses it.
70
0
  if (!mEvent || mEvent->mClass != eDragEventClass) {
71
0
    NS_WARNING("Tried to get dataTransfer from non-drag event!");
72
0
    return nullptr;
73
0
  }
74
0
75
0
  WidgetDragEvent* dragEvent = mEvent->AsDragEvent();
76
0
  // for synthetic events, just use the supplied data transfer object even if null
77
0
  if (!mEventIsInternal) {
78
0
    nsresult rv = nsContentUtils::SetDataTransferInEvent(dragEvent);
79
0
    NS_ENSURE_SUCCESS(rv, nullptr);
80
0
  }
81
0
82
0
  return dragEvent->mDataTransfer;
83
0
}
84
85
// static
86
already_AddRefed<DragEvent>
87
DragEvent::Constructor(const GlobalObject& aGlobal,
88
                       const nsAString& aType,
89
                       const DragEventInit& aParam,
90
                       ErrorResult& aRv)
91
0
{
92
0
  nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
93
0
  RefPtr<DragEvent> e = new DragEvent(t, nullptr, nullptr);
94
0
  bool trusted = e->Init(t);
95
0
  e->InitDragEvent(aType, aParam.mBubbles, aParam.mCancelable,
96
0
                   aParam.mView, aParam.mDetail, aParam.mScreenX,
97
0
                   aParam.mScreenY, aParam.mClientX, aParam.mClientY,
98
0
                   aParam.mCtrlKey, aParam.mAltKey, aParam.mShiftKey,
99
0
                   aParam.mMetaKey, aParam.mButton, aParam.mRelatedTarget,
100
0
                   aParam.mDataTransfer);
101
0
  e->InitializeExtraMouseEventDictionaryMembers(aParam);
102
0
  e->SetTrusted(trusted);
103
0
  e->SetComposed(aParam.mComposed);
104
0
  return e.forget();
105
0
}
106
107
} // namespace dom
108
} // namespace mozilla
109
110
using namespace mozilla;
111
using namespace mozilla::dom;
112
113
already_AddRefed<DragEvent>
114
NS_NewDOMDragEvent(EventTarget* aOwner,
115
                   nsPresContext* aPresContext,
116
                   WidgetDragEvent* aEvent)
117
0
{
118
0
  RefPtr<DragEvent> event =
119
0
    new DragEvent(aOwner, aPresContext, aEvent);
120
0
  return event.forget();
121
0
}