Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / sdk / TracingManager.js
1 /*
2  * Copyright 2014 The Chromium Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 /**
8  * @constructor
9  * @extends {WebInspector.Object}
10  * @implements {WebInspector.TargetManager.Observer}
11  */
12 WebInspector.TracingManager = function()
13 {
14     WebInspector.Object.call(this);
15     this._active = false;
16     WebInspector.targetManager.observeTargets(this);
17 }
18
19 WebInspector.TracingManager.Events = {
20     "BufferUsage": "BufferUsage",
21     "TracingStarted": "TracingStarted",
22     "EventsCollected": "EventsCollected",
23     "TracingStopped": "TracingStopped",
24     "TracingComplete": "TracingComplete"
25 }
26
27 /** @typedef {!{
28         cat: string,
29         pid: number,
30         tid: number,
31         ts: number,
32         ph: string,
33         name: string,
34         args: !Object,
35         dur: number,
36         id: number,
37         s: string
38     }}
39  */
40 WebInspector.TracingManager.EventPayload;
41
42 WebInspector.TracingManager.prototype = {
43     /**
44      * @param {!WebInspector.Target} target
45      */
46     targetAdded: function(target)
47     {
48         if (this._target)
49             return;
50         this._target = target;
51         target.registerTracingDispatcher(new WebInspector.TracingDispatcher(this));
52     },
53
54     /**
55      * @param {!WebInspector.Target} target
56      */
57     targetRemoved: function(target)
58     {
59         if (this._target !== target)
60             return;
61         delete this._target;
62     },
63
64     /**
65      * @param {number} usage
66      */
67     _bufferUsage: function(usage)
68     {
69         this.dispatchEventToListeners(WebInspector.TracingManager.Events.BufferUsage, usage);
70     },
71
72     /**
73      * @param {!Array.<!WebInspector.TracingManager.EventPayload>} events
74      */
75     _eventsCollected: function(events)
76     {
77         this.dispatchEventToListeners(WebInspector.TracingManager.Events.EventsCollected, events);
78     },
79
80     _tracingComplete: function()
81     {
82         this.dispatchEventToListeners(WebInspector.TracingManager.Events.TracingComplete);
83     },
84
85     _tracingStarted: function()
86     {
87         if (this._active)
88             return;
89         this._active = true;
90         this.dispatchEventToListeners(WebInspector.TracingManager.Events.TracingStarted);
91     },
92
93     /**
94      * @param {string} categoryFilter
95      * @param {string} options
96      * @param {function(?string)=} callback
97      */
98     start: function(categoryFilter, options, callback)
99     {
100         if (this._active)
101             return;
102         WebInspector.profilingLock().acquire();
103         this._shouldReleaseLock = true;
104         var bufferUsageReportingIntervalMs = 500;
105         TracingAgent.start(categoryFilter, options, bufferUsageReportingIntervalMs, callback);
106         this._tracingStarted();
107         this._active = true;
108     },
109
110     stop: function()
111     {
112         if (!this._active)
113             return;
114         TracingAgent.end(this._onStop.bind(this));
115         if (this._shouldReleaseLock) {
116             this._shouldReleaseLock = false;
117             WebInspector.profilingLock().release();
118         }
119     },
120
121     _onStop: function()
122     {
123         if (!this._active)
124             return;
125         this.dispatchEventToListeners(WebInspector.TracingManager.Events.TracingStopped);
126         this._active = false;
127     },
128
129     __proto__: WebInspector.Object.prototype
130 }
131
132 /**
133  * @constructor
134  * @implements {TracingAgent.Dispatcher}
135  * @param {!WebInspector.TracingManager} tracingManager
136  */
137 WebInspector.TracingDispatcher = function(tracingManager)
138 {
139     this._tracingManager = tracingManager;
140 }
141
142 WebInspector.TracingDispatcher.prototype = {
143     /**
144      * @param {number} usage
145      */
146     bufferUsage: function(usage)
147     {
148         this._tracingManager._bufferUsage(usage);
149     },
150
151     /**
152      * @param {!Array.<!WebInspector.TracingManager.EventPayload>} data
153      */
154     dataCollected: function(data)
155     {
156         this._tracingManager._eventsCollected(data);
157     },
158
159     tracingComplete: function()
160     {
161         this._tracingManager._tracingComplete();
162     },
163
164     started: function()
165     {
166         this._tracingManager._tracingStarted();
167     }
168 }