mac_util.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2011 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#pragma once 8 9#include <Carbon/Carbon.h> 10#include <string> 11 12#include "base/logging.h" 13 14// TODO(rohitrao): Clean up sites that include mac_util.h and remove this line. 15#include "base/mac/foundation_util.h" 16 17#if defined(__OBJC__) 18#import <Foundation/Foundation.h> 19#else // __OBJC__ 20class NSImage; 21#endif // __OBJC__ 22 23class FilePath; 24 25namespace base { 26namespace mac { 27 28// Full screen modes, in increasing order of priority. More permissive modes 29// take predecence. 30enum 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 41std::string PathFromFSRef(const FSRef& ref); 42bool 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! 46CGColorSpaceRef GetSRGBColorSpace(); 47 48// Returns the color space being used by the main display. The return value 49// is a static value; do not release it! 50CGColorSpaceRef GetSystemColorSpace(); 51 52// Add a full screen request for the given |mode|. Must be paired with a 53// ReleaseFullScreen() call for the same |mode|. This does not by itself create 54// a fullscreen window; rather, it manages per-application state related to 55// hiding the dock and menubar. Must be called on the main thread. 56void RequestFullScreen(FullScreenMode mode); 57 58// Release a request for full screen mode. Must be matched with a 59// RequestFullScreen() call for the same |mode|. As with RequestFullScreen(), 60// this does not affect windows directly, but rather manages per-application 61// state. For example, if there are no other outstanding 62// |kFullScreenModeAutoHideAll| requests, this will reshow the menu bar. Must 63// be called on main thread. 64void ReleaseFullScreen(FullScreenMode mode); 65 66// Convenience method to switch the current fullscreen mode. This has the same 67// net effect as a ReleaseFullScreen(from_mode) call followed immediately by a 68// RequestFullScreen(to_mode). Must be called on the main thread. 69void SwitchFullScreenModes(FullScreenMode from_mode, FullScreenMode to_mode); 70 71// Set the visibility of the cursor. 72void SetCursorVisibility(bool visible); 73 74// Should windows miniaturize on a double-click (on the title bar)? 75bool ShouldWindowsMiniaturizeOnDoubleClick(); 76 77// Activates the process with the given PID. 78void ActivateProcess(pid_t pid); 79 80// Set the Time Machine exclusion property for the given file. 81bool SetFileBackupExclusion(const FilePath& file_path, bool exclude); 82 83// Sets the process name as displayed in Activity Monitor to process_name. 84void SetProcessName(CFStringRef process_name); 85 86// Converts a NSImage to a CGImageRef. Normally, the system frameworks can do 87// this fine, especially on 10.6. On 10.5, however, CGImage cannot handle 88// converting a PDF-backed NSImage into a CGImageRef. This function will 89// rasterize the PDF into a bitmap CGImage. The caller is responsible for 90// releasing the return value. 91CGImageRef CopyNSImageToCGImage(NSImage* image); 92 93// Checks if the current application is set as a Login Item, so it will launch 94// on Login. If a non-NULL pointer to is_hidden is passed, the Login Item also 95// is queried for the 'hide on launch' flag. 96bool CheckLoginItemStatus(bool* is_hidden); 97 98// Adds current application to the set of Login Items with specified "hide" 99// flag. This has the same effect as adding/removing the application in 100// SystemPreferences->Accounts->LoginItems or marking Application in the Dock 101// as "Options->Open on Login". 102// Does nothing if the application is already set up as Login Item with 103// specified hide flag. 104void AddToLoginItems(bool hide_on_startup); 105 106// Removes the current application from the list Of Login Items. 107void RemoveFromLoginItems(); 108 109// Returns true if the current process was automatically launched as a 110// 'Login Item' with 'hide on startup' flag. Used to suppress opening windows. 111bool WasLaunchedAsHiddenLoginItem(); 112 113#if defined(__OBJC__) 114 115// Convert toll-free bridged CFTypes to NSTypes. This does not autorelease 116// |cf_val|. This is useful for the case where there is a CFType in a call that 117// expects an NSType and the compiler is complaining about const casting 118// problems. 119// The call is used like this: 120// NSString *foo = CFToNSCast(CFSTR("Hello")); 121// The macro magic below is to enforce safe casting. It could possibly have 122// been done using template function specialization, but template function 123// specialization doesn't always work intuitively, 124// (http://www.gotw.ca/publications/mill17.htm) so the trusty combination 125// of macros and function overloading is used instead. 126 127#define CF_TO_NS_CAST(TypeCF, TypeNS) \ 128inline TypeNS* CFToNSCast(TypeCF cf_val) { \ 129 TypeNS* ns_val = \ 130 const_cast<TypeNS*>(reinterpret_cast<const TypeNS*>(cf_val)); \ 131 DCHECK(!ns_val || [ns_val isKindOfClass:[TypeNS class]]); \ 132 return ns_val; \ 133} 134 135// List of toll-free bridged types taken from: 136// http://www.cocoadev.com/index.pl?TollFreeBridged 137 138CF_TO_NS_CAST(CFArrayRef, NSArray); 139CF_TO_NS_CAST(CFMutableArrayRef, NSMutableArray); 140CF_TO_NS_CAST(CFAttributedStringRef, NSAttributedString); 141CF_TO_NS_CAST(CFMutableAttributedStringRef, NSMutableAttributedString); 142CF_TO_NS_CAST(CFCalendarRef, NSCalendar); 143CF_TO_NS_CAST(CFCharacterSetRef, NSCharacterSet); 144CF_TO_NS_CAST(CFMutableCharacterSetRef, NSMutableCharacterSet); 145CF_TO_NS_CAST(CFDataRef, NSData); 146CF_TO_NS_CAST(CFMutableDataRef, NSMutableData); 147CF_TO_NS_CAST(CFDateRef, NSDate); 148CF_TO_NS_CAST(CFDictionaryRef, NSDictionary); 149CF_TO_NS_CAST(CFMutableDictionaryRef, NSMutableDictionary); 150CF_TO_NS_CAST(CFNumberRef, NSNumber); 151CF_TO_NS_CAST(CFRunLoopTimerRef, NSTimer); 152CF_TO_NS_CAST(CFSetRef, NSSet); 153CF_TO_NS_CAST(CFMutableSetRef, NSMutableSet); 154CF_TO_NS_CAST(CFStringRef, NSString); 155CF_TO_NS_CAST(CFMutableStringRef, NSMutableString); 156CF_TO_NS_CAST(CFURLRef, NSURL); 157CF_TO_NS_CAST(CFTimeZoneRef, NSTimeZone); 158CF_TO_NS_CAST(CFReadStreamRef, NSInputStream); 159CF_TO_NS_CAST(CFWriteStreamRef, NSOutputStream); 160 161#endif // __OBJC__ 162 163} // namespace mac 164} // namespace base 165 166#endif // BASE_MAC_MAC_UTIL_H_ 167