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#include "base/mac/mac_util.h" 6 7#import <Cocoa/Cocoa.h> 8#import <IOKit/IOKitLib.h> 9 10#include <errno.h> 11#include <string.h> 12#include <sys/utsname.h> 13#include <sys/xattr.h> 14 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/strings/string_number_conversions.h" 24#include "base/strings/string_piece.h" 25#include "base/strings/sys_string_conversions.h" 26 27namespace base { 28namespace mac { 29 30// Replicate specific 10.7 SDK declarations for building with prior SDKs. 31#if !defined(MAC_OS_X_VERSION_10_7) || \ 32 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 33 34enum { 35 NSApplicationPresentationFullScreen = 1 << 10 36}; 37 38#endif // MAC_OS_X_VERSION_10_7 39 40namespace { 41 42// The current count of outstanding requests for full screen mode from browser 43// windows, plugins, etc. 44int g_full_screen_requests[kNumFullScreenModes] = { 0 }; 45 46// Sets the appropriate application presentation option based on the current 47// full screen requests. Since only one presentation option can be active at a 48// given time, full screen requests are ordered by priority. If there are no 49// outstanding full screen requests, reverts to normal mode. If the correct 50// presentation option is already set, does nothing. 51void SetUIMode() { 52 NSApplicationPresentationOptions current_options = 53 [NSApp presentationOptions]; 54 55 // Determine which mode should be active, based on which requests are 56 // currently outstanding. More permissive requests take precedence. For 57 // example, plugins request |kFullScreenModeAutoHideAll|, while browser 58 // windows request |kFullScreenModeHideDock| when the fullscreen overlay is 59 // down. Precedence goes to plugins in this case, so AutoHideAll wins over 60 // HideDock. 61 NSApplicationPresentationOptions desired_options = 62 NSApplicationPresentationDefault; 63 if (g_full_screen_requests[kFullScreenModeAutoHideAll] > 0) { 64 desired_options = NSApplicationPresentationHideDock | 65 NSApplicationPresentationAutoHideMenuBar; 66 } else if (g_full_screen_requests[kFullScreenModeHideDock] > 0) { 67 desired_options = NSApplicationPresentationHideDock; 68 } else if (g_full_screen_requests[kFullScreenModeHideAll] > 0) { 69 desired_options = NSApplicationPresentationHideDock | 70 NSApplicationPresentationHideMenuBar; 71 } 72 73 // Mac OS X bug: if the window is fullscreened (Lion-style) and 74 // NSApplicationPresentationDefault is requested, the result is that the menu 75 // bar doesn't auto-hide. rdar://13576498 http://www.openradar.me/13576498 76 // 77 // As a workaround, in that case, explicitly set the presentation options to 78 // the ones that are set by the system as it fullscreens a window. 79 if (desired_options == NSApplicationPresentationDefault && 80 current_options & NSApplicationPresentationFullScreen) { 81 desired_options |= NSApplicationPresentationFullScreen | 82 NSApplicationPresentationAutoHideMenuBar | 83 NSApplicationPresentationAutoHideDock; 84 } 85 86 if (current_options != desired_options) 87 [NSApp setPresentationOptions:desired_options]; 88} 89 90// Looks into Shared File Lists corresponding to Login Items for the item 91// representing the current application. If such an item is found, returns a 92// retained reference to it. Caller is responsible for releasing the reference. 93LSSharedFileListItemRef GetLoginItemForApp() { 94 ScopedCFTypeRef<LSSharedFileListRef> login_items(LSSharedFileListCreate( 95 NULL, kLSSharedFileListSessionLoginItems, NULL)); 96 97 if (!login_items.get()) { 98 DLOG(ERROR) << "Couldn't get a Login Items list."; 99 return NULL; 100 } 101 102 base::scoped_nsobject<NSArray> login_items_array( 103 CFToNSCast(LSSharedFileListCopySnapshot(login_items, NULL))); 104 105 NSURL* url = [NSURL fileURLWithPath:[base::mac::MainBundle() bundlePath]]; 106 107 for(NSUInteger i = 0; i < [login_items_array count]; ++i) { 108 LSSharedFileListItemRef item = reinterpret_cast<LSSharedFileListItemRef>( 109 [login_items_array objectAtIndex:i]); 110 CFURLRef item_url_ref = NULL; 111 112 if (LSSharedFileListItemResolve(item, 0, &item_url_ref, NULL) == noErr) { 113 ScopedCFTypeRef<CFURLRef> item_url(item_url_ref); 114 if (CFEqual(item_url, url)) { 115 CFRetain(item); 116 return item; 117 } 118 } 119 } 120 121 return NULL; 122} 123 124bool IsHiddenLoginItem(LSSharedFileListItemRef item) { 125 ScopedCFTypeRef<CFBooleanRef> hidden(reinterpret_cast<CFBooleanRef>( 126 LSSharedFileListItemCopyProperty(item, 127 reinterpret_cast<CFStringRef>(kLSSharedFileListLoginItemHidden)))); 128 129 return hidden && hidden == kCFBooleanTrue; 130} 131 132} // namespace 133 134std::string PathFromFSRef(const FSRef& ref) { 135 ScopedCFTypeRef<CFURLRef> url( 136 CFURLCreateFromFSRef(kCFAllocatorDefault, &ref)); 137 NSString *path_string = [(NSURL *)url.get() path]; 138 return [path_string fileSystemRepresentation]; 139} 140 141bool FSRefFromPath(const std::string& path, FSRef* ref) { 142 OSStatus status = FSPathMakeRef((const UInt8*)path.c_str(), 143 ref, nil); 144 return status == noErr; 145} 146 147CGColorSpaceRef GetGenericRGBColorSpace() { 148 // Leaked. That's OK, it's scoped to the lifetime of the application. 149 static CGColorSpaceRef g_color_space_generic_rgb( 150 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); 151 DLOG_IF(ERROR, !g_color_space_generic_rgb) << 152 "Couldn't get the generic RGB color space"; 153 return g_color_space_generic_rgb; 154} 155 156CGColorSpaceRef GetSRGBColorSpace() { 157 // Leaked. That's OK, it's scoped to the lifetime of the application. 158 static CGColorSpaceRef g_color_space_sRGB = 159 CGColorSpaceCreateWithName(kCGColorSpaceSRGB); 160 DLOG_IF(ERROR, !g_color_space_sRGB) << "Couldn't get the sRGB color space"; 161 return g_color_space_sRGB; 162} 163 164CGColorSpaceRef GetSystemColorSpace() { 165 // Leaked. That's OK, it's scoped to the lifetime of the application. 166 // Try to get the main display's color space. 167 static CGColorSpaceRef g_system_color_space = 168 CGDisplayCopyColorSpace(CGMainDisplayID()); 169 170 if (!g_system_color_space) { 171 // Use a generic RGB color space. This is better than nothing. 172 g_system_color_space = CGColorSpaceCreateDeviceRGB(); 173 174 if (g_system_color_space) { 175 DLOG(WARNING) << 176 "Couldn't get the main display's color space, using generic"; 177 } else { 178 DLOG(ERROR) << "Couldn't get any color space"; 179 } 180 } 181 182 return g_system_color_space; 183} 184 185// Add a request for full screen mode. Must be called on the main thread. 186void RequestFullScreen(FullScreenMode mode) { 187 DCHECK_LT(mode, kNumFullScreenModes); 188 if (mode >= kNumFullScreenModes) 189 return; 190 191 DCHECK_GE(g_full_screen_requests[mode], 0); 192 if (mode < 0) 193 return; 194 195 g_full_screen_requests[mode] = std::max(g_full_screen_requests[mode] + 1, 1); 196 SetUIMode(); 197} 198 199// Release a request for full screen mode. Must be called on the main thread. 200void ReleaseFullScreen(FullScreenMode mode) { 201 DCHECK_LT(mode, kNumFullScreenModes); 202 if (mode >= kNumFullScreenModes) 203 return; 204 205 DCHECK_GE(g_full_screen_requests[mode], 0); 206 if (mode < 0) 207 return; 208 209 g_full_screen_requests[mode] = std::max(g_full_screen_requests[mode] - 1, 0); 210 SetUIMode(); 211} 212 213// Switches full screen modes. Releases a request for |from_mode| and adds a 214// new request for |to_mode|. Must be called on the main thread. 215void SwitchFullScreenModes(FullScreenMode from_mode, FullScreenMode to_mode) { 216 DCHECK_LT(from_mode, kNumFullScreenModes); 217 DCHECK_LT(to_mode, kNumFullScreenModes); 218 if (from_mode >= kNumFullScreenModes || to_mode >= kNumFullScreenModes) 219 return; 220 221 DCHECK_GT(g_full_screen_requests[from_mode], 0); 222 DCHECK_GE(g_full_screen_requests[to_mode], 0); 223 g_full_screen_requests[from_mode] = 224 std::max(g_full_screen_requests[from_mode] - 1, 0); 225 g_full_screen_requests[to_mode] = 226 std::max(g_full_screen_requests[to_mode] + 1, 1); 227 SetUIMode(); 228} 229 230void SetCursorVisibility(bool visible) { 231 if (visible) 232 [NSCursor unhide]; 233 else 234 [NSCursor hide]; 235} 236 237bool ShouldWindowsMiniaturizeOnDoubleClick() { 238 // We use an undocumented method in Cocoa; if it doesn't exist, default to 239 // |true|. If it ever goes away, we can do (using an undocumented pref key): 240 // NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; 241 // return ![defaults objectForKey:@"AppleMiniaturizeOnDoubleClick"] || 242 // [defaults boolForKey:@"AppleMiniaturizeOnDoubleClick"]; 243 BOOL methodImplemented = 244 [NSWindow respondsToSelector:@selector(_shouldMiniaturizeOnDoubleClick)]; 245 DCHECK(methodImplemented); 246 return !methodImplemented || 247 [NSWindow performSelector:@selector(_shouldMiniaturizeOnDoubleClick)]; 248} 249 250void ActivateProcess(pid_t pid) { 251 ProcessSerialNumber process; 252 OSStatus status = GetProcessForPID(pid, &process); 253 if (status == noErr) { 254 SetFrontProcess(&process); 255 } else { 256 OSSTATUS_DLOG(WARNING, status) << "Unable to get process for pid " << pid; 257 } 258} 259 260bool AmIForeground() { 261 ProcessSerialNumber foreground_psn = { 0 }; 262 OSErr err = GetFrontProcess(&foreground_psn); 263 if (err != noErr) { 264 OSSTATUS_DLOG(WARNING, err) << "GetFrontProcess"; 265 return false; 266 } 267 268 ProcessSerialNumber my_psn = { 0, kCurrentProcess }; 269 270 Boolean result = FALSE; 271 err = SameProcess(&foreground_psn, &my_psn, &result); 272 if (err != noErr) { 273 OSSTATUS_DLOG(WARNING, err) << "SameProcess"; 274 return false; 275 } 276 277 return result; 278} 279 280bool SetFileBackupExclusion(const FilePath& file_path) { 281 NSString* file_path_ns = 282 [NSString stringWithUTF8String:file_path.value().c_str()]; 283 NSURL* file_url = [NSURL fileURLWithPath:file_path_ns]; 284 285 // When excludeByPath is true the application must be running with root 286 // privileges (admin for 10.6 and earlier) but the URL does not have to 287 // already exist. When excludeByPath is false the URL must already exist but 288 // can be used in non-root (or admin as above) mode. We use false so that 289 // non-root (or admin) users don't get their TimeMachine drive filled up with 290 // unnecessary backups. 291 OSStatus os_err = 292 CSBackupSetItemExcluded(base::mac::NSToCFCast(file_url), TRUE, FALSE); 293 if (os_err != noErr) { 294 OSSTATUS_DLOG(WARNING, os_err) 295 << "Failed to set backup exclusion for file '" 296 << file_path.value().c_str() << "'"; 297 } 298 return os_err == noErr; 299} 300 301bool CheckLoginItemStatus(bool* is_hidden) { 302 ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp()); 303 if (!item.get()) 304 return false; 305 306 if (is_hidden) 307 *is_hidden = IsHiddenLoginItem(item); 308 309 return true; 310} 311 312void AddToLoginItems(bool hide_on_startup) { 313 ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp()); 314 if (item.get() && (IsHiddenLoginItem(item) == hide_on_startup)) { 315 return; // Already is a login item with required hide flag. 316 } 317 318 ScopedCFTypeRef<LSSharedFileListRef> login_items(LSSharedFileListCreate( 319 NULL, kLSSharedFileListSessionLoginItems, NULL)); 320 321 if (!login_items.get()) { 322 DLOG(ERROR) << "Couldn't get a Login Items list."; 323 return; 324 } 325 326 // Remove the old item, it has wrong hide flag, we'll create a new one. 327 if (item.get()) { 328 LSSharedFileListItemRemove(login_items, item); 329 } 330 331 NSURL* url = [NSURL fileURLWithPath:[base::mac::MainBundle() bundlePath]]; 332 333 BOOL hide = hide_on_startup ? YES : NO; 334 NSDictionary* properties = 335 [NSDictionary 336 dictionaryWithObject:[NSNumber numberWithBool:hide] 337 forKey:(NSString*)kLSSharedFileListLoginItemHidden]; 338 339 ScopedCFTypeRef<LSSharedFileListItemRef> new_item; 340 new_item.reset(LSSharedFileListInsertItemURL( 341 login_items, kLSSharedFileListItemLast, NULL, NULL, 342 reinterpret_cast<CFURLRef>(url), 343 reinterpret_cast<CFDictionaryRef>(properties), NULL)); 344 345 if (!new_item.get()) { 346 DLOG(ERROR) << "Couldn't insert current app into Login Items list."; 347 } 348} 349 350void RemoveFromLoginItems() { 351 ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp()); 352 if (!item.get()) 353 return; 354 355 ScopedCFTypeRef<LSSharedFileListRef> login_items(LSSharedFileListCreate( 356 NULL, kLSSharedFileListSessionLoginItems, NULL)); 357 358 if (!login_items.get()) { 359 DLOG(ERROR) << "Couldn't get a Login Items list."; 360 return; 361 } 362 363 LSSharedFileListItemRemove(login_items, item); 364} 365 366bool WasLaunchedAsLoginOrResumeItem() { 367 ProcessSerialNumber psn = { 0, kCurrentProcess }; 368 369 base::scoped_nsobject<NSDictionary> process_info( 370 CFToNSCast(ProcessInformationCopyDictionary( 371 &psn, kProcessDictionaryIncludeAllInformationMask))); 372 373 long long temp = [[process_info objectForKey:@"ParentPSN"] longLongValue]; 374 ProcessSerialNumber parent_psn = 375 { (temp >> 32) & 0x00000000FFFFFFFFLL, temp & 0x00000000FFFFFFFFLL }; 376 377 base::scoped_nsobject<NSDictionary> parent_info( 378 CFToNSCast(ProcessInformationCopyDictionary( 379 &parent_psn, kProcessDictionaryIncludeAllInformationMask))); 380 381 // Check that creator process code is that of loginwindow. 382 BOOL result = 383 [[parent_info objectForKey:@"FileCreator"] isEqualToString:@"lgnw"]; 384 385 return result == YES; 386} 387 388bool WasLaunchedAsHiddenLoginItem() { 389 if (!WasLaunchedAsLoginOrResumeItem()) 390 return false; 391 392 ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp()); 393 if (!item.get()) { 394 // Lion can launch items for the resume feature. So log an error only for 395 // Snow Leopard or earlier. 396 if (IsOSSnowLeopard()) 397 DLOG(ERROR) << 398 "Process launched at Login but can't access Login Item List."; 399 400 return false; 401 } 402 return IsHiddenLoginItem(item); 403} 404 405bool RemoveQuarantineAttribute(const FilePath& file_path) { 406 const char kQuarantineAttrName[] = "com.apple.quarantine"; 407 int status = removexattr(file_path.value().c_str(), kQuarantineAttrName, 0); 408 return status == 0 || errno == ENOATTR; 409} 410 411namespace { 412 413// Returns the running system's Darwin major version. Don't call this, it's 414// an implementation detail and its result is meant to be cached by 415// MacOSXMinorVersion. 416int DarwinMajorVersionInternal() { 417 // base::OperatingSystemVersionNumbers calls Gestalt, which is a 418 // higher-level operation than is needed. It might perform unnecessary 419 // operations. On 10.6, it was observed to be able to spawn threads (see 420 // http://crbug.com/53200). It might also read files or perform other 421 // blocking operations. Actually, nobody really knows for sure just what 422 // Gestalt might do, or what it might be taught to do in the future. 423 // 424 // uname, on the other hand, is implemented as a simple series of sysctl 425 // system calls to obtain the relevant data from the kernel. The data is 426 // compiled right into the kernel, so no threads or blocking or other 427 // funny business is necessary. 428 429 struct utsname uname_info; 430 if (uname(&uname_info) != 0) { 431 DPLOG(ERROR) << "uname"; 432 return 0; 433 } 434 435 if (strcmp(uname_info.sysname, "Darwin") != 0) { 436 DLOG(ERROR) << "unexpected uname sysname " << uname_info.sysname; 437 return 0; 438 } 439 440 int darwin_major_version = 0; 441 char* dot = strchr(uname_info.release, '.'); 442 if (dot) { 443 if (!base::StringToInt(base::StringPiece(uname_info.release, 444 dot - uname_info.release), 445 &darwin_major_version)) { 446 dot = NULL; 447 } 448 } 449 450 if (!dot) { 451 DLOG(ERROR) << "could not parse uname release " << uname_info.release; 452 return 0; 453 } 454 455 return darwin_major_version; 456} 457 458// Returns the running system's Mac OS X minor version. This is the |y| value 459// in 10.y or 10.y.z. Don't call this, it's an implementation detail and the 460// result is meant to be cached by MacOSXMinorVersion. 461int MacOSXMinorVersionInternal() { 462 int darwin_major_version = DarwinMajorVersionInternal(); 463 464 // The Darwin major version is always 4 greater than the Mac OS X minor 465 // version for Darwin versions beginning with 6, corresponding to Mac OS X 466 // 10.2. Since this correspondence may change in the future, warn when 467 // encountering a version higher than anything seen before. Older Darwin 468 // versions, or versions that can't be determined, result in 469 // immediate death. 470 CHECK(darwin_major_version >= 6); 471 int mac_os_x_minor_version = darwin_major_version - 4; 472 DLOG_IF(WARNING, darwin_major_version > 13) << "Assuming Darwin " 473 << base::IntToString(darwin_major_version) << " is Mac OS X 10." 474 << base::IntToString(mac_os_x_minor_version); 475 476 return mac_os_x_minor_version; 477} 478 479// Returns the running system's Mac OS X minor version. This is the |y| value 480// in 10.y or 10.y.z. 481int MacOSXMinorVersion() { 482 static int mac_os_x_minor_version = MacOSXMinorVersionInternal(); 483 return mac_os_x_minor_version; 484} 485 486enum { 487 SNOW_LEOPARD_MINOR_VERSION = 6, 488 LION_MINOR_VERSION = 7, 489 MOUNTAIN_LION_MINOR_VERSION = 8, 490 MAVERICKS_MINOR_VERSION = 9, 491}; 492 493} // namespace 494 495#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_7) 496bool IsOSSnowLeopard() { 497 return MacOSXMinorVersion() == SNOW_LEOPARD_MINOR_VERSION; 498} 499#endif 500 501#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_7) 502bool IsOSLion() { 503 return MacOSXMinorVersion() == LION_MINOR_VERSION; 504} 505#endif 506 507#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_7) 508bool IsOSLionOrLater() { 509 return MacOSXMinorVersion() >= LION_MINOR_VERSION; 510} 511#endif 512 513#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_8) 514bool IsOSMountainLion() { 515 return MacOSXMinorVersion() == MOUNTAIN_LION_MINOR_VERSION; 516} 517#endif 518 519#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_8) 520bool IsOSMountainLionOrLater() { 521 return MacOSXMinorVersion() >= MOUNTAIN_LION_MINOR_VERSION; 522} 523#endif 524 525#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_9) 526bool IsOSMavericks() { 527 return MacOSXMinorVersion() == MAVERICKS_MINOR_VERSION; 528} 529#endif 530 531#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_9) 532bool IsOSMavericksOrLater() { 533 return MacOSXMinorVersion() >= MAVERICKS_MINOR_VERSION; 534} 535#endif 536 537#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_9) 538bool IsOSLaterThanMavericks_DontCallThis() { 539 return MacOSXMinorVersion() > MAVERICKS_MINOR_VERSION; 540} 541#endif 542 543std::string GetModelIdentifier() { 544 std::string return_string; 545 ScopedIOObject<io_service_t> platform_expert( 546 IOServiceGetMatchingService(kIOMasterPortDefault, 547 IOServiceMatching("IOPlatformExpertDevice"))); 548 if (platform_expert) { 549 ScopedCFTypeRef<CFDataRef> model_data( 550 static_cast<CFDataRef>(IORegistryEntryCreateCFProperty( 551 platform_expert, 552 CFSTR("model"), 553 kCFAllocatorDefault, 554 0))); 555 if (model_data) { 556 return_string = 557 reinterpret_cast<const char*>(CFDataGetBytePtr(model_data)); 558 } 559 } 560 return return_string; 561} 562 563bool ParseModelIdentifier(const std::string& ident, 564 std::string* type, 565 int32* major, 566 int32* minor) { 567 size_t number_loc = ident.find_first_of("0123456789"); 568 if (number_loc == std::string::npos) 569 return false; 570 size_t comma_loc = ident.find(',', number_loc); 571 if (comma_loc == std::string::npos) 572 return false; 573 int32 major_tmp, minor_tmp; 574 std::string::const_iterator begin = ident.begin(); 575 if (!StringToInt( 576 StringPiece(begin + number_loc, begin + comma_loc), &major_tmp) || 577 !StringToInt( 578 StringPiece(begin + comma_loc + 1, ident.end()), &minor_tmp)) 579 return false; 580 *type = ident.substr(0, number_loc); 581 *major = major_tmp; 582 *minor = minor_tmp; 583 return true; 584} 585 586} // namespace mac 587} // namespace base 588