Performance.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_Performance_h
  6. #define mozilla_dom_Performance_h
  7. #include "mozilla/Attributes.h"
  8. #include "mozilla/DOMEventTargetHelper.h"
  9. #include "nsCOMPtr.h"
  10. #include "nsDOMNavigationTiming.h"
  11. class nsITimedChannel;
  12. class nsIHttpChannel;
  13. namespace mozilla {
  14. class ErrorResult;
  15. namespace dom {
  16. class PerformanceEntry;
  17. class PerformanceNavigation;
  18. class PerformanceObserver;
  19. class PerformanceService;
  20. class PerformanceTiming;
  21. namespace workers {
  22. class WorkerPrivate;
  23. }
  24. // Base class for main-thread and worker Performance API
  25. class Performance : public DOMEventTargetHelper
  26. {
  27. public:
  28. NS_DECL_ISUPPORTS_INHERITED
  29. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Performance,
  30. DOMEventTargetHelper)
  31. static bool IsEnabled(JSContext* aCx, JSObject* aGlobal);
  32. static bool IsObserverEnabled(JSContext* aCx, JSObject* aGlobal);
  33. static already_AddRefed<Performance>
  34. CreateForMainThread(nsPIDOMWindowInner* aWindow,
  35. nsDOMNavigationTiming* aDOMTiming,
  36. nsITimedChannel* aChannel);
  37. static already_AddRefed<Performance>
  38. CreateForWorker(workers::WorkerPrivate* aWorkerPrivate);
  39. JSObject* WrapObject(JSContext *cx,
  40. JS::Handle<JSObject*> aGivenProto) override;
  41. virtual void GetEntries(nsTArray<RefPtr<PerformanceEntry>>& aRetval);
  42. virtual void GetEntriesByType(const nsAString& aEntryType,
  43. nsTArray<RefPtr<PerformanceEntry>>& aRetval);
  44. virtual void GetEntriesByName(const nsAString& aName,
  45. const Optional<nsAString>& aEntryType,
  46. nsTArray<RefPtr<PerformanceEntry>>& aRetval);
  47. virtual void AddEntry(nsIHttpChannel* channel,
  48. nsITimedChannel* timedChannel) = 0;
  49. void ClearResourceTimings();
  50. DOMHighResTimeStamp Now() const;
  51. DOMHighResTimeStamp TimeOrigin();
  52. void Mark(const nsAString& aName, ErrorResult& aRv);
  53. void ClearMarks(const Optional<nsAString>& aName);
  54. void Measure(const nsAString& aName,
  55. const Optional<nsAString>& aStartMark,
  56. const Optional<nsAString>& aEndMark,
  57. ErrorResult& aRv);
  58. void ClearMeasures(const Optional<nsAString>& aName);
  59. void SetResourceTimingBufferSize(uint64_t aMaxSize);
  60. void AddObserver(PerformanceObserver* aObserver);
  61. void RemoveObserver(PerformanceObserver* aObserver);
  62. void NotifyObservers();
  63. void CancelNotificationObservers();
  64. virtual PerformanceTiming* Timing() = 0;
  65. virtual PerformanceNavigation* Navigation() = 0;
  66. IMPL_EVENT_HANDLER(resourcetimingbufferfull)
  67. #ifdef MOZ_DEVTOOLS_SERVER
  68. virtual void GetMozMemory(JSContext *aCx,
  69. JS::MutableHandle<JSObject*> aObj) = 0;
  70. #endif
  71. virtual nsDOMNavigationTiming* GetDOMTiming() const = 0;
  72. virtual nsITimedChannel* GetChannel() const = 0;
  73. protected:
  74. Performance();
  75. explicit Performance(nsPIDOMWindowInner* aWindow);
  76. virtual ~Performance();
  77. virtual void InsertUserEntry(PerformanceEntry* aEntry);
  78. void InsertResourceEntry(PerformanceEntry* aEntry);
  79. void ClearUserEntries(const Optional<nsAString>& aEntryName,
  80. const nsAString& aEntryType);
  81. DOMHighResTimeStamp ResolveTimestampFromName(const nsAString& aName,
  82. ErrorResult& aRv);
  83. virtual nsISupports* GetAsISupports() = 0;
  84. virtual void DispatchBufferFullEvent() = 0;
  85. virtual TimeStamp CreationTimeStamp() const = 0;
  86. virtual DOMHighResTimeStamp CreationTime() const = 0;
  87. virtual bool IsPerformanceTimingAttribute(const nsAString& aName)
  88. {
  89. return false;
  90. }
  91. virtual DOMHighResTimeStamp
  92. GetPerformanceTimingFromString(const nsAString& aTimingName)
  93. {
  94. return 0;
  95. }
  96. bool IsResourceEntryLimitReached() const
  97. {
  98. return mResourceEntries.Length() >= mResourceTimingBufferSize;
  99. }
  100. void LogEntry(PerformanceEntry* aEntry, const nsACString& aOwner) const;
  101. void TimingNotification(PerformanceEntry* aEntry, const nsACString& aOwner,
  102. uint64_t epoch);
  103. void RunNotificationObserversTask();
  104. void QueueEntry(PerformanceEntry* aEntry);
  105. DOMHighResTimeStamp RoundTime(double aTime) const;
  106. nsTObserverArray<PerformanceObserver*> mObservers;
  107. protected:
  108. nsTArray<RefPtr<PerformanceEntry>> mUserEntries;
  109. nsTArray<RefPtr<PerformanceEntry>> mResourceEntries;
  110. uint64_t mResourceTimingBufferSize;
  111. static const uint64_t kDefaultResourceTimingBufferSize = 150;
  112. bool mPendingNotificationObserversTask;
  113. RefPtr<PerformanceService> mPerformanceService;
  114. };
  115. } // namespace dom
  116. } // namespace mozilla
  117. #endif // mozilla_dom_Performance_h