| OLD | NEW |
| 1 // Copyright (c) 2011 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 CHROME_BROWSER_SSL_SSL_CLIENT_AUTH_OBSERVER_H_ |
| 6 #define CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ | 6 #define CHROME_BROWSER_SSL_SSL_CLIENT_AUTH_OBSERVER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/callback.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/message_loop_helpers.h" | |
| 12 #include "content/common/content_export.h" | |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 #include "content/public/browser/notification_observer.h" | 11 #include "content/public/browser/notification_observer.h" |
| 15 #include "content/public/browser/notification_registrar.h" | 12 #include "content/public/browser/notification_registrar.h" |
| 16 #include "net/base/ssl_cert_request_info.h" | |
| 17 | 13 |
| 18 namespace net { | 14 namespace net { |
| 19 class HttpNetworkSession; | 15 class HttpNetworkSession; |
| 20 class URLRequest; | 16 class SSLCertRequestInfo; |
| 21 class X509Certificate; | 17 class X509Certificate; |
| 22 } // namespace net | 18 } |
| 23 | 19 |
| 24 // This class handles the approval and selection of a certificate for SSL client | 20 class SSLClientAuthObserver : public content::NotificationObserver { |
| 25 // authentication by the user. | |
| 26 // It is self-owned and deletes itself when the UI reports the user selection or | |
| 27 // when the net::URLRequest is cancelled. | |
| 28 class CONTENT_EXPORT SSLClientAuthHandler | |
| 29 : public base::RefCountedThreadSafe< | |
| 30 SSLClientAuthHandler, content::BrowserThread::DeleteOnIOThread> { | |
| 31 public: | 21 public: |
| 32 SSLClientAuthHandler(net::URLRequest* request, | 22 SSLClientAuthObserver( |
| 33 net::SSLCertRequestInfo* cert_request_info); | 23 const net::HttpNetworkSession* network_session, |
| 34 | 24 net::SSLCertRequestInfo* cert_request_info, |
| 35 // Selects a certificate and resumes the URL request with that certificate. | 25 const base::Callback<void(net::X509Certificate*)>& callback); |
| 36 // Should only be called on the IO thread. | |
| 37 void SelectCertificate(); | |
| 38 | |
| 39 // Invoked when the request associated with this handler is cancelled. | |
| 40 // Should only be called on the IO thread. | |
| 41 void OnRequestCancelled(); | |
| 42 | |
| 43 // Calls DoCertificateSelected on the I/O thread. | |
| 44 // Called on the UI thread after the user has made a selection (which may | |
| 45 // be long after DoSelectCertificate returns, if the UI is modeless/async.) | |
| 46 void CertificateSelected(net::X509Certificate* cert); | |
| 47 | |
| 48 // Like CertificateSelected, but does not send SSL_CLIENT_AUTH_CERT_SELECTED | |
| 49 // notification. Used to avoid notification re-spamming when other | |
| 50 // certificate selectors act on a notification matching the same host. | |
| 51 virtual void CertificateSelectedNoNotify(net::X509Certificate* cert); | |
| 52 | |
| 53 // Returns the SSLCertRequestInfo for this handler. | |
| 54 net::SSLCertRequestInfo* cert_request_info() { return cert_request_info_; } | |
| 55 | |
| 56 // Returns the session the URL request is associated with. | |
| 57 const net::HttpNetworkSession* http_network_session() const { | |
| 58 return http_network_session_; | |
| 59 } | |
| 60 | |
| 61 protected: | |
| 62 virtual ~SSLClientAuthHandler(); | |
| 63 | |
| 64 private: | |
| 65 friend class base::RefCountedThreadSafe< | |
| 66 SSLClientAuthHandler, content::BrowserThread::DeleteOnIOThread>; | |
| 67 friend class content::BrowserThread; | |
| 68 friend class base::DeleteHelper<SSLClientAuthHandler>; | |
| 69 | |
| 70 // Notifies that the user has selected a cert. | |
| 71 // Called on the IO thread. | |
| 72 void DoCertificateSelected(net::X509Certificate* cert); | |
| 73 | |
| 74 // Selects a client certificate on the UI thread. | |
| 75 void DoSelectCertificate(int render_process_host_id, | |
| 76 int render_view_host_id); | |
| 77 | |
| 78 // The net::URLRequest that triggered this client auth. | |
| 79 net::URLRequest* request_; | |
| 80 | |
| 81 // The HttpNetworkSession |request_| is associated with. | |
| 82 const net::HttpNetworkSession* http_network_session_; | |
| 83 | |
| 84 // The certs to choose from. | |
| 85 scoped_refptr<net::SSLCertRequestInfo> cert_request_info_; | |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(SSLClientAuthHandler); | |
| 88 }; | |
| 89 | |
| 90 class CONTENT_EXPORT SSLClientAuthObserver | |
| 91 : public content::NotificationObserver { | |
| 92 public: | |
| 93 SSLClientAuthObserver(net::SSLCertRequestInfo* cert_request_info, | |
| 94 SSLClientAuthHandler* handler); | |
| 95 virtual ~SSLClientAuthObserver(); | 26 virtual ~SSLClientAuthObserver(); |
| 96 | 27 |
| 97 // UI should implement this to close the dialog. | 28 // UI should implement this to close the dialog. |
| 98 virtual void OnCertSelectedByNotification() = 0; | 29 virtual void OnCertSelectedByNotification() = 0; |
| 99 | 30 |
| 100 // content::NotificationObserver implementation: | 31 // Send content the certificate. Can also call with NULL if the user |
| 101 virtual void Observe(int type, | 32 // cancelled. Derived classes must use this instead of caching the callback |
| 102 const content::NotificationSource& source, | 33 // and calling it directly. |
| 103 const content::NotificationDetails& details) OVERRIDE; | 34 void CertificateSelected(net::X509Certificate* cert); |
| 104 | 35 |
| 105 // Begins observing notifications from other SSLClientAuthHandler instances. | 36 // Begins observing notifications from other SSLClientAuthHandler instances. |
| 106 // If another instance chooses a cert for a matching SSLCertRequestInfo, we | 37 // If another instance chooses a cert for a matching SSLCertRequestInfo, we |
| 107 // will also use the same cert and OnCertSelectedByNotification will be called | 38 // will also use the same cert and OnCertSelectedByNotification will be called |
| 108 // so that the cert selection UI can be closed. | 39 // so that the cert selection UI can be closed. |
| 109 void StartObserving(); | 40 void StartObserving(); |
| 110 | 41 |
| 111 // Stops observing notifications. We will no longer act on client auth | 42 // Stops observing notifications. We will no longer act on client auth |
| 112 // notifications. | 43 // notifications. |
| 113 void StopObserving(); | 44 void StopObserving(); |
| 114 | 45 |
| 46 net::SSLCertRequestInfo* cert_request_info() const { |
| 47 return cert_request_info_; |
| 48 } |
| 49 |
| 115 private: | 50 private: |
| 51 // content::NotificationObserver implementation: |
| 52 virtual void Observe(int type, |
| 53 const content::NotificationSource& source, |
| 54 const content::NotificationDetails& details) OVERRIDE; |
| 55 |
| 56 const net::HttpNetworkSession* network_session_; |
| 116 scoped_refptr<net::SSLCertRequestInfo> cert_request_info_; | 57 scoped_refptr<net::SSLCertRequestInfo> cert_request_info_; |
| 117 | 58 base::Callback<void(net::X509Certificate*)> callback_; |
| 118 scoped_refptr<SSLClientAuthHandler> handler_; | |
| 119 | |
| 120 content::NotificationRegistrar notification_registrar_; | 59 content::NotificationRegistrar notification_registrar_; |
| 121 | 60 |
| 122 DISALLOW_COPY_AND_ASSIGN(SSLClientAuthObserver); | 61 DISALLOW_COPY_AND_ASSIGN(SSLClientAuthObserver); |
| 123 }; | 62 }; |
| 124 | 63 |
| 125 #endif // CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ | 64 #endif // CHROME_BROWSER_SSL_SSL_CLIENT_AUTH_OBSERVER_H_ |
| OLD | NEW |