1 # Copyright (c) 2012 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.
8 from timeline_model import TimelineModel
14 """Controlls chrome and system tracing, returning a TimelineModel."""
16 def __init__(self, browser, tab_tracker):
19 browser: an instance of a PyUITest
20 tab_tracker: an instance of a TabTracker
22 self._browser = browser
23 self._tab_tracker = tab_tracker
24 self._tab_uuid = tab_tracker.CreateTab('chrome://tracing')
26 # TODO(eatnumber): Find a way to import timeline_model_shim.js from within
28 # I'm not happy with importing timeline_model_shim.js
29 # here. I'd rather pull it in from within TimelineModelProxy.
30 # tracing_test.js depends on timeline_model_shim.js however.
31 files = ['timeline_model_shim.js', 'tracer.js']
32 for fileName in files:
33 with open(os.path.join(os.path.dirname(__file__), fileName), 'r') as fd:
34 self._ExecuteJavascript(fd.read())
37 self._tab_tracker.ReleaseTab(self._tab_uuid)
39 def _ExecuteJavascript(self, js):
40 return self._browser.ExecuteJavascript(
42 windex = self._tab_tracker.GetWindowIndex(),
43 tab_index = self._tab_tracker.GetTabIndex(self._tab_uuid)
46 def BeginTracing(self, system_tracing=True):
50 system_tracing: whether or not to gather system traces along with chrome
53 self._ExecuteJavascript("""
54 window.domAutomationController.send(
55 window.pyautoRecordTrace(%s)
57 """ % ('true' if system_tracing else 'false'))
60 """End tracing and return a TimelineModel.
63 an instance of a TimelineModel which contains the results of the trace
66 js_executor = self._ExecuteJavascript.__get__(self, Tracer),
67 shim_id = self._ExecuteJavascript('tracingController.endTracing();')
71 class TracerFactory(object):
72 """A TracerFactory is used to produce a Tracer.
74 It's recommended to use the same TracerFactory to produce all Tracers so that
75 the same TabTracker can be used throughout
78 browser: an instance of a PyUITest
80 def __init__(self, browser):
81 self._tab_tracker = tab_tracker.TabTracker(browser)
82 self._browser = browser
85 """Produce an instance of a Tracer"""
86 return Tracer(self._browser, self._tab_tracker)