File: PerformanceInteractionMetrics.cpp

package info (click to toggle)
firefox 144.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,637,504 kB
  • sloc: cpp: 7,576,692; javascript: 6,430,831; ansic: 3,748,119; python: 1,398,978; xml: 628,810; asm: 438,679; java: 186,194; sh: 63,212; makefile: 19,159; objc: 13,086; perl: 12,986; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (268 lines) | stat: -rw-r--r-- 9,675 bytes parent folder | download | duplicates (4)
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
/* -*- 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 "PerformanceInteractionMetrics.h"

#include "mozilla/EventForwards.h"
#include "mozilla/Maybe.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/RandomNum.h"
#include "mozilla/TextEvents.h"

// Interaction ID increment. We increase this value by an integer greater than 1
// to discourage developers from using the value to 'count' the number of user
// interactions. This is consistent with the spec, which allows the increasing
// the user interaction value by a small number chosen by the user agent.
constexpr uint32_t kInteractionIdIncrement = 7;
// Minimum potential value for the first Interaction ID.
constexpr uint32_t kMinFirstInteractionID = 100;
// Maximum potential value for the first Interaction ID.
constexpr uint32_t kMaxFirstInteractionID = 10000;

constexpr uint32_t kNonPointerId = -1;

namespace mozilla::dom {

PerformanceInteractionMetrics::PerformanceInteractionMetrics() {
  uint64_t randVal = RandomUint64().valueOr(kMinFirstInteractionID);
  // Choose a random integer as the initial value to discourage developers from
  // using interactionId to counter the number of interactions.
  // https://wicg.github.io/event-timing/#user-interaction-value
  mCurrentInteractionValue =
      kMinFirstInteractionID +
      (randVal % (kMaxFirstInteractionID - kMinFirstInteractionID + 1));
}

// https://w3c.github.io/event-timing/#sec-increasing-interaction-count
uint64_t PerformanceInteractionMetrics::IncreaseInteractionValueAndCount() {
  mCurrentInteractionValue += kInteractionIdIncrement;
  mInteractionCount++;
  return mCurrentInteractionValue;
}

// https://w3c.github.io/event-timing/#sec-computing-interactionid
Maybe<uint64_t> PerformanceInteractionMetrics::ComputeInteractionId(
    PerformanceEventTiming* aEventTiming, const WidgetEvent* aEvent) {
  // Step 1. If event’s isTrusted attribute value is false, return 0.
  if (!aEvent->IsTrusted()) {
    return Some(0);
  }

  // Step 2. Let type be event’s type attribute value.
  const EventMessage eventType = aEvent->mMessage;

  // Step 3. If type is not one among keyup, compositionstart, input,
  // pointercancel, pointerup, or click, return 0.
  // Note: keydown and pointerdown are handled in finalize event timing.
  switch (eventType) {
    case eKeyDown:
    case eKeyPress:
    case eKeyUp:
    case eCompositionStart:
    case eEditorInput:
    case ePointerCancel:
    case ePointerDown:
    case ePointerUp:
    case eContextMenu:
    case ePointerClick:
      break;
    default:
      return Some(0);
  }

  // Step 4-8. Happens in the class constructor.

  if (eventType == ePointerDown) {
    uint32_t pointerId = aEvent->AsPointerEvent()->pointerId;

    // If after the previously buffered pointerdown we haven't encountered any
    // matching pointerup, we need to set it as zero so it properly flushes the
    // events.
    auto bufferedPointerdown = mPendingPointerDowns.MaybeGet(pointerId);
    if (NS_WARN_IF(bufferedPointerdown &&
                   !(*bufferedPointerdown)->HasKnownInteractionId())) {
      (*bufferedPointerdown)->SetInteractionId(0);
    }
    mPendingPointerDowns.InsertOrUpdate(pointerId, aEventTiming);
    mContextMenuTriggered = false;
    // InteractionId for this will be assigned by pointerup or pointercancel
    // later.
    return Nothing();
  }

  if (eventType == eKeyDown) {
    const WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent();

    if (keyEvent->mIsComposing) {
      return Some(0);
    }

    auto code = keyEvent->mKeyCode;

    // This is not part of the spec yet, but it's being discussed and will be
    // added to the spec soon.
    // See: https://github.com/w3c/event-timing/issues/153
    mPendingKeyDowns.InsertOrUpdate(code, aEventTiming);
    uint64_t interactionId = IncreaseInteractionValueAndCount();
    mLastKeydownInteractionValue = Some(interactionId);
    return Some(interactionId);
  }

  // keypress is not a part of the spec yet, but it's being worked on:
  // https://github.com/w3c/event-timing/issues/149
  if (eventType == eKeyPress) {
    // Set the interaction id generated by the previous keydown entry.
    return Some(mCurrentInteractionValue);
  }

  // Step 8. If type is keyup:
  if (eventType == eKeyUp) {
    // Step 8.1. If event’s isComposing attribute value is true, return 0.
    const WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent();
    if (keyEvent->mIsComposing) {
      return Some(0);
    }

    // Step 8.2. Let code be event’s keyCode attribute value.
    const uint32_t code = keyEvent->mKeyCode;

    // Step 8.4. Let entry be pendingKeyDowns[code].
    auto entry = mPendingKeyDowns.MaybeGet(code);
    // Step 8.3. If pendingKeyDowns[code] does not exist, return 0.
    if (!entry) {
      return Some(0);
    }

    uint64_t interactionId = (*entry)->InteractionId();

    // Step 8.9. Remove pendingKeyDowns[code].
    mPendingKeyDowns.Remove(code);

    // Step 8.10. Return interactionId.
    return Some(interactionId);
  }

  // Step 9. If type is compositionstart:
  if (eventType == eCompositionStart) {
    // Step 9.1 For each entry in the values of pendingKeyDowns:
    for (auto iter = mPendingKeyDowns.Iter(); !iter.Done(); iter.Next()) {
      PerformanceEventTiming* entry = iter.Data();
      // Step 9.1.1. Append entry to window’s entries to be queued.
      entry->SetInteractionId(0);
    }

    // Step 9.2. Clear pendingKeyDowns.
    mPendingKeyDowns.Clear();
    // Step 9.3. Return 0
    return Some(0);
  }

  // Step 10. If type is input:
  if (eventType == eEditorInput) {
    // Step 10.1. If event is not an instance of InputEvent, return 0.
    const auto* inputEvent = aEvent->AsEditorInputEvent();
    if (!inputEvent) {
      return Some(0);
    }

    // Step 10.2. If event’s isComposing attribute value is false, return 0.
    if (!inputEvent->mIsComposing) {
      return Some(0);
    }

    mLastKeydownInteractionValue = Nothing();
    return Some(IncreaseInteractionValueAndCount());
  }

  // Step 11. Otherwise (type is pointercancel, pointerup, or click):

  MOZ_ASSERT(eventType == ePointerCancel || eventType == ePointerUp ||
                 eventType == ePointerClick || eventType == eContextMenu,
             "Unexpected event type");
  const auto* mouseEvent = aEvent->AsMouseEvent();
  // Step 11.1. Let pointerId be event’s pointerId attribute value.
  auto pointerId = mouseEvent->pointerId;

  // Step 11.2. If type is click:
  if (eventType == ePointerClick) {
    if (pointerId == kNonPointerId) {
      // -1 pointerId is a reserved value to indicate events that were generated
      // by something other than a pointer device, like keydown.
      // Return the interaction value of the keydown event instead.
      return Some(mLastKeydownInteractionValue.valueOr(0));
    }

    // Step 11.2.2. Let value be pointerMap[pointerId].
    auto value = mPointerInteractionValueMap.MaybeGet(pointerId);
    // Step 11.2.1. If pointerMap[pointerId] does not exist, return 0.
    if (!value) {
      return Some(0);
    }

    // Step 11.2.3. Remove pointerMap[pointerId].
    mPointerInteractionValueMap.Remove(pointerId);
    // Step 11.2.4. Return value.
    return Some(*value);
  }

  // Step 11.3. Assert that type is pointerup or pointercancel.
  MOZ_RELEASE_ASSERT(eventType == ePointerUp || eventType == ePointerCancel ||
                     eventType == eContextMenu);

  // Step 11.5. Let pointerDownEntry be pendingPointerDowns[pointerId].
  auto entry = mPendingPointerDowns.MaybeGet(pointerId);
  // Step 11.4. If pendingPointerDowns[pointerId] does not exist, return 0.
  if (!entry) {
    // This is the case where we have seen a pointerup before a contextmenu
    // event. We return the same interactionId for the contextmenu.
    // See https://github.com/w3c/event-timing/issues/155.
    if (eventType == eContextMenu) {
      return Some(mCurrentInteractionValue);
    }

    // This is the case where we have seen a contextmenu before a pointerup
    // event. Similarly, we return the same interactionId, but also we reset the
    // "is contextmenu triggered" flag to make sure that the next events are
    // handled correctly. See https://github.com/w3c/event-timing/issues/155.
    if (eventType == ePointerUp && mContextMenuTriggered) {
      mContextMenuTriggered = false;
      return Some(mCurrentInteractionValue);
    }
    return Some(0);
  }

  // Step 11.7. If type is pointerup:
  if (eventType == ePointerUp || eventType == eContextMenu) {
    // Step 11.7.1. Increase interaction count on window.
    uint64_t interactionId = IncreaseInteractionValueAndCount();

    // Step 11.7.2. Set pointerMap[pointerId] to window’s user interaction
    // value.
    mPointerInteractionValueMap.InsertOrUpdate(pointerId, interactionId);

    // Step 11.7.3. Set pointerDownEntry’s interactionId to
    // pointerMap[pointerId].
    (*entry)->SetInteractionId(interactionId);
  } else {
    (*entry)->SetInteractionId(0);
  }

  // Step 11.9. Remove pendingPointerDowns[pointerId].
  mPendingPointerDowns.Remove(pointerId);

  if (eventType == eContextMenu) {
    mContextMenuTriggered = true;
  }

  // Step 11.10. If type is pointercancel, return 0.
  if (eventType == ePointerCancel) {
    return Some(0);
  }

  return Some(mPointerInteractionValueMap.Get(pointerId));
}

}  // namespace mozilla::dom