summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorjeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-11 21:35:51 +0000
committerjeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-11 21:35:51 +0000
commit6087662be30caa92947109d6f5c0de51e71190ae (patch)
tree665fdba71978bc4c61dc21bad7586a7c4324d8f7 /base
parent2511cef12cee4b2089094ae1f455e31f0b3ca800 (diff)
downloadchromium_src-6087662be30caa92947109d6f5c0de51e71190ae.zip
chromium_src-6087662be30caa92947109d6f5c0de51e71190ae.tar.gz
chromium_src-6087662be30caa92947109d6f5c0de51e71190ae.tar.bz2
Functions to return locations of various Chrome bundles.
TEST=None, code is currently not called. BUG=None Review URL: http://codereview.chromium.org/9147031 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117293 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/base.gypi2
-rw-r--r--base/mac/bundle_locations.h67
-rw-r--r--base/mac/bundle_locations.mm82
3 files changed, 151 insertions, 0 deletions
diff --git a/base/base.gypi b/base/base.gypi
index 3c02234..20de4c6 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -143,6 +143,8 @@
'logging.h',
'logging_win.cc',
'logging_win.h',
+ 'mac/bundle_locations.h',
+ 'mac/bundle_locations.mm',
'mac/cocoa_protocols.h',
'mac/crash_logging.h',
'mac/crash_logging.mm',
diff --git a/base/mac/bundle_locations.h b/base/mac/bundle_locations.h
new file mode 100644
index 0000000..00ca05d
--- /dev/null
+++ b/base/mac/bundle_locations.h
@@ -0,0 +1,67 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_MAC_BUNDLE_LOCATIONS_H_
+#define BASE_MAC_BUNDLE_LOCATIONS_H_
+#pragma once
+
+#include "base/base_export.h"
+#include "base/file_path.h"
+
+#if defined(__OBJC__)
+#import <Foundation/Foundation.h>
+#else // __OBJC__
+class NSBundle;
+class NSString;
+#endif // __OBJC__
+
+class FilePath;
+
+namespace base {
+namespace mac {
+
+// This file provides several functions to explicitly request the various
+// component bundles of Chrome. Please use these methods rather than calling
+// +[NSBundle mainBundle] or CFBundleGetMainBundle().
+//
+// Terminology
+// - "Outer Bundle" - This is the main bundle for Chrome; it's what
+// +[NSBundle mainBundle] returns when Chrome is launched normally.
+//
+// - "Main Bundle" - This is the bundle from which Chrome was launched.
+// This will be the same as the outer bundle except when Chrome is launched
+// via an app shortcut, in which case this will return the app shortcut's
+// bundle rather than the main Chrome bundle.
+//
+// - "Framework Bundle" - This is the bundle corresponding to the Chrome
+// framework.
+//
+// Guidelines for use:
+// - To access a resource, the Framework bundle should be used.
+// - If the choice is between the Outer or Main bundles then please choose
+// carefully. Most often the Outer bundle will be the right choice, but for
+// cases such as adding an app to the "launch on startup" list, the Main
+// bundle is probably the one to use.
+
+// Methods for retrieving the various bundles.
+BASE_EXPORT NSBundle* MainBundle();
+BASE_EXPORT FilePath MainBundlePath();
+BASE_EXPORT NSBundle* OuterBundle();
+BASE_EXPORT FilePath OuterBundlePath();
+BASE_EXPORT NSBundle* FrameworkBundle();
+BASE_EXPORT FilePath FrameworkBundlePath();
+
+// Set the bundle that the preceding functions will return, overriding the
+// default values. Restore the default by passing in |nil|.
+BASE_EXPORT void SetOverrideOuterBundle(NSBundle* bundle);
+BASE_EXPORT void SetOverrideFrameworkBundle(NSBundle* bundle);
+
+// Same as above but accepting a FilePath argument.
+BASE_EXPORT void SetOverrideOuterBundlePath(const FilePath& file_path);
+BASE_EXPORT void SetOverrideFrameworkBundlePath(const FilePath& file_path);
+
+} // namespace mac
+} // namespace base
+
+#endif // BASE_MAC_BUNDLE_LOCATIONS_H_
diff --git a/base/mac/bundle_locations.mm b/base/mac/bundle_locations.mm
new file mode 100644
index 0000000..36fad66
--- /dev/null
+++ b/base/mac/bundle_locations.mm
@@ -0,0 +1,82 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/mac/bundle_locations.h"
+
+#include "base/logging.h"
+#include "base/sys_string_conversions.h"
+
+namespace base {
+namespace mac {
+
+// NSBundle isn't threadsafe, all functions in this file must be called on the
+// main thread.
+static NSBundle* g_override_framework_bundle = nil;
+static NSBundle* g_override_outer_bundle = nil;
+
+NSBundle* MainBundle() {
+ return [NSBundle mainBundle];
+}
+
+FilePath MainBundlePath() {
+ NSBundle* bundle = MainBundle();
+ return FilePath([[bundle bundlePath] fileSystemRepresentation]);
+}
+
+NSBundle* OuterBundle() {
+ if (g_override_outer_bundle)
+ return g_override_outer_bundle;
+ return [NSBundle mainBundle];
+}
+
+FilePath OuterBundlePath() {
+ NSBundle* bundle = OuterBundle();
+ return FilePath([[bundle bundlePath] fileSystemRepresentation]);
+}
+
+NSBundle* FrameworkBundle() {
+ if (g_override_framework_bundle)
+ return g_override_framework_bundle;
+ return [NSBundle mainBundle];
+}
+
+FilePath FrameworkBundlePath() {
+ NSBundle* bundle = FrameworkBundle();
+ return FilePath([[bundle bundlePath] fileSystemRepresentation]);
+}
+
+static void AssignOverrideBundle(NSBundle* new_bundle,
+ NSBundle** override_bundle) {
+ if (new_bundle != *override_bundle) {
+ [*override_bundle release];
+ *override_bundle = [new_bundle retain];
+ }
+}
+
+static void AssignOverridePath(const FilePath& file_path,
+ NSBundle** override_bundle) {
+ NSString* path = base::SysUTF8ToNSString(file_path.value());
+ NSBundle* new_bundle = [NSBundle bundleWithPath:path];
+ DCHECK(new_bundle) << "Failed to load the bundle at " << file_path.value();
+ AssignOverrideBundle(new_bundle, override_bundle);
+}
+
+void SetOverrideOuterBundle(NSBundle* bundle) {
+ AssignOverrideBundle(bundle, &g_override_outer_bundle);
+}
+
+void SetOverrideFrameworkBundle(NSBundle* bundle) {
+ AssignOverrideBundle(bundle, &g_override_framework_bundle);
+}
+
+void SetOverrideOuterBundlePath(const FilePath& file_path) {
+ AssignOverridePath(file_path, &g_override_outer_bundle);
+}
+
+void SetOverrideFrameworkBundlePath(const FilePath& file_path) {
+ AssignOverridePath(file_path, &g_override_framework_bundle);
+}
+
+} // namespace mac
+} // namespace base