Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / about_tracing / record_and_capture_controller.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/record_selection_dialog.html">
9
10 <script>
11 'use strict';
12
13 tvcm.exportTo('about_tracing', function() {
14   function beginMonitoring(tracingControllerClient) {
15     var finalPromiseResolver;
16     var finalPromise = new Promise(function(resolve, reject) {
17       finalPromiseResolver = {
18         resolve: resolve,
19         reject: reject
20       };
21     });
22
23     // TODO(haraken): Implement a configure dialog to set these options.
24     var monitoringOptions = {
25       categoryFilter: '*',
26       useSystemTracing: false,
27       useContinuousTracing: false,
28       useSampling: true
29     };
30
31
32     var beginMonitoringPromise = tracingControllerClient.beginMonitoring(
33         monitoringOptions);
34
35     beginMonitoringPromise.then(
36         function() {
37           finalPromiseResolver.resolve();
38         },
39         function(err) {
40           finalPromiseResolver.reject(err);
41         });
42
43     return finalPromise;
44   }
45
46   function endMonitoring(tracingControllerClient) {
47     var endMonitoringPromise = tracingControllerClient.endMonitoring();
48     return endMonitoringPromise.then(
49         function() {
50         },
51         function(err) {
52         });
53   }
54
55   function captureMonitoring(tracingControllerClient) {
56     var captureMonitoringPromise =
57         tracingControllerClient.captureMonitoring();
58     return captureMonitoringPromise;
59   }
60
61   function getMonitoringStatus(tracingControllerClient) {
62     var getMonitoringStatusPromise =
63         tracingControllerClient.getMonitoringStatus();
64     return getMonitoringStatusPromise;
65   }
66
67   function beginRecording(tracingControllerClient) {
68     var finalPromiseResolver;
69     var finalPromise = new Promise(function(resolve, reject) {
70       finalPromiseResolver = {
71         resolve: resolve,
72         reject: reject
73       };
74     });
75     finalPromise.selectionDlg = undefined;
76     finalPromise.progressDlg = undefined;
77
78     function beginRecordingError(err) {
79       finalPromiseResolver.reject(err);
80     }
81
82     // Step 0: End recording. This is necessary when the user reloads the
83     // about:tracing page when we are recording. Window.onbeforeunload is not
84     // reliable to end recording on reload.
85     endRecording(tracingControllerClient).then(
86         getCategories,
87         getCategories);  // Ignore error.
88
89     // But just in case, bind onbeforeunload anyway.
90     window.onbeforeunload = function(e) {
91       endRecording(tracingControllerClient);
92     }
93
94     // Step 1: Get categories.
95     function getCategories() {
96       tracingControllerClient.getCategories().then(
97           showTracingDialog,
98           beginRecordingError);
99     }
100
101     // Step 2: Show tracing dialog.
102     var selectionDlg;
103     function showTracingDialog(categories) {
104       selectionDlg = new about_tracing.RecordSelectionDialog();
105       selectionDlg.categories = categories;
106       selectionDlg.settings_key = 'about_tracing.record_selection_dialog';
107       selectionDlg.addEventListener('recordclick', startTracing);
108       selectionDlg.addEventListener('closeclick', cancelRecording);
109       selectionDlg.visible = true;
110
111       finalPromise.selectionDlg = selectionDlg;
112     }
113
114     function cancelRecording() {
115       finalPromise.selectionDlg = undefined;
116       finalPromiseResolver.reject(new UserCancelledError());
117     }
118
119     // Step 2: Do the actual tracing dialog.
120     var progressDlg;
121     var bufferPercentFullDiv;
122     function startTracing() {
123       progressDlg = new tvcm.ui.Overlay();
124       progressDlg.textContent = 'Recording...';
125       progressDlg.userCanClose = false;
126
127       bufferPercentFullDiv = document.createElement('div');
128       progressDlg.appendChild(bufferPercentFullDiv);
129
130       var stopButton = document.createElement('button');
131       stopButton.textContent = 'Stop';
132       progressDlg.clickStopButton = function() {
133         stopButton.click();
134       };
135       progressDlg.appendChild(stopButton);
136
137       var recordingOptions = {
138         categoryFilter: selectionDlg.categoryFilter(),
139         useSystemTracing: selectionDlg.useSystemTracing,
140         useContinuousTracing: selectionDlg.useContinuousTracing,
141         useSampling: selectionDlg.useSampling
142       };
143
144
145       var requestPromise = tracingControllerClient.beginRecording(
146           recordingOptions);
147       requestPromise.then(
148           function() {
149             progressDlg.visible = true;
150             stopButton.focus();
151             updateBufferPercentFull('0');
152           },
153           recordFailed);
154
155       stopButton.addEventListener('click', function() {
156         // TODO(chrishenry): Currently, this only dismiss the progress
157         // dialog when tracingComplete event is received. When performing
158         // remote debugging, the tracingComplete event may be delayed
159         // considerable. We should indicate to user that we are waiting
160         // for tracingComplete event instead of being unresponsive. (For
161         // now, I disable the "stop" button, since clicking on the button
162         // again now cause exception.)
163         var recordingPromise = endRecording(tracingControllerClient);
164         recordingPromise.then(
165             recordFinished,
166             recordFailed);
167         stopButton.disabled = true;
168         bufferPercentFullDiv = undefined;
169       });
170       finalPromise.progressDlg = progressDlg;
171     }
172
173     function recordFinished(tracedData) {
174       progressDlg.visible = false;
175       finalPromise.progressDlg = undefined;
176       finalPromiseResolver.resolve(tracedData);
177     }
178
179     function recordFailed(err) {
180       progressDlg.visible = false;
181       finalPromise.progressDlg = undefined;
182       finalPromiseResolver.reject(err);
183     }
184
185     function getBufferPercentFull() {
186       if (!bufferPercentFullDiv)
187         return;
188
189       tracingControllerClient.beginGetBufferPercentFull().then(
190           updateBufferPercentFull);
191     }
192
193     function updateBufferPercentFull(percent_full) {
194       if (!bufferPercentFullDiv)
195         return;
196
197       percent_full = parseFloat(percent_full);
198       var newText = 'Buffer usage: ' + Math.round(100 * percent_full) + '%';
199       if (bufferPercentFullDiv.textContent != newText)
200         bufferPercentFullDiv.textContent = newText;
201
202       window.setTimeout(getBufferPercentFull, 500);
203     }
204
205     // Thats it! We're done.
206     return finalPromise;
207   };
208
209   function endRecording(tracingControllerClient) {
210     return tracingControllerClient.endRecording();
211   }
212
213   function UserCancelledError() {
214     Error.apply(this, arguments);
215   }
216   UserCancelledError.prototype = {
217     __proto__: Error.prototype
218   };
219
220   return {
221     beginRecording: beginRecording,
222     beginMonitoring: beginMonitoring,
223     endMonitoring: endMonitoring,
224     captureMonitoring: captureMonitoring,
225     getMonitoringStatus: getMonitoringStatus,
226     UserCancelledError: UserCancelledError
227   };
228 });
229 </script>
230