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"
16 # include "mozilla/PolicyChecks.h"
18 #endif // defined(XP_WIN)
20 // Undo X11/X.h's definition of None
25 enum class SafeModeFlag
: uint32_t {
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
);
47 bool result
= ar
== ARG_FOUND
;
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")) {
62 if (result
&& PolicyCheckBoolean(L
"DisableSafeMode")) {
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")) {
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")) {
79 if (aFlags
& SafeModeFlag::Unset
) {
80 // unset the env variable
81 SaveToEnv("MOZ_SAFE_MODE_RESTART=");
88 } // namespace mozilla
90 #endif // mozilla_SafeMode_h