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  /** @const */ var Page = cr.ui.pageManager.Page;
7  /** @const */ var PageManager = cr.ui.pageManager.PageManager;
8
9  /**
10   * CertificateEditCaTrustOverlay class
11   * Encapsulated handling of the 'edit ca trust' and 'import ca' overlay pages.
12   * @class
13   */
14  function CertificateEditCaTrustOverlay() {
15    Page.call(this, 'certificateEditCaTrustOverlay', '',
16              'certificateEditCaTrustOverlay');
17  }
18
19  cr.addSingletonGetter(CertificateEditCaTrustOverlay);
20
21  CertificateEditCaTrustOverlay.prototype = {
22    __proto__: Page.prototype,
23
24    /**
25     * Dismisses the overlay.
26     * @private
27     */
28    dismissOverlay_: function() {
29      PageManager.closeOverlay();
30    },
31
32    /**
33     * Enables or disables input fields.
34     * @private
35     */
36    enableInputs_: function(enabled) {
37      $('certificateCaTrustSSLCheckbox').disabled =
38      $('certificateCaTrustEmailCheckbox').disabled =
39      $('certificateCaTrustObjSignCheckbox').disabled =
40      $('certificateEditCaTrustCancelButton').disabled =
41      $('certificateEditCaTrustOkButton').disabled = !enabled;
42    },
43
44    /**
45     * Attempt the Edit operation.
46     * The overlay will be left up with inputs disabled until the backend
47     * finishes and dismisses it.
48     * @private
49     */
50    finishEdit_: function() {
51      // TODO(mattm): Send checked values as booleans.  For now send them as
52      // strings, since WebUIBindings::send does not support any other types :(
53      chrome.send('editCaCertificateTrust',
54                  [this.certId,
55                   $('certificateCaTrustSSLCheckbox').checked.toString(),
56                   $('certificateCaTrustEmailCheckbox').checked.toString(),
57                   $('certificateCaTrustObjSignCheckbox').checked.toString()]);
58      this.enableInputs_(false);
59    },
60
61    /**
62     * Cancel the Edit operation.
63     * @private
64     */
65    cancelEdit_: function() {
66      this.dismissOverlay_();
67    },
68
69    /**
70     * Attempt the Import operation.
71     * The overlay will be left up with inputs disabled until the backend
72     * finishes and dismisses it.
73     * @private
74     */
75    finishImport_: function() {
76      // TODO(mattm): Send checked values as booleans.  For now send them as
77      // strings, since WebUIBindings::send does not support any other types :(
78      chrome.send('importCaCertificateTrustSelected',
79                  [$('certificateCaTrustSSLCheckbox').checked.toString(),
80                   $('certificateCaTrustEmailCheckbox').checked.toString(),
81                   $('certificateCaTrustObjSignCheckbox').checked.toString()]);
82      this.enableInputs_(false);
83    },
84
85    /**
86     * Cancel the Import operation.
87     * @private
88     */
89    cancelImport_: function() {
90      chrome.send('cancelImportExportCertificate');
91      this.dismissOverlay_();
92    },
93  };
94
95  /**
96   * Callback from CertificateManagerHandler with the trust values.
97   * @param {boolean} trustSSL The initial value of SSL trust checkbox.
98   * @param {boolean} trustEmail The initial value of Email trust checkbox.
99   * @param {boolean} trustObjSign The initial value of Object Signing trust.
100   */
101  CertificateEditCaTrustOverlay.populateTrust = function(
102      trustSSL, trustEmail, trustObjSign) {
103    $('certificateCaTrustSSLCheckbox').checked = trustSSL;
104    $('certificateCaTrustEmailCheckbox').checked = trustEmail;
105    $('certificateCaTrustObjSignCheckbox').checked = trustObjSign;
106    CertificateEditCaTrustOverlay.getInstance().enableInputs_(true);
107  };
108
109  /**
110   * Show the Edit CA Trust overlay.
111   * @param {string} certId The id of the certificate to be passed to the
112   * certificate manager model.
113   * @param {string} certName The display name of the certificate.
114   * checkbox.
115   */
116  CertificateEditCaTrustOverlay.show = function(certId, certName) {
117    var self = CertificateEditCaTrustOverlay.getInstance();
118    self.certId = certId;
119    $('certificateEditCaTrustCancelButton').onclick = function(event) {
120      self.cancelEdit_();
121    };
122    $('certificateEditCaTrustOkButton').onclick = function(event) {
123      self.finishEdit_();
124    };
125    $('certificateEditCaTrustDescription').textContent =
126        loadTimeData.getStringF('certificateEditCaTrustDescriptionFormat',
127                                certName);
128    self.enableInputs_(false);
129    PageManager.showPageByName('certificateEditCaTrustOverlay');
130    chrome.send('getCaCertificateTrust', [certId]);
131  };
132
133  /**
134   * Show the Import CA overlay.
135   * @param {string} certName The display name of the certificate.
136   * checkbox.
137   */
138  CertificateEditCaTrustOverlay.showImport = function(certName) {
139    var self = CertificateEditCaTrustOverlay.getInstance();
140    // TODO(mattm): do we want a view certificate button here like firefox has?
141    $('certificateEditCaTrustCancelButton').onclick = function(event) {
142      self.cancelImport_();
143    };
144    $('certificateEditCaTrustOkButton').onclick = function(event) {
145      self.finishImport_();
146    };
147    $('certificateEditCaTrustDescription').textContent =
148        loadTimeData.getStringF('certificateImportCaDescriptionFormat',
149                                certName);
150    CertificateEditCaTrustOverlay.populateTrust(false, false, false);
151    PageManager.showPageByName('certificateEditCaTrustOverlay');
152  };
153
154  CertificateEditCaTrustOverlay.dismiss = function() {
155    CertificateEditCaTrustOverlay.getInstance().dismissOverlay_();
156  };
157
158  // Export
159  return {
160    CertificateEditCaTrustOverlay: CertificateEditCaTrustOverlay
161  };
162});
163