Roll src/third_party/WebKit bf18a82:a9cee16 (svn 185297:185304)
[chromium-blink-merge.git] / chrome / browser / resources / net_internals / bandwidth_view.js
blob9f0e4e61cb95a1d8f67fee60365adb2352f5d267
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 /** This view displays summary statistics on bandwidth usage. */
6 var BandwidthView = (function() {
7 'use strict';
9 // We inherit from DivView.
10 var superClass = DivView;
12 /**
13 * @constructor
15 function BandwidthView() {
16 assertFirstConstructorCall(BandwidthView);
18 // Call superclass's constructor.
19 superClass.call(this, BandwidthView.MAIN_BOX_ID);
21 g_browser.addSessionNetworkStatsObserver(this, true);
22 g_browser.addHistoricNetworkStatsObserver(this, true);
24 this.sessionNetworkStats_ = null;
25 this.historicNetworkStats_ = null;
28 BandwidthView.TAB_ID = 'tab-handle-bandwidth';
29 BandwidthView.TAB_NAME = 'Bandwidth';
30 BandwidthView.TAB_HASH = '#bandwidth';
32 // IDs for special HTML elements in bandwidth_view.html
33 BandwidthView.MAIN_BOX_ID = 'bandwidth-view-tab-content';
35 cr.addSingletonGetter(BandwidthView);
37 BandwidthView.prototype = {
38 // Inherit the superclass's methods.
39 __proto__: superClass.prototype,
41 onLoadLogFinish: function(data) {
42 // Even though this information is included in log dumps, there's no real
43 // reason to display it when debugging a loaded log file.
44 return false;
47 /**
48 * Retains information on bandwidth usage this session.
50 onSessionNetworkStatsChanged: function(sessionNetworkStats) {
51 this.sessionNetworkStats_ = sessionNetworkStats;
52 return this.updateBandwidthUsageTable_();
55 /**
56 * Displays information on bandwidth usage this session and over the
57 * browser's lifetime.
59 onHistoricNetworkStatsChanged: function(historicNetworkStats) {
60 this.historicNetworkStats_ = historicNetworkStats;
61 return this.updateBandwidthUsageTable_();
64 /**
65 * Update the bandwidth usage table. Returns false on failure.
67 updateBandwidthUsageTable_: function() {
68 var sessionNetworkStats = this.sessionNetworkStats_;
69 var historicNetworkStats = this.historicNetworkStats_;
70 if (!sessionNetworkStats || !historicNetworkStats)
71 return false;
73 var sessionOriginal = sessionNetworkStats.session_original_content_length;
74 var sessionReceived = sessionNetworkStats.session_received_content_length;
75 var historicOriginal =
76 historicNetworkStats.historic_original_content_length;
77 var historicReceived =
78 historicNetworkStats.historic_received_content_length;
80 var rows = [];
81 rows.push({
82 title: 'Original (KB)',
83 sessionValue: bytesToRoundedKilobytes_(sessionOriginal),
84 historicValue: bytesToRoundedKilobytes_(historicOriginal)
85 });
86 rows.push({
87 title: 'Received (KB)',
88 sessionValue: bytesToRoundedKilobytes_(sessionReceived),
89 historicValue: bytesToRoundedKilobytes_(historicReceived)
90 });
91 rows.push({
92 title: 'Savings (KB)',
93 sessionValue:
94 bytesToRoundedKilobytes_(sessionOriginal - sessionReceived),
95 historicValue:
96 bytesToRoundedKilobytes_(historicOriginal - historicReceived)
97 });
98 rows.push({
99 title: 'Savings (%)',
100 sessionValue: getPercentSavings_(sessionOriginal, sessionReceived),
101 historicValue: getPercentSavings_(historicOriginal,
102 historicReceived)
105 var input = new JsEvalContext({rows: rows});
106 jstProcess(input, $(BandwidthView.MAIN_BOX_ID));
107 return true;
112 * Converts bytes to kilobytes rounded to one decimal place.
114 function bytesToRoundedKilobytes_(val) {
115 return (val / 1024).toFixed(1);
119 * Returns bandwidth savings as a percent rounded to one decimal place.
121 function getPercentSavings_(original, received) {
122 if (original > 0) {
123 return ((original - received) * 100 / original).toFixed(1);
125 return '0.0';
128 return BandwidthView;
129 })();