Split OptionsPage into Page and PageManager.
[chromium-blink-merge.git] / chrome / browser / resources / options / certificate_manager.js
blob9a78954d218ef3a51340aa94770c7a81e7c1b861
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;
7   var Page = cr.ui.pageManager.Page;
8   var PageManager = cr.ui.pageManager.PageManager;
10   /////////////////////////////////////////////////////////////////////////////
11   // CertificateManagerTab class:
13   /**
14    * blah
15    * @param {!string} id The id of this tab.
16    * @param {boolean} isKiosk True if dialog is shown during CrOS kiosk launch.
17    */
18   function CertificateManagerTab(id, isKiosk) {
19     this.tree = $(id + '-tree');
21     options.CertificatesTree.decorate(this.tree);
22     this.tree.addEventListener('change',
23         this.handleCertificatesTreeChange_.bind(this));
25     var tree = this.tree;
27     this.viewButton = $(id + '-view');
28     this.viewButton.onclick = function(e) {
29       var selected = tree.selectedItem;
30       chrome.send('viewCertificate', [selected.data.id]);
31     };
33     this.editButton = $(id + '-edit');
34     if (this.editButton !== null) {
35       if (id == 'serverCertsTab') {
36         this.editButton.onclick = function(e) {
37           var selected = tree.selectedItem;
38           chrome.send('editServerCertificate', [selected.data.id]);
39         };
40       } else if (id == 'caCertsTab') {
41         this.editButton.onclick = function(e) {
42           var data = tree.selectedItem.data;
43           CertificateEditCaTrustOverlay.show(data.id, data.name);
44         };
45       }
46     }
48     this.backupButton = $(id + '-backup');
49     if (this.backupButton !== null) {
50       if (id == 'personalCertsTab' && isKiosk) {
51         this.backupButton.hidden = true;
52       } else {
53         this.backupButton.onclick = function(e) {
54           var selected = tree.selectedItem;
55           chrome.send('exportPersonalCertificate', [selected.data.id]);
56         };
57       }
58     }
60     this.backupAllButton = $(id + '-backup-all');
61     if (this.backupAllButton !== null) {
62       if (id == 'personalCertsTab' && isKiosk) {
63         this.backupAllButton.hidden = true;
64       } else {
65         this.backupAllButton.onclick = function(e) {
66           chrome.send('exportAllPersonalCertificates');
67         };
68       }
69     }
71     this.importButton = $(id + '-import');
72     if (this.importButton !== null) {
73       if (id == 'personalCertsTab') {
74         if (isKiosk) {
75           this.importButton.hidden = true;
76         } else {
77           this.importButton.onclick = function(e) {
78             chrome.send('importPersonalCertificate', [false]);
79           };
80         }
81       } else if (id == 'serverCertsTab') {
82         this.importButton.onclick = function(e) {
83           chrome.send('importServerCertificate');
84         };
85       } else if (id == 'caCertsTab') {
86         this.importButton.onclick = function(e) {
87           chrome.send('importCaCertificate');
88         };
89       }
90     }
92     this.importAndBindButton = $(id + '-import-and-bind');
93     if (this.importAndBindButton !== null) {
94       if (id == 'personalCertsTab') {
95         this.importAndBindButton.onclick = function(e) {
96           chrome.send('importPersonalCertificate', [true]);
97         };
98       }
99     }
101     this.exportButton = $(id + '-export');
102     if (this.exportButton !== null) {
103       if (id == 'personalCertsTab' && isKiosk) {
104         this.exportButton.hidden = true;
105       } else {
106         this.exportButton.onclick = function(e) {
107           var selected = tree.selectedItem;
108           chrome.send('exportCertificate', [selected.data.id]);
109         };
110       }
111     }
113     this.deleteButton = $(id + '-delete');
114     this.deleteButton.onclick = function(e) {
115       var data = tree.selectedItem.data;
116       AlertOverlay.show(
117           loadTimeData.getStringF(id + 'DeleteConfirm', data.name),
118           loadTimeData.getString(id + 'DeleteImpact'),
119           loadTimeData.getString('ok'),
120           loadTimeData.getString('cancel'),
121           function() {
122             tree.selectedItem = null;
123             chrome.send('deleteCertificate', [data.id]);
124           });
125     };
126   }
128   CertificateManagerTab.prototype = {
129     /**
130      * Update button state.
131      * @private
132      * @param {!Object} data The data of the selected item.
133      */
134     updateButtonState: function(data) {
135       var isCert = !!data && data.isCert;
136       var readOnly = !!data && data.readonly;
137       var extractable = !!data && data.extractable;
138       var hasChildren = this.tree.items.length > 0;
139       var isPolicy = !!data && data.policy;
140       this.viewButton.disabled = !isCert;
141       if (this.editButton !== null)
142         this.editButton.disabled = !isCert || isPolicy;
143       if (this.backupButton !== null)
144         this.backupButton.disabled = !isCert || !extractable;
145       if (this.backupAllButton !== null)
146         this.backupAllButton.disabled = !hasChildren;
147       if (this.exportButton !== null)
148         this.exportButton.disabled = !isCert;
149       this.deleteButton.disabled = !isCert || readOnly || isPolicy;
150     },
152     /**
153      * Handles certificate tree selection change.
154      * @private
155      * @param {!Event} e The change event object.
156      */
157     handleCertificatesTreeChange_: function(e) {
158       var data = null;
159       if (this.tree.selectedItem)
160         data = this.tree.selectedItem.data;
162       this.updateButtonState(data);
163     },
164   };
166   /////////////////////////////////////////////////////////////////////////////
167   // CertificateManager class:
169   /**
170    * Encapsulated handling of ChromeOS accounts options page.
171    * @constructor
172    */
173   function CertificateManager(model) {
174     Page.call(this, 'certificates',
175               loadTimeData.getString('certificateManagerPageTabTitle'),
176               'certificateManagerPage');
177   }
179   cr.addSingletonGetter(CertificateManager);
181   CertificateManager.prototype = {
182     __proto__: Page.prototype,
184     /** @override */
185     initializePage: function(isKiosk) {
186       Page.prototype.initializePage.call(this);
188       this.personalTab = new CertificateManagerTab('personalCertsTab',
189                                                    !!isKiosk);
190       this.serverTab = new CertificateManagerTab('serverCertsTab', !!isKiosk);
191       this.caTab = new CertificateManagerTab('caCertsTab', !!isKiosk);
192       this.otherTab = new CertificateManagerTab('otherCertsTab', !!isKiosk);
194       this.addEventListener('visibleChange', this.handleVisibleChange_);
196       $('certificate-confirm').onclick = function() {
197         PageManager.closeOverlay();
198       };
199     },
201     initalized_: false,
203     /**
204      * Handler for Page's visible property change event.
205      * @private
206      * @param {Event} e Property change event.
207      */
208     handleVisibleChange_: function(e) {
209       if (!this.initalized_ && this.visible) {
210         this.initalized_ = true;
211         OptionsPage.showTab($('personal-certs-nav-tab'));
212         chrome.send('populateCertificateManager');
213       }
214     }
215   };
217   // CertificateManagerHandler callbacks.
218   CertificateManager.onPopulateTree = function(args) {
219     $(args[0]).populate(args[1]);
220   };
222   CertificateManager.exportPersonalAskPassword = function(args) {
223     CertificateBackupOverlay.show();
224   };
226   CertificateManager.importPersonalAskPassword = function(args) {
227     CertificateRestoreOverlay.show();
228   };
230   CertificateManager.onModelReady = function(userDbAvailable,
231                                              tpmAvailable) {
232     if (!userDbAvailable)
233       return;
234     if (tpmAvailable)
235       $('personalCertsTab-import-and-bind').disabled = false;
236     $('personalCertsTab-import').disabled = false;
237     $('serverCertsTab-import').disabled = false;
238     $('caCertsTab-import').disabled = false;
239   };
241   // Export
242   return {
243     CertificateManagerTab: CertificateManagerTab,
244     CertificateManager: CertificateManager
245   };