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 tvcm.exportTo('about_tracing', function() {
15 function createResolvedPromise(data) {
16 var promise = new Promise(function(resolve, reject) {
26 * Controls tracing using the inspector's FrontendAgentHost APIs.
30 function InspectorTracingControllerClient() {
31 this.recording_ = false;
32 this.bufferUsage_ = 0;
33 this.conn_ = about_tracing.InspectorConnection.instance;
34 this.currentTraceTextChunks_ = undefined;
37 InspectorTracingControllerClient.prototype = {
38 __proto__: about_tracing.TracingControllerClient.prototype,
40 beginMonitoring: function(monitoringOptions) {
41 throw new Error('Not implemented');
44 endMonitoring: function() {
45 throw new Error('Not implemented');
48 captureMonitoring: function() {
49 throw new Error('Not implemented');
52 getMonitoringStatus: function() {
53 return createResolvedPromise({
56 useSystemTracing: false,
57 useContinuousTracing: false,
62 getCategories: function() {
63 var res = this.conn_.req('Tracing.getCategories', {});
64 return res.then(function(result) {
65 return result.categories;
71 beginRecording: function(recordingOptions) {
73 throw new Error('Already recording');
74 this.recording_ = 'starting';
75 var res = this.conn_.req(
78 categories: recordingOptions.categoryFilter,
81 (recordingOptions.useContinuousTracing ?
82 'record-continuously' : ''),
83 (recordingOptions.useSampling ?
84 'enable-sampling' : '')
86 bufferUsageReportingInterval: 1000
90 this.conn_.setNotificationListener(
91 'Tracing.bufferUsage',
92 this.onBufferUsageUpdateFromInspector_.bind(this));
93 this.recording_ = true;
96 this.recording_ = false;
101 onBufferUsageUpdateFromInspector_: function(params) {
102 this.bufferUsage_ = params.value;
105 beginGetBufferPercentFull: function() {
107 return new Promise(function(resolve, reject) {
108 setTimeout(function() {
109 resolve(that.bufferUsage_);
114 onDataCollected_: function(messageString) {
115 if (typeof messageString !== 'string')
116 throw new Error('Invalid data');
117 var key = '"params": { "value": [ ';
118 var keyBeginIndex = messageString.indexOf(key);
119 if (keyBeginIndex == -1)
120 throw new Error('Malformed response');
122 var expectedEndKey = ' ] } }';
123 var actualEndString = messageString.substring(
124 messageString.length - expectedEndKey.length);
125 if (actualEndString !== expectedEndKey)
126 throw new Error('Expected end key not found');
128 var dataStartIndex = keyBeginIndex + key.length;
129 var data = messageString.substring(
130 dataStartIndex, messageString.length - expectedEndKey.length);
131 if (this.currentTraceTextChunks_.length > 1)
132 this.currentTraceTextChunks_.push(',');
133 this.currentTraceTextChunks_.push(data);
136 endRecording: function() {
137 if (this.recording_ === false)
138 return createResolvedPromise();
140 if (this.recording_ !== true)
141 throw new Error('Cannot end');
143 this.currentTraceTextChunks_ = ['['];
144 this.conn_.setNotificationListener(
145 'Tracing.dataCollected', this.onDataCollected_.bind(this));
147 var clearListeners = function() {
148 this.conn_.setNotificationListener(
149 'Tracing.bufferUsage', undefined);
150 this.conn_.setNotificationListener(
151 'Tracing.tracingComplete', undefined);
152 this.conn_.setNotificationListener(
153 'Tracing.dataCollected', undefined);
156 this.recording_ = 'stopping';
157 return new Promise(function(resolve, reject) {
158 function tracingComplete() {
160 this.recording_ = false;
161 this.currentTraceTextChunks_.push(']');
162 var traceText = this.currentTraceTextChunks_.join('');
163 this.currentTraceTextChunks_ = undefined;
167 function tracingFailed(err) {
169 this.recording_ = false;
173 this.conn_.setNotificationListener(
174 'Tracing.tracingComplete', tracingComplete.bind(this));
175 this.conn_.req('Tracing.end', {}).then(
177 // Nothing happens here. We're really waiting for
178 // Tracing.tracingComplete.
180 tracingFailed.bind(this));
186 InspectorTracingControllerClient: InspectorTracingControllerClient