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 "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/widget/widget.h"
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 InitWithText(l10n_util::GetStringFUTF16(
40 IDS_CLIENT_CERT_DIALOG_TEXT
,
41 base::ASCIIToUTF16(cert_request_info()->host_and_port
.ToString())));
44 void SSLClientCertificateSelector::OnCertSelectedByNotification() {
48 bool SSLClientCertificateSelector::Cancel() {
49 CertificateSelected(nullptr);
53 bool SSLClientCertificateSelector::Accept() {
54 scoped_refptr
<net::X509Certificate
> cert
= GetSelectedCert();
56 // Remove the observer before we try unlocking, otherwise we might act on a
57 // notification while waiting for the unlock dialog, causing us to delete
58 // ourself before the Unlocked callback gets called.
61 chrome::UnlockCertSlotIfNecessary(
62 cert
.get(), chrome::kCryptoModulePasswordClientAuth
,
63 cert_request_info()->host_and_port
, GetWidget()->GetNativeView(),
64 base::Bind(&SSLClientCertificateSelector::Unlocked
,
65 base::Unretained(this), cert
));
69 return false; // Unlocked() will close the dialog.
75 bool SSLClientCertificateSelector::Close() {
76 // By default, closing the dialog calls the Cancel method. However, selecting
77 // cancel in the UI currently continues the request with no certificate,
78 // remembering the selection. If the dialog is closed by closing the
79 // containing tab, the request should abort.
80 CancelCertificateSelection();
84 void SSLClientCertificateSelector::Unlocked(net::X509Certificate
* cert
) {
85 CertificateSelected(cert
);
91 void ShowSSLClientCertificateSelector(
92 content::WebContents
* contents
,
93 net::SSLCertRequestInfo
* cert_request_info
,
94 scoped_ptr
<content::ClientCertificateDelegate
> delegate
) {
95 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
97 // Not all WebContentses can show modal dialogs.
99 // TODO(davidben): Move this hook to the WebContentsDelegate and only try to
100 // show a dialog in Browser's implementation. https://crbug.com/456255
101 if (web_modal::PopupManager::FromWebContents(contents
) == nullptr)
104 SSLClientCertificateSelector
* selector
= new SSLClientCertificateSelector(
105 contents
, cert_request_info
, delegate
.Pass());
110 } // namespace chrome