Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / widget / cocoa / nsCocoaFeatures.mm
blob1dd634405100c0d0c4eada2e4b11fa969b31ba31
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 // This file makes some assumptions about the versions of macOS.
7 // We are assuming that the major, minor and bugfix versions are each less than
8 // 256.
9 // There are MOZ_ASSERTs for that.
11 // The formula for the version integer is (major << 16) + (minor << 8) + bugfix.
13 #define MACOS_VERSION_MASK 0x00FFFFFF
14 #define MACOS_MAJOR_VERSION_MASK 0x00FFFFFF
15 #define MACOS_MINOR_VERSION_MASK 0x00FFFFFF
16 #define MACOS_BUGFIX_VERSION_MASK 0x00FFFFFF
17 #define MACOS_VERSION_10_0_HEX 0x000A0000
18 #define MACOS_VERSION_10_9_HEX 0x000A0900
19 #define MACOS_VERSION_10_10_HEX 0x000A0A00
20 #define MACOS_VERSION_10_11_HEX 0x000A0B00
21 #define MACOS_VERSION_10_12_HEX 0x000A0C00
22 #define MACOS_VERSION_10_13_HEX 0x000A0D00
23 #define MACOS_VERSION_10_14_HEX 0x000A0E00
24 #define MACOS_VERSION_10_15_HEX 0x000A0F00
25 #define MACOS_VERSION_10_16_HEX 0x000A1000
26 #define MACOS_VERSION_11_0_HEX 0x000B0000
27 #define MACOS_VERSION_12_0_HEX 0x000C0000
28 #define MACOS_VERSION_13_0_HEX 0x000D0000
30 #include "nsCocoaFeatures.h"
31 #include "nsCocoaUtils.h"
32 #include "nsDebug.h"
33 #include "nsObjCExceptions.h"
35 #import <Cocoa/Cocoa.h>
36 #include <sys/sysctl.h>
38 /*static*/ int32_t nsCocoaFeatures::mOSVersion = 0;
40 // This should not be called with unchecked aMajor, which should be >= 10.
41 inline int32_t AssembleVersion(int32_t aMajor, int32_t aMinor,
42                                int32_t aBugFix) {
43   MOZ_ASSERT(aMajor >= 10);
44   return (aMajor << 16) + (aMinor << 8) + aBugFix;
47 int32_t nsCocoaFeatures::ExtractMajorVersion(int32_t aVersion) {
48   MOZ_ASSERT((aVersion & MACOS_VERSION_MASK) == aVersion);
49   return (aVersion & 0xFF0000) >> 16;
52 int32_t nsCocoaFeatures::ExtractMinorVersion(int32_t aVersion) {
53   MOZ_ASSERT((aVersion & MACOS_VERSION_MASK) == aVersion);
54   return (aVersion & 0xFF00) >> 8;
57 int32_t nsCocoaFeatures::ExtractBugFixVersion(int32_t aVersion) {
58   MOZ_ASSERT((aVersion & MACOS_VERSION_MASK) == aVersion);
59   return aVersion & 0xFF;
62 static int intAtStringIndex(NSArray* array, int index) {
63   return [(NSString*)[array objectAtIndex:index] integerValue];
66 void nsCocoaFeatures::GetSystemVersion(int& major, int& minor, int& bugfix) {
67   major = minor = bugfix = 0;
69   NSString* versionString =
70       [[NSDictionary dictionaryWithContentsOfFile:
71                          @"/System/Library/CoreServices/SystemVersion.plist"]
72           objectForKey:@"ProductVersion"];
73   if (!versionString) {
74     NS_ERROR("Couldn't read /System/Library/CoreServices/SystemVersion.plist "
75              "to determine macOS "
76              "version.");
77     return;
78   }
79   NSArray* versions = [versionString componentsSeparatedByString:@"."];
80   NSUInteger count = [versions count];
81   if (count > 0) {
82     major = intAtStringIndex(versions, 0);
83     if (count > 1) {
84       minor = intAtStringIndex(versions, 1);
85       if (count > 2) {
86         bugfix = intAtStringIndex(versions, 2);
87       }
88     }
89   }
92 int32_t nsCocoaFeatures::GetVersion(int32_t aMajor, int32_t aMinor,
93                                     int32_t aBugFix) {
94   int32_t macOSVersion;
95   if (aMajor < 10) {
96     aMajor = 10;
97     NS_ERROR("Couldn't determine macOS version, assuming 10.9");
98     macOSVersion = MACOS_VERSION_10_9_HEX;
99   } else if (aMajor == 10 && aMinor < 9) {
100     aMinor = 9;
101     NS_ERROR("macOS version too old, assuming 10.9");
102     macOSVersion = MACOS_VERSION_10_9_HEX;
103   } else {
104     MOZ_ASSERT(aMajor >= 10);
105     MOZ_ASSERT(aMajor < 256);
106     MOZ_ASSERT(aMinor >= 0);
107     MOZ_ASSERT(aMinor < 256);
108     MOZ_ASSERT(aBugFix >= 0);
109     MOZ_ASSERT(aBugFix < 256);
110     macOSVersion = AssembleVersion(aMajor, aMinor, aBugFix);
111   }
112   MOZ_ASSERT(aMajor == ExtractMajorVersion(macOSVersion));
113   MOZ_ASSERT(aMinor == ExtractMinorVersion(macOSVersion));
114   MOZ_ASSERT(aBugFix == ExtractBugFixVersion(macOSVersion));
115   return macOSVersion;
118 /*static*/ void nsCocoaFeatures::InitializeVersionNumbers() {
119   NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;
121   // Provide an autorelease pool to avoid leaking Cocoa objects,
122   // as this gets called before the main autorelease pool is in place.
123   nsAutoreleasePool localPool;
125   int major, minor, bugfix;
126   GetSystemVersion(major, minor, bugfix);
127   mOSVersion = GetVersion(major, minor, bugfix);
129   NS_OBJC_END_TRY_IGNORE_BLOCK;
132 /* static */ int32_t nsCocoaFeatures::macOSVersion() {
133   // Don't let this be called while we're first setting the value...
134   MOZ_ASSERT((mOSVersion & MACOS_VERSION_MASK) >= 0);
135   if (!mOSVersion) {
136     mOSVersion = -1;
137     InitializeVersionNumbers();
138   }
139   return mOSVersion;
142 /* static */ int32_t nsCocoaFeatures::macOSVersionMajor() {
143   return ExtractMajorVersion(macOSVersion());
146 /* static */ int32_t nsCocoaFeatures::macOSVersionMinor() {
147   return ExtractMinorVersion(macOSVersion());
150 /* static */ int32_t nsCocoaFeatures::macOSVersionBugFix() {
151   return ExtractBugFixVersion(macOSVersion());
154 /* static */ bool nsCocoaFeatures::OnBigSurOrLater() {
155   // Account for the version being 10.16 or 11.0 on Big Sur.
156   // The version is reported as 10.16 if SYSTEM_VERSION_COMPAT is set to 1,
157   // or if SYSTEM_VERSION_COMPAT is not set and the application is linked
158   // with a pre-Big Sur SDK.
159   // Firefox sets SYSTEM_VERSION_COMPAT to 0 in its Info.plist, so it'll
160   // usually see the correct 11.* version, despite being linked against an
161   // old SDK. However, it still sees the 10.16 compatibility version when
162   // launched from the command line, see bug 1727624. (This only applies to
163   // the Intel build - the arm64 build is linked against a Big Sur SDK and
164   // always sees the correct version.)
165   return ((macOSVersion() >= MACOS_VERSION_10_16_HEX) ||
166           (macOSVersion() >= MACOS_VERSION_11_0_HEX));
169 /* static */ bool nsCocoaFeatures::OnMontereyOrLater() {
170   // This check only works if SYSTEM_VERSION_COMPAT is off, otherwise
171   // Monterey pretends to be 10.16 and is indistinguishable from Big Sur.
172   // In practice, this means that an Intel Firefox build can return false
173   // from this function if it's launched from the command line, see bug 1727624.
174   // This will not be an issue anymore once we link against the Big Sur SDK.
175   return (macOSVersion() >= MACOS_VERSION_12_0_HEX);
178 /* static */ bool nsCocoaFeatures::OnVenturaOrLater() {
179   // See comments above regarding SYSTEM_VERSION_COMPAT.
180   return (macOSVersion() >= MACOS_VERSION_13_0_HEX);
183 /* static */ bool nsCocoaFeatures::IsAtLeastVersion(int32_t aMajor,
184                                                     int32_t aMinor,
185                                                     int32_t aBugFix) {
186   return macOSVersion() >= GetVersion(aMajor, aMinor, aBugFix);
190  * Returns true if the process is running under Rosetta translation. Returns
191  * false if running natively or if an error was encountered. We use the
192  * `sysctl.proc_translated` sysctl which is documented by Apple to be used
193  * for this purpose. Note: using this in a sandboxed process requires allowing
194  * the sysctl in the sandbox policy.
195  */
196 /* static */ bool nsCocoaFeatures::ProcessIsRosettaTranslated() {
197   int ret = 0;
198   size_t size = sizeof(ret);
199   if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1) {
200     if (errno != ENOENT) {
201       fprintf(stderr, "Failed to check for translation environment\n");
202     }
203     return false;
204   }
205   return (ret == 1);