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 
5 #ifndef BASE_MAC_MAC_UTIL_H_
6 #define BASE_MAC_MAC_UTIL_H_
7 
8 #include <AvailabilityMacros.h>
9 #include <Carbon/Carbon.h>
10 #include <stdint.h>
11 #include <string>
12 
13 #include "base/base_export.h"
14 #include "base/logging.h"
15 
16 #if defined(__OBJC__)
17 #import <Foundation/Foundation.h>
18 #else  // __OBJC__
19 class NSImage;
20 #endif  // __OBJC__
21 
22 namespace base {
23 
24 class FilePath;
25 
26 namespace mac {
27 
28 // Full screen modes, in increasing order of priority.  More permissive modes
29 // take predecence.
30 enum FullScreenMode {
31   kFullScreenModeHideAll = 0,
32   kFullScreenModeHideDock = 1,
33   kFullScreenModeAutoHideAll = 2,
34   kNumFullScreenModes = 3,
35 
36   // kFullScreenModeNormal is not a valid FullScreenMode, but it is useful to
37   // other classes, so we include it here.
38   kFullScreenModeNormal = 10,
39 };
40 
41 BASE_EXPORT std::string PathFromFSRef(const FSRef& ref);
42 BASE_EXPORT bool FSRefFromPath(const std::string& path, FSRef* ref);
43 
44 // Returns an sRGB color space.  The return value is a static value; do not
45 // release it!
46 BASE_EXPORT CGColorSpaceRef GetSRGBColorSpace();
47 
48 // Returns the generic RGB color space. The return value is a static value; do
49 // not release it!
50 BASE_EXPORT CGColorSpaceRef GetGenericRGBColorSpace();
51 
52 // Returns the color space being used by the main display.  The return value
53 // is a static value; do not release it!
54 BASE_EXPORT CGColorSpaceRef GetSystemColorSpace();
55 
56 // Add a full screen request for the given |mode|.  Must be paired with a
57 // ReleaseFullScreen() call for the same |mode|.  This does not by itself create
58 // a fullscreen window; rather, it manages per-application state related to
59 // hiding the dock and menubar.  Must be called on the main thread.
60 BASE_EXPORT void RequestFullScreen(FullScreenMode mode);
61 
62 // Release a request for full screen mode.  Must be matched with a
63 // RequestFullScreen() call for the same |mode|.  As with RequestFullScreen(),
64 // this does not affect windows directly, but rather manages per-application
65 // state.  For example, if there are no other outstanding
66 // |kFullScreenModeAutoHideAll| requests, this will reshow the menu bar.  Must
67 // be called on main thread.
68 BASE_EXPORT void ReleaseFullScreen(FullScreenMode mode);
69 
70 // Convenience method to switch the current fullscreen mode.  This has the same
71 // net effect as a ReleaseFullScreen(from_mode) call followed immediately by a
72 // RequestFullScreen(to_mode).  Must be called on the main thread.
73 BASE_EXPORT void SwitchFullScreenModes(FullScreenMode from_mode,
74                                        FullScreenMode to_mode);
75 
76 // Set the visibility of the cursor.
77 BASE_EXPORT void SetCursorVisibility(bool visible);
78 
79 // Activates the process with the given PID.
80 BASE_EXPORT void ActivateProcess(pid_t pid);
81 
82 // Returns true if this process is in the foreground, meaning that it's the
83 // frontmost process, the one whose menu bar is shown at the top of the main
84 // display.
85 BASE_EXPORT bool AmIForeground();
86 
87 // Excludes the file given by |file_path| from being backed up by Time Machine.
88 BASE_EXPORT bool SetFileBackupExclusion(const FilePath& file_path);
89 
90 // Checks if the current application is set as a Login Item, so it will launch
91 // on Login. If a non-NULL pointer to is_hidden is passed, the Login Item also
92 // is queried for the 'hide on launch' flag.
93 BASE_EXPORT bool CheckLoginItemStatus(bool* is_hidden);
94 
95 // Adds current application to the set of Login Items with specified "hide"
96 // flag. This has the same effect as adding/removing the application in
97 // SystemPreferences->Accounts->LoginItems or marking Application in the Dock
98 // as "Options->Open on Login".
99 // Does nothing if the application is already set up as Login Item with
100 // specified hide flag.
101 BASE_EXPORT void AddToLoginItems(bool hide_on_startup);
102 
103 // Removes the current application from the list Of Login Items.
104 BASE_EXPORT void RemoveFromLoginItems();
105 
106 // Returns true if the current process was automatically launched as a
107 // 'Login Item' or via Lion's Resume. Used to suppress opening windows.
108 BASE_EXPORT bool WasLaunchedAsLoginOrResumeItem();
109 
110 // Returns true if the current process was automatically launched as a
111 // 'Login Item' or via Resume, and the 'Reopen windows when logging back in'
112 // checkbox was selected by the user.  This indicates that the previous
113 // session should be restored.
114 BASE_EXPORT bool WasLaunchedAsLoginItemRestoreState();
115 
116 // Returns true if the current process was automatically launched as a
117 // 'Login Item' with 'hide on startup' flag. Used to suppress opening windows.
118 BASE_EXPORT bool WasLaunchedAsHiddenLoginItem();
119 
120 // Remove the quarantine xattr from the given file. Returns false if there was
121 // an error, or true otherwise.
122 BASE_EXPORT bool RemoveQuarantineAttribute(const FilePath& file_path);
123 
124 // Run-time OS version checks. Use these instead of
125 // base::SysInfo::OperatingSystemVersionNumbers. Prefer the "OrEarlier" and
126 // "OrLater" variants to those that check for a specific version, unless you
127 // know for sure that you need to check for a specific version.
128 
129 // Snow Leopard is Mac OS X 10.6, Darwin 10.
130 BASE_EXPORT bool IsOSSnowLeopard();
131 
132 // Lion is Mac OS X 10.7, Darwin 11.
133 BASE_EXPORT bool IsOSLion();
134 BASE_EXPORT bool IsOSLionOrEarlier();
135 BASE_EXPORT bool IsOSLionOrLater();
136 
137 // Mountain Lion is Mac OS X 10.8, Darwin 12.
138 BASE_EXPORT bool IsOSMountainLion();
139 BASE_EXPORT bool IsOSMountainLionOrEarlier();
140 BASE_EXPORT bool IsOSMountainLionOrLater();
141 
142 // Mavericks is Mac OS X 10.9, Darwin 13.
143 BASE_EXPORT bool IsOSMavericks();
144 BASE_EXPORT bool IsOSMavericksOrEarlier();
145 BASE_EXPORT bool IsOSMavericksOrLater();
146 
147 // Yosemite is Mac OS X 10.10, Darwin 14.
148 BASE_EXPORT bool IsOSYosemite();
149 BASE_EXPORT bool IsOSYosemiteOrEarlier();
150 BASE_EXPORT bool IsOSYosemiteOrLater();
151 
152 // El Capitan is Mac OS X 10.11, Darwin 15.
153 BASE_EXPORT bool IsOSElCapitan();
154 BASE_EXPORT bool IsOSElCapitanOrLater();
155 
156 // This should be infrequently used. It only makes sense to use this to avoid
157 // codepaths that are very likely to break on future (unreleased, untested,
158 // unborn) OS releases, or to log when the OS is newer than any known version.
159 BASE_EXPORT bool IsOSLaterThanElCapitan_DontCallThis();
160 
161 // Inline functions that are redundant due to version ranges being mutually-
162 // exclusive.
IsOSLionOrEarlier()163 inline bool IsOSLionOrEarlier() { return !IsOSMountainLionOrLater(); }
IsOSMountainLionOrEarlier()164 inline bool IsOSMountainLionOrEarlier() { return !IsOSMavericksOrLater(); }
IsOSMavericksOrEarlier()165 inline bool IsOSMavericksOrEarlier() { return !IsOSYosemiteOrLater(); }
IsOSYosemiteOrEarlier()166 inline bool IsOSYosemiteOrEarlier() { return !IsOSElCapitanOrLater(); }
167 
168 // When the deployment target is set, the code produced cannot run on earlier
169 // OS releases. That enables some of the IsOS* family to be implemented as
170 // constant-value inline functions. The MAC_OS_X_VERSION_MIN_REQUIRED macro
171 // contains the value of the deployment target.
172 
173 #if defined(MAC_OS_X_VERSION_10_7) && \
174     MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
175 #define BASE_MAC_MAC_UTIL_H_INLINED_GE_10_7
IsOSSnowLeopard()176 inline bool IsOSSnowLeopard() { return false; }
IsOSLionOrLater()177 inline bool IsOSLionOrLater() { return true; }
178 #endif
179 
180 #if defined(MAC_OS_X_VERSION_10_7) && \
181     MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_7
182 #define BASE_MAC_MAC_UTIL_H_INLINED_GT_10_7
IsOSLion()183 inline bool IsOSLion() { return false; }
184 #endif
185 
186 #if defined(MAC_OS_X_VERSION_10_8) && \
187     MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_8
188 #define BASE_MAC_MAC_UTIL_H_INLINED_GE_10_8
IsOSMountainLionOrLater()189 inline bool IsOSMountainLionOrLater() { return true; }
190 #endif
191 
192 #if defined(MAC_OS_X_VERSION_10_8) && \
193     MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_8
194 #define BASE_MAC_MAC_UTIL_H_INLINED_GT_10_8
IsOSMountainLion()195 inline bool IsOSMountainLion() { return false; }
196 #endif
197 
198 #if defined(MAC_OS_X_VERSION_10_9) && \
199     MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_9
200 #define BASE_MAC_MAC_UTIL_H_INLINED_GE_10_9
IsOSMavericksOrLater()201 inline bool IsOSMavericksOrLater() { return true; }
202 #endif
203 
204 #if defined(MAC_OS_X_VERSION_10_9) && \
205     MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
206 #define BASE_MAC_MAC_UTIL_H_INLINED_GT_10_9
IsOSMavericks()207 inline bool IsOSMavericks() { return false; }
208 #endif
209 
210 #if defined(MAC_OS_X_VERSION_10_10) && \
211     MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_10
212 #define BASE_MAC_MAC_UTIL_H_INLINED_GE_10_10
IsOSYosemiteOrLater()213 inline bool IsOSYosemiteOrLater() { return true; }
214 #endif
215 
216 #if defined(MAC_OS_X_VERSION_10_10) && \
217     MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_10
218 #define BASE_MAC_MAC_UTIL_H_INLINED_GT_10_10
IsOSYosemite()219 inline bool IsOSYosemite() { return false; }
220 #endif
221 
222 #if defined(MAC_OS_X_VERSION_10_11) && \
223     MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11
224 #define BASE_MAC_MAC_UTIL_H_INLINED_GE_10_11
IsOSElCapitanOrLater()225 inline bool IsOSElCapitanOrLater() { return true; }
226 #endif
227 
228 #if defined(MAC_OS_X_VERSION_10_11) && \
229     MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_11
230 #define BASE_MAC_MAC_UTIL_H_INLINED_GT_10_11
IsOSElCapitan()231 inline bool IsOSElCapitan() { return false; }
IsOSLaterThanElCapitan_DontCallThis()232 inline bool IsOSLaterThanElCapitan_DontCallThis() { return true; }
233 #endif
234 
235 // Retrieve the system's model identifier string from the IOKit registry:
236 // for example, "MacPro4,1", "MacBookPro6,1". Returns empty string upon
237 // failure.
238 BASE_EXPORT std::string GetModelIdentifier();
239 
240 // Parse a model identifier string; for example, into ("MacBookPro", 6, 1).
241 // If any error occurs, none of the input pointers are touched.
242 BASE_EXPORT bool ParseModelIdentifier(const std::string& ident,
243                                       std::string* type,
244                                       int32_t* major,
245                                       int32_t* minor);
246 
247 }  // namespace mac
248 }  // namespace base
249 
250 #endif  // BASE_MAC_MAC_UTIL_H_
251