[email protected] | 295efba6 | 2013-04-30 05:00:13 | [diff] [blame] | 1 | // Copyright 2013 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/launch_services_util.h" |
| 6 | |
[email protected] | 295efba6 | 2013-04-30 05:00:13 | [diff] [blame] | 7 | #include "base/logging.h" |
| 8 | #include "base/mac/mac_logging.h" |
| 9 | #include "base/mac/mac_util.h" |
avi | 69fed02 | 2014-12-21 01:02:52 | [diff] [blame] | 10 | #include "base/mac/scoped_cftyperef.h" |
[email protected] | 295efba6 | 2013-04-30 05:00:13 | [diff] [blame] | 11 | #include "base/strings/sys_string_conversions.h" |
| 12 | |
| 13 | namespace base { |
| 14 | namespace mac { |
| 15 | |
| 16 | bool OpenApplicationWithPath(const base::FilePath& bundle_path, |
| 17 | const CommandLine& command_line, |
[email protected] | 2fd2437 | 2013-07-24 11:05:33 | [diff] [blame] | 18 | LSLaunchFlags launch_flags, |
[email protected] | 295efba6 | 2013-04-30 05:00:13 | [diff] [blame] | 19 | ProcessSerialNumber* out_psn) { |
| 20 | FSRef app_fsref; |
| 21 | if (!base::mac::FSRefFromPath(bundle_path.value(), &app_fsref)) { |
| 22 | LOG(ERROR) << "base::mac::FSRefFromPath failed for " << bundle_path.value(); |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | std::vector<std::string> argv = command_line.argv(); |
| 27 | int argc = argv.size(); |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 28 | base::ScopedCFTypeRef<CFMutableArrayRef> launch_args( |
[email protected] | 295efba6 | 2013-04-30 05:00:13 | [diff] [blame] | 29 | CFArrayCreateMutable(NULL, argc - 1, &kCFTypeArrayCallBacks)); |
| 30 | if (!launch_args) { |
| 31 | LOG(ERROR) << "CFArrayCreateMutable failed, size was " << argc; |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | for (int i = 1; i < argc; ++i) { |
| 36 | const std::string& arg(argv[i]); |
| 37 | |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 38 | base::ScopedCFTypeRef<CFStringRef> arg_cf(base::SysUTF8ToCFStringRef(arg)); |
[email protected] | 295efba6 | 2013-04-30 05:00:13 | [diff] [blame] | 39 | if (!arg_cf) { |
| 40 | LOG(ERROR) << "base::SysUTF8ToCFStringRef failed for " << arg; |
| 41 | return false; |
| 42 | } |
| 43 | CFArrayAppendValue(launch_args, arg_cf); |
| 44 | } |
| 45 | |
| 46 | LSApplicationParameters ls_parameters = { |
| 47 | 0, // version |
[email protected] | 2fd2437 | 2013-07-24 11:05:33 | [diff] [blame] | 48 | launch_flags, |
[email protected] | 295efba6 | 2013-04-30 05:00:13 | [diff] [blame] | 49 | &app_fsref, |
| 50 | NULL, // asyncLaunchRefCon |
| 51 | NULL, // environment |
| 52 | launch_args, |
| 53 | NULL // initialEvent |
| 54 | }; |
| 55 | // TODO(jeremya): this opens a new browser window if Chrome is already |
| 56 | // running without any windows open. |
| 57 | OSStatus status = LSOpenApplication(&ls_parameters, out_psn); |
| 58 | if (status != noErr) { |
| 59 | OSSTATUS_LOG(ERROR, status) << "LSOpenApplication"; |
| 60 | return false; |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | } // namespace mac |
| 66 | } // namespace base |