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 | import os |
| 6 | |
| 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 | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 23 | def __init__(self, logged_in=True, extension_paths=[]): |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 24 | finder_options = browser_options.BrowserFinderOptions() |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 25 | self._browser_type = (self.BROWSER_TYPE_LOGIN |
| 26 | if logged_in else self.BROWSER_TYPE_GUEST) |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 27 | finder_options.browser_type = self._browser_type |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 28 | |
| 29 | if logged_in: |
| 30 | for path in extension_paths: |
| 31 | extension = extension_to_load.ExtensionToLoad( |
| 32 | path, self._browser_type, is_component=True) |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 33 | finder_options.extensions_to_load.append(extension) |
| 34 | self._extensions_to_load = finder_options.extensions_to_load |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 35 | |
Achuith Bhandarkar | dafc9a5 | 2013-09-24 15:26:33 +0200 | [diff] [blame^] | 36 | finder_options.CreateParser().parse_args(args=[]) |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 37 | browser_to_create = browser_finder.FindBrowser(finder_options) |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 38 | self._browser = browser_to_create.Create() |
Achuith Bhandarkar | c849685 | 2013-08-02 13:57:02 -0700 | [diff] [blame] | 39 | self._browser.Start() |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def __enter__(self): |
| 43 | return self |
| 44 | |
| 45 | |
| 46 | def __exit__(self, *args): |
| 47 | self.browser.Close() |
| 48 | |
| 49 | |
| 50 | @property |
| 51 | def browser(self): |
| 52 | """Returns a telemetry browser instance.""" |
| 53 | return self._browser |
| 54 | |
| 55 | |
| 56 | def get_extension(self, extension_path): |
| 57 | """Fetches a telemetry extension instance given the extension path.""" |
| 58 | for ext in self._extensions_to_load: |
| 59 | if extension_path == ext.path: |
| 60 | return self.browser.extensions[ext] |
| 61 | return None |
| 62 | |
| 63 | |
| 64 | @property |
| 65 | def browser_type(self): |
| 66 | """Returns the browser_type.""" |
| 67 | return self._browser_type |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 68 | |
| 69 | |
| 70 | def wait_for_browser_to_come_up(self): |
| 71 | """Waits for the browser to come up.""" |
| 72 | def _BrowserReady(cr): |
| 73 | try: |
| 74 | tab = cr.browser.tabs.New() |
| 75 | except (exceptions.BrowserGoneException, |
| 76 | exceptions.BrowserConnectionGoneException): |
| 77 | return False |
| 78 | tab.Close() |
| 79 | return True |
| 80 | util.WaitFor(lambda: _BrowserReady(self), poll_interval=1, timeout=10) |
| 81 | |
| 82 | |
Achuith Bhandarkar | 5abf60b | 2013-08-01 12:35:53 -0700 | [diff] [blame] | 83 | def did_browser_crash(self, func): |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 84 | """Runs func, returns True if the browser crashed, False otherwise. |
| 85 | |
| 86 | @param func: function to run. |
| 87 | |
| 88 | """ |
Achuith Bhandarkar | 5abf60b | 2013-08-01 12:35:53 -0700 | [diff] [blame] | 89 | try: |
| 90 | func() |
| 91 | except (exceptions.BrowserGoneException, |
| 92 | exceptions.BrowserConnectionGoneException): |
| 93 | return True |
| 94 | return False |
| 95 | |
| 96 | |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 97 | def is_logged_in(self): |
| 98 | """Returns true iff logged in.""" |
| 99 | # TODO(achuith): Do this better. |
| 100 | return os.path.exists('/var/run/state/logged-in') |