Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: chrome/browser/resources/offline_pages/offline_internals.js

Issue 2038963002: [Offline Pages] Link the internals page with the offline model and request (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More code review comment fixes Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 OfflinePage;
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<OfflinePage>} */
9 * @typedef {{ 35 var offlinePages = null;
Dan Beam 2016/06/14 00:07:50 why is this null and not []?
chili 2016/06/14 04:45:21 Done.
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 savePageRequests = null;
Dan Beam 2016/06/14 00:07:50 why not []?
chili 2016/06/14 04:45:21 Done.
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<OfflinePage>} pages An array object representing
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(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 offlinePages = 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 savePageRequests = 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, '""') + '"';
Dan Beam 2016/06/14 00:07:50 what if there's a comma in the string? I am menti
chili 2016/06/14 04:45:21 That is why we wrap the entire string with quotes
Dan Beam 2016/06/14 15:43:36 ah, ok, cool
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 * TODO(chili): Create a CSV writer that can abstract out the line joining.
144 */
145 function download() {
146 var csv = '';
147 // Create header & csv for stored pages.
148 if (offlinePages && offlinePages.length > 0) {
149 csv += 'Online URL,Namespace,Size,ID,File Path,Creation Time,' +
150 'Last Accessed Time,Access Count\n';
151 for (let page of offlinePages) {
152 var csvFields = [
153 page.onlineUrl,
154 page.namespace,
155 page.size,
156 page.id,
157 page.filePath,
158 new Date(page.creationTime).toString(),
159 new Date(page.lastAccessTime).toString(),
160 page.accessCount];
161 csvFields = csvFields.map(escapeString);
162 csv += csvFields.join(',') + '\n';
163 }
164 }
165 csv += '\n';
166 // Create header & csv for request queue.
167 if (savePageRequests && savePageRequests.length > 0) {
168 csv += 'Online URL,Creation Time,Status,Namespace,Last Attempt Time,ID\n';
169
170 for (let request of savePageRequests) {
171 var csvFields = [
172 request.onlineUrl,
173 new Date(request.creationTime).toString(),
174 request.status,
175 request.namespace,
176 new Date(request.lastAttempt).toString(),
177 request.id];
178 csvFields = csvFields.map(escapeString);
179 csv += csvFields.join(',') + '\n';
180 }
181 }
182
183 var uriContent = 'data:text/csv,' + encodeURIComponent(csv);
184 window.open(uriContent, 'dump.csv');
185 }
186
187 /**
142 * Delete selected pages from the offline store. 188 * Delete selected pages from the offline store.
143 */ 189 */
144 function deleteSelectedPages() { 190 function deleteSelectedPages() {
145 var checkboxes = document.getElementsByName('stored'); 191 var checkboxes = document.getElementsByName('stored');
146 var selectedIds = []; 192 var selectedIds = [];
147 for (var checkbox of checkboxes) { 193
148 if (checkbox.checked) 194 for (var i = 0; i < checkboxes.length; i++) {
149 selectedIds.push(checkbox.value); 195 if (checkboxes[i].checked)
196 selectedIds.push(checkboxes[i].value);
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);
OLDNEW
« no previous file with comments | « chrome/browser/resources/offline_pages/offline_internals.html ('k') | chrome/browser/ui/webui/offline_internals_ui.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698