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 """Finds desktop browsers that can be controlled by telemetry."""
11 from telemetry.core import platform as platform_module
12 from telemetry.core import browser
13 from telemetry.core import possible_browser
14 from telemetry.core.backends.chrome import desktop_browser_backend
15 from telemetry.util import path
18 class PossibleDesktopBrowser(possible_browser.PossibleBrowser):
19 """A desktop browser that can be controlled."""
21 def __init__(self, browser_type, finder_options, executable, flash_path,
22 is_content_shell, browser_directory, is_local_build=False):
23 target_os = sys.platform.lower()
24 super(PossibleDesktopBrowser, self).__init__(
25 browser_type, target_os, not is_content_shell)
26 assert browser_type in FindAllBrowserTypes(finder_options), (
27 'Please add %s to desktop_browser_finder.FindAllBrowserTypes' %
29 self._local_executable = executable
30 self._flash_path = flash_path
31 self._is_content_shell = is_content_shell
32 self._browser_directory = browser_directory
33 self.is_local_build = is_local_build
36 return 'PossibleDesktopBrowser(type=%s, executable=%s, flash=%s)' % (
37 self.browser_type, self._local_executable, self._flash_path)
39 def _InitPlatformIfNeeded(self):
43 self._platform = platform_module.GetHostPlatform()
45 # pylint: disable=W0212
46 self._platform_backend = self._platform._platform_backend
48 def Create(self, finder_options):
49 if self._flash_path and not os.path.exists(self._flash_path):
51 'Could not find Flash at %s. Continuing without Flash.\n'
52 'To run with Flash, check it out via http://go/read-src-internal',
54 self._flash_path = None
56 self._InitPlatformIfNeeded()
58 browser_backend = desktop_browser_backend.DesktopBrowserBackend(
59 self._platform_backend,
60 finder_options.browser_options, self._local_executable,
61 self._flash_path, self._is_content_shell, self._browser_directory,
62 output_profile_path=finder_options.output_profile_path,
63 extensions_to_load=finder_options.extensions_to_load)
64 return browser.Browser(
65 browser_backend, self._platform_backend, self._credentials_path)
67 def SupportsOptions(self, finder_options):
68 if (len(finder_options.extensions_to_load) != 0) and self._is_content_shell:
72 def UpdateExecutableIfNeeded(self):
75 def last_modification_time(self):
76 if os.path.exists(self._local_executable):
77 return os.path.getmtime(self._local_executable)
80 def SelectDefaultBrowser(possible_browsers):
81 local_builds_by_date = [
82 b for b in sorted(possible_browsers,
83 key=lambda b: b.last_modification_time())
85 if local_builds_by_date:
86 return local_builds_by_date[-1]
89 def CanFindAvailableBrowsers():
90 return not platform_module.GetHostPlatform().GetOSName() == 'chromeos'
92 def FindAllBrowserTypes(_):
101 'content-shell-debug',
102 'content-shell-debug_x64',
103 'content-shell-release',
104 'content-shell-release_x64',
107 def FindAllAvailableBrowsers(finder_options):
108 """Finds all the desktop browsers available on this machine."""
111 if not CanFindAvailableBrowsers():
115 if (sys.platform.startswith('linux') and
116 os.getenv('DISPLAY') == None):
119 # Look for a browser in the standard chrome build locations.
120 if finder_options.chrome_root:
121 chrome_root = finder_options.chrome_root
123 chrome_root = path.GetChromiumSrcDir()
125 flash_bin_dir = os.path.join(
126 chrome_root, 'third_party', 'adobe', 'flash', 'binaries', 'ppapi')
128 chromium_app_names = []
129 if sys.platform == 'darwin':
130 chromium_app_names.append('Chromium.app/Contents/MacOS/Chromium')
131 chromium_app_names.append('Google Chrome.app/Contents/MacOS/Google Chrome')
132 content_shell_app_name = 'Content Shell.app/Contents/MacOS/Content Shell'
133 flash_bin = 'PepperFlashPlayer.plugin'
134 flash_path = os.path.join(flash_bin_dir, 'mac', flash_bin)
135 flash_path_64 = os.path.join(flash_bin_dir, 'mac_64', flash_bin)
136 elif sys.platform.startswith('linux'):
137 chromium_app_names.append('chrome')
138 content_shell_app_name = 'content_shell'
139 flash_bin = 'libpepflashplayer.so'
140 flash_path = os.path.join(flash_bin_dir, 'linux', flash_bin)
141 flash_path_64 = os.path.join(flash_bin_dir, 'linux_x64', flash_bin)
142 elif sys.platform.startswith('win'):
143 chromium_app_names.append('chrome.exe')
144 content_shell_app_name = 'content_shell.exe'
145 flash_bin = 'pepflashplayer.dll'
146 flash_path = os.path.join(flash_bin_dir, 'win', flash_bin)
147 flash_path_64 = os.path.join(flash_bin_dir, 'win_x64', flash_bin)
149 raise Exception('Platform not recognized')
151 # Add the explicit browser executable if given.
152 if finder_options.browser_executable:
153 normalized_executable = os.path.expanduser(
154 finder_options.browser_executable)
155 if path.IsExecutable(normalized_executable):
156 browser_directory = os.path.dirname(finder_options.browser_executable)
157 browsers.append(PossibleDesktopBrowser('exact', finder_options,
158 normalized_executable, flash_path,
159 False, browser_directory))
161 raise Exception('%s specified by --browser-executable does not exist',
162 normalized_executable)
164 def AddIfFound(browser_type, build_dir, type_dir, app_name, content_shell):
165 browser_directory = os.path.join(chrome_root, build_dir, type_dir)
166 app = os.path.join(browser_directory, app_name)
167 if path.IsExecutable(app):
168 is_64 = browser_type.endswith('_x64')
169 browsers.append(PossibleDesktopBrowser(
170 browser_type, finder_options, app,
171 flash_path_64 if is_64 else flash_path,
172 content_shell, browser_directory, is_local_build=True))
177 for build_dir, build_type in path.GetBuildDirectories():
178 for chromium_app_name in chromium_app_names:
179 AddIfFound(build_type.lower(), build_dir, build_type,
180 chromium_app_name, False)
181 AddIfFound('content-shell-' + build_type.lower(), build_dir, build_type,
182 content_shell_app_name, True)
184 reference_build_root = os.path.join(
185 chrome_root, 'chrome', 'tools', 'test', 'reference_build')
187 # Mac-specific options.
188 if sys.platform == 'darwin':
189 mac_canary_root = '/Applications/Google Chrome Canary.app/'
190 mac_canary = mac_canary_root + 'Contents/MacOS/Google Chrome Canary'
191 mac_system_root = '/Applications/Google Chrome.app'
192 mac_system = mac_system_root + '/Contents/MacOS/Google Chrome'
193 mac_reference_root = reference_build_root + '/chrome_mac/Google Chrome.app/'
194 mac_reference = mac_reference_root + 'Contents/MacOS/Google Chrome'
195 if path.IsExecutable(mac_canary):
196 browsers.append(PossibleDesktopBrowser('canary', finder_options,
197 mac_canary, None, False,
200 if path.IsExecutable(mac_system):
201 browsers.append(PossibleDesktopBrowser('system', finder_options,
202 mac_system, None, False,
204 if path.IsExecutable(mac_reference):
205 browsers.append(PossibleDesktopBrowser('reference', finder_options,
206 mac_reference, None, False,
209 # Linux specific options.
210 if sys.platform.startswith('linux'):
211 # Look for a google-chrome instance.
214 with open(os.devnull, 'w') as devnull:
215 found = subprocess.call(['google-chrome', '--version'],
216 stdout=devnull, stderr=devnull) == 0
220 browsers.append(PossibleDesktopBrowser('system', finder_options,
221 'google-chrome', None, False,
222 '/opt/google/chrome'))
223 linux_reference_root = os.path.join(reference_build_root, 'chrome_linux')
224 linux_reference = os.path.join(linux_reference_root, 'chrome')
225 if path.IsExecutable(linux_reference):
226 browsers.append(PossibleDesktopBrowser('reference', finder_options,
227 linux_reference, None, False,
228 linux_reference_root))
230 # Win32-specific options.
231 if sys.platform.startswith('win'):
233 ('system', os.path.join('Google', 'Chrome', 'Application')),
234 ('canary', os.path.join('Google', 'Chrome SxS', 'Application')),
235 ('reference', os.path.join(reference_build_root, 'chrome_win')),
238 for browser_name, app_path in app_paths:
239 for chromium_app_name in chromium_app_names:
240 app_path = os.path.join(app_path, chromium_app_name)
241 app_path = path.FindInstalledWindowsApplication(app_path)
243 browsers.append(PossibleDesktopBrowser(
244 browser_name, finder_options, app_path,
245 None, False, os.path.dirname(app_path)))
247 if len(browsers) and not has_display:
249 'Found (%s), but you do not have a DISPLAY environment set.' %
250 ','.join([b.browser_type for b in browsers]))