Extract code handling PrinterProviderAPI from PrintPreviewHandler
[chromium-blink-merge.git] / chrome / browser / resources / options / certificate_manager.js
blob8e9cad6115881b57a0bb073a0f989b4e1b3045c5
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    * @constructor
18    */
19   function CertificateManagerTab(id, isKiosk) {
20     this.tree = $(id + '-tree');
22     options.CertificatesTree.decorate(this.tree);
23     this.tree.addEventListener('change',
24         this.handleCertificatesTreeChange_.bind(this));
26     var tree = this.tree;
28     this.viewButton = $(id + '-view');
29     this.viewButton.onclick = function(e) {
30       var selected = tree.selectedItem;
31       chrome.send('viewCertificate', [selected.data.id]);
32     };
34     this.editButton = $(id + '-edit');
35     if (this.editButton !== null) {
36       if (id == 'serverCertsTab') {
37         this.editButton.onclick = function(e) {
38           var selected = tree.selectedItem;
39           chrome.send('editServerCertificate', [selected.data.id]);
40         };
41       } else if (id == 'caCertsTab') {
42         this.editButton.onclick = function(e) {
43           var data = tree.selectedItem.data;
44           CertificateEditCaTrustOverlay.show(data.id, data.name);
45         };
46       }
47     }
49     this.backupButton = $(id + '-backup');
50     if (this.backupButton !== null) {
51       if (id == 'personalCertsTab' && isKiosk) {
52         this.backupButton.hidden = true;
53       } else {
54         this.backupButton.onclick = function(e) {
55           var selected = tree.selectedItem;
56           chrome.send('exportPersonalCertificate', [selected.data.id]);
57         };
58       }
59     }
61     this.backupAllButton = $(id + '-backup-all');
62     if (this.backupAllButton !== null) {
63       if (id == 'personalCertsTab' && isKiosk) {
64         this.backupAllButton.hidden = true;
65       } else {
66         this.backupAllButton.onclick = function(e) {
67           chrome.send('exportAllPersonalCertificates');
68         };
69       }
70     }
72     this.importButton = $(id + '-import');
73     if (this.importButton !== null) {
74       if (id == 'personalCertsTab') {
75         if (isKiosk) {
76           this.importButton.hidden = true;
77         } else {
78           this.importButton.onclick = function(e) {
79             chrome.send('importPersonalCertificate', [false]);
80           };
81         }
82       } else if (id == 'serverCertsTab') {
83         this.importButton.onclick = function(e) {
84           chrome.send('importServerCertificate');
85         };
86       } else if (id == 'caCertsTab') {
87         this.importButton.onclick = function(e) {
88           chrome.send('importCaCertificate');
89         };
90       }
91     }
93     this.importAndBindButton = $(id + '-import-and-bind');
94     if (this.importAndBindButton !== null) {
95       if (id == 'personalCertsTab') {
96         this.importAndBindButton.onclick = function(e) {
97           chrome.send('importPersonalCertificate', [true]);
98         };
99       }
100     }
102     this.exportButton = $(id + '-export');
103     if (this.exportButton !== null) {
104       if (id == 'personalCertsTab' && isKiosk) {
105         this.exportButton.hidden = true;
106       } else {
107         this.exportButton.onclick = function(e) {
108           var selected = tree.selectedItem;
109           chrome.send('exportCertificate', [selected.data.id]);
110         };
111       }
112     }
114     this.deleteButton = $(id + '-delete');
115     this.deleteButton.onclick = function(e) {
116       var data = tree.selectedItem.data;
117       AlertOverlay.show(
118           loadTimeData.getStringF(id + 'DeleteConfirm', data.name),
119           loadTimeData.getString(id + 'DeleteImpact'),
120           loadTimeData.getString('ok'),
121           loadTimeData.getString('cancel'),
122           function() {
123             tree.selectedItem = null;
124             chrome.send('deleteCertificate', [data.id]);
125           });
126     };
127   }
129   CertificateManagerTab.prototype = {
130     /**
131      * Update button state.
132      * @private
133      * @param {!Object} data The data of the selected item.
134      */
135     updateButtonState: function(data) {
136       var isCert = !!data && data.isCert;
137       var readOnly = !!data && data.readonly;
138       var extractable = !!data && data.extractable;
139       var hasChildren = this.tree.items.length > 0;
140       var isPolicy = !!data && data.policy;
141       this.viewButton.disabled = !isCert;
142       if (this.editButton !== null)
143         this.editButton.disabled = !isCert || isPolicy;
144       if (this.backupButton !== null)
145         this.backupButton.disabled = !isCert || !extractable;
146       if (this.backupAllButton !== null)
147         this.backupAllButton.disabled = !hasChildren;
148       if (this.exportButton !== null)
149         this.exportButton.disabled = !isCert;
150       this.deleteButton.disabled = !isCert || readOnly || isPolicy;
151     },
153     /**
154      * Handles certificate tree selection change.
155      * @private
156      * @param {!Event} e The change event object.
157      */
158     handleCertificatesTreeChange_: function(e) {
159       var data = null;
160       if (this.tree.selectedItem)
161         data = this.tree.selectedItem.data;
163       this.updateButtonState(data);
164     },
165   };
167   /////////////////////////////////////////////////////////////////////////////
168   // CertificateManager class:
170   /**
171    * Encapsulated handling of ChromeOS accounts options page.
172    * @constructor
173    * @extends {cr.ui.pageManager.Page}
174    */
175   function CertificateManager() {
176     Page.call(this, 'certificates',
177               loadTimeData.getString('certificateManagerPageTabTitle'),
178               'certificateManagerPage');
179   }
181   cr.addSingletonGetter(CertificateManager);
183   CertificateManager.prototype = {
184     __proto__: Page.prototype,
186     /** @private {boolean} */
187     isKiosk_: false,
189     /** @param {boolean} isKiosk */
190     setIsKiosk: function(isKiosk) {
191       this.isKiosk_ = isKiosk;
192     },
194     /** @override */
195     initializePage: function() {
196       Page.prototype.initializePage.call(this);
198       this.personalTab = new CertificateManagerTab('personalCertsTab',
199                                                    this.isKiosk_);
200       this.serverTab = new CertificateManagerTab('serverCertsTab',
201                                                  this.isKiosk_);
202       this.caTab = new CertificateManagerTab('caCertsTab', this.isKiosk_);
203       this.otherTab = new CertificateManagerTab('otherCertsTab', this.isKiosk_);
205       this.addEventListener('visibleChange', this.handleVisibleChange_);
207       $('certificate-confirm').onclick = function() {
208         PageManager.closeOverlay();
209       };
210     },
212     initalized_: false,
214     /**
215      * Handler for Page's visible property change event.
216      * @private
217      * @param {Event} e Property change event.
218      */
219     handleVisibleChange_: function(e) {
220       if (!this.initalized_ && this.visible) {
221         this.initalized_ = true;
222         OptionsPage.showTab($('personal-certs-nav-tab'));
223         chrome.send('populateCertificateManager');
224       }
225     }
226   };
228   // CertificateManagerHandler callbacks.
229   CertificateManager.onPopulateTree = function(args) {
230     $(args[0]).populate(args[1]);
231   };
233   CertificateManager.exportPersonalAskPassword = function(args) {
234     CertificateBackupOverlay.show();
235   };
237   CertificateManager.importPersonalAskPassword = function(args) {
238     CertificateRestoreOverlay.show();
239   };
241   CertificateManager.onModelReady = function(userDbAvailable,
242                                              tpmAvailable) {
243     if (!userDbAvailable)
244       return;
245     if (tpmAvailable)
246       $('personalCertsTab-import-and-bind').disabled = false;
247     $('personalCertsTab-import').disabled = false;
248     $('serverCertsTab-import').disabled = false;
249     $('caCertsTab-import').disabled = false;
250   };
252   // Export
253   return {
254     CertificateManagerTab: CertificateManagerTab,
255     CertificateManager: CertificateManager
256   };