CheckerboardReportService.h (4389B)
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_CheckerboardReportService_h 8 #define mozilla_dom_CheckerboardReportService_h 9 10 #include <string> 11 12 #include "js/TypeDecls.h" // for JSContext, JSObject 13 #include "mozilla/StaticPtr.h" // for StaticRefPtr 14 #include "nsCOMPtr.h" // for nsCOMPtr 15 #include "nsISupports.h" // for NS_INLINE_DECL_REFCOUNTING 16 #include "nsTArrayForwardDeclare.h" // for nsTArray 17 #include "nsWrapperCache.h" // for nsWrapperCache 18 19 namespace mozilla { 20 21 namespace dom { 22 struct CheckerboardReport; 23 } 24 25 namespace layers { 26 27 // CheckerboardEventStorage is a singleton that stores info on checkerboard 28 // events, so that they can be accessed from about:checkerboard and visualized. 29 // Note that this class is NOT threadsafe, and all methods must be called on 30 // the main thread. 31 class CheckerboardEventStorage { 32 NS_INLINE_DECL_REFCOUNTING(CheckerboardEventStorage) 33 34 public: 35 /** 36 * Get the singleton instance. 37 */ 38 static already_AddRefed<CheckerboardEventStorage> GetInstance(); 39 40 /** 41 * Get the stored checkerboard reports. 42 */ 43 void GetReports(nsTArray<dom::CheckerboardReport>& aOutReports); 44 45 /** 46 * Save a checkerboard event log, optionally dropping older ones that were 47 * less severe or less recent. Zero-severity reports may be ignored entirely. 48 */ 49 static void Report(uint32_t aSeverity, const std::string& aLog); 50 51 private: 52 /* Stuff for refcounted singleton */ 53 CheckerboardEventStorage() = default; 54 virtual ~CheckerboardEventStorage() = default; 55 56 static StaticRefPtr<CheckerboardEventStorage> sInstance; 57 58 void ReportCheckerboard(uint32_t aSeverity, const std::string& aLog); 59 60 private: 61 /** 62 * Struct that this class uses internally to store a checkerboard report. 63 */ 64 struct CheckerboardReport { 65 uint32_t mSeverity; // if 0, this report is empty 66 int64_t mTimestamp; // microseconds since epoch, as from JS_Now() 67 std::string mLog; 68 69 CheckerboardReport() : mSeverity(0), mTimestamp(0) {} 70 71 CheckerboardReport(uint32_t aSeverity, int64_t aTimestamp, 72 const std::string& aLog) 73 : mSeverity(aSeverity), mTimestamp(aTimestamp), mLog(aLog) {} 74 }; 75 76 // The first 5 (indices 0-4) are the most severe ones in decreasing order 77 // of severity; the next 5 (indices 5-9) are the most recent ones that are 78 // not already in the "severe" list. 79 static const int SEVERITY_MAX_INDEX = 5; 80 static const int RECENT_MAX_INDEX = 10; 81 CheckerboardReport mCheckerboardReports[RECENT_MAX_INDEX]; 82 }; 83 84 } // namespace layers 85 86 namespace dom { 87 88 class GlobalObject; 89 90 /** 91 * CheckerboardReportService is a wrapper object that allows access to the 92 * stuff in CheckerboardEventStorage (above). We need this wrapper for proper 93 * garbage/cycle collection, since this can be accessed from JS. 94 */ 95 class CheckerboardReportService : public nsWrapperCache { 96 public: 97 /** 98 * Check if the given page is allowed to access this object via the WebIDL 99 * bindings. It only returns true if the page is about:checkerboard. 100 */ 101 static bool IsEnabled(JSContext* aCtx, JSObject* aGlobal); 102 103 /* 104 * Other standard WebIDL binding glue. 105 */ 106 107 static already_AddRefed<CheckerboardReportService> Constructor( 108 const dom::GlobalObject& aGlobal); 109 110 explicit CheckerboardReportService(nsISupports* aSupports); 111 112 JSObject* WrapObject(JSContext* aCtx, 113 JS::Handle<JSObject*> aGivenProto) override; 114 115 nsISupports* GetParentObject(); 116 117 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(CheckerboardReportService) 118 NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(CheckerboardReportService) 119 120 public: 121 /* 122 * The methods exposed via the webidl. 123 */ 124 void GetReports(nsTArray<dom::CheckerboardReport>& aOutReports); 125 bool IsRecordingEnabled() const; 126 void SetRecordingEnabled(bool aEnabled); 127 void FlushActiveReports(); 128 129 private: 130 virtual ~CheckerboardReportService() = default; 131 132 nsCOMPtr<nsISupports> mParent; 133 }; 134 135 } // namespace dom 136 } // namespace mozilla 137 138 #endif /* mozilla_layers_CheckerboardReportService_h */