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.
4
5"""Launches Chrome.
6
7This script launches Chrome and waits until its window shows up.
8"""
9
10import optparse
11import sys
12import time
13import win32process
14
15import chrome_helper
16
17
18def WaitForWindow(process_id, class_pattern):
19  """Waits until a window specified by |process_id| and class name shows up.
20
21  Args:
22    process_id: The ID of the process that owns the window.
23    class_pattern: The regular expression pattern of the window class name.
24
25  Returns:
26    A boolean value indicating whether the specified window shows up within
27    30 seconds.
28  """
29  start_time = time.time()
30  while time.time() - start_time < 30:
31    if chrome_helper.WindowExists([process_id], class_pattern):
32      return True
33    time.sleep(0.1)
34  return False
35
36
37def main():
38  usage = 'usage: %prog chrome_path'
39  parser = optparse.OptionParser(usage, description='Launch Chrome.')
40  _, args = parser.parse_args()
41  if len(args) != 1:
42    parser.error('Incorrect number of arguments.')
43  chrome_path = args[0]
44
45  # Use CreateProcess rather than subprocess.Popen to avoid side effects such as
46  # handle interitance.
47  _, _, process_id, _ = win32process.CreateProcess(None, chrome_path, None,
48                                                   None, 0, 0, None, None,
49                                                   win32process.STARTUPINFO())
50  if not WaitForWindow(process_id, 'Chrome_WidgetWin_'):
51    raise Exception('Could not launch Chrome.')
52  return 0
53
54
55if __name__ == '__main__':
56  sys.exit(main())
57