Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/platform_keys_certificate_selector_chromeos.h" | 5 #include "chrome/browser/ui/views/platform_keys_certificate_selector_chromeos.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 11 #include "base/memory/ptr_util.h" | |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/ui/browser_dialogs.h" | 15 #include "chrome/browser/ui/browser_dialogs.h" |
| 15 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "net/ssl/client_cert_identity.h" | |
| 18 #include "net/ssl/ssl_private_key.h" | |
| 16 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/gfx/font.h" | 20 #include "ui/gfx/font.h" |
| 18 #include "ui/views/controls/styled_label.h" | 21 #include "ui/views/controls/styled_label.h" |
| 19 | 22 |
| 20 namespace chromeos { | 23 namespace chromeos { |
| 21 | 24 |
| 25 namespace { | |
| 26 | |
| 27 // Fake ClientCertIdentity that does not support retrieving the private key. | |
| 28 // The platformKeys API currently only deals in certificates, not identities. | |
| 29 // Looking up the private key by the certificate is done as a separate step. | |
| 30 class ClientCertIdentityPlatformKeys : public net::ClientCertIdentity { | |
| 31 public: | |
| 32 explicit ClientCertIdentityPlatformKeys( | |
| 33 scoped_refptr<net::X509Certificate> cert) | |
| 34 : net::ClientCertIdentity(std::move(cert)) {} | |
| 35 ~ClientCertIdentityPlatformKeys() override = default; | |
| 36 | |
| 37 void AcquirePrivateKey( | |
| 38 const base::Callback<void(scoped_refptr<net::SSLPrivateKey>)>& | |
| 39 private_key_callback) override { | |
| 40 NOTREACHED(); | |
| 41 } | |
| 42 }; | |
| 43 | |
| 44 net::ClientCertIdentityList CertificateListToIdentityList( | |
| 45 const net::CertificateList& certs) { | |
| 46 net::ClientCertIdentityList identities; | |
| 47 for (const auto& cert : certs) { | |
| 48 identities.push_back( | |
| 49 base::MakeUnique<ClientCertIdentityPlatformKeys>(cert)); | |
| 50 } | |
| 51 return identities; | |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 22 PlatformKeysCertificateSelector::PlatformKeysCertificateSelector( | 56 PlatformKeysCertificateSelector::PlatformKeysCertificateSelector( |
| 23 const net::CertificateList& certificates, | 57 const net::CertificateList& certificates, |
| 24 const std::string& extension_name, | 58 const std::string& extension_name, |
| 25 const CertificateSelectedCallback& callback, | 59 const CertificateSelectedCallback& callback, |
| 26 content::WebContents* web_contents) | 60 content::WebContents* web_contents) |
| 27 : CertificateSelector(certificates, web_contents), | 61 : CertificateSelector(CertificateListToIdentityList(certificates), |
| 62 web_contents), | |
| 28 extension_name_(extension_name), | 63 extension_name_(extension_name), |
| 29 callback_(callback) { | 64 callback_(callback) { |
| 30 DCHECK(!callback_.is_null()); | 65 DCHECK(!callback_.is_null()); |
| 31 chrome::RecordDialogCreation( | 66 chrome::RecordDialogCreation( |
| 32 chrome::DialogIdentifier::PLATFORM_KEYS_CERTIFICATE_SELECTOR); | 67 chrome::DialogIdentifier::PLATFORM_KEYS_CERTIFICATE_SELECTOR); |
| 33 } | 68 } |
| 34 | 69 |
| 35 PlatformKeysCertificateSelector::~PlatformKeysCertificateSelector() { | 70 PlatformKeysCertificateSelector::~PlatformKeysCertificateSelector() { |
| 36 // Ensure to call back even if the dialog was closed because of the views | 71 // Ensure to call back even if the dialog was closed because of the views |
| 37 // hierarchy being destroyed. | 72 // hierarchy being destroyed. |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 54 label->AddStyleRange(gfx::Range(offset, offset + name.size()), bold_style); | 89 label->AddStyleRange(gfx::Range(offset, offset + name.size()), bold_style); |
| 55 CertificateSelector::InitWithText(std::move(label)); | 90 CertificateSelector::InitWithText(std::move(label)); |
| 56 } | 91 } |
| 57 | 92 |
| 58 bool PlatformKeysCertificateSelector::Cancel() { | 93 bool PlatformKeysCertificateSelector::Cancel() { |
| 59 DCHECK(!callback_.is_null()); | 94 DCHECK(!callback_.is_null()); |
| 60 base::ResetAndReturn(&callback_).Run(nullptr); | 95 base::ResetAndReturn(&callback_).Run(nullptr); |
| 61 return true; | 96 return true; |
| 62 } | 97 } |
| 63 | 98 |
| 64 bool PlatformKeysCertificateSelector::Accept() { | 99 bool PlatformKeysCertificateSelector::AcceptCertificate( |
| 100 std::unique_ptr<net::ClientCertIdentity> identity) { | |
| 65 DCHECK(!callback_.is_null()); | 101 DCHECK(!callback_.is_null()); |
| 66 scoped_refptr<net::X509Certificate> cert = GetSelectedCert(); | 102 base::ResetAndReturn(&callback_) |
| 67 if (!cert) | 103 .Run(make_scoped_refptr(identity->certificate())); |
|
Peter Kasting
2017/06/16 23:29:52
Possible future improvement: Switch the callback t
mattm
2017/06/17 03:20:07
Acknowledged.
| |
| 68 return false; | |
| 69 base::ResetAndReturn(&callback_).Run(cert); | |
| 70 return true; | 104 return true; |
| 71 } | 105 } |
| 72 | 106 |
| 73 void ShowPlatformKeysCertificateSelector( | 107 void ShowPlatformKeysCertificateSelector( |
| 74 content::WebContents* web_contents, | 108 content::WebContents* web_contents, |
| 75 const std::string& extension_name, | 109 const std::string& extension_name, |
| 76 const net::CertificateList& certificates, | 110 const net::CertificateList& certificates, |
| 77 const base::Callback<void(const scoped_refptr<net::X509Certificate>&)>& | 111 const base::Callback<void(const scoped_refptr<net::X509Certificate>&)>& |
| 78 callback) { | 112 callback) { |
| 79 PlatformKeysCertificateSelector* selector = | 113 PlatformKeysCertificateSelector* selector = |
| 80 new PlatformKeysCertificateSelector(certificates, extension_name, | 114 new PlatformKeysCertificateSelector(certificates, extension_name, |
| 81 callback, web_contents); | 115 callback, web_contents); |
| 82 selector->Init(); | 116 selector->Init(); |
| 83 selector->Show(); | 117 selector->Show(); |
| 84 } | 118 } |
| 85 | 119 |
| 86 } // namespace chromeos | 120 } // namespace chromeos |
| OLD | NEW |