MacViews: Use Mac's "Constrained Window Button" style for Button::STYLE_BUTTON LabelB...
[chromium-blink-merge.git] / chrome / browser / ui / views / ssl_client_certificate_selector.cc
blob1963b26447adc99a0216bc5923e50a56309794aa
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/views/ssl_client_certificate_selector.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "components/web_modal/popup_manager.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/client_certificate_delegate.h"
14 #include "content/public/browser/web_contents.h"
15 #include "net/cert/x509_certificate.h"
16 #include "net/ssl/ssl_cert_request_info.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/views/controls/label.h"
19 #include "ui/views/widget/widget.h"
21 #if defined(USE_NSS_CERTS)
22 #include "chrome/browser/ui/crypto_module_password_dialog_nss.h"
23 #endif
25 SSLClientCertificateSelector::SSLClientCertificateSelector(
26 content::WebContents* web_contents,
27 const scoped_refptr<net::SSLCertRequestInfo>& cert_request_info,
28 scoped_ptr<content::ClientCertificateDelegate> delegate)
29 : CertificateSelector(cert_request_info->client_certs, web_contents),
30 SSLClientAuthObserver(web_contents->GetBrowserContext(),
31 cert_request_info,
32 delegate.Pass()) {
35 SSLClientCertificateSelector::~SSLClientCertificateSelector() {
38 void SSLClientCertificateSelector::Init() {
39 StartObserving();
40 scoped_ptr<views::Label> text_label(
41 new views::Label(l10n_util::GetStringFUTF16(
42 IDS_CLIENT_CERT_DIALOG_TEXT,
43 base::ASCIIToUTF16(cert_request_info()->host_and_port.ToString()))));
44 text_label->SetMultiLine(true);
45 text_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
46 text_label->SetAllowCharacterBreak(true);
47 text_label->SizeToFit(kTableViewWidth);
48 InitWithText(text_label.Pass());
51 void SSLClientCertificateSelector::OnCertSelectedByNotification() {
52 GetWidget()->Close();
55 bool SSLClientCertificateSelector::Cancel() {
56 CertificateSelected(nullptr);
57 return true;
60 bool SSLClientCertificateSelector::Accept() {
61 scoped_refptr<net::X509Certificate> cert = GetSelectedCert();
62 if (cert.get()) {
63 // Remove the observer before we try unlocking, otherwise we might act on a
64 // notification while waiting for the unlock dialog, causing us to delete
65 // ourself before the Unlocked callback gets called.
66 StopObserving();
67 #if defined(USE_NSS_CERTS)
68 chrome::UnlockCertSlotIfNecessary(
69 cert.get(), chrome::kCryptoModulePasswordClientAuth,
70 cert_request_info()->host_and_port, GetWidget()->GetNativeView(),
71 base::Bind(&SSLClientCertificateSelector::Unlocked,
72 base::Unretained(this), cert));
73 #else
74 Unlocked(cert.get());
75 #endif
76 return false; // Unlocked() will close the dialog.
79 return false;
82 bool SSLClientCertificateSelector::Close() {
83 // By default, closing the dialog calls the Cancel method. However, selecting
84 // cancel in the UI currently continues the request with no certificate,
85 // remembering the selection. If the dialog is closed by closing the
86 // containing tab, the request should abort.
87 CancelCertificateSelection();
88 return true;
91 void SSLClientCertificateSelector::Unlocked(net::X509Certificate* cert) {
92 CertificateSelected(cert);
93 GetWidget()->Close();
96 namespace chrome {
98 void ShowSSLClientCertificateSelector(
99 content::WebContents* contents,
100 net::SSLCertRequestInfo* cert_request_info,
101 scoped_ptr<content::ClientCertificateDelegate> delegate) {
102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
104 // Not all WebContentses can show modal dialogs.
106 // TODO(davidben): Move this hook to the WebContentsDelegate and only try to
107 // show a dialog in Browser's implementation. https://crbug.com/456255
108 if (!SSLClientCertificateSelector::CanShow(contents))
109 return;
111 SSLClientCertificateSelector* selector = new SSLClientCertificateSelector(
112 contents, cert_request_info, delegate.Pass());
113 selector->Init();
114 selector->Show();
117 } // namespace chrome