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
5cr.define('options', function() {
6  var OptionsPage = options.OptionsPage;
7  var Page = cr.ui.pageManager.Page;
8  var PageManager = cr.ui.pageManager.PageManager;
9
10  /////////////////////////////////////////////////////////////////////////////
11  // CertificateManagerTab class:
12
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');
21
22    options.CertificatesTree.decorate(this.tree);
23    this.tree.addEventListener('change',
24        this.handleCertificatesTreeChange_.bind(this));
25
26    var tree = this.tree;
27
28    this.viewButton = $(id + '-view');
29    this.viewButton.onclick = function(e) {
30      var selected = tree.selectedItem;
31      chrome.send('viewCertificate', [selected.data.id]);
32    };
33
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    }
48
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    }
60
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    }
71
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    }
92
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    }
101
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    }
113
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  }
128
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    },
152
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;
162
163      this.updateButtonState(data);
164    },
165  };
166
167  /////////////////////////////////////////////////////////////////////////////
168  // CertificateManager class:
169
170  /**
171   * Encapsulated handling of ChromeOS accounts options page.
172   * @constructor
173   * @extends {cr.ui.pageManager.Page}
174   */
175  function CertificateManager(model) {
176    Page.call(this, 'certificates',
177              loadTimeData.getString('certificateManagerPageTabTitle'),
178              'certificateManagerPage');
179  }
180
181  cr.addSingletonGetter(CertificateManager);
182
183  CertificateManager.prototype = {
184    __proto__: Page.prototype,
185
186    /** @override */
187    initializePage: function(isKiosk) {
188      Page.prototype.initializePage.call(this);
189
190      this.personalTab = new CertificateManagerTab('personalCertsTab',
191                                                   !!isKiosk);
192      this.serverTab = new CertificateManagerTab('serverCertsTab', !!isKiosk);
193      this.caTab = new CertificateManagerTab('caCertsTab', !!isKiosk);
194      this.otherTab = new CertificateManagerTab('otherCertsTab', !!isKiosk);
195
196      this.addEventListener('visibleChange', this.handleVisibleChange_);
197
198      $('certificate-confirm').onclick = function() {
199        PageManager.closeOverlay();
200      };
201    },
202
203    initalized_: false,
204
205    /**
206     * Handler for Page's visible property change event.
207     * @private
208     * @param {Event} e Property change event.
209     */
210    handleVisibleChange_: function(e) {
211      if (!this.initalized_ && this.visible) {
212        this.initalized_ = true;
213        OptionsPage.showTab($('personal-certs-nav-tab'));
214        chrome.send('populateCertificateManager');
215      }
216    }
217  };
218
219  // CertificateManagerHandler callbacks.
220  CertificateManager.onPopulateTree = function(args) {
221    $(args[0]).populate(args[1]);
222  };
223
224  CertificateManager.exportPersonalAskPassword = function(args) {
225    CertificateBackupOverlay.show();
226  };
227
228  CertificateManager.importPersonalAskPassword = function(args) {
229    CertificateRestoreOverlay.show();
230  };
231
232  CertificateManager.onModelReady = function(userDbAvailable,
233                                             tpmAvailable) {
234    if (!userDbAvailable)
235      return;
236    if (tpmAvailable)
237      $('personalCertsTab-import-and-bind').disabled = false;
238    $('personalCertsTab-import').disabled = false;
239    $('serverCertsTab-import').disabled = false;
240    $('caCertsTab-import').disabled = false;
241  };
242
243  // Export
244  return {
245    CertificateManagerTab: CertificateManagerTab,
246    CertificateManager: CertificateManager
247  };
248});
249