summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/certificate_viewer.js
blob: 95a5d821f66e43daa6e81354d81cdfdc55b6ddaa (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2012 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('cert_viewer', function() {
  'use strict';

  /**
   * Initialize the certificate viewer dialog by wiring up the close button,
   * substituting in translated strings and requesting certificate details.
   */
  function initialize() {
    $('export').onclick = exportCertificate;
    cr.ui.decorate('tabbox', cr.ui.TabBox);

    initializeTree($('hierarchy'), showCertificateFields);
    initializeTree($('cert-fields'), showCertificateFieldValue);

    i18nTemplate.process(document, templateData);
    stripGtkAccessorKeys();
    chrome.send('requestCertificateInfo');
    // TODO(kochi): ESC key should be handled in the views window side.
    document.addEventListener('keydown', function(e) {
      if (e.keyCode == 27)  // ESC
        chrome.send('DialogClose');
    });
  }

  /**
   * Initialize a cr.ui.Tree object from a given element using the specified
   * change handler.
   * @param {HTMLElement} tree The HTMLElement to style as a tree.
   * @param {function()} handler Function to call when a node is selected.
   */
  function initializeTree(tree, handler) {
    cr.ui.decorate(tree, cr.ui.Tree);
    tree.detail = {payload: {}, children: {}};
    tree.addEventListener('change', handler);
  }

  /**
   * The tab name strings in the languages file have accessor keys indicated
   * by a preceding & sign. Strip these out for now.
   * TODO(flackr) These accessor keys could be implemented with Javascript or
   *     translated strings could be added / modified to remove the & sign.
   */
  function stripGtkAccessorKeys() {
    // Copy all the tab labels into an array.
    var nodes = Array.prototype.slice.call($('tabs').childNodes, 0);
    nodes.push($('export'));
    for (var i = 0; i < nodes.length; i++)
      nodes[i].textContent = nodes[i].textContent.replace('&', '');
  }

  /**
   * Expand all nodes of the given tree object.
   * @param {cr.ui.Tree} tree The tree object to expand all nodes on.
   */
  function revealTree(tree) {
    tree.expanded = true;
    for (var key in tree.detail.children) {
      revealTree(tree.detail.children[key]);
    }
  }

  /**
   * This function is called from certificate_viewer_ui.cc with the certificate
   * information. Display all returned information to the user.
   * @param {Object} certInfo Certificate information in named fields.
   */
  function getCertificateInfo(certInfo) {
    for (var key in certInfo.general) {
      $(key).textContent = certInfo.general[key];
    }
    createCertificateHierarchy(certInfo.hierarchy);
  }

  /**
   * This function populates the certificate hierarchy.
   * @param {Object} hierarchy A dictionary containing the hierarchy.
   */
  function createCertificateHierarchy(hierarchy) {
    var treeItem = $('hierarchy');
    var root = constructTree(hierarchy[0]);
    treeItem.detail.children['root'] = root;
    treeItem.add(root);

    // Select the last item in the hierarchy (really we have a list here - each
    // node has at most one child).  This will reveal the parent nodes and
    // populate the fields view.
    var last = root;
    while (last.detail.children && last.detail.children[0])
      last = last.detail.children[0];
    last.selected = true;
  }

  /**
   * Constructs a cr.ui.TreeItem corresponding to the passed in tree
   * @param {Object} tree Dictionary describing the tree structure.
   * @return {cr.ui.TreeItem} Tree node corresponding to the input dictionary.
   */
  function constructTree(tree)
  {
    var treeItem = new cr.ui.TreeItem({
        label: tree.label,
        detail: {payload: tree.payload ? tree.payload : {},
            children: {}
        }});
    if (tree.children) {
      for (var i = 0; i < tree.children.length; i++) {
        treeItem.add(treeItem.detail.children[i] =
            constructTree(tree.children[i]));
      }
    }
    return treeItem;
  }

  /**
   * Clear any previous certificate fields in the tree.
   */
  function clearCertificateFields() {
    var treeItem = $('cert-fields');
    for (var key in treeItem.detail.children) {
      treeItem.remove(treeItem.detail.children[key]);
      delete treeItem.detail.children[key];
    }
  }

  /**
   * Request certificate fields for the selected certificate in the hierarchy.
   */
  function showCertificateFields() {
    clearCertificateFields();
    var item = $('hierarchy').selectedItem;
    if (item && item.detail.payload.index !== undefined)
      chrome.send('requestCertificateFields', [item.detail.payload.index]);
  }

  /**
   * Show the returned certificate fields for the selected certificate.
   * @param {Object} certFields A dictionary containing the fields tree
   *     structure.
   */
  function getCertificateFields(certFields) {
    clearCertificateFields();
    var treeItem = $('cert-fields');
    treeItem.add(treeItem.detail.children['root'] =
        constructTree(certFields[0]));
    revealTree(treeItem);
    // Ensure the list is scrolled  to the top by selecting the first item.
    treeItem.children[0].selected = true;
  }

  /**
   * Show certificate field value for a selected certificate field.
   */
  function showCertificateFieldValue() {
    var item = $('cert-fields').selectedItem;
    if (item && item.detail.payload.val)
      $('cert-field-value').textContent = item.detail.payload.val;
    else
      $('cert-field-value').textContent = '';
  }

  /**
   * Export the selected certificate.
   */
  function exportCertificate() {
    var item = $('hierarchy').selectedItem;
    if (item && item.detail.payload.index !== undefined)
      chrome.send('exportCertificate', [item.detail.payload.index]);
  }

  return {
    initialize: initialize,
    getCertificateInfo: getCertificateInfo,
    getCertificateFields: getCertificateFields,
  };
});

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