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 browser
12 from telemetry.core import platform as core_platform
13 from telemetry.core import possible_browser
14 from telemetry.core import util
15 from telemetry.core.backends.chrome import cros_interface
16 from telemetry.core.backends.chrome import desktop_browser_backend
25 'content-shell-debug',
26 'content-shell-debug_x64',
27 'content-shell-release',
28 'content-shell-release_x64',
31 class PossibleDesktopBrowser(possible_browser.PossibleBrowser):
32 """A desktop browser that can be controlled."""
34 def __init__(self, browser_type, finder_options, executable, flash_path,
35 is_content_shell, browser_directory, is_local_build=False):
36 target_os = sys.platform.lower()
37 super(PossibleDesktopBrowser, self).__init__(browser_type, target_os,
39 assert browser_type in ALL_BROWSER_TYPES, \
40 'Please add %s to ALL_BROWSER_TYPES' % browser_type
41 self._local_executable = executable
42 self._flash_path = flash_path
43 self._is_content_shell = is_content_shell
44 self._browser_directory = browser_directory
45 self.is_local_build = is_local_build
48 return 'PossibleDesktopBrowser(browser_type=%s, executable=%s)' % (
49 self.browser_type, self._local_executable)
52 backend = desktop_browser_backend.DesktopBrowserBackend(
53 self.finder_options.browser_options, self._local_executable,
54 self._flash_path, self._is_content_shell, self._browser_directory,
55 output_profile_path=self.finder_options.output_profile_path,
56 extensions_to_load=self.finder_options.extensions_to_load)
57 b = browser.Browser(backend,
58 core_platform.CreatePlatformBackendForCurrentOS())
61 def SupportsOptions(self, finder_options):
62 if (len(finder_options.extensions_to_load) != 0) and self._is_content_shell:
66 def UpdateExecutableIfNeeded(self):
69 def last_modification_time(self):
70 if os.path.exists(self._local_executable):
71 return os.path.getmtime(self._local_executable)
74 def SelectDefaultBrowser(possible_browsers):
75 local_builds_by_date = [
76 b for b in sorted(possible_browsers,
77 key=lambda b: b.last_modification_time())
79 if local_builds_by_date:
80 return local_builds_by_date[-1]
83 def CanFindAvailableBrowsers():
84 return not cros_interface.IsRunningOnCrosDevice()
86 def FindAllAvailableBrowsers(finder_options):
87 """Finds all the desktop browsers available on this machine."""
90 if not CanFindAvailableBrowsers():
94 if (sys.platform.startswith('linux') and
95 os.getenv('DISPLAY') == None):
98 # Look for a browser in the standard chrome build locations.
99 if finder_options.chrome_root:
100 chrome_root = finder_options.chrome_root
102 chrome_root = util.GetChromiumSrcDir()
104 flash_bin_dir = os.path.join(
105 chrome_root, 'third_party', 'adobe', 'flash', 'binaries', 'ppapi')
107 chromium_app_names = []
108 if sys.platform == 'darwin':
109 chromium_app_names.append('Chromium.app/Contents/MacOS/Chromium')
110 chromium_app_names.append('Google Chrome.app/Contents/MacOS/Google Chrome')
111 content_shell_app_name = 'Content Shell.app/Contents/MacOS/Content Shell'
112 flash_bin = 'PepperFlashPlayer.plugin'
113 flash_path = os.path.join(flash_bin_dir, 'mac', flash_bin)
114 flash_path_64 = os.path.join(flash_bin_dir, 'mac_64', flash_bin)
115 elif sys.platform.startswith('linux'):
116 chromium_app_names.append('chrome')
117 content_shell_app_name = 'content_shell'
118 flash_bin = 'libpepflashplayer.so'
119 flash_path = os.path.join(flash_bin_dir, 'linux', flash_bin)
120 flash_path_64 = os.path.join(flash_bin_dir, 'linux_x64', flash_bin)
121 elif sys.platform.startswith('win'):
122 chromium_app_names.append('chrome.exe')
123 content_shell_app_name = 'content_shell.exe'
124 flash_bin = 'pepflashplayer.dll'
125 flash_path = os.path.join(flash_bin_dir, 'win', flash_bin)
126 flash_path_64 = os.path.join(flash_bin_dir, 'win_x64', flash_bin)
128 raise Exception('Platform not recognized')
130 def IsExecutable(path):
131 return os.path.isfile(path) and os.access(path, os.X_OK)
133 # Add the explicit browser executable if given.
134 if finder_options.browser_executable:
135 normalized_executable = os.path.expanduser(
136 finder_options.browser_executable)
137 if IsExecutable(normalized_executable):
138 browser_directory = os.path.dirname(finder_options.browser_executable)
139 browsers.append(PossibleDesktopBrowser('exact', finder_options,
140 normalized_executable, flash_path,
141 False, browser_directory))
143 logging.warning('%s specified by browser_executable does not exist',
144 normalized_executable)
146 def AddIfFound(browser_type, build_dir, type_dir, app_name, content_shell):
147 browser_directory = os.path.join(chrome_root, build_dir, type_dir)
148 app = os.path.join(browser_directory, app_name)
149 if IsExecutable(app):
150 is_64 = browser_type.endswith('_x64')
151 browsers.append(PossibleDesktopBrowser(
152 browser_type, finder_options, app,
153 flash_path_64 if is_64 else flash_path,
154 content_shell, browser_directory, is_local_build=True))
159 for build_dir, build_type in util.GetBuildDirectories():
160 for chromium_app_name in chromium_app_names:
161 AddIfFound(build_type.lower(), build_dir, build_type,
162 chromium_app_name, False)
163 AddIfFound('content-shell-' + build_type.lower(), build_dir, build_type,
164 content_shell_app_name, True)
166 # Mac-specific options.
167 if sys.platform == 'darwin':
168 mac_canary_root = '/Applications/Google Chrome Canary.app/'
169 mac_canary = mac_canary_root + 'Contents/MacOS/Google Chrome Canary'
170 mac_system_root = '/Applications/Google Chrome.app'
171 mac_system = mac_system_root + '/Contents/MacOS/Google Chrome'
172 if IsExecutable(mac_canary):
173 browsers.append(PossibleDesktopBrowser('canary', finder_options,
174 mac_canary, None, False,
177 if IsExecutable(mac_system):
178 browsers.append(PossibleDesktopBrowser('system', finder_options,
179 mac_system, None, False,
182 # Linux specific options.
183 if sys.platform.startswith('linux'):
184 # Look for a google-chrome instance.
187 with open(os.devnull, 'w') as devnull:
188 found = subprocess.call(['google-chrome', '--version'],
189 stdout=devnull, stderr=devnull) == 0
193 browsers.append(PossibleDesktopBrowser('system', finder_options,
194 'google-chrome', None, False,
195 '/opt/google/chrome'))
197 # Win32-specific options.
198 if sys.platform.startswith('win'):
199 system_path = os.path.join('Google', 'Chrome', 'Application')
200 canary_path = os.path.join('Google', 'Chrome SxS', 'Application')
202 win_search_paths = [os.getenv('PROGRAMFILES(X86)'),
203 os.getenv('PROGRAMFILES'),
204 os.getenv('LOCALAPPDATA')]
206 def AddIfFoundWin(browser_name, app_path):
207 browser_directory = os.path.join(path, app_path)
208 for chromium_app_name in chromium_app_names:
209 app = os.path.join(browser_directory, chromium_app_name)
210 if IsExecutable(app):
211 browsers.append(PossibleDesktopBrowser(browser_name, finder_options,
217 for path in win_search_paths:
220 if AddIfFoundWin('canary', canary_path):
223 for path in win_search_paths:
226 if AddIfFoundWin('system', system_path):
229 if len(browsers) and not has_display:
231 'Found (%s), but you do not have a DISPLAY environment set.' %
232 ','.join([b.browser_type for b in browsers]))