3 Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
8 <link rel="import" href="/about_tracing/tracing_controller_client.html">
9 <link rel="import" href="/about_tracing/inspector_connection.html">
14 tv.exportTo('about_tracing', function() {
15 function createResolvedPromise(data) {
16 var promise = new Promise(function(resolve, reject) {
25 function appendTraceChunksTo(chunks, messageString) {
26 if (typeof messageString !== 'string')
27 throw new Error('Invalid data');
28 var re = /"params":\s*\{\s*"value":\s*\[([^]+)\]\s*\}\s*\}/;
29 var m = re.exec(messageString)
31 throw new Error('Malformed response');
33 if (chunks.length > 1)
39 * Controls tracing using the inspector's FrontendAgentHost APIs.
43 function InspectorTracingControllerClient() {
44 this.recording_ = false;
45 this.bufferUsage_ = 0;
46 this.conn_ = about_tracing.InspectorConnection.instance;
47 this.currentTraceTextChunks_ = undefined;
50 InspectorTracingControllerClient.prototype = {
51 __proto__: about_tracing.TracingControllerClient.prototype,
53 beginMonitoring: function(monitoringOptions) {
54 throw new Error('Not implemented');
57 endMonitoring: function() {
58 throw new Error('Not implemented');
61 captureMonitoring: function() {
62 throw new Error('Not implemented');
65 getMonitoringStatus: function() {
66 return createResolvedPromise({
69 useSystemTracing: false,
70 useContinuousTracing: false,
75 getCategories: function() {
76 var res = this.conn_.req('Tracing.getCategories', {});
77 return res.then(function(result) {
78 return result.categories;
84 beginRecording: function(recordingOptions) {
86 throw new Error('Already recording');
87 this.recording_ = 'starting';
88 var res = this.conn_.req(
91 categories: recordingOptions.categoryFilter,
94 (recordingOptions.useContinuousTracing ?
95 'record-continuously' : ''),
96 (recordingOptions.useSampling ?
97 'enable-sampling' : '')
99 bufferUsageReportingInterval: 1000
103 this.conn_.setNotificationListener(
104 'Tracing.bufferUsage',
105 this.onBufferUsageUpdateFromInspector_.bind(this));
106 this.recording_ = true;
109 this.recording_ = false;
114 onBufferUsageUpdateFromInspector_: function(params) {
115 this.bufferUsage_ = params.value;
118 beginGetBufferPercentFull: function() {
120 return new Promise(function(resolve, reject) {
121 setTimeout(function() {
122 resolve(that.bufferUsage_);
127 onDataCollected_: function(messageString) {
128 appendTraceChunksTo(this.currentTraceTextChunks_, messageString);
131 endRecording: function() {
132 if (this.recording_ === false)
133 return createResolvedPromise();
135 if (this.recording_ !== true)
136 throw new Error('Cannot end');
138 this.currentTraceTextChunks_ = ['['];
139 this.conn_.setNotificationListener(
140 'Tracing.dataCollected', this.onDataCollected_.bind(this));
142 var clearListeners = function() {
143 this.conn_.setNotificationListener(
144 'Tracing.bufferUsage', undefined);
145 this.conn_.setNotificationListener(
146 'Tracing.tracingComplete', undefined);
147 this.conn_.setNotificationListener(
148 'Tracing.dataCollected', undefined);
151 this.recording_ = 'stopping';
152 return new Promise(function(resolve, reject) {
153 function tracingComplete() {
155 this.recording_ = false;
156 this.currentTraceTextChunks_.push(']');
157 var traceText = this.currentTraceTextChunks_.join('');
158 this.currentTraceTextChunks_ = undefined;
162 function tracingFailed(err) {
164 this.recording_ = false;
168 this.conn_.setNotificationListener(
169 'Tracing.tracingComplete', tracingComplete.bind(this));
170 this.conn_.req('Tracing.end', {}).then(
172 // Nothing happens here. We're really waiting for
173 // Tracing.tracingComplete.
175 tracingFailed.bind(this));
181 InspectorTracingControllerClient: InspectorTracingControllerClient,
182 appendTraceChunksTo: appendTraceChunksTo