M73: Revert "[EventTiming] Ship First Input Timing on main frames"

This reverts commit 1ff422d8a30a34713c652508fc170f75120460a1 for M73.
Original CL:
https://chromium-review.googlesource.com/c/chromium/src/+/1368826

Bug: 927380, 823744, 841224

Change-Id: I0298780547f9a5de59a3e7bd9dcacff84fe17b8b
Reviewed-on: https://chromium-review.googlesource.com/c/1448695
Reviewed-by: Timothy Dresser <tdresser@chromium.org>
Cr-Commit-Position: refs/branch-heads/3683@{#129}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/third_party/blink/renderer/core/dom/events/event_dispatcher.cc b/third_party/blink/renderer/core/dom/events/event_dispatcher.cc
index 335c457..e217aa9 100644
--- a/third_party/blink/renderer/core/dom/events/event_dispatcher.cc
+++ b/third_party/blink/renderer/core/dom/events/event_dispatcher.cc
@@ -140,15 +140,14 @@
     return DispatchEventResult::kNotCanceled;
   }
   std::unique_ptr<EventTiming> eventTiming;
-  const Document& document = node_->GetDocument();
-  LocalFrame* frame = document.GetFrame();
-  if (frame && frame->DomWindow()) {
-    if (origin_trials::EventTimingEnabled(&document)) {
-      UseCounter::Count(document,
+  if (origin_trials::EventTimingEnabled(&node_->GetDocument())) {
+    LocalFrame* frame = node_->GetDocument().GetFrame();
+    if (frame && frame->DomWindow()) {
+      UseCounter::Count(node_->GetDocument(),
                         WebFeature::kPerformanceEventTimingConstructor);
+      eventTiming = std::make_unique<EventTiming>(frame->DomWindow());
+      eventTiming->WillDispatchEvent(*event_);
     }
-    eventTiming = std::make_unique<EventTiming>(frame->DomWindow());
-    eventTiming->WillDispatchEvent(*event_);
   }
   event_->GetEventPath().EnsureWindowEventContext();
 
@@ -156,6 +155,8 @@
       event_->IsMouseEvent() && event_->type() == event_type_names::kClick;
 
   if (is_click && event_->isTrusted()) {
+    Document& document = node_->GetDocument();
+    LocalFrame* frame = document.GetFrame();
     if (frame) {
       // A genuine mouse click cannot be triggered by script so we don't expect
       // there are any script in the stack.
diff --git a/third_party/blink/renderer/core/timing/event_timing.cc b/third_party/blink/renderer/core/timing/event_timing.cc
index 3315755..cca33b2 100644
--- a/third_party/blink/renderer/core/timing/event_timing.cc
+++ b/third_party/blink/renderer/core/timing/event_timing.cc
@@ -7,7 +7,6 @@
 #include "third_party/blink/renderer/core/dom/events/event.h"
 #include "third_party/blink/renderer/core/events/pointer_event.h"
 #include "third_party/blink/renderer/core/frame/local_dom_window.h"
-#include "third_party/blink/renderer/core/origin_trials/origin_trials.h"
 #include "third_party/blink/renderer/core/timing/dom_window_performance.h"
 #include "third_party/blink/renderer/core/timing/performance_event_timing.h"
 #include "third_party/blink/renderer/platform/wtf/time.h"
@@ -26,29 +25,22 @@
          event.isTrusted();
 }
 
-void EventTiming::FinishWillDispatch() {
-  processing_start_ = CurrentTimeTicks();
-  finished_will_dispatch_event_ = true;
-}
-
 void EventTiming::WillDispatchEvent(const Event& event) {
   // Assume each event can be dispatched only once.
   DCHECK(!finished_will_dispatch_event_);
   if (!performance_ || !ShouldReportForEventTiming(event))
     return;
 
-  // We care about events when the first input is desired or
-  // when the EventTiming origin trial is enabled.
-  if (performance_->FirstInputDesired()) {
-    FinishWillDispatch();
-    return;
-  }
-  if (!origin_trials::EventTimingEnabled(performance_->GetExecutionContext()))
-    return;
+  // Although we screen the events for timing by setting these conditions here,
+  // we cannot assume that the conditions should still hold true in
+  // DidDispatchEvent. These conditions have to be re-tested before an entry is
+  // dispatched.
   if ((performance_->ShouldBufferEntries() &&
        !performance_->IsEventTimingBufferFull()) ||
-      performance_->HasObserverFor(PerformanceEntry::kEvent)) {
-    FinishWillDispatch();
+      performance_->HasObserverFor(PerformanceEntry::kEvent) ||
+      !performance_->FirstInputDetected()) {
+    processing_start_ = CurrentTimeTicks();
+    finished_will_dispatch_event_ = true;
   }
 }
 
diff --git a/third_party/blink/renderer/core/timing/event_timing.h b/third_party/blink/renderer/core/timing/event_timing.h
index 96648d9..59bacd8 100644
--- a/third_party/blink/renderer/core/timing/event_timing.h
+++ b/third_party/blink/renderer/core/timing/event_timing.h
@@ -26,7 +26,6 @@
   void DidDispatchEvent(const Event&);
 
  private:
-  void FinishWillDispatch();
   bool ShouldReportForEventTiming(const Event& event) const;
   // The time the first event handler or default action started to execute.
   TimeTicks processing_start_;
diff --git a/third_party/blink/renderer/core/timing/performance_event_timing.idl b/third_party/blink/renderer/core/timing/performance_event_timing.idl
index 951011e3..ef577a5 100644
--- a/third_party/blink/renderer/core/timing/performance_event_timing.idl
+++ b/third_party/blink/renderer/core/timing/performance_event_timing.idl
@@ -3,8 +3,10 @@
 // found in the LICENSE file.
 
 // https://github.com/wicg/event-timing
-[Exposed=Window]
-interface PerformanceEventTiming : PerformanceEntry {
+[
+    Exposed=Window,
+    OriginTrialEnabled=EventTiming
+] interface PerformanceEventTiming : PerformanceEntry {
     readonly attribute DOMHighResTimeStamp processingStart;
     readonly attribute DOMHighResTimeStamp processingEnd;
     readonly attribute boolean cancelable;
diff --git a/third_party/blink/renderer/core/timing/performance_observer.cc b/third_party/blink/renderer/core/timing/performance_observer.cc
index 6141b19..fb30b69 100644
--- a/third_party/blink/renderer/core/timing/performance_observer.cc
+++ b/third_party/blink/renderer/core/timing/performance_observer.cc
@@ -59,9 +59,10 @@
   if (execution_context->IsDocument()) {
     if (origin_trials::ElementTimingEnabled(execution_context))
       supportedEntryTypes.push_back(performance_entry_names::kElement);
-    if (origin_trials::EventTimingEnabled(execution_context))
+    if (origin_trials::EventTimingEnabled(execution_context)) {
       supportedEntryTypes.push_back(performance_entry_names::kEvent);
-    supportedEntryTypes.push_back(performance_entry_names::kFirstInput);
+      supportedEntryTypes.push_back(performance_entry_names::kFirstInput);
+    }
     if (origin_trials::LayoutJankAPIEnabled(execution_context))
       supportedEntryTypes.push_back(performance_entry_names::kLayoutJank);
     supportedEntryTypes.push_back(performance_entry_names::kLongtask);
diff --git a/third_party/blink/renderer/core/timing/window_performance.cc b/third_party/blink/renderer/core/timing/window_performance.cc
index 56ef2cf..8616fc2 100644
--- a/third_party/blink/renderer/core/timing/window_performance.cc
+++ b/third_party/blink/renderer/core/timing/window_performance.cc
@@ -333,23 +333,12 @@
   return !timing() || !timing()->loadEventStart();
 }
 
-bool WindowPerformance::FirstInputDesired() const {
-  if (!GetFrame() || !GetFrame()->IsMainFrame())
-    return false;
-
-  return !first_input_timing_;
-}
-
 void WindowPerformance::RegisterEventTiming(const AtomicString& event_type,
                                             TimeTicks start_time,
                                             TimeTicks processing_start,
                                             TimeTicks processing_end,
                                             bool cancelable) {
-  if (!GetFrame())
-    return;
-
-  DCHECK(FirstInputDesired() ||
-         origin_trials::EventTimingEnabled(GetExecutionContext()));
+  DCHECK(origin_trials::EventTimingEnabled(GetExecutionContext()));
 
   // |start_time| could be null in some tests that inject input.
   DCHECK(!processing_start.is_null());
@@ -379,11 +368,7 @@
 
 void WindowPerformance::ReportEventTimings(WebLayerTreeView::SwapResult result,
                                            TimeTicks timestamp) {
-  if (!GetFrame())
-    return;
-
-  DCHECK(FirstInputDesired() ||
-         origin_trials::EventTimingEnabled(GetExecutionContext()));
+  DCHECK(origin_trials::EventTimingEnabled(GetExecutionContext()));
 
   DOMHighResTimeStamp end_time = MonotonicTimeToDOMHighResTimeStamp(timestamp);
   for (const auto& entry : event_timings_) {
@@ -401,11 +386,8 @@
             PerformanceEventTiming::CreateFirstInputTiming(entry));
       }
     }
-    // Do not report EventTiming unless origin trial is enabled!
-    if (!origin_trials::EventTimingEnabled(GetExecutionContext()) ||
-        duration_in_ms <= kEventTimingDurationThresholdInMs) {
+    if (duration_in_ms <= kEventTimingDurationThresholdInMs)
       continue;
-    }
 
     if (HasObserverFor(PerformanceEntry::kEvent)) {
       UseCounter::Count(GetFrame(),
@@ -436,6 +418,8 @@
 
 void WindowPerformance::DispatchFirstInputTiming(
     PerformanceEventTiming* entry) {
+  DCHECK(origin_trials::EventTimingEnabled(GetExecutionContext()));
+
   if (!entry)
     return;
   DCHECK_EQ("firstInput", entry->entryType());
diff --git a/third_party/blink/renderer/core/timing/window_performance.h b/third_party/blink/renderer/core/timing/window_performance.h
index 6efd939..0d23bad 100644
--- a/third_party/blink/renderer/core/timing/window_performance.h
+++ b/third_party/blink/renderer/core/timing/window_performance.h
@@ -71,10 +71,7 @@
 
   bool ShouldBufferEntries();
 
-  // Currently, returns false always for frames that are not main frames because
-  // we're only shipping FirstInputTiming on main frames. For main frames,
-  // returns true if the first input has not yet been calculated.
-  bool FirstInputDesired() const;
+  bool FirstInputDetected() const { return !!first_input_timing_; }
 
   // This method creates a PerformanceEventTiming and if needed creates a swap
   // promise to calculate the |duration| attribute when such promise is
diff --git a/third_party/blink/web_tests/external/wpt/event-timing/supported-types.window.js b/third_party/blink/web_tests/external/wpt/event-timing/supported-types.window.js
deleted file mode 100644
index c81d501..0000000
--- a/third_party/blink/web_tests/external/wpt/event-timing/supported-types.window.js
+++ /dev/null
@@ -1,11 +0,0 @@
-test(() => {
-  if (typeof PerformanceObserver.supportedEntryTypes === "undefined")
-    assert_unreached("supportedEntryTypes is not supported.");
-  const types = PerformanceObserver.supportedEntryTypes;
-  assert_true(types.includes("firstInput"),
-    "There should be 'firstInput' in PerformanceObserver.supportedEntryTypes");
-  assert_true(types.includes("event"),
-    "There should be 'event' in PerformanceObserver.supportedEntryTypes");
-  assert_greater_than(types.indexOf("firstInput"), types.indexOf('event'),
-    "The 'firstInput' entry should appear after the 'event' entry");
-}, "supportedEntryTypes contains 'event' and 'firstInput'.");
diff --git a/third_party/blink/web_tests/virtual/stable/webexposed/global-interface-listing-expected.txt b/third_party/blink/web_tests/virtual/stable/webexposed/global-interface-listing-expected.txt
index b2b7398..c01093e 100644
--- a/third_party/blink/web_tests/virtual/stable/webexposed/global-interface-listing-expected.txt
+++ b/third_party/blink/web_tests/virtual/stable/webexposed/global-interface-listing-expected.txt
@@ -4534,13 +4534,6 @@
     getter startTime
     method constructor
     method toJSON
-interface PerformanceEventTiming : PerformanceEntry
-    attribute @@toStringTag
-    getter cancelable
-    getter processingEnd
-    getter processingStart
-    method constructor
-    method toJSON
 interface PerformanceLongTaskTiming : PerformanceEntry
     attribute @@toStringTag
     getter attribution