OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 """Common helper module for working with Chrome's processes and windows.""" | 5 """Common helper module for working with Chrome's processes and windows.""" |
6 | 6 |
7 import ctypes | 7 import psutil |
8 import pywintypes | |
9 import re | 8 import re |
10 import win32con | |
11 import win32gui | 9 import win32gui |
12 import win32process | 10 import win32process |
13 | 11 |
14 | 12 |
15 def GetProcessIDAndPathPairs(): | 13 def GetProcessIDAndPathPairs(): |
16 """Returns a list of 2-tuples of (process id, process path). | 14 """Returns a list of 2-tuples of (process id, process path). |
17 | |
18 This is needed because psutil is not available on Windows slave machines (see: | |
19 http://crbug.com/257696). | |
20 TODO(sukolsak): Use psutil.process_iter() once it becomes available. | |
21 """ | 15 """ |
22 process_id_and_path_pairs = [] | 16 process_id_and_path_pairs = [] |
23 for process_id in win32process.EnumProcesses(): | 17 for process in psutil.process_iter(): |
24 process_handle = ctypes.windll.kernel32.OpenProcess( | |
25 win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, False, | |
26 process_id) | |
27 if not process_handle: | |
28 continue | |
29 try: | 18 try: |
30 process_path = win32process.GetModuleFileNameEx(process_handle, 0) | 19 process_id_and_path_pairs.append((process.pid, process.exe)) |
31 process_id_and_path_pairs.append((process_id, process_path)) | 20 except psutil.Error: |
32 except pywintypes.error: | |
33 # It's normal that some processes are not accessible. | 21 # It's normal that some processes are not accessible. |
34 pass | 22 pass |
35 return process_id_and_path_pairs | 23 return process_id_and_path_pairs |
36 | 24 |
37 | 25 |
38 def GetProcessIDs(process_path): | 26 def GetProcessIDs(process_path): |
39 """Returns a list of IDs of processes whose path is |process_path|. | 27 """Returns a list of IDs of processes whose path is |process_path|. |
40 | 28 |
41 Args: | 29 Args: |
42 process_path: The path to the process. | 30 process_path: The path to the process. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 process_ids: A list of process IDs. | 66 process_ids: A list of process IDs. |
79 class_pattern: The regular expression pattern of the window class name. | 67 class_pattern: The regular expression pattern of the window class name. |
80 | 68 |
81 Returns: | 69 Returns: |
82 A boolean indicating whether such window exists. | 70 A boolean indicating whether such window exists. |
83 """ | 71 """ |
84 for hwnd in GetWindowHandles(process_ids): | 72 for hwnd in GetWindowHandles(process_ids): |
85 if re.match(class_pattern, win32gui.GetClassName(hwnd)): | 73 if re.match(class_pattern, win32gui.GetClassName(hwnd)): |
86 return True | 74 return True |
87 return False | 75 return False |
OLD | NEW |