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

Side by Side Diff: base/process_util_win.cc

Issue 1689012: Move common code into process_util.cc (Closed)
Patch Set: More fixes Created 10 years, 7 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
« no previous file with comments | « base/process_util_unittest.cc ('k') | base/shared_memory.h » ('j') | 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 start_hidden, process_handle); 215 start_hidden, process_handle);
216 } 216 }
217 217
218 // Attempts to kill the process identified by the given process 218 // Attempts to kill the process identified by the given process
219 // entry structure, giving it the specified exit code. 219 // entry structure, giving it the specified exit code.
220 // Returns true if this is successful, false otherwise. 220 // Returns true if this is successful, false otherwise.
221 bool KillProcessById(ProcessId process_id, int exit_code, bool wait) { 221 bool KillProcessById(ProcessId process_id, int exit_code, bool wait) {
222 HANDLE process = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, 222 HANDLE process = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE,
223 FALSE, // Don't inherit handle 223 FALSE, // Don't inherit handle
224 process_id); 224 process_id);
225 if (!process) 225 if (!process) {
226 DLOG(ERROR) << "Unable to open process " << process_id << " : "
227 << GetLastError();
226 return false; 228 return false;
227 229 }
228 bool ret = KillProcess(process, exit_code, wait); 230 bool ret = KillProcess(process, exit_code, wait);
229 CloseHandle(process); 231 CloseHandle(process);
230 return ret; 232 return ret;
231 } 233 }
232 234
233 bool GetAppOutput(const CommandLine& cl, std::string* output) { 235 bool GetAppOutput(const CommandLine& cl, std::string* output) {
234 HANDLE out_read = NULL; 236 HANDLE out_read = NULL;
235 HANDLE out_write = NULL; 237 HANDLE out_write = NULL;
236 238
237 SECURITY_ATTRIBUTES sa_attr; 239 SECURITY_ATTRIBUTES sa_attr;
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 return false; 379 return false;
378 380
379 // Only close the handle on success, to give the caller a chance to forcefully 381 // Only close the handle on success, to give the caller a chance to forcefully
380 // terminate the process if he wants to. 382 // terminate the process if he wants to.
381 CloseProcessHandle(handle); 383 CloseProcessHandle(handle);
382 384
383 *exit_code = temp_code; 385 *exit_code = temp_code;
384 return true; 386 return true;
385 } 387 }
386 388
387 NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name, 389 ProcessIterator::ProcessIterator(const ProcessFilter* filter)
388 const ProcessFilter* filter)
389 : started_iteration_(false), 390 : started_iteration_(false),
390 executable_name_(executable_name),
391 filter_(filter) { 391 filter_(filter) {
392 snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 392 snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
393 } 393 }
394 394
395 NamedProcessIterator::~NamedProcessIterator() { 395 ProcessIterator::~ProcessIterator() {
396 CloseHandle(snapshot_); 396 CloseHandle(snapshot_);
397 } 397 }
398 398
399 399 bool ProcessIterator::CheckForNextProcess() {
400 const ProcessEntry* NamedProcessIterator::NextProcessEntry() {
401 bool result = false;
402 do {
403 result = CheckForNextProcess();
404 } while (result && !IncludeEntry());
405
406 if (result) {
407 return &entry_;
408 }
409
410 return NULL;
411 }
412
413 bool NamedProcessIterator::CheckForNextProcess() {
414 InitProcessEntry(&entry_); 400 InitProcessEntry(&entry_);
415 401
416 if (!started_iteration_) { 402 if (!started_iteration_) {
417 started_iteration_ = true; 403 started_iteration_ = true;
418 return !!Process32First(snapshot_, &entry_); 404 return !!Process32First(snapshot_, &entry_);
419 } 405 }
420 406
421 return !!Process32Next(snapshot_, &entry_); 407 return !!Process32Next(snapshot_, &entry_);
422 } 408 }
423 409
424 bool NamedProcessIterator::IncludeEntry() { 410 void ProcessIterator::InitProcessEntry(ProcessEntry* entry) {
425 return _wcsicmp(executable_name_.c_str(), entry_.szExeFile) == 0 &&
426 (!filter_ || filter_->Includes(entry_.th32ProcessID,
427 entry_.th32ParentProcessID));
428 }
429
430 void NamedProcessIterator::InitProcessEntry(ProcessEntry* entry) {
431 memset(entry, 0, sizeof(*entry)); 411 memset(entry, 0, sizeof(*entry));
432 entry->dwSize = sizeof(*entry); 412 entry->dwSize = sizeof(*entry);
433 } 413 }
434 414
435 int GetProcessCount(const std::wstring& executable_name, 415 bool NamedProcessIterator::IncludeEntry() {
436 const ProcessFilter* filter) { 416 // Case insensitive.
437 int count = 0; 417 return _wcsicmp(executable_name_.c_str(), entry().exe_file()) == 0 &&
438 418 ProcessIterator::IncludeEntry();
439 NamedProcessIterator iter(executable_name, filter);
440 while (iter.NextProcessEntry())
441 ++count;
442 return count;
443 }
444
445 bool KillProcesses(const std::wstring& executable_name, int exit_code,
446 const ProcessFilter* filter) {
447 bool result = true;
448 const ProcessEntry* entry;
449
450 NamedProcessIterator iter(executable_name, filter);
451 while (entry = iter.NextProcessEntry()) {
452 if (!KillProcessById((*entry).th32ProcessID, exit_code, true))
453 result = false;
454 }
455
456 return result;
457 } 419 }
458 420
459 bool WaitForProcessesToExit(const std::wstring& executable_name, 421 bool WaitForProcessesToExit(const std::wstring& executable_name,
460 int64 wait_milliseconds, 422 int64 wait_milliseconds,
461 const ProcessFilter* filter) { 423 const ProcessFilter* filter) {
462 const ProcessEntry* entry; 424 const ProcessEntry* entry;
463 bool result = true; 425 bool result = true;
464 DWORD start_time = GetTickCount(); 426 DWORD start_time = GetTickCount();
465 427
466 NamedProcessIterator iter(executable_name, filter); 428 NamedProcessIterator iter(executable_name, filter);
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 // We add time_delta / 2 so the result is rounded. 677 // We add time_delta / 2 so the result is rounded.
716 int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) / 678 int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) /
717 time_delta); 679 time_delta);
718 680
719 last_system_time_ = system_time; 681 last_system_time_ = system_time;
720 last_time_ = time; 682 last_time_ = time;
721 683
722 return cpu; 684 return cpu;
723 } 685 }
724 686
725 bool ProcessMetrics::GetIOCounters(base::IoCounters* io_counters) const { 687 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
726 return GetProcessIoCounters(process_, io_counters) != FALSE; 688 return GetProcessIoCounters(process_, io_counters) != FALSE;
727 } 689 }
728 690
729 bool ProcessMetrics::CalculateFreeMemory(FreeMBytes* free) const { 691 bool ProcessMetrics::CalculateFreeMemory(FreeMBytes* free) const {
730 const SIZE_T kTopAdress = 0x7F000000; 692 const SIZE_T kTopAdress = 0x7F000000;
731 const SIZE_T kMegabyte = 1024 * 1024; 693 const SIZE_T kMegabyte = 1024 * 1024;
732 SIZE_T accumulated = 0; 694 SIZE_T accumulated = 0;
733 695
734 MEMORY_BASIC_INFORMATION largest = {0}; 696 MEMORY_BASIC_INFORMATION largest = {0};
735 UINT_PTR scan = 0; 697 UINT_PTR scan = 0;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 799
838 PERFORMANCE_INFORMATION info; 800 PERFORMANCE_INFORMATION info;
839 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { 801 if (!InternalGetPerformanceInfo(&info, sizeof(info))) {
840 LOG(ERROR) << "Failed to fetch internal performance info."; 802 LOG(ERROR) << "Failed to fetch internal performance info.";
841 return 0; 803 return 0;
842 } 804 }
843 return (info.CommitTotal * system_info.dwPageSize) / 1024; 805 return (info.CommitTotal * system_info.dwPageSize) / 1024;
844 } 806 }
845 807
846 } // namespace base 808 } // namespace base
OLDNEW
« no previous file with comments | « base/process_util_unittest.cc ('k') | base/shared_memory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698