Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: base/process_util_win.cc

Issue 7283019: base: refactor LaunchApp variants into a single function (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/process_util_posix.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/process_util.h" 5 #include "base/process_util.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <io.h> 8 #include <io.h>
9 #include <windows.h> 9 #include <windows.h>
10 #include <userenv.h> 10 #include <userenv.h>
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 } else if (integrity_level >= SECURITY_MANDATORY_HIGH_RID) { 210 } else if (integrity_level >= SECURITY_MANDATORY_HIGH_RID) {
211 *level = HIGH_INTEGRITY; 211 *level = HIGH_INTEGRITY;
212 } else { 212 } else {
213 NOTREACHED(); 213 NOTREACHED();
214 return false; 214 return false;
215 } 215 }
216 216
217 return true; 217 return true;
218 } 218 }
219 219
220 bool LaunchAppImpl(const std::wstring& cmdline, 220 bool LaunchProcess(const string16& cmdline,
221 bool wait, bool start_hidden, bool inherit_handles, 221 const LaunchOptions& options) {
222 ProcessHandle* process_handle) { 222 STARTUPINFO startup_info = {};
223 STARTUPINFO startup_info = {0};
224 startup_info.cb = sizeof(startup_info); 223 startup_info.cb = sizeof(startup_info);
224 if (options.empty_desktop_name)
225 startup_info.lpDesktop = L"";
225 startup_info.dwFlags = STARTF_USESHOWWINDOW; 226 startup_info.dwFlags = STARTF_USESHOWWINDOW;
226 startup_info.wShowWindow = start_hidden ? SW_HIDE : SW_SHOW; 227 startup_info.wShowWindow = options.start_hidden ? SW_HIDE : SW_SHOW;
227 PROCESS_INFORMATION process_info; 228 PROCESS_INFORMATION process_info;
228 if (!CreateProcess(NULL,
229 const_cast<wchar_t*>(cmdline.c_str()), NULL, NULL,
230 inherit_handles, 0, NULL, NULL,
231 &startup_info, &process_info))
232 return false;
233 229
234 // Handles must be closed or they will leak 230 if (options.as_user) {
231 DWORD flags = CREATE_UNICODE_ENVIRONMENT;
232 void* enviroment_block = NULL;
233
234 if (!CreateEnvironmentBlock(&enviroment_block, options.as_user, FALSE))
235 return false;
236
237 BOOL launched =
238 CreateProcessAsUser(options.as_user, NULL,
239 const_cast<wchar_t*>(cmdline.c_str()),
240 NULL, NULL, options.inherit_handles, flags,
241 enviroment_block, NULL, &startup_info,
242 &process_info);
243 DestroyEnvironmentBlock(enviroment_block);
244 if (!launched)
245 return false;
246 } else {
247 if (!CreateProcess(NULL,
248 const_cast<wchar_t*>(cmdline.c_str()), NULL, NULL,
249 options.inherit_handles, 0, NULL, NULL,
250 &startup_info, &process_info)) {
251 return false;
252 }
253 }
254
255 // Handles must be closed or they will leak.
235 CloseHandle(process_info.hThread); 256 CloseHandle(process_info.hThread);
236 257
237 if (wait) 258 if (options.wait)
238 WaitForSingleObject(process_info.hProcess, INFINITE); 259 WaitForSingleObject(process_info.hProcess, INFINITE);
239 260
240 // If the caller wants the process handle, we won't close it. 261 // If the caller wants the process handle, we won't close it.
241 if (process_handle) { 262 if (options.process_handle) {
242 *process_handle = process_info.hProcess; 263 *options.process_handle = process_info.hProcess;
243 } else { 264 } else {
244 CloseHandle(process_info.hProcess); 265 CloseHandle(process_info.hProcess);
245 } 266 }
246 return true; 267 return true;
247 } 268 }
248 269
249 bool LaunchApp(const std::wstring& cmdline, 270 bool LaunchProcess(const CommandLine& cmdline,
250 bool wait, bool start_hidden, ProcessHandle* process_handle) { 271 const LaunchOptions& options) {
251 return LaunchAppImpl(cmdline, wait, start_hidden, false, process_handle); 272 return LaunchProcess(cmdline.command_line_string(), options);
252 }
253
254 bool LaunchAppWithHandleInheritance(
255 const std::wstring& cmdline, bool wait, bool start_hidden,
256 ProcessHandle* process_handle) {
257 return LaunchAppImpl(cmdline, wait, start_hidden, true, process_handle);
258 }
259
260 bool LaunchAppAsUser(UserTokenHandle token, const std::wstring& cmdline,
261 bool start_hidden, ProcessHandle* process_handle) {
262 return LaunchAppAsUser(token, cmdline, start_hidden, process_handle,
263 false, false);
264 }
265
266 bool LaunchAppAsUser(UserTokenHandle token, const std::wstring& cmdline,
267 bool start_hidden, ProcessHandle* process_handle,
268 bool empty_desktop_name, bool inherit_handles) {
269 STARTUPINFO startup_info = {0};
270 startup_info.cb = sizeof(startup_info);
271 if (empty_desktop_name)
272 startup_info.lpDesktop = L"";
273 PROCESS_INFORMATION process_info;
274 if (start_hidden) {
275 startup_info.dwFlags = STARTF_USESHOWWINDOW;
276 startup_info.wShowWindow = SW_HIDE;
277 }
278 DWORD flags = CREATE_UNICODE_ENVIRONMENT;
279 void* enviroment_block = NULL;
280
281 if (!CreateEnvironmentBlock(&enviroment_block, token, FALSE))
282 return false;
283
284 BOOL launched =
285 CreateProcessAsUser(token, NULL, const_cast<wchar_t*>(cmdline.c_str()),
286 NULL, NULL, inherit_handles, flags, enviroment_block,
287 NULL, &startup_info, &process_info);
288
289 DestroyEnvironmentBlock(enviroment_block);
290
291 if (!launched)
292 return false;
293
294 CloseHandle(process_info.hThread);
295
296 if (process_handle) {
297 *process_handle = process_info.hProcess;
298 } else {
299 CloseHandle(process_info.hProcess);
300 }
301 return true;
302 }
303
304 bool LaunchApp(const CommandLine& cl,
305 bool wait, bool start_hidden, ProcessHandle* process_handle) {
306 return LaunchAppImpl(cl.command_line_string(), wait,
307 start_hidden, false, process_handle);
308 } 273 }
309 274
310 // Attempts to kill the process identified by the given process 275 // Attempts to kill the process identified by the given process
311 // entry structure, giving it the specified exit code. 276 // entry structure, giving it the specified exit code.
312 // Returns true if this is successful, false otherwise. 277 // Returns true if this is successful, false otherwise.
313 bool KillProcessById(ProcessId process_id, int exit_code, bool wait) { 278 bool KillProcessById(ProcessId process_id, int exit_code, bool wait) {
314 HANDLE process = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, 279 HANDLE process = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE,
315 FALSE, // Don't inherit handle 280 FALSE, // Don't inherit handle
316 process_id); 281 process_id);
317 if (!process) { 282 if (!process) {
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after
899 864
900 PERFORMANCE_INFORMATION info; 865 PERFORMANCE_INFORMATION info;
901 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { 866 if (!InternalGetPerformanceInfo(&info, sizeof(info))) {
902 LOG(ERROR) << "Failed to fetch internal performance info."; 867 LOG(ERROR) << "Failed to fetch internal performance info.";
903 return 0; 868 return 0;
904 } 869 }
905 return (info.CommitTotal * system_info.dwPageSize) / 1024; 870 return (info.CommitTotal * system_info.dwPageSize) / 1024;
906 } 871 }
907 872
908 } // namespace base 873 } // namespace base
OLDNEW
« no previous file with comments | « base/process_util_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698