summaryrefslogtreecommitdiffstats
path: root/base/mac
diff options
context:
space:
mode:
authorjeremya@chromium.org <jeremya@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-30 05:00:13 +0000
committerjeremya@chromium.org <jeremya@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-30 05:00:13 +0000
commit295efba6b8ff371ac9a07b9be369c19a0c6e35d1 (patch)
tree0462e9c3ebb76db3feea2228296bc5a9902bcda1 /base/mac
parent84122d8e56d7493e4a8ec407b2f30c64d4b47d1a (diff)
downloadchromium_src-295efba6b8ff371ac9a07b9be369c19a0c6e35d1.zip
chromium_src-295efba6b8ff371ac9a07b9be369c19a0c6e35d1.tar.gz
chromium_src-295efba6b8ff371ac9a07b9be369c19a0c6e35d1.tar.bz2
Add base::mac::OpenApplicationWithPath()
Uses LSOpenApplication to launch the given application bundle. R=mark@chromium.org TBR=cpu@chromium.org Review URL: https://chromiumcodereview.appspot.com/13835013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@197270 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/mac')
-rw-r--r--base/mac/launch_services_util.cc67
-rw-r--r--base/mac/launch_services_util.h29
2 files changed, 96 insertions, 0 deletions
diff --git a/base/mac/launch_services_util.cc b/base/mac/launch_services_util.cc
new file mode 100644
index 0000000..c24d864
--- /dev/null
+++ b/base/mac/launch_services_util.cc
@@ -0,0 +1,67 @@
+// Copyright 2013 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/launch_services_util.h"
+
+#include <ApplicationServices/ApplicationServices.h>
+
+#include "base/logging.h"
+#include "base/mac/mac_logging.h"
+#include "base/mac/mac_util.h"
+#include "base/strings/sys_string_conversions.h"
+
+namespace base {
+namespace mac {
+
+bool OpenApplicationWithPath(const base::FilePath& bundle_path,
+ const CommandLine& command_line,
+ ProcessSerialNumber* out_psn) {
+ FSRef app_fsref;
+ if (!base::mac::FSRefFromPath(bundle_path.value(), &app_fsref)) {
+ LOG(ERROR) << "base::mac::FSRefFromPath failed for " << bundle_path.value();
+ return false;
+ }
+
+ std::vector<std::string> argv = command_line.argv();
+ int argc = argv.size();
+ base::mac::ScopedCFTypeRef<CFMutableArrayRef> launch_args(
+ CFArrayCreateMutable(NULL, argc - 1, &kCFTypeArrayCallBacks));
+ if (!launch_args) {
+ LOG(ERROR) << "CFArrayCreateMutable failed, size was " << argc;
+ return false;
+ }
+
+ for (int i = 1; i < argc; ++i) {
+ const std::string& arg(argv[i]);
+
+ base::mac::ScopedCFTypeRef<CFStringRef> arg_cf(
+ base::SysUTF8ToCFStringRef(arg));
+ if (!arg_cf) {
+ LOG(ERROR) << "base::SysUTF8ToCFStringRef failed for " << arg;
+ return false;
+ }
+ CFArrayAppendValue(launch_args, arg_cf);
+ }
+
+ LSApplicationParameters ls_parameters = {
+ 0, // version
+ kLSLaunchDefaults,
+ &app_fsref,
+ NULL, // asyncLaunchRefCon
+ NULL, // environment
+ launch_args,
+ NULL // initialEvent
+ };
+ // TODO(jeremya): this opens a new browser window if Chrome is already
+ // running without any windows open.
+ OSStatus status = LSOpenApplication(&ls_parameters, out_psn);
+ if (status != noErr) {
+ OSSTATUS_LOG(ERROR, status) << "LSOpenApplication";
+ return false;
+ }
+ return true;
+}
+
+} // namespace mac
+} // namespace base
diff --git a/base/mac/launch_services_util.h b/base/mac/launch_services_util.h
new file mode 100644
index 0000000..cd17950
--- /dev/null
+++ b/base/mac/launch_services_util.h
@@ -0,0 +1,29 @@
+// Copyright (c) 2013 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_LAUNCH_SERVICES_UTIL_H_
+#define BASE_MAC_LAUNCH_SERVICES_UTIL_H_
+
+#include "base/base_export.h"
+#include "base/command_line.h"
+#include "base/files/file_path.h"
+
+struct ProcessSerialNumber;
+
+namespace base {
+namespace mac {
+
+// Launches the application bundle at |bundle_path|, passing argv[1..] from
+// |command_line| as command line arguments if the app isn't already running.
+// |out_psn|, if not NULL, will be set to the process serial number of the
+// application's main process if the app was successfully launched.
+// Returns true if the app was successfully launched.
+BASE_EXPORT bool OpenApplicationWithPath(const base::FilePath& bundle_path,
+ const CommandLine& command_line,
+ ProcessSerialNumber* out_psn);
+
+} // namespace mac
+} // namespace base
+
+#endif // BASE_MAC_LAUNCH_SERVICES_UTIL_H_