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 = NativeMessageProcessHost::kMaxMessageDataLength; |
| 39 DWORD timeout = NativeMessageProcessHost::kExitTimeoutMS; |
| 40 |
| 41 string16 out_pipe_name = ASCIIToWide(base::StringPrintf( |
| 42 "\\\\.\\pipe\\chrome.nativeMessage.out.%d", ++pipe_counter)); |
| 43 string16 in_pipe_name = ASCIIToWide(base::StringPrintf( |
| 44 "\\\\.\\pipe\\chrome.nativeMessage.in.%d", ++pipe_counter)); |
| 45 |
| 46 HANDLE stdin_parent_pipe = NULL; |
| 47 HANDLE stdout_parent_pipe = NULL; |
| 48 |
| 49 HANDLE stdin_child_pipe = NULL; |
| 50 HANDLE stdout_child_pipe = NULL; |
| 51 |
| 52 // Create the pipes to read and write from. |
| 53 stdout_parent_pipe = CreateNamedPipeW(out_pipe_name.c_str(), pipe_open_mode, |
| 54 pipe_mode, max_instances, buffer_size, |
| 55 buffer_size, timeout, &sa_attr); |
| 56 if (stdout_parent_pipe == INVALID_HANDLE_VALUE) { |
| 57 DPLOG(ERROR) << "Failed to create pipe"; |
| 58 return false; |
| 59 } |
| 60 |
| 61 DWORD desired_access = GENERIC_READ | GENERIC_WRITE; |
| 62 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE; |
| 63 |
| 64 stdout_child_pipe = CreateFileW(out_pipe_name.c_str(), desired_access, |
| 65 share_mode, &sa_attr, OPEN_EXISTING, |
| 66 FILE_FLAG_OVERLAPPED, NULL); |
| 67 if (stdout_child_pipe == INVALID_HANDLE_VALUE) { |
| 68 DPLOG(ERROR) << "Failed to create pipe"; |
| 69 return false; |
| 70 } |
| 71 |
| 72 stdin_parent_pipe = CreateNamedPipeW(in_pipe_name.c_str(), pipe_open_mode, |
| 73 pipe_mode, max_instances, buffer_size, |
| 74 buffer_size, timeout, &sa_attr); |
| 75 if (stdin_parent_pipe == INVALID_HANDLE_VALUE) { |
| 76 DPLOG(ERROR) << "Failed to create pipe"; |
| 77 return false; |
| 78 } |
| 79 |
| 80 stdin_child_pipe = CreateFileW(in_pipe_name.c_str(), desired_access, |
| 81 share_mode, &sa_attr, OPEN_EXISTING, |
| 82 FILE_FLAG_OVERLAPPED, NULL); |
| 83 if (stdin_child_pipe == INVALID_HANDLE_VALUE) { |
| 84 DPLOG(ERROR) << "Failed to create pipe"; |
| 85 return false; |
| 86 } |
| 87 |
| 88 // Don't inherit the parents' |
| 89 if (!SetHandleInformation(stdin_parent_pipe, HANDLE_FLAG_INHERIT, 0) || |
| 90 !SetHandleInformation(stdout_parent_pipe, HANDLE_FLAG_INHERIT, 0)) { |
| 91 DPLOG(ERROR) << "Failed to enable pipe inheritance"; |
| 92 return false; |
| 93 } |
| 94 |
| 95 CommandLine line(path); |
| 96 base::LaunchOptions options; |
| 97 // Need to inherit the file handles. |
| 98 options.inherit_handles = true; |
| 99 options.stdin_handle = stdin_child_pipe; |
| 100 options.stdout_handle = stdout_child_pipe; |
| 101 options.start_hidden = true; |
| 102 |
| 103 if (!base::LaunchProcess(line.GetCommandLineString(), options, |
| 104 native_process_handle)) { |
| 105 LOG(ERROR) << "Error launching process"; |
| 106 return false; |
| 107 } |
| 108 |
| 109 *read_file = stdout_parent_pipe; |
| 110 *write_file = stdin_parent_pipe; |
| 111 |
| 112 return true; |
23 } | 113 } |
24 | 114 |
25 } // namespace extensions | 115 } // namespace extensions |
OLD | NEW |