Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / about_tracing / inspector_tracing_controller_client.html
1 <!DOCTYPE html>
2 <!--
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.
6 -->
7
8 <link rel="import" href="/about_tracing/tracing_controller_client.html">
9 <link rel="import" href="/about_tracing/inspector_connection.html">
10
11 <script>
12 'use strict';
13
14 tvcm.exportTo('about_tracing', function() {
15   function createResolvedPromise(data) {
16     var promise = new Promise(function(resolve, reject) {
17       if (data)
18         resolve(data);
19       else
20         resolve();
21     });
22     return promise;
23   }
24
25   /**
26    * Controls tracing using the inspector's FrontendAgentHost APIs.
27    *
28    * @constructor
29    */
30   function InspectorTracingControllerClient() {
31     this.recording_ = false;
32     this.bufferUsage_ = 0;
33     this.conn_ = about_tracing.InspectorConnection.instance;
34     this.currentTraceTextChunks_ = undefined;
35   }
36
37   InspectorTracingControllerClient.prototype = {
38     __proto__: about_tracing.TracingControllerClient.prototype,
39
40     beginMonitoring: function(monitoringOptions) {
41       throw new Error('Not implemented');
42     },
43
44     endMonitoring: function() {
45       throw new Error('Not implemented');
46     },
47
48     captureMonitoring: function() {
49       throw new Error('Not implemented');
50     },
51
52     getMonitoringStatus: function() {
53       return createResolvedPromise({
54         isMonitoring: false,
55         categoryFilter: '',
56         useSystemTracing: false,
57         useContinuousTracing: false,
58         useSampling: false
59       });
60     },
61
62     getCategories: function() {
63       var res = this.conn_.req('Tracing.getCategories', {});
64       return res.then(function(result) {
65         return result.categories;
66       }, function(err) {
67         return [];
68       });
69     },
70
71     beginRecording: function(recordingOptions) {
72       if (this.recording_)
73         throw new Error('Already recording');
74       this.recording_ = 'starting';
75       var res = this.conn_.req(
76           'Tracing.start',
77           {
78             categories: recordingOptions.categoryFilter,
79             options:
80                 [
81                   (recordingOptions.useContinuousTracing ?
82                      'record-continuously' : ''),
83                   (recordingOptions.useSampling ?
84                      'enable-sampling' : '')
85                 ].join(),
86             bufferUsageReportingInterval: 1000
87           });
88       res = res.then(
89           function ok() {
90             this.conn_.setNotificationListener(
91                 'Tracing.bufferUsage',
92                 this.onBufferUsageUpdateFromInspector_.bind(this));
93             this.recording_ = true;
94           }.bind(this),
95           function error() {
96             this.recording_ = false;
97           }.bind(this));
98       return res;
99     },
100
101     onBufferUsageUpdateFromInspector_: function(params) {
102       this.bufferUsage_ = params.value;
103     },
104
105     beginGetBufferPercentFull: function() {
106       var that = this;
107       return new Promise(function(resolve, reject) {
108         setTimeout(function() {
109           resolve(that.bufferUsage_);
110         }, 100);
111       });
112     },
113
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');
121
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');
127
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);
134     },
135
136     endRecording: function() {
137       if (this.recording_ === false)
138         return createResolvedPromise();
139
140       if (this.recording_ !== true)
141         throw new Error('Cannot end');
142
143       this.currentTraceTextChunks_ = ['['];
144       this.conn_.setNotificationListener(
145           'Tracing.dataCollected', this.onDataCollected_.bind(this));
146
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);
154       }.bind(this);
155
156       this.recording_ = 'stopping';
157       return new Promise(function(resolve, reject) {
158         function tracingComplete() {
159           clearListeners();
160           this.recording_ = false;
161           this.currentTraceTextChunks_.push(']');
162           var traceText = this.currentTraceTextChunks_.join('');
163           this.currentTraceTextChunks_ = undefined;
164           resolve(traceText);
165         }
166
167         function tracingFailed(err) {
168           clearListeners();
169           this.recording_ = false;
170           reject(err);
171         }
172
173         this.conn_.setNotificationListener(
174             'Tracing.tracingComplete', tracingComplete.bind(this));
175         this.conn_.req('Tracing.end', {}).then(
176             function end() {
177               // Nothing happens here. We're really waiting for
178               // Tracing.tracingComplete.
179             }.bind(this),
180             tracingFailed.bind(this));
181       }.bind(this));
182     }
183   };
184
185   return {
186     InspectorTracingControllerClient: InspectorTracingControllerClient
187   };
188 });
189 </script>