OLD | NEW |
(Empty) | |
| 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 information on the host resolver: |
| 7 * |
| 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). |
| 12 */ |
| 13 |
| 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 |
| 18 // elsewhere as well. |
| 19 var DnsView = (function() { |
| 20 'use strict'; |
| 21 |
| 22 // We inherit from DivView. |
| 23 var superClass = DivView; |
| 24 |
| 25 /** |
| 26 * @constructor |
| 27 */ |
| 28 function DnsView() { |
| 29 assertFirstConstructorCall(DnsView); |
| 30 |
| 31 // Call superclass's constructor. |
| 32 superClass.call(this, DnsView.MAIN_BOX_ID); |
| 33 |
| 34 $(DnsView.CLEAR_CACHE_BUTTON_ID).onclick = |
| 35 g_browser.sendClearHostResolverCache.bind(g_browser); |
| 36 |
| 37 // Register to receive changes to the host resolver info. |
| 38 g_browser.addHostResolverInfoObserver(this, false); |
| 39 } |
| 40 |
| 41 DnsView.TAB_ID = 'tab-handle-dns'; |
| 42 DnsView.TAB_NAME = 'DNS'; |
| 43 DnsView.TAB_HASH = '#dns'; |
| 44 |
| 45 // IDs for special HTML elements in dns_view.html |
| 46 DnsView.MAIN_BOX_ID = 'dns-view-tab-content'; |
| 47 |
| 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'; |
| 52 |
| 53 DnsView.CLEAR_CACHE_BUTTON_ID = 'dns-view-clear-cache'; |
| 54 DnsView.CAPACITY_SPAN_ID = 'dns-view-cache-capacity'; |
| 55 |
| 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'; |
| 59 |
| 60 cr.addSingletonGetter(DnsView); |
| 61 |
| 62 DnsView.prototype = { |
| 63 // Inherit the superclass's methods. |
| 64 __proto__: superClass.prototype, |
| 65 |
| 66 onLoadLogFinish: function(data) { |
| 67 return this.onHostResolverInfoChanged(data.hostResolverInfo); |
| 68 }, |
| 69 |
| 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'; |
| 76 |
| 77 // Update fields containing async DNS configuration information. |
| 78 displayAsyncDnsConfig_(hostResolverInfo); |
| 79 |
| 80 // No info. |
| 81 if (!hostResolverInfo || !hostResolverInfo.cache) |
| 82 return false; |
| 83 |
| 84 // Fill in the basic cache information. |
| 85 var hostResolverCache = hostResolverInfo.cache; |
| 86 $(DnsView.CAPACITY_SPAN_ID).innerText = hostResolverCache.capacity; |
| 87 |
| 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. |
| 91 var logDate; |
| 92 if (MainView.isViewingLoadedLog()) { |
| 93 logDate = new Date(ClientInfo.numericDate); |
| 94 } else { |
| 95 logDate = new Date(); |
| 96 } |
| 97 |
| 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'); |
| 102 |
| 103 var hostnameCell = addNode(tr, 'td'); |
| 104 addTextNode(hostnameCell, e.hostname); |
| 105 |
| 106 var familyCell = addNode(tr, 'td'); |
| 107 addTextNode(familyCell, |
| 108 addressFamilyToString(e.address_family)); |
| 109 |
| 110 var addressesCell = addNode(tr, 'td'); |
| 111 |
| 112 if (e.error != undefined) { |
| 113 var errorText = |
| 114 e.error + ' (' + netErrorToString(e.error) + ')'; |
| 115 var errorNode = addTextNode(addressesCell, 'error: ' + errorText); |
| 116 addressesCell.classList.add('warning-text'); |
| 117 } else { |
| 118 addListToNode_(addNode(addressesCell, 'div'), e.addresses); |
| 119 } |
| 120 |
| 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)) { |
| 125 ++expiredEntries; |
| 126 var expiredSpan = addNode(expiresCell, 'span'); |
| 127 expiredSpan.classList.add('warning-text'); |
| 128 addTextNode(expiredSpan, ' [Expired]'); |
| 129 } |
| 130 } |
| 131 |
| 132 $(DnsView.ACTIVE_SPAN_ID).innerText = |
| 133 hostResolverCache.entries.length - expiredEntries; |
| 134 $(DnsView.EXPIRED_SPAN_ID).innerText = expiredEntries; |
| 135 return true; |
| 136 }, |
| 137 }; |
| 138 |
| 139 /** |
| 140 * Displays information corresponding to the current async DNS configuration. |
| 141 * @param {Object} hostResolverInfo The host resolver information. |
| 142 */ |
| 143 function displayAsyncDnsConfig_(hostResolverInfo) { |
| 144 // Clear the table. |
| 145 $(DnsView.INTERNAL_DNS_CONFIG_TBODY_ID).innerHTML = ''; |
| 146 |
| 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); |
| 155 |
| 156 // If the internal DNS resolver is disabled or has no valid configuration, |
| 157 // we're done. |
| 158 if (!enabled || noConfig) |
| 159 return; |
| 160 |
| 161 var dnsConfig = hostResolverInfo.dns_config; |
| 162 |
| 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); |
| 167 |
| 168 // Add everything else in |dnsConfig| to the table. |
| 169 for (var key in dnsConfig) { |
| 170 if (key == 'nameservers') |
| 171 continue; |
| 172 var tr = addNode($(DnsView.INTERNAL_DNS_CONFIG_TBODY_ID), 'tr'); |
| 173 addNodeWithText(tr, 'th', key); |
| 174 var td = addNode(tr, 'td'); |
| 175 |
| 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]); |
| 180 continue; |
| 181 } |
| 182 |
| 183 addTextNode(td, dnsConfig[key]); |
| 184 } |
| 185 } |
| 186 |
| 187 /** |
| 188 * Takes a last of strings and adds them all to a DOM node, displaying them |
| 189 * on separate lines. |
| 190 * @param {DomNode} node The parent node. |
| 191 * @param {Array<string>} list List of strings to add to the node. |
| 192 */ |
| 193 function addListToNode_(node, list) { |
| 194 for (var i = 0; i < list.length; ++i) |
| 195 addNodeWithText(node, 'div', list[i]); |
| 196 } |
| 197 |
| 198 return DnsView; |
| 199 })(); |
| 200 |
OLD | NEW |