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 """Quits Chrome. | 5 """Quits Chrome. |
6 | 6 |
7 This script sends a WM_CLOSE message to each window of Chrome and waits until | 7 This script sends a WM_CLOSE message to each window of Chrome and waits until |
8 the process terminates. | 8 the process terminates. |
9 """ | 9 """ |
10 | 10 |
11 import optparse | 11 import optparse |
12 import pywintypes | |
12 import sys | 13 import sys |
13 import time | 14 import time |
14 import win32con | 15 import win32con |
15 import win32gui | 16 import win32gui |
17 import winerror | |
16 | 18 |
17 import chrome_helper | 19 import chrome_helper |
18 | 20 |
19 | 21 |
20 def CloseWindows(process_path): | 22 def CloseWindows(process_path): |
21 """Closes all windows owned by processes whose path is |process_path|. | 23 """Closes all windows owned by processes whose path is |process_path|. |
22 | 24 |
23 Args: | 25 Args: |
24 process_path: The path to the process. | 26 process_path: The path to the process. |
25 | 27 |
26 Returns: | 28 Returns: |
27 A boolean indicating whether the processes successfully terminate within | 29 A boolean indicating whether the processes successfully terminate within |
28 30 seconds. | 30 30 seconds. |
29 """ | 31 """ |
30 start_time = time.time() | 32 start_time = time.time() |
31 while time.time() - start_time < 30: | 33 while time.time() - start_time < 30: |
32 process_ids = chrome_helper.GetProcessIDs(process_path) | 34 process_ids = chrome_helper.GetProcessIDs(process_path) |
33 if not process_ids: | 35 if not process_ids: |
34 return True | 36 return True |
35 | 37 |
36 for hwnd in chrome_helper.GetWindowHandles(process_ids): | 38 for hwnd in chrome_helper.GetWindowHandles(process_ids): |
37 win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) | 39 try: |
40 win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) | |
41 except pywintypes.error as error: | |
42 if error.args[0] == winerror.ERROR_INVALID_WINDOW_HANDLE: | |
43 # It's normal that some window handles have become invalid. | |
44 pass | |
grt (UTC plus 2)
2013/09/09 18:53:59
is this more idomatic than:
# It's normal that s
gab
2013/09/09 19:29:06
+1 (had to lookup "idomatic", but yes I prefer tha
sukolsak
2013/09/09 21:26:41
Done.
| |
45 else: | |
46 raise | |
38 time.sleep(0) | 47 time.sleep(0) |
39 return False | 48 return False |
40 | 49 |
41 | 50 |
42 def main(): | 51 def main(): |
43 usage = 'usage: %prog chrome_path' | 52 usage = 'usage: %prog chrome_path' |
44 parser = optparse.OptionParser(usage, description='Quit Chrome.') | 53 parser = optparse.OptionParser(usage, description='Quit Chrome.') |
45 _, args = parser.parse_args() | 54 _, args = parser.parse_args() |
46 if len(args) != 1: | 55 if len(args) != 1: |
47 parser.error('Incorrect number of arguments.') | 56 parser.error('Incorrect number of arguments.') |
48 chrome_path = args[0] | 57 chrome_path = args[0] |
49 | 58 |
50 if not CloseWindows(chrome_path): | 59 if not CloseWindows(chrome_path): |
51 raise Exception('Could not quit Chrome.') | 60 raise Exception('Could not quit Chrome.') |
52 return 0 | 61 return 0 |
53 | 62 |
54 | 63 |
55 if __name__ == '__main__': | 64 if __name__ == '__main__': |
56 sys.exit(main()) | 65 sys.exit(main()) |
OLD | NEW |