- add sources.
[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 OptionsPage = options.OptionsPage;
7
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   }
18
19   cr.addSingletonGetter(ClearBrowserDataOverlay);
20
21   ClearBrowserDataOverlay.prototype = {
22     // Inherit ClearBrowserDataOverlay from OptionsPage.
23     __proto__: OptionsPage.prototype,
24
25     // Whether deleting history and downloads is allowed.
26     allowDeletingHistory_: true,
27
28     /**
29      * Initialize the page.
30      */
31     initializePage: function() {
32       // Call base class implementation to starts preference initialization.
33       OptionsPage.prototype.initializePage.call(this);
34
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       });
47
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_();
54
55       $('clear-browser-data-dismiss').onclick = function(event) {
56         ClearBrowserDataOverlay.dismiss();
57       };
58       $('clear-browser-data-commit').onclick = function(event) {
59         ClearBrowserDataOverlay.setClearingState(true);
60         chrome.send('performClearBrowserData');
61       };
62
63       var show = loadTimeData.getBoolean('showDeleteBrowsingHistoryCheckboxes');
64       this.showDeleteHistoryCheckboxes_(show);
65     },
66
67     // Set the enabled state of the commit button.
68     updateCommitButtonState_: function() {
69       var checkboxes = document.querySelectorAll(
70           '#cbd-content-area input[type=checkbox]');
71       var isChecked = false;
72       for (var i = 0; i < checkboxes.length; i++) {
73         if (checkboxes[i].checked) {
74           isChecked = true;
75           break;
76         }
77       }
78       $('clear-browser-data-commit').disabled = !isChecked;
79     },
80
81     setAllowDeletingHistory: function(allowed) {
82       this.allowDeletingHistory_ = allowed;
83     },
84
85     showDeleteHistoryCheckboxes_: function(show) {
86       if (!show) {
87         $('delete-browsing-history-container').hidden = true;
88         $('delete-download-history-container').hidden = true;
89       }
90     },
91
92     /** @override */
93     didShowPage: function() {
94       var allowed = ClearBrowserDataOverlay.getInstance().allowDeletingHistory_;
95       ClearBrowserDataOverlay.updateHistoryCheckboxes(allowed);
96     },
97   };
98
99   //
100   // Chrome callbacks
101   //
102   /**
103    * Updates the disabled status of the browsing-history and downloads
104    * checkboxes, also unchecking them if they are disabled. This is called in
105    * response to a change in the corresponding preference.
106    */
107   ClearBrowserDataOverlay.updateHistoryCheckboxes = function(allowed) {
108     $('delete-browsing-history-checkbox').disabled = !allowed;
109     $('delete-download-history-checkbox').disabled = !allowed;
110     if (!allowed) {
111       $('delete-browsing-history-checkbox').checked = false;
112       $('delete-download-history-checkbox').checked = false;
113     }
114     ClearBrowserDataOverlay.getInstance().setAllowDeletingHistory(allowed);
115   };
116
117   ClearBrowserDataOverlay.setClearingState = function(state) {
118     $('delete-browsing-history-checkbox').disabled = state;
119     $('delete-download-history-checkbox').disabled = state;
120     $('delete-cache-checkbox').disabled = state;
121     $('delete-cookies-checkbox').disabled = state;
122     $('delete-passwords-checkbox').disabled = state;
123     $('delete-form-data-checkbox').disabled = state;
124     $('delete-hosted-apps-data-checkbox').disabled = state;
125     $('deauthorize-content-licenses-checkbox').disabled = state;
126     $('clear-browser-data-time-period').disabled = state;
127     $('cbd-throbber').style.visibility = state ? 'visible' : 'hidden';
128     $('clear-browser-data-dismiss').disabled = state;
129
130     if (state)
131       $('clear-browser-data-commit').disabled = true;
132     else
133       ClearBrowserDataOverlay.getInstance().updateCommitButtonState_();
134   };
135
136   ClearBrowserDataOverlay.setBannerVisibility = function(args) {
137     var visible = args[0];
138     $('clear-browser-data-info-banner').hidden = !visible;
139   };
140
141   ClearBrowserDataOverlay.doneClearing = function() {
142     // The delay gives the user some feedback that the clearing
143     // actually worked. Otherwise the dialog just vanishes instantly in most
144     // cases.
145     window.setTimeout(function() {
146       ClearBrowserDataOverlay.dismiss();
147     }, 200);
148   };
149
150   ClearBrowserDataOverlay.dismiss = function() {
151     OptionsPage.closeOverlay();
152     this.setClearingState(false);
153   };
154
155   // Export
156   return {
157     ClearBrowserDataOverlay: ClearBrowserDataOverlay
158   };
159 });