| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 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 possible_browser | |
| 13 from telemetry.core.chrome import desktop_browser_backend | |
| 14 from telemetry.core.chrome import platform | |
| 15 | |
| 16 ALL_BROWSER_TYPES = ','.join([ | |
| 17 'exact', | |
| 18 'release', | |
| 19 'debug', | |
| 20 'canary', | |
| 21 'content-shell-debug', | |
| 22 'content-shell-release', | |
| 23 'system']) | |
| 24 | |
| 25 class PossibleDesktopBrowser(possible_browser.PossibleBrowser): | |
| 26 """A desktop browser that can be controlled.""" | |
| 27 | |
| 28 def __init__(self, browser_type, options, executable, is_content_shell): | |
| 29 super(PossibleDesktopBrowser, self).__init__(browser_type, options) | |
| 30 self._local_executable = executable | |
| 31 self._is_content_shell = is_content_shell | |
| 32 | |
| 33 def __repr__(self): | |
| 34 return 'PossibleDesktopBrowser(browser_type=%s)' % self.browser_type | |
| 35 | |
| 36 def Create(self): | |
| 37 backend = desktop_browser_backend.DesktopBrowserBackend( | |
| 38 self._options, self._local_executable, self._is_content_shell) | |
| 39 b = browser.Browser(backend, platform.Platform()) | |
| 40 backend.SetBrowser(b) | |
| 41 return b | |
| 42 | |
| 43 def SupportsOptions(self, options): | |
| 44 if (len(options.extensions_to_load) != 0) and self._is_content_shell: | |
| 45 return False | |
| 46 return True | |
| 47 | |
| 48 def FindAllAvailableBrowsers(options): | |
| 49 """Finds all the desktop browsers available on this machine.""" | |
| 50 browsers = [] | |
| 51 | |
| 52 has_display = True | |
| 53 if (sys.platform.startswith('linux') and | |
| 54 os.getenv('DISPLAY') == None): | |
| 55 has_display = False | |
| 56 | |
| 57 # Add the explicit browser executable if given. | |
| 58 if options.browser_executable: | |
| 59 normalized_executable = os.path.expanduser(options.browser_executable) | |
| 60 if os.path.exists(normalized_executable): | |
| 61 browsers.append(PossibleDesktopBrowser('exact', options, | |
| 62 normalized_executable, False)) | |
| 63 else: | |
| 64 logging.warning('%s specified by browser_executable does not exist', | |
| 65 normalized_executable) | |
| 66 | |
| 67 # Look for a browser in the standard chrome build locations. | |
| 68 if options.chrome_root: | |
| 69 chrome_root = options.chrome_root | |
| 70 else: | |
| 71 chrome_root = os.path.join(os.path.dirname(__file__), | |
| 72 '..', '..', '..', '..', '..') | |
| 73 | |
| 74 if sys.platform == 'darwin': | |
| 75 chromium_app_name = 'Chromium.app/Contents/MacOS/Chromium' | |
| 76 content_shell_app_name = 'Content Shell.app/Contents/MacOS/Content Shell' | |
| 77 elif sys.platform.startswith('linux'): | |
| 78 chromium_app_name = 'chrome' | |
| 79 content_shell_app_name = 'content_shell' | |
| 80 elif sys.platform.startswith('win'): | |
| 81 chromium_app_name = 'chrome.exe' | |
| 82 content_shell_app_name = 'content_shell.exe' | |
| 83 else: | |
| 84 raise Exception('Platform not recognized') | |
| 85 | |
| 86 build_dirs = ['build', | |
| 87 'out', | |
| 88 'sconsbuild', | |
| 89 'xcodebuild'] | |
| 90 | |
| 91 def AddIfFound(browser_type, type_dir, app_name, content_shell): | |
| 92 for build_dir in build_dirs: | |
| 93 app = os.path.join(chrome_root, build_dir, type_dir, app_name) | |
| 94 if os.path.exists(app): | |
| 95 browsers.append(PossibleDesktopBrowser(browser_type, options, | |
| 96 app, content_shell)) | |
| 97 return True | |
| 98 return False | |
| 99 | |
| 100 # Add local builds | |
| 101 AddIfFound('debug', 'Debug', chromium_app_name, False) | |
| 102 AddIfFound('content-shell-debug', 'Debug', content_shell_app_name, True) | |
| 103 AddIfFound('release', 'Release', chromium_app_name, False) | |
| 104 AddIfFound('content-shell-release', 'Release', content_shell_app_name, True) | |
| 105 | |
| 106 # Mac-specific options. | |
| 107 if sys.platform == 'darwin': | |
| 108 mac_canary = ('/Applications/Google Chrome Canary.app/' | |
| 109 'Contents/MacOS/Google Chrome Canary') | |
| 110 mac_system = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' | |
| 111 if os.path.exists(mac_canary): | |
| 112 browsers.append(PossibleDesktopBrowser('canary', options, | |
| 113 mac_canary, False)) | |
| 114 | |
| 115 if os.path.exists(mac_system): | |
| 116 browsers.append(PossibleDesktopBrowser('system', options, | |
| 117 mac_system, False)) | |
| 118 | |
| 119 # Linux specific options. | |
| 120 if sys.platform.startswith('linux'): | |
| 121 # Look for a google-chrome instance. | |
| 122 found = False | |
| 123 try: | |
| 124 with open(os.devnull, 'w') as devnull: | |
| 125 found = subprocess.call(['google-chrome', '--version'], | |
| 126 stdout=devnull, stderr=devnull) == 0 | |
| 127 except OSError: | |
| 128 pass | |
| 129 if found: | |
| 130 browsers.append( | |
| 131 PossibleDesktopBrowser('system', options, 'google-chrome', False)) | |
| 132 | |
| 133 # Win32-specific options. | |
| 134 if sys.platform.startswith('win'): | |
| 135 system_path = os.path.join('Google', 'Chrome', 'Application') | |
| 136 canary_path = os.path.join('Google', 'Chrome SxS', 'Application') | |
| 137 | |
| 138 win_search_paths = [os.getenv('PROGRAMFILES(X86)'), | |
| 139 os.getenv('PROGRAMFILES'), | |
| 140 os.getenv('LOCALAPPDATA')] | |
| 141 | |
| 142 for path in win_search_paths: | |
| 143 if not path: | |
| 144 continue | |
| 145 if AddIfFound('canary', os.path.join(path, canary_path), | |
| 146 chromium_app_name, False): | |
| 147 break | |
| 148 | |
| 149 for path in win_search_paths: | |
| 150 if not path: | |
| 151 continue | |
| 152 if AddIfFound('system', os.path.join(path, system_path), | |
| 153 chromium_app_name, False): | |
| 154 break | |
| 155 | |
| 156 if len(browsers) and not has_display: | |
| 157 logging.warning( | |
| 158 'Found (%s), but you do not have a DISPLAY environment set.' % | |
| 159 ','.join([b.browser_type for b in browsers])) | |
| 160 return [] | |
| 161 | |
| 162 return browsers | |
| OLD | NEW |