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_HangAnnotations_h
8 : #define mozilla_HangAnnotations_h
9 :
10 : #include <set>
11 :
12 : #include "mozilla/MemoryReporting.h"
13 : #include "mozilla/Mutex.h"
14 : #include "mozilla/UniquePtr.h"
15 : #include "mozilla/Vector.h"
16 : #include "nsString.h"
17 :
18 : namespace mozilla {
19 : namespace HangMonitor {
20 :
21 : /**
22 : * This class declares an abstraction for a data type that encapsulates all
23 : * of the annotations being reported by a registered hang Annotator.
24 : */
25 0 : class HangAnnotations
26 : {
27 : public:
28 0 : virtual ~HangAnnotations() {}
29 :
30 : virtual void AddAnnotation(const nsAString& aName, const int32_t aData) = 0;
31 : virtual void AddAnnotation(const nsAString& aName, const double aData) = 0;
32 : virtual void AddAnnotation(const nsAString& aName, const nsAString& aData) = 0;
33 : virtual void AddAnnotation(const nsAString& aName, const nsACString& aData) = 0;
34 : virtual void AddAnnotation(const nsAString& aName, const bool aData) = 0;
35 :
36 0 : class Enumerator
37 : {
38 : public:
39 0 : virtual ~Enumerator() {}
40 : virtual bool Next(nsAString& aOutName, nsAString& aOutValue) = 0;
41 : };
42 :
43 : virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const = 0;
44 : virtual bool IsEmpty() const = 0;
45 : virtual UniquePtr<Enumerator> GetEnumerator() = 0;
46 : };
47 :
48 : typedef UniquePtr<HangAnnotations> HangAnnotationsPtr;
49 : typedef Vector<HangAnnotationsPtr> HangAnnotationsVector;
50 :
51 3 : class Annotator
52 : {
53 : public:
54 : /**
55 : * NB: This function is always called by the HangMonitor thread.
56 : * Plan accordingly.
57 : */
58 : virtual void AnnotateHang(HangAnnotations& aAnnotations) = 0;
59 : };
60 :
61 : /**
62 : * Registers an Annotator to be called when a hang is detected.
63 : * @param aAnnotator Reference to an object that implements the
64 : * HangMonitor::Annotator interface.
65 : */
66 : void RegisterAnnotator(Annotator& aAnnotator);
67 :
68 : /**
69 : * Registers an Annotator that was previously registered via RegisterAnnotator.
70 : * @param aAnnotator Reference to an object that implements the
71 : * HangMonitor::Annotator interface.
72 : */
73 : void UnregisterAnnotator(Annotator& aAnnotator);
74 :
75 : /**
76 : * Gathers annotations. This function should be called by ChromeHangs.
77 : * @return UniquePtr to HangAnnotations object or nullptr if none.
78 : */
79 : HangAnnotationsPtr ChromeHangAnnotatorCallout();
80 :
81 : namespace Observer {
82 :
83 : class Annotators
84 : {
85 : public:
86 : Annotators();
87 : ~Annotators();
88 :
89 : bool Register(Annotator& aAnnotator);
90 : bool Unregister(Annotator& aAnnotator);
91 :
92 : HangAnnotationsPtr GatherAnnotations();
93 :
94 : private:
95 : Mutex mMutex;
96 : std::set<Annotator*> mAnnotators;
97 : };
98 :
99 : } // namespace Observer
100 :
101 : } // namespace HangMonitor
102 : } // namespace mozilla
103 :
104 : #endif // mozilla_HangAnnotations_h
|