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