summaryrefslogtreecommitdiffstats
path: root/dom/events/Touch.cpp
blob: a538fa6959d0a44bc7a283d18f52d89b236c5f7a (plain)
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
/* -*- 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 "mozilla/dom/Touch.h"

#include "mozilla/dom/EventTarget.h"
#include "mozilla/dom/TouchEvent.h"
#include "nsGlobalWindow.h"
#include "nsContentUtils.h"
#include "nsIContent.h"

namespace mozilla {
namespace dom {

// static
already_AddRefed<Touch>
Touch::Constructor(const GlobalObject& aGlobal,
                   const TouchInit& aParam,
                   ErrorResult& aRv)
{
  // Annoyingly many parameters, make sure the ordering is the same as in the
  // Touch constructor.
  RefPtr<Touch> touch = new Touch(aParam.mTarget,
                                  aParam.mIdentifier,
                                  aParam.mPageX,
                                  aParam.mPageY,
                                  aParam.mScreenX,
                                  aParam.mScreenY,
                                  aParam.mClientX,
                                  aParam.mClientY,
                                  aParam.mRadiusX,
                                  aParam.mRadiusY,
                                  aParam.mRotationAngle,
                                  aParam.mForce);
  return touch.forget();
}

Touch::Touch(EventTarget* aTarget,
             int32_t aIdentifier,
             int32_t aPageX,
             int32_t aPageY,
             int32_t aScreenX,
             int32_t aScreenY,
             int32_t aClientX,
             int32_t aClientY,
             int32_t aRadiusX,
             int32_t aRadiusY,
             float aRotationAngle,
             float aForce)
{
  mTarget = aTarget;
  mIdentifier = aIdentifier;
  mPagePoint = CSSIntPoint(aPageX, aPageY);
  mScreenPoint = CSSIntPoint(aScreenX, aScreenY);
  mClientPoint = CSSIntPoint(aClientX, aClientY);
  mRefPoint = LayoutDeviceIntPoint(0, 0);
  mPointsInitialized = true;
  mRadius.x = aRadiusX;
  mRadius.y = aRadiusY;
  mRotationAngle = aRotationAngle;
  mForce = aForce;

  mChanged = false;
  mMessage = 0;
  nsJSContext::LikelyShortLivingObjectCreated();
}

Touch::Touch(int32_t aIdentifier,
             LayoutDeviceIntPoint aPoint,
             LayoutDeviceIntPoint aRadius,
             float aRotationAngle,
             float aForce)
{
  mIdentifier = aIdentifier;
  mPagePoint = CSSIntPoint(0, 0);
  mScreenPoint = CSSIntPoint(0, 0);
  mClientPoint = CSSIntPoint(0, 0);
  mRefPoint = aPoint;
  mPointsInitialized = false;
  mRadius = aRadius;
  mRotationAngle = aRotationAngle;
  mForce = aForce;

  mChanged = false;
  mMessage = 0;
  nsJSContext::LikelyShortLivingObjectCreated();
}

Touch::Touch(const Touch& aOther)
  : mTarget(aOther.mTarget)
  , mRefPoint(aOther.mRefPoint)
  , mChanged(aOther.mChanged)
  , mMessage(aOther.mMessage)
  , mIdentifier(aOther.mIdentifier)
  , mPagePoint(aOther.mPagePoint)
  , mClientPoint(aOther.mClientPoint)
  , mScreenPoint(aOther.mScreenPoint)
  , mRadius(aOther.mRadius)
  , mRotationAngle(aOther.mRotationAngle)
  , mForce(aOther.mForce)
  , mPointsInitialized(aOther.mPointsInitialized)
{
  nsJSContext::LikelyShortLivingObjectCreated();
}

Touch::~Touch()
{
}

// static
bool
Touch::PrefEnabled(JSContext* aCx, JSObject* aGlobal)
{
  return TouchEvent::PrefEnabled(aCx, aGlobal);
}

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Touch, mTarget)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Touch)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(Touch)
NS_IMPL_CYCLE_COLLECTING_RELEASE(Touch)

EventTarget*
Touch::GetTarget() const
{
  nsCOMPtr<nsIContent> content = do_QueryInterface(mTarget);
  if (content && content->ChromeOnlyAccess() &&
      !nsContentUtils::LegacyIsCallerNativeCode() &&
      !nsContentUtils::CanAccessNativeAnon()) {
    return content->FindFirstNonChromeOnlyAccessContent();
  }

  return mTarget;
}

void
Touch::InitializePoints(nsPresContext* aPresContext, WidgetEvent* aEvent)
{
  if (mPointsInitialized) {
    return;
  }
  mClientPoint = Event::GetClientCoords(
    aPresContext, aEvent, mRefPoint, mClientPoint);
  mPagePoint = Event::GetPageCoords(
    aPresContext, aEvent, mRefPoint, mClientPoint);
  mScreenPoint = Event::GetScreenCoords(aPresContext, aEvent, mRefPoint);
  mPointsInitialized = true;
}

void
Touch::SetTarget(EventTarget* aTarget)
{
  mTarget = aTarget;
}

bool
Touch::Equals(Touch* aTouch)
{
  return mRefPoint == aTouch->mRefPoint &&
         mForce == aTouch->Force() &&
         mRotationAngle == aTouch->RotationAngle() &&
         mRadius.x == aTouch->RadiusX() &&
         mRadius.y == aTouch->RadiusY();
}

JSObject*
Touch::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
  return TouchBinding::Wrap(aCx, this, aGivenProto);
}

// Parent ourselves to the global of the target. This achieves the desirable
// effects of parenting to the target, but avoids making the touch inaccessible
// when the target happens to be NAC and therefore reflected into the XBL scope.
nsIGlobalObject*
Touch::GetParentObject()
{
  if (!mTarget) {
    return nullptr;
  }
  return mTarget->GetOwnerGlobal();
}

} // namespace dom
} // namespace mozilla