OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ | 5 #ifndef CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ |
6 #define CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ | 6 #define CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "net/ssl/ssl_cert_request_info.h" | 13 #include "net/ssl/ssl_cert_request_info.h" |
14 | 14 |
15 namespace net { | 15 namespace net { |
16 class ClientCertStore; | 16 class ClientCertStore; |
17 class URLRequest; | 17 class URLRequest; |
18 class X509Certificate; | 18 class X509Certificate; |
19 } // namespace net | 19 } // namespace net |
20 | 20 |
21 namespace content { | 21 namespace content { |
22 | 22 |
23 // This class handles the approval and selection of a certificate for SSL client | 23 // This class handles the approval and selection of a certificate for SSL client |
24 // authentication by the user. Should only be used on the IO thread. If the | 24 // authentication by the user. Should only be used on the IO thread. If the |
25 // SSLClientAuthHandler is destroyed before the certificate is selected, the | 25 // SSLClientAuthHandler is destroyed before the certificate is selected, the |
26 // selection is canceled and the callback never called. | 26 // selection is canceled and the delegate never called. |
27 class SSLClientAuthHandler { | 27 class SSLClientAuthHandler { |
28 public: | 28 public: |
29 using CertificateCallback = base::Callback<void(net::X509Certificate*)>; | 29 // Delegate interface for SSLClientAuthHandler. Method implementations may |
| 30 // delete the handler when called. |
| 31 class Delegate { |
| 32 public: |
| 33 // Called to continue the request with |cert|. |cert| may be nullptr. |
| 34 virtual void ContinueWithCertificate(net::X509Certificate* cert) = 0; |
| 35 |
| 36 // Called to cancel the certificate selection and abort the request. |
| 37 virtual void CancelCertificateSelection() = 0; |
| 38 }; |
30 | 39 |
31 SSLClientAuthHandler(scoped_ptr<net::ClientCertStore> client_cert_store, | 40 SSLClientAuthHandler(scoped_ptr<net::ClientCertStore> client_cert_store, |
32 net::URLRequest* request, | 41 net::URLRequest* request, |
33 net::SSLCertRequestInfo* cert_request_info, | 42 net::SSLCertRequestInfo* cert_request_info, |
34 const CertificateCallback& callback); | 43 Delegate* delegate); |
35 ~SSLClientAuthHandler(); | 44 ~SSLClientAuthHandler(); |
36 | 45 |
37 // Selects a certificate and resumes the URL request with that certificate. | 46 // Selects a certificate and resumes the URL request with that certificate. |
38 void SelectCertificate(); | 47 void SelectCertificate(); |
39 | 48 |
| 49 // Called to continue the request associated with |handler| using |cert|. This |
| 50 // is static to avoid deleting |handler| while it is on the stack. |
| 51 static void ContinueWithCertificate( |
| 52 const base::WeakPtr<SSLClientAuthHandler>& handler, |
| 53 net::X509Certificate* cert); |
| 54 |
| 55 // Called to abort the request associated with |handler|. This is static to |
| 56 // avoid deleting |handler| while it is on the stack. |
| 57 static void CancelCertificateSelection( |
| 58 const base::WeakPtr<SSLClientAuthHandler>& handler); |
| 59 |
40 private: | 60 private: |
41 class Core; | 61 class Core; |
42 | 62 |
43 // Called when |core_| is done retrieving the cert list. | 63 // Called when |core_| is done retrieving the cert list. |
44 void DidGetClientCerts(); | 64 void DidGetClientCerts(); |
45 | 65 |
46 // Called when the user has selected a cert. If the user chose to continue | |
47 // with no certificate, |cert| is NULL. | |
48 void CertificateSelected(net::X509Certificate* cert); | |
49 | |
50 // A reference-counted core so the ClientCertStore may outlive | 66 // A reference-counted core so the ClientCertStore may outlive |
51 // SSLClientAuthHandler if the handler is destroyed while an operation on the | 67 // SSLClientAuthHandler if the handler is destroyed while an operation on the |
52 // ClientCertStore is in progress. | 68 // ClientCertStore is in progress. |
53 scoped_refptr<Core> core_; | 69 scoped_refptr<Core> core_; |
54 | 70 |
55 // The net::URLRequest that triggered this client auth. | 71 // The net::URLRequest that triggered this client auth. |
56 net::URLRequest* request_; | 72 net::URLRequest* request_; |
57 | 73 |
58 // The certs to choose from. | 74 // The certs to choose from. |
59 scoped_refptr<net::SSLCertRequestInfo> cert_request_info_; | 75 scoped_refptr<net::SSLCertRequestInfo> cert_request_info_; |
60 | 76 |
61 // The callback to call when the certificate is selected. | 77 // The delegate to call back with the result. The caller ensures the delegate |
62 CertificateCallback callback_; | 78 // does not outlive SSLClientAuthHandler. |
| 79 Delegate* delegate_; |
63 | 80 |
64 base::WeakPtrFactory<SSLClientAuthHandler> weak_factory_; | 81 base::WeakPtrFactory<SSLClientAuthHandler> weak_factory_; |
65 | 82 |
66 DISALLOW_COPY_AND_ASSIGN(SSLClientAuthHandler); | 83 DISALLOW_COPY_AND_ASSIGN(SSLClientAuthHandler); |
67 }; | 84 }; |
68 | 85 |
69 } // namespace content | 86 } // namespace content |
70 | 87 |
71 #endif // CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ | 88 #endif // CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ |
OLD | NEW |