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
|
/* -*- 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 "PerformanceEventTiming.h"
#include <algorithm>
#include "PerformanceInteractionMetrics.h"
#include "PerformanceMainThread.h"
#include "mozilla/EventForwards.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/TextEvents.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/Performance.h"
#include "mozilla/dom/PerformanceEventTimingBinding.h"
#include "nsContentUtils.h"
#include "nsIDocShell.h"
namespace mozilla::dom {
NS_IMPL_CYCLE_COLLECTION_INHERITED(PerformanceEventTiming, PerformanceEntry,
mPerformance, mTarget)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PerformanceEventTiming)
NS_INTERFACE_MAP_END_INHERITING(PerformanceEntry)
NS_IMPL_ADDREF_INHERITED(PerformanceEventTiming, PerformanceEntry)
NS_IMPL_RELEASE_INHERITED(PerformanceEventTiming, PerformanceEntry)
PerformanceEventTiming::PerformanceEventTiming(Performance* aPerformance,
const nsAString& aName,
const TimeStamp& aStartTime,
bool aIsCancelable,
EventMessage aMessage)
: PerformanceEntry(aPerformance->GetParentObject(), aName, u"event"_ns),
mPerformance(aPerformance),
mProcessingStart(aPerformance->NowUnclamped()),
mProcessingEnd(0),
mStartTime(
aPerformance->GetDOMTiming()->TimeStampToDOMHighRes(aStartTime)),
mDuration(0),
mCancelable(aIsCancelable),
mMessage(aMessage) {}
PerformanceEventTiming::PerformanceEventTiming(
const PerformanceEventTiming& aEventTimingEntry)
: PerformanceEntry(aEventTimingEntry.mPerformance->GetParentObject(),
nsDependentAtomString(aEventTimingEntry.GetName()),
nsDependentAtomString(aEventTimingEntry.GetEntryType())),
mPerformance(aEventTimingEntry.mPerformance),
mProcessingStart(aEventTimingEntry.mProcessingStart),
mProcessingEnd(aEventTimingEntry.mProcessingEnd),
mTarget(aEventTimingEntry.mTarget),
mStartTime(aEventTimingEntry.mStartTime),
mDuration(aEventTimingEntry.mDuration),
mCancelable(aEventTimingEntry.mCancelable),
mInteractionId(aEventTimingEntry.mInteractionId),
mMessage(aEventTimingEntry.mMessage) {}
JSObject* PerformanceEventTiming::WrapObject(
JSContext* cx, JS::Handle<JSObject*> aGivenProto) {
return PerformanceEventTiming_Binding::Wrap(cx, this, aGivenProto);
}
already_AddRefed<PerformanceEventTiming>
PerformanceEventTiming::TryGenerateEventTiming(const EventTarget* aTarget,
const WidgetEvent* aEvent) {
MOZ_ASSERT(NS_IsMainThread());
if (!StaticPrefs::dom_enable_event_timing() ||
aEvent->mFlags.mOnlyChromeDispatch) {
return nullptr;
}
if (!aEvent->IsTrusted()) {
return nullptr;
}
switch (aEvent->mMessage) {
case eContextMenu:
case eMouseDoubleClick:
case eMouseDown:
case eMouseEnter:
case eMouseLeave:
case eMouseOut:
case eMouseOver:
case eMouseUp:
case ePointerAuxClick:
case ePointerClick:
case ePointerOver:
case ePointerEnter:
case ePointerDown:
case ePointerUp:
case ePointerCancel:
case ePointerOut:
case ePointerLeave:
case ePointerGotCapture:
case ePointerLostCapture:
case eTouchStart:
case eTouchEnd:
case eTouchCancel:
case eKeyDown:
case eKeyPress:
case eKeyUp:
case eEditorBeforeInput:
case eEditorInput:
case eCompositionStart:
case eCompositionUpdate:
case eCompositionEnd:
case eDragStart:
case eDragEnd:
case eDragEnter:
case eDragLeave:
case eDragOver:
case eDrop:
break;
default:
return nullptr;
}
nsCOMPtr<nsPIDOMWindowInner> innerWindow =
do_QueryInterface(aTarget->GetOwnerGlobal());
if (!innerWindow) {
return nullptr;
}
if (Performance* performance = innerWindow->GetPerformance()) {
const char16_t* eventName = Event::GetEventName(aEvent->mMessage);
MOZ_ASSERT(eventName,
"User defined events shouldn't be considered as event timing");
auto eventTiming =
RefPtr<PerformanceEventTiming>(new PerformanceEventTiming(
performance, nsDependentString(eventName), aEvent->mTimeStamp,
aEvent->mFlags.mCancelable, aEvent->mMessage));
performance->SetInteractionId(eventTiming, aEvent);
return eventTiming.forget();
}
return nullptr;
}
bool PerformanceEventTiming::ShouldAddEntryToBuffer(double aDuration) const {
if (GetEntryType() == nsGkAtoms::firstInput) {
return true;
}
MOZ_ASSERT(GetEntryType() == nsGkAtoms::event);
return RawDuration() >= aDuration;
}
bool PerformanceEventTiming::ShouldAddEntryToObserverBuffer(
PerformanceObserverInit& aOption) const {
if (!PerformanceEntry::ShouldAddEntryToObserverBuffer(aOption)) {
return false;
}
const double minDuration =
aOption.mDurationThreshold.WasPassed()
? std::max(aOption.mDurationThreshold.Value(),
PerformanceMainThread::kDefaultEventTimingMinDuration)
: PerformanceMainThread::kDefaultEventTimingDurationThreshold;
return ShouldAddEntryToBuffer(minDuration);
}
void PerformanceEventTiming::BufferEntryIfNeeded() {
if (ShouldAddEntryToBuffer(
PerformanceMainThread::kDefaultEventTimingDurationThreshold)) {
if (GetEntryType() != nsGkAtoms::firstInput) {
MOZ_ASSERT(GetEntryType() == nsGkAtoms::event);
mPerformance->BufferEventTimingEntryIfNeeded(this);
}
}
}
nsINode* PerformanceEventTiming::GetTarget() const {
nsCOMPtr<Element> element = do_QueryReferent(mTarget);
if (!element) {
return nullptr;
}
nsCOMPtr<nsPIDOMWindowInner> global =
do_QueryInterface(element->GetOwnerGlobal());
if (!global) {
return nullptr;
}
return nsContentUtils::GetAnElementForTiming(element, global->GetExtantDoc(),
mPerformance->GetParentObject());
}
void PerformanceEventTiming::FinalizeEventTiming(const WidgetEvent* aEvent) {
MOZ_ASSERT(aEvent);
EventTarget* target = aEvent->mTarget;
if (!target) {
return;
}
nsCOMPtr<nsPIDOMWindowInner> global =
do_QueryInterface(target->GetOwnerGlobal());
if (!global) {
return;
}
mProcessingEnd = mPerformance->NowUnclamped();
Element* element = Element::FromEventTarget(target);
if (!element || element->ChromeOnlyAccess()) {
return;
}
mTarget = do_GetWeakReference(element);
mPerformance->InsertEventTimingEntry(this);
}
} // namespace mozilla::dom
|