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."""
12 from telemetry.core import browser
13 from telemetry.core import platform as core_platform
14 from telemetry.core import possible_browser
15 from telemetry.core import util
16 from telemetry.core.backends.chrome import cros_interface
17 from telemetry.core.backends.chrome import desktop_browser_backend
19 ALL_BROWSER_TYPES = ','.join([
26 'content-shell-debug',
27 'content-shell-release',
30 class PossibleDesktopBrowser(possible_browser.PossibleBrowser):
31 """A desktop browser that can be controlled."""
33 def __init__(self, browser_type, finder_options, executable, flash_path,
34 is_content_shell, browser_directory, is_local_build=False):
35 super(PossibleDesktopBrowser, self).__init__(browser_type, finder_options)
36 self._local_executable = executable
37 self._flash_path = flash_path
38 self._is_content_shell = is_content_shell
39 self._browser_directory = browser_directory
40 self.is_local_build = is_local_build
43 return 'PossibleDesktopBrowser(browser_type=%s, executable=%s)' % (
44 self.browser_type, self._local_executable)
47 backend = desktop_browser_backend.DesktopBrowserBackend(
48 self.finder_options.browser_options, self._local_executable,
49 self._flash_path, self._is_content_shell, self._browser_directory,
50 output_profile_path=self.finder_options.output_profile_path,
51 extensions_to_load=self.finder_options.extensions_to_load)
52 b = browser.Browser(backend,
53 core_platform.CreatePlatformBackendForCurrentOS())
56 def SupportsOptions(self, finder_options):
57 if (len(finder_options.extensions_to_load) != 0) and self._is_content_shell:
61 def UpdateExecutableIfNeeded(self):
64 def last_modification_time(self):
65 if os.path.exists(self._local_executable):
66 return os.path.getmtime(self._local_executable)
69 def SelectDefaultBrowser(possible_browsers):
70 local_builds_by_date = [
71 b for b in sorted(possible_browsers,
72 key=lambda b: b.last_modification_time())
74 if local_builds_by_date:
75 return local_builds_by_date[-1]
78 def CanFindAvailableBrowsers():
79 return not cros_interface.IsRunningOnCrosDevice()
81 def FindAllAvailableBrowsers(finder_options):
82 """Finds all the desktop browsers available on this machine."""
85 if not CanFindAvailableBrowsers():
89 if (sys.platform.startswith('linux') and
90 os.getenv('DISPLAY') == None):
93 # Look for a browser in the standard chrome build locations.
94 if finder_options.chrome_root:
95 chrome_root = finder_options.chrome_root
97 chrome_root = util.GetChromiumSrcDir()
99 chromium_app_names = []
100 if sys.platform == 'darwin':
101 chromium_app_names.append('Chromium.app/Contents/MacOS/Chromium')
102 chromium_app_names.append('Google Chrome.app/Contents/MacOS/Google Chrome')
103 content_shell_app_name = 'Content Shell.app/Contents/MacOS/Content Shell'
105 if platform.architecture()[0] == '64bit':
107 flash_path = os.path.join(
108 chrome_root, 'third_party', 'adobe', 'flash', 'binaries', 'ppapi',
109 mac_dir, 'PepperFlashPlayer.plugin')
110 elif sys.platform.startswith('linux'):
111 chromium_app_names.append('chrome')
112 content_shell_app_name = 'content_shell'
114 if platform.architecture()[0] == '64bit':
115 linux_dir = 'linux_x64'
116 flash_path = os.path.join(
117 chrome_root, 'third_party', 'adobe', 'flash', 'binaries', 'ppapi',
118 linux_dir, 'libpepflashplayer.so')
119 elif sys.platform.startswith('win'):
120 chromium_app_names.append('chrome.exe')
121 content_shell_app_name = 'content_shell.exe'
123 if platform.architecture()[0] == '64bit':
125 flash_path = os.path.join(
126 chrome_root, 'third_party', 'adobe', 'flash', 'binaries', 'ppapi',
127 win_dir, 'pepflashplayer.dll')
129 raise Exception('Platform not recognized')
131 def IsExecutable(path):
132 return os.path.isfile(path) and os.access(path, os.X_OK)
134 # Add the explicit browser executable if given.
135 if finder_options.browser_executable:
136 normalized_executable = os.path.expanduser(
137 finder_options.browser_executable)
138 if IsExecutable(normalized_executable):
139 browser_directory = os.path.dirname(finder_options.browser_executable)
140 browsers.append(PossibleDesktopBrowser('exact', finder_options,
141 normalized_executable, flash_path,
142 False, browser_directory))
144 logging.warning('%s specified by browser_executable does not exist',
145 normalized_executable)
147 def AddIfFound(browser_type, build_dir, type_dir, app_name, content_shell):
148 browser_directory = os.path.join(chrome_root, build_dir, type_dir)
149 app = os.path.join(browser_directory, app_name)
150 if IsExecutable(app):
151 browsers.append(PossibleDesktopBrowser(browser_type, finder_options,
152 app, flash_path, content_shell,
154 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,
212 app, flash_path, False,
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]))