OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/cert_store.h" | 5 #include "chrome/browser/cert_store.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 | 9 |
10 #include "chrome/browser/render_view_host.h" | 10 #include "chrome/browser/render_view_host.h" |
11 #include "chrome/browser/web_contents.h" | 11 #include "chrome/browser/web_contents.h" |
12 #include "chrome/common/stl_util-inl.h" | 12 #include "chrome/common/stl_util-inl.h" |
13 | 13 |
14 template <typename T> | 14 template <typename T> |
15 struct MatchSecond { | 15 struct MatchSecond { |
16 MatchSecond(const T& t) : value(t) {} | 16 explicit MatchSecond(const T& t) : value(t) {} |
17 | 17 |
18 template<typename Pair> | 18 template<typename Pair> |
19 bool operator()(const Pair& p) const { | 19 bool operator()(const Pair& p) const { |
20 return (value == p.second); | 20 return (value == p.second); |
21 } | 21 } |
22 T value; | 22 T value; |
23 }; | 23 }; |
24 | 24 |
25 // static | 25 // static |
26 CertStore* CertStore::instance_ = NULL; | 26 CertStore* CertStore::instance_ = NULL; |
27 | 27 |
28 // static | 28 // static |
29 void CertStore::Initialize() { | 29 void CertStore::Initialize() { |
30 DCHECK(!instance_); | 30 DCHECK(!instance_); |
31 instance_ = new CertStore(); | 31 instance_ = new CertStore(); |
32 } | 32 } |
33 | 33 |
34 // static | 34 // static |
35 CertStore* CertStore::GetSharedInstance() { | 35 CertStore* CertStore::GetSharedInstance() { |
36 DCHECK(instance_); | 36 DCHECK(instance_); |
37 return instance_; | 37 return instance_; |
38 } | 38 } |
39 | 39 |
40 CertStore::CertStore() : next_cert_id_(1) { | 40 CertStore::CertStore() : next_cert_id_(1) { |
41 // We watch for RenderProcess termination, as this is how we clear | 41 // We watch for RenderProcess termination, as this is how we clear |
42 // certificates for now. | 42 // certificates for now. |
43 // TODO (jcampan): we should be listening to events such as resource cached/ | 43 // TODO(jcampan): we should be listening to events such as resource cached/ |
44 // removed from cache, and remove the cert when we know it | 44 // removed from cache, and remove the cert when we know it |
45 // is not used anymore. | 45 // is not used anymore. |
46 | 46 |
47 // TODO(tc): This notification observer never gets removed because the | 47 // TODO(tc): This notification observer never gets removed because the |
48 // CertStore is never deleted. | 48 // CertStore is never deleted. |
49 NotificationService::current()->AddObserver(this, | 49 NotificationService::current()->AddObserver(this, |
50 NOTIFY_RENDERER_PROCESS_TERMINATED, NotificationService::AllSources()); | 50 NOTIFY_RENDERER_PROCESS_TERMINATED, NotificationService::AllSources()); |
51 } | 51 } |
52 | 52 |
53 CertStore::~CertStore() { | 53 CertStore::~CertStore() { |
54 NotificationService::current()->RemoveObserver(this, | 54 NotificationService::current()->RemoveObserver(this, |
55 NOTIFY_RENDERER_PROCESS_TERMINATED, NotificationService::AllSources()); | 55 NOTIFY_RENDERER_PROCESS_TERMINATED, NotificationService::AllSources()); |
(...skipping 30 matching lines...) Expand all Loading... |
86 MatchSecond<int>(process_id)) == | 86 MatchSecond<int>(process_id)) == |
87 cert_id_to_process_id_.upper_bound(cert_id)) { | 87 cert_id_to_process_id_.upper_bound(cert_id)) { |
88 cert_id_to_process_id_.insert(std::make_pair(cert_id, process_id)); | 88 cert_id_to_process_id_.insert(std::make_pair(cert_id, process_id)); |
89 } | 89 } |
90 | 90 |
91 return cert_id; | 91 return cert_id; |
92 } | 92 } |
93 | 93 |
94 bool CertStore::RetrieveCert(int cert_id, | 94 bool CertStore::RetrieveCert(int cert_id, |
95 scoped_refptr<net::X509Certificate>* cert) { | 95 scoped_refptr<net::X509Certificate>* cert) { |
96 AutoLock autoLock(cert_lock_); | 96 AutoLock autoLock(cert_lock_); |
97 | 97 |
98 CertMap::iterator iter = id_to_cert_.find(cert_id); | 98 CertMap::iterator iter = id_to_cert_.find(cert_id); |
99 if (iter == id_to_cert_.end()) | 99 if (iter == id_to_cert_.end()) |
100 return false; | 100 return false; |
101 *cert = iter->second; | 101 *cert = iter->second; |
102 return true; | 102 return true; |
103 } | 103 } |
104 | 104 |
105 void CertStore::RemoveCert(int cert_id) { | 105 void CertStore::RemoveCertInternal(int cert_id) { |
106 AutoLock autoLock(cert_lock_); | |
107 | |
108 CertMap::iterator cert_iter = id_to_cert_.find(cert_id); | 106 CertMap::iterator cert_iter = id_to_cert_.find(cert_id); |
109 DCHECK(cert_iter != id_to_cert_.end()); | 107 DCHECK(cert_iter != id_to_cert_.end()); |
110 | 108 |
111 ReverseCertMap::iterator id_iter = cert_to_id_.find(cert_iter->second); | 109 ReverseCertMap::iterator id_iter = cert_to_id_.find(cert_iter->second); |
112 DCHECK(id_iter != cert_to_id_.end()); | 110 DCHECK(id_iter != cert_to_id_.end()); |
113 cert_to_id_.erase(id_iter); | 111 cert_to_id_.erase(id_iter); |
114 | 112 |
115 cert_iter->second->Release(); | 113 cert_iter->second->Release(); |
116 id_to_cert_.erase(cert_iter); | 114 id_to_cert_.erase(cert_iter); |
117 } | 115 } |
(...skipping 10 matching lines...) Expand all Loading... |
128 IDMap::iterator proc_iter = | 126 IDMap::iterator proc_iter = |
129 std::find_if(cert_id_to_process_id_.lower_bound(cert_id), | 127 std::find_if(cert_id_to_process_id_.lower_bound(cert_id), |
130 cert_id_to_process_id_.upper_bound(cert_id), | 128 cert_id_to_process_id_.upper_bound(cert_id), |
131 MatchSecond<int>(process_id)); | 129 MatchSecond<int>(process_id)); |
132 DCHECK(proc_iter != cert_id_to_process_id_.upper_bound(cert_id)); | 130 DCHECK(proc_iter != cert_id_to_process_id_.upper_bound(cert_id)); |
133 cert_id_to_process_id_.erase(proc_iter); | 131 cert_id_to_process_id_.erase(proc_iter); |
134 | 132 |
135 if (cert_id_to_process_id_.count(cert_id) == 0) { | 133 if (cert_id_to_process_id_.count(cert_id) == 0) { |
136 // This cert is not referenced by any process, remove it from id_to_cert_ | 134 // This cert is not referenced by any process, remove it from id_to_cert_ |
137 // and cert_to_id_. | 135 // and cert_to_id_. |
138 RemoveCert(cert_id); | 136 RemoveCertInternal(cert_id); |
139 } | 137 } |
140 | 138 |
141 // Erase the current item but keep the iterator valid. | 139 // Erase the current item but keep the iterator valid. |
142 process_id_to_cert_id_.erase(ids_iter++); | 140 process_id_to_cert_id_.erase(ids_iter++); |
143 } | 141 } |
144 } | 142 } |
145 | 143 |
146 void CertStore::Observe(NotificationType type, | 144 void CertStore::Observe(NotificationType type, |
147 const NotificationSource& source, | 145 const NotificationSource& source, |
148 const NotificationDetails& details) { | 146 const NotificationDetails& details) { |
149 DCHECK(type == NOTIFY_RENDERER_PROCESS_TERMINATED); | 147 DCHECK(type == NOTIFY_RENDERER_PROCESS_TERMINATED); |
150 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); | 148 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); |
151 DCHECK(rph); | 149 DCHECK(rph); |
152 RemoveCertsForRenderProcesHost(rph->host_id()); | 150 RemoveCertsForRenderProcesHost(rph->host_id()); |
153 } | 151 } |
154 | 152 |
OLD | NEW |