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/. */
7 #ifndef mozilla_dom_LargestContentfulPaint_h___
8 #define mozilla_dom_LargestContentfulPaint_h___
10 #include "nsCycleCollectionParticipant.h"
11 #include "mozilla/dom/Element.h"
12 #include "mozilla/dom/PerformanceEntry.h"
13 #include "mozilla/dom/PerformanceLargestContentfulPaintBinding.h"
15 #include "imgRequestProxy.h"
18 namespace mozilla::dom
{
20 static constexpr nsLiteralString kLargestContentfulPaintName
=
21 u
"largest-contentful-paint"_ns
;
24 class PerformanceMainThread
;
26 struct LCPTextFrameHelper final
{
27 static void MaybeUnionTextFrame(nsTextFrame
* aTextFrame
,
28 const nsRect
& aRelativeToSelfRect
);
31 class ImagePendingRendering final
{
33 ImagePendingRendering(Element
* aElement
, imgRequestProxy
* aImgRequestProxy
,
34 const TimeStamp
& aLoadTime
)
35 : mElement(do_GetWeakReference(aElement
)),
36 mImageRequestProxy(aImgRequestProxy
),
37 mLoadTime(aLoadTime
) {}
39 Element
* GetElement() const {
40 nsCOMPtr
<Element
> element
= do_QueryReferent(mElement
);
44 imgRequestProxy
* GetImgRequestProxy() const {
45 return static_cast<imgRequestProxy
*>(mImageRequestProxy
.get());
49 WeakPtr
<PreloaderBase
> mImageRequestProxy
;
53 class ContentIdentifierHashEntry
: public PLDHashEntryHdr
{
55 using KeyType
= const Element
*;
56 using KeyTypePointer
= const Element
*;
58 explicit ContentIdentifierHashEntry(KeyTypePointer aKey
) : mElement(aKey
) {}
60 ContentIdentifierHashEntry(ContentIdentifierHashEntry
&&) = default;
62 ~ContentIdentifierHashEntry() = default;
64 bool KeyEquals(KeyTypePointer aKey
) const { return mElement
== aKey
; }
66 static KeyTypePointer
KeyToPointer(KeyType
& aKey
) { return aKey
; }
68 static PLDHashNumber
HashKey(KeyTypePointer aKey
) {
69 return mozilla::HashGeneric(reinterpret_cast<uintptr_t>(aKey
));
72 // mImageRequestProxies isn't memmoveable.
73 enum { ALLOW_MEMMOVE
= false };
75 AutoTArray
<WeakPtr
<PreloaderBase
>, 1> mImageRequestProxies
;
78 // Raw pointer; Element::UnbindFromTree will delete this entry to make
79 // sure mElement is always valid.
80 const Element
* mElement
;
83 class LCPHelpers final
{
85 // Called when the size of the image is known.
86 static void FinalizeLCPEntryForImage(Element
* aContainingBlock
,
87 imgRequestProxy
* aImgRequestProxy
,
88 const nsRect
& aTargetRectRelativeToSelf
);
90 static void FinalizeLCPEntryForText(PerformanceMainThread
* aPerformance
,
91 const TimeStamp
& aRenderTime
,
92 Element
* aContainingBlock
,
93 const nsRect
& aTargetRectRelativeToSelf
,
94 const nsPresContext
* aPresContext
);
96 static bool IsQualifiedImageRequest(imgRequest
* aRequest
,
97 Element
* aContainingElement
);
100 static bool CanFinalizeLCPEntry(const nsIFrame
* aFrame
);
103 // https://w3c.github.io/largest-contentful-paint/
104 class LargestContentfulPaint final
: public PerformanceEntry
{
106 NS_DECL_ISUPPORTS_INHERITED
108 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(LargestContentfulPaint
,
111 LargestContentfulPaint(PerformanceMainThread
* aPerformance
,
112 const TimeStamp
& aRenderTime
,
113 const Maybe
<TimeStamp
>& aLoadTime
,
114 const unsigned long aSize
, nsIURI
* aURI
,
115 Element
* aElement
, bool aShouldExposeRenderTime
);
117 JSObject
* WrapObject(JSContext
* aCx
,
118 JS::Handle
<JSObject
*> aGivenProto
) override
;
120 DOMHighResTimeStamp
RenderTime() const;
121 DOMHighResTimeStamp
LoadTime() const;
122 DOMHighResTimeStamp
StartTime() const override
;
124 unsigned long Size() const { return mSize
; }
125 void GetId(nsAString
& aId
) const {
130 void GetUrl(nsAString
& aUrl
);
132 Element
* GetElement() const;
134 static Element
* GetContainingBlockForTextFrame(const nsTextFrame
* aTextFrame
);
136 void UpdateSize(const Element
* aContainingBlock
,
137 const nsRect
& aTargetRectRelativeToSelf
,
138 const PerformanceMainThread
* aPerformance
, bool aIsImage
);
140 void BufferEntryIfNeeded() override
;
142 static void MaybeProcessImageForElementTiming(imgRequestProxy
* aRequest
,
148 ~LargestContentfulPaint() = default;
150 void ReportLCPToNavigationTimings();
152 RefPtr
<PerformanceMainThread
> mPerformance
;
154 // This is always set but only exposed to web content if
155 // mShouldExposeRenderTime is true.
156 const TimeStamp mRenderTime
;
157 const Maybe
<TimeStamp
> mLoadTime
;
158 // This is set to false when for security reasons web content it not allowed
159 // to see the RenderTime.
160 const bool mShouldExposeRenderTime
;
162 nsCOMPtr
<nsIURI
> mURI
;
167 } // namespace mozilla::dom