Remove NativeWebContentsModalDialog and NativePopup typedefs.
[chromium-blink-merge.git] / chrome / browser / ui / webui / certificate_viewer_webui.cc
blob210ff3875bdd39ec46f8497bb4aa8502fdea2ada
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.
5 #include "chrome/browser/ui/webui/certificate_viewer_webui.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/i18n/time_formatting.h"
10 #include "base/json/json_writer.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/certificate_viewer.h"
14 #include "chrome/browser/platform_util.h"
15 #include "chrome/browser/ui/browser_dialogs.h"
16 #include "chrome/browser/ui/certificate_dialogs.h"
17 #include "chrome/browser/ui/webui/certificate_viewer_ui.h"
18 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
19 #include "chrome/common/net/x509_certificate_model.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "content/public/browser/web_contents.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/gfx/geometry/size.h"
26 using content::WebContents;
27 using content::WebUIMessageHandler;
29 // Shows a certificate using the WebUI certificate viewer.
30 void ShowCertificateViewer(WebContents* web_contents,
31 gfx::NativeWindow parent,
32 net::X509Certificate* cert) {
33 CertificateViewerDialog* dialog = new CertificateViewerDialog(cert);
34 dialog->Show(web_contents, parent);
37 ////////////////////////////////////////////////////////////////////////////////
38 // CertificateViewerDialog
40 CertificateViewerModalDialog::CertificateViewerModalDialog(
41 net::X509Certificate* cert)
42 : cert_(cert), webui_(NULL), window_(NULL) {
43 // Construct the dialog title from the certificate.
44 title_ = l10n_util::GetStringFUTF16(
45 IDS_CERT_INFO_DIALOG_TITLE,
46 base::UTF8ToUTF16(
47 x509_certificate_model::GetTitle(cert_->os_cert_handle())));
50 CertificateViewerModalDialog::~CertificateViewerModalDialog() {
53 void CertificateViewerModalDialog::Show(content::WebContents* web_contents,
54 gfx::NativeWindow parent) {
55 window_ = chrome::ShowWebDialog(parent,
56 web_contents->GetBrowserContext(),
57 this);
60 gfx::NativeWindow
61 CertificateViewerModalDialog::GetNativeWebContentsModalDialog() {
62 #if defined(USE_AURA)
63 return window_;
64 #else
65 NOTREACHED();
66 return NULL;
67 #endif
70 ui::ModalType CertificateViewerModalDialog::GetDialogModalType() const {
71 return ui::MODAL_TYPE_SYSTEM;
74 base::string16 CertificateViewerModalDialog::GetDialogTitle() const {
75 return title_;
78 GURL CertificateViewerModalDialog::GetDialogContentURL() const {
79 return GURL(chrome::kChromeUICertificateViewerDialogURL);
82 void CertificateViewerModalDialog::GetWebUIMessageHandlers(
83 std::vector<WebUIMessageHandler*>* handlers) const {
84 handlers->push_back(new CertificateViewerDialogHandler(
85 const_cast<CertificateViewerModalDialog*>(this), cert_.get()));
88 void CertificateViewerModalDialog::GetDialogSize(gfx::Size* size) const {
89 const int kDefaultWidth = 544;
90 const int kDefaultHeight = 628;
91 size->SetSize(kDefaultWidth, kDefaultHeight);
94 std::string CertificateViewerModalDialog::GetDialogArgs() const {
95 std::string data;
97 // Certificate information. The keys in this dictionary's general key
98 // correspond to the IDs in the Html page.
99 base::DictionaryValue cert_info;
100 net::X509Certificate::OSCertHandle cert_hnd = cert_->os_cert_handle();
102 // Get the certificate chain.
103 net::X509Certificate::OSCertHandles cert_chain;
104 cert_chain.push_back(cert_->os_cert_handle());
105 const net::X509Certificate::OSCertHandles& certs =
106 cert_->GetIntermediateCertificates();
107 cert_chain.insert(cert_chain.end(), certs.begin(), certs.end());
109 // Certificate usage.
110 std::vector<std::string> usages;
111 x509_certificate_model::GetUsageStrings(cert_hnd, &usages);
112 std::string usagestr;
113 for (std::vector<std::string>::iterator it = usages.begin();
114 it != usages.end(); ++it) {
115 if (usagestr.length() > 0) {
116 usagestr += "\n";
118 usagestr += *it;
120 cert_info.SetString("general.usages", usagestr);
122 // Standard certificate details.
123 const std::string alternative_text =
124 l10n_util::GetStringUTF8(IDS_CERT_INFO_FIELD_NOT_PRESENT);
125 cert_info.SetString("general.title", l10n_util::GetStringFUTF8(
126 IDS_CERT_INFO_DIALOG_TITLE,
127 base::UTF8ToUTF16(x509_certificate_model::GetTitle(
128 cert_chain.front()))));
130 // Issued to information.
131 cert_info.SetString("general.issued-cn",
132 x509_certificate_model::GetSubjectCommonName(cert_hnd, alternative_text));
133 cert_info.SetString("general.issued-o",
134 x509_certificate_model::GetSubjectOrgName(cert_hnd, alternative_text));
135 cert_info.SetString("general.issued-ou",
136 x509_certificate_model::GetSubjectOrgUnitName(cert_hnd,
137 alternative_text));
138 cert_info.SetString("general.issued-sn",
139 x509_certificate_model::GetSerialNumberHexified(cert_hnd,
140 alternative_text));
142 // Issuer information.
143 cert_info.SetString("general.issuer-cn",
144 x509_certificate_model::GetIssuerCommonName(cert_hnd, alternative_text));
145 cert_info.SetString("general.issuer-o",
146 x509_certificate_model::GetIssuerOrgName(cert_hnd, alternative_text));
147 cert_info.SetString("general.issuer-ou",
148 x509_certificate_model::GetIssuerOrgUnitName(cert_hnd, alternative_text));
150 // Validity period.
151 base::Time issued, expires;
152 std::string issued_str, expires_str;
153 if (x509_certificate_model::GetTimes(cert_hnd, &issued, &expires)) {
154 issued_str = base::UTF16ToUTF8(
155 base::TimeFormatShortDateNumeric(issued));
156 expires_str = base::UTF16ToUTF8(
157 base::TimeFormatShortDateNumeric(expires));
158 } else {
159 issued_str = alternative_text;
160 expires_str = alternative_text;
162 cert_info.SetString("general.issue-date", issued_str);
163 cert_info.SetString("general.expiry-date", expires_str);
165 cert_info.SetString("general.sha256",
166 x509_certificate_model::HashCertSHA256(cert_hnd));
167 cert_info.SetString("general.sha1",
168 x509_certificate_model::HashCertSHA1(cert_hnd));
170 // Certificate hierarchy is constructed from bottom up.
171 base::ListValue* children = NULL;
172 int index = 0;
173 for (net::X509Certificate::OSCertHandles::const_iterator i =
174 cert_chain.begin(); i != cert_chain.end(); ++i, ++index) {
175 base::DictionaryValue* cert_node = new base::DictionaryValue();
176 base::ListValue cert_details;
177 cert_node->SetString("label", x509_certificate_model::GetTitle(*i).c_str());
178 cert_node->SetDouble("payload.index", index);
179 // Add the child from the previous iteration.
180 if (children)
181 cert_node->Set("children", children);
183 // Add this node to the children list for the next iteration.
184 children = new base::ListValue();
185 children->Append(cert_node);
187 // Set the last node as the top of the certificate hierarchy.
188 cert_info.Set("hierarchy", children);
190 base::JSONWriter::Write(&cert_info, &data);
192 return data;
195 void CertificateViewerModalDialog::OnDialogShown(
196 content::WebUI* webui,
197 content::RenderViewHost* render_view_host) {
198 webui_ = webui;
201 void CertificateViewerModalDialog::OnDialogClosed(
202 const std::string& json_retval) {
205 void CertificateViewerModalDialog::OnCloseContents(WebContents* source,
206 bool* out_close_dialog) {
207 if (out_close_dialog)
208 *out_close_dialog = true;
211 bool CertificateViewerModalDialog::ShouldShowDialogTitle() const {
212 return true;
215 ////////////////////////////////////////////////////////////////////////////////
216 // CertificateViewerDialog
218 CertificateViewerDialog::CertificateViewerDialog(net::X509Certificate* cert)
219 : CertificateViewerModalDialog(cert),
220 dialog_(NULL) {
223 CertificateViewerDialog::~CertificateViewerDialog() {
226 void CertificateViewerDialog::Show(WebContents* web_contents,
227 gfx::NativeWindow parent) {
228 // TODO(bshe): UI tweaks needed for Aura HTML Dialog, such as adding padding
229 // on the title for Aura ConstrainedWebDialogUI.
230 dialog_ = ShowConstrainedWebDialog(web_contents->GetBrowserContext(), this,
231 web_contents);
234 gfx::NativeWindow CertificateViewerDialog::GetNativeWebContentsModalDialog() {
235 return dialog_->GetNativeDialog();
238 GURL CertificateViewerDialog::GetDialogContentURL() const {
239 return GURL(chrome::kChromeUICertificateViewerURL);
242 ui::ModalType CertificateViewerDialog::GetDialogModalType() const {
243 return ui::MODAL_TYPE_NONE;
246 ////////////////////////////////////////////////////////////////////////////////
247 // CertificateViewerDialogHandler
249 CertificateViewerDialogHandler::CertificateViewerDialogHandler(
250 CertificateViewerModalDialog* dialog,
251 net::X509Certificate* cert)
252 : cert_(cert), dialog_(dialog) {
253 cert_chain_.push_back(cert_->os_cert_handle());
254 const net::X509Certificate::OSCertHandles& certs =
255 cert_->GetIntermediateCertificates();
256 cert_chain_.insert(cert_chain_.end(), certs.begin(), certs.end());
259 CertificateViewerDialogHandler::~CertificateViewerDialogHandler() {
262 void CertificateViewerDialogHandler::RegisterMessages() {
263 web_ui()->RegisterMessageCallback("exportCertificate",
264 base::Bind(&CertificateViewerDialogHandler::ExportCertificate,
265 base::Unretained(this)));
266 web_ui()->RegisterMessageCallback("requestCertificateFields",
267 base::Bind(&CertificateViewerDialogHandler::RequestCertificateFields,
268 base::Unretained(this)));
271 void CertificateViewerDialogHandler::ExportCertificate(
272 const base::ListValue* args) {
273 int cert_index = GetCertificateIndex(args);
274 if (cert_index < 0)
275 return;
277 gfx::NativeWindow window =
278 platform_util::GetTopLevel(dialog_->GetNativeWebContentsModalDialog());
279 ShowCertExportDialog(web_ui()->GetWebContents(),
280 window,
281 cert_chain_.begin() + cert_index,
282 cert_chain_.end());
285 void CertificateViewerDialogHandler::RequestCertificateFields(
286 const base::ListValue* args) {
287 int cert_index = GetCertificateIndex(args);
288 if (cert_index < 0)
289 return;
291 net::X509Certificate::OSCertHandle cert = cert_chain_[cert_index];
293 base::ListValue root_list;
294 base::DictionaryValue* node_details;
295 base::DictionaryValue* alt_node_details;
296 base::ListValue* cert_sub_fields;
297 root_list.Append(node_details = new base::DictionaryValue());
298 node_details->SetString("label", x509_certificate_model::GetTitle(cert));
300 base::ListValue* cert_fields;
301 node_details->Set("children", cert_fields = new base::ListValue());
302 cert_fields->Append(node_details = new base::DictionaryValue());
304 node_details->SetString("label",
305 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE));
306 node_details->Set("children", cert_fields = new base::ListValue());
308 // Main certificate fields.
309 cert_fields->Append(node_details = new base::DictionaryValue());
310 node_details->SetString("label",
311 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VERSION));
312 std::string version = x509_certificate_model::GetVersion(cert);
313 if (!version.empty())
314 node_details->SetString("payload.val",
315 l10n_util::GetStringFUTF8(IDS_CERT_DETAILS_VERSION_FORMAT,
316 base::UTF8ToUTF16(version)));
318 cert_fields->Append(node_details = new base::DictionaryValue());
319 node_details->SetString("label",
320 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SERIAL_NUMBER));
321 node_details->SetString("payload.val",
322 x509_certificate_model::GetSerialNumberHexified(cert,
323 l10n_util::GetStringUTF8(IDS_CERT_INFO_FIELD_NOT_PRESENT)));
325 cert_fields->Append(node_details = new base::DictionaryValue());
326 node_details->SetString("label",
327 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG));
328 node_details->SetString("payload.val",
329 x509_certificate_model::ProcessSecAlgorithmSignature(cert));
331 cert_fields->Append(node_details = new base::DictionaryValue());
332 node_details->SetString("label",
333 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_ISSUER));
334 node_details->SetString("payload.val",
335 x509_certificate_model::GetIssuerName(cert));
337 // Validity period.
338 cert_fields->Append(node_details = new base::DictionaryValue());
339 node_details->SetString("label",
340 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VALIDITY));
342 node_details->Set("children", cert_sub_fields = new base::ListValue());
343 cert_sub_fields->Append(node_details = new base::DictionaryValue());
344 node_details->SetString("label",
345 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_BEFORE));
346 cert_sub_fields->Append(alt_node_details = new base::DictionaryValue());
347 alt_node_details->SetString("label",
348 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_AFTER));
349 base::Time issued, expires;
350 if (x509_certificate_model::GetTimes(cert, &issued, &expires)) {
351 node_details->SetString(
352 "payload.val",
353 base::UTF16ToUTF8(
354 base::TimeFormatShortDateAndTimeWithTimeZone(issued)));
355 alt_node_details->SetString(
356 "payload.val",
357 base::UTF16ToUTF8(
358 base::TimeFormatShortDateAndTimeWithTimeZone(expires)));
361 cert_fields->Append(node_details = new base::DictionaryValue());
362 node_details->SetString("label",
363 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT));
364 node_details->SetString("payload.val",
365 x509_certificate_model::GetSubjectName(cert));
367 // Subject key information.
368 cert_fields->Append(node_details = new base::DictionaryValue());
369 node_details->SetString("label",
370 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_INFO));
372 node_details->Set("children", cert_sub_fields = new base::ListValue());
373 cert_sub_fields->Append(node_details = new base::DictionaryValue());
374 node_details->SetString("label",
375 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_ALG));
376 node_details->SetString("payload.val",
377 x509_certificate_model::ProcessSecAlgorithmSubjectPublicKey(cert));
378 cert_sub_fields->Append(node_details = new base::DictionaryValue());
379 node_details->SetString("label",
380 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY));
381 node_details->SetString("payload.val",
382 x509_certificate_model::ProcessSubjectPublicKeyInfo(cert));
384 // Extensions.
385 x509_certificate_model::Extensions extensions;
386 x509_certificate_model::GetExtensions(
387 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_CRITICAL),
388 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_NON_CRITICAL),
389 cert, &extensions);
391 if (!extensions.empty()) {
392 cert_fields->Append(node_details = new base::DictionaryValue());
393 node_details->SetString("label",
394 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_EXTENSIONS));
396 node_details->Set("children", cert_sub_fields = new base::ListValue());
397 for (x509_certificate_model::Extensions::const_iterator i =
398 extensions.begin(); i != extensions.end(); ++i) {
399 cert_sub_fields->Append(node_details = new base::DictionaryValue());
400 node_details->SetString("label", i->name);
401 node_details->SetString("payload.val", i->value);
405 cert_fields->Append(node_details = new base::DictionaryValue());
406 node_details->SetString("label",
407 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG));
408 node_details->SetString("payload.val",
409 x509_certificate_model::ProcessSecAlgorithmSignatureWrap(cert));
411 cert_fields->Append(node_details = new base::DictionaryValue());
412 node_details->SetString("label",
413 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_VALUE));
414 node_details->SetString("payload.val",
415 x509_certificate_model::ProcessRawBitsSignatureWrap(cert));
417 cert_fields->Append(node_details = new base::DictionaryValue());
418 node_details->SetString("label",
419 l10n_util::GetStringUTF8(IDS_CERT_INFO_FINGERPRINTS_GROUP));
420 node_details->Set("children", cert_sub_fields = new base::ListValue());
422 cert_sub_fields->Append(node_details = new base::DictionaryValue());
423 node_details->SetString("label",
424 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA256_FINGERPRINT_LABEL));
425 node_details->SetString("payload.val",
426 x509_certificate_model::HashCertSHA256(cert));
427 cert_sub_fields->Append(node_details = new base::DictionaryValue());
428 node_details->SetString("label",
429 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA1_FINGERPRINT_LABEL));
430 node_details->SetString("payload.val",
431 x509_certificate_model::HashCertSHA1(cert));
433 // Send certificate information to javascript.
434 web_ui()->CallJavascriptFunction("cert_viewer.getCertificateFields",
435 root_list);
438 int CertificateViewerDialogHandler::GetCertificateIndex(
439 const base::ListValue* args) const {
440 int cert_index;
441 double val;
442 if (!(args->GetDouble(0, &val)))
443 return -1;
444 cert_index = static_cast<int>(val);
445 if (cert_index < 0 || cert_index >= static_cast<int>(cert_chain_.size()))
446 return -1;
447 return cert_index;