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'], | |
| 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; | |
| 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 | |
| 92 * the given parent node. | |
| 93 * @param {!settings.CookieTreeNode} parentNode The parent node to add | |
| 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); | |
| 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 | |
| 109 * when 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 node.children_.splice(firstChild, count); | |
| 139 }, | |
| 140 | |
| 141 /** | |
| 142 * Returns an array of cookies from the current node within the cookie tree. | |
| 143 * @return Array< {!CookieDataItem} > The Cookie List. | |
| 144 */ | |
| 145 getCookieList: function() { | |
| 146 var list = []; | |
| 147 | |
| 148 for (var group of this.children_) { | |
| 149 for (var cookie of group.children_) { | |
| 150 list.push({title: cookie.data_.title, | |
| 151 id: cookie.data_.id, | |
| 152 data: cookie.data_}); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 return list; | |
| 157 }, | |
| 158 | |
| 159 /** | |
| 160 * Get a summary list of all sites and their stored data. | |
| 161 * @return {Array<!CookieDataSummaryItem>} The summary list. | |
| 162 */ | |
| 163 getSummaryList: function() { | |
| 164 var list = []; | |
| 165 for (var i = 0; i < this.children_.length; ++i) { | |
| 166 var siteEntry = this.children_[i]; | |
| 167 var title = siteEntry.data_.title; | |
| 168 var id = siteEntry.data_.id; | |
| 169 var description = ''; | |
| 170 | |
| 171 for (var j = 0; j < siteEntry.children_.length; ++j) { | |
| 172 var descriptionNode = siteEntry.children_[j]; | |
| 173 if (j > 0) | |
| 174 description += ', '; | |
| 175 | |
| 176 // Some types, like quota, have no description nodes. | |
| 177 var dataType = ''; | |
| 178 if (descriptionNode.data_.type != undefined) | |
| 179 dataType = descriptionNode.data_.type; | |
| 180 else | |
| 181 dataType = descriptionNode.children_[0].data_.type; | |
| 182 | |
| 183 var category = ''; | |
| 184 if (dataType == 'cookie') { | |
| 185 var cookieCount = descriptionNode.children_.length; | |
| 186 if (cookieCount > 1) | |
| 187 category = loadTimeData.getStringF('cookie_plural', cookieCount); | |
| 188 else | |
| 189 category = loadTimeData.getString('cookie_singular'); | |
| 190 } else if (dataType == 'database') { | |
| 191 category = loadTimeData.getString('cookie_database_storage'); | |
| 192 } else if (dataType == 'local_storage' || dataType == 'indexed_db') { | |
| 193 category = loadTimeData.getString('cookie_local_storage'); | |
| 194 } else if (dataType == 'app_cache') { | |
| 195 category = loadTimeData.getString('cookie_app_cache'); | |
| 196 } else if (dataType == 'file_system') { | |
| 197 category = loadTimeData.getString('cookie_file_system'); | |
| 198 } else if (dataType == 'quota') { | |
| 199 category = descriptionNode.data_.totalUsage; | |
| 200 } else if (dataType == 'channel_id') { | |
| 201 category = loadTimeData.getString('cookie_channel_id'); | |
| 202 } else if (dataType == 'service_worker') { | |
| 203 category = loadTimeData.getString('cookie_service_worker'); | |
| 204 } else if (dataType == 'cache_storage') { | |
| 205 category = loadTimeData.getString('cookie_cache_storage'); | |
| 206 } else if (dataType == 'flash_lso') { | |
| 207 category = loadTimeData.getString('cookie_flash_lso'); | |
| 208 } | |
| 209 | |
| 210 description += category; | |
| 211 } | |
| 212 list.push({ site: title, id: id, localData: description }); | |
| 213 } | |
| 214 list.sort(function(a, b) { | |
| 215 return a.site.localeCompare(b.site); | |
| 216 }); | |
| 217 return list; | |
| 218 }, | |
| 219 | |
| 220 /** | |
| 221 * Fetch a CookieTreeNode by ID. | |
| 222 * @param {string} id The ID to look up. | |
| 223 * @param {boolean} recursive Whether to search the children also. | |
| 224 * @return {settings.CookieTreeNode} The node found, if any. | |
| 225 */ | |
| 226 fetchNodeById: function(id, recursive) { | |
| 227 for (var i = 0; i < this.children_.length; ++i) { | |
| 228 if (this.children_[i] == null) | |
| 229 return null; | |
| 230 if (this.children_[i].data_.id == id) | |
| 231 return this.children_[i]; | |
| 232 if (recursive) { | |
| 233 var node = this.children_[i].fetchNodeById(id, true); | |
| 234 if (node != null) | |
| 235 return node; | |
| 236 } | |
| 237 } | |
| 238 return null; | |
| 239 }, | |
| 240 | |
| 241 /** | |
| 242 * Add cookie data to a given HTML node. | |
| 243 * @param {HTMLElement} root The node to add the data to. | |
| 244 * @param {!settings.CookieTreeNode} item The data to add. | |
| 245 */ | |
| 246 addCookieData: function(root, item) { | |
| 247 var type = item.data_.type; | |
| 
 
michaelpg
2016/07/05 18:01:11
I still suggest some cleanup to avoid the [[][][]]
 
Finnur
2016/07/06 09:57:35
Done.
 
 | |
| 248 for (var i = 0; i < cookieInfo[type].length; ++i) { | |
| 249 // Iterate through the keys found in |cookieInfo| for the given |type| | |
| 250 // and see if those keys are present in the data. If so, display them | |
| 251 // (in the order determined by |cookieInfo|). | |
| 252 var key = item.data_[cookieInfo[type][i][0]]; | |
| 
 
michaelpg
2016/07/05 18:01:11
this is the value, not the key (as evidenced below
 
Finnur
2016/07/06 09:57:35
Fair enough.
 
 | |
| 253 if (key.length > 0) { | |
| 254 var label = loadTimeData.getString(cookieInfo[type][i][1]); | |
| 255 var value = item.data_[cookieInfo[type][i][0]]; | |
| 256 | |
| 257 var header = document.createElement('div'); | |
| 258 header.appendChild(document.createTextNode(label)); | |
| 259 var content = document.createElement('div'); | |
| 260 content.appendChild(document.createTextNode(value)); | |
| 261 root.appendChild(header); | |
| 262 root.appendChild(content); | |
| 263 } | |
| 264 } | |
| 265 }, | |
| 266 }; | |
| 267 | |
| 268 return { | |
| 269 CookieTreeNode: CookieTreeNode, | |
| 270 }; | |
| 271 }); | |
| OLD | NEW |