Update WebCore.FeatureObserver histogram owner.
[chromium-blink-merge.git] / tools / perf / profile_creators / profile_extender.py
blob22ace38fbe5ad168be2f39b89fd5555c551b1a3e
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
31 # A reference to the browser that will be performing all of the tab
32 # navigations.
33 # This member is initialized during SetUpBrowser().
34 self._browser = None
36 def Run(self):
37 """Creates or extends the profile."""
38 raise NotImplementedError()
40 def WebPageReplayArchivePath(self):
41 """Returns the path to the WPR archive.
43 Can be overridden by subclasses.
44 """
45 return None
47 @property
48 def finder_options(self):
49 """The options to use to find and run the browser."""
50 return self._finder_options
52 @property
53 def profile_path(self):
54 """The path of the profile that the browser will use while it's running."""
55 return self.finder_options.output_profile_path
57 @property
58 def browser(self):
59 return self._browser
61 def SetUpBrowser(self):
62 """Finds and starts the browser.
64 Can be overridden by subclasses. The subclass implementation must call the
65 super class implementation.
67 Subclasses do not need to call this method. This method is only necessary
68 if the subclass needs to start a browser. If a subclass does call this
69 method, the subclass must also call TearDownBrowser().
70 """
71 possible_browser = self._GetPossibleBrowser(self.finder_options)
73 assert possible_browser.supports_tab_control
74 assert (platform.GetHostPlatform().GetOSName() in
75 ["win", "mac", "linux"])
77 self._SetUpWebPageReplay(self.finder_options, possible_browser)
78 self._browser = possible_browser.Create(self.finder_options)
80 def TearDownBrowser(self):
81 """Tears down the browser.
83 Can be overridden by subclasses. The subclass implementation must call the
84 super class implementation.
85 """
86 if self._browser:
87 self._browser.Close()
88 self._browser = None
90 def FetchWebPageReplayArchives(self):
91 """Fetches the web page replay archives.
93 Can be overridden by subclasses.
94 """
95 pass
97 def _SetUpWebPageReplay(self, finder_options, possible_browser):
98 """Sets up Web Page Replay, if necessary."""
100 wpr_archive_path = self.WebPageReplayArchivePath()
101 if not wpr_archive_path:
102 return
104 self.FetchWebPageReplayArchives()
106 # The browser options needs to be passed to both the network controller
107 # as well as the browser backend.
108 browser_options = finder_options.browser_options
109 if finder_options.use_live_sites:
110 browser_options.wpr_mode = wpr_modes.WPR_OFF
111 else:
112 browser_options.wpr_mode = wpr_modes.WPR_REPLAY
114 network_controller = possible_browser.platform.network_controller
115 make_javascript_deterministic = True
117 network_controller.SetReplayArgs(
118 wpr_archive_path, browser_options.wpr_mode, browser_options.netsim,
119 browser_options.extra_wpr_args, make_javascript_deterministic)
121 def _GetPossibleBrowser(self, finder_options):
122 """Return a possible_browser with the given options."""
123 possible_browser = browser_finder.FindBrowser(finder_options)
124 if not possible_browser:
125 raise browser_finder_exceptions.BrowserFinderException(
126 'No browser found.\n\nAvailable browsers:\n%s\n' %
127 '\n'.join(browser_finder.GetAllAvailableBrowserTypes(finder_options)))
128 finder_options.browser_options.browser_type = (
129 possible_browser.browser_type)
131 return possible_browser