Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('offlineInternals', function() { | 5 cr.define('offlineInternals', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * @typedef {{ | 9 * @typedef {{ |
| 10 * onlineUrl: string, | 10 * onlineUrl: string, |
| 11 * creationTime: number, | 11 * creationTime: number, |
| 12 * status: number, | |
| 13 * id: string, | 12 * id: string, |
| 14 * namespace: string, | 13 * namespace: string, |
| 15 * size: string, | 14 * size: string, |
| 16 * filePath: string, | 15 * filePath: string, |
| 17 * lastAccessTime: number, | 16 * lastAccessTime: number, |
| 18 * accessCount: number | 17 * accessCount: number |
| 19 * }} | 18 * }} |
| 20 */ | 19 */ |
| 21 var OfflinePageItem; | 20 var OfflinePageItem; |
| 22 | 21 |
| 23 /** | 22 /** |
| 24 * @typedef {{ | 23 * @typedef {{ |
| 25 * status: string, | 24 * status: string, |
| 26 * onlineUrl: string, | 25 * onlineUrl: string, |
| 27 * creationTime: number, | 26 * creationTime: number, |
| 28 * id: string, | 27 * id: string, |
| 29 * namespace: string, | 28 * namespace: string, |
| 30 * attemptCount: number | 29 * attemptCount: number |
| 31 * }} | 30 * }} |
| 32 */ | 31 */ |
| 33 var SavePageRequest; | 32 var SavePageRequest; |
| 34 | 33 |
| 34 var currentStoredPagesData = null; | |
|
dewittj
2016/06/03 23:12:05
needs /** @type {...} */
chili
2016/06/09 22:29:16
Done.
| |
| 35 var currentRequestQueueData = null; | |
| 36 | |
| 35 /** | 37 /** |
| 36 * Clear the specified table. | 38 * Clear the specified table. |
| 37 * @param {string} tableId id of the table to clear. | 39 * @param {string} tableId ID of the table to clear. |
| 38 */ | 40 */ |
| 39 function clearTable(tableId) { | 41 function clearTable(tableId) { |
|
dewittj
2016/06/03 23:12:05
nit: make this take an element, then pass it in;
chili
2016/06/09 22:29:16
Done.
| |
| 40 $(tableId).textContent = ''; | 42 $(tableId).textContent = ''; |
| 41 } | 43 } |
| 42 | 44 |
| 43 /** | 45 /** |
| 44 * Fill stored pages table. | 46 * Fill stored pages table. |
| 45 * @param {HTMLElement} element A HTML element. | 47 * @param {!Array<OfflinePageItem} data An array object representing |
|
dewittj
2016/06/03 23:12:06
Missing ">"
chili
2016/06/09 22:29:16
Done.
| |
| 46 * @param {!Array<OfflinePageItem>} data An array object representing | |
| 47 * stored offline pages. | 48 * stored offline pages. |
| 48 */ | 49 */ |
| 49 function fillStoredPages(element, data) { | 50 function fillStoredPages(data) { |
| 51 clearTable('stored-pages'); | |
| 52 var element = $('stored-pages'); | |
| 53 | |
| 50 for (var i = 0; i < data.length; i++) { | 54 for (var i = 0; i < data.length; i++) { |
| 51 var row = document.createElement('tr'); | 55 var row = document.createElement('tr'); |
| 52 | 56 |
| 53 var checkboxCell = document.createElement('td'); | 57 var checkboxCell = document.createElement('td'); |
| 54 var checkbox = document.createElement('input'); | 58 var checkbox = document.createElement('input'); |
| 55 checkbox.setAttribute('type', 'checkbox'); | 59 checkbox.setAttribute('type', 'checkbox'); |
| 56 checkbox.setAttribute('name', 'stored'); | 60 checkbox.setAttribute('name', 'stored'); |
| 57 checkbox.setAttribute('value', data[i].id); | 61 checkbox.setAttribute('value', data[i].id); |
| 58 | 62 |
| 59 checkboxCell.appendChild(checkbox); | 63 checkboxCell.appendChild(checkbox); |
| 60 row.appendChild(checkboxCell); | 64 row.appendChild(checkboxCell); |
| 61 | 65 |
| 62 var cell = document.createElement('td'); | 66 var cell = document.createElement('td'); |
| 63 cell.textContent = data[i].onlineUrl; | 67 cell.textContent = data[i].onlineUrl; |
| 64 row.appendChild(cell); | 68 row.appendChild(cell); |
| 65 | 69 |
| 66 cell = document.createElement('td'); | 70 cell = document.createElement('td'); |
| 67 cell.textContent = data[i].namespace; | 71 cell.textContent = data[i].namespace; |
| 68 row.appendChild(cell); | 72 row.appendChild(cell); |
| 69 | 73 |
| 70 cell = document.createElement('td'); | 74 cell = document.createElement('td'); |
| 71 cell.textContent = data[i].size; | 75 cell.textContent = Math.round(data[i].size / 1024); |
| 72 row.appendChild(cell); | 76 row.appendChild(cell); |
| 73 | 77 |
| 74 element.appendChild(row); | 78 element.appendChild(row); |
| 75 } | 79 } |
| 80 currentStoredPagesData = data; | |
| 76 } | 81 } |
| 77 | 82 |
| 78 /** | 83 /** |
| 79 * Fill requests table. | 84 * Fill requests table. |
| 80 * @param {HTMLElement} element A HTML element. | |
| 81 * @param {!Array<SavePageRequest>} data An array object representing | 85 * @param {!Array<SavePageRequest>} data An array object representing |
| 82 * the request queue. | 86 * the request queue. |
| 83 */ | 87 */ |
| 84 function fillRequestQueue(element, data) { | 88 function fillRequestQueue(data) { |
| 89 clearTable('request-queue'); | |
| 90 var element = $('request-queue'); | |
| 91 | |
| 85 for (var i = 0; i < data.length; i++) { | 92 for (var i = 0; i < data.length; i++) { |
| 86 var row = document.createElement('tr'); | 93 var row = document.createElement('tr'); |
| 87 | 94 |
| 88 var cell = document.createElement('td'); | 95 var cell = document.createElement('td'); |
| 89 cell.textContent = data[i].onlineUrl; | 96 cell.textContent = data[i].onlineUrl; |
| 90 row.appendChild(cell); | 97 row.appendChild(cell); |
| 91 | 98 |
| 92 cell = document.createElement('td'); | 99 cell = document.createElement('td'); |
| 93 cell.textContent = new Date(data[i].creationTime); | 100 cell.textContent = new Date(data[i].creationTime); |
| 94 row.appendChild(cell); | 101 row.appendChild(cell); |
| 95 | 102 |
| 96 cell = document.createElement('td'); | 103 cell = document.createElement('td'); |
| 97 cell.textContent = data[i].status; | 104 cell.textContent = data[i].status; |
| 98 row.appendChild(cell); | 105 row.appendChild(cell); |
| 99 | 106 |
| 100 element.appendChild(row); | 107 element.appendChild(row); |
| 101 } | 108 } |
| 109 currentRequestQueueData = data; | |
| 102 } | 110 } |
| 103 | 111 |
| 104 /** | 112 /** |
| 105 * Refresh all displayed information. | 113 * Refresh all displayed information. |
| 106 */ | 114 */ |
| 107 function refreshAll() { | 115 function refreshAll() { |
| 108 cr.sendWithPromise('getOfflineInternalsInfo').then(setOfflineInternalsInfo); | 116 cr.sendWithPromise('getStoredPagesInfo').then(fillStoredPages); |
| 117 cr.sendWithPromise('getRequestQueueInfo').then(fillRequestQueue); | |
| 109 } | 118 } |
| 110 | 119 |
| 111 /** | 120 /** |
| 112 * Delete all pages in the offline store. | 121 * Delete all pages in the offline store. |
| 113 */ | 122 */ |
| 114 function deleteAllPages() { | 123 function deleteAllPages() { |
| 115 cr.sendWithPromise('deleteAllPages').then(pagesDeleted); | 124 cr.sendWithPromise('deleteAllPages').then(pagesDeleted); |
| 116 } | 125 } |
| 117 | 126 |
| 118 /** | 127 /** |
| 119 * Callback when pages are deleted. | 128 * Callback when pages are deleted. |
| 120 * @param {string} deletePageStatus The status of delete page call. | 129 * @param {string} status The status of the request. |
| 121 */ | 130 */ |
| 122 function pagesDeleted(deletePageStatus) { | 131 function pagesDeleted(status) { |
| 123 // TODO(chili): decide what to do here. Perhaps a refresh of just | 132 $('random-info').textContent = status; |
| 124 // the stored pages table? | 133 cr.sendWithPromise('getStoredPagesInfo').then(fillStoredPages); |
| 125 } | 134 } |
| 126 | 135 |
| 127 /** | 136 /** |
| 128 * Callback when information is loaded. | 137 * Helper function to add quotes around a string. |
| 129 * @param {{AllPages: !Array<OfflinePageItem>, | 138 * @param {string} strObj The obj to add quotes around. |
|
dewittj
2016/06/03 23:12:05
@return {string} the escaped string
chili
2016/06/09 22:29:16
Done.
| |
| 130 * Queue: !Array<SavePageRequest>}} info An object containing both | |
| 131 * stored pages and request queue status. | |
| 132 */ | 139 */ |
| 133 function setOfflineInternalsInfo(info) { | 140 function addQuotes(strObj) { |
|
dewittj
2016/06/03 23:12:06
need to escape backslashes and quotes in the incom
chili
2016/06/09 22:29:16
backslashes don't need to be escaped. Added escap
| |
| 134 clearTable('stored-pages'); | 141 return '"' + strObj + '"'; |
| 135 clearTable('request-queue'); | |
| 136 | |
| 137 fillStoredPages($('stored-pages'), info.AllPages); | |
| 138 fillRequestQueue($('request-queue'), info.Queue); | |
| 139 } | 142 } |
| 140 | 143 |
| 141 /** | 144 /** |
| 145 * Downloads all the stored page and request queue information into a file. | |
| 146 */ | |
| 147 function download() { | |
| 148 var csv = ''; | |
| 149 // Create header & csv for stored pages. | |
| 150 if (currentStoredPagesData && currentStoredPagesData.length > 0) { | |
| 151 csv += 'Online URL,Namespace,Size,ID,File Path,Creation Time,' + | |
| 152 'Last Accessed Time,Access Count\n'; | |
| 153 for (let obj of currentStoredPagesData) { | |
| 154 var objFieldArr = [ | |
| 155 obj.onlineUrl, | |
| 156 obj.namespace, | |
| 157 obj.size, | |
| 158 obj.id, | |
| 159 obj.filePath, | |
| 160 new Date(obj.creationTime).toString(), | |
| 161 new Date(obj.lastAccessedTime).toString(), | |
| 162 obj.accessCount]; | |
| 163 objFieldArr = objFieldArr.map(addQuotes); | |
| 164 csv += objFieldArr.join(',') + '\n'; | |
| 165 } | |
| 166 } | |
| 167 csv += '\n'; | |
| 168 // Create header & csv for request queue. | |
| 169 if (currentRequestQueueData && currentRequestQueueData.length > 0) { | |
| 170 csv += 'Online URL,Creation Time,Status,Namespace,Last Attempt Time,ID\n'; | |
| 171 | |
| 172 for (let obj of currentRequestQueueData) { | |
| 173 var objFieldArr = [ | |
| 174 obj.onlineUrl, | |
| 175 new Date(obj.creationTime).toString(), | |
| 176 obj.status, | |
| 177 obj.namespace, | |
| 178 new Date(obj.lastAttempt).toString(), | |
| 179 obj.id]; | |
| 180 objFieldArr = objFieldArr.map(addQuotes); | |
| 181 csv += objFieldArr.join(',') + '\n'; | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 var uriContent = 'data:text/csv,' + encodeURIComponent(csv); | |
| 186 window.open(uriContent, 'dump.csv'); | |
|
dewittj
2016/06/03 23:12:06
Does this open a window, or does it cause a downlo
chili
2016/06/09 22:29:16
This will open a window that'll ask you to downloa
| |
| 187 } | |
| 188 | |
| 189 /** | |
| 142 * Delete selected pages from the offline store. | 190 * Delete selected pages from the offline store. |
| 143 */ | 191 */ |
| 144 function deleteSelectedPages() { | 192 function deleteSelectedPages() { |
| 145 var checkboxes = document.getElementsByName('stored'); | 193 var checkboxes = document.getElementsByName('stored'); |
| 146 var selectedIds = []; | 194 var selectedIds = []; |
| 147 for (var checkbox of checkboxes) { | 195 for (var checkbox of checkboxes) { |
| 148 if (checkbox.checked) | 196 if (checkbox.checked) |
| 149 selectedIds.push(checkbox.value); | 197 selectedIds.push(checkbox.value); |
| 150 } | 198 } |
| 151 | 199 |
| 152 cr.sendWithPromise('deleteSelectedPages', selectedIds).then(pagesDeleted); | 200 cr.sendWithPromise('deleteSelectedPages', selectedIds).then(pagesDeleted); |
| 153 } | 201 } |
| 154 | 202 |
| 155 function initialize() { | 203 function initialize() { |
| 156 $('clear-all').onclick = deleteAllPages; | 204 $('clear-all').onclick = deleteAllPages; |
| 157 $('clear-selected').onclick = deleteSelectedPages; | 205 $('clear-selected').onclick = deleteSelectedPages; |
| 158 $('refresh').onclick = refreshAll; | 206 $('refresh').onclick = refreshAll; |
| 207 $('download').onclick = download; | |
| 159 refreshAll(); | 208 refreshAll(); |
| 160 } | 209 } |
| 161 | 210 |
| 162 // Return an object with all of the exports. | 211 // Return an object with all of the exports. |
| 163 return { | 212 return { |
| 164 initialize: initialize, | 213 initialize: initialize, |
| 165 }; | 214 }; |
| 166 }); | 215 }); |
| 167 | 216 |
| 168 document.addEventListener('DOMContentLoaded', offlineInternals.initialize); | 217 document.addEventListener('DOMContentLoaded', offlineInternals.initialize); |
| OLD | NEW |