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"
8 #include "base/bind_helpers.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/client_certificate_delegate.h"
13 #include "content/public/browser/web_contents.h"
14 #include "net/cert/x509_certificate.h"
15 #include "net/ssl/ssl_cert_request_info.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/views/controls/label.h"
18 #include "ui/views/widget/widget.h"
20 #if defined(USE_NSS_CERTS)
21 #include "chrome/browser/ui/crypto_module_password_dialog_nss.h"
24 SSLClientCertificateSelector::SSLClientCertificateSelector(
25 content::WebContents
* web_contents
,
26 const scoped_refptr
<net::SSLCertRequestInfo
>& cert_request_info
,
27 scoped_ptr
<content::ClientCertificateDelegate
> delegate
)
28 : CertificateSelector(cert_request_info
->client_certs
, web_contents
),
29 SSLClientAuthObserver(web_contents
->GetBrowserContext(),
34 SSLClientCertificateSelector::~SSLClientCertificateSelector() {
37 void SSLClientCertificateSelector::Init() {
39 scoped_ptr
<views::Label
> text_label(
40 new views::Label(l10n_util::GetStringFUTF16(
41 IDS_CLIENT_CERT_DIALOG_TEXT
,
42 base::ASCIIToUTF16(cert_request_info()->host_and_port
.ToString()))));
43 text_label
->SetMultiLine(true);
44 text_label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
45 text_label
->SetAllowCharacterBreak(true);
46 text_label
->SizeToFit(kTableViewWidth
);
47 InitWithText(text_label
.Pass());
50 void SSLClientCertificateSelector::OnCertSelectedByNotification() {
54 bool SSLClientCertificateSelector::Cancel() {
55 CertificateSelected(nullptr);
59 bool SSLClientCertificateSelector::Accept() {
60 scoped_refptr
<net::X509Certificate
> cert
= GetSelectedCert();
62 // Remove the observer before we try unlocking, otherwise we might act on a
63 // notification while waiting for the unlock dialog, causing us to delete
64 // ourself before the Unlocked callback gets called.
66 #if defined(USE_NSS_CERTS)
67 chrome::UnlockCertSlotIfNecessary(
68 cert
.get(), chrome::kCryptoModulePasswordClientAuth
,
69 cert_request_info()->host_and_port
, GetWidget()->GetNativeView(),
70 base::Bind(&SSLClientCertificateSelector::Unlocked
,
71 base::Unretained(this), cert
));
75 return false; // Unlocked() will close the dialog.
81 bool SSLClientCertificateSelector::Close() {
82 // By default, closing the dialog calls the Cancel method. However, selecting
83 // cancel in the UI currently continues the request with no certificate,
84 // remembering the selection. If the dialog is closed by closing the
85 // containing tab, the request should abort.
86 CancelCertificateSelection();
90 void SSLClientCertificateSelector::Unlocked(net::X509Certificate
* cert
) {
91 CertificateSelected(cert
);
97 void ShowSSLClientCertificateSelector(
98 content::WebContents
* contents
,
99 net::SSLCertRequestInfo
* cert_request_info
,
100 scoped_ptr
<content::ClientCertificateDelegate
> delegate
) {
101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
103 // Not all WebContentses can show modal dialogs.
105 // TODO(davidben): Move this hook to the WebContentsDelegate and only try to
106 // show a dialog in Browser's implementation. https://crbug.com/456255
107 if (!SSLClientCertificateSelector::CanShow(contents
))
110 SSLClientCertificateSelector
* selector
= new SSLClientCertificateSelector(
111 contents
, cert_request_info
, delegate
.Pass());
116 } // namespace chrome