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.
4 from telemetry.core.timeline import model
6 class TabBackendException(Exception):
9 class InspectorTimeline(object):
10 """Implementation of dev tools timeline."""
11 class Recorder(object):
12 """Utility class to Start / Stop recording timeline."""
13 def __init__(self, tab):
17 self._tab.StartTimelineRecording()
19 def __exit__(self, *args):
20 self._tab.StopTimelineRecording()
22 def __init__(self, inspector_backend):
23 self._inspector_backend = inspector_backend
24 self._is_recording = False
25 self._timeline_model = None
26 self._raw_events = None
29 def timeline_model(self):
30 return self._timeline_model
33 if self._is_recording:
35 self._is_recording = True
36 self._timeline_model = None
38 self._inspector_backend.RegisterDomain('Timeline',
39 self._OnNotification, self._OnClose)
40 req = {'method': 'Timeline.start'}
41 self._SendSyncRequest(req)
44 if not self._is_recording:
45 raise TabBackendException('Stop() called but not started')
46 self._is_recording = False
47 self._timeline_model = model.TimelineModel(event_data=self._raw_events,
48 shift_world_to_zero=False)
49 req = {'method': 'Timeline.stop'}
50 self._SendSyncRequest(req)
51 self._inspector_backend.UnregisterDomain('Timeline')
53 def _SendSyncRequest(self, req, timeout=60):
54 res = self._inspector_backend.SyncRequest(req, timeout)
56 raise TabBackendException(res['error']['message'])
59 def _OnNotification(self, msg):
60 if not self._is_recording:
62 if 'method' in msg and msg['method'] == 'Timeline.eventRecorded':
63 self._OnEventRecorded(msg)
65 def _OnEventRecorded(self, msg):
66 record = msg.get('params', {}).get('record')
68 self._raw_events.append(record)