mac_util.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_MAC_MAC_UTIL_H_
  5. #define BASE_MAC_MAC_UTIL_H_
  6. #include <AvailabilityMacros.h>
  7. #import <CoreGraphics/CoreGraphics.h>
  8. #include <stdint.h>
  9. #include <string>
  10. #include "base/base_export.h"
  11. namespace base {
  12. class FilePath;
  13. namespace mac {
  14. // Returns an sRGB color space. The return value is a static value; do not
  15. // release it!
  16. BASE_EXPORT CGColorSpaceRef GetSRGBColorSpace();
  17. // Returns the generic RGB color space. The return value is a static value; do
  18. // not release it!
  19. BASE_EXPORT CGColorSpaceRef GetGenericRGBColorSpace();
  20. // Returns the color space being used by the main display. The return value
  21. // is a static value; do not release it!
  22. BASE_EXPORT CGColorSpaceRef GetSystemColorSpace();
  23. // Returns true if the file at |file_path| is excluded from Time Machine
  24. // backups.
  25. BASE_EXPORT bool GetFileBackupExclusion(const FilePath& file_path);
  26. // Excludes the file given by |file_path| from Time Machine backups.
  27. BASE_EXPORT bool SetFileBackupExclusion(const FilePath& file_path);
  28. // Checks if the current application is set as a Login Item, so it will launch
  29. // on Login. If a non-NULL pointer to is_hidden is passed, the Login Item also
  30. // is queried for the 'hide on launch' flag.
  31. BASE_EXPORT bool CheckLoginItemStatus(bool* is_hidden);
  32. // Adds current application to the set of Login Items with specified "hide"
  33. // flag. This has the same effect as adding/removing the application in
  34. // SystemPreferences->Accounts->LoginItems or marking Application in the Dock
  35. // as "Options->Open on Login".
  36. // Does nothing if the application is already set up as Login Item with
  37. // specified hide flag.
  38. BASE_EXPORT void AddToLoginItems(bool hide_on_startup);
  39. // Removes the current application from the list Of Login Items.
  40. BASE_EXPORT void RemoveFromLoginItems();
  41. // Returns true if the current process was automatically launched as a
  42. // 'Login Item' or via Lion's Resume. Used to suppress opening windows.
  43. BASE_EXPORT bool WasLaunchedAsLoginOrResumeItem();
  44. // Returns true if the current process was automatically launched as a
  45. // 'Login Item' or via Resume, and the 'Reopen windows when logging back in'
  46. // checkbox was selected by the user. This indicates that the previous
  47. // session should be restored.
  48. BASE_EXPORT bool WasLaunchedAsLoginItemRestoreState();
  49. // Returns true if the current process was automatically launched as a
  50. // 'Login Item' with 'hide on startup' flag. Used to suppress opening windows.
  51. BASE_EXPORT bool WasLaunchedAsHiddenLoginItem();
  52. // Remove the quarantine xattr from the given file. Returns false if there was
  53. // an error, or true otherwise.
  54. BASE_EXPORT bool RemoveQuarantineAttribute(const FilePath& file_path);
  55. namespace internal {
  56. // Returns the system's macOS major and minor version numbers combined into an
  57. // integer value. For example, for macOS Sierra this returns 1012, and for macOS
  58. // Big Sur it returns 1100. Note that the accuracy returned by this function is
  59. // as granular as the major version number of Darwin.
  60. BASE_EXPORT int MacOSVersion();
  61. } // namespace internal
  62. // Run-time OS version checks. Prefer @available in Objective-C files. If that
  63. // is not possible, use these functions instead of
  64. // base::SysInfo::OperatingSystemVersionNumbers. Prefer the "AtLeast" and
  65. // "AtMost" variants to those that check for a specific version, unless you know
  66. // for sure that you need to check for a specific version.
  67. #define DEFINE_OLD_IS_OS_FUNCS_CR_MIN_REQUIRED(V, DEPLOYMENT_TARGET_TEST) \
  68. inline bool IsOS10_##V() { \
  69. DEPLOYMENT_TARGET_TEST(>, V, false) \
  70. return internal::MacOSVersion() == 1000 + V; \
  71. } \
  72. inline bool IsAtMostOS10_##V() { \
  73. DEPLOYMENT_TARGET_TEST(>, V, false) \
  74. return internal::MacOSVersion() <= 1000 + V; \
  75. }
  76. #define DEFINE_OLD_IS_OS_FUNCS(V, DEPLOYMENT_TARGET_TEST) \
  77. DEFINE_OLD_IS_OS_FUNCS_CR_MIN_REQUIRED(V, DEPLOYMENT_TARGET_TEST) \
  78. inline bool IsAtLeastOS10_##V() { \
  79. DEPLOYMENT_TARGET_TEST(>=, V, true) \
  80. return internal::MacOSVersion() >= 1000 + V; \
  81. }
  82. // TODO(https://crbug.com/1105187): Update MAC_OS_X_VERSION_MIN_REQUIRED to
  83. // whatever macro it turns into in the future.
  84. #define DEFINE_IS_OS_FUNCS_CR_MIN_REQUIRED(V, DEPLOYMENT_TARGET_TEST) \
  85. inline bool IsOS##V() { \
  86. DEPLOYMENT_TARGET_TEST(>, V, false) \
  87. return internal::MacOSVersion() == V * 100; \
  88. } \
  89. inline bool IsAtMostOS##V() { \
  90. DEPLOYMENT_TARGET_TEST(>, V, false) \
  91. return internal::MacOSVersion() <= V * 100; \
  92. }
  93. #define DEFINE_IS_OS_FUNCS(V, DEPLOYMENT_TARGET_TEST) \
  94. DEFINE_IS_OS_FUNCS_CR_MIN_REQUIRED(V, DEPLOYMENT_TARGET_TEST) \
  95. inline bool IsAtLeastOS##V() { \
  96. DEPLOYMENT_TARGET_TEST(>=, V, true) \
  97. return internal::MacOSVersion() >= V * 100; \
  98. }
  99. #define OLD_TEST_DEPLOYMENT_TARGET(OP, V, RET) \
  100. if (MAC_OS_X_VERSION_MIN_REQUIRED OP MAC_OS_X_VERSION_10_##V) \
  101. return RET;
  102. #define TEST_DEPLOYMENT_TARGET(OP, V, RET) \
  103. if (MAC_OS_X_VERSION_MIN_REQUIRED OP MAC_OS_VERSION_##V##_0) \
  104. return RET;
  105. #define IGNORE_DEPLOYMENT_TARGET(OP, V, RET)
  106. // Notes:
  107. // - When bumping the minimum version of the macOS required by Chromium, remove
  108. // lines from below corresponding to versions of the macOS no longer
  109. // supported. Ensure that the minimum supported version uses the
  110. // DEFINE_OLD_IS_OS_FUNCS_CR_MIN_REQUIRED macro. When macOS 11.0 is the
  111. // minimum required version, remove all the OLD versions of the macros.
  112. // - When bumping the minimum version of the macOS SDK required to build
  113. // Chromium, remove the #ifdef that switches between
  114. // TEST_DEPLOYMENT_TARGET and IGNORE_DEPLOYMENT_TARGET.
  115. // Versions of macOS supported at runtime but whose SDK is not supported for
  116. // building.
  117. DEFINE_OLD_IS_OS_FUNCS_CR_MIN_REQUIRED(10, OLD_TEST_DEPLOYMENT_TARGET)
  118. DEFINE_OLD_IS_OS_FUNCS(11, OLD_TEST_DEPLOYMENT_TARGET)
  119. DEFINE_OLD_IS_OS_FUNCS(12, OLD_TEST_DEPLOYMENT_TARGET)
  120. DEFINE_OLD_IS_OS_FUNCS(13, OLD_TEST_DEPLOYMENT_TARGET)
  121. DEFINE_OLD_IS_OS_FUNCS(14, OLD_TEST_DEPLOYMENT_TARGET)
  122. // Versions of macOS supported at runtime and whose SDK is supported for
  123. // building.
  124. #ifdef MAC_OS_X_VERSION_10_15
  125. DEFINE_OLD_IS_OS_FUNCS(15, OLD_TEST_DEPLOYMENT_TARGET)
  126. #else
  127. DEFINE_OLD_IS_OS_FUNCS(15, IGNORE_DEPLOYMENT_TARGET)
  128. #endif
  129. #ifdef MAC_OS_VERSION_11_0
  130. DEFINE_IS_OS_FUNCS(11, TEST_DEPLOYMENT_TARGET)
  131. #else
  132. DEFINE_IS_OS_FUNCS(11, IGNORE_DEPLOYMENT_TARGET)
  133. #endif
  134. #undef DEFINE_OLD_IS_OS_FUNCS_CR_MIN_REQUIRED
  135. #undef DEFINE_OLD_IS_OS_FUNCS
  136. #undef DEFINE_IS_OS_FUNCS_CR_MIN_REQUIRED
  137. #undef DEFINE_IS_OS_FUNCS
  138. #undef OLD_TEST_DEPLOYMENT_TARGET
  139. #undef TEST_DEPLOYMENT_TARGET
  140. #undef IGNORE_DEPLOYMENT_TARGET
  141. // This should be infrequently used. It only makes sense to use this to avoid
  142. // codepaths that are very likely to break on future (unreleased, untested,
  143. // unborn) OS releases, or to log when the OS is newer than any known version.
  144. inline bool IsOSLaterThan11_DontCallThis() {
  145. return !IsAtMostOS11();
  146. }
  147. enum class CPUType {
  148. kIntel,
  149. kTranslatedIntel, // Rosetta
  150. kArm,
  151. };
  152. // Returns the type of CPU this is being executed on.
  153. BASE_EXPORT CPUType GetCPUType();
  154. // Retrieve the system's model identifier string from the IOKit registry:
  155. // for example, "MacPro4,1", "MacBookPro6,1". Returns empty string upon
  156. // failure.
  157. BASE_EXPORT std::string GetModelIdentifier();
  158. // Parse a model identifier string; for example, into ("MacBookPro", 6, 1).
  159. // If any error occurs, none of the input pointers are touched.
  160. BASE_EXPORT bool ParseModelIdentifier(const std::string& ident,
  161. std::string* type,
  162. int32_t* major,
  163. int32_t* minor);
  164. // Returns an OS name + version string. e.g.:
  165. //
  166. // "macOS Version 10.14.3 (Build 18D109)"
  167. //
  168. // Parts of this string change based on OS locale, so it's only useful for
  169. // displaying to the user.
  170. BASE_EXPORT std::string GetOSDisplayName();
  171. // Returns the serial number of the macOS device.
  172. BASE_EXPORT std::string GetPlatformSerialNumber();
  173. } // namespace mac
  174. } // namespace base
  175. #endif // BASE_MAC_MAC_UTIL_H_