summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/net_internals/proxyview.js
blob: 239229ac02e40793c8227f30c29fb72c5e966d42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * This view displays information on the proxy setup:
 *
 *   - Shows the current proxy settings.
 *   - Has a button to reload these settings.
 *   - Shows the log entries for the most recent INIT_PROXY_RESOLVER source
 *   - Shows the list of proxy hostnames that are cached as "bad".
 *   - Has a button to clear the cached bad proxies.
 *
 *  @constructor
 */
function ProxyView(mainBoxId,
                   originalSettingsDivId,
                   effectiveSettingsDivId,
                   reloadSettingsButtonId,
                   badProxiesTbodyId,
                   clearBadProxiesButtonId,
                   proxyResolverLogPreId) {
  DivView.call(this, mainBoxId);

  this.latestProxySourceEntries_ = null;
  this.latestProxySourceId_ = 0;

  // Hook up the UI components.
  this.originalSettingsDiv_ = document.getElementById(originalSettingsDivId);
  this.effectiveSettingsDiv_ =
      document.getElementById(effectiveSettingsDivId);
  this.proxyResolverLogPre_ = document.getElementById(proxyResolverLogPreId);
  this.badProxiesTbody_ = document.getElementById(badProxiesTbodyId);

  var reloadSettingsButton = document.getElementById(reloadSettingsButtonId);
  var clearBadProxiesButton = document.getElementById(clearBadProxiesButtonId);

  clearBadProxiesButton.onclick = g_browser.sendClearBadProxies.bind(g_browser);
  reloadSettingsButton.onclick =
      g_browser.sendReloadProxySettings.bind(g_browser);

  // Register to receive proxy information as it changes.
  g_browser.addProxySettingsObserver(this);
  g_browser.addBadProxiesObserver(this);
  g_browser.addLogObserver(this);
}

inherits(ProxyView, DivView);

ProxyView.prototype.onLoadLogStart = function(data) {
  // Need to reset this so the latest proxy source from the dump can be
  // identified when the log entries are loaded.
  this.latestProxySourceId_ = 0;
};

ProxyView.prototype.onLoadLogFinish = function(data, tabData) {
  // It's possible that the last INIT_PROXY_RESOLVER source was deleted from the
  // log, but earlier sources remain.  When that happens, clear the list of
  // entries here, to avoid displaying misleading information.
  if (tabData != this.latestProxySourceId_)
    this.clearLog_();
  return this.onProxySettingsChanged(data.proxySettings) &&
         this.onBadProxiesChanged(data.badProxies);
};

/**
 * Save view-specific state.
 *
 * Save the greatest seen proxy source id, so we will not incorrectly identify
 * the log source associated with the current proxy configuration.
 */
ProxyView.prototype.saveState = function() {
  return this.latestProxySourceId_;
};

ProxyView.prototype.onProxySettingsChanged = function(proxySettings) {
  // Both |original| and |effective| are dictionaries describing the settings.
  this.originalSettingsDiv_.innerHTML = '';
  this.effectiveSettingsDiv_.innerHTML = '';

  if (!proxySettings)
    return false;

  var original = proxySettings.original;
  var effective = proxySettings.effective;

  if (!original || !effective)
    return false;

  addTextNode(this.originalSettingsDiv_, proxySettingsToString(original));
  addTextNode(this.effectiveSettingsDiv_, proxySettingsToString(effective));
  return true;
};

ProxyView.prototype.onBadProxiesChanged = function(badProxies) {
  this.badProxiesTbody_.innerHTML = '';

  if (!badProxies)
    return false;

  // Add a table row for each bad proxy entry.
  for (var i = 0; i < badProxies.length; ++i) {
    var entry = badProxies[i];
    var badUntilDate = g_browser.convertTimeTicksToDate(entry.bad_until);

    var tr = addNode(this.badProxiesTbody_, 'tr');

    var nameCell = addNode(tr, 'td');
    var badUntilCell = addNode(tr, 'td');

    addTextNode(nameCell, entry.proxy_uri);
    addTextNode(badUntilCell, badUntilDate.toLocaleString());
  }
  return true;
};

ProxyView.prototype.onLogEntryAdded = function(logEntry) {
  if (logEntry.source.type != LogSourceType.INIT_PROXY_RESOLVER ||
      this.latestProxySourceId_ > logEntry.source.id) {
    return;
  }

  if (logEntry.source.id > this.latestProxySourceId_) {
    this.latestProxySourceId_ = logEntry.source.id;
    this.latestProxySourceEntries_ = [];
  }

  this.latestProxySourceEntries_.push(logEntry);
  this.proxyResolverLogPre_.innerHTML = '';
  addTextNode(this.proxyResolverLogPre_,
              PrintSourceEntriesAsText(this.latestProxySourceEntries_));
};

/**
 * Clears the display of and log entries for the last proxy lookup.
 */
ProxyView.prototype.clearLog_ = function() {
  this.latestProxySourceEntries_ = [];
  // Prevents display of partial logs.
  ++this.latestProxySourceId_;

  this.proxyResolverLogPre_.innerHTML = '';
  addTextNode(this.proxyResolverLogPre_, 'Deleted.');
};

ProxyView.prototype.onLogEntriesDeleted = function(sourceIds) {
  if (sourceIds.indexOf(this.latestProxySourceId_) != -1)
    this.clearLog_();
};

ProxyView.prototype.onAllLogEntriesDeleted = function() {
  this.clearLog_();
};