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
3 # found in the LICENSE file.
5 """Common helper module for working with Chrome's processes and windows."""
13 def GetProcessIDAndPathPairs():
14 """Returns a list of 2-tuples of (process id, process path).
16 process_id_and_path_pairs
= []
17 for process
in psutil
.process_iter():
19 process_id_and_path_pairs
.append((process
.pid
, process
.exe
))
21 # It's normal that some processes are not accessible.
23 return process_id_and_path_pairs
26 def GetProcessIDs(process_path
):
27 """Returns a list of IDs of processes whose path is |process_path|.
30 process_path: The path to the process.
33 A list of process IDs.
35 return [pid
for (pid
, path
) in GetProcessIDAndPathPairs() if
39 def GetWindowHandles(process_ids
):
40 """Returns a list of handles of windows owned by processes in |process_ids|.
43 process_ids: A list of process IDs.
46 A list of handles of windows owned by processes in |process_ids|.
49 def EnumerateWindowCallback(hwnd
, _
):
50 _
, found_process_id
= win32process
.GetWindowThreadProcessId(hwnd
)
51 if found_process_id
in process_ids
and win32gui
.IsWindowVisible(hwnd
):
53 # Enumerate all the top-level windows and call the callback with the hwnd as
54 # the first parameter.
55 win32gui
.EnumWindows(EnumerateWindowCallback
, None)
59 def WindowExists(process_ids
, class_pattern
):
60 """Returns whether there exists a window with the specified criteria.
62 This method returns whether there exists a window that is owned by a process
63 in |process_ids| and has a class name that matches |class_pattern|.
66 process_ids: A list of process IDs.
67 class_pattern: The regular expression pattern of the window class name.
70 A boolean indicating whether such window exists.
72 for hwnd
in GetWindowHandles(process_ids
):
73 if re
.match(class_pattern
, win32gui
.GetClassName(hwnd
)):