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/browser/loader/resource_request_info_impl.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/content_browser_client.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 typedef base::Callback
<void(net::X509Certificate
*)> CertificateCallback
;
22 void CertificateSelectedOnUIThread(
23 const CertificateCallback
& io_thread_callback
,
24 net::X509Certificate
* cert
) {
25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
27 BrowserThread::PostTask(
28 BrowserThread::IO
, FROM_HERE
,
29 base::Bind(io_thread_callback
, make_scoped_refptr(cert
)));
32 void SelectCertificateOnUIThread(
33 int render_process_host_id
,
34 int render_frame_host_id
,
35 net::SSLCertRequestInfo
* cert_request_info
,
36 const CertificateCallback
& io_thread_callback
) {
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
39 GetContentClient()->browser()->SelectClientCertificate(
40 render_process_host_id
, render_frame_host_id
, cert_request_info
,
41 base::Bind(&CertificateSelectedOnUIThread
, io_thread_callback
));
46 SSLClientAuthHandler::SSLClientAuthHandler(
47 scoped_ptr
<net::ClientCertStore
> client_cert_store
,
48 net::URLRequest
* request
,
49 net::SSLCertRequestInfo
* cert_request_info
,
50 const SSLClientAuthHandler::CertificateCallback
& callback
)
52 cert_request_info_(cert_request_info
),
53 client_cert_store_(client_cert_store
.Pass()),
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
59 SSLClientAuthHandler::~SSLClientAuthHandler() {
62 void SSLClientAuthHandler::SelectCertificate() {
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
65 if (client_cert_store_
) {
66 client_cert_store_
->GetClientCerts(
68 &cert_request_info_
->client_certs
,
69 base::Bind(&SSLClientAuthHandler::DidGetClientCerts
,
70 weak_factory_
.GetWeakPtr()));
76 void SSLClientAuthHandler::DidGetClientCerts() {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
79 // Note that if |client_cert_store_| is NULL, we intentionally fall through to
80 // DoCertificateSelected. This is for platforms where the client cert matching
81 // is not performed by Chrome. Those platforms handle the cert matching before
82 // showing the dialog.
83 if (client_cert_store_
&& cert_request_info_
->client_certs
.empty()) {
84 // No need to query the user if there are no certs to choose from.
85 CertificateSelected(NULL
);
89 int render_process_host_id
;
90 int render_frame_host_id
;
91 if (!ResourceRequestInfo::ForRequest(request_
)->GetAssociatedRenderFrame(
92 &render_process_host_id
,
93 &render_frame_host_id
)) {
95 CertificateSelected(NULL
);
99 BrowserThread::PostTask(
100 BrowserThread::UI
, FROM_HERE
,
101 base::Bind(&SelectCertificateOnUIThread
,
102 render_process_host_id
, render_frame_host_id
,
104 base::Bind(&SSLClientAuthHandler::CertificateSelected
,
105 weak_factory_
.GetWeakPtr())));
108 void SSLClientAuthHandler::CertificateSelected(net::X509Certificate
* cert
) {
109 VLOG(1) << this << " DoCertificateSelected " << cert
;
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
113 // |this| may be deleted at this point.
116 } // namespace content