• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_MAC_MAC_UTIL_H_
6 #define BASE_MAC_MAC_UTIL_H_
7 
8 #include <AvailabilityMacros.h>
9 #import <CoreGraphics/CoreGraphics.h>
10 #include <stdint.h>
11 
12 #include <string>
13 #include <string_view>
14 
15 #include "base/base_export.h"
16 
17 namespace base {
18 class FilePath;
19 }
20 
21 namespace base::mac {
22 
23 // Returns an sRGB color space.  The return value is a static value; do not
24 // release it!
25 BASE_EXPORT CGColorSpaceRef GetSRGBColorSpace();
26 
27 // Returns the generic RGB color space. The return value is a static value; do
28 // not release it!
29 BASE_EXPORT CGColorSpaceRef GetGenericRGBColorSpace();
30 
31 // Returns the color space being used by the main display.  The return value
32 // is a static value; do not release it!
33 BASE_EXPORT CGColorSpaceRef GetSystemColorSpace();
34 
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 
44 // Removes the specified application from the list of Login Items.
45 BASE_EXPORT void RemoveFromLoginItems(const FilePath& app_bundle_file_path);
46 
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 
51 // Returns true if the current process was automatically launched as a
52 // 'Login Item' or via Resume, and the 'Reopen windows when logging back in'
53 // checkbox was selected by the user.  This indicates that the previous
54 // session should be restored.
55 BASE_EXPORT bool WasLaunchedAsLoginItemRestoreState();
56 
57 // Returns true if the current process was automatically launched as a
58 // 'Login Item' with 'hide on startup' flag. Used to suppress opening windows.
59 BASE_EXPORT bool WasLaunchedAsHiddenLoginItem();
60 
61 // Remove the quarantine xattr from the given file. Returns false if there was
62 // an error, or true otherwise.
63 BASE_EXPORT bool RemoveQuarantineAttribute(const FilePath& file_path);
64 
65 // The following two functions return the version of the macOS currently
66 // running. MacOSVersion() returns the full trio of version numbers, packed into
67 // one int (e.g. macOS 12.6.5 returns 12'06'05), and MacOSMajorVersion() returns
68 // only the major version number (e.g. macOS 12.6.5 returns 12). Use for runtime
69 // OS version checking. Prefer to use @available in Objective-C files. Note that
70 // this does not include any Rapid Security Response (RSR) suffixes (the "(a)"
71 // at the end of version numbers.)
72 BASE_EXPORT __attribute__((const)) int MacOSVersion();
MacOSMajorVersion()73 inline __attribute__((const)) int MacOSMajorVersion() {
74   return MacOSVersion() / 1'00'00;
75 }
76 
77 enum class CPUType {
78   kIntel,
79   kTranslatedIntel,  // Rosetta
80   kArm,
81 };
82 
83 // Returns the type of CPU this is being executed on.
84 BASE_EXPORT CPUType GetCPUType();
85 
86 // Returns an OS name + version string. e.g.:
87 //
88 //   "macOS Version 10.14.3 (Build 18D109)"
89 //
90 // Parts of this string change based on OS locale, so it's only useful for
91 // displaying to the user.
92 BASE_EXPORT std::string GetOSDisplayName();
93 
94 // Returns the serial number of the macOS device.
95 BASE_EXPORT std::string GetPlatformSerialNumber();
96 
97 // System Settings (née System Preferences) pane or subpanes to open via
98 // `OpenSystemSettingsPane()`, below. The naming is based on the naming in the
99 // System Settings app in the latest macOS release, macOS 13 Ventura.
100 enum class SystemSettingsPane {
101   // Accessibility > Captions
102   kAccessibility_Captions,
103 
104   // Date & Time
105   kDateTime,
106 
107   // Network > Proxies
108   kNetwork_Proxies,
109 
110   // Printers & Scanners
111   kPrintersScanners,
112 
113   // Privacy & Security > Accessibility
114   kPrivacySecurity_Accessibility,
115 
116   // Privacy & Security > Bluetooth
117   // Available on macOS 11 and later.
118   kPrivacySecurity_Bluetooth,
119 
120   // Privacy & Security > Camera
121   kPrivacySecurity_Camera,
122 
123   // Privacy & Security > Extensions > Sharing
124   kPrivacySecurity_Extensions_Sharing,
125 
126   // Privacy & Security > Location Services
127   kPrivacySecurity_LocationServices,
128 
129   // Privacy & Security > Microphone
130   kPrivacySecurity_Microphone,
131 
132   // Privacy & Security > Screen Recording
133   kPrivacySecurity_ScreenRecording,
134 
135   // Trackpad
136   kTrackpad,
137 };
138 
139 // Opens the specified System Settings pane. If the specified subpane does not
140 // exist on the release of macOS that is running, the parent pane will open
141 // instead.
142 BASE_EXPORT void OpenSystemSettingsPane(SystemSettingsPane pane);
143 
144 // ------- For testing --------
145 
146 // An implementation detail of `MacOSVersion()` above, exposed for testing.
147 BASE_EXPORT int ParseOSProductVersionForTesting(
148     const std::string_view& version);
149 
150 }  // namespace base::mac
151 
152 #endif  // BASE_MAC_MAC_UTIL_H_
153