summaryrefslogtreecommitdiffstats
path: root/chrome/common/mac
diff options
context:
space:
mode:
authorjeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-13 11:49:17 +0000
committerjeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-13 11:49:17 +0000
commitbc6e1488b6e1ff4d27de38f9cf838fdb59c13914 (patch)
tree878d4e71c703b32d6b6875005ddefe2cc3a55b31 /chrome/common/mac
parent80f3de46bef55bb8f761b9ca10439f447031aa8b (diff)
downloadchromium_src-bc6e1488b6e1ff4d27de38f9cf838fdb59c13914.zip
chromium_src-bc6e1488b6e1ff4d27de38f9cf838fdb59c13914.tar.gz
chromium_src-bc6e1488b6e1ff4d27de38f9cf838fdb59c13914.tar.bz2
Mac app mode: locate Chrome + refactor
* Add code to locate the Chrome bundle + unit tests. * Refactor app_mode_app to use the new code, also now we can use stuff from base, remove hacks from the code. BUG=None TEST=Unit tests should pass. Review URL: http://codereview.chromium.org/9351014 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@121693 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/mac')
-rw-r--r--chrome/common/mac/app_mode_chrome_locator.h36
-rw-r--r--chrome/common/mac/app_mode_chrome_locator.mm84
-rw-r--r--chrome/common/mac/app_mode_chrome_locator_unittest.mm68
-rw-r--r--chrome/common/mac/app_mode_common.h19
-rw-r--r--chrome/common/mac/app_mode_common.mm10
5 files changed, 211 insertions, 6 deletions
diff --git a/chrome/common/mac/app_mode_chrome_locator.h b/chrome/common/mac/app_mode_chrome_locator.h
new file mode 100644
index 0000000..81eb93a
--- /dev/null
+++ b/chrome/common/mac/app_mode_chrome_locator.h
@@ -0,0 +1,36 @@
+// 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 CHROME_COMMON_MAC_APP_MODE_CHROME_LOCATOR_H_
+#define CHROME_COMMON_MAC_APP_MODE_CHROME_LOCATOR_H_
+#pragma once
+
+#include <CoreFoundation/CoreFoundation.h>
+
+#include "base/string16.h"
+
+@class NSString;
+
+class FilePath;
+
+namespace app_mode {
+
+// Given a bundle id, return the path of the corresponding bundle.
+// Returns true if the bundle was found, false otherwise.
+bool FindBundleById(NSString* bundle_id, FilePath* out_bundle);
+
+// Given the path to the Chrome bundle, read the following information:
+// |raw_version_str| - Chrome version.
+// |version_path| - |chrome_bundle|/Contents/Versions/|raw_version_str|/
+// |framework_shlib_path| - Path to the chrome framework's shared library (not
+// the framework directory).
+// Returns true if all information read succesfuly, false otherwise.
+bool GetChromeBundleInfo(const FilePath& chrome_bundle,
+ string16* raw_version_str,
+ FilePath* version_path,
+ FilePath* framework_shlib_path);
+
+} // namespace app_mode
+
+#endif // CHROME_COMMON_MAC_APP_MODE_CHROME_LOCATOR_H_
diff --git a/chrome/common/mac/app_mode_chrome_locator.mm b/chrome/common/mac/app_mode_chrome_locator.mm
new file mode 100644
index 0000000..b64778f
--- /dev/null
+++ b/chrome/common/mac/app_mode_chrome_locator.mm
@@ -0,0 +1,84 @@
+// 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.
+
+#import "chrome/common/mac/app_mode_chrome_locator.h"
+
+#import <AppKit/AppKit.h>
+#include <CoreFoundation/CoreFoundation.h>
+
+#include "base/file_path.h"
+#include "base/mac/foundation_util.h"
+#include "base/sys_string_conversions.h"
+
+namespace app_mode {
+
+bool FindBundleById(NSString* bundle_id, FilePath* out_bundle) {
+ NSWorkspace* ws = [NSWorkspace sharedWorkspace];
+ NSString *bundlePath = [ws absolutePathForAppBundleWithIdentifier:bundle_id];
+ if (!bundlePath)
+ return false;
+
+ *out_bundle = base::mac::NSStringToFilePath(bundlePath);
+ return true;
+}
+
+bool GetChromeBundleInfo(const FilePath& chrome_bundle,
+ string16* raw_version_str,
+ FilePath* version_path,
+ FilePath* framework_shlib_path) {
+ using base::mac::ObjCCast;
+
+ NSString* cr_bundle_path = base::mac::FilePathToNSString(chrome_bundle);
+ NSBundle* cr_bundle = [NSBundle bundleWithPath:cr_bundle_path];
+
+ if (!cr_bundle)
+ return false;
+
+ // Read raw version string.
+ NSString* cr_version =
+ ObjCCast<NSString>(
+ [cr_bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]);
+ if (!cr_version)
+ return false;
+
+ // Get versioned directory.
+ NSArray* cr_versioned_path_components =
+ [NSArray arrayWithObjects:cr_bundle_path,
+ @"Contents",
+ @"Versions",
+ cr_version,
+ nil];
+ NSString* cr_versioned_path =
+ [NSString pathWithComponents:cr_versioned_path_components];
+
+ // Get the framework path.
+ NSString* cr_bundle_exe =
+ ObjCCast<NSString>(
+ [cr_bundle objectForInfoDictionaryKey:@"CFBundleExecutable"]);
+ NSString* cr_framework_shlib_path =
+ [cr_versioned_path stringByAppendingPathComponent:
+ [cr_bundle_exe stringByAppendingString:@" Framework.framework"]];
+ cr_framework_shlib_path =
+ [cr_framework_shlib_path stringByAppendingPathComponent:
+ [cr_bundle_exe stringByAppendingString:@" Framework"]];
+ if (!cr_bundle_exe || !cr_framework_shlib_path)
+ return false;
+
+ // A few more sanity checks.
+ BOOL is_directory;
+ BOOL exists = [[NSFileManager defaultManager]
+ fileExistsAtPath:cr_framework_shlib_path
+ isDirectory:&is_directory];
+ if (!exists || is_directory)
+ return false;
+
+ // Everything OK, copy output parameters.
+ *raw_version_str = base::SysNSStringToUTF16(cr_version);
+ *version_path = base::mac::NSStringToFilePath(cr_versioned_path);
+ *framework_shlib_path =
+ base::mac::NSStringToFilePath(cr_framework_shlib_path);
+ return true;
+}
+
+} // namespace app_mode
diff --git a/chrome/common/mac/app_mode_chrome_locator_unittest.mm b/chrome/common/mac/app_mode_chrome_locator_unittest.mm
new file mode 100644
index 0000000..b54fee75
--- /dev/null
+++ b/chrome/common/mac/app_mode_chrome_locator_unittest.mm
@@ -0,0 +1,68 @@
+// 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.
+
+#import "chrome/common/mac/app_mode_chrome_locator.h"
+
+#include <CoreFoundation/CoreFoundation.h>
+
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/path_service.h"
+#include "base/scoped_temp_dir.h"
+#include "chrome/common/chrome_constants.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+// Return the path to the Chrome/Chromium app bundle compiled along with the
+// test executable.
+void GetChromeBundlePath(FilePath* chrome_bundle) {
+ FilePath path;
+ PathService::Get(base::DIR_EXE, &path);
+ path = path.Append(chrome::kBrowserProcessExecutableNameChromium);
+ path = path.ReplaceExtension(FilePath::StringType("app"));
+ *chrome_bundle = path;
+}
+
+} // namespace
+
+TEST(ChromeLocatorTest, FindBundle) {
+ FilePath finder_bundle_path;
+ EXPECT_TRUE(
+ app_mode::FindBundleById(@"com.apple.finder", &finder_bundle_path));
+ EXPECT_TRUE(file_util::DirectoryExists(finder_bundle_path));
+}
+
+TEST(ChromeLocatorTest, FindNonExistentBundle) {
+ FilePath dummy;
+ EXPECT_FALSE(app_mode::FindBundleById(@"this.doesnt.exist", &dummy));
+}
+
+TEST(ChromeLocatorTest, GetNonExistentBundleInfo) {
+ ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+
+ string16 raw_version;
+ FilePath version_path;
+ FilePath framework_path;
+ EXPECT_FALSE(app_mode::GetChromeBundleInfo(temp_dir.path(),
+ &raw_version, &version_path, &framework_path));
+}
+
+TEST(ChromeLocatorTest, GetChromeBundleInfo) {
+ using app_mode::GetChromeBundleInfo;
+
+ FilePath chrome_bundle_path;
+ GetChromeBundlePath(&chrome_bundle_path);
+ ASSERT_TRUE(file_util::DirectoryExists(chrome_bundle_path));
+
+ string16 raw_version;
+ FilePath version_path;
+ FilePath framework_path;
+ EXPECT_TRUE(GetChromeBundleInfo(chrome_bundle_path,
+ &raw_version, &version_path, &framework_path));
+ EXPECT_GT(raw_version.size(), 0U);
+ EXPECT_TRUE(file_util::DirectoryExists(version_path));
+ EXPECT_TRUE(file_util::PathExists(framework_path));
+}
diff --git a/chrome/common/mac/app_mode_common.h b/chrome/common/mac/app_mode_common.h
index bf43f32..5267cd4 100644
--- a/chrome/common/mac/app_mode_common.h
+++ b/chrome/common/mac/app_mode_common.h
@@ -8,6 +8,9 @@
#import <Foundation/Foundation.h>
+#include "base/file_path.h"
+#include "base/string16.h"
+
// This file contains constants, interfaces, etc. which are common to the
// browser application and the app mode loader (a.k.a. shim).
@@ -44,6 +47,10 @@ const unsigned kCurrentChromeAppModeInfoMinorVersion = 0;
// version number. It may refuse to load if the major version of the structure
// is different from the one it accepts.
struct ChromeAppModeInfo {
+ public:
+ ChromeAppModeInfo();
+ ~ChromeAppModeInfo();
+
// Major and minor version number of this structure.
unsigned major_version; // Required: all versions
unsigned minor_version; // Required: all versions
@@ -53,25 +60,25 @@ struct ChromeAppModeInfo {
char** argv; // Required: v1.0
// Versioned path to the browser which is being loaded.
- char* chrome_versioned_path; // Required: v1.0
+ FilePath chrome_versioned_path; // Required: v1.0
// Information about the App Mode shortcut:
// Path to the App Mode Loader application bundle originally run.
- char* app_mode_bundle_path; // Optional: v1.0
+ FilePath app_mode_bundle_path; // Optional: v1.0
// Short ID string, preferably derived from |app_mode_short_name|. Should be
// safe for the file system.
- char* app_mode_id; // Required: v1.0
+ std::string app_mode_id; // Required: v1.0
// Short (e.g., one-word) UTF8-encoded name for the shortcut.
- char* app_mode_short_name; // Optional: v1.0
+ string16 app_mode_short_name; // Optional: v1.0
// Unrestricted (e.g., several-word) UTF8-encoded name for the shortcut.
- char* app_mode_name; // Optional: v1.0
+ string16 app_mode_name; // Optional: v1.0
// URL for the shortcut. Must be a valid URL.
- char* app_mode_url; // Required: v1.0
+ std::string app_mode_url; // Required: v1.0
};
} // namespace app_mode
diff --git a/chrome/common/mac/app_mode_common.mm b/chrome/common/mac/app_mode_common.mm
index b3c3cb2..e006965 100644
--- a/chrome/common/mac/app_mode_common.mm
+++ b/chrome/common/mac/app_mode_common.mm
@@ -13,4 +13,14 @@ NSString* const kCrAppModeShortcutShortNameKey = @"CrAppModeShortcutShortName";
NSString* const kCrAppModeShortcutNameKey = @"CrAppModeShortcutName";
NSString* const kCrAppModeShortcutURLKey = @"CrAppModeShortcutURL";
+ChromeAppModeInfo::ChromeAppModeInfo()
+ : major_version(0),
+ minor_version(0),
+ argc(0),
+ argv(0) {
+}
+
+ChromeAppModeInfo::~ChromeAppModeInfo() {
+}
+
} // namespace app_mode