chrome.py revision 8988eb240fc2f5bf25c0e251fdfa595798a45625
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
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
16class Chrome(object):
17    """Wrapper for creating a telemetry browser instance with extensions."""
18
19
20    CHEETS = '-cheets'
21    BROWSER_TYPE_LOGIN = 'system'
22    BROWSER_TYPE_GUEST = 'system-guest'
23
24
25    def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False,
26                 is_component=True, num_tries=3, extra_browser_args=None,
27                 clear_enterprise_policy=True, dont_override_profile=False,
28                 disable_gaia_services=True, disable_default_apps = True,
29                 auto_login=True, gaia_login=False,
30                 username=None, password=None, gaia_id=None):
31        """
32        Constructor of telemetry wrapper.
33
34        @param logged_in: Regular user (True) or guest user (False).
35        @param extension_paths: path of unpacked extension to install.
36        @param autotest_ext: Load a component extension with privileges to
37                             invoke chrome.autotestPrivate.
38        @param is_component: Whether extensions should be loaded as component
39                             extensions.
40        @param num_tries: Number of attempts to log in.
41        @param extra_browser_args: Additional argument(s) to pass to the
42                                   browser. It can be a string or a list.
43        @param clear_enterprise_policy: Clear enterprise policy before
44                                        logging in.
45        @param dont_override_profile: Don't delete cryptohome before login.
46                                      Telemetry will output a warning with this
47                                      option.
48        @param disable_gaia_services: For enterprise autotests, this option may
49                                      be used to enable policy fetch.
50        @param disable_default_apps: For tests that exercise default apps.
51        @param auto_login: Does not login automatically if this is False.
52                           Useful if you need to examine oobe.
53        @param gaia_login: Logs in to real gaia.
54        @param username: Log in using this username instead of the default.
55        @param password: Log in using this password instead of the default.
56        @param gaia_id: Log in using this gaia_id instead of the default.
57        """
58        self._autotest_ext_path = None
59        if autotest_ext:
60            self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
61                                                   'autotest_private_ext')
62            extension_paths.append(self._autotest_ext_path)
63
64        finder_options = browser_options.BrowserFinderOptions()
65        # Append cheets specific browser args
66        is_cheets_platform = utils.get_current_board().endswith(self.CHEETS)
67        if is_cheets_platform:
68            from autotest_lib.client.common_lib.cros import cheets
69            extra_browser_args = cheets.append_extra_args(extra_browser_args)
70            logged_in = True
71        self._browser_type = (self.BROWSER_TYPE_LOGIN
72                if logged_in else self.BROWSER_TYPE_GUEST)
73        finder_options.browser_type = self.browser_type
74        if extra_browser_args:
75            finder_options.browser_options.AppendExtraBrowserArgs(
76                    extra_browser_args)
77
78        if logged_in:
79            extensions_to_load = finder_options.extensions_to_load
80            for path in extension_paths:
81                extension = extension_to_load.ExtensionToLoad(
82                        path, self.browser_type, is_component=is_component)
83                extensions_to_load.append(extension)
84            self._extensions_to_load = extensions_to_load
85
86        # finder options must be set before parse_args(), browser options must
87        # be set before Create().
88        # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit
89        # autotest debug logs
90        finder_options.verbosity = 2
91        finder_options.CreateParser().parse_args(args=[])
92        b_options = finder_options.browser_options
93        b_options.disable_component_extensions_with_background_pages = False
94        b_options.create_browser_with_oobe = True
95        b_options.clear_enterprise_policy = clear_enterprise_policy
96        b_options.dont_override_profile = dont_override_profile
97        b_options.disable_gaia_services = disable_gaia_services
98        b_options.disable_default_apps = disable_default_apps
99        b_options.disable_component_extensions_with_background_pages = disable_default_apps
100
101        b_options.auto_login = auto_login
102        b_options.gaia_login = gaia_login
103        self.username = b_options.username if username is None else username
104        self.password = b_options.password if password is None else password
105        b_options.username = self.username
106        b_options.password = self.password
107        # gaia_id will be added to telemetry code in chromium repository later
108        try:
109            self.gaia_id = b_options.gaia_id if gaia_id is None else gaia_id
110            b_options.gaia_id = self.gaia_id
111        except AttributeError:
112            pass
113
114        # Turn on collection of Chrome coredumps via creation of a magic file.
115        # (Without this, Chrome coredumps are trashed.)
116        open(constants.CHROME_CORE_MAGIC_FILE, 'w').close()
117
118        for i in range(num_tries):
119            try:
120                browser_to_create = browser_finder.FindBrowser(finder_options)
121                self._browser = browser_to_create.Create(finder_options)
122                if is_cheets_platform:
123                    cheets.post_processing_after_browser()
124                break
125            except (exceptions.LoginException) as e:
126                logging.error('Timed out logging in, tries=%d, error=%s',
127                              i, repr(e))
128                if i == num_tries-1:
129                    raise
130
131
132    def __enter__(self):
133        return self
134
135
136    def __exit__(self, *args):
137        self.close()
138
139
140    @property
141    def browser(self):
142        """Returns a telemetry browser instance."""
143        return self._browser
144
145
146    def get_extension(self, extension_path):
147        """Fetches a telemetry extension instance given the extension path."""
148        for ext in self._extensions_to_load:
149            if extension_path == ext.path:
150                return self.browser.extensions[ext]
151        return None
152
153
154    @property
155    def autotest_ext(self):
156        """Returns the autotest extension."""
157        return self.get_extension(self._autotest_ext_path)
158
159
160    @property
161    def login_status(self):
162        """Returns login status."""
163        ext = self.autotest_ext
164        if not ext:
165            return None
166
167        ext.ExecuteJavaScript('''
168            window.__login_status = null;
169            chrome.autotestPrivate.loginStatus(function(s) {
170              window.__login_status = s;
171            });
172        ''')
173        return ext.EvaluateJavaScript('window.__login_status')
174
175
176    @property
177    def browser_type(self):
178        """Returns the browser_type."""
179        return self._browser_type
180
181
182    @staticmethod
183    def did_browser_crash(func):
184        """Runs func, returns True if the browser crashed, False otherwise.
185
186        @param func: function to run.
187
188        """
189        try:
190            func()
191        except (Error):
192            return True
193        return False
194
195
196    @staticmethod
197    def wait_for_browser_restart(func):
198        """Runs func, and waits for a browser restart.
199
200        @param func: function to run.
201
202        """
203        _cri = cros_interface.CrOSInterface()
204        pid = _cri.GetChromePid()
205        Chrome.did_browser_crash(func)
206        utils.poll_for_condition(lambda: pid != _cri.GetChromePid(), timeout=60)
207
208
209    def wait_for_browser_to_come_up(self):
210        """Waits for the browser to come up. This should only be called after a
211        browser crash.
212        """
213        def _BrowserReady(cr):
214            tabs = []  # Wrapper for pass by reference.
215            if self.did_browser_crash(
216                    lambda: tabs.append(cr.browser.tabs.New())):
217                return False
218            try:
219                tabs[0].Close()
220            except:
221                # crbug.com/350941
222                logging.error('Timed out closing tab')
223            return True
224        util.WaitFor(lambda: _BrowserReady(self), timeout=10)
225
226
227    def close(self):
228        """Closes the browser."""
229        self._browser.Close()
230