- add sources.
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / backends / chrome / inspector_timeline.py
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
5
6 class TabBackendException(Exception):
7   pass
8
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):
14       self._tab = tab
15
16     def __enter__(self):
17       self._tab.StartTimelineRecording()
18
19     def __exit__(self, *args):
20       self._tab.StopTimelineRecording()
21
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
27
28   @property
29   def timeline_model(self):
30     return self._timeline_model
31
32   def Start(self):
33     if self._is_recording:
34       return
35     self._is_recording = True
36     self._timeline_model = None
37     self._raw_events = []
38     self._inspector_backend.RegisterDomain('Timeline',
39        self._OnNotification, self._OnClose)
40     req = {'method': 'Timeline.start'}
41     self._SendSyncRequest(req)
42
43   def Stop(self):
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')
52
53   def _SendSyncRequest(self, req, timeout=60):
54     res = self._inspector_backend.SyncRequest(req, timeout)
55     if 'error' in res:
56       raise TabBackendException(res['error']['message'])
57     return res['result']
58
59   def _OnNotification(self, msg):
60     if not self._is_recording:
61       return
62     if 'method' in msg and msg['method'] == 'Timeline.eventRecorded':
63       self._OnEventRecorded(msg)
64
65   def _OnEventRecorded(self, msg):
66     record = msg.get('params', {}).get('record')
67     if record:
68       self._raw_events.append(record)
69
70   def _OnClose(self):
71     pass