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 #include <vector>
15
16 #include "base/base_export.h"
17
18 namespace base {
19 class FilePath;
20 }
21
22 namespace base::mac {
23
24 // Returns an sRGB color space. The return value is a static value; do not
25 // release it!
26 BASE_EXPORT CGColorSpaceRef GetSRGBColorSpace();
27
28 // Adds the specified application to the set of Login Items with specified
29 // "hide" 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(const FilePath& app_bundle_file_path,
35 bool hide_on_startup);
36
37 // Removes the specified application from the list of Login Items.
38 BASE_EXPORT void RemoveFromLoginItems(const FilePath& app_bundle_file_path);
39
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
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
50 // Returns true if the current process was automatically launched as a
51 // 'Login Item' with 'hide on startup' flag. Used to suppress opening windows.
52 BASE_EXPORT bool WasLaunchedAsHiddenLoginItem();
53
54 // Remove the quarantine xattr from the given file. Returns false if there was
55 // an error, or true otherwise.
56 BASE_EXPORT bool RemoveQuarantineAttribute(const FilePath& file_path);
57
58 // Sets the tags on a given file or folder.
59 BASE_EXPORT void SetFileTags(const FilePath& file_path,
60 const std::vector<std::string>& file_tags);
61
62 // The following two functions return the version of the macOS currently
63 // running. MacOSVersion() returns the full trio of version numbers, packed into
64 // one int (e.g. macOS 12.6.5 returns 12'06'05), and MacOSMajorVersion() returns
65 // only the major version number (e.g. macOS 12.6.5 returns 12). Use for runtime
66 // OS version checking. Prefer to use @available in Objective-C files. Note that
67 // this does not include any Rapid Security Response (RSR) suffixes (the "(a)"
68 // at the end of version numbers.)
69 BASE_EXPORT __attribute__((const)) int MacOSVersion();
MacOSMajorVersion()70 inline __attribute__((const)) int MacOSMajorVersion() {
71 return MacOSVersion() / 1'00'00;
72 }
73
74 enum class CPUType {
75 kIntel,
76 kTranslatedIntel, // Rosetta
77 kArm,
78 };
79
80 // Returns the type of CPU this is being executed on.
81 BASE_EXPORT CPUType GetCPUType();
82
83 // Returns an OS name + version string. e.g.:
84 //
85 // "macOS Version 10.14.3 (Build 18D109)"
86 //
87 // Parts of this string change based on OS locale, so it's only useful for
88 // displaying to the user.
89 BASE_EXPORT std::string GetOSDisplayName();
90
91 // Returns the serial number of the macOS device.
92 BASE_EXPORT std::string GetPlatformSerialNumber();
93
94 // System Settings (née System Preferences) pane or subpanes to open via
95 // `OpenSystemSettingsPane()`, below. The naming is based on the naming in the
96 // System Settings app in the latest macOS release, macOS 13 Ventura.
97 enum class SystemSettingsPane {
98 // Accessibility > Captions
99 kAccessibility_Captions,
100
101 // Date & Time
102 kDateTime,
103
104 // Network > Proxies
105 kNetwork_Proxies,
106
107 // Notifications; optionally pass a bundle identifier as `id_param` to
108 // directly open the notification settings page for the given app.
109 kNotifications,
110
111 // Printers & Scanners
112 kPrintersScanners,
113
114 // Privacy & Security > Accessibility
115 kPrivacySecurity_Accessibility,
116
117 // Privacy & Security > Bluetooth
118 // Available on macOS 11 and later.
119 kPrivacySecurity_Bluetooth,
120
121 // Privacy & Security > Camera
122 kPrivacySecurity_Camera,
123
124 // Privacy & Security > Extensions > Sharing
125 kPrivacySecurity_Extensions_Sharing,
126
127 // Privacy & Security > Location Services
128 kPrivacySecurity_LocationServices,
129
130 // Privacy & Security > Microphone
131 kPrivacySecurity_Microphone,
132
133 // Privacy & Security > Screen Recording
134 kPrivacySecurity_ScreenRecording,
135
136 // Trackpad
137 kTrackpad,
138 };
139
140 // Opens the specified System Settings pane. If the specified subpane does not
141 // exist on the release of macOS that is running, the parent pane will open
142 // instead. For some panes, `id_param` can be used to specify a subpane. See the
143 // various SystemSettingsPane values for details.
144 BASE_EXPORT void OpenSystemSettingsPane(SystemSettingsPane pane,
145 const std::string& id_param = "");
146
147 // ------- For testing --------
148
149 // An implementation detail of `MacOSVersion()` above, exposed for testing.
150 BASE_EXPORT int ParseOSProductVersionForTesting(
151 const std::string_view& version);
152
153 } // namespace base::mac
154
155 #endif // BASE_MAC_MAC_UTIL_H_
156