mac_util.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <stdint.h>
  7. #include <string>
  8. #import <CoreGraphics/CoreGraphics.h>
  9. #include "base/base_export.h"
  10. namespace base {
  11. class FilePath;
  12. namespace mac {
  13. // Returns an sRGB color space. The return value is a static value; do not
  14. // release it!
  15. BASE_EXPORT CGColorSpaceRef GetSRGBColorSpace();
  16. // Returns the generic RGB color space. The return value is a static value; do
  17. // not release it!
  18. BASE_EXPORT CGColorSpaceRef GetGenericRGBColorSpace();
  19. // Returns the color space being used by the main display. The return value
  20. // is a static value; do not release it!
  21. BASE_EXPORT CGColorSpaceRef GetSystemColorSpace();
  22. // Returns true if the file at |file_path| is excluded from Time Machine
  23. // backups.
  24. BASE_EXPORT bool GetFileBackupExclusion(const FilePath& file_path);
  25. // Excludes the file given by |file_path| from Time Machine backups.
  26. BASE_EXPORT bool SetFileBackupExclusion(const FilePath& file_path);
  27. // Checks if the current application is set as a Login Item, so it will launch
  28. // on Login. If a non-NULL pointer to is_hidden is passed, the Login Item also
  29. // is queried for the 'hide on launch' flag.
  30. BASE_EXPORT bool CheckLoginItemStatus(bool* is_hidden);
  31. // Adds current application to the set of Login Items with specified "hide"
  32. // flag. This has the same effect as adding/removing the application in
  33. // SystemPreferences->Accounts->LoginItems or marking Application in the Dock
  34. // as "Options->Open on Login".
  35. // Does nothing if the application is already set up as Login Item with
  36. // specified hide flag.
  37. BASE_EXPORT void AddToLoginItems(bool hide_on_startup);
  38. // Removes the current application from the list Of Login Items.
  39. BASE_EXPORT void RemoveFromLoginItems();
  40. // Returns true if the current process was automatically launched as a
  41. // 'Login Item' or via Lion's Resume. Used to suppress opening windows.
  42. BASE_EXPORT bool WasLaunchedAsLoginOrResumeItem();
  43. // Returns true if the current process was automatically launched as a
  44. // 'Login Item' or via Resume, and the 'Reopen windows when logging back in'
  45. // checkbox was selected by the user. This indicates that the previous
  46. // session should be restored.
  47. BASE_EXPORT bool WasLaunchedAsLoginItemRestoreState();
  48. // Returns true if the current process was automatically launched as a
  49. // 'Login Item' with 'hide on startup' flag. Used to suppress opening windows.
  50. BASE_EXPORT bool WasLaunchedAsHiddenLoginItem();
  51. // Remove the quarantine xattr from the given file. Returns false if there was
  52. // an error, or true otherwise.
  53. BASE_EXPORT bool RemoveQuarantineAttribute(const FilePath& file_path);
  54. namespace internal {
  55. // Returns the system's Mac OS X minor version. This is the |y| value
  56. // in 10.y or 10.y.z.
  57. BASE_EXPORT int MacOSXMinorVersion();
  58. } // namespace internal
  59. // Run-time OS version checks. Prefer @available in Objective-C files. If that
  60. // is not possible, use these functions instead of
  61. // base::SysInfo::OperatingSystemVersionNumbers. Prefer the "AtLeast" and
  62. // "AtMost" variants to those that check for a specific version, unless you know
  63. // for sure that you need to check for a specific version.
  64. #define DEFINE_IS_OS_FUNCS_CR_MIN_REQUIRED(V, TEST_DEPLOYMENT_TARGET) \
  65. inline bool IsOS10_##V() { \
  66. TEST_DEPLOYMENT_TARGET(>, V, false) \
  67. return internal::MacOSXMinorVersion() == V; \
  68. } \
  69. inline bool IsAtMostOS10_##V() { \
  70. TEST_DEPLOYMENT_TARGET(>, V, false) \
  71. return internal::MacOSXMinorVersion() <= V; \
  72. }
  73. #define DEFINE_IS_OS_FUNCS(V, TEST_DEPLOYMENT_TARGET) \
  74. DEFINE_IS_OS_FUNCS_CR_MIN_REQUIRED(V, TEST_DEPLOYMENT_TARGET) \
  75. inline bool IsAtLeastOS10_##V() { \
  76. TEST_DEPLOYMENT_TARGET(>=, V, true) \
  77. return internal::MacOSXMinorVersion() >= V; \
  78. }
  79. #define TEST_DEPLOYMENT_TARGET(OP, V, RET) \
  80. if (MAC_OS_X_VERSION_MIN_REQUIRED OP MAC_OS_X_VERSION_10_##V) \
  81. return RET;
  82. #define IGNORE_DEPLOYMENT_TARGET(OP, V, RET)
  83. // Notes:
  84. // - When bumping the minimum version of the macOS required by Chromium, remove
  85. // lines from below corresponding to versions of the macOS no longer
  86. // supported. Ensure that the minimum supported version uses the
  87. // DEFINE_IS_OS_FUNCS_CR_MIN_REQUIRED macro.
  88. // - When bumping the minimum version of the macOS SDK required to build
  89. // Chromium, remove the #ifdef that switches between TEST_DEPLOYMENT_TARGET
  90. // and IGNORE_DEPLOYMENT_TARGET.
  91. // Versions of macOS supported at runtime but whose SDK is not supported for
  92. // building.
  93. DEFINE_IS_OS_FUNCS_CR_MIN_REQUIRED(10, TEST_DEPLOYMENT_TARGET)
  94. DEFINE_IS_OS_FUNCS(11, TEST_DEPLOYMENT_TARGET)
  95. DEFINE_IS_OS_FUNCS(12, TEST_DEPLOYMENT_TARGET)
  96. DEFINE_IS_OS_FUNCS(13, TEST_DEPLOYMENT_TARGET)
  97. DEFINE_IS_OS_FUNCS(14, TEST_DEPLOYMENT_TARGET)
  98. // Versions of macOS supported at runtime and whose SDK is supported for
  99. // building.
  100. #ifdef MAC_OS_X_VERSION_10_15
  101. DEFINE_IS_OS_FUNCS(15, TEST_DEPLOYMENT_TARGET)
  102. #else
  103. DEFINE_IS_OS_FUNCS(15, IGNORE_DEPLOYMENT_TARGET)
  104. #endif
  105. #undef IGNORE_DEPLOYMENT_TARGET
  106. #undef TEST_DEPLOYMENT_TARGET
  107. #undef DEFINE_IS_OS_FUNCS_CR_MIN_REQUIRED
  108. #undef DEFINE_IS_OS_FUNCS
  109. // This should be infrequently used. It only makes sense to use this to avoid
  110. // codepaths that are very likely to break on future (unreleased, untested,
  111. // unborn) OS releases, or to log when the OS is newer than any known version.
  112. inline bool IsOSLaterThan10_15_DontCallThis() {
  113. return !IsAtMostOS10_15();
  114. }
  115. // Retrieve the system's model identifier string from the IOKit registry:
  116. // for example, "MacPro4,1", "MacBookPro6,1". Returns empty string upon
  117. // failure.
  118. BASE_EXPORT std::string GetModelIdentifier();
  119. // Parse a model identifier string; for example, into ("MacBookPro", 6, 1).
  120. // If any error occurs, none of the input pointers are touched.
  121. BASE_EXPORT bool ParseModelIdentifier(const std::string& ident,
  122. std::string* type,
  123. int32_t* major,
  124. int32_t* minor);
  125. // Returns an OS name + version string. e.g.:
  126. //
  127. // "macOS Version 10.14.3 (Build 18D109)"
  128. //
  129. // Parts of this string change based on OS locale, so it's only useful for
  130. // displaying to the user.
  131. BASE_EXPORT std::string GetOSDisplayName();
  132. // Returns the serial number of the macOS device.
  133. BASE_EXPORT std::string GetPlatformSerialNumber();
  134. } // namespace mac
  135. } // namespace base
  136. #endif // BASE_MAC_MAC_UTIL_H_