blob: a0ed856721f66f695475cc4056fbbaaba3f4e0c7 [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=[]):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070024 finder_options = browser_options.BrowserFinderOptions()
Achuith Bhandarkared498932013-07-16 17:01:40 -070025 self._browser_type = (self.BROWSER_TYPE_LOGIN
26 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070027 finder_options.browser_type = self.browser_type
28
29 b_options = finder_options.browser_options
30 b_options.disable_component_extensions_with_background_pages = False
Achuith Bhandarkared498932013-07-16 17:01:40 -070031
32 if logged_in:
33 for path in extension_paths:
34 extension = extension_to_load.ExtensionToLoad(
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070035 path, self.browser_type, is_component=True)
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070036 finder_options.extensions_to_load.append(extension)
37 self._extensions_to_load = finder_options.extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070038
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +020039 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070040 browser_to_create = browser_finder.FindBrowser(finder_options)
Achuith Bhandarkared498932013-07-16 17:01:40 -070041 self._browser = browser_to_create.Create()
Achuith Bhandarkarc8496852013-08-02 13:57:02 -070042 self._browser.Start()
Achuith Bhandarkared498932013-07-16 17:01:40 -070043
44
45 def __enter__(self):
46 return self
47
48
49 def __exit__(self, *args):
50 self.browser.Close()
51
52
53 @property
54 def browser(self):
55 """Returns a telemetry browser instance."""
56 return self._browser
57
58
59 def get_extension(self, extension_path):
60 """Fetches a telemetry extension instance given the extension path."""
61 for ext in self._extensions_to_load:
62 if extension_path == ext.path:
63 return self.browser.extensions[ext]
64 return None
65
66
67 @property
68 def browser_type(self):
69 """Returns the browser_type."""
70 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -070071
72
73 def wait_for_browser_to_come_up(self):
74 """Waits for the browser to come up."""
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 Bhandarkar5abf60b2013-08-01 12:35:53 -070086 def did_browser_crash(self, func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070087 """Runs func, returns True if the browser crashed, False otherwise.
88
89 @param func: function to run.
90
91 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -070092 try:
93 func()
94 except (exceptions.BrowserGoneException,
95 exceptions.BrowserConnectionGoneException):
96 return True
97 return False
98
99
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700100 def is_logged_in(self):
101 """Returns true iff logged in."""
102 # TODO(achuith): Do this better.
103 return os.path.exists('/var/run/state/logged-in')