| 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 cr.define('settings', function() { | 
 |   29   'use strict'; | 
 |   30  | 
 |   31   /** | 
 |   32    * @constructor | 
 |   33    */ | 
 |   34   function CookieTreeNode(data) { | 
 |   35     /** | 
 |   36      * The data for this cookie node. | 
 |   37      * @private {CookieDetails} | 
 |   38      */ | 
 |   39     this.data_ = data; | 
 |   40  | 
 |   41     /** | 
 |   42      * The child cookie nodes. | 
 |   43      * @private {!Array<!settings.CookieTreeNode>} | 
 |   44      */ | 
 |   45     this.children_ = []; | 
 |   46   }; | 
 |   47  | 
 |   48   CookieTreeNode.prototype = { | 
 |   49     /** | 
 |   50      * Converts a list of cookies and add them as CookieTreeNode children to | 
 |   51      * the given parent node. | 
 |   52      * @param {!settings.CookieTreeNode} parentNode The parent node to add | 
 |   53      *     children to. | 
 |   54      * @param {!Array<!CookieDetails>} newNodes The list containing the data to | 
 |   55      *     add. | 
 |   56      */ | 
 |   57     addChildNodes: function(parentNode, newNodes) { | 
 |   58       var nodes = newNodes.map(function(x) { | 
 |   59         return new settings.CookieTreeNode(x); | 
 |   60       }); | 
 |   61       parentNode.children_ = nodes; | 
 |   62     }, | 
 |   63  | 
 |   64     /** | 
 |   65      * Looks up a parent node and adds a list of CookieTreeNodes to them. | 
 |   66      * @param {string} parentId The ID of the parent to add the nodes to. | 
 |   67      * @param {!settings.CookieTreeNode} startingNode The node to start with | 
 |   68      *     when looking for the parent node to add the children to. | 
 |   69      * @param {!Array<!CookieDetails>} newNodes The list containing the data to | 
 |   70          add. | 
 |   71      * @return {boolean} True if the parent node was found. | 
 |   72      */ | 
 |   73     populateChildNodes: function(parentId, startingNode, newNodes) { | 
 |   74       for (var i = 0; i < startingNode.children_.length; ++i) { | 
 |   75         if (startingNode.children_[i].data_.id == parentId) { | 
 |   76           this.addChildNodes(startingNode.children_[i], newNodes); | 
 |   77           return true; | 
 |   78         } | 
 |   79  | 
 |   80         if (this.populateChildNodes( | 
 |   81             parentId, startingNode.children_[i], newNodes)) { | 
 |   82           return true; | 
 |   83         } | 
 |   84       } | 
 |   85       return false; | 
 |   86     }, | 
 |   87  | 
 |   88     /** | 
 |   89      * Removes child nodes from a node with a given id. | 
 |   90      * @param {string} id The id of the parent node to delete from. | 
 |   91      * @param {number} firstChild The index of the first child to start deleting | 
 |   92      *     from. | 
 |   93      * @param {number} count The number of children to delete. | 
 |   94      */ | 
 |   95     removeByParentId: function(id, firstChild, count) { | 
 |   96       var node = id == null ? this : this.fetchNodeById(id, true); | 
 |   97       node.children_.splice(firstChild, count); | 
 |   98     }, | 
 |   99  | 
 |  100     /** | 
 |  101      * Returns an array of cookies from the current node within the cookie tree. | 
 |  102      * @return {!Array<!CookieDataItem>} The Cookie List. | 
 |  103      */ | 
 |  104     getCookieList: function() { | 
 |  105       var list = []; | 
 |  106  | 
 |  107       for (var group of this.children_) { | 
 |  108         for (var cookie of group.children_) { | 
 |  109           list.push({title: cookie.data_.title, | 
 |  110                      id: cookie.data_.id, | 
 |  111                      data: cookie.data_}); | 
 |  112         } | 
 |  113       } | 
 |  114  | 
 |  115       return list; | 
 |  116     }, | 
 |  117  | 
 |  118     /** | 
 |  119      * Get a summary list of all sites and their stored data. | 
 |  120      * @return {!Array<!CookieDataSummaryItem>} The summary list. | 
 |  121      */ | 
 |  122     getSummaryList: function() { | 
 |  123       var list = []; | 
 |  124       for (var i = 0; i < this.children_.length; ++i) { | 
 |  125         var siteEntry = this.children_[i]; | 
 |  126         var title = siteEntry.data_.title; | 
 |  127         var id = siteEntry.data_.id; | 
 |  128         var description = ''; | 
 |  129  | 
 |  130         for (var j = 0; j < siteEntry.children_.length; ++j) { | 
 |  131           var descriptionNode = siteEntry.children_[j]; | 
 |  132           if (j > 0) | 
 |  133             description += ', '; | 
 |  134  | 
 |  135           // Some types, like quota, have no description nodes. | 
 |  136           var dataType = ''; | 
 |  137           if (descriptionNode.data_.type != undefined) | 
 |  138             dataType = descriptionNode.data_.type; | 
 |  139           else | 
 |  140             dataType = descriptionNode.children_[0].data_.type; | 
 |  141  | 
 |  142           var category = ''; | 
 |  143           if (dataType == 'cookie') { | 
 |  144             var cookieCount = descriptionNode.children_.length; | 
 |  145             if (cookieCount > 1) | 
 |  146               category = loadTimeData.getStringF('cookiePlural', cookieCount); | 
 |  147             else | 
 |  148               category = loadTimeData.getString('cookieSingular'); | 
 |  149           } else if (dataType == 'database') { | 
 |  150             category = loadTimeData.getString('cookieDatabaseStorage'); | 
 |  151           } else if (dataType == 'local_storage' || dataType == 'indexed_db') { | 
 |  152             category = loadTimeData.getString('cookieLocalStorage'); | 
 |  153           } else if (dataType == 'app_cache') { | 
 |  154             category = loadTimeData.getString('cookieAppCache'); | 
 |  155           } else if (dataType == 'file_system') { | 
 |  156             category = loadTimeData.getString('cookieFileSystem'); | 
 |  157           } else if (dataType == 'quota') { | 
 |  158             category = descriptionNode.data_.totalUsage; | 
 |  159           } else if (dataType == 'channel_id') { | 
 |  160             category = loadTimeData.getString('cookieChannelId'); | 
 |  161           } else if (dataType == 'service_worker') { | 
 |  162             category = loadTimeData.getString('cookieServiceWorker'); | 
 |  163           } else if (dataType == 'cache_storage') { | 
 |  164             category = loadTimeData.getString('cookieCacheStorage'); | 
 |  165           } else if (dataType == 'flash_lso') { | 
 |  166             category = loadTimeData.getString('cookieFlashLso'); | 
 |  167           } | 
 |  168  | 
 |  169           description += category; | 
 |  170         } | 
 |  171         list.push({ site: title, id: id, localData: description }); | 
 |  172       } | 
 |  173       list.sort(function(a, b) { | 
 |  174         return a.site.localeCompare(b.site); | 
 |  175       }); | 
 |  176       return list; | 
 |  177     }, | 
 |  178  | 
 |  179     /** | 
 |  180      * Fetch a CookieTreeNode by ID. | 
 |  181      * @param {string} id The ID to look up. | 
 |  182      * @param {boolean} recursive Whether to search the children also. | 
 |  183      * @return {settings.CookieTreeNode} The node found, if any. | 
 |  184      */ | 
 |  185     fetchNodeById: function(id, recursive) { | 
 |  186       for (var i = 0; i < this.children_.length; ++i) { | 
 |  187         if (this.children_[i] == null) | 
 |  188           return null; | 
 |  189         if (this.children_[i].data_.id == id) | 
 |  190           return this.children_[i]; | 
 |  191         if (recursive) { | 
 |  192           var node = this.children_[i].fetchNodeById(id, true); | 
 |  193           if (node != null) | 
 |  194             return node; | 
 |  195         } | 
 |  196       } | 
 |  197       return null; | 
 |  198     }, | 
 |  199  | 
 |  200     /** | 
 |  201      * Add cookie data to a given HTML node. | 
 |  202      * @param {HTMLElement} root The node to add the data to. | 
 |  203      * @param {!settings.CookieTreeNode} item The data to add. | 
 |  204      */ | 
 |  205     addCookieData: function(root, item) { | 
 |  206       var fields = cookieInfo[item.data_.type]; | 
 |  207       for (var field of fields) { | 
 |  208         // Iterate through the keys found in |cookieInfo| for the given |type| | 
 |  209         // and see if those keys are present in the data. If so, display them | 
 |  210         // (in the order determined by |cookieInfo|). | 
 |  211         var key = field[0]; | 
 |  212         if (item.data_[key].length > 0) { | 
 |  213           var label = loadTimeData.getString(field[1]); | 
 |  214  | 
 |  215           var header = document.createElement('div'); | 
 |  216           header.appendChild(document.createTextNode(label)); | 
 |  217           var content = document.createElement('div'); | 
 |  218           content.appendChild(document.createTextNode(item.data_[key])); | 
 |  219           root.appendChild(header); | 
 |  220           root.appendChild(content); | 
 |  221         } | 
 |  222       } | 
 |  223     }, | 
 |  224   }; | 
 |  225  | 
 |  226   return { | 
 |  227     CookieTreeNode: CookieTreeNode, | 
 |  228   }; | 
 |  229 }); | 
| OLD | NEW |