blob: 4acc8bef4e5697b83e4bf6505d4d2f4587dbd952 [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 Bhandarkarf3469ec2016-06-08 16:58:57 -07005import logging, os, re
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 Bhandarkarf3469ec2016-06-08 16:58:57 -070044def 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
Achuith Bhandarkared498932013-07-16 17:01:40 -070059class Chrome(object):
60 """Wrapper for creating a telemetry browser instance with extensions."""
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070061
62
Achuith Bhandarkared498932013-07-16 17:01:40 -070063 BROWSER_TYPE_LOGIN = 'system'
64 BROWSER_TYPE_GUEST = 'system-guest'
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070065
66
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080067 def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False,
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070068 is_component=True, num_tries=3, extra_browser_args=None,
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070069 clear_enterprise_policy=True, dont_override_profile=False,
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080070 disable_gaia_services=True, disable_default_apps = True,
71 auto_login=True, gaia_login=False,
Victor Hsieh000240b2016-04-11 16:04:31 -070072 username=None, password=None, gaia_id=None,
Victor Hsieh43651ac2016-04-20 12:52:21 -070073 arc_mode=None):
Dean Liaob12e2ee2013-11-19 16:49:12 +080074 """
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080075 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.
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070083 @param num_tries: Number of attempts to log in.
Dean Liaob12e2ee2013-11-19 16:49:12 +080084 @param extra_browser_args: Additional argument(s) to pass to the
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070085 browser. It can be a string or a list.
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -070086 @param clear_enterprise_policy: Clear enterprise policy before
87 logging in.
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070088 @param dont_override_profile: Don't delete cryptohome before login.
89 Telemetry will output a warning with this
90 option.
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070091 @param disable_gaia_services: For enterprise autotests, this option may
92 be used to enable policy fetch.
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080093 @param disable_default_apps: For tests that exercise default apps.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080094 @param auto_login: Does not login automatically if this is False.
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070095 Useful if you need to examine oobe.
96 @param gaia_login: Logs in to real gaia.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080097 @param username: Log in using this username instead of the default.
Alexander Alekseevd9682862015-10-01 22:42:49 -070098 @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.
Victor Hsieh43651ac2016-04-20 12:52:21 -0700100 @param arc_mode: How ARC instance should be started. Default is to not
101 start.
Dean Liaob12e2ee2013-11-19 16:49:12 +0800102 """
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700103 self._autotest_ext_path = None
104 if autotest_ext:
105 self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
106 'autotest_private_ext')
107 extension_paths.append(self._autotest_ext_path)
108
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700109 finder_options = browser_options.BrowserFinderOptions()
Victor Hsieh43651ac2016-04-20 12:52:21 -0700110 if _is_arc_available() and arc_util.should_start_arc(arc_mode):
111 # TODO(achuith): Fix extra_browser_args, so that appending the
112 # following flags to it is simpler.
113 finder_options.browser_options.AppendExtraBrowserArgs(
114 arc_util.get_extra_chrome_flags())
115 logged_in = True
116
Achuith Bhandarkared498932013-07-16 17:01:40 -0700117 self._browser_type = (self.BROWSER_TYPE_LOGIN
118 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -0700119 finder_options.browser_type = self.browser_type
Dean Liaob12e2ee2013-11-19 16:49:12 +0800120 if extra_browser_args:
121 finder_options.browser_options.AppendExtraBrowserArgs(
122 extra_browser_args)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -0700123
Achuith Bhandarkar6fb93872016-04-19 21:14:56 +0000124 # TODO(achuith): Remove this after PFQ revs. crbug.com/603169.
125 if logged_in:
126 try:
127 extensions_to_load = finder_options.extensions_to_load
128 for path in extension_paths:
129 extension = extension_to_load.ExtensionToLoad(
130 path, self.browser_type, is_component=is_component)
131 extensions_to_load.append(extension)
132 self._extensions_to_load = extensions_to_load
133 except AttributeError:
134 pass
135
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -0700136 # finder options must be set before parse_args(), browser options must
137 # be set before Create().
Todd Brocheb6f4812014-04-29 16:04:28 -0700138 # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit
139 # autotest debug logs
140 finder_options.verbosity = 2
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +0200141 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700142 b_options = finder_options.browser_options
143 b_options.disable_component_extensions_with_background_pages = False
144 b_options.create_browser_with_oobe = True
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -0700145 b_options.clear_enterprise_policy = clear_enterprise_policy
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -0700146 b_options.dont_override_profile = dont_override_profile
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -0700147 b_options.disable_gaia_services = disable_gaia_services
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -0800148 b_options.disable_default_apps = disable_default_apps
149 b_options.disable_component_extensions_with_background_pages = disable_default_apps
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -0800150
151 b_options.auto_login = auto_login
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -0700152 b_options.gaia_login = gaia_login
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -0800153 self.username = b_options.username if username is None else username
154 self.password = b_options.password if password is None else password
Achuith Bhandarkarf3469ec2016-06-08 16:58:57 -0700155 self.username = NormalizeEmail(self.username)
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -0800156 b_options.username = self.username
157 b_options.password = self.password
Achuith Bhandarkar6fb93872016-04-19 21:14:56 +0000158 # gaia_id will be added to telemetry code in chromium repository later
159 try:
160 self.gaia_id = b_options.gaia_id if gaia_id is None else gaia_id
161 b_options.gaia_id = self.gaia_id
162 except AttributeError:
163 pass
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700164
Shuhei Takahashi8462b882016-05-31 17:27:32 +0900165 self.arc_mode = arc_mode
166
Achuith Bhandarkar531a62a2016-04-13 11:09:43 -0700167 if logged_in:
Achuith Bhandarkar6fb93872016-04-19 21:14:56 +0000168 try:
169 extensions_to_load = b_options.extensions_to_load
170 for path in extension_paths:
171 extension = extension_to_load.ExtensionToLoad(
172 path, self.browser_type, is_component=is_component)
173 extensions_to_load.append(extension)
174 self._extensions_to_load = extensions_to_load
175 except AttributeError:
176 pass
Achuith Bhandarkar531a62a2016-04-13 11:09:43 -0700177
David James54a830e2015-05-06 17:21:47 -0700178 # Turn on collection of Chrome coredumps via creation of a magic file.
179 # (Without this, Chrome coredumps are trashed.)
Don Garrett11b13972015-09-11 17:37:53 +0000180 open(constants.CHROME_CORE_MAGIC_FILE, 'w').close()
David James54a830e2015-05-06 17:21:47 -0700181
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800182 for i in range(num_tries):
183 try:
184 browser_to_create = browser_finder.FindBrowser(finder_options)
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -0800185 self._browser = browser_to_create.Create(finder_options)
Victor Hsieh43651ac2016-04-20 12:52:21 -0700186 if _is_arc_available():
Shuhei Takahashi8462b882016-05-31 17:27:32 +0900187 arc_util.post_processing_after_browser(self)
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800188 break
Achuith Bhandarkarfab82d12015-02-26 11:05:35 -0800189 except (exceptions.LoginException) as e:
Achuith Bhandarkarf800edf2013-12-06 13:37:52 -0800190 logging.error('Timed out logging in, tries=%d, error=%s',
191 i, repr(e))
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800192 if i == num_tries-1:
193 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -0700194
195
196 def __enter__(self):
197 return self
198
199
200 def __exit__(self, *args):
Derek Basehore41acbf82014-07-11 18:34:30 -0700201 self.close()
Achuith Bhandarkared498932013-07-16 17:01:40 -0700202
203
204 @property
205 def browser(self):
206 """Returns a telemetry browser instance."""
207 return self._browser
208
209
210 def get_extension(self, extension_path):
211 """Fetches a telemetry extension instance given the extension path."""
212 for ext in self._extensions_to_load:
213 if extension_path == ext.path:
214 return self.browser.extensions[ext]
215 return None
216
217
218 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700219 def autotest_ext(self):
220 """Returns the autotest extension."""
221 return self.get_extension(self._autotest_ext_path)
222
223
224 @property
225 def login_status(self):
226 """Returns login status."""
227 ext = self.autotest_ext
228 if not ext:
229 return None
230
231 ext.ExecuteJavaScript('''
232 window.__login_status = null;
233 chrome.autotestPrivate.loginStatus(function(s) {
234 window.__login_status = s;
235 });
236 ''')
237 return ext.EvaluateJavaScript('window.__login_status')
238
239
Victor Hsiehfc3417e2016-03-25 16:49:59 -0700240 def get_visible_notifications(self):
241 """Returns an array of visible notifications of Chrome.
242
243 For specific type of each notification, please refer to Chromium's
244 chrome/common/extensions/api/autotest_private.idl.
245 """
246 ext = self.autotest_ext
247 if not ext:
248 return None
249
250 ext.ExecuteJavaScript('''
251 window.__items = null;
252 chrome.autotestPrivate.getVisibleNotifications(function(items) {
253 window.__items = items;
254 });
255 ''')
256 if ext.EvaluateJavaScript('window.__items') is None:
257 return None
258 return ext.EvaluateJavaScript('window.__items')
259
260
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700261 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700262 def browser_type(self):
263 """Returns the browser_type."""
264 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700265
266
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800267 @staticmethod
268 def did_browser_crash(func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700269 """Runs func, returns True if the browser crashed, False otherwise.
270
271 @param func: function to run.
272
273 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700274 try:
275 func()
Achuith Bhandarkar704134d2015-03-05 17:31:57 -0800276 except (Error):
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700277 return True
278 return False
Derek Basehore41acbf82014-07-11 18:34:30 -0700279
280
Achuith Bhandarkar88a2bab2015-07-09 17:36:25 -0700281 @staticmethod
282 def wait_for_browser_restart(func):
283 """Runs func, and waits for a browser restart.
284
285 @param func: function to run.
286
287 """
288 _cri = cros_interface.CrOSInterface()
289 pid = _cri.GetChromePid()
290 Chrome.did_browser_crash(func)
291 utils.poll_for_condition(lambda: pid != _cri.GetChromePid(), timeout=60)
292
293
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800294 def wait_for_browser_to_come_up(self):
295 """Waits for the browser to come up. This should only be called after a
296 browser crash.
297 """
298 def _BrowserReady(cr):
299 tabs = [] # Wrapper for pass by reference.
300 if self.did_browser_crash(
301 lambda: tabs.append(cr.browser.tabs.New())):
302 return False
303 try:
304 tabs[0].Close()
Achuith Bhandarkarfab82d12015-02-26 11:05:35 -0800305 except:
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800306 # crbug.com/350941
307 logging.error('Timed out closing tab')
308 return True
309 util.WaitFor(lambda: _BrowserReady(self), timeout=10)
310
311
Derek Basehore41acbf82014-07-11 18:34:30 -0700312 def close(self):
313 """Closes the browser."""
Hidehiko Abe202ed322016-04-14 15:08:43 +0900314 try:
Victor Hsieh43651ac2016-04-20 12:52:21 -0700315 if _is_arc_available():
Shuhei Takahashi8462b882016-05-31 17:27:32 +0900316 arc_util.pre_processing_before_close(self)
Hidehiko Abe202ed322016-04-14 15:08:43 +0900317 finally:
318 self._browser.Close()