| <!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/base/base64.html"> |
| <link rel="import" |
| href="/tracing/ui/extras/about_tracing/tracing_controller_client.html"> |
| |
| <script> |
| 'use strict'; |
| |
| tr.exportTo('tr.ui.e.about_tracing', function() { |
| var Base64 = tr.b.Base64; |
| |
| function beginXhr(method, path, data) { |
| if (data === undefined) |
| data = null; |
| return new Promise(function(resolve, reject) { |
| var req = new XMLHttpRequest(); |
| if (method != 'POST' && data !== null) |
| throw new Error('Non-POST should have data==null'); |
| req.open(method, path, true); |
| req.onreadystatechange = function(e) { |
| if (req.readyState == 4) { |
| window.setTimeout(function() { |
| if (req.status == 200 && req.responseText != '##ERROR##') { |
| resolve(req.responseText); |
| } else { |
| reject(new Error('Error occured at ' + path)); |
| } |
| }, 0); |
| } |
| }; |
| req.send(data); |
| }); |
| } |
| |
| /** |
| * @constructor |
| */ |
| function XhrBasedTracingControllerClient() { } |
| |
| XhrBasedTracingControllerClient.prototype = { |
| __proto__: tr.ui.e.about_tracing.TracingControllerClient.prototype, |
| |
| beginMonitoring: function(monitoringOptions) { |
| var monitoringOptionsB64 = Base64.btoa(JSON.stringify(monitoringOptions)); |
| return beginXhr('GET', '/json/begin_monitoring?' + monitoringOptionsB64); |
| }, |
| |
| endMonitoring: function() { |
| return beginXhr('GET', '/json/end_monitoring'); |
| }, |
| |
| captureMonitoring: function() { |
| return beginXhr('GET', '/json/capture_monitoring_compressed').then( |
| function(data) { |
| var decoded_size = Base64.getDecodedBufferLength(data); |
| var buffer = new ArrayBuffer(decoded_size); |
| Base64.DecodeToTypedArray(data, new DataView(buffer)); |
| return buffer; |
| } |
| ); |
| }, |
| |
| getMonitoringStatus: function() { |
| return beginXhr('GET', '/json/get_monitoring_status').then( |
| function(monitoringOptionsB64) { |
| return JSON.parse(Base64.atob(monitoringOptionsB64)); |
| }); |
| }, |
| |
| getCategories: function() { |
| return beginXhr('GET', '/json/categories').then( |
| function(json) { |
| return JSON.parse(json); |
| }); |
| }, |
| |
| beginRecording: function(recordingOptions) { |
| var recordingOptionsB64 = Base64.btoa(JSON.stringify(recordingOptions)); |
| return beginXhr('GET', '/json/begin_recording?' + |
| recordingOptionsB64); |
| }, |
| |
| beginGetBufferPercentFull: function() { |
| return beginXhr('GET', '/json/get_buffer_percent_full'); |
| }, |
| |
| endRecording: function() { |
| return beginXhr('GET', '/json/end_recording_compressed').then( |
| function(data) { |
| var decoded_size = Base64.getDecodedBufferLength(data); |
| var buffer = new ArrayBuffer(decoded_size); |
| Base64.DecodeToTypedArray(data, new DataView(buffer)); |
| return buffer; |
| } |
| ); |
| }, |
| |
| defaultTraceName: function() { |
| return 'trace.json.gz'; |
| } |
| }; |
| |
| return { |
| XhrBasedTracingControllerClient: XhrBasedTracingControllerClient |
| }; |
| }); |
| </script> |