| OLD | NEW |
| (Empty) |
| 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/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" | |
| 27 | |
| 28 namespace base { | |
| 29 namespace mac { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 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 }; | |
| 36 | |
| 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. | |
| 42 void SetUIMode() { | |
| 43 NSApplicationPresentationOptions current_options = | |
| 44 [NSApp presentationOptions]; | |
| 45 | |
| 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 | |
| 51 // HideDock. | |
| 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; | |
| 62 } | |
| 63 | |
| 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 | |
| 67 // | |
| 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; | |
| 75 } | |
| 76 | |
| 77 if (current_options != desired_options) | |
| 78 [NSApp setPresentationOptions:desired_options]; | |
| 79 } | |
| 80 | |
| 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)); | |
| 87 | |
| 88 if (!login_items.get()) { | |
| 89 DLOG(ERROR) << "Couldn't get a Login Items list."; | |
| 90 return NULL; | |
| 91 } | |
| 92 | |
| 93 base::scoped_nsobject<NSArray> login_items_array( | |
| 94 CFToNSCast(LSSharedFileListCopySnapshot(login_items, NULL))); | |
| 95 | |
| 96 NSURL* url = [NSURL fileURLWithPath:[base::mac::MainBundle() bundlePath]]; | |
| 97 | |
| 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; | |
| 102 | |
| 103 if (LSSharedFileListItemResolve(item, 0, &item_url_ref, NULL) == noErr) { | |
| 104 ScopedCFTypeRef<CFURLRef> item_url(item_url_ref); | |
| 105 if (CFEqual(item_url, url)) { | |
| 106 CFRetain(item); | |
| 107 return item; | |
| 108 } | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 return NULL; | |
| 113 } | |
| 114 | |
| 115 bool IsHiddenLoginItem(LSSharedFileListItemRef item) { | |
| 116 ScopedCFTypeRef<CFBooleanRef> hidden(reinterpret_cast<CFBooleanRef>( | |
| 117 LSSharedFileListItemCopyProperty(item, | |
| 118 reinterpret_cast<CFStringRef>(kLSSharedFileListLoginItemHidden)))); | |
| 119 | |
| 120 return hidden && hidden == kCFBooleanTrue; | |
| 121 } | |
| 122 | |
| 123 } // namespace | |
| 124 | |
| 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]; | |
| 130 } | |
| 131 | |
| 132 bool FSRefFromPath(const std::string& path, FSRef* ref) { | |
| 133 OSStatus status = FSPathMakeRef((const UInt8*)path.c_str(), | |
| 134 ref, nil); | |
| 135 return status == noErr; | |
| 136 } | |
| 137 | |
| 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; | |
| 145 } | |
| 146 | |
| 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; | |
| 153 } | |
| 154 | |
| 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()); | |
| 160 | |
| 161 if (!g_system_color_space) { | |
| 162 // Use a generic RGB color space. This is better than nothing. | |
| 163 g_system_color_space = CGColorSpaceCreateDeviceRGB(); | |
| 164 | |
| 165 if (g_system_color_space) { | |
| 166 DLOG(WARNING) << | |
| 167 "Couldn't get the main display's color space, using generic"; | |
| 168 } else { | |
| 169 DLOG(ERROR) << "Couldn't get any color space"; | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 return g_system_color_space; | |
| 174 } | |
| 175 | |
| 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) | |
| 180 return; | |
| 181 | |
| 182 DCHECK_GE(g_full_screen_requests[mode], 0); | |
| 183 if (mode < 0) | |
| 184 return; | |
| 185 | |
| 186 g_full_screen_requests[mode] = std::max(g_full_screen_requests[mode] + 1, 1); | |
| 187 SetUIMode(); | |
| 188 } | |
| 189 | |
| 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) | |
| 194 return; | |
| 195 | |
| 196 DCHECK_GE(g_full_screen_requests[mode], 0); | |
| 197 if (mode < 0) | |
| 198 return; | |
| 199 | |
| 200 g_full_screen_requests[mode] = std::max(g_full_screen_requests[mode] - 1, 0); | |
| 201 SetUIMode(); | |
| 202 } | |
| 203 | |
| 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) | |
| 210 return; | |
| 211 | |
| 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); | |
| 218 SetUIMode(); | |
| 219 } | |
| 220 | |
| 221 void SetCursorVisibility(bool visible) { | |
| 222 if (visible) | |
| 223 [NSCursor unhide]; | |
| 224 else | |
| 225 [NSCursor hide]; | |
| 226 } | |
| 227 | |
| 228 void ActivateProcess(pid_t pid) { | |
| 229 ProcessSerialNumber process; | |
| 230 OSStatus status = GetProcessForPID(pid, &process); | |
| 231 if (status == noErr) { | |
| 232 SetFrontProcess(&process); | |
| 233 } else { | |
| 234 OSSTATUS_DLOG(WARNING, status) << "Unable to get process for pid " << pid; | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 bool AmIForeground() { | |
| 239 ProcessSerialNumber foreground_psn = { 0 }; | |
| 240 OSErr err = GetFrontProcess(&foreground_psn); | |
| 241 if (err != noErr) { | |
| 242 OSSTATUS_DLOG(WARNING, err) << "GetFrontProcess"; | |
| 243 return false; | |
| 244 } | |
| 245 | |
| 246 ProcessSerialNumber my_psn = { 0, kCurrentProcess }; | |
| 247 | |
| 248 Boolean result = FALSE; | |
| 249 err = SameProcess(&foreground_psn, &my_psn, &result); | |
| 250 if (err != noErr) { | |
| 251 OSSTATUS_DLOG(WARNING, err) << "SameProcess"; | |
| 252 return false; | |
| 253 } | |
| 254 | |
| 255 return result; | |
| 256 } | |
| 257 | |
| 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]; | |
| 262 | |
| 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. | |
| 269 OSStatus os_err = | |
| 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() << "'"; | |
| 275 } | |
| 276 return os_err == noErr; | |
| 277 } | |
| 278 | |
| 279 bool CheckLoginItemStatus(bool* is_hidden) { | |
| 280 ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp()); | |
| 281 if (!item.get()) | |
| 282 return false; | |
| 283 | |
| 284 if (is_hidden) | |
| 285 *is_hidden = IsHiddenLoginItem(item); | |
| 286 | |
| 287 return true; | |
| 288 } | |
| 289 | |
| 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. | |
| 294 } | |
| 295 | |
| 296 ScopedCFTypeRef<LSSharedFileListRef> login_items(LSSharedFileListCreate( | |
| 297 NULL, kLSSharedFileListSessionLoginItems, NULL)); | |
| 298 | |
| 299 if (!login_items.get()) { | |
| 300 DLOG(ERROR) << "Couldn't get a Login Items list."; | |
| 301 return; | |
| 302 } | |
| 303 | |
| 304 // Remove the old item, it has wrong hide flag, we'll create a new one. | |
| 305 if (item.get()) { | |
| 306 LSSharedFileListItemRemove(login_items, item); | |
| 307 } | |
| 308 | |
| 309 NSURL* url = [NSURL fileURLWithPath:[base::mac::MainBundle() bundlePath]]; | |
| 310 | |
| 311 BOOL hide = hide_on_startup ? YES : NO; | |
| 312 NSDictionary* properties = | |
| 313 [NSDictionary | |
| 314 dictionaryWithObject:[NSNumber numberWithBool:hide] | |
| 315 forKey:(NSString*)kLSSharedFileListLoginItemHidden]; | |
| 316 | |
| 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)); | |
| 322 | |
| 323 if (!new_item.get()) { | |
| 324 DLOG(ERROR) << "Couldn't insert current app into Login Items list."; | |
| 325 } | |
| 326 } | |
| 327 | |
| 328 void RemoveFromLoginItems() { | |
| 329 ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp()); | |
| 330 if (!item.get()) | |
| 331 return; | |
| 332 | |
| 333 ScopedCFTypeRef<LSSharedFileListRef> login_items(LSSharedFileListCreate( | |
| 334 NULL, kLSSharedFileListSessionLoginItems, NULL)); | |
| 335 | |
| 336 if (!login_items.get()) { | |
| 337 DLOG(ERROR) << "Couldn't get a Login Items list."; | |
| 338 return; | |
| 339 } | |
| 340 | |
| 341 LSSharedFileListItemRemove(login_items, item); | |
| 342 } | |
| 343 | |
| 344 bool WasLaunchedAsLoginOrResumeItem() { | |
| 345 ProcessSerialNumber psn = { 0, kCurrentProcess }; | |
| 346 ProcessInfoRec info = {}; | |
| 347 info.processInfoLength = sizeof(info); | |
| 348 | |
| 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'; | |
| 354 } | |
| 355 return false; | |
| 356 } | |
| 357 | |
| 358 bool WasLaunchedAsLoginItemRestoreState() { | |
| 359 // "Reopen windows..." option was added for Lion. Prior OS versions should | |
| 360 // not have this behavior. | |
| 361 if (IsOSSnowLeopard() || !WasLaunchedAsLoginOrResumeItem()) | |
| 362 return false; | |
| 363 | |
| 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/bps
ystemstartup/chapters/CustomLogin.html | |
| 373 if (!plist) | |
| 374 return true; | |
| 375 | |
| 376 if (CFBooleanRef restore_state = base::mac::CFCast<CFBooleanRef>(plist)) | |
| 377 return CFBooleanGetValue(restore_state); | |
| 378 | |
| 379 return false; | |
| 380 } | |
| 381 | |
| 382 bool WasLaunchedAsHiddenLoginItem() { | |
| 383 if (!WasLaunchedAsLoginOrResumeItem()) | |
| 384 return false; | |
| 385 | |
| 386 ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp()); | |
| 387 if (!item.get()) { | |
| 388 // Lion can launch items for the resume feature. So log an error only for | |
| 389 // Snow Leopard or earlier. | |
| 390 if (IsOSSnowLeopard()) | |
| 391 DLOG(ERROR) << | |
| 392 "Process launched at Login but can't access Login Item List."; | |
| 393 | |
| 394 return false; | |
| 395 } | |
| 396 return IsHiddenLoginItem(item); | |
| 397 } | |
| 398 | |
| 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; | |
| 403 } | |
| 404 | |
| 405 namespace { | |
| 406 | |
| 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. | |
| 417 // | |
| 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. | |
| 422 | |
| 423 struct utsname uname_info; | |
| 424 if (uname(&uname_info) != 0) { | |
| 425 DPLOG(ERROR) << "uname"; | |
| 426 return 0; | |
| 427 } | |
| 428 | |
| 429 if (strcmp(uname_info.sysname, "Darwin") != 0) { | |
| 430 DLOG(ERROR) << "unexpected uname sysname " << uname_info.sysname; | |
| 431 return 0; | |
| 432 } | |
| 433 | |
| 434 int darwin_major_version = 0; | |
| 435 char* dot = strchr(uname_info.release, '.'); | |
| 436 if (dot) { | |
| 437 if (!base::StringToInt(base::StringPiece(uname_info.release, | |
| 438 dot - uname_info.release), | |
| 439 &darwin_major_version)) { | |
| 440 dot = NULL; | |
| 441 } | |
| 442 } | |
| 443 | |
| 444 if (!dot) { | |
| 445 DLOG(ERROR) << "could not parse uname release " << uname_info.release; | |
| 446 return 0; | |
| 447 } | |
| 448 | |
| 449 return darwin_major_version; | |
| 450 } | |
| 451 | |
| 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(); | |
| 457 | |
| 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 | |
| 463 // immediate death. | |
| 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); | |
| 469 | |
| 470 return mac_os_x_minor_version; | |
| 471 } | |
| 472 | |
| 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; | |
| 478 } | |
| 479 | |
| 480 enum { | |
| 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, | |
| 486 EL_CAPITAN_MINOR_VERSION = 11, | |
| 487 }; | |
| 488 | |
| 489 } // namespace | |
| 490 | |
| 491 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_7) | |
| 492 bool IsOSSnowLeopard() { | |
| 493 return MacOSXMinorVersion() == SNOW_LEOPARD_MINOR_VERSION; | |
| 494 } | |
| 495 #endif | |
| 496 | |
| 497 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_7) | |
| 498 bool IsOSLion() { | |
| 499 return MacOSXMinorVersion() == LION_MINOR_VERSION; | |
| 500 } | |
| 501 #endif | |
| 502 | |
| 503 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_7) | |
| 504 bool IsOSLionOrLater() { | |
| 505 return MacOSXMinorVersion() >= LION_MINOR_VERSION; | |
| 506 } | |
| 507 #endif | |
| 508 | |
| 509 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_8) | |
| 510 bool IsOSMountainLion() { | |
| 511 return MacOSXMinorVersion() == MOUNTAIN_LION_MINOR_VERSION; | |
| 512 } | |
| 513 #endif | |
| 514 | |
| 515 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_8) | |
| 516 bool IsOSMountainLionOrLater() { | |
| 517 return MacOSXMinorVersion() >= MOUNTAIN_LION_MINOR_VERSION; | |
| 518 } | |
| 519 #endif | |
| 520 | |
| 521 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_9) | |
| 522 bool IsOSMavericks() { | |
| 523 return MacOSXMinorVersion() == MAVERICKS_MINOR_VERSION; | |
| 524 } | |
| 525 #endif | |
| 526 | |
| 527 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_9) | |
| 528 bool IsOSMavericksOrLater() { | |
| 529 return MacOSXMinorVersion() >= MAVERICKS_MINOR_VERSION; | |
| 530 } | |
| 531 #endif | |
| 532 | |
| 533 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_10) | |
| 534 bool IsOSYosemite() { | |
| 535 return MacOSXMinorVersion() == YOSEMITE_MINOR_VERSION; | |
| 536 } | |
| 537 #endif | |
| 538 | |
| 539 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_10) | |
| 540 bool IsOSYosemiteOrLater() { | |
| 541 return MacOSXMinorVersion() >= YOSEMITE_MINOR_VERSION; | |
| 542 } | |
| 543 #endif | |
| 544 | |
| 545 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_10) | |
| 546 bool IsOSLaterThanYosemite_DontCallThis() { | |
| 547 return MacOSXMinorVersion() > YOSEMITE_MINOR_VERSION; | |
| 548 } | |
| 549 #endif | |
| 550 | |
| 551 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_11) | |
| 552 bool IsOSElCapitan() { | |
| 553 return MacOSXMinorVersion() == EL_CAPITAN_MINOR_VERSION; | |
| 554 } | |
| 555 #endif | |
| 556 | |
| 557 #if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_11) | |
| 558 bool IsOSElCapitanOrLater() { | |
| 559 return MacOSXMinorVersion() >= EL_CAPITAN_MINOR_VERSION; | |
| 560 } | |
| 561 #endif | |
| 562 | |
| 563 std::string GetModelIdentifier() { | |
| 564 std::string return_string; | |
| 565 ScopedIOObject<io_service_t> platform_expert( | |
| 566 IOServiceGetMatchingService(kIOMasterPortDefault, | |
| 567 IOServiceMatching("IOPlatformExpertDevice"))); | |
| 568 if (platform_expert) { | |
| 569 ScopedCFTypeRef<CFDataRef> model_data( | |
| 570 static_cast<CFDataRef>(IORegistryEntryCreateCFProperty( | |
| 571 platform_expert, | |
| 572 CFSTR("model"), | |
| 573 kCFAllocatorDefault, | |
| 574 0))); | |
| 575 if (model_data) { | |
| 576 return_string = | |
| 577 reinterpret_cast<const char*>(CFDataGetBytePtr(model_data)); | |
| 578 } | |
| 579 } | |
| 580 return return_string; | |
| 581 } | |
| 582 | |
| 583 bool ParseModelIdentifier(const std::string& ident, | |
| 584 std::string* type, | |
| 585 int32* major, | |
| 586 int32* minor) { | |
| 587 size_t number_loc = ident.find_first_of("0123456789"); | |
| 588 if (number_loc == std::string::npos) | |
| 589 return false; | |
| 590 size_t comma_loc = ident.find(',', number_loc); | |
| 591 if (comma_loc == std::string::npos) | |
| 592 return false; | |
| 593 int32 major_tmp, minor_tmp; | |
| 594 std::string::const_iterator begin = ident.begin(); | |
| 595 if (!StringToInt( | |
| 596 StringPiece(begin + number_loc, begin + comma_loc), &major_tmp) || | |
| 597 !StringToInt( | |
| 598 StringPiece(begin + comma_loc + 1, ident.end()), &minor_tmp)) | |
| 599 return false; | |
| 600 *type = ident.substr(0, number_loc); | |
| 601 *major = major_tmp; | |
| 602 *minor = minor_tmp; | |
| 603 return true; | |
| 604 } | |
| 605 | |
| 606 } // namespace mac | |
| 607 } // namespace base | |
| OLD | NEW |