summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/ui/extras/about_tracing/record_controller.html
blob: a72ddfeba07af1edddb8a7fea221adbb583d55b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<!--
Copyright (c) 2013 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/ui/extras/about_tracing/record_selection_dialog.html">

<script>
'use strict';

tr.exportTo('tr.ui.e.about_tracing', function() {
  function beginRecording(tracingControllerClient) {
    var finalPromiseResolver;
    var finalPromise = new Promise(function(resolve, reject) {
      finalPromiseResolver = {
        resolve: resolve,
        reject: reject
      };
    });
    finalPromise.selectionDlg = undefined;
    finalPromise.progressDlg = undefined;

    function beginRecordingError(err) {
      finalPromiseResolver.reject(err);
    }

    // Step 0: End recording. This is necessary when the user reloads the
    // about:tracing page when we are recording. Window.onbeforeunload is not
    // reliable to end recording on reload.
    endRecording(tracingControllerClient).then(
        getCategories,
        getCategories);  // Ignore error.

    // But just in case, bind onbeforeunload anyway.
    window.onbeforeunload = function(e) {
      endRecording(tracingControllerClient);
    };

    // Step 1: Get categories.
    function getCategories() {
      var p = tracingControllerClient.getCategories().then(
          showTracingDialog,
          beginRecordingError);
      p.catch(function(err) {
        beginRecordingError(err);
      });
    }

    // Step 2: Show tracing dialog.
    var selectionDlg;
    function showTracingDialog(categories) {
      selectionDlg = new tr.ui.e.about_tracing.RecordSelectionDialog();
      selectionDlg.categories = categories;
      selectionDlg.settings_key =
          'tr.ui.e.about_tracing.record_selection_dialog';
      selectionDlg.addEventListener('recordclick', startTracing);
      selectionDlg.addEventListener('closeclick', cancelRecording);
      selectionDlg.visible = true;

      finalPromise.selectionDlg = selectionDlg;
    }

    function cancelRecording() {
      finalPromise.selectionDlg = undefined;
      finalPromiseResolver.reject(new UserCancelledError());
    }

    // Step 2: Do the actual tracing dialog.
    var progressDlg;
    var bufferPercentFullDiv;
    function startTracing() {
      progressDlg = new tr.ui.b.Overlay();
      Polymer.dom(progressDlg).textContent = 'Recording...';
      progressDlg.userCanClose = false;

      bufferPercentFullDiv = document.createElement('div');
      Polymer.dom(progressDlg).appendChild(bufferPercentFullDiv);

      var stopButton = document.createElement('button');
      Polymer.dom(stopButton).textContent = 'Stop';
      progressDlg.clickStopButton = function() {
        stopButton.click();
      };
      Polymer.dom(progressDlg).appendChild(stopButton);

      var recordingOptions = {
        categoryFilter: selectionDlg.categoryFilter(),
        useSystemTracing: selectionDlg.useSystemTracing,
        tracingRecordMode: selectionDlg.tracingRecordMode,
        useSampling: selectionDlg.useSampling
      };


      var requestPromise = tracingControllerClient.beginRecording(
          recordingOptions);
      requestPromise.then(
          function() {
            progressDlg.visible = true;
            stopButton.focus();
            updateBufferPercentFull('0');
          },
          recordFailed);

      stopButton.addEventListener('click', function() {
        // TODO(chrishenry): Currently, this only dismiss the progress
        // dialog when tracingComplete event is received. When performing
        // remote debugging, the tracingComplete event may be delayed
        // considerable. We should indicate to user that we are waiting
        // for tracingComplete event instead of being unresponsive. (For
        // now, I disable the "stop" button, since clicking on the button
        // again now cause exception.)
        var recordingPromise = endRecording(tracingControllerClient);
        recordingPromise.then(
            recordFinished,
            recordFailed);
        stopButton.disabled = true;
        bufferPercentFullDiv = undefined;
      });
      finalPromise.progressDlg = progressDlg;
    }

    function recordFinished(tracedData) {
      progressDlg.visible = false;
      finalPromise.progressDlg = undefined;
      finalPromiseResolver.resolve(tracedData);
    }

    function recordFailed(err) {
      progressDlg.visible = false;
      finalPromise.progressDlg = undefined;
      finalPromiseResolver.reject(err);
    }

    function getBufferPercentFull() {
      if (!bufferPercentFullDiv)
        return;

      tracingControllerClient.beginGetBufferPercentFull().then(
          updateBufferPercentFull);
    }

    function updateBufferPercentFull(percentFull) {
      if (!bufferPercentFullDiv)
        return;

      percentFull = Math.round(100 * parseFloat(percentFull));
      var newText = 'Buffer usage: ' + percentFull + '%';
      if (Polymer.dom(bufferPercentFullDiv).textContent !== newText)
        Polymer.dom(bufferPercentFullDiv).textContent = newText;

      window.setTimeout(getBufferPercentFull, 500);
    }

    // Thats it! We're done.
    return finalPromise;
  }

  function endRecording(tracingControllerClient) {
    return tracingControllerClient.endRecording();
  }

  function defaultTraceName(tracingControllerClient) {
    return tracingControllerClient.defaultTraceName();
  }

  function UserCancelledError() {
    Error.apply(this, arguments);
  }
  UserCancelledError.prototype = {
    __proto__: Error.prototype
  };

  return {
    beginRecording: beginRecording,
    UserCancelledError: UserCancelledError,
    defaultTraceName: defaultTraceName
  };
});
</script>