OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 cr.define('options', function() { | |
6 /** @const */ var DeletableItemList = options.DeletableItemList; | |
7 /** @const */ var DeletableItem = options.DeletableItem; | |
8 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; | |
9 /** @const */ var ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; | |
10 | |
11 // This structure maps the various cookie type names from C++ (hence the | |
12 // underscores) to arrays of the different types of data each has, along with | |
13 // the i18n name for the description of that data type. | |
14 /** @const */ var cookieInfo = { | |
15 'cookie': [['name', 'label_cookie_name'], | |
16 ['content', 'label_cookie_content'], | |
17 ['domain', 'label_cookie_domain'], | |
18 ['path', 'label_cookie_path'], | |
19 ['sendfor', 'label_cookie_send_for'], | |
20 ['accessibleToScript', 'label_cookie_accessible_to_script'], | |
21 ['created', 'label_cookie_created'], | |
22 ['expires', 'label_cookie_expires']], | |
23 'app_cache': [['manifest', 'label_app_cache_manifest'], | |
24 ['size', 'label_local_storage_size'], | |
25 ['created', 'label_cookie_created'], | |
26 ['accessed', 'label_cookie_last_accessed']], | |
27 'database': [['name', 'label_cookie_name'], | |
28 ['desc', 'label_webdb_desc'], | |
29 ['size', 'label_local_storage_size'], | |
30 ['modified', 'label_local_storage_last_modified']], | |
31 'local_storage': [['origin', 'label_local_storage_origin'], | |
32 ['size', 'label_local_storage_size'], | |
33 ['modified', 'label_local_storage_last_modified']], | |
34 'indexed_db': [['origin', 'label_indexed_db_origin'], | |
35 ['size', 'label_indexed_db_size'], | |
36 ['modified', 'label_indexed_db_last_modified']], | |
37 'file_system': [['origin', 'label_file_system_origin'], | |
38 ['persistent', 'label_file_system_persistent_usage'], | |
39 ['temporary', 'label_file_system_temporary_usage']], | |
40 'server_bound_cert': [['serverId', 'label_server_bound_cert_server_id'], | |
41 ['certType', 'label_server_bound_cert_type'], | |
42 ['created', 'label_server_bound_cert_created'], | |
43 ['expires', 'label_server_bound_cert_expires']], | |
44 'flash_lso': [['domain', 'label_cookie_domain']], | |
45 }; | |
46 | |
47 /** | |
48 * Returns the item's height, like offsetHeight but such that it works better | |
49 * when the page is zoomed. See the similar calculation in @{code cr.ui.List}. | |
50 * This version also accounts for the animation done in this file. | |
51 * @param {Element} item The item to get the height of. | |
52 * @return {number} The height of the item, calculated with zooming in mind. | |
53 */ | |
54 function getItemHeight(item) { | |
55 var height = item.style.height; | |
56 // Use the fixed animation target height if set, in case the element is | |
57 // currently being animated and we'd get an intermediate height below. | |
58 if (height && height.substr(-2) == 'px') | |
59 return parseInt(height.substr(0, height.length - 2)); | |
60 return item.getBoundingClientRect().height; | |
61 } | |
62 | |
63 /** | |
64 * Create tree nodes for the objects in the data array, and insert them all | |
65 * into the given list using its @{code splice} method at the given index. | |
66 * @param {Array.<Object>} data The data objects for the nodes to add. | |
67 * @param {number} start The index at which to start inserting the nodes. | |
68 * @return {Array.<CookieTreeNode>} An array of CookieTreeNodes added. | |
69 */ | |
70 function spliceTreeNodes(data, start, list) { | |
71 var nodes = data.map(function(x) { return new CookieTreeNode(x); }); | |
72 // Insert [start, 0] at the beginning of the array of nodes, making it | |
73 // into the arguments we want to pass to @{code list.splice} below. | |
74 nodes.splice(0, 0, start, 0); | |
75 list.splice.apply(list, nodes); | |
76 // Remove the [start, 0] prefix and return the array of nodes. | |
77 nodes.splice(0, 2); | |
78 return nodes; | |
79 } | |
80 | |
81 /** | |
82 * Adds information about an app that protects this data item to the | |
83 * @{code element}. | |
84 * @param {Element} element The DOM element the information should be | |
85 appended to. | |
86 * @param {{id: string, name: string}} appInfo Information about an app. | |
87 */ | |
88 function addAppInfo(element, appInfo) { | |
89 var img = element.ownerDocument.createElement('img'); | |
90 img.src = 'chrome://extension-icon/' + appInfo.id + '/16/1'; | |
91 img.title = loadTimeData.getString('label_protected_by_apps') + | |
92 ' ' + appInfo.name; | |
93 img.className = 'protecting-app'; | |
94 element.appendChild(img); | |
95 } | |
96 | |
97 var parentLookup = {}; | |
98 var lookupRequests = {}; | |
99 | |
100 /** | |
101 * Creates a new list item for sites data. Note that these are created and | |
102 * destroyed lazily as they scroll into and out of view, so they must be | |
103 * stateless. We cache the expanded item in @{code CookiesList} though, so it | |
104 * can keep state. (Mostly just which item is selected.) | |
105 * @param {Object} origin Data used to create a cookie list item. | |
106 * @param {CookiesList} list The list that will contain this item. | |
107 * @constructor | |
108 * @extends {DeletableItem} | |
109 */ | |
110 function CookieListItem(origin, list) { | |
111 var listItem = new DeletableItem(null); | |
112 listItem.__proto__ = CookieListItem.prototype; | |
113 | |
114 listItem.origin = origin; | |
115 listItem.list = list; | |
116 listItem.decorate(); | |
117 | |
118 // This hooks up updateOrigin() to the list item, makes the top-level | |
119 // tree nodes (i.e., origins) register their IDs in parentLookup, and | |
120 // causes them to request their children if they have none. Note that we | |
121 // have special logic in the setter for the parent property to make sure | |
122 // that we can still garbage collect list items when they scroll out of | |
123 // view, even though it appears that we keep a direct reference. | |
124 if (origin) { | |
125 origin.parent = listItem; | |
126 origin.updateOrigin(); | |
127 } | |
128 | |
129 return listItem; | |
130 } | |
131 | |
132 CookieListItem.prototype = { | |
133 __proto__: DeletableItem.prototype, | |
134 | |
135 /** @inheritDoc */ | |
136 decorate: function() { | |
137 this.siteChild = this.ownerDocument.createElement('div'); | |
138 this.siteChild.className = 'cookie-site'; | |
139 this.dataChild = this.ownerDocument.createElement('div'); | |
140 this.dataChild.className = 'cookie-data'; | |
141 this.sizeChild = this.ownerDocument.createElement('div'); | |
142 this.sizeChild.className = 'cookie-size'; | |
143 this.itemsChild = this.ownerDocument.createElement('div'); | |
144 this.itemsChild.className = 'cookie-items'; | |
145 this.infoChild = this.ownerDocument.createElement('div'); | |
146 this.infoChild.className = 'cookie-details'; | |
147 this.infoChild.hidden = true; | |
148 | |
149 if (this.origin.data.appId) { | |
150 this.siteChild.classList.add('app-cookie-site'); | |
151 this.itemsChild.classList.add('app-cookie-items'); | |
152 } | |
153 | |
154 var remove = this.ownerDocument.createElement('button'); | |
155 remove.textContent = loadTimeData.getString('remove_cookie'); | |
156 remove.onclick = this.removeCookie_.bind(this); | |
157 this.infoChild.appendChild(remove); | |
158 var content = this.contentElement; | |
159 content.appendChild(this.siteChild); | |
160 content.appendChild(this.dataChild); | |
161 content.appendChild(this.sizeChild); | |
162 content.appendChild(this.itemsChild); | |
163 this.itemsChild.appendChild(this.infoChild); | |
164 if (this.origin && this.origin.data) { | |
165 this.siteChild.textContent = this.origin.data.title; | |
166 this.siteChild.setAttribute('title', this.origin.data.title); | |
167 } | |
168 this.itemList_ = []; | |
169 }, | |
170 | |
171 /** @type {boolean} */ | |
172 get expanded() { | |
173 return this.expanded_; | |
174 }, | |
175 set expanded(expanded) { | |
176 if (this.expanded_ == expanded) | |
177 return; | |
178 this.expanded_ = expanded; | |
179 if (expanded) { | |
180 var oldExpanded = this.list.expandedItem; | |
181 this.list.expandedItem = this; | |
182 this.updateItems_(); | |
183 if (oldExpanded) | |
184 oldExpanded.expanded = false; | |
185 this.classList.add('show-items'); | |
186 } else { | |
187 if (this.list.expandedItem == this) { | |
188 this.list.expandedItem = null; | |
189 } | |
190 this.style.height = ''; | |
191 this.itemsChild.style.height = ''; | |
192 this.classList.remove('show-items'); | |
193 } | |
194 }, | |
195 | |
196 /** | |
197 * The callback for the "remove" button shown when an item is selected. | |
198 * Requests that the currently selected cookie be removed. | |
199 * @private | |
200 */ | |
201 removeCookie_: function() { | |
202 if (this.selectedIndex_ >= 0) { | |
203 var item = this.itemList_[this.selectedIndex_]; | |
204 if (item && item.node) | |
205 chrome.send('removeCookie', [item.node.pathId]); | |
206 } | |
207 }, | |
208 | |
209 /** | |
210 * Disable animation within this cookie list item, in preparation for making | |
211 * changes that will need to be animated. Makes it possible to measure the | |
212 * contents without displaying them, to set animation targets. | |
213 * @private | |
214 */ | |
215 disableAnimation_: function() { | |
216 this.itemsHeight_ = getItemHeight(this.itemsChild); | |
217 this.classList.add('measure-items'); | |
218 }, | |
219 | |
220 /** | |
221 * Enable animation after changing the contents of this cookie list item. | |
222 * See @{code disableAnimation_}. | |
223 * @private | |
224 */ | |
225 enableAnimation_: function() { | |
226 if (!this.classList.contains('measure-items')) | |
227 this.disableAnimation_(); | |
228 this.itemsChild.style.height = ''; | |
229 // This will force relayout in order to calculate the new heights. | |
230 var itemsHeight = getItemHeight(this.itemsChild); | |
231 var fixedHeight = getItemHeight(this) + itemsHeight - this.itemsHeight_; | |
232 this.itemsChild.style.height = this.itemsHeight_ + 'px'; | |
233 // Force relayout before enabling animation, so that if we have | |
234 // changed things since the last layout, they will not be animated | |
235 // during subsequent layouts. | |
236 this.itemsChild.offsetHeight; | |
237 this.classList.remove('measure-items'); | |
238 this.itemsChild.style.height = itemsHeight + 'px'; | |
239 this.style.height = fixedHeight + 'px'; | |
240 }, | |
241 | |
242 /** | |
243 * Updates the origin summary to reflect changes in its items. | |
244 * Both CookieListItem and CookieTreeNode implement this API. | |
245 * This implementation scans the descendants to update the text. | |
246 */ | |
247 updateOrigin: function() { | |
248 var info = { | |
249 cookies: 0, | |
250 database: false, | |
251 localStorage: false, | |
252 appCache: false, | |
253 indexedDb: false, | |
254 fileSystem: false, | |
255 serverBoundCerts: 0, | |
256 }; | |
257 if (this.origin) | |
258 this.origin.collectSummaryInfo(info); | |
259 | |
260 var list = []; | |
261 if (info.cookies > 1) | |
262 list.push(loadTimeData.getStringF('cookie_plural', info.cookies)); | |
263 else if (info.cookies > 0) | |
264 list.push(loadTimeData.getString('cookie_singular')); | |
265 if (info.database || info.indexedDb) | |
266 list.push(loadTimeData.getString('cookie_database_storage')); | |
267 if (info.localStorage) | |
268 list.push(loadTimeData.getString('cookie_local_storage')); | |
269 if (info.appCache) | |
270 list.push(loadTimeData.getString('cookie_app_cache')); | |
271 if (info.fileSystem) | |
272 list.push(loadTimeData.getString('cookie_file_system')); | |
273 if (info.serverBoundCerts) | |
274 list.push(loadTimeData.getString('cookie_server_bound_cert')); | |
275 if (info.flashLSO) | |
276 list.push(loadTimeData.getString('cookie_flash_lso')); | |
277 | |
278 var text = ''; | |
279 for (var i = 0; i < list.length; ++i) { | |
280 if (text.length > 0) | |
281 text += ', ' + list[i]; | |
282 else | |
283 text = list[i]; | |
284 } | |
285 this.dataChild.textContent = text; | |
286 | |
287 for (var key in info.appsProtectingThis) { | |
288 addAppInfo(this.dataChild, apps[key]); | |
289 } | |
290 | |
291 if (info.quota && info.quota.totalUsage) | |
292 this.sizeChild.textContent = info.quota.totalUsage; | |
293 | |
294 if (this.expanded) | |
295 this.updateItems_(); | |
296 }, | |
297 | |
298 /** | |
299 * Updates the items section to reflect changes, animating to the new state. | |
300 * Removes existing contents and calls @{code CookieTreeNode.createItems}. | |
301 * @private | |
302 */ | |
303 updateItems_: function() { | |
304 this.disableAnimation_(); | |
305 this.itemsChild.textContent = ''; | |
306 this.infoChild.hidden = true; | |
307 this.selectedIndex_ = -1; | |
308 this.itemList_ = []; | |
309 if (this.origin) | |
310 this.origin.createItems(this); | |
311 this.itemsChild.appendChild(this.infoChild); | |
312 this.enableAnimation_(); | |
313 }, | |
314 | |
315 /** | |
316 * Append a new cookie node "bubble" to this list item. | |
317 * @param {CookieTreeNode} node The cookie node to add a bubble for. | |
318 * @param {Element} div The DOM element for the bubble itself. | |
319 * @return {number} The index the bubble was added at. | |
320 */ | |
321 appendItem: function(node, div) { | |
322 this.itemList_.push({node: node, div: div}); | |
323 this.itemsChild.appendChild(div); | |
324 return this.itemList_.length - 1; | |
325 }, | |
326 | |
327 /** | |
328 * The currently selected cookie node ("cookie bubble") index. | |
329 * @type {number} | |
330 * @private | |
331 */ | |
332 selectedIndex_: -1, | |
333 | |
334 /** | |
335 * Get the currently selected cookie node ("cookie bubble") index. | |
336 * @type {number} | |
337 */ | |
338 get selectedIndex() { | |
339 return this.selectedIndex_; | |
340 }, | |
341 | |
342 /** | |
343 * Set the currently selected cookie node ("cookie bubble") index to | |
344 * @{code itemIndex}, unselecting any previously selected node first. | |
345 * @param {number} itemIndex The index to set as the selected index. | |
346 */ | |
347 set selectedIndex(itemIndex) { | |
348 // Get the list index up front before we change anything. | |
349 var index = this.list.getIndexOfListItem(this); | |
350 // Unselect any previously selected item. | |
351 if (this.selectedIndex_ >= 0) { | |
352 var item = this.itemList_[this.selectedIndex_]; | |
353 if (item && item.div) | |
354 item.div.removeAttribute('selected'); | |
355 } | |
356 // Special case: decrementing -1 wraps around to the end of the list. | |
357 if (itemIndex == -2) | |
358 itemIndex = this.itemList_.length - 1; | |
359 // Check if we're going out of bounds and hide the item details. | |
360 if (itemIndex < 0 || itemIndex >= this.itemList_.length) { | |
361 this.selectedIndex_ = -1; | |
362 this.disableAnimation_(); | |
363 this.infoChild.hidden = true; | |
364 this.enableAnimation_(); | |
365 return; | |
366 } | |
367 // Set the new selected item and show the item details for it. | |
368 this.selectedIndex_ = itemIndex; | |
369 this.itemList_[itemIndex].div.setAttribute('selected', ''); | |
370 this.disableAnimation_(); | |
371 this.itemList_[itemIndex].node.setDetailText(this.infoChild, | |
372 this.list.infoNodes); | |
373 this.infoChild.hidden = false; | |
374 this.enableAnimation_(); | |
375 // If we're near the bottom of the list this may cause the list item to go | |
376 // beyond the end of the visible area. Fix it after the animation is done. | |
377 var list = this.list; | |
378 window.setTimeout(function() { list.scrollIndexIntoView(index); }, 150); | |
379 }, | |
380 }; | |
381 | |
382 /** | |
383 * {@code CookieTreeNode}s mirror the structure of the cookie tree lazily, and | |
384 * contain all the actual data used to generate the {@code CookieListItem}s. | |
385 * @param {Object} data The data object for this node. | |
386 * @constructor | |
387 */ | |
388 function CookieTreeNode(data) { | |
389 this.data = data; | |
390 this.children = []; | |
391 } | |
392 | |
393 CookieTreeNode.prototype = { | |
394 /** | |
395 * Insert the given list of cookie tree nodes at the given index. | |
396 * Both CookiesList and CookieTreeNode implement this API. | |
397 * @param {Array.<Object>} data The data objects for the nodes to add. | |
398 * @param {number} start The index at which to start inserting the nodes. | |
399 */ | |
400 insertAt: function(data, start) { | |
401 var nodes = spliceTreeNodes(data, start, this.children); | |
402 for (var i = 0; i < nodes.length; i++) | |
403 nodes[i].parent = this; | |
404 this.updateOrigin(); | |
405 }, | |
406 | |
407 /** | |
408 * Remove a cookie tree node from the given index. | |
409 * Both CookiesList and CookieTreeNode implement this API. | |
410 * @param {number} index The index of the tree node to remove. | |
411 */ | |
412 remove: function(index) { | |
413 if (index < this.children.length) { | |
414 this.children.splice(index, 1); | |
415 this.updateOrigin(); | |
416 } | |
417 }, | |
418 | |
419 /** | |
420 * Clears all children. | |
421 * Both CookiesList and CookieTreeNode implement this API. | |
422 * It is used by CookiesList.loadChildren(). | |
423 */ | |
424 clear: function() { | |
425 // We might leave some garbage in parentLookup for removed children. | |
426 // But that should be OK because parentLookup is cleared when we | |
427 // reload the tree. | |
428 this.children = []; | |
429 this.updateOrigin(); | |
430 }, | |
431 | |
432 /** | |
433 * The counter used by startBatchUpdates() and endBatchUpdates(). | |
434 * @type {number} | |
435 */ | |
436 batchCount_: 0, | |
437 | |
438 /** | |
439 * See cr.ui.List.startBatchUpdates(). | |
440 * Both CookiesList (via List) and CookieTreeNode implement this API. | |
441 */ | |
442 startBatchUpdates: function() { | |
443 this.batchCount_++; | |
444 }, | |
445 | |
446 /** | |
447 * See cr.ui.List.endBatchUpdates(). | |
448 * Both CookiesList (via List) and CookieTreeNode implement this API. | |
449 */ | |
450 endBatchUpdates: function() { | |
451 if (!--this.batchCount_) | |
452 this.updateOrigin(); | |
453 }, | |
454 | |
455 /** | |
456 * Requests updating the origin summary to reflect changes in this item. | |
457 * Both CookieListItem and CookieTreeNode implement this API. | |
458 */ | |
459 updateOrigin: function() { | |
460 if (!this.batchCount_ && this.parent) | |
461 this.parent.updateOrigin(); | |
462 }, | |
463 | |
464 /** | |
465 * Summarize the information in this node and update @{code info}. | |
466 * This will recurse into child nodes to summarize all descendants. | |
467 * @param {Object} info The info object from @{code updateOrigin}. | |
468 */ | |
469 collectSummaryInfo: function(info) { | |
470 if (this.children.length > 0) { | |
471 for (var i = 0; i < this.children.length; ++i) | |
472 this.children[i].collectSummaryInfo(info); | |
473 } else if (this.data && !this.data.hasChildren) { | |
474 if (this.data.type == 'cookie') { | |
475 info.cookies++; | |
476 } else if (this.data.type == 'database') { | |
477 info.database = true; | |
478 } else if (this.data.type == 'local_storage') { | |
479 info.localStorage = true; | |
480 } else if (this.data.type == 'app_cache') { | |
481 info.appCache = true; | |
482 } else if (this.data.type == 'indexed_db') { | |
483 info.indexedDb = true; | |
484 } else if (this.data.type == 'file_system') { | |
485 info.fileSystem = true; | |
486 } else if (this.data.type == 'quota') { | |
487 info.quota = this.data; | |
488 } else if (this.data.type == 'server_bound_cert') { | |
489 info.serverBoundCerts++; | |
490 } else if (this.data.type == 'flash_lso') { | |
491 info.flashLSO = true; | |
492 } | |
493 | |
494 var apps = this.data.appsProtectingThis; | |
495 if (apps) { | |
496 if (!info.appsProtectingThis) | |
497 info.appsProtectingThis = {}; | |
498 apps.forEach(function(appInfo) { | |
499 info.appsProtectingThis[appInfo.id] = appInfo; | |
500 }); | |
501 } | |
502 } | |
503 }, | |
504 | |
505 /** | |
506 * Create the cookie "bubbles" for this node, recursing into children | |
507 * if there are any. Append the cookie bubbles to @{code item}. | |
508 * @param {CookieListItem} item The cookie list item to create items in. | |
509 */ | |
510 createItems: function(item) { | |
511 if (this.children.length > 0) { | |
512 for (var i = 0; i < this.children.length; ++i) | |
513 this.children[i].createItems(item); | |
514 return; | |
515 } | |
516 | |
517 if (!this.data || this.data.hasChildren) | |
518 return; | |
519 | |
520 var text = ''; | |
521 switch (this.data.type) { | |
522 case 'cookie': | |
523 case 'database': | |
524 text = this.data.name; | |
525 break; | |
526 default: | |
527 text = loadTimeData.getString('cookie_' + this.data.type); | |
528 } | |
529 if (!text) | |
530 return; | |
531 | |
532 var div = item.ownerDocument.createElement('div'); | |
533 div.className = 'cookie-item'; | |
534 // Help out screen readers and such: this is a clickable thing. | |
535 div.setAttribute('role', 'button'); | |
536 div.tabIndex = 0; | |
537 div.textContent = text; | |
538 var apps = this.data.appsProtectingThis; | |
539 if (apps) | |
540 apps.forEach(addAppInfo.bind(null, div)); | |
541 | |
542 var index = item.appendItem(this, div); | |
543 div.onclick = function() { | |
544 item.selectedIndex = (item.selectedIndex == index) ? -1 : index; | |
545 }; | |
546 }, | |
547 | |
548 /** | |
549 * Set the detail text to be displayed to that of this cookie tree node. | |
550 * Uses preallocated DOM elements for each cookie node type from @{code | |
551 * infoNodes}, and inserts the appropriate elements to @{code element}. | |
552 * @param {Element} element The DOM element to insert elements to. | |
553 * @param {Object.<string, {table: Element, info: Object.<string, | |
554 * Element>}>} infoNodes The map from cookie node types to maps from | |
555 * cookie attribute names to DOM elements to display cookie attribute | |
556 * values, created by @{code CookiesList.decorate}. | |
557 */ | |
558 setDetailText: function(element, infoNodes) { | |
559 var table; | |
560 if (this.data && !this.data.hasChildren && cookieInfo[this.data.type]) { | |
561 var info = cookieInfo[this.data.type]; | |
562 var nodes = infoNodes[this.data.type].info; | |
563 for (var i = 0; i < info.length; ++i) { | |
564 var name = info[i][0]; | |
565 if (name != 'id' && this.data[name]) | |
566 nodes[name].textContent = this.data[name]; | |
567 else | |
568 nodes[name].textContent = ''; | |
569 } | |
570 table = infoNodes[this.data.type].table; | |
571 } | |
572 | |
573 while (element.childNodes.length > 1) | |
574 element.removeChild(element.firstChild); | |
575 | |
576 if (table) | |
577 element.insertBefore(table, element.firstChild); | |
578 }, | |
579 | |
580 /** | |
581 * The parent of this cookie tree node. | |
582 * @type {?CookieTreeNode|CookieListItem} | |
583 */ | |
584 get parent() { | |
585 // See below for an explanation of this special case. | |
586 if (typeof this.parent_ == 'number') | |
587 return this.list_.getListItemByIndex(this.parent_); | |
588 return this.parent_; | |
589 }, | |
590 set parent(parent) { | |
591 if (parent == this.parent) | |
592 return; | |
593 | |
594 if (parent instanceof CookieListItem) { | |
595 // If the parent is to be a CookieListItem, then we keep the reference | |
596 // to it by its containing list and list index, rather than directly. | |
597 // This allows the list items to be garbage collected when they scroll | |
598 // out of view (except the expanded item, which we cache). This is | |
599 // transparent except in the setter and getter, where we handle it. | |
600 this.parent_ = parent.listIndex; | |
601 this.list_ = parent.list; | |
602 parent.addEventListener('listIndexChange', | |
603 this.parentIndexChanged_.bind(this)); | |
604 } else { | |
605 this.parent_ = parent; | |
606 } | |
607 | |
608 if (this.data && this.data.id) { | |
609 if (parent) | |
610 parentLookup[this.data.id] = this; | |
611 else | |
612 delete parentLookup[this.data.id]; | |
613 } | |
614 | |
615 if (this.data && this.data.hasChildren && | |
616 !this.children.length && !lookupRequests[this.data.id]) { | |
617 lookupRequests[this.data.id] = true; | |
618 chrome.send('loadCookie', [this.pathId]); | |
619 } | |
620 }, | |
621 | |
622 /** | |
623 * Called when the parent is a CookieListItem whose index has changed. | |
624 * See the code above that avoids keeping a direct reference to | |
625 * CookieListItem parents, to allow them to be garbage collected. | |
626 * @private | |
627 */ | |
628 parentIndexChanged_: function(event) { | |
629 if (typeof this.parent_ == 'number') { | |
630 this.parent_ = event.newValue; | |
631 // We set a timeout to update the origin, rather than doing it right | |
632 // away, because this callback may occur while the list items are | |
633 // being repopulated following a scroll event. Calling updateOrigin() | |
634 // immediately could trigger relayout that would reset the scroll | |
635 // position within the list, among other things. | |
636 window.setTimeout(this.updateOrigin.bind(this), 0); | |
637 } | |
638 }, | |
639 | |
640 /** | |
641 * The cookie tree path id. | |
642 * @type {string} | |
643 */ | |
644 get pathId() { | |
645 var parent = this.parent; | |
646 if (parent && parent instanceof CookieTreeNode) | |
647 return parent.pathId + ',' + this.data.id; | |
648 return this.data.id; | |
649 }, | |
650 }; | |
651 | |
652 /** | |
653 * Creates a new cookies list. | |
654 * @param {Object=} opt_propertyBag Optional properties. | |
655 * @constructor | |
656 * @extends {DeletableItemList} | |
657 */ | |
658 var CookiesList = cr.ui.define('list'); | |
659 | |
660 CookiesList.prototype = { | |
661 __proto__: DeletableItemList.prototype, | |
662 | |
663 /** @inheritDoc */ | |
664 decorate: function() { | |
665 DeletableItemList.prototype.decorate.call(this); | |
666 this.classList.add('cookie-list'); | |
667 this.dataModel = new ArrayDataModel([]); | |
668 this.addEventListener('keydown', this.handleKeyLeftRight_.bind(this)); | |
669 var sm = new ListSingleSelectionModel(); | |
670 sm.addEventListener('change', this.cookieSelectionChange_.bind(this)); | |
671 sm.addEventListener('leadIndexChange', this.cookieLeadChange_.bind(this)); | |
672 this.selectionModel = sm; | |
673 this.infoNodes = {}; | |
674 this.fixedHeight = false; | |
675 var doc = this.ownerDocument; | |
676 // Create a table for each type of site data (e.g. cookies, databases, | |
677 // etc.) and save it so that we can reuse it for all origins. | |
678 for (var type in cookieInfo) { | |
679 var table = doc.createElement('table'); | |
680 table.className = 'cookie-details-table'; | |
681 var tbody = doc.createElement('tbody'); | |
682 table.appendChild(tbody); | |
683 var info = {}; | |
684 for (var i = 0; i < cookieInfo[type].length; i++) { | |
685 var tr = doc.createElement('tr'); | |
686 var name = doc.createElement('td'); | |
687 var data = doc.createElement('td'); | |
688 var pair = cookieInfo[type][i]; | |
689 name.className = 'cookie-details-label'; | |
690 name.textContent = loadTimeData.getString(pair[1]); | |
691 data.className = 'cookie-details-value'; | |
692 data.textContent = ''; | |
693 tr.appendChild(name); | |
694 tr.appendChild(data); | |
695 tbody.appendChild(tr); | |
696 info[pair[0]] = data; | |
697 } | |
698 this.infoNodes[type] = {table: table, info: info}; | |
699 } | |
700 }, | |
701 | |
702 /** | |
703 * Handles key down events and looks for left and right arrows, then | |
704 * dispatches to the currently expanded item, if any. | |
705 * @param {Event} e The keydown event. | |
706 * @private | |
707 */ | |
708 handleKeyLeftRight_: function(e) { | |
709 var id = e.keyIdentifier; | |
710 if ((id == 'Left' || id == 'Right') && this.expandedItem) { | |
711 var cs = this.ownerDocument.defaultView.getComputedStyle(this); | |
712 var rtl = cs.direction == 'rtl'; | |
713 if ((!rtl && id == 'Left') || (rtl && id == 'Right')) | |
714 this.expandedItem.selectedIndex--; | |
715 else | |
716 this.expandedItem.selectedIndex++; | |
717 this.scrollIndexIntoView(this.expandedItem.listIndex); | |
718 // Prevent the page itself from scrolling. | |
719 e.preventDefault(); | |
720 } | |
721 }, | |
722 | |
723 /** | |
724 * Called on selection model selection changes. | |
725 * @param {Event} ce The selection change event. | |
726 * @private | |
727 */ | |
728 cookieSelectionChange_: function(ce) { | |
729 ce.changes.forEach(function(change) { | |
730 var listItem = this.getListItemByIndex(change.index); | |
731 if (listItem) { | |
732 if (!change.selected) { | |
733 // We set a timeout here, rather than setting the item unexpanded | |
734 // immediately, so that if another item gets set expanded right | |
735 // away, it will be expanded before this item is unexpanded. It | |
736 // will notice that, and unexpand this item in sync with its own | |
737 // expansion. Later, this callback will end up having no effect. | |
738 window.setTimeout(function() { | |
739 if (!listItem.selected || !listItem.lead) | |
740 listItem.expanded = false; | |
741 }, 0); | |
742 } else if (listItem.lead) { | |
743 listItem.expanded = true; | |
744 } | |
745 } | |
746 }, this); | |
747 }, | |
748 | |
749 /** | |
750 * Called on selection model lead changes. | |
751 * @param {Event} pe The lead change event. | |
752 * @private | |
753 */ | |
754 cookieLeadChange_: function(pe) { | |
755 if (pe.oldValue != -1) { | |
756 var listItem = this.getListItemByIndex(pe.oldValue); | |
757 if (listItem) { | |
758 // See cookieSelectionChange_ above for why we use a timeout here. | |
759 window.setTimeout(function() { | |
760 if (!listItem.lead || !listItem.selected) | |
761 listItem.expanded = false; | |
762 }, 0); | |
763 } | |
764 } | |
765 if (pe.newValue != -1) { | |
766 var listItem = this.getListItemByIndex(pe.newValue); | |
767 if (listItem && listItem.selected) | |
768 listItem.expanded = true; | |
769 } | |
770 }, | |
771 | |
772 /** | |
773 * The currently expanded item. Used by CookieListItem above. | |
774 * @type {?CookieListItem} | |
775 */ | |
776 expandedItem: null, | |
777 | |
778 // from cr.ui.List | |
779 /** @inheritDoc */ | |
780 createItem: function(data) { | |
781 // We use the cached expanded item in order to allow it to maintain some | |
782 // state (like its fixed height, and which bubble is selected). | |
783 if (this.expandedItem && this.expandedItem.origin == data) | |
784 return this.expandedItem; | |
785 return new CookieListItem(data, this); | |
786 }, | |
787 | |
788 // from options.DeletableItemList | |
789 /** @inheritDoc */ | |
790 deleteItemAtIndex: function(index) { | |
791 var item = this.dataModel.item(index); | |
792 if (item) { | |
793 var pathId = item.pathId; | |
794 if (pathId) | |
795 chrome.send('removeCookie', [pathId]); | |
796 } | |
797 }, | |
798 | |
799 /** | |
800 * Insert the given list of cookie tree nodes at the given index. | |
801 * Both CookiesList and CookieTreeNode implement this API. | |
802 * @param {Array.<Object>} data The data objects for the nodes to add. | |
803 * @param {number} start The index at which to start inserting the nodes. | |
804 */ | |
805 insertAt: function(data, start) { | |
806 spliceTreeNodes(data, start, this.dataModel); | |
807 }, | |
808 | |
809 /** | |
810 * Remove a cookie tree node from the given index. | |
811 * Both CookiesList and CookieTreeNode implement this API. | |
812 * @param {number} index The index of the tree node to remove. | |
813 */ | |
814 remove: function(index) { | |
815 if (index < this.dataModel.length) | |
816 this.dataModel.splice(index, 1); | |
817 }, | |
818 | |
819 /** | |
820 * Clears the list. | |
821 * Both CookiesList and CookieTreeNode implement this API. | |
822 * It is used by CookiesList.loadChildren(). | |
823 */ | |
824 clear: function() { | |
825 parentLookup = {}; | |
826 this.dataModel.splice(0, this.dataModel.length); | |
827 this.redraw(); | |
828 }, | |
829 | |
830 /** | |
831 * Add tree nodes by given parent. | |
832 * @param {Object} parent The parent node. | |
833 * @param {number} start The index at which to start inserting the nodes. | |
834 * @param {Array} nodesData Nodes data array. | |
835 * @private | |
836 */ | |
837 addByParent_: function(parent, start, nodesData) { | |
838 if (!parent) | |
839 return; | |
840 | |
841 parent.startBatchUpdates(); | |
842 parent.insertAt(nodesData, start); | |
843 parent.endBatchUpdates(); | |
844 | |
845 cr.dispatchSimpleEvent(this, 'change'); | |
846 }, | |
847 | |
848 /** | |
849 * Add tree nodes by parent id. | |
850 * This is used by cookies_view.js. | |
851 * @param {string} parentId Id of the parent node. | |
852 * @param {number} start The index at which to start inserting the nodes. | |
853 * @param {Array} nodesData Nodes data array. | |
854 */ | |
855 addByParentId: function(parentId, start, nodesData) { | |
856 var parent = parentId ? parentLookup[parentId] : this; | |
857 this.addByParent_(parent, start, nodesData); | |
858 }, | |
859 | |
860 /** | |
861 * Removes tree nodes by parent id. | |
862 * This is used by cookies_view.js. | |
863 * @param {string} parentId Id of the parent node. | |
864 * @param {number} start The index at which to start removing the nodes. | |
865 * @param {number} count Number of nodes to remove. | |
866 */ | |
867 removeByParentId: function(parentId, start, count) { | |
868 var parent = parentId ? parentLookup[parentId] : this; | |
869 if (!parent) | |
870 return; | |
871 | |
872 parent.startBatchUpdates(); | |
873 while (count-- > 0) | |
874 parent.remove(start); | |
875 parent.endBatchUpdates(); | |
876 | |
877 cr.dispatchSimpleEvent(this, 'change'); | |
878 }, | |
879 | |
880 /** | |
881 * Loads the immediate children of given parent node. | |
882 * This is used by cookies_view.js. | |
883 * @param {string} parentId Id of the parent node. | |
884 * @param {Array} children The immediate children of parent node. | |
885 */ | |
886 loadChildren: function(parentId, children) { | |
887 if (parentId) | |
888 delete lookupRequests[parentId]; | |
889 var parent = parentId ? parentLookup[parentId] : this; | |
890 if (!parent) | |
891 return; | |
892 | |
893 parent.startBatchUpdates(); | |
894 parent.clear(); | |
895 this.addByParent_(parent, 0, children); | |
896 parent.endBatchUpdates(); | |
897 }, | |
898 }; | |
899 | |
900 return { | |
901 CookiesList: CookiesList | |
902 }; | |
903 }); | |
OLD | NEW |