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