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
3 // found in the LICENSE file.
5 #include "content/browser/ssl/ssl_client_auth_handler.h"
8 #include "base/logging.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/content_browser_client.h"
11 #include "content/public/browser/resource_request_info.h"
12 #include "net/cert/x509_certificate.h"
13 #include "net/ssl/client_cert_store.h"
14 #include "net/url_request/url_request.h"
20 void CertificateSelectedOnUIThread(
21 const SSLClientAuthHandler::CertificateCallback
& io_thread_callback
,
22 net::X509Certificate
* cert
) {
23 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
25 BrowserThread::PostTask(
26 BrowserThread::IO
, FROM_HERE
,
27 base::Bind(io_thread_callback
, make_scoped_refptr(cert
)));
30 void SelectCertificateOnUIThread(
31 int render_process_host_id
,
32 int render_frame_host_id
,
33 net::SSLCertRequestInfo
* cert_request_info
,
34 const SSLClientAuthHandler::CertificateCallback
& io_thread_callback
) {
35 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
37 GetContentClient()->browser()->SelectClientCertificate(
38 render_process_host_id
, render_frame_host_id
, cert_request_info
,
39 base::Bind(&CertificateSelectedOnUIThread
, io_thread_callback
));
44 // A reference-counted core to allow the ClientCertStore and SSLCertRequestInfo
45 // to outlive SSLClientAuthHandler if needbe.
46 class SSLClientAuthHandler::Core
: public base::RefCountedThreadSafe
<Core
> {
48 Core(const base::WeakPtr
<SSLClientAuthHandler
>& handler
,
49 scoped_ptr
<net::ClientCertStore
> client_cert_store
,
50 net::SSLCertRequestInfo
* cert_request_info
)
52 client_cert_store_(client_cert_store
.Pass()),
53 cert_request_info_(cert_request_info
) {}
55 bool has_client_cert_store() const { return client_cert_store_
; }
57 void GetClientCerts() {
58 if (client_cert_store_
) {
59 // TODO(davidben): This is still a cyclical ownership where
60 // GetClientCerts' requirement that |client_cert_store_| remains alive
61 // until the call completes is maintained by the reference held in the
63 client_cert_store_
->GetClientCerts(
64 *cert_request_info_
, &cert_request_info_
->client_certs
,
65 base::Bind(&SSLClientAuthHandler::Core::DidGetClientCerts
, this));
72 friend class base::RefCountedThreadSafe
<Core
>;
76 // Called when |client_cert_store_| is done retrieving the cert list.
77 void DidGetClientCerts() {
79 handler_
->DidGetClientCerts();
82 base::WeakPtr
<SSLClientAuthHandler
> handler_
;
83 scoped_ptr
<net::ClientCertStore
> client_cert_store_
;
84 scoped_refptr
<net::SSLCertRequestInfo
> cert_request_info_
;
87 SSLClientAuthHandler::SSLClientAuthHandler(
88 scoped_ptr
<net::ClientCertStore
> client_cert_store
,
89 net::URLRequest
* request
,
90 net::SSLCertRequestInfo
* cert_request_info
,
91 const SSLClientAuthHandler::CertificateCallback
& callback
)
93 cert_request_info_(cert_request_info
),
96 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
98 core_
= new Core(weak_factory_
.GetWeakPtr(), client_cert_store
.Pass(),
99 cert_request_info_
.get());
102 SSLClientAuthHandler::~SSLClientAuthHandler() {
105 void SSLClientAuthHandler::SelectCertificate() {
106 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
108 // |core_| will call DidGetClientCerts when done.
109 core_
->GetClientCerts();
112 void SSLClientAuthHandler::DidGetClientCerts() {
113 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
115 // Note that if |client_cert_store_| is NULL, we intentionally fall through to
116 // DoCertificateSelected. This is for platforms where the client cert matching
117 // is not performed by Chrome. Those platforms handle the cert matching before
118 // showing the dialog.
119 if (core_
->has_client_cert_store() &&
120 cert_request_info_
->client_certs
.empty()) {
121 // No need to query the user if there are no certs to choose from.
122 CertificateSelected(NULL
);
126 int render_process_host_id
;
127 int render_frame_host_id
;
128 if (!ResourceRequestInfo::ForRequest(request_
)->GetAssociatedRenderFrame(
129 &render_process_host_id
, &render_frame_host_id
)) {
131 CertificateSelected(NULL
);
135 BrowserThread::PostTask(
136 BrowserThread::UI
, FROM_HERE
,
137 base::Bind(&SelectCertificateOnUIThread
, render_process_host_id
,
138 render_frame_host_id
, cert_request_info_
,
139 base::Bind(&SSLClientAuthHandler::CertificateSelected
,
140 weak_factory_
.GetWeakPtr())));
143 void SSLClientAuthHandler::CertificateSelected(net::X509Certificate
* cert
) {
144 DVLOG(1) << this << " DoCertificateSelected " << cert
;
145 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
148 // |this| may be deleted at this point.
151 } // namespace content