blob: b55363a62984dbd67c9c055f83e129228d1960d3 [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 Bhandarkar49c72d92013-07-25 11:10:10 -07005import os
6
7from telemetry.core import browser_finder, browser_options, exceptions
8from telemetry.core import extension_to_load, util
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -07009
10
Achuith Bhandarkared498932013-07-16 17:01:40 -070011# Name of the logged-in user specified by the telemetry login extension.
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -070012LOGIN_USER = 'test@test.test'
13
14
Achuith Bhandarkared498932013-07-16 17:01:40 -070015class Chrome(object):
16 """Wrapper for creating a telemetry browser instance with extensions."""
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070017
18
Achuith Bhandarkared498932013-07-16 17:01:40 -070019 BROWSER_TYPE_LOGIN = 'system'
20 BROWSER_TYPE_GUEST = 'system-guest'
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070021
22
Achuith Bhandarkared498932013-07-16 17:01:40 -070023 def __init__(self, logged_in=True, extension_paths=[]):
24 options = browser_options.BrowserOptions()
25 self._browser_type = (self.BROWSER_TYPE_LOGIN
26 if logged_in else self.BROWSER_TYPE_GUEST)
27 options.browser_type = self._browser_type
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)
33 options.extensions_to_load.append(extension)
34 self._extensions_to_load = options.extensions_to_load
35
36 browser_to_create = browser_finder.FindBrowser(options)
37 self._browser = browser_to_create.Create()
Achuith Bhandarkarc8496852013-08-02 13:57:02 -070038 self._browser.Start()
Achuith Bhandarkared498932013-07-16 17:01:40 -070039
40
41 def __enter__(self):
42 return self
43
44
45 def __exit__(self, *args):
46 self.browser.Close()
47
48
49 @property
50 def browser(self):
51 """Returns a telemetry browser instance."""
52 return self._browser
53
54
55 def get_extension(self, extension_path):
56 """Fetches a telemetry extension instance given the extension path."""
57 for ext in self._extensions_to_load:
58 if extension_path == ext.path:
59 return self.browser.extensions[ext]
60 return None
61
62
63 @property
64 def browser_type(self):
65 """Returns the browser_type."""
66 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -070067
68
69 def wait_for_browser_to_come_up(self):
70 """Waits for the browser to come up."""
71 def _BrowserReady(cr):
72 try:
73 tab = cr.browser.tabs.New()
74 except (exceptions.BrowserGoneException,
75 exceptions.BrowserConnectionGoneException):
76 return False
77 tab.Close()
78 return True
79 util.WaitFor(lambda: _BrowserReady(self), poll_interval=1, timeout=10)
80
81
82 def is_logged_in(self):
83 """Returns true iff logged in."""
84 # TODO(achuith): Do this better.
85 return os.path.exists('/var/run/state/logged-in')