Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / test / mini_installer / quit_chrome.py
blob90e3e6e09646d3cad74ea3337df0f19cd2b11cea
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 """Quits Chrome.
7 This script sends a WM_CLOSE message to each window of Chrome and waits until
8 the process terminates.
9 """
11 import optparse
12 import pywintypes
13 import sys
14 import time
15 import win32con
16 import win32gui
17 import winerror
19 import chrome_helper
22 def CloseWindows(process_path):
23 """Closes all windows owned by processes whose path is |process_path|.
25 Args:
26 process_path: The path to the process.
28 Returns:
29 A boolean indicating whether the processes successfully terminate within
30 30 seconds.
31 """
32 start_time = time.time()
33 while time.time() - start_time < 30:
34 process_ids = chrome_helper.GetProcessIDs(process_path)
35 if not process_ids:
36 return True
38 for hwnd in chrome_helper.GetWindowHandles(process_ids):
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
45 time.sleep(0.1)
46 return False
49 def main():
50 usage = 'usage: %prog chrome_path'
51 parser = optparse.OptionParser(usage, description='Quit Chrome.')
52 _, args = parser.parse_args()
53 if len(args) != 1:
54 parser.error('Incorrect number of arguments.')
55 chrome_path = args[0]
57 if not CloseWindows(chrome_path):
58 raise Exception('Could not quit Chrome.')
59 return 0
62 if __name__ == '__main__':
63 sys.exit(main())