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/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 DWORD comspec_length = ::GetEnvironmentVariable(L"COMSPEC", NULL, 0); | 121 DWORD comspec_length = ::GetEnvironmentVariable(L"COMSPEC", NULL, 0); |
122 if (comspec_length == 0) { | 122 if (comspec_length == 0) { |
123 LOG(ERROR) << "COMSPEC is not set"; | 123 LOG(ERROR) << "COMSPEC is not set"; |
124 return false; | 124 return false; |
125 } | 125 } |
126 scoped_ptr<wchar_t[]> comspec(new wchar_t[comspec_length]); | 126 scoped_ptr<wchar_t[]> comspec(new wchar_t[comspec_length]); |
127 ::GetEnvironmentVariable(L"COMSPEC", comspec.get(), comspec_length); | 127 ::GetEnvironmentVariable(L"COMSPEC", comspec.get(), comspec_length); |
128 | 128 |
129 string16 command_line_string = command_line.GetCommandLineString(); | 129 string16 command_line_string = command_line.GetCommandLineString(); |
130 | 130 |
131 // 'start' command has a moronic syntax: if first argument is quoted then it | |
132 // interprets it as a command title. Host path may need to be in quotes, so | |
133 // we always need to specify the title as the first argument. | |
134 string16 command = base::StringPrintf( | 131 string16 command = base::StringPrintf( |
135 L"%ls /c start \"Chrome Native Messaging Host\" /b " | 132 L"%ls /c %ls < %ls > %ls", |
136 L"%ls < %ls > %ls", | |
137 comspec.get(), command_line_string.c_str(), | 133 comspec.get(), command_line_string.c_str(), |
138 in_pipe_name.c_str(), out_pipe_name.c_str()); | 134 in_pipe_name.c_str(), out_pipe_name.c_str()); |
139 | 135 |
140 base::LaunchOptions options; | 136 base::LaunchOptions options; |
141 options.start_hidden = true; | 137 options.start_hidden = true; |
142 base::ProcessHandle cmd_handle; | 138 base::ProcessHandle cmd_handle; |
143 if (!base::LaunchProcess(command.c_str(), options, &cmd_handle)) { | 139 if (!base::LaunchProcess(command.c_str(), options, &cmd_handle)) { |
144 LOG(ERROR) << "Error launching process " | 140 LOG(ERROR) << "Error launching process " |
145 << command_line.GetProgram().MaybeAsASCII(); | 141 << command_line.GetProgram().MaybeAsASCII(); |
146 return false; | 142 return false; |
147 } | 143 } |
148 | 144 |
149 bool stdout_connected = ConnectNamedPipe(stdout_pipe.Get(), NULL) ? | 145 bool stdout_connected = ConnectNamedPipe(stdout_pipe.Get(), NULL) ? |
150 TRUE : GetLastError() == ERROR_PIPE_CONNECTED; | 146 TRUE : GetLastError() == ERROR_PIPE_CONNECTED; |
151 bool stdin_connected = ConnectNamedPipe(stdin_pipe.Get(), NULL) ? | 147 bool stdin_connected = ConnectNamedPipe(stdin_pipe.Get(), NULL) ? |
152 TRUE : GetLastError() == ERROR_PIPE_CONNECTED; | 148 TRUE : GetLastError() == ERROR_PIPE_CONNECTED; |
153 if (!stdout_connected || !stdin_connected) { | 149 if (!stdout_connected || !stdin_connected) { |
154 base::KillProcess(cmd_handle, 0, false); | 150 base::KillProcess(cmd_handle, 0, false); |
155 base::CloseProcessHandle(cmd_handle); | 151 base::CloseProcessHandle(cmd_handle); |
156 LOG(ERROR) << "Failed to connect IO pipes when starting " | 152 LOG(ERROR) << "Failed to connect IO pipes when starting " |
157 << command_line.GetProgram().MaybeAsASCII(); | 153 << command_line.GetProgram().MaybeAsASCII(); |
158 return false; | 154 return false; |
159 } | 155 } |
160 | 156 |
161 // Check that cmd.exe has completed with 0 exit code to make sure it was | |
162 // able to connect IO pipes. | |
163 int error_code; | |
164 if (!base::WaitForExitCodeWithTimeout( | |
165 cmd_handle, &error_code, | |
166 base::TimeDelta::FromMilliseconds(kTimeoutMs)) || | |
167 error_code != 0) { | |
168 LOG(ERROR) << "cmd.exe did not exit cleanly"; | |
169 base::KillProcess(cmd_handle, 0, false); | |
170 base::CloseProcessHandle(cmd_handle); | |
171 return false; | |
172 } | |
173 | |
174 base::CloseProcessHandle(cmd_handle); | 157 base::CloseProcessHandle(cmd_handle); |
175 | 158 |
176 *read_file = stdout_pipe.Take(); | 159 *read_file = stdout_pipe.Take(); |
177 *write_file = stdin_pipe.Take(); | 160 *write_file = stdin_pipe.Take(); |
178 | 161 |
179 return true; | 162 return true; |
180 } | 163 } |
181 | 164 |
182 } // namespace extensions | 165 } // namespace extensions |
OLD | NEW |