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 /** | |
| 6 * @typedef {{ | |
| 7 * onlineUrl: string, | |
| 8 * creationTime: number, | |
| 9 * id: string, | |
| 10 * namespace: string, | |
| 11 * size: string, | |
| 12 * filePath: string, | |
| 13 * lastAccessTime: number, | |
| 14 * accessCount: number | |
| 15 * }} | |
|
Dan Beam
2016/06/09 23:35:01
indent off by 1 \s
chili
2016/06/10 01:04:51
Done.
| |
| 16 */ | |
| 17 var OfflinePageItem; | |
| 18 | |
| 19 /** | |
| 20 * @typedef {{ | |
| 21 * status: string, | |
| 22 * onlineUrl: string, | |
| 23 * creationTime: number, | |
| 24 * id: string, | |
| 25 * namespace: string, | |
| 26 * lastAttempt: number | |
| 27 * }} | |
| 28 */ | |
| 29 var SavePageRequest; | |
| 30 | |
| 5 cr.define('offlineInternals', function() { | 31 cr.define('offlineInternals', function() { |
| 6 'use strict'; | 32 'use strict'; |
| 7 | 33 |
| 8 /** | 34 /** @type {Array<OfflinePageItem>} */ |
|
Dan Beam
2016/06/09 23:35:01
nit: ?Array<!OfflinePageItems>
! means "not null"
dewittj
2016/06/09 23:41:25
I don't think "!" is required for record types (wh
Dan Beam
2016/06/09 23:50:01
@typedefs are nullable by default
dewittj
2016/06/10 00:13:49
Can you point me to documentation to that effect?
Dan Beam
2016/06/10 00:28:17
eh, do whatever
i care far less about what you do
dpapad
2016/06/10 00:47:04
FYI, it seems that non-nullable is the default for
chili
2016/06/10 01:04:51
Acknowledged.
chili
2016/06/10 01:04:51
Acknowledged.
chili
2016/06/10 01:04:51
Done.
chili
2016/06/10 20:21:03
removed the !
| |
| 9 * @typedef {{ | 35 var currentStoredPagesData = null; |
| 10 * onlineUrl: string, | |
| 11 * creationTime: number, | |
| 12 * status: number, | |
| 13 * id: string, | |
| 14 * namespace: string, | |
| 15 * size: string, | |
| 16 * filePath: string, | |
| 17 * lastAccessTime: number, | |
| 18 * accessCount: number | |
| 19 * }} | |
| 20 */ | |
| 21 var OfflinePageItem; | |
| 22 | 36 |
| 23 /** | 37 /** @type {Array<SavePageRequest>} */ |
| 24 * @typedef {{ | 38 var currentRequestQueueData = null; |
| 25 * status: string, | |
| 26 * onlineUrl: string, | |
| 27 * creationTime: number, | |
| 28 * id: string, | |
| 29 * namespace: string, | |
| 30 * attemptCount: number | |
| 31 * }} | |
| 32 */ | |
| 33 var SavePageRequest; | |
| 34 | |
| 35 /** | |
| 36 * Clear the specified table. | |
| 37 * @param {string} tableId id of the table to clear. | |
| 38 */ | |
| 39 function clearTable(tableId) { | |
| 40 $(tableId).textContent = ''; | |
| 41 } | |
| 42 | 39 |
| 43 /** | 40 /** |
| 44 * Fill stored pages table. | 41 * Fill stored pages table. |
| 45 * @param {HTMLElement} element A HTML element. | 42 * @param {!Array<OfflinePageItem>} data An array object representing |
|
Dan Beam
2016/06/09 23:35:01
unless this array can have null in it, !Array<!Off
Dan Beam
2016/06/09 23:35:01
why did you revert pages to data?
chili
2016/06/10 01:04:51
Done.
chili
2016/06/10 01:04:51
I had this CL sitting for almost as long as the pr
| |
| 46 * @param {!Array<OfflinePageItem>} pages An array object representing | |
| 47 * stored offline pages. | 43 * stored offline pages. |
| 48 */ | 44 */ |
| 49 function fillStoredPages(element, pages) { | 45 function fillStoredPages(data) { |
| 50 for (var i = 0; i < pages.length; i++) { | 46 var element = $('stored-pages'); |
|
Dan Beam
2016/06/09 23:35:01
element -> storedPages?
chili
2016/06/10 01:04:51
Done.
| |
| 47 element.textContent = ''; | |
| 48 | |
| 49 for (var i = 0; i < data.length; i++) { | |
| 51 var row = document.createElement('tr'); | 50 var row = document.createElement('tr'); |
| 52 | 51 |
| 53 var checkboxCell = document.createElement('td'); | 52 var checkboxCell = document.createElement('td'); |
| 54 var checkbox = document.createElement('input'); | 53 var checkbox = document.createElement('input'); |
| 55 checkbox.setAttribute('type', 'checkbox'); | 54 checkbox.setAttribute('type', 'checkbox'); |
| 56 checkbox.setAttribute('name', 'stored'); | 55 checkbox.setAttribute('name', 'stored'); |
| 57 checkbox.setAttribute('value', pages[i].id); | 56 checkbox.setAttribute('value', data[i].id); |
| 58 | 57 |
| 59 checkboxCell.appendChild(checkbox); | 58 checkboxCell.appendChild(checkbox); |
| 60 row.appendChild(checkboxCell); | 59 row.appendChild(checkboxCell); |
| 61 | 60 |
| 62 var cell = document.createElement('td'); | 61 var cell = document.createElement('td'); |
| 63 cell.textContent = pages[i].onlineUrl; | 62 cell.textContent = data[i].onlineUrl; |
| 64 row.appendChild(cell); | 63 row.appendChild(cell); |
| 65 | 64 |
| 66 cell = document.createElement('td'); | 65 cell = document.createElement('td'); |
| 67 cell.textContent = pages[i].namespace; | 66 cell.textContent = data[i].namespace; |
| 68 row.appendChild(cell); | 67 row.appendChild(cell); |
| 69 | 68 |
| 70 cell = document.createElement('td'); | 69 cell = document.createElement('td'); |
| 71 cell.textContent = pages[i].size; | 70 cell.textContent = Math.round(data[i].size / 1024); |
| 72 row.appendChild(cell); | 71 row.appendChild(cell); |
| 73 | 72 |
| 74 element.appendChild(row); | 73 element.appendChild(row); |
| 75 } | 74 } |
| 75 currentStoredPagesData = data; | |
| 76 } | 76 } |
| 77 | 77 |
| 78 /** | 78 /** |
| 79 * Fill requests table. | 79 * Fill requests table. |
| 80 * @param {HTMLElement} element A HTML element. | 80 * @param {!Array<SavePageRequest>} data An array object representing |
| 81 * @param {!Array<SavePageRequest>} requests An array object representing | |
| 82 * the request queue. | 81 * the request queue. |
| 83 */ | 82 */ |
| 84 function fillRequestQueue(element, requests) { | 83 function fillRequestQueue(data) { |
|
Dan Beam
2016/06/09 23:35:01
same question regarding data vs requests
chili
2016/06/10 01:04:51
Same as above... bad merge
| |
| 85 for (var i = 0; i < requests.length; i++) { | 84 var element = $('request-queue'); |
| 85 element.textContent = ''; | |
| 86 | |
| 87 for (var i = 0; i < data.length; i++) { | |
| 86 var row = document.createElement('tr'); | 88 var row = document.createElement('tr'); |
| 87 | 89 |
| 88 var cell = document.createElement('td'); | 90 var cell = document.createElement('td'); |
| 89 cell.textContent = requests[i].onlineUrl; | 91 cell.textContent = data[i].onlineUrl; |
| 90 row.appendChild(cell); | 92 row.appendChild(cell); |
| 91 | 93 |
| 92 cell = document.createElement('td'); | 94 cell = document.createElement('td'); |
| 93 cell.textContent = new Date(requests[i].creationTime); | 95 cell.textContent = new Date(data[i].creationTime); |
| 94 row.appendChild(cell); | 96 row.appendChild(cell); |
| 95 | 97 |
| 96 cell = document.createElement('td'); | 98 cell = document.createElement('td'); |
| 97 cell.textContent = requests[i].status; | 99 cell.textContent = data[i].status; |
| 98 row.appendChild(cell); | 100 row.appendChild(cell); |
| 99 | 101 |
| 100 element.appendChild(row); | 102 element.appendChild(row); |
| 101 } | 103 } |
| 104 currentRequestQueueData = data; | |
|
Dan Beam
2016/06/09 23:35:01
currentRequests?
chili
2016/06/10 01:04:51
Done.
| |
| 102 } | 105 } |
| 103 | 106 |
| 104 /** | 107 /** |
| 105 * Refresh all displayed information. | 108 * Refresh all displayed information. |
| 106 */ | 109 */ |
| 107 function refreshAll() { | 110 function refreshAll() { |
| 108 cr.sendWithPromise('getOfflineInternalsInfo').then(setOfflineInternalsInfo); | 111 cr.sendWithPromise('getStoredPagesInfo').then(fillStoredPages); |
| 112 cr.sendWithPromise('getRequestQueueInfo').then(fillRequestQueue); | |
| 109 } | 113 } |
| 110 | 114 |
| 111 /** | 115 /** |
| 112 * Delete all pages in the offline store. | 116 * Delete all pages in the offline store. |
| 113 */ | 117 */ |
| 114 function deleteAllPages() { | 118 function deleteAllPages() { |
| 115 cr.sendWithPromise('deleteAllPages').then(pagesDeleted); | 119 cr.sendWithPromise('deleteAllPages').then(pagesDeleted); |
| 116 } | 120 } |
| 117 | 121 |
| 118 /** | 122 /** |
| 119 * Callback when pages are deleted. | 123 * Callback when pages are deleted. |
| 120 * @param {string} deletePageStatus The status of delete page call. | 124 * @param {string} status The status of the request. |
| 121 */ | 125 */ |
| 122 function pagesDeleted(deletePageStatus) { | 126 function pagesDeleted(status) { |
| 123 // TODO(chili): decide what to do here. Perhaps a refresh of just | 127 $('page-actions-info').textContent = status; |
| 124 // the stored pages table? | 128 cr.sendWithPromise('getStoredPagesInfo').then(fillStoredPages); |
| 125 } | 129 } |
| 126 | 130 |
| 127 /** | 131 /** |
| 128 * Callback when information is loaded. | 132 * Helper function to JSON-escape and add quotes around a string. |
| 129 * @param {{AllPages: !Array<OfflinePageItem>, | 133 * @param {string} strObj The obj to escape and add quotes around. |
| 130 * Queue: !Array<SavePageRequest>}} info An object containing both | 134 * @return {string} the escaped string. |
|
dewittj
2016/06/09 23:41:25
nit: capitalize the T
chili
2016/06/10 01:04:51
Done.
| |
| 131 * stored pages and request queue status. | |
| 132 */ | 135 */ |
| 133 function setOfflineInternalsInfo(info) { | 136 function escapeString(strObj) { |
| 134 clearTable('stored-pages'); | 137 strObj.replace(/"/g, '""'); // CSV single quotes are encoded as "". |
|
Dan Beam
2016/06/09 23:35:01
this returns a new string and does not work in pla
chili
2016/06/10 01:04:51
oops. fixed.
| |
| 135 clearTable('request-queue'); | 138 return '"' + strObj + '"'; // There can be commas in the string. |
|
Dan Beam
2016/06/09 23:35:01
return '"' + strObj.replace(/"/g, '""') + '"';
chili
2016/06/10 01:04:51
Done.
| |
| 136 | |
| 137 fillStoredPages($('stored-pages'), info.AllPages); | |
| 138 fillRequestQueue($('request-queue'), info.Queue); | |
| 139 } | 139 } |
| 140 | 140 |
| 141 /** | 141 /** |
| 142 * Downloads all the stored page and request queue information into a file. | |
| 143 */ | |
| 144 function download() { | |
| 145 var csv = ''; | |
| 146 // Create header & csv for stored pages. | |
| 147 if (currentStoredPagesData && currentStoredPagesData.length > 0) { | |
| 148 csv += 'Online URL,Namespace,Size,ID,File Path,Creation Time,' + | |
| 149 'Last Accessed Time,Access Count\n'; | |
| 150 for (let obj of currentStoredPagesData) { | |
| 151 var objFieldArr = [ | |
| 152 obj.onlineUrl, | |
| 153 obj.namespace, | |
| 154 obj.size, | |
| 155 obj.id, | |
| 156 obj.filePath, | |
| 157 new Date(obj.creationTime).toString(), | |
| 158 new Date(obj.lastAccessTime).toString(), | |
| 159 obj.accessCount]; | |
| 160 objFieldArr = objFieldArr.map(escapeString); | |
| 161 csv += objFieldArr.join(',') + '\n'; | |
| 162 } | |
| 163 } | |
| 164 csv += '\n'; | |
| 165 // Create header & csv for request queue. | |
| 166 if (currentRequestQueueData && currentRequestQueueData.length > 0) { | |
| 167 csv += 'Online URL,Creation Time,Status,Namespace,Last Attempt Time,ID\n'; | |
| 168 | |
| 169 for (let obj of currentRequestQueueData) { | |
| 170 var objFieldArr = [ | |
| 171 obj.onlineUrl, | |
| 172 new Date(obj.creationTime).toString(), | |
| 173 obj.status, | |
| 174 obj.namespace, | |
| 175 new Date(obj.lastAttempt).toString(), | |
| 176 obj.id]; | |
| 177 objFieldArr = objFieldArr.map(escapeString); | |
| 178 csv += objFieldArr.join(',') + '\n'; | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 var uriContent = 'data:text/csv,' + encodeURIComponent(csv); | |
| 183 window.open(uriContent, 'dump.csv'); | |
| 184 } | |
|
Dan Beam
2016/06/09 23:35:01
is there any particular reason you want to do this
chili
2016/06/10 01:04:51
Not knowing too much about webui and how to get do
Dan Beam
2016/06/13 19:13:28
this doesn't make much sense. why would you write
chili
2016/06/13 20:47:14
Right now I prefer to keep the CSV generation in J
Dan Beam
2016/06/13 21:13:23
Is there a reason other than your preference? Why
chili
2016/06/13 23:39:17
I was looking at the uses for csv_writer when you
| |
| 185 | |
| 186 /** | |
| 142 * Delete selected pages from the offline store. | 187 * Delete selected pages from the offline store. |
| 143 */ | 188 */ |
| 144 function deleteSelectedPages() { | 189 function deleteSelectedPages() { |
| 145 var checkboxes = document.getElementsByName('stored'); | 190 var checkboxes = document.getElementsByName('stored'); |
| 146 var selectedIds = []; | 191 var selectedIds = []; |
| 147 for (var checkbox of checkboxes) { | 192 |
| 148 if (checkbox.checked) | 193 for (var i = 0; i < checkboxes.length; i++) { |
| 149 selectedIds.push(checkbox.value); | 194 if (checkboxes[i].checked) { |
|
Dan Beam
2016/06/09 23:35:01
no curlies
chili
2016/06/10 01:04:51
Done.
| |
| 195 selectedIds.push(checkboxes[i].value); | |
| 196 } | |
| 150 } | 197 } |
| 151 | 198 |
| 152 cr.sendWithPromise('deleteSelectedPages', selectedIds).then(pagesDeleted); | 199 cr.sendWithPromise('deleteSelectedPages', selectedIds).then(pagesDeleted); |
| 153 } | 200 } |
| 154 | 201 |
| 155 function initialize() { | 202 function initialize() { |
| 156 $('clear-all').onclick = deleteAllPages; | 203 $('clear-all').onclick = deleteAllPages; |
| 157 $('clear-selected').onclick = deleteSelectedPages; | 204 $('clear-selected').onclick = deleteSelectedPages; |
| 158 $('refresh').onclick = refreshAll; | 205 $('refresh').onclick = refreshAll; |
| 206 $('download').onclick = download; | |
| 159 refreshAll(); | 207 refreshAll(); |
| 160 } | 208 } |
| 161 | 209 |
| 162 // Return an object with all of the exports. | 210 // Return an object with all of the exports. |
| 163 return { | 211 return { |
| 164 initialize: initialize, | 212 initialize: initialize, |
| 165 }; | 213 }; |
| 166 }); | 214 }); |
| 167 | 215 |
| 168 document.addEventListener('DOMContentLoaded', offlineInternals.initialize); | 216 document.addEventListener('DOMContentLoaded', offlineInternals.initialize); |
| OLD | NEW |