| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 5 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/command_line.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/process_util.h" | 11 #include "base/process_util.h" |
| 11 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 12 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 14 #include "base/utf_string_conversions.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 int pipe_counter = 0; |
| 19 |
| 20 } // namespace |
| 13 | 21 |
| 14 namespace extensions { | 22 namespace extensions { |
| 15 | 23 |
| 16 bool NativeProcessLauncher::LaunchNativeProcess( | 24 bool NativeProcessLauncher::LaunchNativeProcess( |
| 17 const FilePath& path, | 25 const FilePath& path, |
| 18 base::ProcessHandle* native_process_handle, | 26 base::ProcessHandle* native_process_handle, |
| 19 NativeMessageProcessHost::FileHandle* read_file, | 27 NativeMessageProcessHost::FileHandle* read_file, |
| 20 NativeMessageProcessHost::FileHandle* write_file) const { | 28 NativeMessageProcessHost::FileHandle* write_file) const { |
| 21 NOTREACHED(); | 29 SECURITY_ATTRIBUTES sa_attr; |
| 22 return false; | 30 // Set the bInheritHandle flag so pipe handles are inherited. |
| 31 sa_attr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 32 sa_attr.bInheritHandle = TRUE; |
| 33 sa_attr.lpSecurityDescriptor = NULL; |
| 34 |
| 35 DWORD pipe_open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED; |
| 36 DWORD pipe_mode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE; |
| 37 DWORD max_instances = 1; |
| 38 DWORD buffer_size = 10 * 1024 * 1024; |
| 39 // TODO(eriq): How long to make the timeout? |
| 40 DWORD timeout = 5000; |
| 41 |
| 42 string16 out_pipe_name = ASCIIToWide(base::StringPrintf( |
| 43 "\\\\.\\pipe\\chrome.nativeMessage.out.%d", ++pipe_counter)); |
| 44 string16 in_pipe_name = ASCIIToWide(base::StringPrintf( |
| 45 "\\\\.\\pipe\\chrome.nativeMessage.in.%d", ++pipe_counter)); |
| 46 |
| 47 HANDLE stdin_parent_pipe = NULL; |
| 48 HANDLE stdout_parent_pipe = NULL; |
| 49 |
| 50 HANDLE stdin_child_pipe = NULL; |
| 51 HANDLE stdout_child_pipe = NULL; |
| 52 |
| 53 // Create the pipes to read and write from. |
| 54 stdout_parent_pipe = CreateNamedPipeW(out_pipe_name.c_str(), pipe_open_mode, |
| 55 pipe_mode, max_instances, buffer_size, |
| 56 buffer_size, timeout, &sa_attr); |
| 57 if (stdout_parent_pipe == INVALID_HANDLE_VALUE) { |
| 58 NOTREACHED() << "Failed to create pipe"; |
| 59 return false; |
| 60 } |
| 61 |
| 62 DWORD desired_access = GENERIC_READ | GENERIC_WRITE; |
| 63 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE; |
| 64 |
| 65 stdout_child_pipe = CreateFileW(out_pipe_name.c_str(), desired_access, |
| 66 share_mode, &sa_attr, OPEN_EXISTING, |
| 67 FILE_FLAG_OVERLAPPED, NULL); |
| 68 if (stdout_child_pipe == INVALID_HANDLE_VALUE) { |
| 69 NOTREACHED() << "Failed to create pipe"; |
| 70 return false; |
| 71 } |
| 72 |
| 73 stdin_parent_pipe = CreateNamedPipeW(in_pipe_name.c_str(), pipe_open_mode, |
| 74 pipe_mode, max_instances, buffer_size, |
| 75 buffer_size, timeout, &sa_attr); |
| 76 if (stdin_parent_pipe == INVALID_HANDLE_VALUE) { |
| 77 NOTREACHED() << "Failed to create pipe"; |
| 78 return false; |
| 79 } |
| 80 |
| 81 stdin_child_pipe = CreateFileW(in_pipe_name.c_str(), desired_access, |
| 82 share_mode, &sa_attr, OPEN_EXISTING, |
| 83 FILE_FLAG_OVERLAPPED, NULL); |
| 84 if (stdin_child_pipe == INVALID_HANDLE_VALUE) { |
| 85 NOTREACHED() << "Failed to create pipe"; |
| 86 return false; |
| 87 } |
| 88 |
| 89 // Don't inherit the parents' |
| 90 if (!SetHandleInformation(stdin_parent_pipe, HANDLE_FLAG_INHERIT, 0) || |
| 91 !SetHandleInformation(stdout_parent_pipe, HANDLE_FLAG_INHERIT, 0)) { |
| 92 NOTREACHED() << "Failed to enable pipe inheritance"; |
| 93 return false; |
| 94 } |
| 95 |
| 96 CommandLine line(path); |
| 97 base::LaunchOptions options; |
| 98 // Need to inherit the file handles. |
| 99 options.inherit_handles = true; |
| 100 options.stdin_handle = stdin_child_pipe; |
| 101 options.stdout_handle = stdout_child_pipe; |
| 102 options.start_hidden = true; |
| 103 |
| 104 if (!base::LaunchProcess(line.GetCommandLineString(), options, |
| 105 native_process_handle)) { |
| 106 LOG(ERROR) << "Error launching process"; |
| 107 return false; |
| 108 } |
| 109 |
| 110 *read_file = stdout_parent_pipe; |
| 111 *write_file = stdin_parent_pipe; |
| 112 |
| 113 return true; |
| 23 } | 114 } |
| 24 | 115 |
| 25 } // namespace extensions | 116 } // namespace extensions |
| OLD | NEW |