Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / options / clear_browser_data_overlay.js
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.
4
5 cr.define('options', function() {
6   var Page = cr.ui.pageManager.Page;
7   var PageManager = cr.ui.pageManager.PageManager;
8
9   /**
10    * ClearBrowserDataOverlay class
11    * Encapsulated handling of the 'Clear Browser Data' overlay page.
12    * @class
13    */
14   function ClearBrowserDataOverlay() {
15     Page.call(this, 'clearBrowserData',
16                      loadTimeData.getString('clearBrowserDataOverlayTabTitle'),
17                      'clear-browser-data-overlay');
18   }
19
20   cr.addSingletonGetter(ClearBrowserDataOverlay);
21
22   ClearBrowserDataOverlay.prototype = {
23     // Inherit ClearBrowserDataOverlay from Page.
24     __proto__: Page.prototype,
25
26     /**
27      * Whether deleting history and downloads is allowed.
28      * @type {boolean}
29      * @private
30      */
31     allowDeletingHistory_: true,
32
33     /**
34      * Whether or not clearing browsing data is currently in progress.
35      * @type {boolean}
36      * @private
37      */
38     isClearingInProgress_: false,
39
40     /**
41      * Whether or not the WebUI handler has completed initialization.
42      *
43      * Unless this becomes true, it must be assumed that the above flags might
44      * not contain the authoritative values.
45      *
46      * @type {boolean}
47      * @private
48      */
49     isInitializationComplete_: false,
50
51     /** @override */
52     initializePage: function() {
53       Page.prototype.initializePage.call(this);
54
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);
66       });
67
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;
72       }
73
74       this.createStuffRemainsFooter_();
75
76       $('clear-browser-data-dismiss').onclick = function(event) {
77         ClearBrowserDataOverlay.dismiss();
78       };
79       $('clear-browser-data-commit').onclick = function(event) {
80         ClearBrowserDataOverlay.setClearing(true);
81         chrome.send('performClearBrowserData');
82       };
83
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;
90       }
91
92       this.updateStateOfControls_();
93     },
94
95     /**
96      * Create a footer that explains that some content is not cleared by the
97      * clear browsing history dialog.
98      */
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
104       // span elements.
105       var footer =
106           document.querySelector('#some-stuff-remains-footer p');
107       var footerFragments =
108           loadTimeData.getString('contentSettingsAndSearchEnginesRemain')
109                       .split(/([|#])/);
110       for (var i = 0; i < footerFragments.length;) {
111         var buttonId = '';
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';
118           }
119         }
120
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);
127           i += 3;
128         } else {
129           var span = document.createElement('span');
130           span.textContent = footerFragments[i];
131           footer.appendChild(span);
132           i += 1;
133         }
134       }
135       $('open-content-settings-from-clear-browsing-data').onclick =
136           function(event) {
137         PageManager.showPageByName('content');
138       };
139       $('open-search-engines-from-clear-browsing-data').onclick =
140           function(event) {
141         PageManager.showPageByName('searchEngines');
142       };
143     },
144
145     /**
146      * Sets whether or not we are in the process of clearing data.
147      * @param {boolean} clearing Whether the browsing data is currently being
148      *     cleared.
149      * @private
150      */
151     setClearing_: function(clearing) {
152       this.isClearingInProgress_ = clearing;
153       this.updateStateOfControls_();
154     },
155
156     /**
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.
161      * @private
162      */
163     setAllowDeletingHistory_: function(allowed) {
164       this.allowDeletingHistory_ = allowed;
165       this.updateStateOfControls_();
166     },
167
168     /**
169      * Called by the WebUI handler to signal that it has finished calling all
170      * initialization methods.
171      * @private
172      */
173     markInitializationComplete_: function() {
174       this.isInitializationComplete_ = true;
175       this.updateStateOfControls_();
176     },
177
178     /**
179      * Updates the enabled/disabled/hidden status of all controls on the dialog.
180      * @private
181      */
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.
188       var enabled = 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) {
194             enabled = true;
195             break;
196           }
197         }
198       }
199       $('clear-browser-data-commit').disabled = !enabled;
200
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;
211       }
212
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;
224     }
225   };
226
227   //
228   // Chrome callbacks
229   //
230   ClearBrowserDataOverlay.setAllowDeletingHistory = function(allowed) {
231     ClearBrowserDataOverlay.getInstance().setAllowDeletingHistory_(allowed);
232   };
233
234   ClearBrowserDataOverlay.setClearing = function(clearing) {
235     ClearBrowserDataOverlay.getInstance().setClearing_(clearing);
236   };
237
238   ClearBrowserDataOverlay.markInitializationComplete = function() {
239     ClearBrowserDataOverlay.getInstance().markInitializationComplete_();
240   };
241
242   ClearBrowserDataOverlay.setBannerVisibility = function(args) {
243     var visible = args[0];
244     $('clear-browser-data-info-banner').hidden = !visible;
245   };
246
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
250     // cases.
251     window.setTimeout(function() {
252       ClearBrowserDataOverlay.setClearing(false);
253       ClearBrowserDataOverlay.dismiss();
254     }, 200);
255   };
256
257   ClearBrowserDataOverlay.dismiss = function() {
258     var topmostVisiblePage = PageManager.getTopmostVisiblePage();
259     if (topmostVisiblePage && topmostVisiblePage.name == 'clearBrowserData')
260       PageManager.closeOverlay();
261   };
262
263   // Export
264   return {
265     ClearBrowserDataOverlay: ClearBrowserDataOverlay
266   };
267 });