Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / resources / options / clear_browser_data_overlay.js
blob78d5333e986b099008fe6289a96eaf994507facb
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 OptionsPage = options.OptionsPage;
8   /**
9    * ClearBrowserDataOverlay class
10    * Encapsulated handling of the 'Clear Browser Data' overlay page.
11    * @class
12    */
13   function ClearBrowserDataOverlay() {
14     OptionsPage.call(this, 'clearBrowserData',
15                      loadTimeData.getString('clearBrowserDataOverlayTabTitle'),
16                      'clear-browser-data-overlay');
17   }
19   cr.addSingletonGetter(ClearBrowserDataOverlay);
21   ClearBrowserDataOverlay.prototype = {
22     // Inherit ClearBrowserDataOverlay from OptionsPage.
23     __proto__: OptionsPage.prototype,
25     // Whether deleting history and downloads is allowed.
26     allowDeletingHistory_: true,
28     /**
29      * Initialize the page.
30      */
31     initializePage: function() {
32       // Call base class implementation to starts preference initialization.
33       OptionsPage.prototype.initializePage.call(this);
35       var f = this.updateCommitButtonState_.bind(this);
36       var types = ['browser.clear_data.browsing_history',
37                    'browser.clear_data.download_history',
38                    'browser.clear_data.cache',
39                    'browser.clear_data.cookies',
40                    'browser.clear_data.passwords',
41                    'browser.clear_data.form_data',
42                    'browser.clear_data.hosted_apps_data',
43                    'browser.clear_data.content_licenses'];
44       types.forEach(function(type) {
45           Preferences.getInstance().addEventListener(type, f);
46       });
48       var checkboxes = document.querySelectorAll(
49           '#cbd-content-area input[type=checkbox]');
50       for (var i = 0; i < checkboxes.length; i++) {
51         checkboxes[i].onclick = f;
52       }
53       this.updateCommitButtonState_();
55       this.createStuffRemainsFooter_();
57       $('clear-browser-data-dismiss').onclick = function(event) {
58         ClearBrowserDataOverlay.dismiss();
59       };
60       $('clear-browser-data-commit').onclick = function(event) {
61         ClearBrowserDataOverlay.setClearingState(true);
62         chrome.send('performClearBrowserData');
63       };
65       var show = loadTimeData.getBoolean('showDeleteBrowsingHistoryCheckboxes');
66       this.showDeleteHistoryCheckboxes_(show);
67     },
69     // Create a footer that explains that some content is not cleared by the
70     // clear browsing history dialog.
71     createStuffRemainsFooter_: function() {
72       // The localized string is of the form "Saved [content settings] and
73       // {search engines} will not be cleared and may reflect your browsing
74       // habits.". The following parses out the parts in brackts and braces and
75       // converts them into buttons whereas the remainders are represented as
76       // span elements.
77       var footer =
78           document.querySelector('#some-stuff-remains-footer p');
79       var footerFragments =
80           loadTimeData.getString('contentSettingsAndSearchEnginesRemain')
81                       .split(/([|#])/);
82       for (var i = 0; i < footerFragments.length;) {
83         var buttonId = '';
84         if (i + 2 < footerFragments.length) {
85           if (footerFragments[i] == '|' && footerFragments[i + 2] == '|') {
86             buttonId = 'open-content-settings-from-clear-browsing-data';
87           } else if (footerFragments[i] == '#' &&
88                      footerFragments[i + 2] == '#') {
89             buttonId = 'open-search-engines-from-clear-browsing-data';
90           }
91         }
93         if (buttonId != '') {
94           var button = document.createElement('button');
95           button.setAttribute('id', buttonId);
96           button.setAttribute('class', 'link-button');
97           button.textContent = footerFragments[i + 1];
98           footer.appendChild(button);
99           i += 3;
100         } else {
101           var span = document.createElement('span');
102           span.textContent = footerFragments[i];
103           footer.appendChild(span);
104           i += 1;
105         }
106       }
107       $('open-content-settings-from-clear-browsing-data').onclick =
108           function(event) {
109         OptionsPage.navigateToPage('content');
110       }
111       $('open-search-engines-from-clear-browsing-data').onclick =
112           function(event) {
113         OptionsPage.navigateToPage('searchEngines');
114       }
115     },
117     // Set the enabled state of the commit button.
118     updateCommitButtonState_: function() {
119       var checkboxes = document.querySelectorAll(
120           '#cbd-content-area input[type=checkbox]');
121       var isChecked = false;
122       for (var i = 0; i < checkboxes.length; i++) {
123         if (checkboxes[i].checked) {
124           isChecked = true;
125           break;
126         }
127       }
128       $('clear-browser-data-commit').disabled = !isChecked;
129     },
131     setAllowDeletingHistory: function(allowed) {
132       this.allowDeletingHistory_ = allowed;
133     },
135     showDeleteHistoryCheckboxes_: function(show) {
136       if (!show) {
137         $('delete-browsing-history-container').hidden = true;
138         $('delete-download-history-container').hidden = true;
139       }
140     },
142     /** @override */
143     didShowPage: function() {
144       var allowed = ClearBrowserDataOverlay.getInstance().allowDeletingHistory_;
145       ClearBrowserDataOverlay.updateHistoryCheckboxes(allowed);
146     },
147   };
149   //
150   // Chrome callbacks
151   //
152   /**
153    * Updates the disabled status of the browsing-history and downloads
154    * checkboxes, also unchecking them if they are disabled. This is called in
155    * response to a change in the corresponding preference.
156    */
157   ClearBrowserDataOverlay.updateHistoryCheckboxes = function(allowed) {
158     $('delete-browsing-history-checkbox').disabled = !allowed;
159     $('delete-download-history-checkbox').disabled = !allowed;
160     if (!allowed) {
161       $('delete-browsing-history-checkbox').checked = false;
162       $('delete-download-history-checkbox').checked = false;
163     }
164     ClearBrowserDataOverlay.getInstance().setAllowDeletingHistory(allowed);
165   };
167   ClearBrowserDataOverlay.setClearingState = function(state) {
168     $('delete-browsing-history-checkbox').disabled = state;
169     $('delete-download-history-checkbox').disabled = state;
170     $('delete-cache-checkbox').disabled = state;
171     $('delete-cookies-checkbox').disabled = state;
172     $('delete-passwords-checkbox').disabled = state;
173     $('delete-form-data-checkbox').disabled = state;
174     $('delete-hosted-apps-data-checkbox').disabled = state;
175     $('deauthorize-content-licenses-checkbox').disabled = state;
176     $('clear-browser-data-time-period').disabled = state;
177     $('cbd-throbber').style.visibility = state ? 'visible' : 'hidden';
178     $('clear-browser-data-dismiss').disabled = state;
180     if (state)
181       $('clear-browser-data-commit').disabled = true;
182     else
183       ClearBrowserDataOverlay.getInstance().updateCommitButtonState_();
184   };
186   ClearBrowserDataOverlay.setBannerVisibility = function(args) {
187     var visible = args[0];
188     $('clear-browser-data-info-banner').hidden = !visible;
189   };
191   ClearBrowserDataOverlay.doneClearing = function() {
192     // The delay gives the user some feedback that the clearing
193     // actually worked. Otherwise the dialog just vanishes instantly in most
194     // cases.
195     window.setTimeout(function() {
196       ClearBrowserDataOverlay.dismiss();
197     }, 200);
198   };
200   ClearBrowserDataOverlay.dismiss = function() {
201     var topmostVisiblePage = OptionsPage.getTopmostVisiblePage();
202     if (topmostVisiblePage && topmostVisiblePage.name == 'clearBrowserData')
203       OptionsPage.closeOverlay();
204     this.setClearingState(false);
205   };
207   // Export
208   return {
209     ClearBrowserDataOverlay: ClearBrowserDataOverlay
210   };