summaryrefslogtreecommitdiff
path: root/mfbt/WindowsVersion.h
blob: 2f2cbac22bdb5cab027d9f29b11e1a35b9fc5cca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_WindowsVersion_h
#define mozilla_WindowsVersion_h

#include "mozilla/Atomics.h"
#include "mozilla/Attributes.h"
#include <stdint.h>
#include <windows.h>

namespace mozilla {

inline bool
IsWindowsVersionOrLater(uint32_t aVersion)
{
  static Atomic<uint32_t> minVersion(0);
  static Atomic<uint32_t> maxVersion(UINT32_MAX);

  if (minVersion >= aVersion) {
    return true;
  }

  if (aVersion >= maxVersion) {
    return false;
  }

  OSVERSIONINFOEX info;
  ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
  info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  info.dwMajorVersion = aVersion >> 24;
  info.dwMinorVersion = (aVersion >> 16) & 0xFF;
  info.wServicePackMajor = (aVersion >> 8) & 0xFF;
  info.wServicePackMinor = aVersion & 0xFF;

  DWORDLONG conditionMask = 0;
  VER_SET_CONDITION(conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
  VER_SET_CONDITION(conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
  VER_SET_CONDITION(conditionMask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
  VER_SET_CONDITION(conditionMask, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL);

  if (VerifyVersionInfo(&info,
                        VER_MAJORVERSION | VER_MINORVERSION |
                        VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
                        conditionMask)) {
    minVersion = aVersion;
    return true;
  }

  maxVersion = aVersion;
  return false;
}

inline bool
IsWindowsBuildOrLater(uint32_t aBuild)
{
  static Atomic<uint32_t> minBuild(0);
  static Atomic<uint32_t> maxBuild(UINT32_MAX);

  if (minBuild >= aBuild) {
    return true;
  }

  if (aBuild >= maxBuild) {
    return false;
  }

  OSVERSIONINFOEX info;
  ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
  info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  info.dwBuildNumber = aBuild;

  DWORDLONG conditionMask = 0;
  VER_SET_CONDITION(conditionMask, VER_BUILDNUMBER, VER_GREATER_EQUAL);

  if (VerifyVersionInfo(&info, VER_BUILDNUMBER, conditionMask)) {
    minBuild = aBuild;
    return true;
  }

  maxBuild = aBuild;
  return false;
}

inline bool
IsWindows10BuildOrLater(uint32_t aBuild)
{
  static Atomic<uint32_t> minBuild(0);
  static Atomic<uint32_t> maxBuild(UINT32_MAX);

  if (minBuild >= aBuild) {
    return true;
  }

  if (aBuild >= maxBuild) {
    return false;
  }

  OSVERSIONINFOEX info;
  ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
  info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  info.dwMajorVersion = 10;
  info.dwBuildNumber = aBuild;

  DWORDLONG conditionMask = 0;
  VER_SET_CONDITION(conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
  VER_SET_CONDITION(conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
  VER_SET_CONDITION(conditionMask, VER_BUILDNUMBER, VER_GREATER_EQUAL);
  VER_SET_CONDITION(conditionMask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
  VER_SET_CONDITION(conditionMask, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL);

  if (VerifyVersionInfo(&info,
                        VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER |
                        VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
                        conditionMask)) {
    minBuild = aBuild;
    return true;
  }

  maxBuild = aBuild;
  return false;
}

// Windows 7
MOZ_ALWAYS_INLINE bool
IsWin7SP1OrLater()
{
  return IsWindowsVersionOrLater(0x06010100ul);
}

// Windows 8
MOZ_ALWAYS_INLINE bool
IsWin8OrLater()
{
  return IsWindowsVersionOrLater(0x06020000ul);
}

// Windows 8.1
MOZ_ALWAYS_INLINE bool
IsWin8Point1OrLater()
{
  return IsWindowsVersionOrLater(0x06030000ul);
}

// Windows 10
MOZ_ALWAYS_INLINE bool
IsWin10OrLater()
{
  return IsWindowsVersionOrLater(0x0a000000ul);
}

// Windows 10 - RTM
MOZ_ALWAYS_INLINE bool
IsWin10v1507OrLater()
{
  return IsWindows10BuildOrLater(10240);
}

// Windows 10 - November Update
MOZ_ALWAYS_INLINE bool
IsWin10November2015UpdateOrLater()
{
  return IsWindows10BuildOrLater(10586);
}

// Windows 10 - Redstone 1 - Anniversary Update
MOZ_ALWAYS_INLINE bool
IsWin10AnniversaryUpdateOrLater()
{
  return IsWindows10BuildOrLater(14393);
}

// Windows 10 - Redstone 2 - Creators Update
MOZ_ALWAYS_INLINE bool
IsWin10CreatorsUpdateOrLater()
{
  return IsWindows10BuildOrLater(15063);
}

// Windows 10 - Redstone 3 - Fall Creators Update
MOZ_ALWAYS_INLINE bool
IsWin10FallCreatorsUpdateOrLater()
{
  return IsWindows10BuildOrLater(16299);
}

// Windows 10 - Redstone 4 - April 2018 Update
MOZ_ALWAYS_INLINE bool
IsWin10April2018UpdateOrLater()
{
  return IsWindows10BuildOrLater(17134);
}

// Windows 10 - Redstone 5 - September 2018 Update
MOZ_ALWAYS_INLINE bool
IsWin10Sep2018UpdateOrLater()
{
  return IsWindows10BuildOrLater(17763);
}

// Windows 10 - 19H1 - May 2019 Update
MOZ_ALWAYS_INLINE bool
IsWin10May2019UpdateOrLater()
{
  return IsWindows10BuildOrLater(18362);
}

// Windows 11 RTM
MOZ_ALWAYS_INLINE bool
IsWin11OrLater()
{
  // Windows 11 still identifies itself as NT 10.
  return IsWindows10BuildOrLater(22000);
}

// Compatibility version checks for the above for GRE porting.
// XXXMC: Needs cleanup. We shouldn't be using these.
MOZ_ALWAYS_INLINE bool IsWin10v1511OrLater() { return IsWin10November2015UpdateOrLater(); }
MOZ_ALWAYS_INLINE bool IsWin10v1607OrLater() { return IsWin10AnniversaryUpdateOrLater(); }
MOZ_ALWAYS_INLINE bool IsWin10v1703OrLater() { return IsWin10CreatorsUpdateOrLater(); }
MOZ_ALWAYS_INLINE bool IsWin10v1709OrLater() { return IsWin10FallCreatorsUpdateOrLater(); }
MOZ_ALWAYS_INLINE bool IsWin10v1803OrLater() { return IsWin10April2018UpdateOrLater(); }
MOZ_ALWAYS_INLINE bool IsWin10v1809OrLater() { return IsWin10Sep2018UpdateOrLater(); }
MOZ_ALWAYS_INLINE bool IsWin10v1903OrLater() { return IsWin10May2019UpdateOrLater(); }

// --- Additional granular version checks.
// XXXMC: Not sure why we can't just use IsWindows10BuildOrLater(build) directly, or at least
// a helper function taking an enum or something if build numbers are too difficult to remember.
// Unnecessary indirection and function declaration inflation. Kept for now, needs cleanup.

// Windows 10 - 19H2 - November 2019 Update
MOZ_ALWAYS_INLINE bool
IsWin10v1909OrLater()
{
  return IsWindows10BuildOrLater(18363);
}

// Windows 10 - 20H1 - May 2020 Update
MOZ_ALWAYS_INLINE bool
IsWin10v2004OrLater()
{
  return IsWindows10BuildOrLater(19041);
}

// Windows 10 - 20H2 - October 2020 Update
MOZ_ALWAYS_INLINE bool
IsWin10v20H2OrLater()
{
  return IsWindows10BuildOrLater(19042);
}

// Windows 10 - 21H1 - May 2021 Update
MOZ_ALWAYS_INLINE bool
IsWin10v21H1OrLater()
{
  return IsWindows10BuildOrLater(19043);
}

// Windows 10 - 21H2 - November 2021 Update
MOZ_ALWAYS_INLINE bool
IsWin10v21H2OrLater()
{
  return IsWindows10BuildOrLater(19044);
}

// --- End additional granular version checks.

MOZ_ALWAYS_INLINE bool
IsNotWin7PreRTM()
{
  return IsWin7SP1OrLater() || IsWindowsBuildOrLater(7600);
}

// Compatibility Mode
inline bool
IsWin7AndPre2000Compatible()
{
  /*
   * See Bug 1279171.
   * We'd like to avoid using WMF on specific OS version when compatibility
   * mode is in effect. The purpose of this function is to check if FF runs on
   * Win7 OS with application compatibility mode being set to 95/98/ME.
   * Those compatibility mode options (95/98/ME) can only display and
   * be selected for 32-bit application.
   * If the compatibility mode is in effect, the GetVersionEx function will
   * report the OS as it identifies itself, which may not be the OS that is
   * installed.
   * Note : 1) We only target for Win7 build number greater than 7600.
   *        2) GetVersionEx may be altered or unavailable for release after
   *           Win8.1. Set pragma to avoid build warning as error.
   */
  bool isWin7 = IsNotWin7PreRTM() && !IsWin8OrLater();
  if (!isWin7) {
    return false;
  }

  OSVERSIONINFOEX info;
  ZeroMemory(&info, sizeof(OSVERSIONINFOEX));

  info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
#pragma warning(push)
#pragma warning(disable : 4996)
  bool success = GetVersionEx((LPOSVERSIONINFO)&info);
#pragma warning(pop)
  if (!success) {
    return false;
  }
  return info.dwMajorVersion < 5;
}

}  // namespace mozilla

#endif /* mozilla_WindowsVersion_h */