Chromium Code Reviews| OLD | NEW | 
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @typedef {{hasChildren: boolean, | |
| 7 * id: string, | |
| 8 * title: string, | |
| 9 * totalUsage: string, | |
| 10 * type: string}} | |
| 11 */ | |
| 12 var CookieDetails; | |
| 13 | |
| 14 /** | |
| 15 * @typedef {{title: string, | |
| 16 * id: string, | |
| 17 * data: CookieDetails}} | |
| 18 */ | |
| 19 var CookieDataItem; | |
| 20 | |
| 21 /** | |
| 22 * @typedef {{site: string, | |
| 23 * id: string, | |
| 24 * localData: string}} | |
| 25 */ | |
| 26 var CookieDataSummaryItem; | |
| 27 | |
| 28 // This structure maps the various cookie type names from C++ (hence the | |
| 29 // underscores) to arrays of the different types of data each has, along with | |
| 30 // the i18n name for the description of that data type. | |
| 31 /** @const */ var cookieInfo = { | |
| 32 'cookie': [['name', 'label_cookie_name'], | |
| 
 
michaelpg
2016/07/04 19:44:21
Yikes. Can we generate this in C++ instead of dupl
 
Finnur
2016/07/05 11:04:49
So... couple of comments. But first some backgroun
 
michaelpg
2016/07/05 18:01:11
The comment doesn't make that clear (that this was
 
Finnur
2016/07/06 09:57:34
I've updated the comment and moved it to a separat
 
 | |
| 33 ['content', 'label_cookie_content'], | |
| 34 ['domain', 'label_cookie_domain'], | |
| 35 ['path', 'label_cookie_path'], | |
| 36 ['sendfor', 'label_cookie_send_for'], | |
| 37 ['accessibleToScript', 'label_cookie_accessible_to_script'], | |
| 38 ['created', 'label_cookie_created'], | |
| 39 ['expires', 'label_cookie_expires']], | |
| 40 'app_cache': [['manifest', 'label_app_cache_manifest'], | |
| 41 ['size', 'label_local_storage_size'], | |
| 42 ['created', 'label_cookie_created'], | |
| 43 ['accessed', 'label_cookie_last_accessed']], | |
| 44 'database': [['name', 'label_cookie_name'], | |
| 45 ['desc', 'label_webdb_desc'], | |
| 46 ['size', 'label_local_storage_size'], | |
| 47 ['modified', 'label_local_storage_last_modified']], | |
| 48 'local_storage': [['origin', 'label_local_storage_origin'], | |
| 49 ['size', 'label_local_storage_size'], | |
| 50 ['modified', 'label_local_storage_last_modified']], | |
| 51 'indexed_db': [['origin', 'label_indexed_db_origin'], | |
| 52 ['size', 'label_indexed_db_size'], | |
| 53 ['modified', 'label_indexed_db_last_modified']], | |
| 54 'file_system': [['origin', 'label_file_system_origin'], | |
| 55 ['persistent', 'label_file_system_persistent_usage'], | |
| 56 ['temporary', 'label_file_system_temporary_usage']], | |
| 57 'channel_id': [['serverId', 'label_channel_id_server_id'], | |
| 58 ['certType', 'label_channel_id_type'], | |
| 59 ['created', 'label_channel_id_created']], | |
| 60 'service_worker': [['origin', 'label_service_worker_origin'], | |
| 61 ['size', 'label_service_worker_size'], | |
| 62 ['scopes', 'label_service_worker_scopes']], | |
| 63 'cache_storage': [['origin', 'label_cache_storage_origin'], | |
| 64 ['size', 'label_cache_storage_size'], | |
| 65 ['modified', 'label_cache_storage_last_modified']], | |
| 66 'flash_lso': [['domain', 'label_cookie_domain']], | |
| 67 }; | |
| 68 | |
| 69 cr.define('settings', function() { | |
| 70 'use strict'; | |
| 71 | |
| 72 /** | |
| 73 * @constructor | |
| 74 */ | |
| 75 function CookieTreeNode(data) { | |
| 76 /** | |
| 77 * The data for this cookie node. | |
| 78 * @private {CookieDetails} | |
| 79 */ | |
| 80 this.data = data; | |
| 
 
michaelpg
2016/07/04 19:44:20
throughout: trailing underscores for private membe
 
Finnur
2016/07/05 11:04:49
Done.
 
 | |
| 81 | |
| 82 /** | |
| 83 * The child cookie nodes. | |
| 84 * @private {!Array<settings.CookieTreeNode>} | |
| 85 */ | |
| 86 this.children = []; | |
| 87 }; | |
| 88 | |
| 89 CookieTreeNode.prototype = { | |
| 90 /** | |
| 91 * Converts a list of cookies and add them as CookieTreenode children to | |
| 
 
michaelpg
2016/07/04 19:44:21
Node
 
Finnur
2016/07/05 11:04:49
Done.
 
 | |
| 92 * the given parent node. | |
| 93 * @param {settings.CookieTreeNode} parentNode The parent node to add | |
| 
 
michaelpg
2016/07/04 19:44:21
{!...}
 
Finnur
2016/07/05 11:04:48
Done.
 
 | |
| 94 * children to. | |
| 95 * @param {!Array<CookieDetails>} newNodes The list containing the data to | |
| 96 * add. | |
| 97 */ | |
| 98 addChildNodes: function(parentNode, newNodes) { | |
| 99 var nodes = newNodes.map(function(x) { | |
| 100 return new settings.CookieTreeNode(x); | |
| 
 
michaelpg
2016/07/04 19:44:20
2-space indent for function
 
Finnur
2016/07/05 11:04:49
Done.
 
 | |
| 101 }); | |
| 102 parentNode.children = nodes; | |
| 103 }, | |
| 104 | |
| 105 /** | |
| 106 * Looks up a parent node and adds a list of CookieTreeNodes to them. | |
| 107 * @param {string} parentId The ID of the parent to add the nodes to. | |
| 108 * @param {settings.CookieTreeNode} startingNode The node to start with when | |
| 109 * looking for the parent node to add the children to. | |
| 110 * @param {!Array<CookieDetails>} newNodes The list containing the data to | |
| 111 add. | |
| 112 * @return {boolean} True if the parent node was found. | |
| 113 */ | |
| 114 populateChildNodes: function(parentId, startingNode, newNodes) { | |
| 115 for (var i = 0; i < startingNode.children.length; ++i) { | |
| 116 if (startingNode.children[i].data.id == parentId) { | |
| 117 this.addChildNodes(startingNode.children[i], newNodes); | |
| 118 return true; | |
| 119 } | |
| 120 | |
| 121 if (this.populateChildNodes( | |
| 122 parentId, startingNode.children[i], newNodes)) { | |
| 123 return true; | |
| 124 } | |
| 125 } | |
| 126 return false; | |
| 127 }, | |
| 128 | |
| 129 /** | |
| 130 * Removes child nodes from a node with a given id. | |
| 131 * @param {string} id The id of the parent node to delete from. | |
| 132 * @param {number} firstChild The index of the first child to start deleting | |
| 133 * from. | |
| 134 * @param {number} count The number of children to delete. | |
| 135 */ | |
| 136 removeByParentId: function(id, firstChild, count) { | |
| 137 var node = id == null ? this : this.fetchNodeById(id, true); | |
| 138 while (count--) | |
| 
 
michaelpg
2016/07/04 19:44:21
node.children.splice(firstChild, count) ?
 
Finnur
2016/07/05 11:04:48
Bah. Of course. Done. :)
 
 | |
| 139 node.children.splice(firstChild, 1); | |
| 140 }, | |
| 141 | |
| 142 /** | |
| 143 * Returns an array of cookies from the current node within the cookie tree. | |
| 144 * @return Array< {CookieDataItem} > The Cookie List. | |
| 
 
michaelpg
2016/07/04 19:44:20
nit: no spaces <{...}>
 
Finnur
2016/07/05 11:04:49
The presubmit check barfs on that...
Found JavaSc
 
michaelpg
2016/07/05 18:01:11
try wrapping Array in {}
 
Finnur
2016/07/06 09:57:34
Done.
 
 | |
| 145 */ | |
| 146 getCookieList: function() { | |
| 147 var list = []; | |
| 148 for (var i = 0; i < this.children.length; ++i) { | |
| 149 var group = this.children[i]; | |
| 150 for (var j = 0; j < group.children.length; ++j) { | |
| 
 
michaelpg
2016/07/04 19:44:21
opt nit: for-of for nested loops for readability:
 
Finnur
2016/07/05 11:04:49
Done.
 
 | |
| 151 var cookie = group.children[j]; | |
| 152 list.push({title: cookie.data.title, | |
| 153 id: cookie.data.id, | |
| 154 data: cookie.data}); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 return list; | |
| 159 }, | |
| 160 | |
| 161 /** | |
| 162 * Get a summary list of all sites and their stored data. | |
| 163 * @return {Array<CookieDataSummaryItem>} The summary list. | |
| 164 */ | |
| 165 getSummaryList: function() { | |
| 166 var list = []; | |
| 167 for (var i = 0; i < this.children.length; ++i) { | |
| 168 var siteEntry = this.children[i]; | |
| 169 var title = siteEntry.data.title; | |
| 170 var id = siteEntry.data.id; | |
| 171 var description = ''; | |
| 172 | |
| 173 for (var j = 0; j < siteEntry.children.length; ++j) { | |
| 174 var descriptionNode = siteEntry.children[j]; | |
| 175 if (j > 0) | |
| 176 description += ', '; | |
| 177 | |
| 178 // Some types, like quota, have no description nodes. | |
| 179 var dataType = ''; | |
| 180 if (descriptionNode.data.type != undefined) | |
| 181 dataType = descriptionNode.data.type; | |
| 182 else | |
| 183 dataType = descriptionNode.children[0].data.type; | |
| 184 | |
| 185 var category = ''; | |
| 186 if (dataType == 'cookie') { | |
| 187 var cookieCount = descriptionNode.children.length; | |
| 188 if (cookieCount > 1) | |
| 189 category = loadTimeData.getStringF('cookie_plural', cookieCount); | |
| 190 else | |
| 191 category = loadTimeData.getString('cookie_singular'); | |
| 192 } else if (dataType == 'database') { | |
| 193 category = loadTimeData.getString('cookie_database_storage'); | |
| 194 } else if (dataType == 'local_storage' || dataType == 'indexed_db') { | |
| 195 category = loadTimeData.getString('cookie_local_storage'); | |
| 196 } else if (dataType == 'app_cache') { | |
| 197 category = loadTimeData.getString('cookie_app_cache'); | |
| 198 } else if (dataType == 'file_system') { | |
| 199 category = loadTimeData.getString('cookie_file_system'); | |
| 200 } else if (dataType == 'quota') { | |
| 201 category = descriptionNode.data.totalUsage; | |
| 202 } else if (dataType == 'channel_id') { | |
| 203 category = loadTimeData.getString('cookie_channel_id'); | |
| 204 } else if (dataType == 'service_worker') { | |
| 205 category = loadTimeData.getString('cookie_service_worker'); | |
| 206 } else if (dataType == 'cache_storage') { | |
| 207 category = loadTimeData.getString('cookie_cache_storage'); | |
| 208 } else if (dataType == 'flash_lso') { | |
| 209 category = loadTimeData.getString('cookie_flash_lso'); | |
| 210 } | |
| 211 | |
| 212 description += category; | |
| 213 } | |
| 214 list.push({ site: title, id: id, localData: description }); | |
| 215 } | |
| 216 list.sort(function(a, b) { | |
| 217 return a.site.localeCompare(b.site); | |
| 218 }); | |
| 219 return list; | |
| 220 }, | |
| 221 | |
| 222 /** | |
| 223 * Fetch a CookieTreeNode by ID. | |
| 224 * @param {string} id The ID to look up. | |
| 225 * @param {boolean} recursive Whether to search the children also. | |
| 226 * @return {settings.CookieTreeNode} The node found, if any. | |
| 227 */ | |
| 228 fetchNodeById: function(id, recursive) { | |
| 229 for (var i = 0; i < this.children.length; ++i) { | |
| 230 if (this.children[i] == null) | |
| 231 return null; | |
| 232 if (this.children[i].data.id == id) | |
| 233 return this.children[i]; | |
| 234 if (recursive) { | |
| 235 var node = this.children[i].fetchNodeById(id, true); | |
| 236 if (node != null) | |
| 237 return node; | |
| 238 } | |
| 239 } | |
| 240 return null; | |
| 241 }, | |
| 242 | |
| 243 /** | |
| 244 * Add cookie data to a given HTML node. | |
| 245 * @param {HTMLElement} root The node to add the data to. | |
| 246 * @param {settings.CookieTreeNode} item The data to add. | |
| 247 */ | |
| 248 addCookieData: function(root, item) { | |
| 249 var type = item.data.type; | |
| 250 for (var i = 0; i < cookieInfo[type].length; ++i) { | |
| 251 var key = item.data[cookieInfo[type][i][0]]; | |
| 
 
michaelpg
2016/07/04 19:44:21
now my brain hurts >_<
 
Finnur
2016/07/05 11:04:49
I've made an attempt at commenting what is going o
 
michaelpg
2016/07/05 18:01:11
Yes, very much!
 
 | |
| 252 if (key.length > 0) { | |
| 253 var label = loadTimeData.getString(cookieInfo[type][i][1]); | |
| 254 var value = item.data[cookieInfo[type][i][0]]; | |
| 255 | |
| 256 var header = document.createElement('div'); | |
| 257 header.appendChild(document.createTextNode(label)); | |
| 258 var content = document.createElement('div'); | |
| 259 content.appendChild(document.createTextNode(value)); | |
| 260 root.appendChild(header); | |
| 261 root.appendChild(content); | |
| 262 } | |
| 263 } | |
| 264 }, | |
| 265 }; | |
| 266 | |
| 267 return { | |
| 268 CookieTreeNode: CookieTreeNode, | |
| 269 }; | |
| 270 }); | |
| OLD | NEW |