Roll gyp r1967:1970.
[chromium-blink-merge.git] / base / mac / bundle_locations.mm
blob54021b85ee08c29ad26e1c3a258f9165a87eb4a4
1 // Copyright (c) 2012 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.
5 #include "base/mac/bundle_locations.h"
7 #include "base/logging.h"
8 #include "base/mac/foundation_util.h"
9 #include "base/strings/sys_string_conversions.h"
11 namespace base {
12 namespace mac {
14 // NSBundle isn't threadsafe, all functions in this file must be called on the
15 // main thread.
16 static NSBundle* g_override_framework_bundle = nil;
17 static NSBundle* g_override_outer_bundle = nil;
19 NSBundle* MainBundle() {
20   return [NSBundle mainBundle];
23 FilePath MainBundlePath() {
24   NSBundle* bundle = MainBundle();
25   return NSStringToFilePath([bundle bundlePath]);
28 NSBundle* OuterBundle() {
29   if (g_override_outer_bundle)
30     return g_override_outer_bundle;
31   return [NSBundle mainBundle];
34 FilePath OuterBundlePath() {
35   NSBundle* bundle = OuterBundle();
36   return NSStringToFilePath([bundle bundlePath]);
39 NSBundle* FrameworkBundle() {
40   if (g_override_framework_bundle)
41     return g_override_framework_bundle;
42   return [NSBundle mainBundle];
45 FilePath FrameworkBundlePath() {
46   NSBundle* bundle = FrameworkBundle();
47   return NSStringToFilePath([bundle bundlePath]);
50 static void AssignOverrideBundle(NSBundle* new_bundle,
51                                  NSBundle** override_bundle) {
52   if (new_bundle != *override_bundle) {
53     [*override_bundle release];
54     *override_bundle = [new_bundle retain];
55   }
58 static void AssignOverridePath(const FilePath& file_path,
59                                NSBundle** override_bundle) {
60   NSString* path = base::SysUTF8ToNSString(file_path.value());
61   NSBundle* new_bundle = [NSBundle bundleWithPath:path];
62   DCHECK(new_bundle) << "Failed to load the bundle at " << file_path.value();
63   AssignOverrideBundle(new_bundle, override_bundle);
66 void SetOverrideOuterBundle(NSBundle* bundle) {
67   AssignOverrideBundle(bundle, &g_override_outer_bundle);
70 void SetOverrideFrameworkBundle(NSBundle* bundle) {
71   AssignOverrideBundle(bundle, &g_override_framework_bundle);
74 void SetOverrideOuterBundlePath(const FilePath& file_path) {
75   AssignOverridePath(file_path, &g_override_outer_bundle);
78 void SetOverrideFrameworkBundlePath(const FilePath& file_path) {
79   AssignOverridePath(file_path, &g_override_framework_bundle);
82 }  // namespace mac
83 }  // namespace base