summaryrefslogtreecommitdiffstats
path: root/dom/performance/LargestContentfulPaint.cpp
blob: 29984ade73d19dc418c99093789036f382bfcbe5 (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
/* -*- 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/. */
#include "mozilla/dom/Element.h"
#include "nsContentUtils.h"
#include "nsRFPService.h"
#include "Performance.h"
#include "PerformanceMainThread.h"
#include "LargestContentfulPaint.h"

namespace mozilla::dom {

NS_IMPL_CYCLE_COLLECTION_INHERITED(LargestContentfulPaint, PerformanceEntry,
                                   mPerformance, mURI, mElement)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(LargestContentfulPaint)
NS_INTERFACE_MAP_END_INHERITING(PerformanceEntry)

NS_IMPL_ADDREF_INHERITED(LargestContentfulPaint, PerformanceEntry)
NS_IMPL_RELEASE_INHERITED(LargestContentfulPaint, PerformanceEntry)

LargestContentfulPaint::LargestContentfulPaint(Performance* aPerformance,
                                               DOMHighResTimeStamp aRenderTime,
                                               DOMHighResTimeStamp aLoadTime,
                                               unsigned long aSize,
                                               nsIURI* aURI, Element* aElement)
    : PerformanceEntry(aPerformance->GetParentObject(), u""_ns,
                       kLargestContentfulPaintName),
      mPerformance(static_cast<PerformanceMainThread*>(aPerformance)),
      mRenderTime(aRenderTime),
      mLoadTime(aLoadTime),
      mSize(aSize),
      mURI(aURI),
      mElement(aElement) {
  MOZ_ASSERT(mPerformance);
  MOZ_ASSERT(mElement);
  // The element could be a pseudo-element
  if (mElement->ChromeOnlyAccess()) {
    mElement = Element::FromNodeOrNull(
        aElement->FindFirstNonChromeOnlyAccessContent());
  }

  if (const Element* element = GetElement()) {
    mId = element->GetID();
  }
}

JSObject* LargestContentfulPaint::WrapObject(
    JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
  return LargestContentfulPaint_Binding::Wrap(aCx, this, aGivenProto);
}

Element* LargestContentfulPaint::GetElement() const {
  return mElement ? nsContentUtils::GetAnElementForTiming(
                        mElement, mElement->GetComposedDoc(), nullptr)
                  : nullptr;
}

DOMHighResTimeStamp LargestContentfulPaint::RenderTime() const {
  return nsRFPService::ReduceTimePrecisionAsMSecs(
      mRenderTime, mPerformance->GetRandomTimelineSeed(),
      mPerformance->GetRTPCallerType());
}

DOMHighResTimeStamp LargestContentfulPaint::LoadTime() const {
  return nsRFPService::ReduceTimePrecisionAsMSecs(
      mLoadTime, mPerformance->GetRandomTimelineSeed(),
      mPerformance->GetRTPCallerType());
}

DOMHighResTimeStamp LargestContentfulPaint::StartTime() const {
  DOMHighResTimeStamp startTime = !mRenderTime ? mLoadTime : mRenderTime;
  return nsRFPService::ReduceTimePrecisionAsMSecs(
      startTime, mPerformance->GetRandomTimelineSeed(),
      mPerformance->GetRTPCallerType());
}

void LargestContentfulPaint::GetUrl(nsAString& aUrl) {
  if (mURI) {
    CopyUTF8toUTF16(mURI->GetSpecOrDefault(), aUrl);
  }
}
}  // namespace mozilla::dom