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