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.
6 * This view displays information on the host resolver:
8 * - Shows the default address family.
9 * - Shows the current host cache contents.
10 * - Has a button to clear the host cache.
11 * - Shows the parameters used to construct the host cache (capacity, ttl).
14 // TODO(mmenke): Add links for each address entry to the corresponding NetLog
15 // source. This could either be done by adding NetLog source ids
16 // to cache entries, or tracking sources based on their type and
17 // description. Former is simpler, latter may be useful
19 var DnsView = (function() {
22 // We inherit from DivView.
23 var superClass = DivView;
29 assertFirstConstructorCall(DnsView);
31 // Call superclass's constructor.
32 superClass.call(this, DnsView.MAIN_BOX_ID);
34 $(DnsView.CLEAR_CACHE_BUTTON_ID).onclick =
35 g_browser.sendClearHostResolverCache.bind(g_browser);
37 // Register to receive changes to the host resolver info.
38 g_browser.addHostResolverInfoObserver(this, false);
41 DnsView.TAB_ID = 'tab-handle-dns';
42 DnsView.TAB_NAME = 'DNS';
43 DnsView.TAB_HASH = '#dns';
45 // IDs for special HTML elements in dns_view.html
46 DnsView.MAIN_BOX_ID = 'dns-view-tab-content';
48 DnsView.INTERNAL_DNS_ENABLED_SPAN_ID = 'dns-view-internal-dns-enabled';
49 DnsView.INTERNAL_DNS_INVALID_CONFIG_SPAN_ID =
50 'dns-view-internal-dns-invalid-config';
51 DnsView.INTERNAL_DNS_CONFIG_TBODY_ID = 'dns-view-internal-dns-config-tbody';
53 DnsView.CLEAR_CACHE_BUTTON_ID = 'dns-view-clear-cache';
54 DnsView.CAPACITY_SPAN_ID = 'dns-view-cache-capacity';
56 DnsView.ACTIVE_SPAN_ID = 'dns-view-cache-active';
57 DnsView.EXPIRED_SPAN_ID = 'dns-view-cache-expired';
58 DnsView.CACHE_TBODY_ID = 'dns-view-cache-tbody';
60 cr.addSingletonGetter(DnsView);
63 // Inherit the superclass's methods.
64 __proto__: superClass.prototype,
66 onLoadLogFinish: function(data) {
67 return this.onHostResolverInfoChanged(data.hostResolverInfo);
70 onHostResolverInfoChanged: function(hostResolverInfo) {
71 // Clear the existing values.
72 $(DnsView.CAPACITY_SPAN_ID).innerHTML = '';
73 $(DnsView.CACHE_TBODY_ID).innerHTML = '';
74 $(DnsView.ACTIVE_SPAN_ID).innerHTML = '0';
75 $(DnsView.EXPIRED_SPAN_ID).innerHTML = '0';
77 // Update fields containing async DNS configuration information.
78 displayAsyncDnsConfig_(hostResolverInfo);
81 if (!hostResolverInfo || !hostResolverInfo.cache)
84 // Fill in the basic cache information.
85 var hostResolverCache = hostResolverInfo.cache;
86 $(DnsView.CAPACITY_SPAN_ID).innerText = hostResolverCache.capacity;
88 var expiredEntries = 0;
89 // Date the cache was logged. This will be either now, when actively
90 // logging data, or the date the log dump was created.
92 if (MainView.isViewingLoadedLog()) {
93 logDate = new Date(ClientInfo.numericDate);
98 // Fill in the cache contents table.
99 for (var i = 0; i < hostResolverCache.entries.length; ++i) {
100 var e = hostResolverCache.entries[i];
101 var tr = addNode($(DnsView.CACHE_TBODY_ID), 'tr');
103 var hostnameCell = addNode(tr, 'td');
104 addTextNode(hostnameCell, e.hostname);
106 var familyCell = addNode(tr, 'td');
107 addTextNode(familyCell,
108 addressFamilyToString(e.address_family));
110 var addressesCell = addNode(tr, 'td');
112 if (e.error != undefined) {
114 e.error + ' (' + netErrorToString(e.error) + ')';
115 var errorNode = addTextNode(addressesCell, 'error: ' + errorText);
116 addressesCell.classList.add('warning-text');
118 addListToNode_(addNode(addressesCell, 'div'), e.addresses);
121 var expiresDate = timeutil.convertTimeTicksToDate(e.expiration);
122 var expiresCell = addNode(tr, 'td');
123 timeutil.addNodeWithDate(expiresCell, expiresDate);
124 if (logDate > timeutil.convertTimeTicksToDate(e.expiration)) {
126 var expiredSpan = addNode(expiresCell, 'span');
127 expiredSpan.classList.add('warning-text');
128 addTextNode(expiredSpan, ' [Expired]');
132 $(DnsView.ACTIVE_SPAN_ID).innerText =
133 hostResolverCache.entries.length - expiredEntries;
134 $(DnsView.EXPIRED_SPAN_ID).innerText = expiredEntries;
140 * Displays information corresponding to the current async DNS configuration.
141 * @param {Object} hostResolverInfo The host resolver information.
143 function displayAsyncDnsConfig_(hostResolverInfo) {
145 $(DnsView.INTERNAL_DNS_CONFIG_TBODY_ID).innerHTML = '';
147 // Figure out if the internal DNS resolver is disabled or has no valid
148 // configuration information, and update display accordingly.
149 var enabled = hostResolverInfo &&
150 hostResolverInfo.dns_config !== undefined;
151 var noConfig = enabled &&
152 hostResolverInfo.dns_config.nameservers === undefined;
153 $(DnsView.INTERNAL_DNS_ENABLED_SPAN_ID).innerText = enabled;
154 setNodeDisplay($(DnsView.INTERNAL_DNS_INVALID_CONFIG_SPAN_ID), noConfig);
156 // If the internal DNS resolver is disabled or has no valid configuration,
158 if (!enabled || noConfig)
161 var dnsConfig = hostResolverInfo.dns_config;
163 // Display nameservers first.
164 var nameserverRow = addNode($(DnsView.INTERNAL_DNS_CONFIG_TBODY_ID), 'tr');
165 addNodeWithText(nameserverRow, 'th', 'nameservers');
166 addListToNode_(addNode(nameserverRow, 'td'), dnsConfig.nameservers);
168 // Add everything else in |dnsConfig| to the table.
169 for (var key in dnsConfig) {
170 if (key == 'nameservers')
172 var tr = addNode($(DnsView.INTERNAL_DNS_CONFIG_TBODY_ID), 'tr');
173 addNodeWithText(tr, 'th', key);
174 var td = addNode(tr, 'td');
176 // For lists, display each list entry on a separate line.
177 if (typeof dnsConfig[key] == 'object' &&
178 dnsConfig[key].constructor == Array) {
179 addListToNode_(td, dnsConfig[key]);
183 addTextNode(td, dnsConfig[key]);
188 * Takes a last of strings and adds them all to a DOM node, displaying them
190 * @param {DomNode} node The parent node.
191 * @param {Array<string>} list List of strings to add to the node.
193 function addListToNode_(node, list) {
194 for (var i = 0; i < list.length; ++i)
195 addNodeWithText(node, 'div', list[i]);