OLD | NEW |
| (Empty) |
1 # Copyright (c) 2012 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 """Finds browsers that can be controlled by telemetry.""" | |
5 | |
6 import logging | |
7 | |
8 from telemetry.core.chrome import android_browser_finder | |
9 from telemetry.core.chrome import cros_browser_finder | |
10 from telemetry.core.chrome import desktop_browser_finder | |
11 | |
12 ALL_BROWSER_TYPES = ( | |
13 desktop_browser_finder.ALL_BROWSER_TYPES + ',' + | |
14 android_browser_finder.ALL_BROWSER_TYPES + ',' + | |
15 cros_browser_finder.ALL_BROWSER_TYPES) | |
16 | |
17 class BrowserTypeRequiredException(Exception): | |
18 pass | |
19 | |
20 def FindBrowser(options): | |
21 """Finds the best PossibleBrowser object to run given the provided | |
22 BrowserOptions object. The returned possiblity object can then be used to | |
23 connect to and control the located browser. | |
24 """ | |
25 if options.browser_type == 'exact' and options.browser_executable == None: | |
26 raise Exception('--browser=exact requires --browser-executable to be set.') | |
27 if options.browser_type != 'exact' and options.browser_executable != None: | |
28 raise Exception('--browser-executable requires --browser=exact.') | |
29 | |
30 if options.browser_type == 'cros-chrome' and options.cros_remote == None: | |
31 raise Exception('browser_type=cros-chrome requires cros_remote be set.') | |
32 if options.browser_type != 'cros-chrome' and options.cros_remote != None: | |
33 raise Exception('cros_remote requires browser_type=cros-chrome.') | |
34 | |
35 if options.browser_type == None: | |
36 raise BrowserTypeRequiredException('browser_type must be specified') | |
37 | |
38 browsers = [] | |
39 browsers.extend(desktop_browser_finder.FindAllAvailableBrowsers(options)) | |
40 browsers.extend(android_browser_finder.FindAllAvailableBrowsers(options)) | |
41 browsers.extend(cros_browser_finder.FindAllAvailableBrowsers(options)) | |
42 | |
43 if options.browser_type == 'any': | |
44 types = ALL_BROWSER_TYPES.split(',') | |
45 def compare_browsers_on_type_priority(x, y): | |
46 x_idx = types.index(x.browser_type) | |
47 y_idx = types.index(y.browser_type) | |
48 return x_idx - y_idx | |
49 browsers.sort(compare_browsers_on_type_priority) | |
50 if len(browsers) >= 1: | |
51 return browsers[0] | |
52 else: | |
53 return None | |
54 | |
55 matching_browsers = [b for b in browsers | |
56 if b.browser_type == options.browser_type and b.SupportsOptions(options)] | |
57 | |
58 if len(matching_browsers) == 1: | |
59 return matching_browsers[0] | |
60 elif len(matching_browsers) > 1: | |
61 logging.warning('Multiple browsers of the same type found: %s' % ( | |
62 repr(matching_browsers))) | |
63 return matching_browsers[0] | |
64 else: | |
65 return None | |
66 | |
67 def GetAllAvailableBrowserTypes(options): | |
68 """Returns an array of browser types supported on this system.""" | |
69 browsers = [] | |
70 browsers.extend(desktop_browser_finder.FindAllAvailableBrowsers(options)) | |
71 browsers.extend(android_browser_finder.FindAllAvailableBrowsers(options)) | |
72 browsers.extend(cros_browser_finder.FindAllAvailableBrowsers(options)) | |
73 | |
74 type_list = set([browser.browser_type for browser in browsers]) | |
75 type_list = list(type_list) | |
76 type_list.sort() | |
77 return type_list | |
OLD | NEW |