Update To 11.40.268.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 tv.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   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)
30     if (!m)
31       throw new Error('Malformed response');
32
33     if (chunks.length > 1)
34       chunks.push(',');
35     chunks.push(m[1]);
36   }
37
38   /**
39    * Controls tracing using the inspector's FrontendAgentHost APIs.
40    *
41    * @constructor
42    */
43   function InspectorTracingControllerClient() {
44     this.recording_ = false;
45     this.bufferUsage_ = 0;
46     this.conn_ = about_tracing.InspectorConnection.instance;
47     this.currentTraceTextChunks_ = undefined;
48   }
49
50   InspectorTracingControllerClient.prototype = {
51     __proto__: about_tracing.TracingControllerClient.prototype,
52
53     beginMonitoring: function(monitoringOptions) {
54       throw new Error('Not implemented');
55     },
56
57     endMonitoring: function() {
58       throw new Error('Not implemented');
59     },
60
61     captureMonitoring: function() {
62       throw new Error('Not implemented');
63     },
64
65     getMonitoringStatus: function() {
66       return createResolvedPromise({
67         isMonitoring: false,
68         categoryFilter: '',
69         useSystemTracing: false,
70         useContinuousTracing: false,
71         useSampling: false
72       });
73     },
74
75     getCategories: function() {
76       var res = this.conn_.req('Tracing.getCategories', {});
77       return res.then(function(result) {
78         return result.categories;
79       }, function(err) {
80         return [];
81       });
82     },
83
84     beginRecording: function(recordingOptions) {
85       if (this.recording_)
86         throw new Error('Already recording');
87       this.recording_ = 'starting';
88       var res = this.conn_.req(
89           'Tracing.start',
90           {
91             categories: recordingOptions.categoryFilter,
92             options:
93                 [
94                   (recordingOptions.useContinuousTracing ?
95                      'record-continuously' : ''),
96                   (recordingOptions.useSampling ?
97                      'enable-sampling' : '')
98                 ].join(),
99             bufferUsageReportingInterval: 1000
100           });
101       res = res.then(
102           function ok() {
103             this.conn_.setNotificationListener(
104                 'Tracing.bufferUsage',
105                 this.onBufferUsageUpdateFromInspector_.bind(this));
106             this.recording_ = true;
107           }.bind(this),
108           function error() {
109             this.recording_ = false;
110           }.bind(this));
111       return res;
112     },
113
114     onBufferUsageUpdateFromInspector_: function(params) {
115       this.bufferUsage_ = params.value;
116     },
117
118     beginGetBufferPercentFull: function() {
119       var that = this;
120       return new Promise(function(resolve, reject) {
121         setTimeout(function() {
122           resolve(that.bufferUsage_);
123         }, 100);
124       });
125     },
126
127     onDataCollected_: function(messageString) {
128       appendTraceChunksTo(this.currentTraceTextChunks_, messageString);
129     },
130
131     endRecording: function() {
132       if (this.recording_ === false)
133         return createResolvedPromise();
134
135       if (this.recording_ !== true)
136         throw new Error('Cannot end');
137
138       this.currentTraceTextChunks_ = ['['];
139       this.conn_.setNotificationListener(
140           'Tracing.dataCollected', this.onDataCollected_.bind(this));
141
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);
149       }.bind(this);
150
151       this.recording_ = 'stopping';
152       return new Promise(function(resolve, reject) {
153         function tracingComplete() {
154           clearListeners();
155           this.recording_ = false;
156           this.currentTraceTextChunks_.push(']');
157           var traceText = this.currentTraceTextChunks_.join('');
158           this.currentTraceTextChunks_ = undefined;
159           resolve(traceText);
160         }
161
162         function tracingFailed(err) {
163           clearListeners();
164           this.recording_ = false;
165           reject(err);
166         }
167
168         this.conn_.setNotificationListener(
169             'Tracing.tracingComplete', tracingComplete.bind(this));
170         this.conn_.req('Tracing.end', {}).then(
171             function end() {
172               // Nothing happens here. We're really waiting for
173               // Tracing.tracingComplete.
174             }.bind(this),
175             tracingFailed.bind(this));
176       }.bind(this));
177     }
178   };
179
180   return {
181     InspectorTracingControllerClient: InspectorTracingControllerClient,
182     appendTraceChunksTo: appendTraceChunksTo
183   };
184 });
185 </script>