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 #ifndef gfx_src_DriverCrashGuard_h__
7 #define gfx_src_DriverCrashGuard_h__
10 #include "nsIGfxInfo.h"
24 enum class DriverInitStatus
{
25 // Drivers have not been initialized yet.
28 // We're attempting to initialize drivers.
31 // Drivers were successfully initialized last run.
34 // We crashed during driver initialization, and have restarted.
38 enum class CrashGuardType
: uint32_t {
42 // Add new entries above this line, update the name array in
43 // DriverCrashGuard.cpp, make sure to add an entry in ContentParent.cpp,
44 // and add a fluent identifier in aboutSupport.ftl.
49 // DriverCrashGuard is used to detect crashes at graphics driver callsites.
51 // If the graphics environment is unrecognized or has changed since the last
52 // session, the crash guard will activate and will detect any crashes within
53 // the scope of the guard object.
55 // If a callsite has a previously encountered crash, and the environment has
56 // not changed since the last session, then the guard will set a status flag
57 // indicating that the driver should not be used.
58 class DriverCrashGuard
{
60 DriverCrashGuard(CrashGuardType aType
, dom::ContentParent
* aContentParent
);
61 virtual ~DriverCrashGuard();
66 // These are the values reported to Telemetry (GRAPHICS_DRIVER_STARTUP_TEST).
67 // Values should not change; add new values to the end.
68 enum class TelemetryState
{
70 EnvironmentChanged
= 1,
71 RecoveredFromCrash
= 2,
79 // Acting as a proxy between the parent and child process.
83 typedef std::function
<void(const char* aName
, const char* aPrefName
)>
85 static void ForEachActiveCrashGuard(const CrashGuardCallback
& aCallback
);
88 virtual void Initialize();
89 // UpdateEnvironment needs to return true should we need to attempt the
90 // operation once again.
91 // It should return true once only so that in case of a crash, we won't
92 // needlessly attempt the operation over and over again leading to continual
93 // crashes. several times
94 virtual bool UpdateEnvironment() {
95 // We don't care about any extra preferences here.
98 virtual void LogCrashRecovery() = 0;
99 virtual void LogFeatureDisabled() = 0;
102 bool FeatureEnabled(int aFeature
, bool aDefault
= true);
103 bool CheckAndUpdatePref(const char* aPrefName
,
104 const nsAString
& aCurrentValue
);
105 bool CheckAndUpdateBoolPref(const char* aPrefName
, bool aCurrentValue
);
106 std::string
GetFullPrefName(const char* aPref
);
110 void InitializeIfNeeded();
111 bool CheckOrRefreshEnvironment();
112 bool UpdateBaseEnvironment();
113 DriverInitStatus
GetStatus() const;
115 // Parent process only.
116 nsCOMPtr
<nsIFile
> GetGuardFile();
117 bool RecoverFromCrash();
118 void ActivateGuard();
119 void FlushPreferences();
120 void SetStatus(DriverInitStatus aStatus
);
123 CrashGuardType mType
;
126 bool mGuardActivated
;
128 nsCOMPtr
<nsIFile
> mGuardFile
;
131 nsCString mStatusPref
;
132 nsCOMPtr
<nsIGfxInfo
> mGfxInfo
;
135 class D3D11LayersCrashGuard final
: public DriverCrashGuard
{
137 explicit D3D11LayersCrashGuard(dom::ContentParent
* aContentParent
= nullptr);
140 void Initialize() override
;
141 bool UpdateEnvironment() override
;
142 void LogCrashRecovery() override
;
143 void LogFeatureDisabled() override
;
146 void RecordTelemetry(TelemetryState aState
);
149 class GLContextCrashGuard final
: public DriverCrashGuard
{
151 explicit GLContextCrashGuard(dom::ContentParent
* aContentParent
= nullptr);
152 void Initialize() override
;
155 bool UpdateEnvironment() override
;
156 void LogCrashRecovery() override
;
157 void LogFeatureDisabled() override
;
160 class WMFVPXVideoCrashGuard final
: public DriverCrashGuard
{
162 explicit WMFVPXVideoCrashGuard(dom::ContentParent
* aContentParent
= nullptr);
165 void LogCrashRecovery() override
;
166 void LogFeatureDisabled() override
;
170 } // namespace mozilla
172 #endif // gfx_src_DriverCrashGuard_h__