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