Backed out changeset af2c13b9fdb7 (bug 1801244) per developer request.
[gecko.git] / toolkit / xre / SafeMode.h
blob9323ff1a9cd2678dbbdcff063cac9f998453210f
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 https://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_SafeMode_h
8 #define mozilla_SafeMode_h
10 // NB: This code must be able to run apart from XPCOM
12 #include "mozilla/CmdLineAndEnvUtils.h"
13 #include "mozilla/Maybe.h"
15 #if defined(XP_WIN)
16 # include "mozilla/PolicyChecks.h"
17 # include <windows.h>
18 #endif // defined(XP_WIN)
20 // Undo X11/X.h's definition of None
21 #undef None
23 namespace mozilla {
25 enum class SafeModeFlag : uint32_t {
26 None = 0,
27 Unset = (1 << 0),
28 NoKeyPressCheck = (1 << 1),
31 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(SafeModeFlag)
33 template <typename CharT>
34 inline Maybe<bool> IsSafeModeRequested(
35 int& aArgc, CharT* aArgv[],
36 const SafeModeFlag aFlags = SafeModeFlag::Unset) {
37 CheckArgFlag checkArgFlags = CheckArgFlag::None;
38 if (aFlags & SafeModeFlag::Unset) {
39 checkArgFlags |= CheckArgFlag::RemoveArg;
42 ArgResult ar = CheckArg(aArgc, aArgv, "safe-mode", nullptr, checkArgFlags);
43 if (ar == ARG_BAD) {
44 return Nothing();
47 bool result = ar == ARG_FOUND;
49 #if defined(XP_WIN)
50 // If the shift key is pressed and the ctrl and / or alt keys are not pressed
51 // during startup, start in safe mode. GetKeyState returns a short and the
52 // high order bit will be 1 if the key is pressed. By masking the returned
53 // short with 0x8000 the result will be 0 if the key is not pressed and
54 // non-zero otherwise.
55 if (!(aFlags & SafeModeFlag::NoKeyPressCheck) &&
56 (GetKeyState(VK_SHIFT) & 0x8000) && !(GetKeyState(VK_CONTROL) & 0x8000) &&
57 !(GetKeyState(VK_MENU) & 0x8000) &&
58 !EnvHasValue("MOZ_DISABLE_SAFE_MODE_KEY")) {
59 result = true;
62 if (result && PolicyCheckBoolean(L"DisableSafeMode")) {
63 result = false;
65 #endif // defined(XP_WIN)
67 #if defined(XP_MACOSX)
68 if (!(aFlags & SafeModeFlag::NoKeyPressCheck) &&
69 (GetCurrentEventKeyModifiers() & optionKey) &&
70 !EnvHasValue("MOZ_DISABLE_SAFE_MODE_KEY")) {
71 result = true;
73 #endif // defined(XP_MACOSX)
75 // The Safe Mode Policy should not be enforced for the env var case
76 // (used by updater and crash-recovery).
77 if (EnvHasValue("MOZ_SAFE_MODE_RESTART")) {
78 result = true;
79 if (aFlags & SafeModeFlag::Unset) {
80 // unset the env variable
81 SaveToEnv("MOZ_SAFE_MODE_RESTART=");
85 return Some(result);
88 } // namespace mozilla
90 #endif // mozilla_SafeMode_h