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 | b0e9907 | 2013-11-18 16:15:03 -0800 | [diff] [blame] | 5 | import logging, os |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 6 | |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 7 | from telemetry.core import browser_finder, browser_options, exceptions |
| 8 | from telemetry.core import extension_to_load, util |
Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 9 | |
| 10 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 11 | class Chrome(object): |
| 12 | """Wrapper for creating a telemetry browser instance with extensions.""" |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame] | 13 | |
| 14 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 15 | BROWSER_TYPE_LOGIN = 'system' |
| 16 | BROWSER_TYPE_GUEST = 'system-guest' |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame] | 17 | |
| 18 | |
Achuith Bhandarkar | b0e9907 | 2013-11-18 16:15:03 -0800 | [diff] [blame] | 19 | def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False, |
Achuith Bhandarkar | 1cce9dc | 2014-02-05 14:23:34 -0800 | [diff] [blame^] | 20 | is_component=True, num_tries=1, extra_browser_args=None, |
| 21 | auto_login=True, username=None, password=None): |
Dean Liao | b12e2ee | 2013-11-19 16:49:12 +0800 | [diff] [blame] | 22 | """ |
Achuith Bhandarkar | 944405a | 2013-11-21 14:47:48 -0800 | [diff] [blame] | 23 | Constructor of telemetry wrapper. |
| 24 | |
| 25 | @param logged_in: Regular user (True) or guest user (False). |
| 26 | @param extension_paths: path of unpacked extension to install. |
| 27 | @param autotest_ext: Load a component extension with privileges to |
| 28 | invoke chrome.autotestPrivate. |
| 29 | @param is_component: Whether extensions should be loaded as component |
| 30 | extensions. |
| 31 | @param num_tries: Number of attempts to log in. (Temporary for |
| 32 | debugging). |
Dean Liao | b12e2ee | 2013-11-19 16:49:12 +0800 | [diff] [blame] | 33 | @param extra_browser_args: Additional argument(s) to pass to the |
| 34 | browser. It can be a string or a list. |
Achuith Bhandarkar | 1cce9dc | 2014-02-05 14:23:34 -0800 | [diff] [blame^] | 35 | @param auto_login: Does not login automatically if this is False. |
| 36 | Useful if you need to examine oobe. |
| 37 | @param username: Log in using this username instead of the default. |
| 38 | @param username: Log in using this password instead of the default. |
Dean Liao | b12e2ee | 2013-11-19 16:49:12 +0800 | [diff] [blame] | 39 | """ |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 40 | self._autotest_ext_path = None |
| 41 | if autotest_ext: |
| 42 | self._autotest_ext_path = os.path.join(os.path.dirname(__file__), |
| 43 | 'autotest_private_ext') |
| 44 | extension_paths.append(self._autotest_ext_path) |
| 45 | |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 46 | finder_options = browser_options.BrowserFinderOptions() |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 47 | self._browser_type = (self.BROWSER_TYPE_LOGIN |
| 48 | if logged_in else self.BROWSER_TYPE_GUEST) |
Achuith Bhandarkar | f130c40 | 2013-09-11 14:39:22 -0700 | [diff] [blame] | 49 | finder_options.browser_type = self.browser_type |
Dean Liao | b12e2ee | 2013-11-19 16:49:12 +0800 | [diff] [blame] | 50 | if extra_browser_args: |
| 51 | finder_options.browser_options.AppendExtraBrowserArgs( |
| 52 | extra_browser_args) |
Achuith Bhandarkar | f130c40 | 2013-09-11 14:39:22 -0700 | [diff] [blame] | 53 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 54 | if logged_in: |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 55 | extensions_to_load = finder_options.extensions_to_load |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 56 | for path in extension_paths: |
| 57 | extension = extension_to_load.ExtensionToLoad( |
Achuith Bhandarkar | 944405a | 2013-11-21 14:47:48 -0800 | [diff] [blame] | 58 | path, self.browser_type, is_component=is_component) |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 59 | extensions_to_load.append(extension) |
| 60 | self._extensions_to_load = extensions_to_load |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 61 | |
Achuith Bhandarkar | 3e5051d | 2013-10-15 15:20:12 -0700 | [diff] [blame] | 62 | # finder options must be set before parse_args(), browser options must |
| 63 | # be set before Create(). |
| 64 | finder_options.verbosity = 1 # info logging for telemetry. |
Achuith Bhandarkar | dafc9a5 | 2013-09-24 15:26:33 +0200 | [diff] [blame] | 65 | finder_options.CreateParser().parse_args(args=[]) |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 66 | b_options = finder_options.browser_options |
| 67 | b_options.disable_component_extensions_with_background_pages = False |
| 68 | b_options.create_browser_with_oobe = True |
Achuith Bhandarkar | 1cce9dc | 2014-02-05 14:23:34 -0800 | [diff] [blame^] | 69 | |
| 70 | b_options.auto_login = auto_login |
| 71 | self.username = b_options.username if username is None else username |
| 72 | self.password = b_options.password if password is None else password |
| 73 | b_options.username = self.username |
| 74 | b_options.password = self.password |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 75 | |
Achuith Bhandarkar | b0e9907 | 2013-11-18 16:15:03 -0800 | [diff] [blame] | 76 | for i in range(num_tries): |
| 77 | try: |
| 78 | browser_to_create = browser_finder.FindBrowser(finder_options) |
| 79 | self._browser = browser_to_create.Create() |
| 80 | self._browser.Start() |
| 81 | break |
Achuith Bhandarkar | f800edf | 2013-12-06 13:37:52 -0800 | [diff] [blame] | 82 | except (util.TimeoutException, exceptions.LoginException) as e: |
| 83 | logging.error('Timed out logging in, tries=%d, error=%s', |
| 84 | i, repr(e)) |
Achuith Bhandarkar | b0e9907 | 2013-11-18 16:15:03 -0800 | [diff] [blame] | 85 | if i == num_tries-1: |
| 86 | raise |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 87 | |
| 88 | |
| 89 | def __enter__(self): |
| 90 | return self |
| 91 | |
| 92 | |
| 93 | def __exit__(self, *args): |
| 94 | self.browser.Close() |
| 95 | |
| 96 | |
| 97 | @property |
| 98 | def browser(self): |
| 99 | """Returns a telemetry browser instance.""" |
| 100 | return self._browser |
| 101 | |
| 102 | |
| 103 | def get_extension(self, extension_path): |
| 104 | """Fetches a telemetry extension instance given the extension path.""" |
| 105 | for ext in self._extensions_to_load: |
| 106 | if extension_path == ext.path: |
| 107 | return self.browser.extensions[ext] |
| 108 | return None |
| 109 | |
| 110 | |
| 111 | @property |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 112 | def autotest_ext(self): |
| 113 | """Returns the autotest extension.""" |
| 114 | return self.get_extension(self._autotest_ext_path) |
| 115 | |
| 116 | |
| 117 | @property |
| 118 | def login_status(self): |
| 119 | """Returns login status.""" |
| 120 | ext = self.autotest_ext |
| 121 | if not ext: |
| 122 | return None |
| 123 | |
| 124 | ext.ExecuteJavaScript(''' |
| 125 | window.__login_status = null; |
| 126 | chrome.autotestPrivate.loginStatus(function(s) { |
| 127 | window.__login_status = s; |
| 128 | }); |
| 129 | ''') |
| 130 | return ext.EvaluateJavaScript('window.__login_status') |
| 131 | |
| 132 | |
| 133 | @property |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 134 | def browser_type(self): |
| 135 | """Returns the browser_type.""" |
| 136 | return self._browser_type |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 137 | |
| 138 | |
| 139 | def wait_for_browser_to_come_up(self): |
Achuith Bhandarkar | a5bb3f6 | 2013-10-10 10:39:57 -0700 | [diff] [blame] | 140 | """Waits for the browser to come up. This should only be called after a |
| 141 | browser crash. |
| 142 | """ |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 143 | def _BrowserReady(cr): |
| 144 | try: |
| 145 | tab = cr.browser.tabs.New() |
| 146 | except (exceptions.BrowserGoneException, |
| 147 | exceptions.BrowserConnectionGoneException): |
| 148 | return False |
| 149 | tab.Close() |
| 150 | return True |
| 151 | util.WaitFor(lambda: _BrowserReady(self), poll_interval=1, timeout=10) |
| 152 | |
| 153 | |
Achuith Bhandarkar | 5abf60b | 2013-08-01 12:35:53 -0700 | [diff] [blame] | 154 | def did_browser_crash(self, func): |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 155 | """Runs func, returns True if the browser crashed, False otherwise. |
| 156 | |
| 157 | @param func: function to run. |
| 158 | |
| 159 | """ |
Achuith Bhandarkar | 5abf60b | 2013-08-01 12:35:53 -0700 | [diff] [blame] | 160 | try: |
| 161 | func() |
| 162 | except (exceptions.BrowserGoneException, |
| 163 | exceptions.BrowserConnectionGoneException): |
| 164 | return True |
| 165 | return False |
| 166 | |