1// Copyright (c) 2013 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
5cr.define('indexeddb', function() {
6  'use strict';
7
8  function initialize() {
9    chrome.send('getAllOrigins');
10  }
11
12  function progressNodeFor(link) {
13    return link.parentNode.querySelector('.download-status');
14  }
15
16  function downloadOriginData(event) {
17    var link = event.target;
18    progressNodeFor(link).style.display = 'inline';
19    chrome.send('downloadOriginData', [link.idb_partition_path,
20                                       link.idb_origin_url]);
21    return false;
22  }
23
24  function forceClose(event) {
25    var link = event.target;
26    progressNodeFor(link).style.display = 'inline';
27    chrome.send('forceClose', [link.idb_partition_path,
28                               link.idb_origin_url]);
29    return false;
30  }
31
32  function withNode(selector, partition_path, origin_url, callback) {
33    var links = document.querySelectorAll(selector);
34    for (var i = 0; i < links.length; ++i) {
35      var link = links[i];
36      if (partition_path == link.idb_partition_path &&
37          origin_url == link.idb_origin_url) {
38        callback(link);
39      }
40    }
41  }
42  // Fired from the backend after the data has been zipped up, and the
43  // download manager has begun downloading the file.
44  function onOriginDownloadReady(partition_path, origin_url, connection_count) {
45    withNode('a.download', partition_path, origin_url, function(link) {
46      progressNodeFor(link).style.display = 'none';
47    });
48    withNode('.connection-count', partition_path, origin_url, function(span) {
49      span.innerText = connection_count;
50    });
51  }
52
53  function onForcedClose(partition_path, origin_url, connection_count) {
54    withNode('a.force-close', partition_path, origin_url, function(link) {
55      progressNodeFor(link).style.display = 'none';
56    });
57    withNode('.connection-count', partition_path, origin_url, function(span) {
58      span.innerText = connection_count;
59    });
60  }
61
62  // Fired from the backend with a single partition's worth of
63  // IndexedDB metadata.
64  function onOriginsReady(origins, partition_path) {
65    var template = jstGetTemplate('indexeddb-list-template');
66    var container = $('indexeddb-list');
67    container.appendChild(template);
68    jstProcess(new JsEvalContext({ idbs: origins,
69                                   partition_path: partition_path}), template);
70
71    var downloadLinks = container.querySelectorAll('a.download');
72    for (var i = 0; i < downloadLinks.length; ++i) {
73      downloadLinks[i].addEventListener('click', downloadOriginData, false);
74    }
75    var forceCloseLinks = container.querySelectorAll('a.force-close');
76    for (i = 0; i < forceCloseLinks.length; ++i) {
77      forceCloseLinks[i].addEventListener('click', forceClose, false);
78    }
79  }
80
81  return {
82    initialize: initialize,
83    onForcedClose: onForcedClose,
84    onOriginDownloadReady: onOriginDownloadReady,
85    onOriginsReady: onOriginsReady,
86  };
87});
88
89document.addEventListener('DOMContentLoaded', indexeddb.initialize);
90