Line data Source code
1 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 :
6 : #ifndef mozilla_TimelineConsumers_h_
7 : #define mozilla_TimelineConsumers_h_
8 :
9 : #include "nsIObserver.h"
10 : #include "mozilla/StaticPtr.h"
11 : #include "mozilla/UniquePtr.h"
12 : #include "mozilla/LinkedList.h"
13 : #include "mozilla/StaticMutex.h"
14 : #include "TimelineMarkerEnums.h" // for MarkerTracingType
15 :
16 : class nsDocShell;
17 : class nsIDocShell;
18 : struct JSContext;
19 :
20 : namespace mozilla {
21 : class TimeStamp;
22 : class MarkersStorage;
23 : class AbstractTimelineMarker;
24 :
25 : namespace dom {
26 : struct ProfileTimelineMarker;
27 : }
28 :
29 : class TimelineConsumers : public nsIObserver
30 : {
31 : public:
32 : NS_DECL_THREADSAFE_ISUPPORTS
33 : NS_DECL_NSIOBSERVER
34 :
35 : private:
36 : TimelineConsumers();
37 : TimelineConsumers(const TimelineConsumers& aOther) = delete;
38 : void operator=(const TimelineConsumers& aOther) = delete;
39 0 : virtual ~TimelineConsumers() = default;
40 :
41 : bool Init();
42 : bool RemoveObservers();
43 :
44 : public:
45 : static already_AddRefed<TimelineConsumers> Get();
46 :
47 : // Methods for registering interested consumers (i.e. "devtools toolboxes").
48 : // Each consumer should be directly focused on a particular docshell, but
49 : // timeline markers don't necessarily have to be tied to that docshell.
50 : // See the public `AddMarker*` methods below.
51 : // Main thread only.
52 : void AddConsumer(nsDocShell* aDocShell);
53 : void RemoveConsumer(nsDocShell* aDocShell);
54 :
55 : bool HasConsumer(nsIDocShell* aDocShell);
56 :
57 : // Checks if there's any existing interested consumer.
58 : // May be called from any thread.
59 : bool IsEmpty();
60 :
61 : // Methods for adding markers relevant for particular docshells, or generic
62 : // (meaning that they either can't be tied to a particular docshell, or one
63 : // wasn't accessible in the part of the codebase where they're instantiated).
64 : // These will only add markers if at least one docshell is currently being
65 : // observed by a timeline. Markers tied to a particular docshell won't be
66 : // created unless that docshell is specifically being currently observed.
67 : // See nsIDocShell::recordProfileTimelineMarkers
68 :
69 : // These methods create a basic TimelineMarker from a name and some metadata,
70 : // relevant for a specific docshell.
71 : // Main thread only.
72 : void AddMarkerForDocShell(nsDocShell* aDocShell,
73 : const char* aName,
74 : MarkerTracingType aTracingType,
75 : MarkerStackRequest aStackRequest = MarkerStackRequest::STACK);
76 : void AddMarkerForDocShell(nsIDocShell* aDocShell,
77 : const char* aName,
78 : MarkerTracingType aTracingType,
79 : MarkerStackRequest aStackRequest = MarkerStackRequest::STACK);
80 :
81 : void AddMarkerForDocShell(nsDocShell* aDocShell,
82 : const char* aName,
83 : const TimeStamp& aTime,
84 : MarkerTracingType aTracingType,
85 : MarkerStackRequest aStackRequest = MarkerStackRequest::STACK);
86 : void AddMarkerForDocShell(nsIDocShell* aDocShell,
87 : const char* aName,
88 : const TimeStamp& aTime,
89 : MarkerTracingType aTracingType,
90 : MarkerStackRequest aStackRequest = MarkerStackRequest::STACK);
91 :
92 : // These methods register and receive ownership of an already created marker,
93 : // relevant for a specific docshell.
94 : // Main thread only.
95 : void AddMarkerForDocShell(nsDocShell* aDocShell,
96 : UniquePtr<AbstractTimelineMarker>&& aMarker);
97 : void AddMarkerForDocShell(nsIDocShell* aDocShell,
98 : UniquePtr<AbstractTimelineMarker>&& aMarker);
99 :
100 : // These methods create a basic marker from a name and some metadata,
101 : // which doesn't have to be relevant to a specific docshell.
102 : // May be called from any thread.
103 : void AddMarkerForAllObservedDocShells(const char* aName,
104 : MarkerTracingType aTracingType,
105 : MarkerStackRequest aStackRequest = MarkerStackRequest::STACK);
106 : void AddMarkerForAllObservedDocShells(const char* aName,
107 : const TimeStamp& aTime,
108 : MarkerTracingType aTracingType,
109 : MarkerStackRequest aStackRequest = MarkerStackRequest::STACK);
110 :
111 : // This method clones and registers an already instantiated marker,
112 : // which doesn't have to be relevant to a specific docshell.
113 : // May be called from any thread.
114 : void AddMarkerForAllObservedDocShells(UniquePtr<AbstractTimelineMarker>& aMarker);
115 :
116 : void PopMarkers(nsDocShell* aDocShell,
117 : JSContext* aCx,
118 : nsTArray<dom::ProfileTimelineMarker>& aStore);
119 :
120 : private:
121 : static StaticRefPtr<TimelineConsumers> sInstance;
122 : static bool sInShutdown;
123 :
124 : // Counter for how many timelines are currently interested in markers,
125 : // and a list of the MarkersStorage interfaces representing them.
126 : unsigned long mActiveConsumers;
127 : LinkedList<MarkersStorage> mMarkersStores;
128 :
129 : // Protects this class's data structures.
130 : static StaticMutex sMutex;
131 : };
132 :
133 : } // namespace mozilla
134 :
135 : #endif /* mozilla_TimelineConsumers_h_ */
|