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() {
9 // We inherit from DivView.
10 var superClass
= DivView
;
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.
48 * Retains information on bandwidth usage this session.
50 onSessionNetworkStatsChanged: function(sessionNetworkStats
) {
51 this.sessionNetworkStats_
= sessionNetworkStats
;
52 return this.updateBandwidthUsageTable_();
56 * Displays information on bandwidth usage this session and over the
59 onHistoricNetworkStatsChanged: function(historicNetworkStats
) {
60 this.historicNetworkStats_
= historicNetworkStats
;
61 return this.updateBandwidthUsageTable_();
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
)
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
;
82 title
: 'Original (KB)',
83 sessionValue
: bytesToRoundedKilobytes_(sessionOriginal
),
84 historicValue
: bytesToRoundedKilobytes_(historicOriginal
)
87 title
: 'Received (KB)',
88 sessionValue
: bytesToRoundedKilobytes_(sessionReceived
),
89 historicValue
: bytesToRoundedKilobytes_(historicReceived
)
92 title
: 'Savings (KB)',
94 bytesToRoundedKilobytes_(sessionOriginal
- sessionReceived
),
96 bytesToRoundedKilobytes_(historicOriginal
- historicReceived
)
100 sessionValue
: getPercentSavings_(sessionOriginal
, sessionReceived
),
101 historicValue
: getPercentSavings_(historicOriginal
,
105 var input
= new JsEvalContext({rows
: rows
});
106 jstProcess(input
, $(BandwidthView
.MAIN_BOX_ID
));
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
) {
123 return ((original
- received
) * 100 / original
).toFixed(1);
128 return BandwidthView
;