summaryrefslogtreecommitdiffstats
path: root/dom/performance/Performance.h
blob: 42cb975aebf74f4181b1d53b11a192d93b3900bc (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
237
238
239
240
241
242
243
/* -*- 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_Performance_h
#define mozilla_dom_Performance_h

#include "mozilla/Attributes.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "nsCOMPtr.h"
#include "nsContentUtils.h"
#include "nsDOMNavigationTiming.h"
#include "nsTObserverArray.h"

class nsITimedChannel;

namespace mozilla {

class ErrorResult;

namespace dom {

class OwningStringOrDouble;
class StringOrPerformanceMeasureOptions;
class PerformanceEntry;
class PerformanceMark;
struct PerformanceMarkOptions;
struct PerformanceMeasureOptions;
class PerformanceMeasure;
class PerformanceNavigation;
class PerformancePaintTiming;
class PerformanceObserver;
class PerformanceService;
class PerformanceStorage;
class PerformanceTiming;
class PerformanceEventTiming;
class WorkerPrivate;
class EventCounts;

// Base class for main-thread and worker Performance API
class Performance : public DOMEventTargetHelper {
 public:
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Performance, DOMEventTargetHelper)

  static bool IsObserverEnabled(JSContext* aCx, JSObject* aGlobal);

  static already_AddRefed<Performance> CreateForMainThread(
      nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
      nsDOMNavigationTiming* aDOMTiming, nsITimedChannel* aChannel);

  static already_AddRefed<Performance> CreateForWorker(
      WorkerPrivate* aWorkerPrivate);

  // This will return nullptr if called outside of a Window or Worker.
  static already_AddRefed<Performance> Get(JSContext* aCx,
                                           nsIGlobalObject* aGlobal);

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

  virtual void GetEntries(nsTArray<RefPtr<PerformanceEntry>>& aRetval);

  virtual void GetEntriesByType(const nsAString& aEntryType,
                                nsTArray<RefPtr<PerformanceEntry>>& aRetval);

  virtual void GetEntriesByTypeForObserver(
      const nsAString& aEntryType, nsTArray<RefPtr<PerformanceEntry>>& aRetval);

  virtual void GetEntriesByName(const nsAString& aName,
                                const Optional<nsAString>& aEntryType,
                                nsTArray<RefPtr<PerformanceEntry>>& aRetval);

  virtual PerformanceStorage* AsPerformanceStorage() = 0;

  void ClearResourceTimings();

  DOMHighResTimeStamp Now();

  DOMHighResTimeStamp NowUnclamped() const;

  DOMHighResTimeStamp TimeOrigin();

  already_AddRefed<PerformanceMark> Mark(
      JSContext* aCx, const nsAString& aName,
      const PerformanceMarkOptions& aMarkOptions, ErrorResult& aRv);

  void ClearMarks(const Optional<nsAString>& aName);

  already_AddRefed<PerformanceMeasure> Measure(
      JSContext* aCx, const nsAString& aName,
      const StringOrPerformanceMeasureOptions& aStartOrMeasureOptions,
      const Optional<nsAString>& aEndMark, ErrorResult& aRv);

  void ClearMeasures(const Optional<nsAString>& aName);

  void SetResourceTimingBufferSize(uint64_t aMaxSize);

  void AddObserver(PerformanceObserver* aObserver);
  void RemoveObserver(PerformanceObserver* aObserver);
  MOZ_CAN_RUN_SCRIPT void NotifyObservers();
  void CancelNotificationObservers();

  virtual PerformanceTiming* Timing() = 0;

  virtual PerformanceNavigation* Navigation() = 0;

  virtual void SetFCPTimingEntry(PerformancePaintTiming* aEntry) = 0;

  IMPL_EVENT_HANDLER(resourcetimingbufferfull)

  virtual void GetMozMemory(JSContext* aCx,
                            JS::MutableHandle<JSObject*> aObj) = 0;

  virtual nsDOMNavigationTiming* GetDOMTiming() const = 0;

  virtual nsITimedChannel* GetChannel() const = 0;

  virtual TimeStamp CreationTimeStamp() const = 0;

  RTPCallerType GetRTPCallerType() const { return mRTPCallerType; }

  DOMHighResTimeStamp TimeStampToDOMHighResForRendering(TimeStamp) const;

  virtual uint64_t GetRandomTimelineSeed() = 0;

  void MemoryPressure();

  size_t SizeOfUserEntries(mozilla::MallocSizeOf aMallocSizeOf) const;
  size_t SizeOfResourceEntries(mozilla::MallocSizeOf aMallocSizeOf) const;
  virtual size_t SizeOfEventEntries(mozilla::MallocSizeOf aMallocSizeOf) const {
    return 0;
  }

  void InsertResourceEntry(PerformanceEntry* aEntry);

  virtual void InsertEventTimingEntry(PerformanceEventTiming* aEntry) = 0;

  virtual void BufferEventTimingEntryIfNeeded(
      PerformanceEventTiming* aEntry) = 0;

  virtual class EventCounts* EventCounts() = 0;

  virtual void QueueNavigationTimingEntry() = 0;

  virtual void UpdateNavigationTimingEntry() = 0;

  virtual bool CrossOriginIsolated() const = 0;

  virtual void DispatchPendingEventTimingEntries() = 0;

  void QueueNotificationObserversTask();

  bool IsPerformanceTimingAttribute(const nsAString& aName) const;

  virtual bool IsGlobalObjectWindow() const { return false; };

 protected:
  Performance(nsIGlobalObject* aGlobal);
  Performance(nsPIDOMWindowInner* aWindow);

  virtual ~Performance();

  virtual void InsertUserEntry(PerformanceEntry* aEntry);

  void ClearUserEntries(const Optional<nsAString>& aEntryName,
                        const nsAString& aEntryType);

  virtual void DispatchBufferFullEvent() = 0;

  virtual DOMHighResTimeStamp CreationTime() const = 0;

  virtual DOMHighResTimeStamp GetPerformanceTimingFromString(
      const nsAString& aTimingName) {
    return 0;
  }

  void LogEntry(PerformanceEntry* aEntry, const nsACString& aOwner) const;
  void TimingNotification(PerformanceEntry* aEntry, const nsACString& aOwner,
                          const double aEpoch);

  void RunNotificationObserversTask();
  void QueueEntry(PerformanceEntry* aEntry);

  nsTObserverArray<RefPtr<PerformanceObserver>> mObservers;

 protected:
  static const uint64_t kDefaultResourceTimingBufferSize = 250;

  // When kDefaultResourceTimingBufferSize is increased or removed, these should
  // be changed to use SegmentedVector
  AutoTArray<RefPtr<PerformanceEntry>, kDefaultResourceTimingBufferSize>
      mUserEntries;
  AutoTArray<RefPtr<PerformanceEntry>, kDefaultResourceTimingBufferSize>
      mResourceEntries;
  AutoTArray<RefPtr<PerformanceEntry>, kDefaultResourceTimingBufferSize>
      mSecondaryResourceEntries;

  uint64_t mResourceTimingBufferSize;
  bool mPendingNotificationObserversTask;

  bool mPendingResourceTimingBufferFullEvent;

  RefPtr<PerformanceService> mPerformanceService;

  enum RTPCallerType mRTPCallerType;

 private:
  MOZ_ALWAYS_INLINE bool CanAddResourceTimingEntry();
  void BufferEvent();

  // The attributes of a PerformanceMeasureOptions that we call
  // ResolveTimestamp* on.
  enum class ResolveTimestampAttribute;

  DOMHighResTimeStamp ConvertMarkToTimestampWithString(const nsAString& aName,
                                                       ErrorResult& aRv);
  DOMHighResTimeStamp ConvertMarkToTimestampWithDOMHighResTimeStamp(
      const ResolveTimestampAttribute aAttribute, const double aTimestamp,
      ErrorResult& aRv);
  DOMHighResTimeStamp ConvertMarkToTimestamp(
      const ResolveTimestampAttribute aAttribute,
      const OwningStringOrDouble& aMarkNameOrTimestamp, ErrorResult& aRv);

  DOMHighResTimeStamp ConvertNameToTimestamp(const nsAString& aName,
                                             ErrorResult& aRv);

  DOMHighResTimeStamp ResolveEndTimeForMeasure(
      const Optional<nsAString>& aEndMark,
      const Maybe<const PerformanceMeasureOptions&>& aOptions,
      ErrorResult& aRv);
  DOMHighResTimeStamp ResolveStartTimeForMeasure(
      const Maybe<const nsAString&>& aStartMark,
      const Maybe<const PerformanceMeasureOptions&>& aOptions,
      ErrorResult& aRv);
};

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_Performance_h