blob: 20c331aa1b565db94507ab233a5598e2ea916551 [file] [log] [blame]
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -07001# 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 Bhandarkara2bceaf2014-11-14 12:10:16 -08005import logging, os
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -07006
Don Garrett11b13972015-09-11 17:37:53 +00007from autotest_lib.client.cros import constants
Achuith Bhandarkar88a2bab2015-07-09 17:36:25 -07008from autotest_lib.client.bin import utils
Prathmesh Prabhuc3961df2015-07-21 10:44:54 -07009from telemetry.core import cros_interface, exceptions, util
Achuith Bhandarkarb058a8f2015-07-02 01:29:05 -070010from telemetry.internal.browser import browser_finder, browser_options
11from telemetry.internal.browser import extension_to_load
Achuith Bhandarkar704134d2015-03-05 17:31:57 -080012
13Error = exceptions.Error
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -070014
15
Victor Hsieh43651ac2016-04-20 12:52:21 -070016# Cached result of whether ARC is available on current device.
17_arc_available = None
18
19
20def _is_arc_available():
Victor Hsieh000240b2016-04-11 16:04:31 -070021 """Returns true if ARC is available on current device."""
Victor Hsieh43651ac2016-04-20 12:52:21 -070022 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
Victor Hsieh000240b2016-04-11 16:04:31 -070042
43
Achuith Bhandarkared498932013-07-16 17:01:40 -070044class Chrome(object):
45 """Wrapper for creating a telemetry browser instance with extensions."""
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070046
47
Achuith Bhandarkared498932013-07-16 17:01:40 -070048 BROWSER_TYPE_LOGIN = 'system'
49 BROWSER_TYPE_GUEST = 'system-guest'
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070050
51
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080052 def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False,
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070053 is_component=True, num_tries=3, extra_browser_args=None,
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070054 clear_enterprise_policy=True, dont_override_profile=False,
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080055 disable_gaia_services=True, disable_default_apps = True,
56 auto_login=True, gaia_login=False,
Victor Hsieh000240b2016-04-11 16:04:31 -070057 username=None, password=None, gaia_id=None,
Victor Hsieh43651ac2016-04-20 12:52:21 -070058 arc_mode=None):
Dean Liaob12e2ee2013-11-19 16:49:12 +080059 """
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080060 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.
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070068 @param num_tries: Number of attempts to log in.
Dean Liaob12e2ee2013-11-19 16:49:12 +080069 @param extra_browser_args: Additional argument(s) to pass to the
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070070 browser. It can be a string or a list.
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -070071 @param clear_enterprise_policy: Clear enterprise policy before
72 logging in.
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070073 @param dont_override_profile: Don't delete cryptohome before login.
74 Telemetry will output a warning with this
75 option.
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070076 @param disable_gaia_services: For enterprise autotests, this option may
77 be used to enable policy fetch.
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080078 @param disable_default_apps: For tests that exercise default apps.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080079 @param auto_login: Does not login automatically if this is False.
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070080 Useful if you need to examine oobe.
81 @param gaia_login: Logs in to real gaia.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080082 @param username: Log in using this username instead of the default.
Alexander Alekseevd9682862015-10-01 22:42:49 -070083 @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.
Victor Hsieh43651ac2016-04-20 12:52:21 -070085 @param arc_mode: How ARC instance should be started. Default is to not
86 start.
Dean Liaob12e2ee2013-11-19 16:49:12 +080087 """
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070088 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
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070094 finder_options = browser_options.BrowserFinderOptions()
Victor Hsieh43651ac2016-04-20 12:52:21 -070095 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
Achuith Bhandarkared498932013-07-16 17:01:40 -0700102 self._browser_type = (self.BROWSER_TYPE_LOGIN
103 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -0700104 finder_options.browser_type = self.browser_type
Dean Liaob12e2ee2013-11-19 16:49:12 +0800105 if extra_browser_args:
106 finder_options.browser_options.AppendExtraBrowserArgs(
107 extra_browser_args)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -0700108
Achuith Bhandarkar6fb93872016-04-19 21:14:56 +0000109 # 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
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -0700121 # finder options must be set before parse_args(), browser options must
122 # be set before Create().
Todd Brocheb6f4812014-04-29 16:04:28 -0700123 # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit
124 # autotest debug logs
125 finder_options.verbosity = 2
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +0200126 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700127 b_options = finder_options.browser_options
128 b_options.disable_component_extensions_with_background_pages = False
129 b_options.create_browser_with_oobe = True
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -0700130 b_options.clear_enterprise_policy = clear_enterprise_policy
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -0700131 b_options.dont_override_profile = dont_override_profile
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -0700132 b_options.disable_gaia_services = disable_gaia_services
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -0800133 b_options.disable_default_apps = disable_default_apps
134 b_options.disable_component_extensions_with_background_pages = disable_default_apps
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -0800135
136 b_options.auto_login = auto_login
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -0700137 b_options.gaia_login = gaia_login
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -0800138 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
Achuith Bhandarkar6fb93872016-04-19 21:14:56 +0000142 # 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
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700148
Shuhei Takahashi8462b882016-05-31 17:27:32 +0900149 self.arc_mode = arc_mode
150
Achuith Bhandarkar531a62a2016-04-13 11:09:43 -0700151 if logged_in:
Achuith Bhandarkar6fb93872016-04-19 21:14:56 +0000152 try:
153 extensions_to_load = b_options.extensions_to_load
154 for path in extension_paths:
155 extension = extension_to_load.ExtensionToLoad(
156 path, self.browser_type, is_component=is_component)
157 extensions_to_load.append(extension)
158 self._extensions_to_load = extensions_to_load
159 except AttributeError:
160 pass
Achuith Bhandarkar531a62a2016-04-13 11:09:43 -0700161
David James54a830e2015-05-06 17:21:47 -0700162 # Turn on collection of Chrome coredumps via creation of a magic file.
163 # (Without this, Chrome coredumps are trashed.)
Don Garrett11b13972015-09-11 17:37:53 +0000164 open(constants.CHROME_CORE_MAGIC_FILE, 'w').close()
David James54a830e2015-05-06 17:21:47 -0700165
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800166 for i in range(num_tries):
167 try:
168 browser_to_create = browser_finder.FindBrowser(finder_options)
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -0800169 self._browser = browser_to_create.Create(finder_options)
Victor Hsieh43651ac2016-04-20 12:52:21 -0700170 if _is_arc_available():
Shuhei Takahashi8462b882016-05-31 17:27:32 +0900171 arc_util.post_processing_after_browser(self)
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800172 break
Achuith Bhandarkarfab82d12015-02-26 11:05:35 -0800173 except (exceptions.LoginException) as e:
Achuith Bhandarkarf800edf2013-12-06 13:37:52 -0800174 logging.error('Timed out logging in, tries=%d, error=%s',
175 i, repr(e))
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800176 if i == num_tries-1:
177 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -0700178
179
180 def __enter__(self):
181 return self
182
183
184 def __exit__(self, *args):
Derek Basehore41acbf82014-07-11 18:34:30 -0700185 self.close()
Achuith Bhandarkared498932013-07-16 17:01:40 -0700186
187
188 @property
189 def browser(self):
190 """Returns a telemetry browser instance."""
191 return self._browser
192
193
194 def get_extension(self, extension_path):
195 """Fetches a telemetry extension instance given the extension path."""
196 for ext in self._extensions_to_load:
197 if extension_path == ext.path:
198 return self.browser.extensions[ext]
199 return None
200
201
202 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700203 def autotest_ext(self):
204 """Returns the autotest extension."""
205 return self.get_extension(self._autotest_ext_path)
206
207
208 @property
209 def login_status(self):
210 """Returns login status."""
211 ext = self.autotest_ext
212 if not ext:
213 return None
214
215 ext.ExecuteJavaScript('''
216 window.__login_status = null;
217 chrome.autotestPrivate.loginStatus(function(s) {
218 window.__login_status = s;
219 });
220 ''')
221 return ext.EvaluateJavaScript('window.__login_status')
222
223
Victor Hsiehfc3417e2016-03-25 16:49:59 -0700224 def get_visible_notifications(self):
225 """Returns an array of visible notifications of Chrome.
226
227 For specific type of each notification, please refer to Chromium's
228 chrome/common/extensions/api/autotest_private.idl.
229 """
230 ext = self.autotest_ext
231 if not ext:
232 return None
233
234 ext.ExecuteJavaScript('''
235 window.__items = null;
236 chrome.autotestPrivate.getVisibleNotifications(function(items) {
237 window.__items = items;
238 });
239 ''')
240 if ext.EvaluateJavaScript('window.__items') is None:
241 return None
242 return ext.EvaluateJavaScript('window.__items')
243
244
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700245 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700246 def browser_type(self):
247 """Returns the browser_type."""
248 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700249
250
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800251 @staticmethod
252 def did_browser_crash(func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700253 """Runs func, returns True if the browser crashed, False otherwise.
254
255 @param func: function to run.
256
257 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700258 try:
259 func()
Achuith Bhandarkar704134d2015-03-05 17:31:57 -0800260 except (Error):
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700261 return True
262 return False
Derek Basehore41acbf82014-07-11 18:34:30 -0700263
264
Achuith Bhandarkar88a2bab2015-07-09 17:36:25 -0700265 @staticmethod
266 def wait_for_browser_restart(func):
267 """Runs func, and waits for a browser restart.
268
269 @param func: function to run.
270
271 """
272 _cri = cros_interface.CrOSInterface()
273 pid = _cri.GetChromePid()
274 Chrome.did_browser_crash(func)
275 utils.poll_for_condition(lambda: pid != _cri.GetChromePid(), timeout=60)
276
277
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800278 def wait_for_browser_to_come_up(self):
279 """Waits for the browser to come up. This should only be called after a
280 browser crash.
281 """
282 def _BrowserReady(cr):
283 tabs = [] # Wrapper for pass by reference.
284 if self.did_browser_crash(
285 lambda: tabs.append(cr.browser.tabs.New())):
286 return False
287 try:
288 tabs[0].Close()
Achuith Bhandarkarfab82d12015-02-26 11:05:35 -0800289 except:
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800290 # crbug.com/350941
291 logging.error('Timed out closing tab')
292 return True
293 util.WaitFor(lambda: _BrowserReady(self), timeout=10)
294
295
Derek Basehore41acbf82014-07-11 18:34:30 -0700296 def close(self):
297 """Closes the browser."""
Hidehiko Abe202ed322016-04-14 15:08:43 +0900298 try:
Victor Hsieh43651ac2016-04-20 12:52:21 -0700299 if _is_arc_available():
Shuhei Takahashi8462b882016-05-31 17:27:32 +0900300 arc_util.pre_processing_before_close(self)
Hidehiko Abe202ed322016-04-14 15:08:43 +0900301 finally:
302 self._browser.Close()