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 * }} |
| 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>} */ |
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 currentRequestsData = 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. | |
46 * @param {!Array<OfflinePageItem>} pages An array object representing | 42 * @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(pages) { |
| 46 var storedPagesTable = $('stored-pages'); |
| 47 storedPagesTable.textContent = ''; |
| 48 |
50 for (var i = 0; i < pages.length; i++) { | 49 for (var i = 0; i < pages.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', pages[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 = pages[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 = pages[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(pages[i].size / 1024); |
72 row.appendChild(cell); | 71 row.appendChild(cell); |
73 | 72 |
74 element.appendChild(row); | 73 storedPagesTable.appendChild(row); |
75 } | 74 } |
| 75 currentStoredPagesData = pages; |
76 } | 76 } |
77 | 77 |
78 /** | 78 /** |
79 * Fill requests table. | 79 * Fill requests table. |
80 * @param {HTMLElement} element A HTML element. | |
81 * @param {!Array<SavePageRequest>} requests An array object representing | 80 * @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(requests) { |
| 84 var requestQueueTable = $('request-queue'); |
| 85 requestQueueTable.textContent = ''; |
| 86 |
85 for (var i = 0; i < requests.length; i++) { | 87 for (var i = 0; i < requests.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 = requests[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(requests[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 = requests[i].status; |
98 row.appendChild(cell); | 100 row.appendChild(cell); |
99 | 101 |
100 element.appendChild(row); | 102 requestQueueTable.appendChild(row); |
101 } | 103 } |
| 104 currentRequestsData = requests; |
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. |
131 * stored pages and request queue status. | |
132 */ | 135 */ |
133 function setOfflineInternalsInfo(info) { | 136 function escapeString(strObj) { |
134 clearTable('stored-pages'); | 137 // CSV single quotes are encoded as "". There can also be commas. |
135 clearTable('request-queue'); | 138 return '"' + strObj.replace(/"/g, '""') + '"'; |
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 } |
| 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) |
| 195 selectedIds.push(checkboxes[i].value); |
150 } | 196 } |
151 | 197 |
152 cr.sendWithPromise('deleteSelectedPages', selectedIds).then(pagesDeleted); | 198 cr.sendWithPromise('deleteSelectedPages', selectedIds).then(pagesDeleted); |
153 } | 199 } |
154 | 200 |
155 function initialize() { | 201 function initialize() { |
156 $('clear-all').onclick = deleteAllPages; | 202 $('clear-all').onclick = deleteAllPages; |
157 $('clear-selected').onclick = deleteSelectedPages; | 203 $('clear-selected').onclick = deleteSelectedPages; |
158 $('refresh').onclick = refreshAll; | 204 $('refresh').onclick = refreshAll; |
| 205 $('download').onclick = download; |
159 refreshAll(); | 206 refreshAll(); |
160 } | 207 } |
161 | 208 |
162 // Return an object with all of the exports. | 209 // Return an object with all of the exports. |
163 return { | 210 return { |
164 initialize: initialize, | 211 initialize: initialize, |
165 }; | 212 }; |
166 }); | 213 }); |
167 | 214 |
168 document.addEventListener('DOMContentLoaded', offlineInternals.initialize); | 215 document.addEventListener('DOMContentLoaded', offlineInternals.initialize); |
OLD | NEW |