| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 network related log data and is specific fo ChromeOS. | |
| 7 * We get log data from chrome by filtering system logs for network related | |
| 8 * keywords. Logs are not fetched until we actually need them. | |
| 9 * | |
| 10 * @constructor | |
| 11 */ | |
| 12 function LogsView() { | |
| 13 const mainBoxId = 'logsTabContent'; | |
| 14 const tableId = 'logTable'; | |
| 15 const globalShowButtonId = 'logsGlobalShowBtn'; | |
| 16 const globalHideButtonId = 'logsGlobalHideBtn'; | |
| 17 const refreshLogsButtonId = 'logsRefreshBtn'; | |
| 18 | |
| 19 var tableDiv = $(tableId); | |
| 20 this.rows = []; | |
| 21 this.PopulateTable(tableDiv, this.logFilterList); | |
| 22 $(globalShowButtonId).addEventListener('click', | |
| 23 this.onGlobalChangeVisibleClick_.bind(this, true)); | |
| 24 $(globalHideButtonId).addEventListener('click', | |
| 25 this.onGlobalChangeVisibleClick_.bind(this, false)); | |
| 26 $(refreshLogsButtonId).addEventListener('click', | |
| 27 this.onLogsRefresh_.bind(this)); | |
| 28 DivView.call(this, mainBoxId); | |
| 29 }; | |
| 30 | |
| 31 inherits(LogsView, DivView); | |
| 32 | |
| 33 /** | |
| 34 * Contains log keys we are interested in. | |
| 35 */ | |
| 36 LogsView.prototype.logFilterList = [ | |
| 37 { | |
| 38 key:'syslog', | |
| 39 }, | |
| 40 { | |
| 41 key:'ui_log', | |
| 42 }, | |
| 43 { | |
| 44 key:'chrome_system_log', | |
| 45 }, | |
| 46 { | |
| 47 key:'chrome_log', | |
| 48 }, | |
| 49 ]; | |
| 50 | |
| 51 /** | |
| 52 * Called during View's initialization. Creates the row of a table logs will be | |
| 53 * shown in. Each row has 4 cells. | |
| 54 * | |
| 55 * First cell's content will be set to |logKey|, second will contain a button | |
| 56 * that will be used to show or hide third cell, which will contain the filtered | |
| 57 * log. | |
| 58 * |logKey| also tells us which log we are getting data from. | |
| 59 */ | |
| 60 LogsView.prototype.CreateTableRow = function(logKey) { | |
| 61 var row = document.createElement('tr'); | |
| 62 | |
| 63 var cells = []; | |
| 64 for (var i = 0; i < 3; i++) { | |
| 65 var rowCell = document.createElement('td'); | |
| 66 cells.push(rowCell); | |
| 67 row.appendChild(rowCell); | |
| 68 } | |
| 69 // Log key cell. | |
| 70 cells[0].className = 'logCellText'; | |
| 71 cells[0].textContent = logKey; | |
| 72 // Cell log is displayed in. Log content is in div element that is initially | |
| 73 // hidden and empty. | |
| 74 cells[2].className = 'logCellText'; | |
| 75 var logDiv = document.createElement('div'); | |
| 76 logDiv.textContent = ''; | |
| 77 logDiv.className = 'logCellLog'; | |
| 78 logDiv.id = 'logsView.logCell.' + this.rows.length; | |
| 79 cells[2].appendChild(logDiv); | |
| 80 | |
| 81 // Button that we use to show or hide div element with log content. Logs are | |
| 82 // not visible initially, so we initialize button accordingly. | |
| 83 var expandButton = document.createElement('button'); | |
| 84 expandButton.textContent = 'Show...'; | |
| 85 expandButton.className = 'logButton'; | |
| 86 expandButton.addEventListener('click', | |
| 87 this.onButtonClicked_.bind(this, row)); | |
| 88 | |
| 89 // Cell that contains show/hide button. | |
| 90 cells[1].appendChild(expandButton); | |
| 91 cells[1].className = 'logTableButtonColumn'; | |
| 92 | |
| 93 // Initially, log is not visible. | |
| 94 row.className = 'logRowCollapsed'; | |
| 95 | |
| 96 // We will need those to process row buttons' onclick events. | |
| 97 row.logKey = logKey; | |
| 98 row.expandButton = expandButton; | |
| 99 row.logDiv = logDiv; | |
| 100 row.logVisible = false; | |
| 101 this.rows.push(row); | |
| 102 | |
| 103 return row; | |
| 104 }; | |
| 105 | |
| 106 /** | |
| 107 * Initializes |tableDiv| to represent data from |logList| which should be of | |
| 108 * type LogsView.logFilterList. | |
| 109 */ | |
| 110 LogsView.prototype.PopulateTable = function(tableDiv, logList) { | |
| 111 for (var i = 0; i < logList.length; i++) { | |
| 112 var logSource = this.CreateTableRow(logList[i].key); | |
| 113 tableDiv.appendChild(logSource); | |
| 114 } | |
| 115 }; | |
| 116 | |
| 117 /** | |
| 118 * Processes clicks on buttons that show or hide log contents in log row. | |
| 119 * Row containing the clicked button is given to the method since it contains | |
| 120 * all data we need to process the click (unlike button object itself). | |
| 121 */ | |
| 122 LogsView.prototype.onButtonClicked_ = function(containingRow) { | |
| 123 if (!containingRow.logVisible) { | |
| 124 containingRow.className = 'logRowExpanded'; | |
| 125 containingRow.expandButton.textContent = 'Hide...'; | |
| 126 var logDiv = containingRow.logDiv; | |
| 127 if (logDiv.textContent == '') { | |
| 128 logDiv.textContent = 'Getting logs...'; | |
| 129 // Callback will be executed by g_browser. | |
| 130 g_browser.getSystemLog(containingRow.logKey, | |
| 131 containingRow.logDiv.id); | |
| 132 } | |
| 133 } else { | |
| 134 containingRow.className = 'logRowCollapsed'; | |
| 135 containingRow.expandButton.textContent = 'Show...'; | |
| 136 } | |
| 137 containingRow.logVisible = !containingRow.logVisible; | |
| 138 }; | |
| 139 | |
| 140 /** | |
| 141 * Processes click on one of the buttons that are used to show or hide all logs | |
| 142 * we care about. | |
| 143 */ | |
| 144 LogsView.prototype.onGlobalChangeVisibleClick_ = function(isShowAll) { | |
| 145 for (var row in this.rows) { | |
| 146 if (isShowAll != this.rows[row].logVisible) { | |
| 147 this.onButtonClicked_(this.rows[row]); | |
| 148 } | |
| 149 } | |
| 150 }; | |
| 151 | |
| 152 /** | |
| 153 * Processes click event on the button we use to refresh fetched logs. we get | |
| 154 * the newest logs from libcros, and refresh content of the visible log cells. | |
| 155 */ | |
| 156 LogsView.prototype.onLogsRefresh_ = function() { | |
| 157 g_browser.refreshSystemLogs(); | |
| 158 | |
| 159 var visibleLogRows = []; | |
| 160 var hiddenLogRows = []; | |
| 161 for (var row in this.rows) { | |
| 162 if (this.rows[row].logVisible) { | |
| 163 visibleLogRows.push(this.rows[row]); | |
| 164 } else { | |
| 165 hiddenLogRows.push(this.rows[row]); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 // We have to refresh text content in visible rows. | |
| 170 for (row in visibleLogRows) { | |
| 171 visibleLogRows[row].logDiv.textContent = 'Getting logs...'; | |
| 172 g_browser.getSystemLog(visibleLogRows[row].logKey, | |
| 173 visibleLogRows[row].logDiv.id); | |
| 174 } | |
| 175 | |
| 176 // In hidden rows we just clear potential log text, so we know we have to get | |
| 177 // new contents when we show the row next time. | |
| 178 for (row in hiddenLogRows) { | |
| 179 hiddenLogRows[row].logDiv.textContent = ''; | |
| 180 } | |
| 181 }; | |
| OLD | NEW |