summaryrefslogtreecommitdiffstats
path: root/tools/perf/profile_creators/profile_extender.py
blob: b1dccfa68bb897cbcf05a852af9dd2ebee112306 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from telemetry.core import browser_finder
from telemetry.core import browser_finder_exceptions
from telemetry.core import platform
from telemetry.core import wpr_modes


class ProfileExtender(object):
  """Abstract base class for an object that constructs a Chrome profile."""

  def __init__(self, finder_options):
    """Initializer.

    |finder_options| is an instance of BrowserFinderOptions. When subclass
    implementations of this method inevitably attempt to find and launch a
    browser, they should pass |finder_options| to the relevant methods.

    Several properties of |finder_options| might require direct manipulation by
    subclasses. These are:
      |finder_options.output_profile_path|: The path at which the profile
      should be created.
      |finder_options.browser_options.profile_dir|: If this property is None,
      then a new profile is created. Otherwise, the existing profile is
      appended on to.
    """
    self._finder_options = finder_options

    # A reference to the browser that will be performing all of the tab
    # navigations.
    # This member is initialized during SetUpBrowser().
    self._browser = None

  def Run(self):
    """Creates or extends the profile."""
    raise NotImplementedError()

  def WebPageReplayArchivePath(self):
    """Returns the path to the WPR archive.

    Can be overridden by subclasses.
    """
    return None

  @property
  def finder_options(self):
    """The options to use to find and run the browser."""
    return self._finder_options

  @property
  def profile_path(self):
    """The path of the profile that the browser will use while it's running."""
    return self.finder_options.output_profile_path

  @property
  def browser(self):
    return self._browser

  def SetUpBrowser(self):
    """Finds and starts the browser.

    Can be overridden by subclasses. The subclass implementation must call the
    super class implementation.

    Subclasses do not need to call this method. This method is only necessary
    if the subclass needs to start a browser. If a subclass does call this
    method, the subclass must also call TearDownBrowser().
    """
    possible_browser = self._GetPossibleBrowser(self.finder_options)

    assert possible_browser.supports_tab_control
    assert (platform.GetHostPlatform().GetOSName() in
        ["win", "mac", "linux"])

    self._SetUpWebPageReplay(self.finder_options, possible_browser)
    self._browser = possible_browser.Create(self.finder_options)

  def TearDownBrowser(self):
    """Tears down the browser.

    Can be overridden by subclasses. The subclass implementation must call the
    super class implementation.
    """
    if self._browser:
      self._browser.Close()
      self._browser = None

  def FetchWebPageReplayArchives(self):
    """Fetches the web page replay archives.

    Can be overridden by subclasses.
    """
    pass

  def _SetUpWebPageReplay(self, finder_options, possible_browser):
    """Sets up Web Page Replay, if necessary."""

    wpr_archive_path = self.WebPageReplayArchivePath()
    if not wpr_archive_path:
      return

    self.FetchWebPageReplayArchives()

    # The browser options needs to be passed to both the network controller
    # as well as the browser backend.
    browser_options = finder_options.browser_options
    if finder_options.use_live_sites:
      browser_options.wpr_mode = wpr_modes.WPR_OFF
    else:
      browser_options.wpr_mode = wpr_modes.WPR_REPLAY

    network_controller = possible_browser.platform.network_controller
    make_javascript_deterministic = True

    network_controller.SetReplayArgs(
        wpr_archive_path, browser_options.wpr_mode, browser_options.netsim,
        browser_options.extra_wpr_args, make_javascript_deterministic)

  def _GetPossibleBrowser(self, finder_options):
    """Return a possible_browser with the given options."""
    possible_browser = browser_finder.FindBrowser(finder_options)
    if not possible_browser:
      raise browser_finder_exceptions.BrowserFinderException(
          'No browser found.\n\nAvailable browsers:\n%s\n' %
          '\n'.join(browser_finder.GetAllAvailableBrowserTypes(finder_options)))
    finder_options.browser_options.browser_type = (
        possible_browser.browser_type)

    return possible_browser