OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/eintr_wrapper.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/process_util.h" |
| 12 |
| 13 namespace extensions { |
| 14 |
| 15 bool NativeProcessLauncher::LaunchNativeProcess( |
| 16 const FilePath& path, |
| 17 base::ProcessHandle* native_process_handle, |
| 18 NativeMessageProcessHost::FileHandle* read_file, |
| 19 NativeMessageProcessHost::FileHandle* write_file) const { |
| 20 base::FileHandleMappingVector fd_map; |
| 21 |
| 22 int read_pipe_fds[2] = {0}; |
| 23 if (HANDLE_EINTR(pipe(read_pipe_fds)) != 0) { |
| 24 LOG(ERROR) << "Bad read pipe"; |
| 25 return false; |
| 26 } |
| 27 file_util::ScopedFD read_pipe_read_fd(&read_pipe_fds[0]); |
| 28 file_util::ScopedFD read_pipe_write_fd(&read_pipe_fds[1]); |
| 29 fd_map.push_back(std::make_pair(*read_pipe_write_fd, STDOUT_FILENO)); |
| 30 |
| 31 int write_pipe_fds[2] = {0}; |
| 32 if (HANDLE_EINTR(pipe(write_pipe_fds)) != 0) { |
| 33 LOG(ERROR) << "Bad write pipe"; |
| 34 return false; |
| 35 } |
| 36 file_util::ScopedFD write_pipe_read_fd(&write_pipe_fds[0]); |
| 37 file_util::ScopedFD write_pipe_write_fd(&write_pipe_fds[1]); |
| 38 fd_map.push_back(std::make_pair(*write_pipe_read_fd, STDIN_FILENO)); |
| 39 |
| 40 CommandLine line(path); |
| 41 base::LaunchOptions options; |
| 42 options.fds_to_remap = &fd_map; |
| 43 if (!base::LaunchProcess(line, options, native_process_handle)) { |
| 44 LOG(ERROR) << "Error launching process"; |
| 45 return false; |
| 46 } |
| 47 |
| 48 // We will not be reading from the write pipe, nor writing from the read pipe. |
| 49 write_pipe_read_fd.reset(); |
| 50 read_pipe_write_fd.reset(); |
| 51 |
| 52 *read_file = *read_pipe_read_fd.release(); |
| 53 *write_file = *write_pipe_write_fd.release(); |
| 54 |
| 55 return true; |
| 56 } |
| 57 |
| 58 } // namespace extensions |
OLD | NEW |