OLD | NEW |
| (Empty) |
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 // CertificateManagerTab class: | |
10 | |
11 /** | |
12 * blah | |
13 * @param {!string} id The id of this tab. | |
14 */ | |
15 function CertificateManagerTab(id) { | |
16 this.tree = $(id + '-tree'); | |
17 | |
18 options.CertificatesTree.decorate(this.tree); | |
19 this.tree.addEventListener('change', | |
20 this.handleCertificatesTreeChange_.bind(this)); | |
21 | |
22 var tree = this.tree; | |
23 | |
24 this.viewButton = $(id + '-view'); | |
25 this.viewButton.onclick = function(e) { | |
26 var selected = tree.selectedItem; | |
27 chrome.send('viewCertificate', [selected.data.id]); | |
28 } | |
29 | |
30 this.editButton = $(id + '-edit'); | |
31 if (this.editButton !== null) { | |
32 if (id == 'serverCertsTab') { | |
33 this.editButton.onclick = function(e) { | |
34 var selected = tree.selectedItem; | |
35 chrome.send('editServerCertificate', [selected.data.id]); | |
36 } | |
37 } else if (id == 'caCertsTab') { | |
38 this.editButton.onclick = function(e) { | |
39 var data = tree.selectedItem.data; | |
40 CertificateEditCaTrustOverlay.show(data.id, data.name); | |
41 } | |
42 } | |
43 } | |
44 | |
45 this.backupButton = $(id + '-backup'); | |
46 if (this.backupButton !== null) { | |
47 this.backupButton.onclick = function(e) { | |
48 var selected = tree.selectedItem; | |
49 chrome.send('exportPersonalCertificate', [selected.data.id]); | |
50 } | |
51 } | |
52 | |
53 this.backupAllButton = $(id + '-backup-all'); | |
54 if (this.backupAllButton !== null) { | |
55 this.backupAllButton.onclick = function(e) { | |
56 chrome.send('exportAllPersonalCertificates'); | |
57 } | |
58 } | |
59 | |
60 this.importButton = $(id + '-import'); | |
61 if (this.importButton !== null) { | |
62 if (id == 'personalCertsTab') { | |
63 this.importButton.onclick = function(e) { | |
64 chrome.send('importPersonalCertificate', [false]); | |
65 } | |
66 } else if (id == 'serverCertsTab') { | |
67 this.importButton.onclick = function(e) { | |
68 chrome.send('importServerCertificate'); | |
69 } | |
70 } else if (id == 'caCertsTab') { | |
71 this.importButton.onclick = function(e) { | |
72 chrome.send('importCaCertificate'); | |
73 } | |
74 } | |
75 } | |
76 | |
77 this.importAndBindButton = $(id + '-import-and-bind'); | |
78 if (this.importAndBindButton !== null) { | |
79 if (id == 'personalCertsTab') { | |
80 this.importAndBindButton.onclick = function(e) { | |
81 chrome.send('importPersonalCertificate', [true]); | |
82 } | |
83 } | |
84 } | |
85 | |
86 this.exportButton = $(id + '-export'); | |
87 if (this.exportButton !== null) { | |
88 this.exportButton.onclick = function(e) { | |
89 var selected = tree.selectedItem; | |
90 chrome.send('exportCertificate', [selected.data.id]); | |
91 } | |
92 } | |
93 | |
94 this.deleteButton = $(id + '-delete'); | |
95 this.deleteButton.onclick = function(e) { | |
96 var data = tree.selectedItem.data; | |
97 AlertOverlay.show( | |
98 loadTimeData.getStringF(id + 'DeleteConfirm', data.name), | |
99 loadTimeData.getString(id + 'DeleteImpact'), | |
100 loadTimeData.getString('ok'), | |
101 loadTimeData.getString('cancel'), | |
102 function() { | |
103 tree.selectedItem = null; | |
104 chrome.send('deleteCertificate', [data.id]); | |
105 }); | |
106 } | |
107 } | |
108 | |
109 CertificateManagerTab.prototype = { | |
110 | |
111 /** | |
112 * Update button state. | |
113 * @private | |
114 * @param {!Object} data The data of the selected item. | |
115 */ | |
116 updateButtonState: function(data) { | |
117 var isCert = !!data && data.isCert; | |
118 var readOnly = !!data && data.readonly; | |
119 var extractable = !!data && data.extractable; | |
120 var hasChildren = this.tree.items.length > 0; | |
121 this.viewButton.disabled = !isCert; | |
122 if (this.editButton !== null) | |
123 this.editButton.disabled = !isCert; | |
124 if (this.backupButton !== null) | |
125 this.backupButton.disabled = !isCert || !extractable; | |
126 if (this.backupAllButton !== null) | |
127 this.backupAllButton.disabled = !hasChildren; | |
128 if (this.exportButton !== null) | |
129 this.exportButton.disabled = !isCert; | |
130 this.deleteButton.disabled = !isCert || readOnly; | |
131 }, | |
132 | |
133 /** | |
134 * Handles certificate tree selection change. | |
135 * @private | |
136 * @param {!Event} e The change event object. | |
137 */ | |
138 handleCertificatesTreeChange_: function(e) { | |
139 var data = null; | |
140 if (this.tree.selectedItem) { | |
141 data = this.tree.selectedItem.data; | |
142 } | |
143 | |
144 this.updateButtonState(data); | |
145 }, | |
146 }; | |
147 | |
148 // TODO(xiyuan): Use notification from backend instead of polling. | |
149 // TPM token check polling timer. | |
150 var tpmPollingTimer; | |
151 | |
152 // Initiate tpm token check if needed. | |
153 function checkTpmToken() { | |
154 var importAndBindButton = $('personalCertsTab-import-and-bind'); | |
155 | |
156 if (importAndBindButton && importAndBindButton.disabled) | |
157 chrome.send('checkTpmTokenReady'); | |
158 } | |
159 | |
160 // Stop tpm polling timer. | |
161 function stopTpmTokenCheckPolling() { | |
162 if (tpmPollingTimer) { | |
163 window.clearTimeout(tpmPollingTimer); | |
164 tpmPollingTimer = undefined; | |
165 } | |
166 } | |
167 | |
168 ///////////////////////////////////////////////////////////////////////////// | |
169 // CertificateManager class: | |
170 | |
171 /** | |
172 * Encapsulated handling of ChromeOS accounts options page. | |
173 * @constructor | |
174 */ | |
175 function CertificateManager(model) { | |
176 OptionsPage.call(this, 'certificates', | |
177 loadTimeData.getString('certificateManagerPageTabTitle'), | |
178 'certificateManagerPage'); | |
179 } | |
180 | |
181 cr.addSingletonGetter(CertificateManager); | |
182 | |
183 CertificateManager.prototype = { | |
184 __proto__: OptionsPage.prototype, | |
185 | |
186 initializePage: function() { | |
187 OptionsPage.prototype.initializePage.call(this); | |
188 | |
189 this.personalTab = new CertificateManagerTab('personalCertsTab'); | |
190 this.serverTab = new CertificateManagerTab('serverCertsTab'); | |
191 this.caTab = new CertificateManagerTab('caCertsTab'); | |
192 this.otherTab = new CertificateManagerTab('otherCertsTab'); | |
193 | |
194 this.addEventListener('visibleChange', this.handleVisibleChange_); | |
195 | |
196 $('certificate-confirm').onclick = function() { | |
197 OptionsPage.closeOverlay(); | |
198 }; | |
199 }, | |
200 | |
201 initalized_: false, | |
202 | |
203 /** | |
204 * Handler for OptionsPage'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 if (cr.isChromeOS) { | |
216 // Ensure TPM token check on visible and stop polling when hidden. | |
217 if (this.visible) | |
218 checkTpmToken(); | |
219 else | |
220 stopTpmTokenCheckPolling(); | |
221 } | |
222 } | |
223 }; | |
224 | |
225 // CertificateManagerHandler callbacks. | |
226 CertificateManager.onPopulateTree = function(args) { | |
227 $(args[0]).populate(args[1]); | |
228 }; | |
229 | |
230 CertificateManager.exportPersonalAskPassword = function(args) { | |
231 CertificateBackupOverlay.show(); | |
232 }; | |
233 | |
234 CertificateManager.importPersonalAskPassword = function(args) { | |
235 CertificateRestoreOverlay.show(); | |
236 }; | |
237 | |
238 CertificateManager.onCheckTpmTokenReady = function(ready) { | |
239 var importAndBindButton = $('personalCertsTab-import-and-bind'); | |
240 if (importAndBindButton) { | |
241 importAndBindButton.disabled = !ready; | |
242 | |
243 // Check again after 5 seconds if Tpm is not ready and certificate manager | |
244 // is still visible. | |
245 if (!ready && CertificateManager.getInstance().visible) | |
246 tpmPollingTimer = window.setTimeout(checkTpmToken, 5000); | |
247 } | |
248 }; | |
249 | |
250 // Export | |
251 return { | |
252 CertificateManagerTab: CertificateManagerTab, | |
253 CertificateManager: CertificateManager | |
254 }; | |
255 }); | |
OLD | NEW |