summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/ssl_client_certificate_selector.js
blob: fc6be54d6ca8d61d3d24ac3a7132ec40ce03ec5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('sslClientCertificateSelector', function() {
  'use strict';

  /**
   * Disables the controls while the dialog is busy.
   */
  function disableControls() {
    $('info').disabled = true;
    $('cancel').disabled = true;
    $('ok').disabled = true;
  }

  /**
   * Closes the dialog and passes a result value to the dialog close handler.
   * @param {number|null} result The value to pass to the dialog close handler,
   *     representing the index of the certificate to use or null if it is a
   *     Cancel operation.
   */
  function closeWithResult(result) {
    disableControls();
    var json = JSON.stringify([result]);
    chrome.send('DialogClose', [json]);
  }

  var details;

  /**
   * Updates the details area using the currently selected certificate.
   */
  function updateDetails() {
    var index = $('certificates').value;
    $('details').textContent = details[index];
  }

  /**
   * Gets the selected certificate index.
   * @return {number} The index of the selected certificate.
   */
  function selectedCertificateIndex() {
    return Number($('certificates').value);
  }

  /**
   * Shows the certificate viewer for the selected certificate.
   */
  function viewCertificate() {
    chrome.send('viewCertificate', [selectedCertificateIndex()]);
  }

  /**
   * Inserts translated strings on loading.
   */
  function initialize() {
    i18nTemplate.process(document, templateData);
    $('info').onclick = viewCertificate;
    $('cancel').onclick = function() {
      closeWithResult();  // No arguments means cancel.
    };
    $('ok').onclick = function() {
      closeWithResult(selectedCertificateIndex());
    };
    $('certificates').onchange = updateDetails;
    chrome.send('requestDetails');
  }

  /**
   * Adds elements to the DOM to populate the tab contents list box area of the
   * dialog. Substitutes the favicon source and title text from the details
   * using a template mechanism (clones hidden parts of the dialog DOM).
   * @param {{site: string, certificates: List<string>, details:
   *     List<string>}} dict Specifies the site (that is requesting a
   *     certificate), the display names of the certificates, and the details of
   *     the certificates.
   */
  function setDetails(dict) {
    if (dict.site) {
      $('site').textContent = dict.site;
    }
    var certificates = dict.certificates;
    if (certificates) {
      var numCertificates = certificates.length;
      var selectElement = $('certificates');
      for (var i = 0; i < numCertificates; i++) {
        var certificate = certificates[i];
        var optionElement = document.createElement('option');
        optionElement.value = i;
        optionElement.textContent = certificate;
        selectElement.appendChild(optionElement);
      }
    }
    if (dict.details) {
      details = dict.details;
    }
    updateDetails();
  }

  return {
    initialize: initialize,
    setDetails: setDetails,
  };
});

document.addEventListener('DOMContentLoaded',
                          sslClientCertificateSelector.initialize);