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 psutil | 7 import ctypes |
| 8 import pywintypes |
8 import re | 9 import re |
| 10 import win32con |
9 import win32gui | 11 import win32gui |
10 import win32process | 12 import win32process |
11 | 13 |
12 import path_resolver | 14 import path_resolver |
13 | 15 |
14 | 16 |
| 17 def GetProcessIDAndPathPairs(): |
| 18 """Returns a list of 2-tuples of (process id, process path). |
| 19 |
| 20 This is needed because psutil is not available on Windows slave machines (see: |
| 21 http://crbug.com/257696). |
| 22 TODO(sukolsak): Use psutil.process_iter() once it becomes available. |
| 23 """ |
| 24 process_id_and_path_pairs = [] |
| 25 for process_id in win32process.EnumProcesses(): |
| 26 process_handle = ctypes.windll.kernel32.OpenProcess( |
| 27 win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, False, |
| 28 process_id) |
| 29 if not process_handle: |
| 30 continue |
| 31 try: |
| 32 process_path = win32process.GetModuleFileNameEx(process_handle, 0) |
| 33 process_id_and_path_pairs.append((process_id, process_path)) |
| 34 except pywintypes.error: |
| 35 # It's normal that some processes are not accessible. |
| 36 pass |
| 37 return process_id_and_path_pairs |
| 38 |
| 39 |
15 def GetProcessIDs(process_path): | 40 def GetProcessIDs(process_path): |
16 """Returns a list of IDs of processes whose path is |process_path|. | 41 """Returns a list of IDs of processes whose path is |process_path|. |
17 | 42 |
18 Args: | 43 Args: |
19 process_path: The path to the process. | 44 process_path: The path to the process. |
20 | 45 |
21 Returns: | 46 Returns: |
22 A list of process IDs. | 47 A list of process IDs. |
23 """ | 48 """ |
24 process_ids = [] | 49 return [pid for (pid, path) in GetProcessIDAndPathPairs() if |
25 for process in psutil.process_iter(): | 50 path == process_path] |
26 try: | |
27 found_process_path = process.exe | |
28 if found_process_path == process_path: | |
29 process_ids.append(process.pid) | |
30 except psutil.AccessDenied: | |
31 # It's normal that some processes are not accessible. | |
32 pass | |
33 return process_ids | |
34 | 51 |
35 | 52 |
36 def GetWindowHandles(process_ids): | 53 def GetWindowHandles(process_ids): |
37 """Returns a list of handles of windows owned by processes in |process_ids|. | 54 """Returns a list of handles of windows owned by processes in |process_ids|. |
38 | 55 |
39 Args: | 56 Args: |
40 process_ids: A list of process IDs. | 57 process_ids: A list of process IDs. |
41 | 58 |
42 Returns: | 59 Returns: |
43 A list of handles of windows owned by processes in |process_ids|. | 60 A list of handles of windows owned by processes in |process_ids|. |
(...skipping 29 matching lines...) Expand all Loading... |
73 | 90 |
74 | 91 |
75 def GetChromePath(system_level): | 92 def GetChromePath(system_level): |
76 """Returns the path to Chrome, at the |system_level| or user level.""" | 93 """Returns the path to Chrome, at the |system_level| or user level.""" |
77 chrome_path = None | 94 chrome_path = None |
78 if system_level: | 95 if system_level: |
79 chrome_path = '$PROGRAM_FILES\\$CHROME_DIR\\Application\\chrome.exe' | 96 chrome_path = '$PROGRAM_FILES\\$CHROME_DIR\\Application\\chrome.exe' |
80 else: | 97 else: |
81 chrome_path = '$LOCAL_APPDATA\\$CHROME_DIR\\Application\\chrome.exe' | 98 chrome_path = '$LOCAL_APPDATA\\$CHROME_DIR\\Application\\chrome.exe' |
82 return path_resolver.ResolvePath(chrome_path) | 99 return path_resolver.ResolvePath(chrome_path) |
OLD | NEW |