xref: /aosp_15_r20/external/cronet/base/apple/bundle_locations.mm (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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/apple/bundle_locations.h"
6
7#include "base/apple/foundation_util.h"
8#include "base/check.h"
9#include "base/strings/sys_string_conversions.h"
10
11namespace base::apple {
12
13namespace {
14
15NSBundle* g_override_framework_bundle = nil;
16NSBundle* g_override_outer_bundle = nil;
17
18}  // namespace
19
20NSBundle* MainBundle() {
21  return NSBundle.mainBundle;
22}
23
24NSURL* MainBundleURL() {
25  return MainBundle().bundleURL;
26}
27
28FilePath MainBundlePath() {
29  return apple::NSStringToFilePath(MainBundle().bundlePath);
30}
31
32std::string MainBundleIdentifier() {
33  return base::SysNSStringToUTF8(MainBundle().bundleIdentifier);
34}
35
36NSBundle* OuterBundle() {
37  if (g_override_outer_bundle) {
38    return g_override_outer_bundle;
39  }
40  return NSBundle.mainBundle;
41}
42
43NSURL* OuterBundleURL() {
44  return OuterBundle().bundleURL;
45}
46
47FilePath OuterBundlePath() {
48  return apple::NSStringToFilePath(OuterBundle().bundlePath);
49}
50
51NSBundle* FrameworkBundle() {
52  if (g_override_framework_bundle) {
53    return g_override_framework_bundle;
54  }
55  return NSBundle.mainBundle;
56}
57
58FilePath FrameworkBundlePath() {
59  return apple::NSStringToFilePath(FrameworkBundle().bundlePath);
60}
61
62namespace {
63
64NSBundle* BundleFromPath(const FilePath& file_path) {
65  if (file_path.empty()) {
66    return nil;
67  }
68
69  NSBundle* bundle = [NSBundle bundleWithURL:apple::FilePathToNSURL(file_path)];
70  CHECK(bundle) << "Failed to load the bundle at " << file_path.value();
71
72  return bundle;
73}
74
75}  // namespace
76
77void SetOverrideOuterBundle(NSBundle* bundle) {
78  g_override_outer_bundle = bundle;
79}
80
81void SetOverrideFrameworkBundle(NSBundle* bundle) {
82  g_override_framework_bundle = bundle;
83}
84
85void SetOverrideOuterBundlePath(const FilePath& file_path) {
86  NSBundle* bundle = BundleFromPath(file_path);
87  g_override_outer_bundle = bundle;
88}
89
90void SetOverrideFrameworkBundlePath(const FilePath& file_path) {
91  NSBundle* bundle = BundleFromPath(file_path);
92  g_override_framework_bundle = bundle;
93}
94
95}  // namespace base::apple
96