1<!DOCTYPE html>
2<!--
3Copyright (c) 2013 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/tracing/base/base64.html">
9<link rel="import"
10      href="/tracing/ui/extras/about_tracing/tracing_controller_client.html">
11
12<script>
13'use strict';
14
15tr.exportTo('tr.ui.e.about_tracing', function() {
16  var Base64 = tr.b.Base64;
17
18  function beginXhr(method, path, data) {
19    if (data === undefined)
20      data = null;
21    return new Promise(function(resolve, reject) {
22      var req = new XMLHttpRequest();
23      if (method != 'POST' && data !== null)
24        throw new Error('Non-POST should have data==null');
25      req.open(method, path, true);
26      req.onreadystatechange = function(e) {
27        if (req.readyState == 4) {
28          window.setTimeout(function() {
29            if (req.status == 200 && req.responseText != '##ERROR##') {
30              resolve(req.responseText);
31            } else {
32              reject(new Error('Error occured at ' + path));
33            }
34          }, 0);
35        }
36      };
37      req.send(data);
38    });
39  }
40
41  /**
42   * @constructor
43   */
44  function XhrBasedTracingControllerClient() { }
45
46  XhrBasedTracingControllerClient.prototype = {
47    __proto__: tr.ui.e.about_tracing.TracingControllerClient.prototype,
48
49    beginMonitoring: function(monitoringOptions) {
50      var monitoringOptionsB64 = Base64.btoa(JSON.stringify(monitoringOptions));
51      return beginXhr('GET', '/json/begin_monitoring?' + monitoringOptionsB64);
52    },
53
54    endMonitoring: function() {
55      return beginXhr('GET', '/json/end_monitoring');
56    },
57
58    captureMonitoring: function() {
59      return beginXhr('GET', '/json/capture_monitoring_compressed').then(
60        function(data) {
61          var decoded_size = Base64.getDecodedBufferLength(data);
62          var buffer = new ArrayBuffer(decoded_size);
63          Base64.DecodeToTypedArray(data, new DataView(buffer));
64          return buffer;
65        }
66      );
67    },
68
69    getMonitoringStatus: function() {
70      return beginXhr('GET', '/json/get_monitoring_status').then(
71          function(monitoringOptionsB64) {
72            return JSON.parse(Base64.atob(monitoringOptionsB64));
73          });
74    },
75
76    getCategories: function() {
77      return beginXhr('GET', '/json/categories').then(
78          function(json) {
79            return JSON.parse(json);
80          });
81    },
82
83    beginRecording: function(recordingOptions) {
84      var recordingOptionsB64 = Base64.btoa(JSON.stringify(recordingOptions));
85      return beginXhr('GET', '/json/begin_recording?' +
86                      recordingOptionsB64);
87    },
88
89    beginGetBufferPercentFull: function() {
90      return beginXhr('GET', '/json/get_buffer_percent_full');
91    },
92
93    endRecording: function() {
94      return beginXhr('GET', '/json/end_recording_compressed').then(
95        function(data) {
96          var decoded_size = Base64.getDecodedBufferLength(data);
97          var buffer = new ArrayBuffer(decoded_size);
98          Base64.DecodeToTypedArray(data, new DataView(buffer));
99          return buffer;
100        }
101      );
102    },
103
104    defaultTraceName: function() {
105      return 'trace.json.gz';
106    }
107  };
108
109  return {
110    XhrBasedTracingControllerClient: XhrBasedTracingControllerClient
111  };
112});
113</script>
114