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