2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2011-08-05 22:36:51 +04:00
|
|
|
|
2020-05-05 00:11:09 +03:00
|
|
|
// This file makes some assumptions about the versions of macOS.
|
|
|
|
// We are assuming that the major, minor and bugfix versions are each less than
|
|
|
|
// 256.
|
2014-11-29 01:28:59 +03:00
|
|
|
// There are MOZ_ASSERTs for that.
|
2014-11-11 06:15:22 +03:00
|
|
|
|
2020-05-05 00:11:09 +03:00
|
|
|
// The formula for the version integer is (major << 16) + (minor << 8) + bugfix.
|
|
|
|
|
|
|
|
#define MACOS_VERSION_MASK 0x00FFFFFF
|
|
|
|
#define MACOS_MAJOR_VERSION_MASK 0x00FFFFFF
|
|
|
|
#define MACOS_MINOR_VERSION_MASK 0x00FFFFFF
|
|
|
|
#define MACOS_BUGFIX_VERSION_MASK 0x00FFFFFF
|
|
|
|
#define MACOS_VERSION_10_0_HEX 0x000A0000
|
|
|
|
#define MACOS_VERSION_10_9_HEX 0x000A0900
|
|
|
|
#define MACOS_VERSION_10_10_HEX 0x000A0A00
|
|
|
|
#define MACOS_VERSION_10_11_HEX 0x000A0B00
|
|
|
|
#define MACOS_VERSION_10_12_HEX 0x000A0C00
|
|
|
|
#define MACOS_VERSION_10_13_HEX 0x000A0D00
|
|
|
|
#define MACOS_VERSION_10_14_HEX 0x000A0E00
|
|
|
|
#define MACOS_VERSION_10_15_HEX 0x000A0F00
|
2020-09-29 21:50:36 +03:00
|
|
|
#define MACOS_VERSION_10_16_HEX 0x000A1000
|
|
|
|
#define MACOS_VERSION_11_0_HEX 0x000B0000
|
2011-08-05 22:36:51 +04:00
|
|
|
|
|
|
|
#include "nsCocoaFeatures.h"
|
2013-12-29 15:32:02 +04:00
|
|
|
#include "nsCocoaUtils.h"
|
2011-08-05 22:36:51 +04:00
|
|
|
#include "nsDebug.h"
|
2012-05-17 11:53:20 +04:00
|
|
|
#include "nsObjCExceptions.h"
|
2011-08-05 22:36:51 +04:00
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2020-05-05 00:11:09 +03:00
|
|
|
/*static*/ int32_t nsCocoaFeatures::mOSVersion = 0;
|
2014-11-11 06:15:22 +03:00
|
|
|
|
2014-11-29 01:28:59 +03:00
|
|
|
// This should not be called with unchecked aMajor, which should be >= 10.
|
2014-11-11 06:15:22 +03:00
|
|
|
inline int32_t AssembleVersion(int32_t aMajor, int32_t aMinor, int32_t aBugFix) {
|
2014-11-29 01:28:59 +03:00
|
|
|
MOZ_ASSERT(aMajor >= 10);
|
2020-05-05 00:11:09 +03:00
|
|
|
return (aMajor << 16) + (aMinor << 8) + aBugFix;
|
2014-11-11 06:15:22 +03:00
|
|
|
}
|
|
|
|
|
2014-11-29 01:28:59 +03:00
|
|
|
int32_t nsCocoaFeatures::ExtractMajorVersion(int32_t aVersion) {
|
2020-05-05 00:11:09 +03:00
|
|
|
MOZ_ASSERT((aVersion & MACOS_VERSION_MASK) == aVersion);
|
|
|
|
return (aVersion & 0xFF0000) >> 16;
|
2014-11-11 06:15:22 +03:00
|
|
|
}
|
|
|
|
|
2014-11-29 01:28:59 +03:00
|
|
|
int32_t nsCocoaFeatures::ExtractMinorVersion(int32_t aVersion) {
|
2020-05-05 00:11:09 +03:00
|
|
|
MOZ_ASSERT((aVersion & MACOS_VERSION_MASK) == aVersion);
|
|
|
|
return (aVersion & 0xFF00) >> 8;
|
2014-11-11 06:15:22 +03:00
|
|
|
}
|
|
|
|
|
2014-11-29 01:28:59 +03:00
|
|
|
int32_t nsCocoaFeatures::ExtractBugFixVersion(int32_t aVersion) {
|
2020-05-05 00:11:09 +03:00
|
|
|
MOZ_ASSERT((aVersion & MACOS_VERSION_MASK) == aVersion);
|
|
|
|
return aVersion & 0xFF;
|
2014-11-11 06:15:22 +03:00
|
|
|
}
|
2011-08-05 22:36:51 +04:00
|
|
|
|
2013-03-08 22:02:46 +04:00
|
|
|
static int intAtStringIndex(NSArray* array, int index) {
|
|
|
|
return [(NSString*)[array objectAtIndex:index] integerValue];
|
|
|
|
}
|
|
|
|
|
2014-11-29 01:28:59 +03:00
|
|
|
void nsCocoaFeatures::GetSystemVersion(int& major, int& minor, int& bugfix) {
|
2013-03-08 22:02:46 +04:00
|
|
|
major = minor = bugfix = 0;
|
2014-11-29 01:28:59 +03:00
|
|
|
|
2013-03-08 22:02:46 +04:00
|
|
|
NSString* versionString = [[NSDictionary
|
|
|
|
dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]
|
|
|
|
objectForKey:@"ProductVersion"];
|
2020-05-05 00:11:09 +03:00
|
|
|
if (!versionString) {
|
|
|
|
NS_ERROR("Couldn't read /System/Library/CoreServices/SystemVersion.plist to determine macOS "
|
|
|
|
"version.");
|
|
|
|
return;
|
|
|
|
}
|
2013-03-08 22:02:46 +04:00
|
|
|
NSArray* versions = [versionString componentsSeparatedByString:@"."];
|
|
|
|
NSUInteger count = [versions count];
|
|
|
|
if (count > 0) {
|
|
|
|
major = intAtStringIndex(versions, 0);
|
|
|
|
if (count > 1) {
|
|
|
|
minor = intAtStringIndex(versions, 1);
|
|
|
|
if (count > 2) {
|
|
|
|
bugfix = intAtStringIndex(versions, 2);
|
|
|
|
}
|
|
|
|
}
|
2019-01-21 20:18:16 +03:00
|
|
|
}
|
2013-03-08 22:02:46 +04:00
|
|
|
}
|
|
|
|
|
2014-11-29 01:28:59 +03:00
|
|
|
int32_t nsCocoaFeatures::GetVersion(int32_t aMajor, int32_t aMinor, int32_t aBugFix) {
|
2020-05-05 00:11:09 +03:00
|
|
|
int32_t macOSVersion;
|
2014-11-11 06:15:22 +03:00
|
|
|
if (aMajor < 10) {
|
|
|
|
aMajor = 10;
|
2020-05-05 00:11:09 +03:00
|
|
|
NS_ERROR("Couldn't determine macOS version, assuming 10.9");
|
|
|
|
macOSVersion = MACOS_VERSION_10_9_HEX;
|
|
|
|
} else if (aMajor == 10 && aMinor < 9) {
|
|
|
|
aMinor = 9;
|
|
|
|
NS_ERROR("macOS version too old, assuming 10.9");
|
|
|
|
macOSVersion = MACOS_VERSION_10_9_HEX;
|
2014-11-11 06:15:22 +03:00
|
|
|
} else {
|
2020-05-05 00:11:09 +03:00
|
|
|
MOZ_ASSERT(aMajor >= 10);
|
|
|
|
MOZ_ASSERT(aMajor < 256);
|
|
|
|
MOZ_ASSERT(aMinor >= 0);
|
|
|
|
MOZ_ASSERT(aMinor < 256);
|
2014-11-11 06:15:22 +03:00
|
|
|
MOZ_ASSERT(aBugFix >= 0);
|
2020-05-05 00:11:09 +03:00
|
|
|
MOZ_ASSERT(aBugFix < 256);
|
|
|
|
macOSVersion = AssembleVersion(aMajor, aMinor, aBugFix);
|
2014-11-11 06:15:22 +03:00
|
|
|
}
|
2020-05-05 00:11:09 +03:00
|
|
|
MOZ_ASSERT(aMajor == ExtractMajorVersion(macOSVersion));
|
|
|
|
MOZ_ASSERT(aMinor == ExtractMinorVersion(macOSVersion));
|
|
|
|
MOZ_ASSERT(aBugFix == ExtractBugFixVersion(macOSVersion));
|
|
|
|
return macOSVersion;
|
2014-11-11 06:15:22 +03:00
|
|
|
}
|
|
|
|
|
2013-12-19 20:42:19 +04:00
|
|
|
/*static*/ void nsCocoaFeatures::InitializeVersionNumbers() {
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2013-12-29 15:32:02 +04:00
|
|
|
// Provide an autorelease pool to avoid leaking Cocoa objects,
|
|
|
|
// as this gets called before the main autorelease pool is in place.
|
|
|
|
nsAutoreleasePool localPool;
|
|
|
|
|
2013-12-19 20:42:19 +04:00
|
|
|
int major, minor, bugfix;
|
|
|
|
GetSystemVersion(major, minor, bugfix);
|
2020-05-05 00:11:09 +03:00
|
|
|
mOSVersion = GetVersion(major, minor, bugfix);
|
2013-12-19 20:42:19 +04:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
2020-05-05 00:11:09 +03:00
|
|
|
/* static */ int32_t nsCocoaFeatures::macOSVersion() {
|
2014-11-11 06:15:22 +03:00
|
|
|
// Don't let this be called while we're first setting the value...
|
2020-05-05 00:11:09 +03:00
|
|
|
MOZ_ASSERT((mOSVersion & MACOS_VERSION_MASK) >= 0);
|
|
|
|
if (!mOSVersion) {
|
|
|
|
mOSVersion = -1;
|
2013-12-19 20:42:19 +04:00
|
|
|
InitializeVersionNumbers();
|
2011-08-05 22:36:51 +04:00
|
|
|
}
|
2020-05-05 00:11:09 +03:00
|
|
|
return mOSVersion;
|
2013-12-19 20:42:19 +04:00
|
|
|
}
|
|
|
|
|
2020-05-05 00:11:09 +03:00
|
|
|
/* static */ int32_t nsCocoaFeatures::macOSVersionMajor() {
|
|
|
|
return ExtractMajorVersion(macOSVersion());
|
2013-12-19 20:42:19 +04:00
|
|
|
}
|
|
|
|
|
2020-05-05 00:11:09 +03:00
|
|
|
/* static */ int32_t nsCocoaFeatures::macOSVersionMinor() {
|
|
|
|
return ExtractMinorVersion(macOSVersion());
|
2013-12-19 20:42:19 +04:00
|
|
|
}
|
|
|
|
|
2020-05-05 00:11:09 +03:00
|
|
|
/* static */ int32_t nsCocoaFeatures::macOSVersionBugFix() {
|
|
|
|
return ExtractBugFixVersion(macOSVersion());
|
2011-08-05 22:36:51 +04:00
|
|
|
}
|
|
|
|
|
2014-06-06 02:02:46 +04:00
|
|
|
/* static */ bool nsCocoaFeatures::OnYosemiteOrLater() {
|
2020-05-05 00:11:09 +03:00
|
|
|
return (macOSVersion() >= MACOS_VERSION_10_10_HEX);
|
2014-06-06 02:02:46 +04:00
|
|
|
}
|
2014-09-18 01:48:07 +04:00
|
|
|
|
2015-09-10 02:08:25 +03:00
|
|
|
/* static */ bool nsCocoaFeatures::OnElCapitanOrLater() {
|
2020-05-05 00:11:09 +03:00
|
|
|
return (macOSVersion() >= MACOS_VERSION_10_11_HEX);
|
2015-09-10 02:08:25 +03:00
|
|
|
}
|
|
|
|
|
2018-04-19 00:08:41 +03:00
|
|
|
/* static */ bool nsCocoaFeatures::OnSierraExactly() {
|
2020-05-05 00:11:09 +03:00
|
|
|
return (macOSVersion() >= MACOS_VERSION_10_12_HEX) && (macOSVersion() < MACOS_VERSION_10_13_HEX);
|
2018-04-19 00:08:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Version of OnSierraExactly as global function callable from cairo & skia */
|
|
|
|
bool Gecko_OnSierraExactly() { return nsCocoaFeatures::OnSierraExactly(); }
|
2017-02-10 00:37:24 +03:00
|
|
|
|
2018-06-29 04:18:25 +03:00
|
|
|
/* static */ bool nsCocoaFeatures::OnSierraOrLater() {
|
2020-05-05 00:11:09 +03:00
|
|
|
return (macOSVersion() >= MACOS_VERSION_10_12_HEX);
|
2018-06-29 04:18:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool nsCocoaFeatures::OnHighSierraOrLater() {
|
2020-05-05 00:11:09 +03:00
|
|
|
return (macOSVersion() >= MACOS_VERSION_10_13_HEX);
|
2018-06-29 04:18:25 +03:00
|
|
|
}
|
|
|
|
|
2018-04-20 19:18:03 +03:00
|
|
|
bool Gecko_OnHighSierraOrLater() { return nsCocoaFeatures::OnHighSierraOrLater(); }
|
|
|
|
|
2018-06-29 04:18:25 +03:00
|
|
|
/* static */ bool nsCocoaFeatures::OnMojaveOrLater() {
|
2020-05-05 00:11:09 +03:00
|
|
|
return (macOSVersion() >= MACOS_VERSION_10_14_HEX);
|
2018-06-29 04:18:25 +03:00
|
|
|
}
|
|
|
|
|
2019-09-10 18:20:34 +03:00
|
|
|
/* static */ bool nsCocoaFeatures::OnCatalinaOrLater() {
|
2020-05-05 00:11:09 +03:00
|
|
|
return (macOSVersion() >= MACOS_VERSION_10_15_HEX);
|
2019-09-10 18:20:34 +03:00
|
|
|
}
|
|
|
|
|
2020-09-29 21:50:36 +03:00
|
|
|
/* static */ bool nsCocoaFeatures::OnBigSurOrLater() {
|
|
|
|
// Account for the version being 10.16 (which occurs when the
|
|
|
|
// application is linked with an older SDK) or 11.0 on Big Sur.
|
|
|
|
return ((macOSVersion() >= MACOS_VERSION_10_16_HEX) ||
|
|
|
|
(macOSVersion() >= MACOS_VERSION_11_0_HEX));
|
|
|
|
}
|
|
|
|
|
2014-11-11 06:15:22 +03:00
|
|
|
/* static */ bool nsCocoaFeatures::IsAtLeastVersion(int32_t aMajor, int32_t aMinor,
|
|
|
|
int32_t aBugFix) {
|
2020-05-05 00:11:09 +03:00
|
|
|
return macOSVersion() >= GetVersion(aMajor, aMinor, aBugFix);
|
2014-11-11 06:15:22 +03:00
|
|
|
}
|
2020-09-14 00:34:22 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if the process is running under Rosetta translation. Returns
|
|
|
|
* false if running natively or if an error was encountered. We use the
|
|
|
|
* `sysctl.proc_translated` sysctl which is documented by Apple to be used
|
|
|
|
* for this purpose. Note: using this in a sandboxed process requires allowing
|
|
|
|
* the sysctl in the sandbox policy.
|
|
|
|
*/
|
|
|
|
/* static */ bool nsCocoaFeatures::ProcessIsRosettaTranslated() {
|
|
|
|
int ret = 0;
|
|
|
|
size_t size = sizeof(ret);
|
|
|
|
if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
fprintf(stderr, "Failed to check for translation environment\n");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (ret == 1);
|
|
|
|
}
|