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 | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 5 | from telemetry.core import browser_finder, browser_options, exceptions |
| 6 | from telemetry.core import extension_to_load, util |
Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 7 | |
| 8 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 9 | # Name of the logged-in user specified by the telemetry login extension. |
Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 10 | LOGIN_USER = 'test@test.test' |
| 11 | |
| 12 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 13 | class Chrome(object): |
| 14 | """Wrapper for creating a telemetry browser instance with extensions.""" |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame] | 15 | |
| 16 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 17 | BROWSER_TYPE_LOGIN = 'system' |
| 18 | BROWSER_TYPE_GUEST = 'system-guest' |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame] | 19 | |
| 20 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 21 | def __init__(self, logged_in=True, extension_paths=[]): |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 22 | finder_options = browser_options.BrowserFinderOptions() |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 23 | self._browser_type = (self.BROWSER_TYPE_LOGIN |
| 24 | if logged_in else self.BROWSER_TYPE_GUEST) |
Achuith Bhandarkar | f130c40 | 2013-09-11 14:39:22 -0700 | [diff] [blame] | 25 | finder_options.browser_type = self.browser_type |
| 26 | |
| 27 | b_options = finder_options.browser_options |
| 28 | b_options.disable_component_extensions_with_background_pages = False |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 29 | |
| 30 | if logged_in: |
| 31 | for path in extension_paths: |
| 32 | extension = extension_to_load.ExtensionToLoad( |
Achuith Bhandarkar | f130c40 | 2013-09-11 14:39:22 -0700 | [diff] [blame] | 33 | path, self.browser_type, is_component=True) |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 34 | finder_options.extensions_to_load.append(extension) |
| 35 | self._extensions_to_load = finder_options.extensions_to_load |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 36 | |
Achuith Bhandarkar | dafc9a5 | 2013-09-24 15:26:33 +0200 | [diff] [blame] | 37 | finder_options.CreateParser().parse_args(args=[]) |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 38 | browser_to_create = browser_finder.FindBrowser(finder_options) |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 39 | self._browser = browser_to_create.Create() |
Achuith Bhandarkar | c849685 | 2013-08-02 13:57:02 -0700 | [diff] [blame] | 40 | self._browser.Start() |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def __enter__(self): |
| 44 | return self |
| 45 | |
| 46 | |
| 47 | def __exit__(self, *args): |
| 48 | self.browser.Close() |
| 49 | |
| 50 | |
| 51 | @property |
| 52 | def browser(self): |
| 53 | """Returns a telemetry browser instance.""" |
| 54 | return self._browser |
| 55 | |
| 56 | |
| 57 | def get_extension(self, extension_path): |
| 58 | """Fetches a telemetry extension instance given the extension path.""" |
| 59 | for ext in self._extensions_to_load: |
| 60 | if extension_path == ext.path: |
| 61 | return self.browser.extensions[ext] |
| 62 | return None |
| 63 | |
| 64 | |
| 65 | @property |
| 66 | def browser_type(self): |
| 67 | """Returns the browser_type.""" |
| 68 | return self._browser_type |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 69 | |
| 70 | |
| 71 | def wait_for_browser_to_come_up(self): |
Achuith Bhandarkar | a5bb3f6 | 2013-10-10 10:39:57 -0700 | [diff] [blame] | 72 | """Waits for the browser to come up. This should only be called after a |
| 73 | browser crash. |
| 74 | """ |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 75 | def _BrowserReady(cr): |
| 76 | try: |
| 77 | tab = cr.browser.tabs.New() |
| 78 | except (exceptions.BrowserGoneException, |
| 79 | exceptions.BrowserConnectionGoneException): |
| 80 | return False |
| 81 | tab.Close() |
| 82 | return True |
| 83 | util.WaitFor(lambda: _BrowserReady(self), poll_interval=1, timeout=10) |
| 84 | |
| 85 | |
Achuith Bhandarkar | 5abf60b | 2013-08-01 12:35:53 -0700 | [diff] [blame] | 86 | def did_browser_crash(self, func): |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 87 | """Runs func, returns True if the browser crashed, False otherwise. |
| 88 | |
| 89 | @param func: function to run. |
| 90 | |
| 91 | """ |
Achuith Bhandarkar | 5abf60b | 2013-08-01 12:35:53 -0700 | [diff] [blame] | 92 | try: |
| 93 | func() |
| 94 | except (exceptions.BrowserGoneException, |
| 95 | exceptions.BrowserConnectionGoneException): |
| 96 | return True |
| 97 | return False |
| 98 | |