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