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 desktop browsers that can be controlled by telemetry.""" | |
5 | |
6 import logging | |
7 import os | |
8 import subprocess | |
9 import sys | |
10 | |
11 from telemetry import browser | |
12 from telemetry import desktop_browser_backend | |
13 from telemetry import platform | |
14 from telemetry import possible_browser | |
15 | |
16 ALL_BROWSER_TYPES = ','.join([ | |
17 'exact', | |
18 'release', | |
19 'debug', | |
20 'canary', | |
21 'content-shell-debug', | |
22 'content-shell-release', | |
23 'system']) | |
24 | |
25 class PossibleDesktopBrowser(possible_browser.PossibleBrowser): | |
26 """A desktop browser that can be controlled.""" | |
27 | |
28 def __init__(self, browser_type, options, executable, is_content_shell): | |
29 super(PossibleDesktopBrowser, self).__init__(browser_type, options) | |
30 self._local_executable = executable | |
31 self._is_content_shell = is_content_shell | |
32 | |
33 def __repr__(self): | |
34 return 'PossibleDesktopBrowser(browser_type=%s)' % self.browser_type | |
35 | |
36 def Create(self): | |
37 backend = desktop_browser_backend.DesktopBrowserBackend( | |
38 self._options, self._local_executable, self._is_content_shell) | |
39 b = browser.Browser(backend, platform.Platform()) | |
40 backend.SetBrowser(b) | |
41 return b | |
42 | |
43 def SupportsOptions(self, options): | |
44 if (len(options.extensions_to_load) != 0) and self._is_content_shell: | |
45 return False | |
46 return True | |
47 | |
48 def FindAllAvailableBrowsers(options): | |
49 """Finds all the desktop browsers available on this machine.""" | |
50 browsers = [] | |
51 | |
52 has_display = True | |
53 if (sys.platform.startswith('linux') and | |
54 os.getenv('DISPLAY') == None): | |
55 has_display = False | |
56 | |
57 # Add the explicit browser executable if given. | |
58 if options.browser_executable: | |
59 normalized_executable = os.path.expanduser(options.browser_executable) | |
60 if os.path.exists(normalized_executable): | |
61 browsers.append(PossibleDesktopBrowser('exact', options, | |
62 normalized_executable, False)) | |
63 else: | |
64 logging.warning('%s specified by browser_executable does not exist', | |
65 normalized_executable) | |
66 | |
67 # Look for a browser in the standard chrome build locations. | |
68 if options.chrome_root: | |
69 chrome_root = options.chrome_root | |
70 else: | |
71 chrome_root = os.path.join(os.path.dirname(__file__), '..', '..', '..') | |
72 | |
73 if sys.platform == 'darwin': | |
74 chromium_app_name = 'Chromium.app/Contents/MacOS/Chromium' | |
75 content_shell_app_name = 'Content Shell.app/Contents/MacOS/Content Shell' | |
76 elif sys.platform.startswith('linux'): | |
77 chromium_app_name = 'chrome' | |
78 content_shell_app_name = 'content_shell' | |
79 elif sys.platform.startswith('win'): | |
80 chromium_app_name = 'chrome.exe' | |
81 content_shell_app_name = 'content_shell.exe' | |
82 else: | |
83 raise Exception('Platform not recognized') | |
84 | |
85 build_dirs = ['build', | |
86 'out', | |
87 'sconsbuild', | |
88 'xcodebuild'] | |
89 | |
90 def AddIfFound(browser_type, type_dir, app_name, content_shell): | |
91 for build_dir in build_dirs: | |
92 app = os.path.join(chrome_root, build_dir, type_dir, app_name) | |
93 if os.path.exists(app): | |
94 browsers.append(PossibleDesktopBrowser(browser_type, options, | |
95 app, content_shell)) | |
96 return True | |
97 return False | |
98 | |
99 # Add local builds | |
100 AddIfFound('debug', 'Debug', chromium_app_name, False) | |
101 AddIfFound('content-shell-debug', 'Debug', content_shell_app_name, True) | |
102 AddIfFound('release', 'Release', chromium_app_name, False) | |
103 AddIfFound('content-shell-release', 'Release', content_shell_app_name, True) | |
104 | |
105 # Mac-specific options. | |
106 if sys.platform == 'darwin': | |
107 mac_canary = ('/Applications/Google Chrome Canary.app/' | |
108 'Contents/MacOS/Google Chrome Canary') | |
109 mac_system = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' | |
110 if os.path.exists(mac_canary): | |
111 browsers.append(PossibleDesktopBrowser('canary', options, | |
112 mac_canary, False)) | |
113 | |
114 if os.path.exists(mac_system): | |
115 browsers.append(PossibleDesktopBrowser('system', options, | |
116 mac_system, False)) | |
117 | |
118 # Linux specific options. | |
119 if sys.platform.startswith('linux'): | |
120 # Look for a google-chrome instance. | |
121 found = False | |
122 try: | |
123 with open(os.devnull, 'w') as devnull: | |
124 found = subprocess.call(['google-chrome', '--version'], | |
125 stdout=devnull, stderr=devnull) == 0 | |
126 except OSError: | |
127 pass | |
128 if found: | |
129 browsers.append( | |
130 PossibleDesktopBrowser('system', options, 'google-chrome', False)) | |
131 | |
132 # Win32-specific options. | |
133 if sys.platform.startswith('win'): | |
134 system_path = os.path.join('Google', 'Chrome', 'Application') | |
135 canary_path = os.path.join('Google', 'Chrome SxS', 'Application') | |
136 | |
137 win_search_paths = [os.getenv('PROGRAMFILES(X86)'), | |
138 os.getenv('PROGRAMFILES'), | |
139 os.getenv('LOCALAPPDATA')] | |
140 | |
141 for path in win_search_paths: | |
142 if not path: | |
143 continue | |
144 if AddIfFound('canary', os.path.join(path, canary_path), | |
145 chromium_app_name, False): | |
146 break | |
147 | |
148 for path in win_search_paths: | |
149 if not path: | |
150 continue | |
151 if AddIfFound('system', os.path.join(path, system_path), | |
152 chromium_app_name, False): | |
153 break | |
154 | |
155 if len(browsers) and not has_display: | |
156 logging.warning( | |
157 'Found (%s), but you do not have a DISPLAY environment set.' % | |
158 ','.join([b.browser_type for b in browsers])) | |
159 return [] | |
160 | |
161 return browsers | |
OLD | NEW |