blob: 813c0632dbd372b3b50a4bd287e38e6d67839d26 [file] [log] [blame]
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -07001# 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 Bhandarkared498932013-07-16 17:01:40 -07005from telemetry.core import browser_finder, browser_options, extension_to_load
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -07006
7
Achuith Bhandarkared498932013-07-16 17:01:40 -07008# Name of the logged-in user specified by the telemetry login extension.
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -07009LOGIN_USER = 'test@test.test'
10
11
Achuith Bhandarkared498932013-07-16 17:01:40 -070012class Chrome(object):
13 """Wrapper for creating a telemetry browser instance with extensions."""
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070014
15
Achuith Bhandarkared498932013-07-16 17:01:40 -070016 BROWSER_TYPE_LOGIN = 'system'
17 BROWSER_TYPE_GUEST = 'system-guest'
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070018
19
Achuith Bhandarkared498932013-07-16 17:01:40 -070020 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()
Achuith Bhandarkarc8496852013-08-02 13:57:02 -070035 self._browser.Start()
Achuith Bhandarkared498932013-07-16 17:01:40 -070036
37
38 def __enter__(self):
39 return self
40
41
42 def __exit__(self, *args):
43 self.browser.Close()
44
45
46 @property
47 def browser(self):
48 """Returns a telemetry browser instance."""
49 return self._browser
50
51
52 def get_extension(self, extension_path):
53 """Fetches a telemetry extension instance given the extension path."""
54 for ext in self._extensions_to_load:
55 if extension_path == ext.path:
56 return self.browser.extensions[ext]
57 return None
58
59
60 @property
61 def browser_type(self):
62 """Returns the browser_type."""
63 return self._browser_type