diff options
author | thomasvl@chromium.org <thomasvl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-03 16:26:03 +0000 |
---|---|---|
committer | thomasvl@chromium.org <thomasvl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-03 16:26:03 +0000 |
commit | 6f33add36219e1faadea9d295b35d43841ebd508 (patch) | |
tree | bc5561b8d3cedeb13e504c971a7992a34e6d2353 | |
parent | abf6c7a76bc68d0a01c1665e97972b0056654a26 (diff) | |
download | chromium_src-6f33add36219e1faadea9d295b35d43841ebd508.zip chromium_src-6f33add36219e1faadea9d295b35d43841ebd508.tar.gz chromium_src-6f33add36219e1faadea9d295b35d43841ebd508.tar.bz2 |
Add a macutil for the main app bundle and override
- provide apis to get and override the app bundle
- w/in the core code, use this api for fetching the bundle
- render sandbox config
- resource bundles
- test shell font
- w/in the unittest boot straps, use the mac util to override the bundle so resources can be found.
Review URL: http://codereview.chromium.org/28214
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10798 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/mac_util.h | 17 | ||||
-rw-r--r-- | base/mac_util.mm | 25 | ||||
-rw-r--r-- | chrome/common/resource_bundle_mac.mm | 29 | ||||
-rw-r--r-- | chrome/renderer/renderer_main_platform_delegate_mac.mm | 30 | ||||
-rw-r--r-- | chrome/test/unit/chrome_test_suite.h | 14 | ||||
-rw-r--r-- | webkit/tools/test_shell/run_all_tests.cc | 11 | ||||
-rw-r--r-- | webkit/tools/test_shell/test_shell_mac.mm | 23 |
7 files changed, 84 insertions, 65 deletions
diff --git a/base/mac_util.h b/base/mac_util.h index a6c81fa..371aa9e 100644 --- a/base/mac_util.h +++ b/base/mac_util.h @@ -6,6 +6,13 @@ #define BASE_MAC_UTIL_H_ struct FSRef; +class FilePath; + +#ifdef __OBJC__ +@class NSBundle; +#else +class NSBundle; +#endif #include <string> @@ -17,6 +24,16 @@ bool FSRefFromPath(const std::string& path, FSRef* ref); // Returns true if the application is running from a bundle bool AmIBundled(); +// Returns the main bundle or the override, used for code that needs +// to fetch resources from bundles, but work within a unittest where we +// aren't a bundle. +NSBundle* MainAppBundle(); + +// Set the bundle that MainAppBundle will return, overriding the default value +// (Restore the default by calling SetOverrideAppBundle(nil)). +void SetOverrideAppBundle(NSBundle* bundle); +void SetOverrideAppBundlePath(const FilePath& file_path); + } // namespace mac_util #endif // BASE_MAC_UTIL_H_ diff --git a/base/mac_util.mm b/base/mac_util.mm index 462d165..156cb1c 100644 --- a/base/mac_util.mm +++ b/base/mac_util.mm @@ -7,7 +7,10 @@ #include <Carbon/Carbon.h> #import <Cocoa/Cocoa.h> +#include "base/file_path.h" +#include "base/logging.h" #include "base/scoped_cftyperef.h" +#include "base/sys_string_conversions.h" namespace mac_util { @@ -41,4 +44,26 @@ bool AmIBundled() { return info.nodeFlags & kFSNodeIsDirectoryMask; } +// No threading worries since NSBundle isn't thread safe. +static NSBundle* g_override_app_bundle = nil; + +NSBundle* MainAppBundle() { + if (g_override_app_bundle) + return g_override_app_bundle; + return [NSBundle mainBundle]; +} + +void SetOverrideAppBundle(NSBundle* bundle) { + [g_override_app_bundle release]; + g_override_app_bundle = [bundle retain]; +} + +void SetOverrideAppBundlePath(const FilePath& file_path) { + NSString* path = base::SysUTF8ToNSString(file_path.value()); + NSBundle* bundle = [NSBundle bundleWithPath:path]; + DCHECK(bundle) << "failed to load the bundle: " << file_path.value(); + + SetOverrideAppBundle(bundle); +} + } // namespace mac_util diff --git a/chrome/common/resource_bundle_mac.mm b/chrome/common/resource_bundle_mac.mm index e739855..3757fc89 100644 --- a/chrome/common/resource_bundle_mac.mm +++ b/chrome/common/resource_bundle_mac.mm @@ -36,33 +36,8 @@ namespace { base::DataPack *LoadResourceDataPack(NSString *name) { base::DataPack *resource_pack = NULL; - NSString *const pakExt = @"pak"; - - // TODO(thomasvl): THIS SUCKS! We need to remove this gate. It's here - // because of the unittests, but we have no other way to find our resources. - if (!mac_util::AmIBundled()) { - FilePath path; - PathService::Get(base::DIR_EXE, &path); - path = path.AppendASCII("Chromium.app"); - path = path.AppendASCII("Contents"); - path = path.AppendASCII("Resources"); - if ([name isEqual:@"locale"]) { - path = path.AppendASCII("en.lproj"); - } - NSString *pakName = [name stringByAppendingPathExtension:pakExt]; - path = path.Append([pakName fileSystemRepresentation]); - resource_pack = new base::DataPack; - bool success = resource_pack->Load(path); - DCHECK(success) << "failed to load chrome.pak"; - if (!success) { - delete resource_pack; - resource_pack = NULL; - } - return resource_pack; - } - - NSString *resource_path = [[NSBundle mainBundle] pathForResource:name - ofType:pakExt]; + NSString *resource_path = [mac_util::MainAppBundle() pathForResource:name + ofType:@"pak"]; if (resource_path) { FilePath resources_pak_path([resource_path fileSystemRepresentation]); resource_pack = new base::DataPack; diff --git a/chrome/renderer/renderer_main_platform_delegate_mac.mm b/chrome/renderer/renderer_main_platform_delegate_mac.mm index 70ce7f4..2b3a3d5 100644 --- a/chrome/renderer/renderer_main_platform_delegate_mac.mm +++ b/chrome/renderer/renderer_main_platform_delegate_mac.mm @@ -14,6 +14,7 @@ extern "C" { } #include "base/sys_info.h" +#include "base/mac_util.h" #include "chrome/common/chrome_switches.h" #include "third_party/WebKit/WebKit/mac/WebCoreSupport/WebSystemInterface.h" @@ -98,27 +99,20 @@ bool RendererMainPlatformDelegate::EnableSandbox() { base::SysInfo::CacheSysInfo(); // For the renderer, we give it a custom sandbox to lock down as tight as - // possible, but still be able to draw. If we're not a renderer process, it - // usually means we're a unittest, so we use a pure compute sandbox instead. - - const char *sandbox_profile = kSBXProfilePureComputation; - uint64_t sandbox_flags = SANDBOX_NAMED; - - if (parameters_.sandbox_info_.ProcessType() == switches::kRendererProcess) { - NSString* sandbox_profile_path = - [[NSBundle mainBundle] pathForResource:@"renderer" ofType:@"sb"]; - BOOL is_dir = NO; - if (![[NSFileManager defaultManager] fileExistsAtPath:sandbox_profile_path - isDirectory:&is_dir] || is_dir) { - LOG(ERROR) << "Failed to find the sandbox profile on disk"; - return false; - } - sandbox_profile = [sandbox_profile_path fileSystemRepresentation]; - sandbox_flags = SANDBOX_NAMED_EXTERNAL; + // possible, but still be able to draw. + + NSString* sandbox_profile_path = + [mac_util::MainAppBundle() pathForResource:@"renderer" ofType:@"sb"]; + BOOL is_dir = NO; + if (![[NSFileManager defaultManager] fileExistsAtPath:sandbox_profile_path + isDirectory:&is_dir] || is_dir) { + LOG(ERROR) << "Failed to find the sandbox profile on disk"; + return false; } + const char *sandbox_profile = [sandbox_profile_path fileSystemRepresentation]; char* error_buff = NULL; - int error = sandbox_init(sandbox_profile, sandbox_flags, + int error = sandbox_init(sandbox_profile, SANDBOX_NAMED_EXTERNAL, &error_buff); bool success = (error == 0 && error_buff == NULL); if (error == -1) { diff --git a/chrome/test/unit/chrome_test_suite.h b/chrome/test/unit/chrome_test_suite.h index f89030c..8dcdbf6 100644 --- a/chrome/test/unit/chrome_test_suite.h +++ b/chrome/test/unit/chrome_test_suite.h @@ -9,6 +9,9 @@ #include "base/stats_table.h" #include "base/file_util.h" +#if defined(OS_MACOSX) +#include "base/mac_util.h" +#endif #include "base/path_service.h" #include "base/scoped_nsautorelease_pool.h" #include "base/test_suite.h" @@ -47,6 +50,13 @@ protected: if (!user_data_dir.empty()) PathService::Override(chrome::DIR_USER_DATA, user_data_dir); +#if defined(OS_MACOSX) + FilePath path; + PathService::Get(base::DIR_EXE, &path); + path = path.AppendASCII("Chromium.app"); + mac_util::SetOverrideAppBundlePath(path); +#endif + // Force unittests to run using en-us so if we test against string // output, it'll pass regardless of the system language. ResourceBundle::InitSharedInstance(L"en-us"); @@ -64,6 +74,10 @@ protected: // TODO(port): Remove the #ifdef when ResourceBundle is ported. ResourceBundle::CleanupSharedInstance(); +#if defined(OS_MACOSX) + mac_util::SetOverrideAppBundle(NULL); +#endif + delete g_browser_process; g_browser_process = NULL; diff --git a/webkit/tools/test_shell/run_all_tests.cc b/webkit/tools/test_shell/run_all_tests.cc index df6a1ae..5cc12a2 100644 --- a/webkit/tools/test_shell/run_all_tests.cc +++ b/webkit/tools/test_shell/run_all_tests.cc @@ -15,6 +15,10 @@ #include "base/at_exit.h" #include "base/command_line.h" #include "base/icu_util.h" +#if defined(OS_MACOSX) +#include "base/mac_util.h" +#include "base/path_service.h" +#endif #include "base/message_loop.h" #include "base/process_util.h" #include "base/scoped_nsautorelease_pool.h" @@ -42,6 +46,13 @@ int main(int argc, char* argv[]) { // the AtExitManager or else we will leak objects. base::AtExitManager at_exit_manager; +#if defined(OS_MACOSX) + FilePath path; + PathService::Get(base::DIR_EXE, &path); + path = path.AppendASCII("TestShell.app"); + mac_util::SetOverrideAppBundlePath(path); +#endif + TestShellPlatformDelegate::PreflightArgs(&argc, &argv); CommandLine::Init(argc, argv); const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); diff --git a/webkit/tools/test_shell/test_shell_mac.mm b/webkit/tools/test_shell/test_shell_mac.mm index 55cacd9..dea940a 100644 --- a/webkit/tools/test_shell/test_shell_mac.mm +++ b/webkit/tools/test_shell/test_shell_mac.mm @@ -180,26 +180,9 @@ void TestShell::InitializeTestShell(bool layout_test_mode) { // Load the Ahem font, which is used by layout tests. const char* ahem_path_c; - FilePath ahem_path; // Ensure ahem_path_c storage is not freed too soon. - if (mac_util::AmIBundled()) { - // When bundled (in TestShell.app), expect to find the font in - // Contents/Resources. - NSString* ahem_path = [[[NSBundle mainBundle] resourcePath] - stringByAppendingPathComponent:@"AHEM____.TTF"]; - ahem_path_c = [ahem_path fileSystemRepresentation]; - } else { - // When not bundled (in test_shell_tests), look in the source tree for - // the font. - PathService::Get(base::DIR_SOURCE_ROOT, &ahem_path); - ahem_path = ahem_path.Append("webkit"); - ahem_path = ahem_path.Append("tools"); - ahem_path = ahem_path.Append("test_shell"); - ahem_path = ahem_path.Append("resources"); - ahem_path = ahem_path.Append("AHEM____.TTF"); - - ahem_path_c = ahem_path.value().c_str(); - } - + NSString* ahem_path = [[mac_util::MainAppBundle() resourcePath] + stringByAppendingPathComponent:@"AHEM____.TTF"]; + ahem_path_c = [ahem_path fileSystemRepresentation]; FSRef ahem_fsref; if (!mac_util::FSRefFromPath(ahem_path_c, &ahem_fsref)) { DLOG(FATAL) << "FSRefFromPath " << ahem_path_c; |