mac_util.h revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2010 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#include <vector> 12 13#include "base/logging.h" 14 15#if defined(__OBJC__) 16#import <Foundation/Foundation.h> 17 18@class NSBundle; 19@class NSWindow; 20#else // __OBJC__ 21class NSBundle; 22class NSImage; 23class NSWindow; 24#endif // __OBJC__ 25 26class FilePath; 27 28// Adapted from NSPathUtilities.h and NSObjCRuntime.h. 29#if __LP64__ || NS_BUILD_32_LIKE_64 30typedef unsigned long NSSearchPathDirectory; 31typedef unsigned long NSSearchPathDomainMask; 32#else 33typedef unsigned int NSSearchPathDirectory; 34typedef unsigned int NSSearchPathDomainMask; 35#endif 36 37namespace base { 38namespace mac { 39 40// Full screen modes, in increasing order of priority. More permissive modes 41// take predecence. 42enum FullScreenMode { 43 kFullScreenModeHideAll = 0, 44 kFullScreenModeHideDock = 1, 45 kFullScreenModeAutoHideAll = 2, 46 kNumFullScreenModes = 3, 47 48 // kFullScreenModeNormal is not a valid FullScreenMode, but it is useful to 49 // other classes, so we include it here. 50 kFullScreenModeNormal = 10, 51}; 52 53std::string PathFromFSRef(const FSRef& ref); 54bool FSRefFromPath(const std::string& path, FSRef* ref); 55 56// Returns true if the application is running from a bundle 57bool AmIBundled(); 58void SetOverrideAmIBundled(bool value); 59 60// Returns true if this process is marked as a "Background only process". 61bool IsBackgroundOnlyProcess(); 62 63// Returns the main bundle or the override, used for code that needs 64// to fetch resources from bundles, but work within a unittest where we 65// aren't a bundle. 66NSBundle* MainAppBundle(); 67FilePath MainAppBundlePath(); 68 69// Set the bundle that MainAppBundle will return, overriding the default value 70// (Restore the default by calling SetOverrideAppBundle(nil)). 71void SetOverrideAppBundle(NSBundle* bundle); 72void SetOverrideAppBundlePath(const FilePath& file_path); 73 74// Returns the creator code associated with the CFBundleRef at bundle. 75OSType CreatorCodeForCFBundleRef(CFBundleRef bundle); 76 77// Returns the creator code associated with this application, by calling 78// CreatorCodeForCFBundleRef for the application's main bundle. If this 79// information cannot be determined, returns kUnknownType ('????'). This 80// does not respect the override app bundle because it's based on CFBundle 81// instead of NSBundle, and because callers probably don't want the override 82// app bundle's creator code anyway. 83OSType CreatorCodeForApplication(); 84 85// Searches for directories for the given key in only the given |domain_mask|. 86// If found, fills result (which must always be non-NULL) with the 87// first found directory and returns true. Otherwise, returns false. 88bool GetSearchPathDirectory(NSSearchPathDirectory directory, 89 NSSearchPathDomainMask domain_mask, 90 FilePath* result); 91 92// Searches for directories for the given key in only the user domain. 93// If found, fills result (which must always be non-NULL) with the 94// first found directory and returns true. Otherwise, returns false. 95bool GetUserDirectory(NSSearchPathDirectory directory, FilePath* result); 96 97// Searches for directories for the given key in only the local domain. 98// If found, fills result (which must always be non-NULL) with the 99// first found directory and returns true. Otherwise, returns false. 100bool GetLocalDirectory(NSSearchPathDirectory directory, FilePath* result); 101 102// Returns the ~/Library directory. 103FilePath GetUserLibraryPath(); 104 105// Returns an sRGB color space. The return value is a static value; do not 106// release it! 107CGColorSpaceRef GetSRGBColorSpace(); 108 109// Returns the color space being used by the main display. The return value 110// is a static value; do not release it! 111CGColorSpaceRef GetSystemColorSpace(); 112 113// Add a full screen request for the given |mode|. Must be paired with a 114// ReleaseFullScreen() call for the same |mode|. This does not by itself create 115// a fullscreen window; rather, it manages per-application state related to 116// hiding the dock and menubar. Must be called on the main thread. 117void RequestFullScreen(FullScreenMode mode); 118 119// Release a request for full screen mode. Must be matched with a 120// RequestFullScreen() call for the same |mode|. As with RequestFullScreen(), 121// this does not affect windows directly, but rather manages per-application 122// state. For example, if there are no other outstanding 123// |kFullScreenModeAutoHideAll| requests, this will reshow the menu bar. Must 124// be called on main thread. 125void ReleaseFullScreen(FullScreenMode mode); 126 127// Convenience method to switch the current fullscreen mode. This has the same 128// net effect as a ReleaseFullScreen(from_mode) call followed immediately by a 129// RequestFullScreen(to_mode). Must be called on the main thread. 130void SwitchFullScreenModes(FullScreenMode from_mode, FullScreenMode to_mode); 131 132// Set the visibility of the cursor. 133void SetCursorVisibility(bool visible); 134 135// Should windows miniaturize on a double-click (on the title bar)? 136bool ShouldWindowsMiniaturizeOnDoubleClick(); 137 138// Activates the process with the given PID. 139void ActivateProcess(pid_t); 140 141// Pulls a snapshot of the entire browser into png_representation. 142void GrabWindowSnapshot(NSWindow* window, 143 std::vector<unsigned char>* png_representation, 144 int* width, int* height); 145 146// Takes a path to an (executable) binary and tries to provide the path to an 147// application bundle containing it. It takes the outermost bundle that it can 148// find (so for "/Foo/Bar.app/.../Baz.app/..." it produces "/Foo/Bar.app"). 149// |exec_name| - path to the binary 150// returns - path to the application bundle, or empty on error 151FilePath GetAppBundlePath(const FilePath& exec_name); 152 153// Set the Time Machine exclusion property for the given file. 154bool SetFileBackupExclusion(const FilePath& file_path, bool exclude); 155 156// Utility function to pull out a value from a dictionary, check its type, and 157// return it. Returns NULL if the key is not present or of the wrong type. 158CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, 159 CFStringRef key, 160 CFTypeID expected_type); 161 162// Sets the process name as displayed in Activity Monitor to process_name. 163void SetProcessName(CFStringRef process_name); 164 165// Converts a NSImage to a CGImageRef. Normally, the system frameworks can do 166// this fine, especially on 10.6. On 10.5, however, CGImage cannot handle 167// converting a PDF-backed NSImage into a CGImageRef. This function will 168// rasterize the PDF into a bitmap CGImage. The caller is responsible for 169// releasing the return value. 170CGImageRef CopyNSImageToCGImage(NSImage* image); 171 172// Checks if the current application is set as a Login Item, so it will launch 173// on Login. If a non-NULL pointer to is_hidden is passed, the Login Item also 174// is queried for the 'hide on launch' flag. 175bool CheckLoginItemStatus(bool* is_hidden); 176 177// Adds current application to the set of Login Items with specified "hide" 178// flag. This has the same effect as adding/removing the application in 179// SystemPreferences->Accounts->LoginItems or marking Application in the Dock 180// as "Options->Open on Login". 181// Does nothing if the application is already set up as Login Item with 182// specified hide flag. 183void AddToLoginItems(bool hide_on_startup); 184 185// Removes the current application from the list Of Login Items. 186void RemoveFromLoginItems(); 187 188// Returns true if the current process was automatically launched as a 189// 'Login Item' with 'hide on startup' flag. Used to suppress opening windows. 190bool WasLaunchedAsHiddenLoginItem(); 191 192// Retain/release calls for memory management in C++. 193void NSObjectRetain(void* obj); 194void NSObjectRelease(void* obj); 195 196#if defined(__OBJC__) 197 198// Convert toll-free bridged CFTypes to NSTypes. This does not autorelease 199// |cf_val|. This is useful for the case where there is a CFType in a call that 200// expects an NSType and the compiler is complaining about const casting 201// problems. 202// The call is used like this: 203// NSString *foo = CFToNSCast(CFSTR("Hello")); 204// The macro magic below is to enforce safe casting. It could possibly have 205// been done using template function specialization, but template function 206// specialization doesn't always work intuitively, 207// (http://www.gotw.ca/publications/mill17.htm) so the trusty combination 208// of macros and function overloading is used instead. 209 210#define CF_TO_NS_CAST(TypeCF, TypeNS) \ 211inline TypeNS* CFToNSCast(TypeCF cf_val) { \ 212 TypeNS* ns_val = \ 213 const_cast<TypeNS*>(reinterpret_cast<const TypeNS*>(cf_val)); \ 214 DCHECK(!ns_val || [ns_val isKindOfClass:[TypeNS class]]); \ 215 return ns_val; \ 216} 217 218// List of toll-free bridged types taken from: 219// http://www.cocoadev.com/index.pl?TollFreeBridged 220 221CF_TO_NS_CAST(CFArrayRef, NSArray); 222CF_TO_NS_CAST(CFMutableArrayRef, NSMutableArray); 223CF_TO_NS_CAST(CFAttributedStringRef, NSAttributedString); 224CF_TO_NS_CAST(CFMutableAttributedStringRef, NSMutableAttributedString); 225CF_TO_NS_CAST(CFCalendarRef, NSCalendar); 226CF_TO_NS_CAST(CFCharacterSetRef, NSCharacterSet); 227CF_TO_NS_CAST(CFMutableCharacterSetRef, NSMutableCharacterSet); 228CF_TO_NS_CAST(CFDataRef, NSData); 229CF_TO_NS_CAST(CFMutableDataRef, NSMutableData); 230CF_TO_NS_CAST(CFDateRef, NSDate); 231CF_TO_NS_CAST(CFDictionaryRef, NSDictionary); 232CF_TO_NS_CAST(CFMutableDictionaryRef, NSMutableDictionary); 233CF_TO_NS_CAST(CFNumberRef, NSNumber); 234CF_TO_NS_CAST(CFRunLoopTimerRef, NSTimer); 235CF_TO_NS_CAST(CFSetRef, NSSet); 236CF_TO_NS_CAST(CFMutableSetRef, NSMutableSet); 237CF_TO_NS_CAST(CFStringRef, NSString); 238CF_TO_NS_CAST(CFMutableStringRef, NSMutableString); 239CF_TO_NS_CAST(CFURLRef, NSURL); 240CF_TO_NS_CAST(CFTimeZoneRef, NSTimeZone); 241CF_TO_NS_CAST(CFReadStreamRef, NSInputStream); 242CF_TO_NS_CAST(CFWriteStreamRef, NSOutputStream); 243 244#endif // __OBJC__ 245 246} // namespace mac 247} // namespace base 248 249#endif // BASE_MAC_MAC_UTIL_H_ 250