2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @extends {WebInspector.TargetAwareObject}
34 * @param {!WebInspector.Target} target
36 WebInspector.TimelineManager = function(target)
38 WebInspector.TargetAwareObject.call(this, target);
39 this._dispatcher = new WebInspector.TimelineDispatcher(this);
40 this._enablementCount = 0;
41 this._jsProfilerStarted = false;
42 TimelineAgent.enable();
45 WebInspector.TimelineManager.EventTypes = {
46 TimelineStarted: "TimelineStarted",
47 TimelineStopped: "TimelineStopped",
48 TimelineEventRecorded: "TimelineEventRecorded",
49 TimelineProgress: "TimelineProgress"
52 WebInspector.TimelineManager.prototype = {
58 return this._dispatcher.isStarted();
62 * @param {number=} maxCallStackDepth
63 * @param {boolean=} bufferEvents
64 * @param {string=} liveEvents
65 * @param {boolean=} includeCounters
66 * @param {boolean=} includeGPUEvents
67 * @param {function(?Protocol.Error)=} callback
69 start: function(maxCallStackDepth, bufferEvents, liveEvents, includeCounters, includeGPUEvents, callback)
71 this._enablementCount++;
72 this.target().profilingLock.acquire();
73 if (WebInspector.experimentsSettings.timelineJSCPUProfile.isEnabled() && maxCallStackDepth) {
74 this._configureCpuProfilerSamplingInterval();
75 this._jsProfilerStarted = true;
76 ProfilerAgent.start();
78 if (this._enablementCount === 1)
79 TimelineAgent.start(maxCallStackDepth, bufferEvents, liveEvents, includeCounters, includeGPUEvents, callback);
85 * @param {function(?Protocol.Error,?ProfilerAgent.CPUProfile)} callback
87 stop: function(callback)
89 this._enablementCount--;
90 if (this._enablementCount < 0) {
91 console.error("WebInspector.TimelineManager start/stop calls are unbalanced " + new Error().stack);
95 var masterError = null;
96 var masterProfile = null;
97 var callbackBarrier = new CallbackBarrier();
99 if (this._jsProfilerStarted) {
100 ProfilerAgent.stop(callbackBarrier.createCallback(profilerCallback));
101 this._jsProfilerStarted = false;
103 if (!this._enablementCount)
104 TimelineAgent.stop(callbackBarrier.createCallback(this._processBufferedEvents.bind(this, timelineCallback)));
106 callbackBarrier.callWhenDone(allDoneCallback.bind(this));
109 * @param {?Protocol.Error} error
111 function timelineCallback(error)
113 masterError = masterError || error;
117 * @param {?Protocol.Error} error
118 * @param {!ProfilerAgent.CPUProfile} profile
120 function profilerCallback(error, profile)
122 masterError = masterError || error;
123 masterProfile = profile;
127 * @this {WebInspector.TimelineManager}
129 function allDoneCallback()
131 this.target().profilingLock.release();
132 callback(masterError, masterProfile);
137 * @param {function(?Protocol.Error)|undefined} callback
138 * @param {?Protocol.Error} error
139 * @param {!Array.<!TimelineAgent.TimelineEvent>=} events
141 _processBufferedEvents: function(callback, error, events)
144 for (var i = 0; i < events.length; ++i)
145 this._dispatcher.eventRecorded(events[i]);
151 _configureCpuProfilerSamplingInterval: function()
153 var intervalUs = WebInspector.settings.highResolutionCpuProfiling.get() ? 100 : 1000;
154 ProfilerAgent.setSamplingInterval(intervalUs, didChangeInterval);
155 function didChangeInterval(error)
158 WebInspector.console.showErrorMessage(error);
162 __proto__: WebInspector.TargetAwareObject.prototype
167 * @implements {TimelineAgent.Dispatcher}
169 WebInspector.TimelineDispatcher = function(manager)
171 this._manager = manager;
172 InspectorBackend.registerTimelineDispatcher(this);
175 WebInspector.TimelineDispatcher.prototype = {
177 * @param {!TimelineAgent.TimelineEvent} record
179 eventRecorded: function(record)
181 this._manager.dispatchEventToListeners(WebInspector.TimelineManager.EventTypes.TimelineEventRecorded, record);
187 isStarted: function()
189 return !!this._started;
193 * @param {boolean=} consoleTimeline
195 started: function(consoleTimeline)
197 if (consoleTimeline) {
198 // Wake up timeline panel module.
199 WebInspector.moduleManager.loadModule("timeline");
201 this._started = true;
202 this._manager.dispatchEventToListeners(WebInspector.TimelineManager.EventTypes.TimelineStarted, consoleTimeline);
206 * @param {boolean=} consoleTimeline
208 stopped: function(consoleTimeline)
210 this._started = false;
211 this._manager.dispatchEventToListeners(WebInspector.TimelineManager.EventTypes.TimelineStopped, consoleTimeline);
215 * @param {number} count
217 progress: function(count)
219 this._manager.dispatchEventToListeners(WebInspector.TimelineManager.EventTypes.TimelineProgress, count);
224 * @type {!WebInspector.TimelineManager}
226 WebInspector.timelineManager;