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 | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame^] | 5 | import os |
| 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 | # Name of the logged-in user specified by the telemetry login extension. |
Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 12 | LOGIN_USER = 'test@test.test' |
| 13 | |
| 14 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 15 | class Chrome(object): |
| 16 | """Wrapper for creating a telemetry browser instance with extensions.""" |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame] | 17 | |
| 18 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 19 | BROWSER_TYPE_LOGIN = 'system' |
| 20 | BROWSER_TYPE_GUEST = 'system-guest' |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame] | 21 | |
| 22 | |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame^] | 23 | def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False): |
| 24 | self._autotest_ext_path = None |
| 25 | if autotest_ext: |
| 26 | self._autotest_ext_path = os.path.join(os.path.dirname(__file__), |
| 27 | 'autotest_private_ext') |
| 28 | extension_paths.append(self._autotest_ext_path) |
| 29 | |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 30 | finder_options = browser_options.BrowserFinderOptions() |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 31 | self._browser_type = (self.BROWSER_TYPE_LOGIN |
| 32 | if logged_in else self.BROWSER_TYPE_GUEST) |
Achuith Bhandarkar | f130c40 | 2013-09-11 14:39:22 -0700 | [diff] [blame] | 33 | finder_options.browser_type = self.browser_type |
| 34 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 35 | if logged_in: |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame^] | 36 | extensions_to_load = finder_options.extensions_to_load |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 37 | for path in extension_paths: |
| 38 | extension = extension_to_load.ExtensionToLoad( |
Achuith Bhandarkar | f130c40 | 2013-09-11 14:39:22 -0700 | [diff] [blame] | 39 | path, self.browser_type, is_component=True) |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame^] | 40 | extensions_to_load.append(extension) |
| 41 | self._extensions_to_load = extensions_to_load |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 42 | |
Achuith Bhandarkar | dafc9a5 | 2013-09-24 15:26:33 +0200 | [diff] [blame] | 43 | finder_options.CreateParser().parse_args(args=[]) |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame^] | 44 | b_options = finder_options.browser_options |
| 45 | b_options.disable_component_extensions_with_background_pages = False |
| 46 | b_options.create_browser_with_oobe = True |
| 47 | |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 48 | browser_to_create = browser_finder.FindBrowser(finder_options) |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 49 | self._browser = browser_to_create.Create() |
Achuith Bhandarkar | c849685 | 2013-08-02 13:57:02 -0700 | [diff] [blame] | 50 | self._browser.Start() |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 51 | |
| 52 | |
| 53 | def __enter__(self): |
| 54 | return self |
| 55 | |
| 56 | |
| 57 | def __exit__(self, *args): |
| 58 | self.browser.Close() |
| 59 | |
| 60 | |
| 61 | @property |
| 62 | def browser(self): |
| 63 | """Returns a telemetry browser instance.""" |
| 64 | return self._browser |
| 65 | |
| 66 | |
| 67 | def get_extension(self, extension_path): |
| 68 | """Fetches a telemetry extension instance given the extension path.""" |
| 69 | for ext in self._extensions_to_load: |
| 70 | if extension_path == ext.path: |
| 71 | return self.browser.extensions[ext] |
| 72 | return None |
| 73 | |
| 74 | |
| 75 | @property |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame^] | 76 | def autotest_ext(self): |
| 77 | """Returns the autotest extension.""" |
| 78 | return self.get_extension(self._autotest_ext_path) |
| 79 | |
| 80 | |
| 81 | @property |
| 82 | def login_status(self): |
| 83 | """Returns login status.""" |
| 84 | ext = self.autotest_ext |
| 85 | if not ext: |
| 86 | return None |
| 87 | |
| 88 | ext.ExecuteJavaScript(''' |
| 89 | window.__login_status = null; |
| 90 | chrome.autotestPrivate.loginStatus(function(s) { |
| 91 | window.__login_status = s; |
| 92 | }); |
| 93 | ''') |
| 94 | return ext.EvaluateJavaScript('window.__login_status') |
| 95 | |
| 96 | |
| 97 | @property |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 98 | def browser_type(self): |
| 99 | """Returns the browser_type.""" |
| 100 | return self._browser_type |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def wait_for_browser_to_come_up(self): |
Achuith Bhandarkar | a5bb3f6 | 2013-10-10 10:39:57 -0700 | [diff] [blame] | 104 | """Waits for the browser to come up. This should only be called after a |
| 105 | browser crash. |
| 106 | """ |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 107 | def _BrowserReady(cr): |
| 108 | try: |
| 109 | tab = cr.browser.tabs.New() |
| 110 | except (exceptions.BrowserGoneException, |
| 111 | exceptions.BrowserConnectionGoneException): |
| 112 | return False |
| 113 | tab.Close() |
| 114 | return True |
| 115 | util.WaitFor(lambda: _BrowserReady(self), poll_interval=1, timeout=10) |
| 116 | |
| 117 | |
Achuith Bhandarkar | 5abf60b | 2013-08-01 12:35:53 -0700 | [diff] [blame] | 118 | def did_browser_crash(self, func): |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 119 | """Runs func, returns True if the browser crashed, False otherwise. |
| 120 | |
| 121 | @param func: function to run. |
| 122 | |
| 123 | """ |
Achuith Bhandarkar | 5abf60b | 2013-08-01 12:35:53 -0700 | [diff] [blame] | 124 | try: |
| 125 | func() |
| 126 | except (exceptions.BrowserGoneException, |
| 127 | exceptions.BrowserConnectionGoneException): |
| 128 | return True |
| 129 | return False |
| 130 | |