foundation_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_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__
18class NSBundle;
19#endif  // __OBJC__
20
21class FilePath;
22
23// Adapted from NSPathUtilities.h and NSObjCRuntime.h.
24#if __LP64__ || NS_BUILD_32_LIKE_64
25typedef unsigned long NSSearchPathDirectory;
26typedef unsigned long NSSearchPathDomainMask;
27#else
28typedef unsigned int NSSearchPathDirectory;
29typedef unsigned int NSSearchPathDomainMask;
30#endif
31
32namespace base {
33namespace mac {
34
35// Returns true if the application is running from a bundle
36bool AmIBundled();
37void SetOverrideAmIBundled(bool value);
38
39// Returns true if this process is marked as a "Background only process".
40bool 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.
45NSBundle* MainAppBundle();
46FilePath MainAppBundlePath();
47
48// Returns the path to a resource within the MainAppBundle.
49FilePath PathForMainAppBundleResource(CFStringRef resourceName);
50
51// Set the bundle that MainAppBundle will return, overriding the default value
52// (Restore the default by calling SetOverrideAppBundle(nil)).
53void SetOverrideAppBundle(NSBundle* bundle);
54void SetOverrideAppBundlePath(const FilePath& file_path);
55
56// Returns the creator code associated with the CFBundleRef at bundle.
57OSType 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.
65OSType 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.
70bool 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.
77bool 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.
82bool GetUserDirectory(NSSearchPathDirectory directory, FilePath* result);
83
84// Returns the ~/Library directory.
85FilePath 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
92FilePath 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.
96CFTypeRef GetValueFromDictionary(CFDictionaryRef dict,
97                                 CFStringRef key,
98                                 CFTypeID expected_type);
99
100// Retain/release calls for memory management in C++.
101void NSObjectRetain(void* obj);
102void NSObjectRelease(void* obj);
103
104}  // namespace mac
105}  // namespace base
106
107#endif  // BASE_MAC_FOUNDATION_UTIL_H_
108