Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ssl / ssl_client_auth_observer.cc
blob4a2dedb74822bb6ea55eacf54c087ce08714ed31
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 "chrome/browser/ssl/ssl_client_auth_observer.h"
7 #include <utility>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/client_certificate_delegate.h"
14 #include "content/public/browser/notification_service.h"
15 #include "net/cert/x509_certificate.h"
16 #include "net/ssl/ssl_cert_request_info.h"
18 using content::BrowserThread;
20 typedef std::pair<net::SSLCertRequestInfo*, net::X509Certificate*> CertDetails;
22 SSLClientAuthObserver::SSLClientAuthObserver(
23 const content::BrowserContext* browser_context,
24 const scoped_refptr<net::SSLCertRequestInfo>& cert_request_info,
25 scoped_ptr<content::ClientCertificateDelegate> delegate)
26 : browser_context_(browser_context),
27 cert_request_info_(cert_request_info),
28 delegate_(delegate.Pass()) {
31 SSLClientAuthObserver::~SSLClientAuthObserver() {
34 void SSLClientAuthObserver::CertificateSelected(
35 net::X509Certificate* certificate) {
36 if (!delegate_)
37 return;
39 // Stop listening now that the delegate has been resolved. This is also to
40 // avoid getting a self-notification.
41 StopObserving();
43 CertDetails details;
44 details.first = cert_request_info_.get();
45 details.second = certificate;
46 content::NotificationService* service =
47 content::NotificationService::current();
48 service->Notify(chrome::NOTIFICATION_SSL_CLIENT_AUTH_CERT_SELECTED,
49 content::Source<content::BrowserContext>(browser_context_),
50 content::Details<CertDetails>(&details));
52 delegate_->ContinueWithCertificate(certificate);
53 delegate_.reset();
56 void SSLClientAuthObserver::CancelCertificateSelection() {
57 if (!delegate_)
58 return;
60 // Stop observing now that the delegate has been resolved.
61 StopObserving();
62 delegate_.reset();
65 void SSLClientAuthObserver::Observe(
66 int type,
67 const content::NotificationSource& source,
68 const content::NotificationDetails& details) {
69 DVLOG(1) << "SSLClientAuthObserver::Observe " << this;
70 DCHECK_CURRENTLY_ON(BrowserThread::UI);
71 DCHECK(type == chrome::NOTIFICATION_SSL_CLIENT_AUTH_CERT_SELECTED);
73 CertDetails* cert_details = content::Details<CertDetails>(details).ptr();
74 if (!cert_details->first->host_and_port.Equals(
75 cert_request_info_->host_and_port))
76 return;
78 DVLOG(1) << this << " got matching notification and selecting cert "
79 << cert_details->second;
80 StopObserving();
81 delegate_->ContinueWithCertificate(cert_details->second);
82 delegate_.reset();
83 OnCertSelectedByNotification();
86 void SSLClientAuthObserver::StartObserving() {
87 DCHECK_CURRENTLY_ON(BrowserThread::UI);
88 notification_registrar_.Add(
89 this, chrome::NOTIFICATION_SSL_CLIENT_AUTH_CERT_SELECTED,
90 content::Source<content::BrowserContext>(browser_context_));
93 void SSLClientAuthObserver::StopObserving() {
94 DCHECK_CURRENTLY_ON(BrowserThread::UI);
95 notification_registrar_.RemoveAll();