Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / backends / chrome / desktop_browser_finder.py
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."""
5
6 import logging
7 import os
8 import subprocess
9 import sys
10
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
16
17
18 class PossibleDesktopBrowser(possible_browser.PossibleBrowser):
19   """A desktop browser that can be controlled."""
20
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' %
28         browser_type)
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
34
35   def __repr__(self):
36     return 'PossibleDesktopBrowser(type=%s, executable=%s, flash=%s)' % (
37         self.browser_type, self._local_executable, self._flash_path)
38
39   def _InitPlatformIfNeeded(self):
40     if self._platform:
41       return
42
43     self._platform = platform_module.GetHostPlatform()
44
45     # pylint: disable=W0212
46     self._platform_backend = self._platform._platform_backend
47
48   def Create(self, finder_options):
49     if self._flash_path and not os.path.exists(self._flash_path):
50       logging.warning(
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',
53           self._flash_path)
54       self._flash_path = None
55
56     self._InitPlatformIfNeeded()
57
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)
66
67   def SupportsOptions(self, finder_options):
68     if (len(finder_options.extensions_to_load) != 0) and self._is_content_shell:
69       return False
70     return True
71
72   def UpdateExecutableIfNeeded(self):
73     pass
74
75   def last_modification_time(self):
76     if os.path.exists(self._local_executable):
77       return os.path.getmtime(self._local_executable)
78     return -1
79
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())
84       if b.is_local_build]
85   if local_builds_by_date:
86     return local_builds_by_date[-1]
87   return None
88
89 def CanFindAvailableBrowsers():
90   return not platform_module.GetHostPlatform().GetOSName() == 'chromeos'
91
92 def FindAllBrowserTypes(_):
93   return [
94       'exact',
95       'reference',
96       'release',
97       'release_x64',
98       'debug',
99       'debug_x64',
100       'canary',
101       'content-shell-debug',
102       'content-shell-debug_x64',
103       'content-shell-release',
104       'content-shell-release_x64',
105       'system']
106
107 def FindAllAvailableBrowsers(finder_options):
108   """Finds all the desktop browsers available on this machine."""
109   browsers = []
110
111   if not CanFindAvailableBrowsers():
112     return []
113
114   has_display = True
115   if (sys.platform.startswith('linux') and
116       os.getenv('DISPLAY') == None):
117     has_display = False
118
119   # Look for a browser in the standard chrome build locations.
120   if finder_options.chrome_root:
121     chrome_root = finder_options.chrome_root
122   else:
123     chrome_root = path.GetChromiumSrcDir()
124
125   flash_bin_dir = os.path.join(
126       chrome_root, 'third_party', 'adobe', 'flash', 'binaries', 'ppapi')
127
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)
148   else:
149     raise Exception('Platform not recognized')
150
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))
160     else:
161       raise Exception('%s specified by --browser-executable does not exist',
162                       normalized_executable)
163
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))
173       return True
174     return False
175
176   # Add local builds
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)
183
184   reference_build_root = os.path.join(
185      chrome_root, 'chrome', 'tools', 'test', 'reference_build')
186
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,
198                                              mac_canary_root))
199
200     if path.IsExecutable(mac_system):
201       browsers.append(PossibleDesktopBrowser('system', finder_options,
202                                              mac_system, None, False,
203                                              mac_system_root))
204     if path.IsExecutable(mac_reference):
205       browsers.append(PossibleDesktopBrowser('reference', finder_options,
206                                              mac_reference, None, False,
207                                              mac_reference_root))
208
209   # Linux specific options.
210   if sys.platform.startswith('linux'):
211     # Look for a google-chrome instance.
212     found = False
213     try:
214       with open(os.devnull, 'w') as devnull:
215         found = subprocess.call(['google-chrome', '--version'],
216                                 stdout=devnull, stderr=devnull) == 0
217     except OSError:
218       pass
219     if found:
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))
229
230   # Win32-specific options.
231   if sys.platform.startswith('win'):
232     app_paths = (
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')),
236     )
237
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)
242         if app_path:
243           browsers.append(PossibleDesktopBrowser(
244               browser_name, finder_options, app_path,
245               None, False, os.path.dirname(app_path)))
246
247   if len(browsers) and not has_display:
248     logging.warning(
249       'Found (%s), but you do not have a DISPLAY environment set.' %
250       ','.join([b.browser_type for b in browsers]))
251     return []
252
253   return browsers