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/**
6 * This view displays controls for capturing network events.
7 */
8var CaptureView = (function() {
9  'use strict';
10
11  // We inherit from DivView.
12  var superClass = DivView;
13
14  /**
15   * @constructor
16   */
17  function CaptureView() {
18    assertFirstConstructorCall(CaptureView);
19
20    // Call superclass's constructor.
21    superClass.call(this, CaptureView.MAIN_BOX_ID);
22
23    var byteLoggingCheckbox = $(CaptureView.BYTE_LOGGING_CHECKBOX_ID);
24    byteLoggingCheckbox.onclick = this.onSetByteLogging_.bind(this);
25
26    $(CaptureView.LIMIT_CHECKBOX_ID).onclick = this.onChangeLimit_.bind(this);
27
28    $(CaptureView.STOP_BUTTON_ID).onclick =
29        this.onStopButtonClicked_.bind(this);
30    $(CaptureView.RESET_BUTTON_ID).onclick =
31        this.onResetButtonClicked_.bind(this);
32
33    if (byteLoggingCheckbox.checked) {
34      // The code to display a warning on ExportView relies on bytelogging
35      // being off by default. If this ever changes, the code will need to
36      // be updated.
37      throw 'Not expecting byte logging to be enabled!';
38    }
39
40    new MouseOverHelp(CaptureView.LIMIT_HELP_ID,
41                      CaptureView.LIMIT_HELP_HOVER_ID);
42
43    new MouseOverHelp(CaptureView.BYTE_LOGGING_HELP_ID,
44                      CaptureView.BYTE_LOGGING_HELP_HOVER_ID);
45
46    this.onChangeLimit_();
47  }
48
49  CaptureView.TAB_ID = 'tab-handle-capture';
50  CaptureView.TAB_NAME = 'Capture';
51  CaptureView.TAB_HASH = '#capture';
52
53  // IDs for special HTML elements in capture_view.html
54  CaptureView.MAIN_BOX_ID = 'capture-view-tab-content';
55  CaptureView.BYTE_LOGGING_CHECKBOX_ID = 'capture-view-byte-logging-checkbox';
56  CaptureView.LIMIT_CHECKBOX_ID = 'capture-view-limit-checkbox';
57  CaptureView.LIMIT_HELP_ID = 'capture-view-limit-help';
58  CaptureView.LIMIT_HELP_HOVER_ID = 'capture-view-limit-help-hover';
59  CaptureView.BYTE_LOGGING_HELP_ID = 'capture-view-byte-logging-help';
60  CaptureView.BYTE_LOGGING_HELP_HOVER_ID =
61      'capture-view-byte-logging-help-hover';
62  CaptureView.STOP_BUTTON_ID = 'capture-view-stop-button';
63  CaptureView.RESET_BUTTON_ID = 'capture-view-reset-button';
64
65  cr.addSingletonGetter(CaptureView);
66
67  CaptureView.prototype = {
68    // Inherit the superclass's methods.
69    __proto__: superClass.prototype,
70
71    /**
72     * Called when a log file is loaded, after clearing the old log entries and
73     * loading the new ones.  Returns false to indicate the view should
74     * be hidden.
75     */
76    onLoadLogFinish: function(data) {
77      return false;
78    },
79
80    /**
81     * Depending on the value of the checkbox, enables or disables logging of
82     * actual bytes transferred.
83     */
84    onSetByteLogging_: function() {
85      var byteLoggingCheckbox = $(CaptureView.BYTE_LOGGING_CHECKBOX_ID);
86
87      if (byteLoggingCheckbox.checked) {
88        g_browser.setLogLevel(LogLevelType.LOG_ALL);
89
90        // Once we enable byte logging, all bets are off on what gets captured.
91        // Have the export view warn that the "strip cookies" option is
92        // ineffective from this point on.
93        //
94        // In theory we could clear this warning after unchecking the box and
95        // then deleting all the events which had been captured. We don't
96        // currently do that; if you want the warning to go away, will need to
97        // reload.
98        ExportView.getInstance().showPrivacyWarning();
99      } else {
100        g_browser.setLogLevel(LogLevelType.LOG_ALL_BUT_BYTES);
101      }
102    },
103
104    onChangeLimit_: function() {
105      var limitCheckbox = $(CaptureView.LIMIT_CHECKBOX_ID);
106
107      // Default to unlimited.
108      var softLimit = Infinity;
109      var hardLimit = Infinity;
110
111      if (limitCheckbox.checked) {
112        // The chosen limits are kind of arbitrary. I based it off the
113        // following observation:
114        //   A user-submitted log file which spanned a 7 hour time period
115        //   comprised 778,235 events and required 128MB of JSON.
116        //
117        // That feels too big. Assuming it was representative, then scaling
118        // by a factor of 4 should translate into a 32MB log file and cover
119        // close to 2 hours of events, which feels better.
120        //
121        // A large gap is left between the hardLimit and softLimit to avoid
122        // resetting the events often.
123        hardLimit = 300000;
124        softLimit = 150000;
125      }
126
127      EventsTracker.getInstance().setLimits(softLimit, hardLimit);
128    },
129
130    onStopButtonClicked_: function() {
131      MainView.getInstance().switchToViewOnlyMode();
132    },
133
134    onResetButtonClicked_: function() {
135      EventsTracker.getInstance().deleteAllLogEntries();
136    },
137  };
138
139  return CaptureView;
140})();
141