Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / views / ssl_client_certificate_selector.cc
blob8222614d980fbc6427672ee5ee1debb87ee458b4
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/widget/widget.h"
20 #if defined(USE_NSS)
21 #include "chrome/browser/ui/crypto_module_password_dialog_nss.h"
22 #endif
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(),
30 cert_request_info,
31 delegate.Pass()) {
34 SSLClientCertificateSelector::~SSLClientCertificateSelector() {
37 void SSLClientCertificateSelector::Init() {
38 StartObserving();
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() {
45 GetWidget()->Close();
48 bool SSLClientCertificateSelector::Cancel() {
49 CertificateSelected(nullptr);
50 return true;
53 bool SSLClientCertificateSelector::Accept() {
54 scoped_refptr<net::X509Certificate> cert = GetSelectedCert();
55 if (cert.get()) {
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.
59 StopObserving();
60 #if defined(USE_NSS)
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));
66 #else
67 Unlocked(cert.get());
68 #endif
69 return false; // Unlocked() will close the dialog.
72 return false;
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();
81 return true;
84 void SSLClientCertificateSelector::Unlocked(net::X509Certificate* cert) {
85 CertificateSelected(cert);
86 GetWidget()->Close();
89 namespace chrome {
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)
102 return;
104 SSLClientCertificateSelector* selector = new SSLClientCertificateSelector(
105 contents, cert_request_info, delegate.Pass());
106 selector->Init();
107 selector->Show();
110 } // namespace chrome