mac_util.h revision dc0f95d653279beabeb9817299e2902918ba123e
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} // namespace mac 114} // namespace base 115 116#if !defined(__OBJC__) 117#define OBJC_CPP_CLASS_DECL(x) class x; 118#else // __OBJC__ 119#define OBJC_CPP_CLASS_DECL(x) 120#endif // __OBJC__ 121 122// Convert toll-free bridged CFTypes to NSTypes and vice-versa. This does not 123// autorelease |cf_val|. This is useful for the case where there is a CFType in 124// a call that expects an NSType and the compiler is complaining about const 125// casting problems. 126// The calls are used like this: 127// NSString *foo = CFToNSCast(CFSTR("Hello")); 128// CFStringRef foo2 = NSToCFCast(@"Hello"); 129// The macro magic below is to enforce safe casting. It could possibly have 130// been done using template function specialization, but template function 131// specialization doesn't always work intuitively, 132// (http://www.gotw.ca/publications/mill17.htm) so the trusty combination 133// of macros and function overloading is used instead. 134 135#define CF_TO_NS_CAST_DECL(TypeCF, TypeNS) \ 136OBJC_CPP_CLASS_DECL(TypeNS) \ 137\ 138namespace base { \ 139namespace mac { \ 140TypeNS* CFToNSCast(TypeCF##Ref cf_val); \ 141TypeCF##Ref NSToCFCast(TypeNS* ns_val); \ 142} \ 143} \ 144 145#define CF_TO_NS_MUTABLE_CAST_DECL(name) \ 146CF_TO_NS_CAST_DECL(CF##name, NS##name) \ 147OBJC_CPP_CLASS_DECL(NSMutable##name) \ 148\ 149namespace base { \ 150namespace mac { \ 151NSMutable##name* CFToNSCast(CFMutable##name##Ref cf_val); \ 152CFMutable##name##Ref NSToCFCast(NSMutable##name* ns_val); \ 153} \ 154} \ 155 156// List of toll-free bridged types taken from: 157// http://www.cocoadev.com/index.pl?TollFreeBridged 158 159CF_TO_NS_MUTABLE_CAST_DECL(Array); 160CF_TO_NS_MUTABLE_CAST_DECL(AttributedString); 161CF_TO_NS_CAST_DECL(CFCalendar, NSCalendar); 162CF_TO_NS_MUTABLE_CAST_DECL(CharacterSet); 163CF_TO_NS_MUTABLE_CAST_DECL(Data); 164CF_TO_NS_CAST_DECL(CFDate, NSDate); 165CF_TO_NS_MUTABLE_CAST_DECL(Dictionary); 166CF_TO_NS_CAST_DECL(CFError, NSError); 167CF_TO_NS_CAST_DECL(CFLocale, NSLocale); 168CF_TO_NS_CAST_DECL(CFNumber, NSNumber); 169CF_TO_NS_CAST_DECL(CFRunLoopTimer, NSTimer); 170CF_TO_NS_CAST_DECL(CFTimeZone, NSTimeZone); 171CF_TO_NS_MUTABLE_CAST_DECL(Set); 172CF_TO_NS_CAST_DECL(CFReadStream, NSInputStream); 173CF_TO_NS_CAST_DECL(CFWriteStream, NSOutputStream); 174CF_TO_NS_MUTABLE_CAST_DECL(String); 175CF_TO_NS_CAST_DECL(CFURL, NSURL); 176 177// Stream operations for CFTypes. They can be used with NSTypes as well 178// by using the NSToCFCast methods above. 179// e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); 180// Operator << can not be overloaded for ObjectiveC types as the compiler 181// can not distinguish between overloads for id with overloads for void*. 182extern std::ostream& operator<<(std::ostream& o, const CFErrorRef err); 183extern std::ostream& operator<<(std::ostream& o, const CFStringRef str); 184 185#endif // BASE_MAC_MAC_UTIL_H_ 186