Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / remoting / webapp / crd / js / connection_stats.js
blob7ab1652f597d5b66cc8aee21bb4333dccc527d40
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 /**
6 * @fileoverview
7 * Module to support debug overlay window with connection stats.
8 */
10 'use strict';
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
15 /**
16 * @constructor
17 * @param {Element} statsElement The HTML div to which to update stats.
18 * @param {remoting.ClientPlugin} plugin
20 * @implements {remoting.WindowShape.ClientUI}
21 * @implements {base.Disposable}
23 remoting.ConnectionStats = function(statsElement, plugin) {
24 /** @private */
25 this.statsElement_ = statsElement;
27 /** @private {remoting.ClientSession.PerfStats} */
28 this.mostRecent_ = null
30 /** @private */
31 this.plugin_ = plugin;
33 var that = this;
35 /** @private */
36 this.timer_ = new base.RepeatingTimer(function(){
37 that.update(plugin.getPerfStats());
38 }, 1000, true);
40 remoting.windowShape.addCallback(this);
43 remoting.ConnectionStats.prototype.dispose = function() {
44 base.dispose(this.timer_);
45 this.timer_ = null;
46 this.plugin_ = null;
49 /**
50 * @return {remoting.ClientSession.PerfStats} The most recently-set PerfStats,
51 * or null if update() has not yet been called.
53 remoting.ConnectionStats.prototype.mostRecent = function() {
54 return this.mostRecent_;
57 /**
58 * Show or hide the connection stats div.
60 remoting.ConnectionStats.prototype.toggle = function() {
61 this.statsElement_.hidden = !this.statsElement_.hidden;
64 /**
65 * @return {boolean}
67 remoting.ConnectionStats.prototype.isVisible = function() {
68 return !this.statsElement_.hidden;
71 /**
72 * Show or hide the connection stats div.
73 * @param {boolean} show
75 remoting.ConnectionStats.prototype.show = function(show) {
76 this.statsElement_.hidden = !show;
79 /**
80 * If the stats panel is visible, add its bounding rectangle to the specified
81 * region.
82 * @param {Array<{left: number, top: number, width: number, height: number}>}
83 * rects List of rectangles.
86 remoting.ConnectionStats.prototype.addToRegion = function(rects) {
87 if (!this.statsElement_.hidden) {
88 rects.push(this.statsElement_.getBoundingClientRect());
92 /**
93 * Update the statistics panel.
94 * @param {remoting.ClientSession.PerfStats} stats The connection statistics.
96 remoting.ConnectionStats.prototype.update = function(stats) {
97 this.mostRecent_ = stats;
98 var units = '';
99 var videoBandwidth = stats.videoBandwidth;
100 if (videoBandwidth != undefined) {
101 if (videoBandwidth < 1024) {
102 units = 'Bps';
103 } else if (videoBandwidth < 1048576) {
104 units = 'KiBps';
105 videoBandwidth = videoBandwidth / 1024;
106 } else if (videoBandwidth < 1073741824) {
107 units = 'MiBps';
108 videoBandwidth = videoBandwidth / 1048576;
109 } else {
110 units = 'GiBps';
111 videoBandwidth = videoBandwidth / 1073741824;
116 * @param {number} value
117 * @param {string} units
118 * @return {string} Formatted number.
120 function formatStatNumber(value, units) {
121 if (value != undefined) {
122 return value.toFixed(2) + ' ' + units;
123 } else {
124 return "n/a";
128 var statistics = document.getElementById('statistics');
129 this.statsElement_.innerText = (
130 'Bandwidth: ' + formatStatNumber(videoBandwidth, units) +
131 ', Frame Rate: ' + formatStatNumber(stats.videoFrameRate, 'fps') +
132 ', Capture: ' + formatStatNumber(stats.captureLatency, 'ms') +
133 ', Encode: ' + formatStatNumber(stats.encodeLatency, 'ms') +
134 ', Decode: ' + formatStatNumber(stats.decodeLatency, 'ms') +
135 ', Render: ' + formatStatNumber(stats.renderLatency, 'ms') +
136 ', Latency: ' + formatStatNumber(stats.roundtripLatency, 'ms'));