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.
4
5#include "base/mac/bundle_locations.h"
6
7#include "base/logging.h"
8#include "base/mac/foundation_util.h"
9#include "base/strings/sys_string_conversions.h"
10
11namespace base {
12namespace mac {
13
14// NSBundle isn't threadsafe, all functions in this file must be called on the
15// main thread.
16static NSBundle* g_override_framework_bundle = nil;
17static NSBundle* g_override_outer_bundle = nil;
18
19NSBundle* MainBundle() {
20  return [NSBundle mainBundle];
21}
22
23FilePath MainBundlePath() {
24  NSBundle* bundle = MainBundle();
25  return NSStringToFilePath([bundle bundlePath]);
26}
27
28NSBundle* OuterBundle() {
29  if (g_override_outer_bundle)
30    return g_override_outer_bundle;
31  return [NSBundle mainBundle];
32}
33
34FilePath OuterBundlePath() {
35  NSBundle* bundle = OuterBundle();
36  return NSStringToFilePath([bundle bundlePath]);
37}
38
39NSBundle* FrameworkBundle() {
40  if (g_override_framework_bundle)
41    return g_override_framework_bundle;
42  return [NSBundle mainBundle];
43}
44
45FilePath FrameworkBundlePath() {
46  NSBundle* bundle = FrameworkBundle();
47  return NSStringToFilePath([bundle bundlePath]);
48}
49
50static 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  }
56}
57
58static 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);
64}
65
66void SetOverrideOuterBundle(NSBundle* bundle) {
67  AssignOverrideBundle(bundle, &g_override_outer_bundle);
68}
69
70void SetOverrideFrameworkBundle(NSBundle* bundle) {
71  AssignOverrideBundle(bundle, &g_override_framework_bundle);
72}
73
74void SetOverrideOuterBundlePath(const FilePath& file_path) {
75  AssignOverridePath(file_path, &g_override_outer_bundle);
76}
77
78void SetOverrideFrameworkBundlePath(const FilePath& file_path) {
79  AssignOverridePath(file_path, &g_override_framework_bundle);
80}
81
82}  // namespace mac
83}  // namespace base
84