chrome.py revision 43651acdf736d03f2591acef9893c029e8b149d6
1# Copyright (c) 2013 The Chromium OS 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 5import logging, os 6 7from autotest_lib.client.cros import constants 8from autotest_lib.client.bin import utils 9from telemetry.core import cros_interface, exceptions, util 10from telemetry.internal.browser import browser_finder, browser_options 11from telemetry.internal.browser import extension_to_load 12 13Error = exceptions.Error 14 15 16# Cached result of whether ARC is available on current device. 17_arc_available = None 18 19 20def _is_arc_available(): 21 """Returns true if ARC is available on current device.""" 22 global _arc_available 23 if _arc_available is not None: 24 return _arc_available 25 26 def _check_lsb_release(): 27 lsb_release = '/etc/lsb-release' 28 if not os.path.exists(lsb_release): 29 return False 30 with open(lsb_release) as f: 31 for line in f: 32 if line.startswith('CHROMEOS_ARC_VERSION='): 33 return True 34 return False 35 36 _arc_available = _check_lsb_release() 37 return _arc_available 38 39 40if _is_arc_available(): 41 from autotest_lib.client.common_lib.cros import arc_util 42 43 44class Chrome(object): 45 """Wrapper for creating a telemetry browser instance with extensions.""" 46 47 48 BROWSER_TYPE_LOGIN = 'system' 49 BROWSER_TYPE_GUEST = 'system-guest' 50 51 52 def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False, 53 is_component=True, num_tries=3, extra_browser_args=None, 54 clear_enterprise_policy=True, dont_override_profile=False, 55 disable_gaia_services=True, disable_default_apps = True, 56 auto_login=True, gaia_login=False, 57 username=None, password=None, gaia_id=None, 58 arc_mode=None): 59 """ 60 Constructor of telemetry wrapper. 61 62 @param logged_in: Regular user (True) or guest user (False). 63 @param extension_paths: path of unpacked extension to install. 64 @param autotest_ext: Load a component extension with privileges to 65 invoke chrome.autotestPrivate. 66 @param is_component: Whether extensions should be loaded as component 67 extensions. 68 @param num_tries: Number of attempts to log in. 69 @param extra_browser_args: Additional argument(s) to pass to the 70 browser. It can be a string or a list. 71 @param clear_enterprise_policy: Clear enterprise policy before 72 logging in. 73 @param dont_override_profile: Don't delete cryptohome before login. 74 Telemetry will output a warning with this 75 option. 76 @param disable_gaia_services: For enterprise autotests, this option may 77 be used to enable policy fetch. 78 @param disable_default_apps: For tests that exercise default apps. 79 @param auto_login: Does not login automatically if this is False. 80 Useful if you need to examine oobe. 81 @param gaia_login: Logs in to real gaia. 82 @param username: Log in using this username instead of the default. 83 @param password: Log in using this password instead of the default. 84 @param gaia_id: Log in using this gaia_id instead of the default. 85 @param arc_mode: How ARC instance should be started. Default is to not 86 start. 87 """ 88 self._autotest_ext_path = None 89 if autotest_ext: 90 self._autotest_ext_path = os.path.join(os.path.dirname(__file__), 91 'autotest_private_ext') 92 extension_paths.append(self._autotest_ext_path) 93 94 finder_options = browser_options.BrowserFinderOptions() 95 if _is_arc_available() and arc_util.should_start_arc(arc_mode): 96 # TODO(achuith): Fix extra_browser_args, so that appending the 97 # following flags to it is simpler. 98 finder_options.browser_options.AppendExtraBrowserArgs( 99 arc_util.get_extra_chrome_flags()) 100 logged_in = True 101 102 self._browser_type = (self.BROWSER_TYPE_LOGIN 103 if logged_in else self.BROWSER_TYPE_GUEST) 104 finder_options.browser_type = self.browser_type 105 if extra_browser_args: 106 finder_options.browser_options.AppendExtraBrowserArgs( 107 extra_browser_args) 108 109 # TODO(achuith): Remove this after PFQ revs. crbug.com/603169. 110 if logged_in: 111 try: 112 extensions_to_load = finder_options.extensions_to_load 113 for path in extension_paths: 114 extension = extension_to_load.ExtensionToLoad( 115 path, self.browser_type, is_component=is_component) 116 extensions_to_load.append(extension) 117 self._extensions_to_load = extensions_to_load 118 except AttributeError: 119 pass 120 121 # finder options must be set before parse_args(), browser options must 122 # be set before Create(). 123 # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit 124 # autotest debug logs 125 finder_options.verbosity = 2 126 finder_options.CreateParser().parse_args(args=[]) 127 b_options = finder_options.browser_options 128 b_options.disable_component_extensions_with_background_pages = False 129 b_options.create_browser_with_oobe = True 130 b_options.clear_enterprise_policy = clear_enterprise_policy 131 b_options.dont_override_profile = dont_override_profile 132 b_options.disable_gaia_services = disable_gaia_services 133 b_options.disable_default_apps = disable_default_apps 134 b_options.disable_component_extensions_with_background_pages = disable_default_apps 135 136 b_options.auto_login = auto_login 137 b_options.gaia_login = gaia_login 138 self.username = b_options.username if username is None else username 139 self.password = b_options.password if password is None else password 140 b_options.username = self.username 141 b_options.password = self.password 142 # gaia_id will be added to telemetry code in chromium repository later 143 try: 144 self.gaia_id = b_options.gaia_id if gaia_id is None else gaia_id 145 b_options.gaia_id = self.gaia_id 146 except AttributeError: 147 pass 148 149 if logged_in: 150 try: 151 extensions_to_load = b_options.extensions_to_load 152 for path in extension_paths: 153 extension = extension_to_load.ExtensionToLoad( 154 path, self.browser_type, is_component=is_component) 155 extensions_to_load.append(extension) 156 self._extensions_to_load = extensions_to_load 157 except AttributeError: 158 pass 159 160 # Turn on collection of Chrome coredumps via creation of a magic file. 161 # (Without this, Chrome coredumps are trashed.) 162 open(constants.CHROME_CORE_MAGIC_FILE, 'w').close() 163 164 for i in range(num_tries): 165 try: 166 browser_to_create = browser_finder.FindBrowser(finder_options) 167 self._browser = browser_to_create.Create(finder_options) 168 if _is_arc_available(): 169 arc_util.post_processing_after_browser(arc_mode) 170 break 171 except (exceptions.LoginException) as e: 172 logging.error('Timed out logging in, tries=%d, error=%s', 173 i, repr(e)) 174 if i == num_tries-1: 175 raise 176 177 178 def __enter__(self): 179 return self 180 181 182 def __exit__(self, *args): 183 self.close() 184 185 186 @property 187 def browser(self): 188 """Returns a telemetry browser instance.""" 189 return self._browser 190 191 192 def get_extension(self, extension_path): 193 """Fetches a telemetry extension instance given the extension path.""" 194 for ext in self._extensions_to_load: 195 if extension_path == ext.path: 196 return self.browser.extensions[ext] 197 return None 198 199 200 @property 201 def autotest_ext(self): 202 """Returns the autotest extension.""" 203 return self.get_extension(self._autotest_ext_path) 204 205 206 @property 207 def login_status(self): 208 """Returns login status.""" 209 ext = self.autotest_ext 210 if not ext: 211 return None 212 213 ext.ExecuteJavaScript(''' 214 window.__login_status = null; 215 chrome.autotestPrivate.loginStatus(function(s) { 216 window.__login_status = s; 217 }); 218 ''') 219 return ext.EvaluateJavaScript('window.__login_status') 220 221 222 def get_visible_notifications(self): 223 """Returns an array of visible notifications of Chrome. 224 225 For specific type of each notification, please refer to Chromium's 226 chrome/common/extensions/api/autotest_private.idl. 227 """ 228 ext = self.autotest_ext 229 if not ext: 230 return None 231 232 ext.ExecuteJavaScript(''' 233 window.__items = null; 234 chrome.autotestPrivate.getVisibleNotifications(function(items) { 235 window.__items = items; 236 }); 237 ''') 238 if ext.EvaluateJavaScript('window.__items') is None: 239 return None 240 return ext.EvaluateJavaScript('window.__items') 241 242 243 @property 244 def browser_type(self): 245 """Returns the browser_type.""" 246 return self._browser_type 247 248 249 @staticmethod 250 def did_browser_crash(func): 251 """Runs func, returns True if the browser crashed, False otherwise. 252 253 @param func: function to run. 254 255 """ 256 try: 257 func() 258 except (Error): 259 return True 260 return False 261 262 263 @staticmethod 264 def wait_for_browser_restart(func): 265 """Runs func, and waits for a browser restart. 266 267 @param func: function to run. 268 269 """ 270 _cri = cros_interface.CrOSInterface() 271 pid = _cri.GetChromePid() 272 Chrome.did_browser_crash(func) 273 utils.poll_for_condition(lambda: pid != _cri.GetChromePid(), timeout=60) 274 275 276 def wait_for_browser_to_come_up(self): 277 """Waits for the browser to come up. This should only be called after a 278 browser crash. 279 """ 280 def _BrowserReady(cr): 281 tabs = [] # Wrapper for pass by reference. 282 if self.did_browser_crash( 283 lambda: tabs.append(cr.browser.tabs.New())): 284 return False 285 try: 286 tabs[0].Close() 287 except: 288 # crbug.com/350941 289 logging.error('Timed out closing tab') 290 return True 291 util.WaitFor(lambda: _BrowserReady(self), timeout=10) 292 293 294 def close(self): 295 """Closes the browser.""" 296 try: 297 if _is_arc_available(): 298 arc_util.pre_processing_before_close() 299 finally: 300 self._browser.Close() 301