LargestContentfulPaint.h (4744B)
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_LargestContentfulPaint_h___ 8 #define mozilla_dom_LargestContentfulPaint_h___ 9 10 #include "imgRequestProxy.h" 11 #include "mozilla/dom/Element.h" 12 #include "mozilla/dom/PerformanceEntry.h" 13 #include "mozilla/dom/PerformanceLargestContentfulPaintBinding.h" 14 #include "nsCycleCollectionParticipant.h" 15 16 class nsTextFrame; 17 namespace mozilla::dom { 18 19 static constexpr nsLiteralString kLargestContentfulPaintName = 20 u"largest-contentful-paint"_ns; 21 22 class Performance; 23 class PerformanceMainThread; 24 25 struct LCPTextFrameHelper final { 26 static void MaybeUnionTextFrame(nsTextFrame* aTextFrame, 27 const nsRect& aRelativeToSelfRect); 28 }; 29 30 class ImagePendingRendering final { 31 public: 32 ImagePendingRendering(Element* aElement, imgRequestProxy* aImgRequestProxy, 33 const TimeStamp& aLoadTime) 34 : mElement(do_GetWeakReference(aElement)), 35 mImageRequestProxy(aImgRequestProxy), 36 mLoadTime(aLoadTime) {} 37 38 Element* GetElement() const { 39 nsCOMPtr<Element> element = do_QueryReferent(mElement); 40 return element; 41 } 42 43 imgRequestProxy* GetImgRequestProxy() const { 44 return static_cast<imgRequestProxy*>(mImageRequestProxy.get()); 45 } 46 47 nsWeakPtr mElement; 48 WeakPtr<PreloaderBase> mImageRequestProxy; 49 TimeStamp mLoadTime; 50 }; 51 52 class LCPHelpers final { 53 public: 54 // Called when the size of the image is known. 55 static void FinalizeLCPEntryForImage(Element* aContainingBlock, 56 imgRequestProxy* aImgRequestProxy, 57 const nsRect& aTargetRectRelativeToSelf); 58 59 static void FinalizeLCPEntryForText(PerformanceMainThread* aPerformance, 60 const TimeStamp& aRenderTime, 61 Element* aContainingBlock, 62 const nsRect& aTargetRectRelativeToSelf, 63 const nsPresContext* aPresContext); 64 65 static bool IsQualifiedImageRequest(imgRequest* aRequest, 66 Element* aContainingElement); 67 68 private: 69 static bool CanFinalizeLCPEntry(const nsIFrame* aFrame); 70 }; 71 72 // https://w3c.github.io/largest-contentful-paint/ 73 class LargestContentfulPaint final : public PerformanceEntry { 74 public: 75 NS_DECL_ISUPPORTS_INHERITED 76 77 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(LargestContentfulPaint, 78 PerformanceEntry) 79 80 LargestContentfulPaint(PerformanceMainThread* aPerformance, 81 const TimeStamp& aRenderTime, 82 const Maybe<TimeStamp>& aLoadTime, 83 const unsigned long aSize, nsIURI* aURI, 84 Element* aElement, bool aShouldExposeRenderTime); 85 86 JSObject* WrapObject(JSContext* aCx, 87 JS::Handle<JSObject*> aGivenProto) override; 88 89 DOMHighResTimeStamp RenderTime() const; 90 DOMHighResTimeStamp LoadTime() const; 91 DOMHighResTimeStamp StartTime() const override; 92 DOMHighResTimeStamp PaintTime() const { return RenderTime(); } 93 Nullable<DOMHighResTimeStamp> GetPresentationTime() const { return nullptr; } 94 95 unsigned long Size() const { return mSize; } 96 void GetId(nsAString& aId) const { 97 if (mId) { 98 mId->ToString(aId); 99 } 100 } 101 void GetUrl(nsAString& aUrl); 102 103 Element* GetElement() const; 104 105 static Element* GetContainingBlockForTextFrame(const nsTextFrame* aTextFrame); 106 107 void UpdateSize(const Element* aContainingBlock, 108 const nsRect& aTargetRectRelativeToSelf, 109 const PerformanceMainThread* aPerformance, bool aIsImage); 110 111 void BufferEntryIfNeeded() override; 112 113 static void MaybeProcessImageForElementTiming(imgRequestProxy* aRequest, 114 Element* aElement); 115 116 void QueueEntry(); 117 118 private: 119 ~LargestContentfulPaint() = default; 120 121 void ReportLCPToNavigationTimings(); 122 123 RefPtr<PerformanceMainThread> mPerformance; 124 125 // This is always set but only exposed to web content if 126 // mShouldExposeRenderTime is true. 127 const TimeStamp mRenderTime; 128 const Maybe<TimeStamp> mLoadTime; 129 // This is set to false when for security reasons web content it not allowed 130 // to see the RenderTime. 131 const bool mShouldExposeRenderTime; 132 unsigned long mSize; 133 nsCOMPtr<nsIURI> mURI; 134 135 nsWeakPtr mElement; 136 RefPtr<nsAtom> mId; 137 }; 138 } // namespace mozilla::dom 139 #endif