PerformanceMainThread.h (7614B)
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 #ifndef mozilla_dom_PerformanceMainThread_h 8 #define mozilla_dom_PerformanceMainThread_h 9 10 #include "LargestContentfulPaint.h" 11 #include "Performance.h" 12 #include "PerformanceInteractionMetrics.h" 13 #include "PerformanceStorage.h" 14 #include "nsTextFrame.h" 15 16 namespace mozilla::dom { 17 18 class PerformanceNavigationTiming; 19 class PerformanceEventTiming; 20 21 using TextFrameUnions = nsTHashMap<nsRefPtrHashKey<Element>, nsRect>; 22 23 class PerformanceMainThread final : public Performance, 24 public PerformanceStorage { 25 public: 26 PerformanceMainThread(nsPIDOMWindowInner* aWindow, 27 nsDOMNavigationTiming* aDOMTiming, 28 nsITimedChannel* aChannel); 29 30 NS_DECL_ISUPPORTS_INHERITED 31 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(PerformanceMainThread, 32 Performance) 33 34 PerformanceStorage* AsPerformanceStorage() override { return this; } 35 36 virtual PerformanceTiming* Timing() override; 37 38 virtual PerformanceNavigation* Navigation() override; 39 40 virtual void AddEntry(nsIHttpChannel* channel, 41 nsITimedChannel* timedChannel) override; 42 43 // aData must be non-null. 44 virtual void AddEntry(const nsString& entryName, 45 const nsString& initiatorType, 46 UniquePtr<PerformanceTimingData>&& aData) override; 47 48 // aPerformanceTimingData must be non-null. 49 void AddRawEntry(UniquePtr<PerformanceTimingData> aPerformanceTimingData, 50 const nsAString& aInitiatorType, 51 const nsAString& aEntryName); 52 virtual void SetFCPTimingEntry(PerformancePaintTiming* aEntry) override; 53 bool HadFCPTimingEntry() const { return mFCPTiming; } 54 55 void InsertEventTimingEntry(PerformanceEventTiming*) override; 56 void BufferEventTimingEntryIfNeeded(PerformanceEventTiming*) override; 57 void DispatchPendingEventTimingEntries() override; 58 59 PerformanceInteractionMetrics& GetPerformanceInteractionMetrics() override; 60 61 void SetInteractionId(PerformanceEventTiming* aEventTiming, 62 const WidgetEvent* aEvent) override; 63 64 void BufferLargestContentfulPaintEntryIfNeeded(LargestContentfulPaint*); 65 66 TimeStamp CreationTimeStamp() const override; 67 68 DOMHighResTimeStamp CreationTime() const override; 69 70 virtual void GetMozMemory(JSContext* aCx, 71 JS::MutableHandle<JSObject*> aObj) override; 72 73 virtual nsDOMNavigationTiming* GetDOMTiming() const override { 74 return mDOMTiming; 75 } 76 77 virtual uint64_t GetRandomTimelineSeed() override { 78 return GetDOMTiming()->GetRandomTimelineSeed(); 79 } 80 81 virtual nsITimedChannel* GetChannel() const override { return mChannel; } 82 83 // The GetEntries* methods need to be overriden in order to add the 84 // the document entry of type navigation. 85 virtual void GetEntries(nsTArray<RefPtr<PerformanceEntry>>& aRetval) override; 86 87 // Return entries which qualify availableFromTimeline boolean check 88 virtual void GetEntriesByType( 89 const nsAString& aEntryType, 90 nsTArray<RefPtr<PerformanceEntry>>& aRetval) override; 91 92 // There are entries that we don't want expose via performance, however 93 // we do want PerformanceObserver to get them 94 void GetEntriesByTypeForObserver( 95 const nsAString& aEntryType, 96 nsTArray<RefPtr<PerformanceEntry>>& aRetval) override; 97 virtual void GetEntriesByName( 98 const nsAString& aName, const Optional<nsAString>& aEntryType, 99 nsTArray<RefPtr<PerformanceEntry>>& aRetval) override; 100 101 void UpdateNavigationTimingEntry() override; 102 void QueueNavigationTimingEntry() override; 103 void QueueLargestContentfulPaintEntry(LargestContentfulPaint* aEntry); 104 105 size_t SizeOfEventEntries(mozilla::MallocSizeOf aMallocSizeOf) const override; 106 107 static constexpr uint32_t kDefaultEventTimingBufferSize = 150; 108 static constexpr uint32_t kDefaultEventTimingDurationThreshold = 104; 109 static constexpr double kDefaultEventTimingMinDuration = 16.0; 110 111 static constexpr uint32_t kMaxLargestContentfulPaintBufferSize = 150; 112 113 class EventCounts* EventCounts() override; 114 115 uint64_t InteractionCount() override; 116 117 bool IsGlobalObjectWindow() const override { return true; }; 118 119 bool HasDispatchedInputEvent() const { return mHasDispatchedInputEvent; } 120 121 void SetHasDispatchedScrollEvent(); 122 bool HasDispatchedScrollEvent() const { return mHasDispatchedScrollEvent; } 123 124 void ProcessElementTiming(); 125 126 void AddImagesPendingRendering(ImagePendingRendering aImagePendingRendering) { 127 mImagesPendingRendering.AppendElement(aImagePendingRendering); 128 } 129 130 bool IsPendingLCPCandidate(Element* aElement, 131 imgRequestProxy* aImgRequestProxy); 132 133 bool UpdateLargestContentfulPaintSize(double aSize); 134 double GetLargestContentfulPaintSize() const { 135 return mLargestContentfulPaintSize; 136 } 137 138 nsTHashMap<nsRefPtrHashKey<Element>, nsRect>& GetTextFrameUnions() { 139 return mTextFrameUnions; 140 } 141 142 void FinalizeLCPEntriesForText(); 143 144 void ClearGeneratedTempDataForLCP(); 145 146 protected: 147 ~PerformanceMainThread(); 148 149 void CreateNavigationTimingEntry(); 150 151 void InsertUserEntry(PerformanceEntry* aEntry) override; 152 153 DOMHighResTimeStamp GetPerformanceTimingFromString( 154 const nsAString& aTimingName) override; 155 156 void DispatchResourceTimingBufferFullEvent() override; 157 158 RefPtr<PerformanceNavigationTiming> mDocEntry; 159 RefPtr<nsDOMNavigationTiming> mDOMTiming; 160 nsCOMPtr<nsITimedChannel> mChannel; 161 RefPtr<PerformanceTiming> mTiming; 162 RefPtr<PerformanceNavigation> mNavigation; 163 RefPtr<PerformancePaintTiming> mFCPTiming; 164 JS::Heap<JSObject*> mMozMemory; 165 166 nsTArray<RefPtr<PerformanceEventTiming>> mEventTimingEntries; 167 nsTArray<RefPtr<LargestContentfulPaint>> mLargestContentfulPaintEntries; 168 169 AutoCleanLinkedList<RefPtr<PerformanceEventTiming>> 170 mPendingEventTimingEntries; 171 bool mHasDispatchedInputEvent = false; 172 bool mHasDispatchedScrollEvent = false; 173 174 RefPtr<PerformanceEventTiming> mFirstInputEvent; 175 RefPtr<PerformanceEventTiming> mPendingPointerDown; 176 177 PerformanceInteractionMetrics mInteractionMetrics; 178 179 private: 180 void SetHasDispatchedInputEvent(); 181 182 bool mHasQueuedRefreshdriverObserver = false; 183 184 RefPtr<class EventCounts> mEventCounts; 185 void IncEventCount(const nsAtom* aType); 186 187 PresShell* GetPresShell(); 188 189 nsTArray<ImagePendingRendering> mImagesPendingRendering; 190 191 // Keeps track of the rendered size of the largest contentful paint that 192 // we have processed so far. 193 double mLargestContentfulPaintSize = 0.0; 194 195 // When a text frame is painted, its area (relative to the 196 // containing block) is unioned with other text frames that 197 // belong to the same containing block. 198 // mTextFrameUnions's key is the containing block, and 199 // the value is the unioned area. 200 TextFrameUnions mTextFrameUnions; 201 }; 202 203 inline void ImplCycleCollectionTraverse( 204 nsCycleCollectionTraversalCallback& aCallback, 205 const TextFrameUnions& aField, const char* aName, uint32_t aFlags = 0) { 206 for (const auto& entry : aField) { 207 CycleCollectionNoteChild(aCallback, entry.GetKey(), 208 "TextFrameUnions's key (nsRefPtrHashKey<Element>)", 209 aFlags); 210 } 211 } 212 213 } // namespace mozilla::dom 214 215 #endif // mozilla_dom_PerformanceMainThread_h