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