1// Copyright (c) 2010 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 information on the host resolver:
7 *
8 *   - Shows the default address family.
9 *   - Has a button to enable IPv6, if it is disabled.
10 *   - Shows the current host cache contents.
11 *   - Has a button to clear the host cache.
12 *   - Shows the parameters used to construct the host cache (capacity, ttl).
13 *
14 *  @constructor
15 */
16function DnsView(mainBoxId,
17                 cacheTbodyId,
18                 clearCacheButtonId,
19                 defaultFamilySpanId,
20                 ipv6DisabledSpanId,
21                 enableIPv6ButtonId,
22                 capacitySpanId,
23                 ttlSuccessSpanId,
24                 ttlFailureSpanId) {
25  DivView.call(this, mainBoxId);
26
27  // Hook up the UI components.
28  this.cacheTbody_ = document.getElementById(cacheTbodyId);
29  this.defaultFamilySpan_ = document.getElementById(defaultFamilySpanId);
30  this.ipv6DisabledSpan_ = document.getElementById(ipv6DisabledSpanId);
31
32  document.getElementById(enableIPv6ButtonId).onclick =
33      g_browser.enableIPv6.bind(g_browser);
34
35  this.capacitySpan_ = document.getElementById(capacitySpanId);
36  this.ttlSuccessSpan_ = document.getElementById(ttlSuccessSpanId);
37  this.ttlFailureSpan_ = document.getElementById(ttlFailureSpanId);
38
39  var clearCacheButton = document.getElementById(clearCacheButtonId);
40  clearCacheButton.onclick =
41      g_browser.sendClearHostResolverCache.bind(g_browser);
42
43  // Register to receive changes to the host resolver info.
44  g_browser.addHostResolverInfoObserver(this);
45}
46
47inherits(DnsView, DivView);
48
49DnsView.prototype.onHostResolverInfoChanged = function(hostResolverInfo) {
50  // Clear the existing values.
51  this.defaultFamilySpan_.innerHTML = '';
52  this.capacitySpan_.innerHTML = '';
53  this.ttlSuccessSpan_.innerHTML = '';
54  this.ttlFailureSpan_.innerHTML = '';
55  this.cacheTbody_.innerHTML = '';
56
57  // No info.
58  if (!hostResolverInfo)
59    return;
60
61  var family = hostResolverInfo.default_address_family;
62  addTextNode(this.defaultFamilySpan_, getKeyWithValue(AddressFamily, family));
63
64  var ipv6Disabled = (family == AddressFamily.ADDRESS_FAMILY_IPV4);
65  setNodeDisplay(this.ipv6DisabledSpan_, ipv6Disabled);
66
67  // Fill in the basic cache information.
68  var hostResolverCache = hostResolverInfo.cache;
69  addTextNode(this.capacitySpan_, hostResolverCache.capacity);
70  addTextNode(this.ttlSuccessSpan_, hostResolverCache.ttl_success_ms);
71  addTextNode(this.ttlFailureSpan_, hostResolverCache.ttl_failure_ms);
72
73  // Fill in the cache contents table.
74  for (var i = 0; i < hostResolverCache.entries.length; ++i) {
75    var e = hostResolverCache.entries[i];
76    var tr = addNode(this.cacheTbody_, 'tr');
77
78    var hostnameCell = addNode(tr, 'td');
79    addTextNode(hostnameCell, e.hostname);
80
81    var familyCell = addNode(tr, 'td');
82    addTextNode(familyCell, getKeyWithValue(AddressFamily, e.address_family));
83
84    var addressesCell = addNode(tr, 'td');
85
86    if (e.error != undefined) {
87      addTextNode(addressesCell, 'error: ' + e.error);
88    } else {
89      for (var j = 0; j < e.addresses.length; ++j) {
90        var address = e.addresses[j];
91        if (j != 0)
92          addNode(addressesCell, 'br');
93        addTextNode(addressesCell, address);
94      }
95    }
96
97    var expiresDate = g_browser.convertTimeTicksToDate(e.expiration);
98    var expiresCell = addNode(tr, 'td');
99    addTextNode(expiresCell, expiresDate.toLocaleString());
100  }
101};
102