Chromium Code Reviews| 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).""" | |
|
gab
2013/08/30 16:20:21
Nice work; perhaps add a comment here saying that
sukolsak
2013/08/30 19:03:36
Done. psutil is available on Python 2.6, but it's
| |
| 19 process_id_and_path_pairs = [] | |
| 20 for process_id in win32process.EnumProcesses(): | |
| 21 process_handle = ctypes.windll.kernel32.OpenProcess( | |
| 22 win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, False, | |
| 23 process_id) | |
| 24 if not process_handle: | |
| 25 continue | |
| 26 try: | |
| 27 process_path = win32process.GetModuleFileNameEx(process_handle, 0) | |
| 28 process_id_and_path_pairs.append((process_id, process_path)) | |
| 29 except pywintypes.error: | |
| 30 # It's normal that some processes are not accessible. | |
| 31 pass | |
| 32 return process_id_and_path_pairs | |
| 33 | |
| 34 | |
| 15 def GetProcessIDs(process_path): | 35 def GetProcessIDs(process_path): |
| 16 """Returns a list of IDs of processes whose path is |process_path|. | 36 """Returns a list of IDs of processes whose path is |process_path|. |
| 17 | 37 |
| 18 Args: | 38 Args: |
| 19 process_path: The path to the process. | 39 process_path: The path to the process. |
| 20 | 40 |
| 21 Returns: | 41 Returns: |
| 22 A list of process IDs. | 42 A list of process IDs. |
| 23 """ | 43 """ |
| 24 process_ids = [] | 44 process_ids = [] |
| 25 for process in psutil.process_iter(): | 45 for found_process_id, found_process_path in GetProcessIDAndPathPairs(): |
| 26 try: | 46 if found_process_path == process_path: |
| 27 found_process_path = process.exe | 47 process_ids.append(found_process_id) |
| 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 | 48 return process_ids |
| 34 | 49 |
| 35 | 50 |
| 36 def GetWindowHandles(process_ids): | 51 def GetWindowHandles(process_ids): |
| 37 """Returns a list of handles of windows owned by processes in |process_ids|. | 52 """Returns a list of handles of windows owned by processes in |process_ids|. |
| 38 | 53 |
| 39 Args: | 54 Args: |
| 40 process_ids: A list of process IDs. | 55 process_ids: A list of process IDs. |
| 41 | 56 |
| 42 Returns: | 57 Returns: |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 73 | 88 |
| 74 | 89 |
| 75 def GetChromePath(system_level): | 90 def GetChromePath(system_level): |
| 76 """Returns the path to Chrome, at the |system_level| or user level.""" | 91 """Returns the path to Chrome, at the |system_level| or user level.""" |
| 77 chrome_path = None | 92 chrome_path = None |
| 78 if system_level: | 93 if system_level: |
| 79 chrome_path = '$PROGRAM_FILES\\$CHROME_DIR\\Application\\chrome.exe' | 94 chrome_path = '$PROGRAM_FILES\\$CHROME_DIR\\Application\\chrome.exe' |
| 80 else: | 95 else: |
| 81 chrome_path = '$LOCAL_APPDATA\\$CHROME_DIR\\Application\\chrome.exe' | 96 chrome_path = '$LOCAL_APPDATA\\$CHROME_DIR\\Application\\chrome.exe' |
| 82 return path_resolver.ResolvePath(chrome_path) | 97 return path_resolver.ResolvePath(chrome_path) |
| OLD | NEW |