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.
5 cr.define('options', function() {
6 var Page = cr.ui.pageManager.Page;
7 var PageManager = cr.ui.pageManager.PageManager;
10 * ClearBrowserDataOverlay class
11 * Encapsulated handling of the 'Clear Browser Data' overlay page.
14 function ClearBrowserDataOverlay() {
15 Page.call(this, 'clearBrowserData',
16 loadTimeData.getString('clearBrowserDataOverlayTabTitle'),
17 'clear-browser-data-overlay');
20 cr.addSingletonGetter(ClearBrowserDataOverlay);
22 ClearBrowserDataOverlay.prototype = {
23 // Inherit ClearBrowserDataOverlay from Page.
24 __proto__: Page.prototype,
27 * Whether deleting history and downloads is allowed.
31 allowDeletingHistory_: true,
34 * Whether or not clearing browsing data is currently in progress.
38 isClearingInProgress_: false,
41 * Whether or not the WebUI handler has completed initialization.
43 * Unless this becomes true, it must be assumed that the above flags might
44 * not contain the authoritative values.
49 isInitializationComplete_: false,
52 initializePage: function() {
53 Page.prototype.initializePage.call(this);
55 var f = this.updateStateOfControls_.bind(this);
56 var types = ['browser.clear_data.browsing_history',
57 'browser.clear_data.download_history',
58 'browser.clear_data.cache',
59 'browser.clear_data.cookies',
60 'browser.clear_data.passwords',
61 'browser.clear_data.form_data',
62 'browser.clear_data.hosted_apps_data',
63 'browser.clear_data.content_licenses'];
64 types.forEach(function(type) {
65 Preferences.getInstance().addEventListener(type, f);
68 var checkboxes = document.querySelectorAll(
69 '#cbd-content-area input[type=checkbox]');
70 for (var i = 0; i < checkboxes.length; i++) {
71 checkboxes[i].onclick = f;
74 this.createStuffRemainsFooter_();
76 $('clear-browser-data-dismiss').onclick = function(event) {
77 ClearBrowserDataOverlay.dismiss();
79 $('clear-browser-data-commit').onclick = function(event) {
80 ClearBrowserDataOverlay.setClearing(true);
81 chrome.send('performClearBrowserData');
84 // For managed profiles, hide the checkboxes controlling whether or not
85 // browsing and download history should be cleared. Note that this is
86 // different than just disabling them as a result of enterprise policies.
87 if (!loadTimeData.getBoolean('showDeleteBrowsingHistoryCheckboxes')) {
88 $('delete-browsing-history-container').hidden = true;
89 $('delete-download-history-container').hidden = true;
92 this.updateStateOfControls_();
96 * Create a footer that explains that some content is not cleared by the
97 * clear browsing history dialog.
99 createStuffRemainsFooter_: function() {
100 // The localized string is of the form "Saved [content settings] and
101 // {search engines} will not be cleared and may reflect your browsing
102 // habits.". The following parses out the parts in brackts and braces and
103 // converts them into buttons whereas the remainders are represented as
106 document.querySelector('#some-stuff-remains-footer p');
107 var footerFragments =
108 loadTimeData.getString('contentSettingsAndSearchEnginesRemain')
110 for (var i = 0; i < footerFragments.length;) {
112 if (i + 2 < footerFragments.length) {
113 if (footerFragments[i] == '|' && footerFragments[i + 2] == '|') {
114 linkId = 'open-content-settings-from-clear-browsing-data';
115 } else if (footerFragments[i] == '#' &&
116 footerFragments[i + 2] == '#') {
117 linkId = 'open-search-engines-from-clear-browsing-data';
122 var link = new ActionLink;
124 link.textContent = footerFragments[i + 1];
125 footer.appendChild(link);
128 var span = document.createElement('span');
129 span.textContent = footerFragments[i];
130 footer.appendChild(span);
134 $('open-content-settings-from-clear-browsing-data').onclick =
136 PageManager.showPageByName('content');
138 $('open-search-engines-from-clear-browsing-data').onclick =
140 PageManager.showPageByName('searchEngines');
145 * Sets whether or not we are in the process of clearing data.
146 * @param {boolean} clearing Whether the browsing data is currently being
150 setClearing_: function(clearing) {
151 this.isClearingInProgress_ = clearing;
152 this.updateStateOfControls_();
156 * Sets whether deleting history and downloads is disallowed by enterprise
157 * policies. This is called on initialization and in response to a change in
158 * the corresponding preference.
159 * @param {boolean} allowed Whether to allow deleting history and downloads.
162 setAllowDeletingHistory_: function(allowed) {
163 this.allowDeletingHistory_ = allowed;
164 this.updateStateOfControls_();
168 * Called by the WebUI handler to signal that it has finished calling all
169 * initialization methods.
172 markInitializationComplete_: function() {
173 this.isInitializationComplete_ = true;
174 this.updateStateOfControls_();
178 * Updates the enabled/disabled/hidden status of all controls on the dialog.
181 updateStateOfControls_: function() {
182 // The commit button is enabled if at least one data type selected to be
183 // cleared, and if we are not already in the process of clearing.
184 // To prevent the commit button from being hazardously enabled for a very
185 // short time before setClearing() is called the first time by the native
186 // side, also disable the button if |isInitializationComplete_| is false.
188 if (this.isInitializationComplete_ && !this.isClearingInProgress_) {
189 var checkboxes = document.querySelectorAll(
190 '#cbd-content-area input[type=checkbox]');
191 for (var i = 0; i < checkboxes.length; i++) {
192 if (checkboxes[i].checked) {
198 $('clear-browser-data-commit').disabled = !enabled;
200 // The checkboxes for clearing history/downloads are enabled unless they
201 // are disallowed by policies, or we are in the process of clearing data.
202 // To prevent flickering, these, and the rest of the controls can safely
203 // be enabled for a short time before the first call to setClearing().
204 var enabled = this.allowDeletingHistory_ && !this.isClearingInProgress_;
205 $('delete-browsing-history-checkbox').disabled = !enabled;
206 $('delete-download-history-checkbox').disabled = !enabled;
207 if (!this.allowDeletingHistory_) {
208 $('delete-browsing-history-checkbox').checked = false;
209 $('delete-download-history-checkbox').checked = false;
212 // Enable everything else unless we are in the process of clearing.
213 var clearing = this.isClearingInProgress_;
214 $('delete-cache-checkbox').disabled = clearing;
215 $('delete-cookies-checkbox').disabled = clearing;
216 $('delete-passwords-checkbox').disabled = clearing;
217 $('delete-form-data-checkbox').disabled = clearing;
218 $('delete-hosted-apps-data-checkbox').disabled = clearing;
219 $('deauthorize-content-licenses-checkbox').disabled = clearing;
220 $('clear-browser-data-time-period').disabled = clearing;
221 $('cbd-throbber').style.visibility = clearing ? 'visible' : 'hidden';
222 $('clear-browser-data-dismiss').disabled = clearing;
226 * Updates the given data volume counter with a given text.
227 * @param {string} pref_name Name of the deletion preference of the counter
229 * @param {string} text The new text of the counter.
232 updateCounter_: function(pref_name, text) {
233 var counter = document.querySelector(
234 'input[pref="' + pref_name + '"] ~ .clear-browser-data-counter');
235 counter.textContent = text;
242 ClearBrowserDataOverlay.setAllowDeletingHistory = function(allowed) {
243 ClearBrowserDataOverlay.getInstance().setAllowDeletingHistory_(allowed);
246 ClearBrowserDataOverlay.updateCounter = function(pref_name, text) {
247 ClearBrowserDataOverlay.getInstance().updateCounter_(pref_name, text);
250 ClearBrowserDataOverlay.setClearing = function(clearing) {
251 ClearBrowserDataOverlay.getInstance().setClearing_(clearing);
254 ClearBrowserDataOverlay.markInitializationComplete = function() {
255 ClearBrowserDataOverlay.getInstance().markInitializationComplete_();
258 ClearBrowserDataOverlay.setBannerText = function(text) {
259 $('clear-browser-data-info-banner').innerText = text;
262 ClearBrowserDataOverlay.doneClearing = function() {
263 // The delay gives the user some feedback that the clearing
264 // actually worked. Otherwise the dialog just vanishes instantly in most
266 window.setTimeout(function() {
267 ClearBrowserDataOverlay.setClearing(false);
268 ClearBrowserDataOverlay.dismiss();
272 ClearBrowserDataOverlay.dismiss = function() {
273 var topmostVisiblePage = PageManager.getTopmostVisiblePage();
274 if (topmostVisiblePage && topmostVisiblePage.name == 'clearBrowserData')
275 PageManager.closeOverlay();
280 ClearBrowserDataOverlay: ClearBrowserDataOverlay