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 32NSBundle* OuterBundle() { 33 if (g_override_outer_bundle) { 34 return g_override_outer_bundle; 35 } 36 return NSBundle.mainBundle; 37} 38 39NSURL* OuterBundleURL() { 40 return OuterBundle().bundleURL; 41} 42 43FilePath OuterBundlePath() { 44 return apple::NSStringToFilePath(OuterBundle().bundlePath); 45} 46 47NSBundle* FrameworkBundle() { 48 if (g_override_framework_bundle) { 49 return g_override_framework_bundle; 50 } 51 return NSBundle.mainBundle; 52} 53 54FilePath FrameworkBundlePath() { 55 return apple::NSStringToFilePath(FrameworkBundle().bundlePath); 56} 57 58namespace { 59 60NSBundle* BundleFromPath(const FilePath& file_path) { 61 if (file_path.empty()) { 62 return nil; 63 } 64 65 NSBundle* bundle = [NSBundle bundleWithURL:apple::FilePathToNSURL(file_path)]; 66 CHECK(bundle) << "Failed to load the bundle at " << file_path.value(); 67 68 return bundle; 69} 70 71} // namespace 72 73void SetOverrideOuterBundle(NSBundle* bundle) { 74 g_override_outer_bundle = bundle; 75} 76 77void SetOverrideFrameworkBundle(NSBundle* bundle) { 78 g_override_framework_bundle = bundle; 79} 80 81void SetOverrideOuterBundlePath(const FilePath& file_path) { 82 NSBundle* bundle = BundleFromPath(file_path); 83 g_override_outer_bundle = bundle; 84} 85 86void SetOverrideFrameworkBundlePath(const FilePath& file_path) { 87 NSBundle* bundle = BundleFromPath(file_path); 88 g_override_framework_bundle = bundle; 89} 90 91} // namespace base::apple 92