[email protected] | e2ac7000 | 2008-12-09 14:58:13 | [diff] [blame] | 1 | // Copyright (c) 2008 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 | #include "base/mac_util.h" |
| 6 | |
[email protected] | 480e129 | 2008-12-10 22:05:40 | [diff] [blame] | 7 | #include <Carbon/Carbon.h> |
[email protected] | e2ac7000 | 2008-12-09 14:58:13 | [diff] [blame] | 8 | #import <Cocoa/Cocoa.h> |
| 9 | |
[email protected] | 6f33add3 | 2009-03-03 16:26:03 | [diff] [blame] | 10 | #include "base/file_path.h" |
| 11 | #include "base/logging.h" |
[email protected] | e2ac7000 | 2008-12-09 14:58:13 | [diff] [blame] | 12 | #include "base/scoped_cftyperef.h" |
[email protected] | 6f33add3 | 2009-03-03 16:26:03 | [diff] [blame] | 13 | #include "base/sys_string_conversions.h" |
[email protected] | e2ac7000 | 2008-12-09 14:58:13 | [diff] [blame] | 14 | |
| 15 | namespace mac_util { |
| 16 | |
| 17 | std::string PathFromFSRef(const FSRef& ref) { |
| 18 | scoped_cftyperef<CFURLRef> url( |
| 19 | CFURLCreateFromFSRef(kCFAllocatorDefault, &ref)); |
| 20 | NSString *path_string = [(NSURL *)url.get() path]; |
| 21 | return [path_string fileSystemRepresentation]; |
| 22 | } |
| 23 | |
| 24 | bool FSRefFromPath(const std::string& path, FSRef* ref) { |
| 25 | OSStatus status = FSPathMakeRef((const UInt8*)path.c_str(), |
| 26 | ref, nil); |
| 27 | return status == noErr; |
| 28 | } |
| 29 | |
[email protected] | 480e129 | 2008-12-10 22:05:40 | [diff] [blame] | 30 | // Adapted from http://developer.apple.com/carbon/tipsandtricks.html#AmIBundled |
| 31 | bool AmIBundled() { |
| 32 | ProcessSerialNumber psn = {0, kCurrentProcess}; |
| 33 | |
| 34 | FSRef fsref; |
| 35 | if (GetProcessBundleLocation(&psn, &fsref) != noErr) |
| 36 | return false; |
| 37 | |
| 38 | FSCatalogInfo info; |
| 39 | if (FSGetCatalogInfo(&fsref, kFSCatInfoNodeFlags, &info, |
| 40 | NULL, NULL, NULL) != noErr) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | return info.nodeFlags & kFSNodeIsDirectoryMask; |
| 45 | } |
| 46 | |
[email protected] | 6f33add3 | 2009-03-03 16:26:03 | [diff] [blame] | 47 | // No threading worries since NSBundle isn't thread safe. |
| 48 | static NSBundle* g_override_app_bundle = nil; |
| 49 | |
| 50 | NSBundle* MainAppBundle() { |
| 51 | if (g_override_app_bundle) |
| 52 | return g_override_app_bundle; |
| 53 | return [NSBundle mainBundle]; |
| 54 | } |
| 55 | |
| 56 | void SetOverrideAppBundle(NSBundle* bundle) { |
| 57 | [g_override_app_bundle release]; |
| 58 | g_override_app_bundle = [bundle retain]; |
| 59 | } |
| 60 | |
| 61 | void SetOverrideAppBundlePath(const FilePath& file_path) { |
| 62 | NSString* path = base::SysUTF8ToNSString(file_path.value()); |
| 63 | NSBundle* bundle = [NSBundle bundleWithPath:path]; |
| 64 | DCHECK(bundle) << "failed to load the bundle: " << file_path.value(); |
| 65 | |
| 66 | SetOverrideAppBundle(bundle); |
| 67 | } |
| 68 | |
[email protected] | e2ac7000 | 2008-12-09 14:58:13 | [diff] [blame] | 69 | } // namespace mac_util |