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