summaryrefslogtreecommitdiffstats
path: root/dom/performance/LargestContentfulPaint.h
blob: 0193cd858a5dc4717665ec215b9a015893800de5 (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* -*- 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/. */

#ifndef mozilla_dom_LargestContentfulPaint_h___
#define mozilla_dom_LargestContentfulPaint_h___

#include "nsCycleCollectionParticipant.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/PerformanceEntry.h"
#include "mozilla/dom/PerformanceLargestContentfulPaintBinding.h"

#include "imgRequestProxy.h"

class nsTextFrame;
namespace mozilla::dom {

static constexpr nsLiteralString kLargestContentfulPaintName =
    u"largest-contentful-paint"_ns;

class Performance;
class PerformanceMainThread;

struct LCPImageEntryKey {
  LCPImageEntryKey(Element* aElement, imgRequestProxy* aImgRequestProxy)
      : mElement(do_GetWeakReference(aElement)),
        mImageRequestProxy(aImgRequestProxy) {
    MOZ_ASSERT(aElement);
    MOZ_ASSERT(aImgRequestProxy);

    mHash = mozilla::HashGeneric(reinterpret_cast<uintptr_t>(aElement),
                                 reinterpret_cast<uintptr_t>(aImgRequestProxy));
  }

  LCPImageEntryKey(const LCPImageEntryKey& aLCPImageEntryKey) {
    mElement = aLCPImageEntryKey.mElement;
    mImageRequestProxy = aLCPImageEntryKey.mImageRequestProxy;
    mHash = aLCPImageEntryKey.mHash;
  }

  Element* GetElement() const {
    nsCOMPtr<Element> element = do_QueryReferent(mElement);
    return element;
  }

  imgRequestProxy* GetImgRequestProxy() const {
    return static_cast<imgRequestProxy*>(mImageRequestProxy.get());
  }

  bool operator==(const LCPImageEntryKey& aOther) const {
    imgRequestProxy* imgRequest = GetImgRequestProxy();
    if (!imgRequest) {
      return false;
    }

    imgRequestProxy* otherImgRequest = aOther.GetImgRequestProxy();
    if (!otherImgRequest) {
      return false;
    }

    Element* element = GetElement();
    if (!element) {
      return false;
    }

    Element* otherElement = aOther.GetElement();
    if (!otherElement) {
      return false;
    }

    return element == otherElement && imgRequest == otherImgRequest;
  }

  bool Equals(const Element* aElement,
              const imgRequestProxy* aImgRequestProxy) const {
    Element* element = GetElement();
    if (!element || !mImageRequestProxy) {
      return false;
    }

    return element == aElement && mImageRequestProxy == mImageRequestProxy;
  }

  nsWeakPtr mElement;

  WeakPtr<PreloaderBase> mImageRequestProxy;

  PLDHashNumber mHash = 0;

  ~LCPImageEntryKey() = default;
};

struct LCPTextFrameHelper final {
  static void MaybeUnionTextFrame(nsTextFrame* aTextFrame,
                                  const nsRect& aRelativeToSelfRect);
};

class ImagePendingRendering final {
 public:
  ImagePendingRendering(const LCPImageEntryKey& aLCPImageEntryKey,
                        const TimeStamp& aLoadTime);

  Element* GetElement() const { return mLCPImageEntryKey.GetElement(); }

  imgRequestProxy* GetImgRequestProxy() const {
    return mLCPImageEntryKey.GetImgRequestProxy();
  }

  LCPImageEntryKey mLCPImageEntryKey;
  TimeStamp mLoadTime;
};

class LCPEntryHashEntry : public PLDHashEntryHdr {
 public:
  typedef const LCPImageEntryKey& KeyType;
  typedef const LCPImageEntryKey* KeyTypePointer;

  explicit LCPEntryHashEntry(KeyTypePointer aKey) : mKey(*aKey) {}
  LCPEntryHashEntry(LCPEntryHashEntry&&) = default;

  ~LCPEntryHashEntry() = default;

  bool KeyEquals(KeyTypePointer aKey) const { return mKey == *aKey; }

  KeyType GetKey() const { return mKey; }

  static KeyTypePointer KeyToPointer(KeyType& aKey) { return &aKey; }

  static PLDHashNumber HashKey(KeyTypePointer aKey) { return aKey->mHash; }
  enum { ALLOW_MEMMOVE = true };

  LCPImageEntryKey mKey;
};

class LCPHelpers final {
 public:
  // Creates the LCP Entry for images with all information except the size of
  // the element. The size of the image is unknown at the moment. The entry is
  // not going to be queued in this function.
  static void CreateLCPEntryForImage(
      PerformanceMainThread* aPerformance, Element* aElement,
      imgRequestProxy* aRequestProxy, const TimeStamp& aLoadTime,
      const TimeStamp& aRenderTime, const LCPImageEntryKey& aContentIdentifier);

  // Called when the size of the image is known.
  static void FinalizeLCPEntryForImage(Element* aContainingBlock,
                                       imgRequestProxy* aImgRequestProxy,
                                       const nsRect& aTargetRectRelativeToSelf);

  static void FinalizeLCPEntryForText(PerformanceMainThread* aPerformance,
                                      const TimeStamp& aRenderTime,
                                      Element* aContainingBlock,
                                      const nsRect& aTargetRectRelativeToSelf,
                                      const nsPresContext* aPresContext);

  static bool IsQualifiedImageRequest(imgRequest* aRequest,
                                      Element* aContainingElement);

 private:
  static bool CanFinalizeLCPEntry(const nsIFrame* aFrame);
};

// https://w3c.github.io/largest-contentful-paint/
class LargestContentfulPaint final : public PerformanceEntry {
 public:
  NS_DECL_ISUPPORTS_INHERITED

  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(LargestContentfulPaint,
                                           PerformanceEntry)

  LargestContentfulPaint(PerformanceMainThread* aPerformance,
                         const TimeStamp& aRenderTime,
                         const Maybe<TimeStamp>& aLoadTime,
                         const unsigned long aSize, nsIURI* aURI,
                         Element* aElement,
                         const Maybe<const LCPImageEntryKey>& aLCPImageEntryKey,
                         bool aShouldExposeRenderTime);

  JSObject* WrapObject(JSContext* aCx,
                       JS::Handle<JSObject*> aGivenProto) override;

  DOMHighResTimeStamp RenderTime() const;
  DOMHighResTimeStamp LoadTime() const;
  DOMHighResTimeStamp StartTime() const override;

  unsigned long Size() const { return mSize; }
  void GetId(nsAString& aId) const {
    if (mId) {
      mId->ToString(aId);
    }
  }
  void GetUrl(nsAString& aUrl);

  Element* GetElement() const;

  static Element* GetContainingBlockForTextFrame(const nsTextFrame* aTextFrame);

  void UpdateSize(const Element* aContainingBlock,
                  const nsRect& aTargetRectRelativeToSelf,
                  const PerformanceMainThread* aPerformance, bool aIsImage);

  void BufferEntryIfNeeded() override;

  static void MaybeProcessImageForElementTiming(imgRequestProxy* aRequest,
                                                Element* aElement);

  void QueueEntry();

  Maybe<LCPImageEntryKey>& GetLCPImageEntryKey() { return mLCPImageEntryKey; }

 private:
  ~LargestContentfulPaint() = default;

  void ReportLCPToNavigationTimings();

  RefPtr<PerformanceMainThread> mPerformance;

  // This is always set but only exposed to web content if
  // mShouldExposeRenderTime is true.
  const TimeStamp mRenderTime;
  const Maybe<TimeStamp> mLoadTime;
  // This is set to false when for security reasons web content it not allowed
  // to see the RenderTime.
  const bool mShouldExposeRenderTime;
  unsigned long mSize;
  nsCOMPtr<nsIURI> mURI;

  nsWeakPtr mElement;
  RefPtr<nsAtom> mId;

  Maybe<LCPImageEntryKey> mLCPImageEntryKey;
};
}  // namespace mozilla::dom
#endif