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.
4
5/** This view displays summary statistics on bandwidth usage. */
6var BandwidthView = (function() {
7  'use strict';
8
9  // We inherit from DivView.
10  var superClass = DivView;
11
12  /**
13   * @constructor
14   */
15  function BandwidthView() {
16    assertFirstConstructorCall(BandwidthView);
17
18    // Call superclass's constructor.
19    superClass.call(this, BandwidthView.MAIN_BOX_ID);
20
21    g_browser.addSessionNetworkStatsObserver(this, true);
22    g_browser.addHistoricNetworkStatsObserver(this, true);
23
24    this.sessionNetworkStats_ = null;
25    this.historicNetworkStats_ = null;
26  }
27
28  BandwidthView.TAB_ID = 'tab-handle-bandwidth';
29  BandwidthView.TAB_NAME = 'Bandwidth';
30  BandwidthView.TAB_HASH = '#bandwidth';
31
32  // IDs for special HTML elements in bandwidth_view.html
33  BandwidthView.MAIN_BOX_ID = 'bandwidth-view-tab-content';
34
35  cr.addSingletonGetter(BandwidthView);
36
37  BandwidthView.prototype = {
38    // Inherit the superclass's methods.
39    __proto__: superClass.prototype,
40
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;
45    },
46
47    /**
48     * Retains information on bandwidth usage this session.
49     */
50    onSessionNetworkStatsChanged: function(sessionNetworkStats) {
51      this.sessionNetworkStats_ = sessionNetworkStats;
52      return this.updateBandwidthUsageTable_();
53    },
54
55    /**
56     * Displays information on bandwidth usage this session and over the
57     * browser's lifetime.
58     */
59    onHistoricNetworkStatsChanged: function(historicNetworkStats) {
60      this.historicNetworkStats_ = historicNetworkStats;
61      return this.updateBandwidthUsageTable_();
62    },
63
64    /**
65     * Update the bandwidth usage table.  Returns false on failure.
66     */
67    updateBandwidthUsageTable_: function() {
68      var sessionNetworkStats = this.sessionNetworkStats_;
69      var historicNetworkStats = this.historicNetworkStats_;
70      if (!sessionNetworkStats || !historicNetworkStats)
71        return false;
72
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;
79
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)
103      });
104
105      var input = new JsEvalContext({rows: rows});
106      jstProcess(input, $(BandwidthView.MAIN_BOX_ID));
107      return true;
108    }
109  };
110
111  /**
112   * Converts bytes to kilobytes rounded to one decimal place.
113   */
114  function bytesToRoundedKilobytes_(val) {
115    return (val / 1024).toFixed(1);
116  }
117
118  /**
119   * Returns bandwidth savings as a percent rounded to one decimal place.
120   */
121  function getPercentSavings_(original, received) {
122    if (original > 0) {
123      return ((original - received) * 100 / original).toFixed(1);
124    }
125    return '0.0';
126  }
127
128  return BandwidthView;
129})();
130