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 | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame^] | 5 | from telemetry.core import browser_finder, browser_options, extension_to_load |
Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 6 | |
| 7 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame^] | 8 | # Name of the logged-in user specified by the telemetry login extension. |
Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 9 | LOGIN_USER = 'test@test.test' |
| 10 | |
| 11 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame^] | 12 | class Chrome(object): |
| 13 | """Wrapper for creating a telemetry browser instance with extensions.""" |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame] | 14 | |
| 15 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame^] | 16 | BROWSER_TYPE_LOGIN = 'system' |
| 17 | BROWSER_TYPE_GUEST = 'system-guest' |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame] | 18 | |
| 19 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame^] | 20 | def __init__(self, logged_in=True, extension_paths=[]): |
| 21 | options = browser_options.BrowserOptions() |
| 22 | self._browser_type = (self.BROWSER_TYPE_LOGIN |
| 23 | if logged_in else self.BROWSER_TYPE_GUEST) |
| 24 | options.browser_type = self._browser_type |
| 25 | |
| 26 | if logged_in: |
| 27 | for path in extension_paths: |
| 28 | extension = extension_to_load.ExtensionToLoad( |
| 29 | path, self._browser_type, is_component=True) |
| 30 | options.extensions_to_load.append(extension) |
| 31 | self._extensions_to_load = options.extensions_to_load |
| 32 | |
| 33 | browser_to_create = browser_finder.FindBrowser(options) |
| 34 | self._browser = browser_to_create.Create() |
| 35 | |
| 36 | |
| 37 | def __enter__(self): |
| 38 | return self |
| 39 | |
| 40 | |
| 41 | def __exit__(self, *args): |
| 42 | self.browser.Close() |
| 43 | |
| 44 | |
| 45 | @property |
| 46 | def browser(self): |
| 47 | """Returns a telemetry browser instance.""" |
| 48 | return self._browser |
| 49 | |
| 50 | |
| 51 | def get_extension(self, extension_path): |
| 52 | """Fetches a telemetry extension instance given the extension path.""" |
| 53 | for ext in self._extensions_to_load: |
| 54 | if extension_path == ext.path: |
| 55 | return self.browser.extensions[ext] |
| 56 | return None |
| 57 | |
| 58 | |
| 59 | @property |
| 60 | def browser_type(self): |
| 61 | """Returns the browser_type.""" |
| 62 | return self._browser_type |