Upstream version 5.34.92.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 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
17
18 ALL_BROWSER_TYPES = [
19     'exact',
20     'release',
21     'release_x64',
22     'debug',
23     'debug_x64',
24     'canary',
25     'content-shell-debug',
26     'content-shell-debug_x64',
27     'content-shell-release',
28     'content-shell-release_x64',
29     'system']
30
31 class PossibleDesktopBrowser(possible_browser.PossibleBrowser):
32   """A desktop browser that can be controlled."""
33
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,
38         finder_options)
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
46
47   def __repr__(self):
48     return 'PossibleDesktopBrowser(browser_type=%s, executable=%s)' % (
49         self.browser_type, self._local_executable)
50
51   def Create(self):
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())
59     return b
60
61   def SupportsOptions(self, finder_options):
62     if (len(finder_options.extensions_to_load) != 0) and self._is_content_shell:
63       return False
64     return True
65
66   def UpdateExecutableIfNeeded(self):
67     pass
68
69   def last_modification_time(self):
70     if os.path.exists(self._local_executable):
71       return os.path.getmtime(self._local_executable)
72     return -1
73
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())
78       if b.is_local_build]
79   if local_builds_by_date:
80     return local_builds_by_date[-1]
81   return None
82
83 def CanFindAvailableBrowsers():
84   return not cros_interface.IsRunningOnCrosDevice()
85
86 def FindAllAvailableBrowsers(finder_options):
87   """Finds all the desktop browsers available on this machine."""
88   browsers = []
89
90   if not CanFindAvailableBrowsers():
91     return []
92
93   has_display = True
94   if (sys.platform.startswith('linux') and
95       os.getenv('DISPLAY') == None):
96     has_display = False
97
98   # Look for a browser in the standard chrome build locations.
99   if finder_options.chrome_root:
100     chrome_root = finder_options.chrome_root
101   else:
102     chrome_root = util.GetChromiumSrcDir()
103
104   flash_bin_dir = os.path.join(
105       chrome_root, 'third_party', 'adobe', 'flash', 'binaries', 'ppapi')
106
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)
127   else:
128     raise Exception('Platform not recognized')
129
130   def IsExecutable(path):
131     return os.path.isfile(path) and os.access(path, os.X_OK)
132
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))
142     else:
143       logging.warning('%s specified by browser_executable does not exist',
144                       normalized_executable)
145
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))
155       return True
156     return False
157
158   # Add local builds
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)
165
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,
175                                              mac_canary_root))
176
177     if IsExecutable(mac_system):
178       browsers.append(PossibleDesktopBrowser('system', finder_options,
179                                              mac_system, None, False,
180                                              mac_system_root))
181
182   # Linux specific options.
183   if sys.platform.startswith('linux'):
184     # Look for a google-chrome instance.
185     found = False
186     try:
187       with open(os.devnull, 'w') as devnull:
188         found = subprocess.call(['google-chrome', '--version'],
189                                 stdout=devnull, stderr=devnull) == 0
190     except OSError:
191       pass
192     if found:
193       browsers.append(PossibleDesktopBrowser('system', finder_options,
194                                              'google-chrome', None, False,
195                                              '/opt/google/chrome'))
196
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')
201
202     win_search_paths = [os.getenv('PROGRAMFILES(X86)'),
203                         os.getenv('PROGRAMFILES'),
204                         os.getenv('LOCALAPPDATA')]
205
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, None, False,
213                                                  browser_directory))
214         return True
215       return False
216
217     for path in win_search_paths:
218       if not path:
219         continue
220       if AddIfFoundWin('canary', canary_path):
221         break
222
223     for path in win_search_paths:
224       if not path:
225         continue
226       if AddIfFoundWin('system', system_path):
227         break
228
229   if len(browsers) and not has_display:
230     logging.warning(
231       'Found (%s), but you do not have a DISPLAY environment set.' %
232       ','.join([b.browser_type for b in browsers]))
233     return []
234
235   return browsers