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.
5 #include "base/mac/mac_util.h"
7 #import <Cocoa/Cocoa.h>
8 #import <IOKit/IOKitLib.h>
12 #include <sys/utsname.h>
13 #include <sys/xattr.h>
15 #include "base/files/file_path.h"
16 #include "base/logging.h"
17 #include "base/mac/bundle_locations.h"
18 #include "base/mac/foundation_util.h"
19 #include "base/mac/mac_logging.h"
20 #include "base/mac/scoped_cftyperef.h"
21 #include "base/mac/scoped_ioobject.h"
22 #include "base/mac/scoped_nsobject.h"
23 #include "base/mac/sdk_forward_declarations.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_piece.h"
26 #include "base/strings/sys_string_conversions.h"
33 // The current count of outstanding requests for full screen mode from browser
34 // windows, plugins, etc.
35 int g_full_screen_requests[kNumFullScreenModes] = { 0 };
37 // Sets the appropriate application presentation option based on the current
38 // full screen requests. Since only one presentation option can be active at a
39 // given time, full screen requests are ordered by priority. If there are no
40 // outstanding full screen requests, reverts to normal mode. If the correct
41 // presentation option is already set, does nothing.
43 NSApplicationPresentationOptions current_options =
44 [NSApp presentationOptions];
46 // Determine which mode should be active, based on which requests are
47 // currently outstanding. More permissive requests take precedence. For
48 // example, plugins request |kFullScreenModeAutoHideAll|, while browser
49 // windows request |kFullScreenModeHideDock| when the fullscreen overlay is
50 // down. Precedence goes to plugins in this case, so AutoHideAll wins over
52 NSApplicationPresentationOptions desired_options =
53 NSApplicationPresentationDefault;
54 if (g_full_screen_requests[kFullScreenModeAutoHideAll] > 0) {
55 desired_options = NSApplicationPresentationHideDock |
56 NSApplicationPresentationAutoHideMenuBar;
57 } else if (g_full_screen_requests[kFullScreenModeHideDock] > 0) {
58 desired_options = NSApplicationPresentationHideDock;
59 } else if (g_full_screen_requests[kFullScreenModeHideAll] > 0) {
60 desired_options = NSApplicationPresentationHideDock |
61 NSApplicationPresentationHideMenuBar;
64 // Mac OS X bug: if the window is fullscreened (Lion-style) and
65 // NSApplicationPresentationDefault is requested, the result is that the menu
66 // bar doesn't auto-hide. rdar://13576498 http://www.openradar.me/13576498
68 // As a workaround, in that case, explicitly set the presentation options to
69 // the ones that are set by the system as it fullscreens a window.
70 if (desired_options == NSApplicationPresentationDefault &&
71 current_options & NSApplicationPresentationFullScreen) {
72 desired_options |= NSApplicationPresentationFullScreen |
73 NSApplicationPresentationAutoHideMenuBar |
74 NSApplicationPresentationAutoHideDock;
77 if (current_options != desired_options)
78 [NSApp setPresentationOptions:desired_options];
81 // Looks into Shared File Lists corresponding to Login Items for the item
82 // representing the current application. If such an item is found, returns a
83 // retained reference to it. Caller is responsible for releasing the reference.
84 LSSharedFileListItemRef GetLoginItemForApp() {
85 ScopedCFTypeRef<LSSharedFileListRef> login_items(LSSharedFileListCreate(
86 NULL, kLSSharedFileListSessionLoginItems, NULL));
88 if (!login_items.get()) {
89 DLOG(ERROR) << "Couldn't get a Login Items list.";
93 base::scoped_nsobject<NSArray> login_items_array(
94 CFToNSCast(LSSharedFileListCopySnapshot(login_items, NULL)));
96 NSURL* url = [NSURL fileURLWithPath:[base::mac::MainBundle() bundlePath]];
98 for(NSUInteger i = 0; i < [login_items_array count]; ++i) {
99 LSSharedFileListItemRef item = reinterpret_cast<LSSharedFileListItemRef>(
100 [login_items_array objectAtIndex:i]);
101 CFURLRef item_url_ref = NULL;
103 if (LSSharedFileListItemResolve(item, 0, &item_url_ref, NULL) == noErr) {
104 ScopedCFTypeRef<CFURLRef> item_url(item_url_ref);
105 if (CFEqual(item_url, url)) {
115 bool IsHiddenLoginItem(LSSharedFileListItemRef item) {
116 ScopedCFTypeRef<CFBooleanRef> hidden(reinterpret_cast<CFBooleanRef>(
117 LSSharedFileListItemCopyProperty(item,
118 reinterpret_cast<CFStringRef>(kLSSharedFileListLoginItemHidden))));
120 return hidden && hidden == kCFBooleanTrue;
125 std::string PathFromFSRef(const FSRef& ref) {
126 ScopedCFTypeRef<CFURLRef> url(
127 CFURLCreateFromFSRef(kCFAllocatorDefault, &ref));
128 NSString *path_string = [(NSURL *)url.get() path];
129 return [path_string fileSystemRepresentation];
132 bool FSRefFromPath(const std::string& path, FSRef* ref) {
133 OSStatus status = FSPathMakeRef((const UInt8*)path.c_str(),
135 return status == noErr;
138 CGColorSpaceRef GetGenericRGBColorSpace() {
139 // Leaked. That's OK, it's scoped to the lifetime of the application.
140 static CGColorSpaceRef g_color_space_generic_rgb(
141 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
142 DLOG_IF(ERROR, !g_color_space_generic_rgb) <<
143 "Couldn't get the generic RGB color space";
144 return g_color_space_generic_rgb;
147 CGColorSpaceRef GetSRGBColorSpace() {
148 // Leaked. That's OK, it's scoped to the lifetime of the application.
149 static CGColorSpaceRef g_color_space_sRGB =
150 CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
151 DLOG_IF(ERROR, !g_color_space_sRGB) << "Couldn't get the sRGB color space";
152 return g_color_space_sRGB;
155 CGColorSpaceRef GetSystemColorSpace() {
156 // Leaked. That's OK, it's scoped to the lifetime of the application.
157 // Try to get the main display's color space.
158 static CGColorSpaceRef g_system_color_space =
159 CGDisplayCopyColorSpace(CGMainDisplayID());
161 if (!g_system_color_space) {
162 // Use a generic RGB color space. This is better than nothing.
163 g_system_color_space = CGColorSpaceCreateDeviceRGB();
165 if (g_system_color_space) {
167 "Couldn't get the main display's color space, using generic";
169 DLOG(ERROR) << "Couldn't get any color space";
173 return g_system_color_space;
176 // Add a request for full screen mode. Must be called on the main thread.
177 void RequestFullScreen(FullScreenMode mode) {
178 DCHECK_LT(mode, kNumFullScreenModes);
179 if (mode >= kNumFullScreenModes)
182 DCHECK_GE(g_full_screen_requests[mode], 0);
186 g_full_screen_requests[mode] = std::max(g_full_screen_requests[mode] + 1, 1);
190 // Release a request for full screen mode. Must be called on the main thread.
191 void ReleaseFullScreen(FullScreenMode mode) {
192 DCHECK_LT(mode, kNumFullScreenModes);
193 if (mode >= kNumFullScreenModes)
196 DCHECK_GE(g_full_screen_requests[mode], 0);
200 g_full_screen_requests[mode] = std::max(g_full_screen_requests[mode] - 1, 0);
204 // Switches full screen modes. Releases a request for |from_mode| and adds a
205 // new request for |to_mode|. Must be called on the main thread.
206 void SwitchFullScreenModes(FullScreenMode from_mode, FullScreenMode to_mode) {
207 DCHECK_LT(from_mode, kNumFullScreenModes);
208 DCHECK_LT(to_mode, kNumFullScreenModes);
209 if (from_mode >= kNumFullScreenModes || to_mode >= kNumFullScreenModes)
212 DCHECK_GT(g_full_screen_requests[from_mode], 0);
213 DCHECK_GE(g_full_screen_requests[to_mode], 0);
214 g_full_screen_requests[from_mode] =
215 std::max(g_full_screen_requests[from_mode] - 1, 0);
216 g_full_screen_requests[to_mode] =
217 std::max(g_full_screen_requests[to_mode] + 1, 1);
221 void SetCursorVisibility(bool visible) {
228 void ActivateProcess(pid_t pid) {
229 ProcessSerialNumber process;
230 OSStatus status = GetProcessForPID(pid, &process);
231 if (status == noErr) {
232 SetFrontProcess(&process);
234 OSSTATUS_DLOG(WARNING, status) << "Unable to get process for pid " << pid;
238 bool AmIForeground() {
239 ProcessSerialNumber foreground_psn = { 0 };
240 OSErr err = GetFrontProcess(&foreground_psn);
242 OSSTATUS_DLOG(WARNING, err) << "GetFrontProcess";
246 ProcessSerialNumber my_psn = { 0, kCurrentProcess };
248 Boolean result = FALSE;
249 err = SameProcess(&foreground_psn, &my_psn, &result);
251 OSSTATUS_DLOG(WARNING, err) << "SameProcess";
258 bool SetFileBackupExclusion(const FilePath& file_path) {
259 NSString* file_path_ns =
260 [NSString stringWithUTF8String:file_path.value().c_str()];
261 NSURL* file_url = [NSURL fileURLWithPath:file_path_ns];
263 // When excludeByPath is true the application must be running with root
264 // privileges (admin for 10.6 and earlier) but the URL does not have to
265 // already exist. When excludeByPath is false the URL must already exist but
266 // can be used in non-root (or admin as above) mode. We use false so that
267 // non-root (or admin) users don't get their TimeMachine drive filled up with
268 // unnecessary backups.
270 CSBackupSetItemExcluded(base::mac::NSToCFCast(file_url), TRUE, FALSE);
271 if (os_err != noErr) {
272 OSSTATUS_DLOG(WARNING, os_err)
273 << "Failed to set backup exclusion for file '"
274 << file_path.value().c_str() << "'";
276 return os_err == noErr;
279 bool CheckLoginItemStatus(bool* is_hidden) {
280 ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp());
285 *is_hidden = IsHiddenLoginItem(item);
290 void AddToLoginItems(bool hide_on_startup) {
291 ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp());
292 if (item.get() && (IsHiddenLoginItem(item) == hide_on_startup)) {
293 return; // Already is a login item with required hide flag.
296 ScopedCFTypeRef<LSSharedFileListRef> login_items(LSSharedFileListCreate(
297 NULL, kLSSharedFileListSessionLoginItems, NULL));
299 if (!login_items.get()) {
300 DLOG(ERROR) << "Couldn't get a Login Items list.";
304 // Remove the old item, it has wrong hide flag, we'll create a new one.
306 LSSharedFileListItemRemove(login_items, item);
309 NSURL* url = [NSURL fileURLWithPath:[base::mac::MainBundle() bundlePath]];
311 BOOL hide = hide_on_startup ? YES : NO;
312 NSDictionary* properties =
314 dictionaryWithObject:[NSNumber numberWithBool:hide]
315 forKey:(NSString*)kLSSharedFileListLoginItemHidden];
317 ScopedCFTypeRef<LSSharedFileListItemRef> new_item;
318 new_item.reset(LSSharedFileListInsertItemURL(
319 login_items, kLSSharedFileListItemLast, NULL, NULL,
320 reinterpret_cast<CFURLRef>(url),
321 reinterpret_cast<CFDictionaryRef>(properties), NULL));
323 if (!new_item.get()) {
324 DLOG(ERROR) << "Couldn't insert current app into Login Items list.";
328 void RemoveFromLoginItems() {
329 ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp());
333 ScopedCFTypeRef<LSSharedFileListRef> login_items(LSSharedFileListCreate(
334 NULL, kLSSharedFileListSessionLoginItems, NULL));
336 if (!login_items.get()) {
337 DLOG(ERROR) << "Couldn't get a Login Items list.";
341 LSSharedFileListItemRemove(login_items, item);
344 bool WasLaunchedAsLoginOrResumeItem() {
345 ProcessSerialNumber psn = { 0, kCurrentProcess };
346 ProcessInfoRec info = {};
347 info.processInfoLength = sizeof(info);
349 if (GetProcessInformation(&psn, &info) == noErr) {
350 ProcessInfoRec parent_info = {};
351 parent_info.processInfoLength = sizeof(parent_info);
352 if (GetProcessInformation(&info.processLauncher, &parent_info) == noErr)
353 return parent_info.processSignature == 'lgnw';
358 bool WasLaunchedAsLoginItemRestoreState() {
359 // "Reopen windows..." option was added for Lion. Prior OS versions should
360 // not have this behavior.
361 if (IsOSSnowLeopard() || !WasLaunchedAsLoginOrResumeItem())
364 CFStringRef app = CFSTR("com.apple.loginwindow");
365 CFStringRef save_state = CFSTR("TALLogoutSavesState");
366 ScopedCFTypeRef<CFPropertyListRef> plist(
367 CFPreferencesCopyAppValue(save_state, app));
368 // According to documentation, com.apple.loginwindow.plist does not exist on a
369 // fresh installation until the user changes a login window setting. The
370 // "reopen windows" option is checked by default, so the plist would exist had
371 // the user unchecked it.
372 // https://developer.apple.com/library/mac/documentation/macosx/conceptual/bpsystemstartup/chapters/CustomLogin.html
376 if (CFBooleanRef restore_state = base::mac::CFCast<CFBooleanRef>(plist))
377 return CFBooleanGetValue(restore_state);
382 bool WasLaunchedAsHiddenLoginItem() {
383 if (!WasLaunchedAsLoginOrResumeItem())
386 ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp());
388 // Lion can launch items for the resume feature. So log an error only for
389 // Snow Leopard or earlier.
390 if (IsOSSnowLeopard())
392 "Process launched at Login but can't access Login Item List.";
396 return IsHiddenLoginItem(item);
399 bool RemoveQuarantineAttribute(const FilePath& file_path) {
400 const char kQuarantineAttrName[] = "com.apple.quarantine";
401 int status = removexattr(file_path.value().c_str(), kQuarantineAttrName, 0);
402 return status == 0 || errno == ENOATTR;
407 // Returns the running system's Darwin major version. Don't call this, it's
408 // an implementation detail and its result is meant to be cached by
409 // MacOSXMinorVersion.
410 int DarwinMajorVersionInternal() {
411 // base::OperatingSystemVersionNumbers calls Gestalt, which is a
412 // higher-level operation than is needed. It might perform unnecessary
413 // operations. On 10.6, it was observed to be able to spawn threads (see
414 // http://crbug.com/53200). It might also read files or perform other
415 // blocking operations. Actually, nobody really knows for sure just what
416 // Gestalt might do, or what it might be taught to do in the future.
418 // uname, on the other hand, is implemented as a simple series of sysctl
419 // system calls to obtain the relevant data from the kernel. The data is
420 // compiled right into the kernel, so no threads or blocking or other
421 // funny business is necessary.
423 struct utsname uname_info;
424 if (uname(&uname_info) != 0) {
425 DPLOG(ERROR) << "uname";
429 if (strcmp(uname_info.sysname, "Darwin") != 0) {
430 DLOG(ERROR) << "unexpected uname sysname " << uname_info.sysname;
434 int darwin_major_version = 0;
435 char* dot = strchr(uname_info.release, '.');
437 if (!base::StringToInt(base::StringPiece(uname_info.release,
438 dot - uname_info.release),
439 &darwin_major_version)) {
445 DLOG(ERROR) << "could not parse uname release " << uname_info.release;
449 return darwin_major_version;
452 // Returns the running system's Mac OS X minor version. This is the |y| value
453 // in 10.y or 10.y.z. Don't call this, it's an implementation detail and the
454 // result is meant to be cached by MacOSXMinorVersion.
455 int MacOSXMinorVersionInternal() {
456 int darwin_major_version = DarwinMajorVersionInternal();
458 // The Darwin major version is always 4 greater than the Mac OS X minor
459 // version for Darwin versions beginning with 6, corresponding to Mac OS X
460 // 10.2. Since this correspondence may change in the future, warn when
461 // encountering a version higher than anything seen before. Older Darwin
462 // versions, or versions that can't be determined, result in
464 CHECK(darwin_major_version >= 6);
465 int mac_os_x_minor_version = darwin_major_version - 4;
466 DLOG_IF(WARNING, darwin_major_version > 14) << "Assuming Darwin "
467 << base::IntToString(darwin_major_version) << " is Mac OS X 10."
468 << base::IntToString(mac_os_x_minor_version);
470 return mac_os_x_minor_version;
473 // Returns the running system's Mac OS X minor version. This is the |y| value
474 // in 10.y or 10.y.z.
475 int MacOSXMinorVersion() {
476 static int mac_os_x_minor_version = MacOSXMinorVersionInternal();
477 return mac_os_x_minor_version;
481 SNOW_LEOPARD_MINOR_VERSION = 6,
482 LION_MINOR_VERSION = 7,
483 MOUNTAIN_LION_MINOR_VERSION = 8,
484 MAVERICKS_MINOR_VERSION = 9,
485 YOSEMITE_MINOR_VERSION = 10,
490 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_7)
491 bool IsOSSnowLeopard() {
492 return MacOSXMinorVersion() == SNOW_LEOPARD_MINOR_VERSION;
496 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_7)
498 return MacOSXMinorVersion() == LION_MINOR_VERSION;
502 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_7)
503 bool IsOSLionOrLater() {
504 return MacOSXMinorVersion() >= LION_MINOR_VERSION;
508 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_8)
509 bool IsOSMountainLion() {
510 return MacOSXMinorVersion() == MOUNTAIN_LION_MINOR_VERSION;
514 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_8)
515 bool IsOSMountainLionOrLater() {
516 return MacOSXMinorVersion() >= MOUNTAIN_LION_MINOR_VERSION;
520 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_9)
521 bool IsOSMavericks() {
522 return MacOSXMinorVersion() == MAVERICKS_MINOR_VERSION;
526 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_9)
527 bool IsOSMavericksOrLater() {
528 return MacOSXMinorVersion() >= MAVERICKS_MINOR_VERSION;
532 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_10)
533 bool IsOSYosemite() {
534 return MacOSXMinorVersion() == YOSEMITE_MINOR_VERSION;
538 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_10)
539 bool IsOSYosemiteOrLater() {
540 return MacOSXMinorVersion() >= YOSEMITE_MINOR_VERSION;
544 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_10)
545 bool IsOSLaterThanYosemite_DontCallThis() {
546 return MacOSXMinorVersion() > YOSEMITE_MINOR_VERSION;
550 std::string GetModelIdentifier() {
551 std::string return_string;
552 ScopedIOObject<io_service_t> platform_expert(
553 IOServiceGetMatchingService(kIOMasterPortDefault,
554 IOServiceMatching("IOPlatformExpertDevice")));
555 if (platform_expert) {
556 ScopedCFTypeRef<CFDataRef> model_data(
557 static_cast<CFDataRef>(IORegistryEntryCreateCFProperty(
564 reinterpret_cast<const char*>(CFDataGetBytePtr(model_data));
567 return return_string;
570 bool ParseModelIdentifier(const std::string& ident,
574 size_t number_loc = ident.find_first_of("0123456789");
575 if (number_loc == std::string::npos)
577 size_t comma_loc = ident.find(',', number_loc);
578 if (comma_loc == std::string::npos)
580 int32 major_tmp, minor_tmp;
581 std::string::const_iterator begin = ident.begin();
583 StringPiece(begin + number_loc, begin + comma_loc), &major_tmp) ||
585 StringPiece(begin + comma_loc + 1, ident.end()), &minor_tmp))
587 *type = ident.substr(0, number_loc);