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_FOUNDATION_UTIL_H_ 6 #define BASE_MAC_FOUNDATION_UTIL_H_ 7 #pragma once 8 9 #include <string> 10 #include <vector> 11 12 #include "base/logging.h" 13 14 #if defined(__OBJC__) 15 #import <Foundation/Foundation.h> 16 @class NSBundle; 17 #else // __OBJC__ 18 class NSBundle; 19 #endif // __OBJC__ 20 21 class FilePath; 22 23 // Adapted from NSPathUtilities.h and NSObjCRuntime.h. 24 #if __LP64__ || NS_BUILD_32_LIKE_64 25 typedef unsigned long NSSearchPathDirectory; 26 typedef unsigned long NSSearchPathDomainMask; 27 #else 28 typedef unsigned int NSSearchPathDirectory; 29 typedef unsigned int NSSearchPathDomainMask; 30 #endif 31 32 namespace base { 33 namespace mac { 34 35 // Returns true if the application is running from a bundle 36 bool AmIBundled(); 37 void SetOverrideAmIBundled(bool value); 38 39 // Returns true if this process is marked as a "Background only process". 40 bool IsBackgroundOnlyProcess(); 41 42 // Returns the main bundle or the override, used for code that needs 43 // to fetch resources from bundles, but work within a unittest where we 44 // aren't a bundle. 45 NSBundle* MainAppBundle(); 46 FilePath MainAppBundlePath(); 47 48 // Returns the path to a resource within the MainAppBundle. 49 FilePath PathForMainAppBundleResource(CFStringRef resourceName); 50 51 // Set the bundle that MainAppBundle will return, overriding the default value 52 // (Restore the default by calling SetOverrideAppBundle(nil)). 53 void SetOverrideAppBundle(NSBundle* bundle); 54 void SetOverrideAppBundlePath(const FilePath& file_path); 55 56 // Returns the creator code associated with the CFBundleRef at bundle. 57 OSType CreatorCodeForCFBundleRef(CFBundleRef bundle); 58 59 // Returns the creator code associated with this application, by calling 60 // CreatorCodeForCFBundleRef for the application's main bundle. If this 61 // information cannot be determined, returns kUnknownType ('????'). This 62 // does not respect the override app bundle because it's based on CFBundle 63 // instead of NSBundle, and because callers probably don't want the override 64 // app bundle's creator code anyway. 65 OSType CreatorCodeForApplication(); 66 67 // Searches for directories for the given key in only the given |domain_mask|. 68 // If found, fills result (which must always be non-NULL) with the 69 // first found directory and returns true. Otherwise, returns false. 70 bool GetSearchPathDirectory(NSSearchPathDirectory directory, 71 NSSearchPathDomainMask domain_mask, 72 FilePath* result); 73 74 // Searches for directories for the given key in only the local domain. 75 // If found, fills result (which must always be non-NULL) with the 76 // first found directory and returns true. Otherwise, returns false. 77 bool GetLocalDirectory(NSSearchPathDirectory directory, FilePath* result); 78 79 // Searches for directories for the given key in only the user domain. 80 // If found, fills result (which must always be non-NULL) with the 81 // first found directory and returns true. Otherwise, returns false. 82 bool GetUserDirectory(NSSearchPathDirectory directory, FilePath* result); 83 84 // Returns the ~/Library directory. 85 FilePath GetUserLibraryPath(); 86 87 // Takes a path to an (executable) binary and tries to provide the path to an 88 // application bundle containing it. It takes the outermost bundle that it can 89 // find (so for "/Foo/Bar.app/.../Baz.app/..." it produces "/Foo/Bar.app"). 90 // |exec_name| - path to the binary 91 // returns - path to the application bundle, or empty on error 92 FilePath GetAppBundlePath(const FilePath& exec_name); 93 94 // Utility function to pull out a value from a dictionary, check its type, and 95 // return it. Returns NULL if the key is not present or of the wrong type. 96 CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, 97 CFStringRef key, 98 CFTypeID expected_type); 99 100 // Retain/release calls for memory management in C++. 101 void NSObjectRetain(void* obj); 102 void NSObjectRelease(void* obj); 103 104 } // namespace mac 105 } // namespace base 106 107 #endif // BASE_MAC_FOUNDATION_UTIL_H_ 108