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