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_Performance_h
8 #define mozilla_dom_Performance_h
9 
10 #include "mozilla/Attributes.h"
11 #include "mozilla/DOMEventTargetHelper.h"
12 #include "mozilla/dom/DOMPrefs.h"
13 #include "nsCOMPtr.h"
14 #include "nsDOMNavigationTiming.h"
15 
16 class nsITimedChannel;
17 
18 namespace mozilla {
19 
20 class ErrorResult;
21 
22 namespace dom {
23 
24 class PerformanceEntry;
25 class PerformanceNavigation;
26 class PerformanceObserver;
27 class PerformanceService;
28 class PerformanceStorage;
29 class PerformanceTiming;
30 class WorkerPrivate;
31 
32 // Base class for main-thread and worker Performance API
33 class Performance : public DOMEventTargetHelper {
34  public:
35   NS_DECL_ISUPPORTS_INHERITED
36   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Performance, DOMEventTargetHelper)
37 
38   static bool IsObserverEnabled(JSContext* aCx, JSObject* aGlobal);
39 
40   static already_AddRefed<Performance> CreateForMainThread(
41       nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
42       nsDOMNavigationTiming* aDOMTiming, nsITimedChannel* aChannel);
43 
44   static already_AddRefed<Performance> CreateForWorker(
45       WorkerPrivate* aWorkerPrivate);
46 
47   JSObject* WrapObject(JSContext* cx,
48                        JS::Handle<JSObject*> aGivenProto) override;
49 
50   virtual void GetEntries(nsTArray<RefPtr<PerformanceEntry>>& aRetval);
51 
52   virtual void GetEntriesByType(const nsAString& aEntryType,
53                                 nsTArray<RefPtr<PerformanceEntry>>& aRetval);
54 
55   virtual void GetEntriesByName(const nsAString& aName,
56                                 const Optional<nsAString>& aEntryType,
57                                 nsTArray<RefPtr<PerformanceEntry>>& aRetval);
58 
59   virtual PerformanceStorage* AsPerformanceStorage() = 0;
60 
61   void ClearResourceTimings();
62 
63   DOMHighResTimeStamp Now();
64 
65   DOMHighResTimeStamp NowUnclamped() const;
66 
67   DOMHighResTimeStamp TimeOrigin();
68 
69   void Mark(const nsAString& aName, ErrorResult& aRv);
70 
71   void ClearMarks(const Optional<nsAString>& aName);
72 
73   void Measure(const nsAString& aName, const Optional<nsAString>& aStartMark,
74                const Optional<nsAString>& aEndMark, ErrorResult& aRv);
75 
76   void ClearMeasures(const Optional<nsAString>& aName);
77 
78   void SetResourceTimingBufferSize(uint64_t aMaxSize);
79 
80   void AddObserver(PerformanceObserver* aObserver);
81   void RemoveObserver(PerformanceObserver* aObserver);
82   void NotifyObservers();
83   void CancelNotificationObservers();
84 
85   virtual PerformanceTiming* Timing() = 0;
86 
87   virtual PerformanceNavigation* Navigation() = 0;
88 
89   IMPL_EVENT_HANDLER(resourcetimingbufferfull)
90 
91   virtual void GetMozMemory(JSContext* aCx,
92                             JS::MutableHandle<JSObject*> aObj) = 0;
93 
94   virtual nsDOMNavigationTiming* GetDOMTiming() const = 0;
95 
96   virtual nsITimedChannel* GetChannel() const = 0;
97 
98   virtual TimeStamp CreationTimeStamp() const = 0;
99 
IsSystemPrincipal()100   uint64_t IsSystemPrincipal() { return mSystemPrincipal; }
101 
102   virtual uint64_t GetRandomTimelineSeed() = 0;
103 
104   void MemoryPressure();
105 
106   size_t SizeOfUserEntries(mozilla::MallocSizeOf aMallocSizeOf) const;
107   size_t SizeOfResourceEntries(mozilla::MallocSizeOf aMallocSizeOf) const;
108 
109   void InsertResourceEntry(PerformanceEntry* aEntry);
110 
111  protected:
112   Performance();
113   explicit Performance(nsPIDOMWindowInner* aWindow);
114 
115   virtual ~Performance();
116 
117   virtual void InsertUserEntry(PerformanceEntry* aEntry);
118 
119   void ClearUserEntries(const Optional<nsAString>& aEntryName,
120                         const nsAString& aEntryType);
121 
122   DOMHighResTimeStamp ResolveTimestampFromName(const nsAString& aName,
123                                                ErrorResult& aRv);
124 
125   virtual void DispatchBufferFullEvent() = 0;
126 
127   virtual DOMHighResTimeStamp CreationTime() const = 0;
128 
IsPerformanceTimingAttribute(const nsAString & aName)129   virtual bool IsPerformanceTimingAttribute(const nsAString& aName) {
130     return false;
131   }
132 
GetPerformanceTimingFromString(const nsAString & aTimingName)133   virtual DOMHighResTimeStamp GetPerformanceTimingFromString(
134       const nsAString& aTimingName) {
135     return 0;
136   }
137 
138   void LogEntry(PerformanceEntry* aEntry, const nsACString& aOwner) const;
139   void TimingNotification(PerformanceEntry* aEntry, const nsACString& aOwner,
140                           uint64_t epoch);
141 
142   void RunNotificationObserversTask();
143   void QueueEntry(PerformanceEntry* aEntry);
144 
145   nsTObserverArray<PerformanceObserver*> mObservers;
146 
147  protected:
148   static const uint64_t kDefaultResourceTimingBufferSize = 150;
149 
150   // When kDefaultResourceTimingBufferSize is increased or removed, these should
151   // be changed to use SegmentedVector
152   AutoTArray<RefPtr<PerformanceEntry>, kDefaultResourceTimingBufferSize>
153       mUserEntries;
154   AutoTArray<RefPtr<PerformanceEntry>, kDefaultResourceTimingBufferSize>
155       mResourceEntries;
156 
157   uint64_t mResourceTimingBufferSize;
158   bool mPendingNotificationObserversTask;
159 
160   RefPtr<PerformanceService> mPerformanceService;
161 
162   bool mSystemPrincipal;
163 };
164 
165 }  // namespace dom
166 }  // namespace mozilla
167 
168 #endif  // mozilla_dom_Performance_h
169