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.
6 * This class wraps the popup's form, and performs the proper clearing of data
7 * based on the user's selections. It depends on the form containing a single
8 * select element with an id of 'timeframe', and a single button with an id of
9 * 'button'. When you write actual code you should probably be a little more
10 * accepting of variance, but this is just a sample app. :)
12 * Most of this is boilerplate binding the controller to the UI. The bits that
13 * specifically will be useful when using the BrowsingData API are contained in
14 * `parseMilliseconds_`, `handleCallback_`, and `handleClick_`.
18 var PopupController = function () {
19 this.button_ = document.getElementById('button');
20 this.timeframe_ = document.getElementById('timeframe');
24 PopupController.prototype = {
26 * A cached reference to the button element.
34 * A cached reference to the select element.
42 * Adds event listeners to the button in order to capture a user's click, and
43 * perform some action in response.
47 addListeners_: function () {
48 this.button_.addEventListener('click', this.handleClick_.bind(this));
52 * Given a string, return milliseconds since epoch. If the string isn't
53 * valid, returns undefined.
55 * @param {string} timeframe One of 'hour', 'day', 'week', '4weeks', or
57 * @returns {number} Milliseconds since epoch.
60 parseMilliseconds_: function (timeframe) {
61 var now = new Date().getTime();
63 'hour': 60 * 60 * 1000,
64 'day': 24 * 60 * 60 * 1000,
65 'week': 7 * 24 * 60 * 60 * 1000,
66 '4weeks': 4 * 7 * 24 * 60 * 60 * 1000
69 if (milliseconds[timeframe])
70 return now - milliseconds[timeframe];
72 if (timeframe === 'forever')
79 * Handle a success/failure callback from the `browsingData` API methods,
80 * updating the UI appropriately.
84 handleCallback_: function () {
85 var success = document.createElement('div');
86 success.classList.add('overlay');
87 success.setAttribute('role', 'alert');
88 success.textContent = 'Data has been cleared.';
89 document.body.appendChild(success);
91 setTimeout(function() { success.classList.add('visible'); }, 10);
92 setTimeout(function() {
94 success.classList.remove('visible');
101 * When a user clicks the button, this method is called: it reads the current
102 * state of `timeframe_` in order to pull a timeframe, then calls the clearing
103 * method with appropriate arguments.
107 handleClick_: function () {
108 var removal_start = this.parseMilliseconds_(this.timeframe_.value);
109 if (removal_start !== undefined) {
110 this.button_.setAttribute('disabled', 'disabled');
111 this.button_.innerText = 'Clearing...';
112 chrome.browsingData.remove({ "since" : removal_start }, {
121 "localStorage": true,
122 "serverBoundCertificates": true,
126 }, this.handleCallback_.bind(this));
131 document.addEventListener('DOMContentLoaded', function () {
132 window.PC = new PopupController();