[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 74b962a | 2011-06-03 21:22:54 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ |
| 6 | #define CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 8 | |
| 9 | #include "base/basictypes.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
[email protected] | 8d128d6 | 2011-09-13 22:11:57 | [diff] [blame] | 11 | #include "content/common/content_export.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame^] | 12 | #include "content/public/browser/browser_thread.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 13 | #include "content/public/browser/notification_observer.h" |
| 14 | #include "content/public/browser/notification_registrar.h" |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 15 | #include "net/base/ssl_cert_request_info.h" |
| 16 | |
| 17 | namespace net { |
[email protected] | fe4fb43 | 2011-10-20 22:43:42 | [diff] [blame] | 18 | class HttpNetworkSession; |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 19 | class URLRequest; |
[email protected] | edfe7fab | 2010-11-28 13:11:52 | [diff] [blame] | 20 | class X509Certificate; |
| 21 | } // namespace net |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 22 | |
| 23 | // This class handles the approval and selection of a certificate for SSL client |
| 24 | // authentication by the user. |
| 25 | // It is self-owned and deletes itself when the UI reports the user selection or |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 26 | // when the net::URLRequest is cancelled. |
[email protected] | 8d128d6 | 2011-09-13 22:11:57 | [diff] [blame] | 27 | class CONTENT_EXPORT SSLClientAuthHandler |
[email protected] | 1f18184a | 2010-07-21 19:34:49 | [diff] [blame] | 28 | : public base::RefCountedThreadSafe<SSLClientAuthHandler, |
[email protected] | 9a7e150 | 2010-10-08 04:03:50 | [diff] [blame] | 29 | BrowserThread::DeleteOnIOThread> { |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 30 | public: |
[email protected] | edfe7fab | 2010-11-28 13:11:52 | [diff] [blame] | 31 | SSLClientAuthHandler(net::URLRequest* request, |
[email protected] | 0d3dc8e2 | 2009-11-03 02:27:01 | [diff] [blame] | 32 | net::SSLCertRequestInfo* cert_request_info); |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 33 | |
[email protected] | c99c442e | 2011-08-24 11:37:30 | [diff] [blame] | 34 | // Selects a certificate and resumes the URL request with that certificate. |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 35 | // Should only be called on the IO thread. |
| 36 | void SelectCertificate(); |
| 37 | |
| 38 | // Invoked when the request associated with this handler is cancelled. |
| 39 | // Should only be called on the IO thread. |
| 40 | void OnRequestCancelled(); |
| 41 | |
[email protected] | b1f18494 | 2010-03-04 01:46:57 | [diff] [blame] | 42 | // Calls DoCertificateSelected on the I/O thread. |
| 43 | // Called on the UI thread after the user has made a selection (which may |
| 44 | // be long after DoSelectCertificate returns, if the UI is modeless/async.) |
| 45 | void CertificateSelected(net::X509Certificate* cert); |
| 46 | |
[email protected] | d39dbf1 | 2011-04-18 23:37:31 | [diff] [blame] | 47 | // Like CertificateSelected, but does not send SSL_CLIENT_AUTH_CERT_SELECTED |
| 48 | // notification. Used to avoid notification re-spamming when other |
| 49 | // certificate selectors act on a notification matching the same host. |
[email protected] | f245542 | 2011-07-21 02:56:58 | [diff] [blame] | 50 | virtual void CertificateSelectedNoNotify(net::X509Certificate* cert); |
[email protected] | d39dbf1 | 2011-04-18 23:37:31 | [diff] [blame] | 51 | |
[email protected] | 1f18184a | 2010-07-21 19:34:49 | [diff] [blame] | 52 | // Returns the SSLCertRequestInfo for this handler. |
| 53 | net::SSLCertRequestInfo* cert_request_info() { return cert_request_info_; } |
| 54 | |
[email protected] | fe4fb43 | 2011-10-20 22:43:42 | [diff] [blame] | 55 | // Returns the session the URL request is associated with. |
| 56 | const net::HttpNetworkSession* http_network_session() const { |
| 57 | return http_network_session_; |
| 58 | } |
| 59 | |
[email protected] | f245542 | 2011-07-21 02:56:58 | [diff] [blame] | 60 | protected: |
| 61 | virtual ~SSLClientAuthHandler(); |
| 62 | |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 63 | private: |
[email protected] | 8d128d6 | 2011-09-13 22:11:57 | [diff] [blame] | 64 | friend class base::RefCountedThreadSafe<SSLClientAuthHandler, |
| 65 | BrowserThread::DeleteOnIOThread>; |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 66 | friend class BrowserThread; |
[email protected] | 1f18184a | 2010-07-21 19:34:49 | [diff] [blame] | 67 | friend class DeleteTask<SSLClientAuthHandler>; |
[email protected] | e6e6ba4 | 2009-11-07 01:56:19 | [diff] [blame] | 68 | |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 69 | // Notifies that the user has selected a cert. |
| 70 | // Called on the IO thread. |
[email protected] | 3e1fc8e | 2010-02-18 22:45:05 | [diff] [blame] | 71 | void DoCertificateSelected(net::X509Certificate* cert); |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 72 | |
[email protected] | c99c442e | 2011-08-24 11:37:30 | [diff] [blame] | 73 | // Selects a client certificate on the UI thread. |
| 74 | void DoSelectCertificate(int render_process_host_id, |
| 75 | int render_view_host_id); |
[email protected] | c6176905 | 2011-05-18 18:38:35 | [diff] [blame] | 76 | |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 77 | // The net::URLRequest that triggered this client auth. |
[email protected] | edfe7fab | 2010-11-28 13:11:52 | [diff] [blame] | 78 | net::URLRequest* request_; |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 79 | |
[email protected] | fe4fb43 | 2011-10-20 22:43:42 | [diff] [blame] | 80 | // The HttpNetworkSession |request_| is associated with. |
| 81 | const net::HttpNetworkSession* http_network_session_; |
| 82 | |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 83 | // The certs to choose from. |
| 84 | scoped_refptr<net::SSLCertRequestInfo> cert_request_info_; |
| 85 | |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 86 | DISALLOW_COPY_AND_ASSIGN(SSLClientAuthHandler); |
| 87 | }; |
| 88 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 89 | class CONTENT_EXPORT SSLClientAuthObserver |
| 90 | : public content::NotificationObserver { |
[email protected] | d39dbf1 | 2011-04-18 23:37:31 | [diff] [blame] | 91 | public: |
| 92 | SSLClientAuthObserver(net::SSLCertRequestInfo* cert_request_info, |
| 93 | SSLClientAuthHandler* handler); |
| 94 | virtual ~SSLClientAuthObserver(); |
| 95 | |
| 96 | // UI should implement this to close the dialog. |
| 97 | virtual void OnCertSelectedByNotification() = 0; |
| 98 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 99 | // content::NotificationObserver implementation: |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 100 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 101 | const content::NotificationSource& source, |
| 102 | const content::NotificationDetails& details); |
[email protected] | d39dbf1 | 2011-04-18 23:37:31 | [diff] [blame] | 103 | |
| 104 | // Begins observing notifications from other SSLClientAuthHandler instances. |
| 105 | // If another instance chooses a cert for a matching SSLCertRequestInfo, we |
| 106 | // will also use the same cert and OnCertSelectedByNotification will be called |
| 107 | // so that the cert selection UI can be closed. |
| 108 | void StartObserving(); |
| 109 | |
| 110 | // Stops observing notifications. We will no longer act on client auth |
| 111 | // notifications. |
| 112 | void StopObserving(); |
| 113 | |
| 114 | private: |
| 115 | scoped_refptr<net::SSLCertRequestInfo> cert_request_info_; |
| 116 | |
| 117 | scoped_refptr<SSLClientAuthHandler> handler_; |
| 118 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 119 | content::NotificationRegistrar notification_registrar_; |
[email protected] | d39dbf1 | 2011-04-18 23:37:31 | [diff] [blame] | 120 | |
| 121 | DISALLOW_COPY_AND_ASSIGN(SSLClientAuthObserver); |
| 122 | }; |
| 123 | |
[email protected] | 74b962a | 2011-06-03 21:22:54 | [diff] [blame] | 124 | #endif // CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ |