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 buttonId = 'open-content-settings-from-clear-browsing-data';
115 } else if (footerFragments[i] == '#' &&
116 footerFragments[i + 2] == '#') {
117 buttonId = 'open-search-engines-from-clear-browsing-data';
121 if (buttonId != '') {
122 var button = document.createElement('button');
123 button.id = buttonId;
124 button.className = 'link-button';
125 button.textContent = footerFragments[i + 1];
126 footer.appendChild(button);
129 var span = document.createElement('span');
130 span.textContent = footerFragments[i];
131 footer.appendChild(span);
135 $('open-content-settings-from-clear-browsing-data').onclick =
137 PageManager.showPageByName('content');
139 $('open-search-engines-from-clear-browsing-data').onclick =
141 PageManager.showPageByName('searchEngines');
146 * Sets whether or not we are in the process of clearing data.
147 * @param {boolean} clearing Whether the browsing data is currently being
151 setClearing_: function(clearing) {
152 this.isClearingInProgress_ = clearing;
153 this.updateStateOfControls_();
157 * Sets whether deleting history and downloads is disallowed by enterprise
158 * policies. This is called on initialization and in response to a change in
159 * the corresponding preference.
160 * @param {boolean} allowed Whether to allow deleting history and downloads.
163 setAllowDeletingHistory_: function(allowed) {
164 this.allowDeletingHistory_ = allowed;
165 this.updateStateOfControls_();
169 * Called by the WebUI handler to signal that it has finished calling all
170 * initialization methods.
173 markInitializationComplete_: function() {
174 this.isInitializationComplete_ = true;
175 this.updateStateOfControls_();
179 * Updates the enabled/disabled/hidden status of all controls on the dialog.
182 updateStateOfControls_: function() {
183 // The commit button is enabled if at least one data type selected to be
184 // cleared, and if we are not already in the process of clearing.
185 // To prevent the commit button from being hazardously enabled for a very
186 // short time before setClearing() is called the first time by the native
187 // side, also disable the button if |isInitializationComplete_| is false.
189 if (this.isInitializationComplete_ && !this.isClearingInProgress_) {
190 var checkboxes = document.querySelectorAll(
191 '#cbd-content-area input[type=checkbox]');
192 for (var i = 0; i < checkboxes.length; i++) {
193 if (checkboxes[i].checked) {
199 $('clear-browser-data-commit').disabled = !enabled;
201 // The checkboxes for clearing history/downloads are enabled unless they
202 // are disallowed by policies, or we are in the process of clearing data.
203 // To prevent flickering, these, and the rest of the controls can safely
204 // be enabled for a short time before the first call to setClearing().
205 var enabled = this.allowDeletingHistory_ && !this.isClearingInProgress_;
206 $('delete-browsing-history-checkbox').disabled = !enabled;
207 $('delete-download-history-checkbox').disabled = !enabled;
208 if (!this.allowDeletingHistory_) {
209 $('delete-browsing-history-checkbox').checked = false;
210 $('delete-download-history-checkbox').checked = false;
213 // Enable everything else unless we are in the process of clearing.
214 var clearing = this.isClearingInProgress_;
215 $('delete-cache-checkbox').disabled = clearing;
216 $('delete-cookies-checkbox').disabled = clearing;
217 $('delete-passwords-checkbox').disabled = clearing;
218 $('delete-form-data-checkbox').disabled = clearing;
219 $('delete-hosted-apps-data-checkbox').disabled = clearing;
220 $('deauthorize-content-licenses-checkbox').disabled = clearing;
221 $('clear-browser-data-time-period').disabled = clearing;
222 $('cbd-throbber').style.visibility = clearing ? 'visible' : 'hidden';
223 $('clear-browser-data-dismiss').disabled = clearing;
230 ClearBrowserDataOverlay.setAllowDeletingHistory = function(allowed) {
231 ClearBrowserDataOverlay.getInstance().setAllowDeletingHistory_(allowed);
234 ClearBrowserDataOverlay.setClearing = function(clearing) {
235 ClearBrowserDataOverlay.getInstance().setClearing_(clearing);
238 ClearBrowserDataOverlay.markInitializationComplete = function() {
239 ClearBrowserDataOverlay.getInstance().markInitializationComplete_();
242 ClearBrowserDataOverlay.setBannerVisibility = function(args) {
243 var visible = args[0];
244 $('clear-browser-data-info-banner').hidden = !visible;
247 ClearBrowserDataOverlay.doneClearing = function() {
248 // The delay gives the user some feedback that the clearing
249 // actually worked. Otherwise the dialog just vanishes instantly in most
251 window.setTimeout(function() {
252 ClearBrowserDataOverlay.setClearing(false);
253 ClearBrowserDataOverlay.dismiss();
257 ClearBrowserDataOverlay.dismiss = function() {
258 var topmostVisiblePage = PageManager.getTopmostVisiblePage();
259 if (topmostVisiblePage && topmostVisiblePage.name == 'clearBrowserData')
260 PageManager.closeOverlay();
265 ClearBrowserDataOverlay: ClearBrowserDataOverlay