OLD | NEW |
(Empty) | |
| 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/sys_string_conversions.h" |
| 9 |
| 10 namespace base { |
| 11 namespace mac { |
| 12 |
| 13 // NSBundle isn't threadsafe, all functions in this file must be called on the |
| 14 // main thread. |
| 15 static NSBundle* g_override_framework_bundle = nil; |
| 16 static NSBundle* g_override_outer_bundle = nil; |
| 17 |
| 18 NSBundle* MainBundle() { |
| 19 return [NSBundle mainBundle]; |
| 20 } |
| 21 |
| 22 FilePath MainBundlePath() { |
| 23 NSBundle* bundle = MainBundle(); |
| 24 return FilePath([[bundle bundlePath] fileSystemRepresentation]); |
| 25 } |
| 26 |
| 27 NSBundle* OuterBundle() { |
| 28 if (g_override_outer_bundle) |
| 29 return g_override_outer_bundle; |
| 30 return [NSBundle mainBundle]; |
| 31 } |
| 32 |
| 33 FilePath OuterBundlePath() { |
| 34 NSBundle* bundle = OuterBundle(); |
| 35 return FilePath([[bundle bundlePath] fileSystemRepresentation]); |
| 36 } |
| 37 |
| 38 NSBundle* FrameworkBundle() { |
| 39 if (g_override_framework_bundle) |
| 40 return g_override_framework_bundle; |
| 41 return [NSBundle mainBundle]; |
| 42 } |
| 43 |
| 44 FilePath FrameworkBundlePath() { |
| 45 NSBundle* bundle = FrameworkBundle(); |
| 46 return FilePath([[bundle bundlePath] fileSystemRepresentation]); |
| 47 } |
| 48 |
| 49 static void AssignOverrideBundle(NSBundle* new_bundle, |
| 50 NSBundle** override_bundle) { |
| 51 if (new_bundle != *override_bundle) { |
| 52 [*override_bundle release]; |
| 53 *override_bundle = [new_bundle retain]; |
| 54 } |
| 55 } |
| 56 |
| 57 static void AssignOverridePath(const FilePath& file_path, |
| 58 NSBundle** override_bundle) { |
| 59 NSString* path = base::SysUTF8ToNSString(file_path.value()); |
| 60 NSBundle* new_bundle = [NSBundle bundleWithPath:path]; |
| 61 DCHECK(new_bundle) << "Failed to load the bundle at " << file_path.value(); |
| 62 AssignOverrideBundle(new_bundle, override_bundle); |
| 63 } |
| 64 |
| 65 void SetOverrideOuterBundle(NSBundle* bundle) { |
| 66 AssignOverrideBundle(bundle, &g_override_outer_bundle); |
| 67 } |
| 68 |
| 69 void SetOverrideFrameworkBundle(NSBundle* bundle) { |
| 70 AssignOverrideBundle(bundle, &g_override_framework_bundle); |
| 71 } |
| 72 |
| 73 void SetOverrideOuterBundlePath(const FilePath& file_path) { |
| 74 AssignOverridePath(file_path, &g_override_outer_bundle); |
| 75 } |
| 76 |
| 77 void SetOverrideFrameworkBundlePath(const FilePath& file_path) { |
| 78 AssignOverridePath(file_path, &g_override_framework_bundle); |
| 79 } |
| 80 |
| 81 } // namespace mac |
| 82 } // namespace base |
OLD | NEW |