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 # It's normal that some window handles have become invalid. |
| 43 if error.args[0] != winerror.ERROR_INVALID_WINDOW_HANDLE: |
| 44 raise |
38 time.sleep(0) | 45 time.sleep(0) |
39 return False | 46 return False |
40 | 47 |
41 | 48 |
42 def main(): | 49 def main(): |
43 usage = 'usage: %prog chrome_path' | 50 usage = 'usage: %prog chrome_path' |
44 parser = optparse.OptionParser(usage, description='Quit Chrome.') | 51 parser = optparse.OptionParser(usage, description='Quit Chrome.') |
45 _, args = parser.parse_args() | 52 _, args = parser.parse_args() |
46 if len(args) != 1: | 53 if len(args) != 1: |
47 parser.error('Incorrect number of arguments.') | 54 parser.error('Incorrect number of arguments.') |
48 chrome_path = args[0] | 55 chrome_path = args[0] |
49 | 56 |
50 if not CloseWindows(chrome_path): | 57 if not CloseWindows(chrome_path): |
51 raise Exception('Could not quit Chrome.') | 58 raise Exception('Could not quit Chrome.') |
52 return 0 | 59 return 0 |
53 | 60 |
54 | 61 |
55 if __name__ == '__main__': | 62 if __name__ == '__main__': |
56 sys.exit(main()) | 63 sys.exit(main()) |
OLD | NEW |