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