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