Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / ssl / ssl_client_auth_handler.cc
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.
4
5 #include "content/browser/ssl/ssl_client_auth_handler.h"
6
7 #include "base/bind.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"
15
16 namespace content {
17
18 namespace {
19
20 typedef base::Callback<void(net::X509Certificate*)> CertificateCallback;
21
22 void CertificateSelectedOnUIThread(
23     const CertificateCallback& io_thread_callback,
24     net::X509Certificate* cert) {
25   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
26
27   BrowserThread::PostTask(
28       BrowserThread::IO, FROM_HERE,
29       base::Bind(io_thread_callback, make_scoped_refptr(cert)));
30 }
31
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));
38
39   GetContentClient()->browser()->SelectClientCertificate(
40       render_process_host_id, render_frame_host_id, cert_request_info,
41       base::Bind(&CertificateSelectedOnUIThread, io_thread_callback));
42 }
43
44 }  // namespace
45
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)
51     : request_(request),
52       cert_request_info_(cert_request_info),
53       client_cert_store_(client_cert_store.Pass()),
54       callback_(callback),
55       weak_factory_(this) {
56   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
57 }
58
59 SSLClientAuthHandler::~SSLClientAuthHandler() {
60 }
61
62 void SSLClientAuthHandler::SelectCertificate() {
63   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
64
65   if (client_cert_store_) {
66     client_cert_store_->GetClientCerts(
67         *cert_request_info_,
68         &cert_request_info_->client_certs,
69         base::Bind(&SSLClientAuthHandler::DidGetClientCerts,
70                    weak_factory_.GetWeakPtr()));
71   } else {
72     DidGetClientCerts();
73   }
74 }
75
76 void SSLClientAuthHandler::DidGetClientCerts() {
77   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
78
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);
86     return;
87   }
88
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)) {
94     NOTREACHED();
95     CertificateSelected(NULL);
96     return;
97   }
98
99   BrowserThread::PostTask(
100       BrowserThread::UI, FROM_HERE,
101       base::Bind(&SelectCertificateOnUIThread,
102                  render_process_host_id, render_frame_host_id,
103                  cert_request_info_,
104                  base::Bind(&SSLClientAuthHandler::CertificateSelected,
105                             weak_factory_.GetWeakPtr())));
106 }
107
108 void SSLClientAuthHandler::CertificateSelected(net::X509Certificate* cert) {
109   VLOG(1) << this << " DoCertificateSelected " << cert;
110   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
111
112   callback_.Run(cert);
113   // |this| may be deleted at this point.
114 }
115
116 }  // namespace content