• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2012 The Chromium Authors
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/check.h"
8#include "base/mac/foundation_util.h"
9#include "base/strings/sys_string_conversions.h"
10
11namespace base::mac {
12
13// NSBundle isn't threadsafe, all functions in this file must be called on the
14// main thread.
15static NSBundle* g_override_framework_bundle = nil;
16static NSBundle* g_override_outer_bundle = nil;
17
18NSBundle* MainBundle() {
19  return NSBundle.mainBundle;
20}
21
22NSURL* MainBundleURL() {
23  return MainBundle().bundleURL;
24}
25
26FilePath MainBundlePath() {
27  return NSStringToFilePath(MainBundle().bundlePath);
28}
29
30NSBundle* OuterBundle() {
31  if (g_override_outer_bundle)
32    return g_override_outer_bundle;
33  return NSBundle.mainBundle;
34}
35
36NSURL* OuterBundleURL() {
37  return OuterBundle().bundleURL;
38}
39
40FilePath OuterBundlePath() {
41  return NSStringToFilePath(OuterBundle().bundlePath);
42}
43
44NSBundle* FrameworkBundle() {
45  if (g_override_framework_bundle)
46    return g_override_framework_bundle;
47  return NSBundle.mainBundle;
48}
49
50FilePath FrameworkBundlePath() {
51  return NSStringToFilePath(FrameworkBundle().bundlePath);
52}
53
54static void AssignOverrideBundle(NSBundle* new_bundle,
55                                 NSBundle** override_bundle) {
56  if (new_bundle != *override_bundle) {
57    [*override_bundle release];
58    *override_bundle = [new_bundle retain];
59  }
60}
61
62static void AssignOverridePath(const FilePath& file_path,
63                               NSBundle** override_bundle) {
64  NSBundle* new_bundle = nil;
65  if (!file_path.empty()) {
66    new_bundle = [NSBundle bundleWithURL:base::mac::FilePathToNSURL(file_path)];
67    CHECK(new_bundle) << "Failed to load the bundle at " << file_path.value();
68  }
69  AssignOverrideBundle(new_bundle, override_bundle);
70}
71
72void SetOverrideOuterBundle(NSBundle* bundle) {
73  AssignOverrideBundle(bundle, &g_override_outer_bundle);
74}
75
76void SetOverrideFrameworkBundle(NSBundle* bundle) {
77  AssignOverrideBundle(bundle, &g_override_framework_bundle);
78}
79
80void SetOverrideOuterBundlePath(const FilePath& file_path) {
81  AssignOverridePath(file_path, &g_override_outer_bundle);
82}
83
84void SetOverrideFrameworkBundlePath(const FilePath& file_path) {
85  AssignOverridePath(file_path, &g_override_framework_bundle);
86}
87
88}  // namespace base::mac
89