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 #include "content/browser/ssl/ssl_client_auth_handler.h" | 5 #include "content/browser/ssl/ssl_client_auth_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/browser/client_certificate_delegate.h" | |
10 #include "content/public/browser/content_browser_client.h" | 11 #include "content/public/browser/content_browser_client.h" |
12 #include "content/public/browser/render_frame_host.h" | |
11 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
14 #include "content/public/browser/web_contents.h" | |
12 #include "net/cert/x509_certificate.h" | 15 #include "net/cert/x509_certificate.h" |
13 #include "net/ssl/client_cert_store.h" | 16 #include "net/ssl/client_cert_store.h" |
14 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
15 | 18 |
16 namespace content { | 19 namespace content { |
17 | 20 |
18 namespace { | 21 namespace { |
19 | 22 |
20 void CertificateSelectedOnUIThread( | 23 class ClientCertificateDelegateImpl : public ClientCertificateDelegate { |
21 const SSLClientAuthHandler::CertificateCallback& io_thread_callback, | 24 public: |
22 net::X509Certificate* cert) { | 25 explicit ClientCertificateDelegateImpl( |
23 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 26 const base::WeakPtr<SSLClientAuthHandler>& handler) |
27 : handler_(handler), called_(false) {} | |
24 | 28 |
25 BrowserThread::PostTask( | 29 ~ClientCertificateDelegateImpl() override { |
26 BrowserThread::IO, FROM_HERE, | 30 if (!called_) { |
27 base::Bind(io_thread_callback, make_scoped_refptr(cert))); | 31 BrowserThread::PostTask( |
28 } | 32 BrowserThread::IO, FROM_HERE, |
33 base::Bind(&SSLClientAuthHandler::CancelCertificateSelection, | |
34 handler_)); | |
35 } | |
36 } | |
37 | |
38 // ClientCertificateDelegate implementation: | |
39 void ContinueWithCertificate(net::X509Certificate* cert) override { | |
40 DCHECK(!called_); | |
41 called_ = true; | |
42 BrowserThread::PostTask( | |
43 BrowserThread::IO, FROM_HERE, | |
44 base::Bind(&SSLClientAuthHandler::ContinueWithCertificate, handler_, | |
45 make_scoped_refptr(cert))); | |
46 } | |
47 | |
48 private: | |
49 base::WeakPtr<SSLClientAuthHandler> handler_; | |
50 bool called_; | |
mmenke
2015/03/09 18:30:27
Can we name this something clearer? continue_call
davidben
2015/03/10 02:07:57
Done. (Went with continue_called_)
| |
51 | |
52 DISALLOW_COPY_AND_ASSIGN(ClientCertificateDelegateImpl); | |
53 }; | |
29 | 54 |
30 void SelectCertificateOnUIThread( | 55 void SelectCertificateOnUIThread( |
31 int render_process_host_id, | 56 int render_process_host_id, |
32 int render_frame_host_id, | 57 int render_frame_host_id, |
33 net::SSLCertRequestInfo* cert_request_info, | 58 net::SSLCertRequestInfo* cert_request_info, |
34 const SSLClientAuthHandler::CertificateCallback& io_thread_callback) { | 59 const base::WeakPtr<SSLClientAuthHandler>& handler) { |
35 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 60 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
36 | 61 |
62 scoped_ptr<ClientCertificateDelegate> delegate( | |
63 new ClientCertificateDelegateImpl(handler)); | |
64 | |
65 RenderFrameHost* rfh = | |
66 RenderFrameHost::FromID(render_process_host_id, render_frame_host_id); | |
67 WebContents* web_contents = WebContents::FromRenderFrameHost(rfh); | |
68 if (!web_contents) | |
69 return; | |
70 | |
37 GetContentClient()->browser()->SelectClientCertificate( | 71 GetContentClient()->browser()->SelectClientCertificate( |
38 render_process_host_id, render_frame_host_id, cert_request_info, | 72 web_contents, cert_request_info, delegate.Pass()); |
39 base::Bind(&CertificateSelectedOnUIThread, io_thread_callback)); | |
40 } | 73 } |
41 | 74 |
42 } // namespace | 75 } // namespace |
43 | 76 |
44 // A reference-counted core to allow the ClientCertStore and SSLCertRequestInfo | 77 // A reference-counted core to allow the ClientCertStore and SSLCertRequestInfo |
45 // to outlive SSLClientAuthHandler if needbe. | 78 // to outlive SSLClientAuthHandler if needbe. |
46 class SSLClientAuthHandler::Core : public base::RefCountedThreadSafe<Core> { | 79 class SSLClientAuthHandler::Core : public base::RefCountedThreadSafe<Core> { |
47 public: | 80 public: |
48 Core(const base::WeakPtr<SSLClientAuthHandler>& handler, | 81 Core(const base::WeakPtr<SSLClientAuthHandler>& handler, |
49 scoped_ptr<net::ClientCertStore> client_cert_store, | 82 scoped_ptr<net::ClientCertStore> client_cert_store, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 | 114 |
82 base::WeakPtr<SSLClientAuthHandler> handler_; | 115 base::WeakPtr<SSLClientAuthHandler> handler_; |
83 scoped_ptr<net::ClientCertStore> client_cert_store_; | 116 scoped_ptr<net::ClientCertStore> client_cert_store_; |
84 scoped_refptr<net::SSLCertRequestInfo> cert_request_info_; | 117 scoped_refptr<net::SSLCertRequestInfo> cert_request_info_; |
85 }; | 118 }; |
86 | 119 |
87 SSLClientAuthHandler::SSLClientAuthHandler( | 120 SSLClientAuthHandler::SSLClientAuthHandler( |
88 scoped_ptr<net::ClientCertStore> client_cert_store, | 121 scoped_ptr<net::ClientCertStore> client_cert_store, |
89 net::URLRequest* request, | 122 net::URLRequest* request, |
90 net::SSLCertRequestInfo* cert_request_info, | 123 net::SSLCertRequestInfo* cert_request_info, |
91 const SSLClientAuthHandler::CertificateCallback& callback) | 124 SSLClientAuthHandler::Delegate* delegate) |
92 : request_(request), | 125 : request_(request), |
93 cert_request_info_(cert_request_info), | 126 cert_request_info_(cert_request_info), |
94 callback_(callback), | 127 delegate_(delegate), |
95 weak_factory_(this) { | 128 weak_factory_(this) { |
96 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 129 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
97 | 130 |
98 core_ = new Core(weak_factory_.GetWeakPtr(), client_cert_store.Pass(), | 131 core_ = new Core(weak_factory_.GetWeakPtr(), client_cert_store.Pass(), |
99 cert_request_info_.get()); | 132 cert_request_info_.get()); |
100 } | 133 } |
101 | 134 |
102 SSLClientAuthHandler::~SSLClientAuthHandler() { | 135 SSLClientAuthHandler::~SSLClientAuthHandler() { |
103 } | 136 } |
104 | 137 |
105 void SSLClientAuthHandler::SelectCertificate() { | 138 void SSLClientAuthHandler::SelectCertificate() { |
106 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 139 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
107 | 140 |
108 // |core_| will call DidGetClientCerts when done. | 141 // |core_| will call DidGetClientCerts when done. |
109 core_->GetClientCerts(); | 142 core_->GetClientCerts(); |
110 } | 143 } |
111 | 144 |
145 // static | |
146 void SSLClientAuthHandler::ContinueWithCertificate( | |
147 const base::WeakPtr<SSLClientAuthHandler>& handler, | |
148 net::X509Certificate* cert) { | |
149 if (handler) | |
150 handler->delegate_->ContinueWithCertificate(cert); | |
151 } | |
152 | |
153 // static | |
154 void SSLClientAuthHandler::CancelCertificateSelection( | |
155 const base::WeakPtr<SSLClientAuthHandler>& handler) { | |
156 if (handler) | |
157 handler->delegate_->CancelCertificateSelection(); | |
158 } | |
159 | |
112 void SSLClientAuthHandler::DidGetClientCerts() { | 160 void SSLClientAuthHandler::DidGetClientCerts() { |
113 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 161 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
114 | 162 |
115 // Note that if |client_cert_store_| is NULL, we intentionally fall through to | 163 // Note that if |client_cert_store_| is NULL, we intentionally fall through to |
116 // DoCertificateSelected. This is for platforms where the client cert matching | 164 // SelectCertificateOnUIThread. This is for platforms where the client cert |
117 // is not performed by Chrome. Those platforms handle the cert matching before | 165 // matching is not performed by Chrome. Those platforms handle the cert |
118 // showing the dialog. | 166 // matching before showing the dialog. |
119 if (core_->has_client_cert_store() && | 167 if (core_->has_client_cert_store() && |
120 cert_request_info_->client_certs.empty()) { | 168 cert_request_info_->client_certs.empty()) { |
121 // No need to query the user if there are no certs to choose from. | 169 // No need to query the user if there are no certs to choose from. |
122 CertificateSelected(NULL); | 170 // |
171 // TODO(davidben): The WebContents-less check on the UI thread should come | |
172 // before checking ClientCertStore; ClientCertStore itself should probably | |
173 // be handled by the embedder (https://crbug.com/394131), especially since | |
174 // this doesn't work on Android (https://crbug.com/345641). | |
175 BrowserThread::PostTask( | |
176 BrowserThread::IO, FROM_HERE, | |
177 base::Bind(&SSLClientAuthHandler::ContinueWithCertificate, | |
178 weak_factory_.GetWeakPtr(), | |
179 scoped_refptr<net::X509Certificate>())); | |
123 return; | 180 return; |
124 } | 181 } |
125 | 182 |
126 int render_process_host_id; | 183 int render_process_host_id; |
127 int render_frame_host_id; | 184 int render_frame_host_id; |
128 if (!ResourceRequestInfo::ForRequest(request_)->GetAssociatedRenderFrame( | 185 if (!ResourceRequestInfo::ForRequest(request_)->GetAssociatedRenderFrame( |
129 &render_process_host_id, &render_frame_host_id)) { | 186 &render_process_host_id, &render_frame_host_id)) { |
130 NOTREACHED(); | 187 NOTREACHED(); |
131 CertificateSelected(NULL); | 188 BrowserThread::PostTask( |
189 BrowserThread::IO, FROM_HERE, | |
190 base::Bind(&SSLClientAuthHandler::CancelCertificateSelection, | |
191 weak_factory_.GetWeakPtr())); | |
132 return; | 192 return; |
133 } | 193 } |
134 | 194 |
135 BrowserThread::PostTask( | 195 BrowserThread::PostTask( |
136 BrowserThread::UI, FROM_HERE, | 196 BrowserThread::UI, FROM_HERE, |
137 base::Bind(&SelectCertificateOnUIThread, render_process_host_id, | 197 base::Bind(&SelectCertificateOnUIThread, render_process_host_id, |
138 render_frame_host_id, cert_request_info_, | 198 render_frame_host_id, cert_request_info_, |
139 base::Bind(&SSLClientAuthHandler::CertificateSelected, | 199 weak_factory_.GetWeakPtr())); |
140 weak_factory_.GetWeakPtr()))); | |
141 } | |
142 | |
143 void SSLClientAuthHandler::CertificateSelected(net::X509Certificate* cert) { | |
144 DVLOG(1) << this << " DoCertificateSelected " << cert; | |
145 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
146 | |
147 callback_.Run(cert); | |
148 // |this| may be deleted at this point. | |
149 } | 200 } |
150 | 201 |
151 } // namespace content | 202 } // namespace content |
OLD | NEW |