Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <psapi.h> | 11 #include <psapi.h> |
| 11 | 12 |
| 12 #include <ios> | 13 #include <ios> |
| 13 | 14 |
| 14 #include "base/debug_util.h" | 15 #include "base/debug_util.h" |
| 15 #include "base/histogram.h" | 16 #include "base/histogram.h" |
| 16 #include "base/logging.h" | 17 #include "base/logging.h" |
| 17 #include "base/scoped_handle_win.h" | 18 #include "base/scoped_handle_win.h" |
| 18 #include "base/scoped_ptr.h" | 19 #include "base/scoped_ptr.h" |
| 19 | 20 |
| 21 // userenv.dll is required for CreateEnvironmentBlock(). | |
| 22 #pragma comment(lib, "userenv.lib") | |
| 23 | |
| 20 namespace base { | 24 namespace base { |
| 21 | 25 |
| 22 namespace { | 26 namespace { |
| 23 | 27 |
| 24 // System pagesize. This value remains constant on x86/64 architectures. | 28 // System pagesize. This value remains constant on x86/64 architectures. |
| 25 const int PAGESIZE_KB = 4; | 29 const int PAGESIZE_KB = 4; |
| 26 | 30 |
| 27 // HeapSetInformation function pointer. | 31 // HeapSetInformation function pointer. |
| 28 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); | 32 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); |
| 29 | 33 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 | 159 |
| 156 // If the caller wants the process handle, we won't close it. | 160 // If the caller wants the process handle, we won't close it. |
| 157 if (process_handle) { | 161 if (process_handle) { |
| 158 *process_handle = process_info.hProcess; | 162 *process_handle = process_info.hProcess; |
| 159 } else { | 163 } else { |
| 160 CloseHandle(process_info.hProcess); | 164 CloseHandle(process_info.hProcess); |
| 161 } | 165 } |
| 162 return true; | 166 return true; |
| 163 } | 167 } |
| 164 | 168 |
| 169 | |
| 170 bool LaunchAppAsUser(UserTokenHandle token, const std::wstring& cmdline, | |
| 171 bool start_hidden, ProcessHandle* process_handle) { | |
|
ananta
2010/02/01 00:52:29
Would it make sense to take in the desired STARTUP
| |
| 172 STARTUPINFO startup_info = {0}; | |
| 173 startup_info.cb = sizeof(startup_info); | |
| 174 PROCESS_INFORMATION process_info; | |
| 175 if (start_hidden) { | |
| 176 startup_info.dwFlags = STARTF_USESHOWWINDOW; | |
| 177 startup_info.wShowWindow = SW_HIDE; | |
| 178 } | |
| 179 DWORD flags = CREATE_UNICODE_ENVIRONMENT; | |
| 180 void* enviroment_block = NULL; | |
| 181 | |
| 182 if(!CreateEnvironmentBlock(&enviroment_block, token, FALSE)) | |
| 183 return false; | |
| 184 | |
| 185 BOOL launched = | |
| 186 CreateProcessAsUser(token, NULL, const_cast<wchar_t*>(cmdline.c_str()), | |
| 187 NULL, NULL, FALSE, flags, enviroment_block, | |
| 188 NULL, &startup_info, &process_info); | |
| 189 | |
| 190 DestroyEnvironmentBlock(enviroment_block); | |
| 191 | |
| 192 if (!launched) | |
| 193 return false; | |
| 194 | |
| 195 CloseHandle(process_info.hThread); | |
| 196 | |
| 197 if (process_handle) { | |
| 198 *process_handle = process_info.hProcess; | |
| 199 } else { | |
| 200 CloseHandle(process_info.hProcess); | |
| 201 } | |
| 202 return true; | |
| 203 } | |
| 204 | |
| 165 bool LaunchApp(const CommandLine& cl, | 205 bool LaunchApp(const CommandLine& cl, |
| 166 bool wait, bool start_hidden, ProcessHandle* process_handle) { | 206 bool wait, bool start_hidden, ProcessHandle* process_handle) { |
| 167 return LaunchApp(cl.command_line_string(), wait, | 207 return LaunchApp(cl.command_line_string(), wait, |
| 168 start_hidden, process_handle); | 208 start_hidden, process_handle); |
| 169 } | 209 } |
| 170 | 210 |
| 171 // Attempts to kill the process identified by the given process | 211 // Attempts to kill the process identified by the given process |
| 172 // entry structure, giving it the specified exit code. | 212 // entry structure, giving it the specified exit code. |
| 173 // Returns true if this is successful, false otherwise. | 213 // Returns true if this is successful, false otherwise. |
| 174 bool KillProcessById(ProcessId process_id, int exit_code, bool wait) { | 214 bool KillProcessById(ProcessId process_id, int exit_code, bool wait) { |
| (...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 766 | 806 |
| 767 PERFORMANCE_INFORMATION info; | 807 PERFORMANCE_INFORMATION info; |
| 768 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { | 808 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { |
| 769 LOG(ERROR) << "Failed to fetch internal performance info."; | 809 LOG(ERROR) << "Failed to fetch internal performance info."; |
| 770 return 0; | 810 return 0; |
| 771 } | 811 } |
| 772 return (info.CommitTotal * system_info.dwPageSize) / 1024; | 812 return (info.CommitTotal * system_info.dwPageSize) / 1024; |
| 773 } | 813 } |
| 774 | 814 |
| 775 } // namespace base | 815 } // namespace base |
| OLD | NEW |