blob: 156cb1c2fa7ba02d5d236dec7c9f63240c873213 [file] [log] [blame]
[email protected]e2ac70002008-12-09 14:58:131// 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]480e1292008-12-10 22:05:407#include <Carbon/Carbon.h>
[email protected]e2ac70002008-12-09 14:58:138#import <Cocoa/Cocoa.h>
9
[email protected]6f33add32009-03-03 16:26:0310#include "base/file_path.h"
11#include "base/logging.h"
[email protected]e2ac70002008-12-09 14:58:1312#include "base/scoped_cftyperef.h"
[email protected]6f33add32009-03-03 16:26:0313#include "base/sys_string_conversions.h"
[email protected]e2ac70002008-12-09 14:58:1314
15namespace mac_util {
16
17std::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
24bool 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]480e1292008-12-10 22:05:4030// Adapted from http://developer.apple.com/carbon/tipsandtricks.html#AmIBundled
31bool 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]6f33add32009-03-03 16:26:0347// No threading worries since NSBundle isn't thread safe.
48static NSBundle* g_override_app_bundle = nil;
49
50NSBundle* MainAppBundle() {
51 if (g_override_app_bundle)
52 return g_override_app_bundle;
53 return [NSBundle mainBundle];
54}
55
56void SetOverrideAppBundle(NSBundle* bundle) {
57 [g_override_app_bundle release];
58 g_override_app_bundle = [bundle retain];
59}
60
61void 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]e2ac70002008-12-09 14:58:1369} // namespace mac_util