Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / tools / perf / profile_creators / profile_extender.py
blob936a1cb7a746050486159fbdda106afadd6a606f
1 # Copyright 2013 The Chromium 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.
5 from telemetry.core import platform
6 from telemetry.util import wpr_modes
7 from telemetry.internal.browser import browser_finder
8 from telemetry.internal.browser import browser_finder_exceptions
11 class ProfileExtender(object):
12 """Abstract base class for an object that constructs a Chrome profile."""
14 def __init__(self, finder_options):
15 """Initializer.
17 |finder_options| is an instance of BrowserFinderOptions. When subclass
18 implementations of this method inevitably attempt to find and launch a
19 browser, they should pass |finder_options| to the relevant methods.
21 Several properties of |finder_options| might require direct manipulation by
22 subclasses. These are:
23 |finder_options.output_profile_path|: The path at which the profile
24 should be created.
25 |finder_options.browser_options.profile_dir|: If this property is None,
26 then a new profile is created. Otherwise, the existing profile is
27 appended on to.
28 """
29 self._finder_options = finder_options
30 # Since profile extenders are not supported on remote platforms,
31 # this should be the same as target platform.
32 self._os_name = platform.GetHostPlatform().GetOSName()
34 # A reference to the browser that will be performing all of the tab
35 # navigations.
36 # This member is initialized during SetUpBrowser().
37 self._browser = None
39 def Run(self):
40 """Creates or extends the profile."""
41 raise NotImplementedError()
43 def WebPageReplayArchivePath(self):
44 """Returns the path to the WPR archive.
46 Can be overridden by subclasses.
47 """
48 return None
50 @property
51 def finder_options(self):
52 """The options to use to find and run the browser."""
53 return self._finder_options
55 @property
56 def profile_path(self):
57 """The path of the profile that the browser will use while it's running."""
58 return self.finder_options.output_profile_path
60 @property
61 def browser(self):
62 return self._browser
64 @property
65 def os_name(self):
66 """Name of OS that extender is currently running on."""
67 return self._os_name
69 def EnabledOSList(self):
70 """Returns a list of OSes that this extender can run on.
72 Can be overridden by subclasses.
74 Returns:
75 List of OS ('win', 'mac', or 'linux') that this extender can run on.
76 """
77 return ["win", "mac", "linux"]
79 def SetUpBrowser(self):
80 """Finds and starts the browser.
82 Can be overridden by subclasses. The subclass implementation must call the
83 super class implementation.
85 Subclasses do not need to call this method. This method is only necessary
86 if the subclass needs to start a browser. If a subclass does call this
87 method, the subclass must also call TearDownBrowser().
88 """
89 possible_browser = self._GetPossibleBrowser(self.finder_options)
90 enabled_os_list = self.EnabledOSList()
91 if self._os_name not in enabled_os_list:
92 raise NotImplementedError(
93 'This profile extender on %s is not yet supported'
94 % self._os_name)
95 if possible_browser.IsRemote():
96 raise NotImplementedError(
97 'Profile extenders are not yet supported on remote platforms.')
98 assert possible_browser.supports_tab_control
100 self._SetUpWebPageReplay(self.finder_options, possible_browser)
101 self._browser = possible_browser.Create(self.finder_options)
103 def TearDownBrowser(self):
104 """Tears down the browser.
106 Can be overridden by subclasses. The subclass implementation must call the
107 super class implementation.
109 if self._browser:
110 self._browser.Close()
111 self._browser = None
113 def FetchWebPageReplayArchives(self):
114 """Fetches the web page replay archives.
116 Can be overridden by subclasses.
118 pass
120 def _SetUpWebPageReplay(self, finder_options, possible_browser):
121 """Sets up Web Page Replay, if necessary."""
123 wpr_archive_path = self.WebPageReplayArchivePath()
124 if not wpr_archive_path:
125 return
127 self.FetchWebPageReplayArchives()
129 # The browser options needs to be passed to both the network controller
130 # as well as the browser backend.
131 browser_options = finder_options.browser_options
132 if finder_options.use_live_sites:
133 browser_options.wpr_mode = wpr_modes.WPR_OFF
134 else:
135 browser_options.wpr_mode = wpr_modes.WPR_REPLAY
137 network_controller = possible_browser.platform.network_controller
138 make_javascript_deterministic = True
140 network_controller.SetReplayArgs(
141 wpr_archive_path, browser_options.wpr_mode, browser_options.netsim,
142 browser_options.extra_wpr_args, make_javascript_deterministic)
144 def _GetPossibleBrowser(self, finder_options):
145 """Return a possible_browser with the given options."""
146 possible_browser = browser_finder.FindBrowser(finder_options)
147 if not possible_browser:
148 raise browser_finder_exceptions.BrowserFinderException(
149 'No browser found.\n\nAvailable browsers:\n%s\n' %
150 '\n'.join(browser_finder.GetAllAvailableBrowserTypes(finder_options)))
151 finder_options.browser_options.browser_type = (
152 possible_browser.browser_type)
154 return possible_browser