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/cert_store.h" | 5 #include "content/browser/cert_store_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "content/browser/renderer_host/render_process_host_impl.h" | 12 #include "content/browser/renderer_host/render_process_host_impl.h" |
13 #include "content/browser/renderer_host/render_view_host_impl.h" | 13 #include "content/browser/renderer_host/render_view_host_impl.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
16 #include "content/public/browser/notification_types.h" | 16 #include "content/public/browser/notification_types.h" |
17 | 17 |
18 template <typename T> | 18 template <typename T> |
19 struct MatchSecond { | 19 struct MatchSecond { |
20 explicit MatchSecond(const T& t) : value(t) {} | 20 explicit MatchSecond(const T& t) : value(t) {} |
21 | 21 |
22 template<typename Pair> | 22 template<typename Pair> |
23 bool operator()(const Pair& p) const { | 23 bool operator()(const Pair& p) const { |
24 return (value == p.second); | 24 return (value == p.second); |
25 } | 25 } |
26 T value; | 26 T value; |
27 }; | 27 }; |
28 | 28 |
29 // static | 29 // static |
30 CertStore* CertStore::GetInstance() { | 30 content::CertStore* content::CertStore::GetInstance() { |
31 return Singleton<CertStore>::get(); | 31 return CertStoreImpl::GetInstance(); |
32 } | 32 } |
33 | 33 |
34 CertStore::CertStore() : next_cert_id_(1) { | 34 // static |
| 35 CertStoreImpl* CertStoreImpl::GetInstance() { |
| 36 return Singleton<CertStoreImpl>::get(); |
| 37 } |
| 38 |
| 39 CertStoreImpl::CertStoreImpl() : next_cert_id_(1) { |
35 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | 40 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
36 RegisterForNotification(); | 41 RegisterForNotification(); |
37 } else { | 42 } else { |
38 content::BrowserThread::PostTask( | 43 content::BrowserThread::PostTask( |
39 content::BrowserThread::UI, FROM_HERE, | 44 content::BrowserThread::UI, FROM_HERE, |
40 base::Bind(&CertStore::RegisterForNotification, | 45 base::Bind(&CertStoreImpl::RegisterForNotification, |
41 base::Unretained(this))); | 46 base::Unretained(this))); |
42 } | 47 } |
43 } | 48 } |
44 | 49 |
45 CertStore::~CertStore() { | 50 CertStoreImpl::~CertStoreImpl() { |
46 } | 51 } |
47 | 52 |
48 void CertStore::RegisterForNotification() { | 53 void CertStoreImpl::RegisterForNotification() { |
49 // We watch for RenderProcess termination, as this is how we clear | 54 // We watch for RenderProcess termination, as this is how we clear |
50 // certificates for now. | 55 // certificates for now. |
51 // TODO(jcampan): we should be listening to events such as resource cached/ | 56 // TODO(jcampan): we should be listening to events such as resource cached/ |
52 // removed from cache, and remove the cert when we know it | 57 // removed from cache, and remove the cert when we know it |
53 // is not used anymore. | 58 // is not used anymore. |
54 | 59 |
55 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 60 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
56 content::NotificationService::AllBrowserContextsAndSources()); | 61 content::NotificationService::AllBrowserContextsAndSources()); |
57 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 62 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
58 content::NotificationService::AllBrowserContextsAndSources()); | 63 content::NotificationService::AllBrowserContextsAndSources()); |
59 } | 64 } |
60 | 65 |
61 int CertStore::StoreCert(net::X509Certificate* cert, int process_id) { | 66 int CertStoreImpl::StoreCert(net::X509Certificate* cert, int process_id) { |
62 DCHECK(cert); | 67 DCHECK(cert); |
63 base::AutoLock auto_lock(cert_lock_); | 68 base::AutoLock auto_lock(cert_lock_); |
64 | 69 |
65 int cert_id; | 70 int cert_id; |
66 | 71 |
67 // Do we already know this cert? | 72 // Do we already know this cert? |
68 ReverseCertMap::iterator cert_iter = cert_to_id_.find(cert); | 73 ReverseCertMap::iterator cert_iter = cert_to_id_.find(cert); |
69 if (cert_iter == cert_to_id_.end()) { | 74 if (cert_iter == cert_to_id_.end()) { |
70 cert_id = next_cert_id_++; | 75 cert_id = next_cert_id_++; |
71 // We use 0 as an invalid cert_id value. In the unlikely event that | 76 // We use 0 as an invalid cert_id value. In the unlikely event that |
(...skipping 19 matching lines...) Expand all Loading... |
91 std::pair<IDMap::iterator, IDMap::iterator> cert_ids = | 96 std::pair<IDMap::iterator, IDMap::iterator> cert_ids = |
92 cert_id_to_process_id_.equal_range(cert_id); | 97 cert_id_to_process_id_.equal_range(cert_id); |
93 if (std::find_if(cert_ids.first, cert_ids.second, | 98 if (std::find_if(cert_ids.first, cert_ids.second, |
94 MatchSecond<int>(process_id)) == cert_ids.second) { | 99 MatchSecond<int>(process_id)) == cert_ids.second) { |
95 cert_id_to_process_id_.insert(std::make_pair(cert_id, process_id)); | 100 cert_id_to_process_id_.insert(std::make_pair(cert_id, process_id)); |
96 } | 101 } |
97 | 102 |
98 return cert_id; | 103 return cert_id; |
99 } | 104 } |
100 | 105 |
101 bool CertStore::RetrieveCert(int cert_id, | 106 bool CertStoreImpl::RetrieveCert(int cert_id, |
102 scoped_refptr<net::X509Certificate>* cert) { | 107 scoped_refptr<net::X509Certificate>* cert) { |
103 base::AutoLock auto_lock(cert_lock_); | 108 base::AutoLock auto_lock(cert_lock_); |
104 | 109 |
105 CertMap::iterator iter = id_to_cert_.find(cert_id); | 110 CertMap::iterator iter = id_to_cert_.find(cert_id); |
106 if (iter == id_to_cert_.end()) | 111 if (iter == id_to_cert_.end()) |
107 return false; | 112 return false; |
108 if (cert) | 113 if (cert) |
109 *cert = iter->second; | 114 *cert = iter->second; |
110 return true; | 115 return true; |
111 } | 116 } |
112 | 117 |
113 void CertStore::RemoveCertInternal(int cert_id) { | 118 void CertStoreImpl::RemoveCertInternal(int cert_id) { |
114 CertMap::iterator cert_iter = id_to_cert_.find(cert_id); | 119 CertMap::iterator cert_iter = id_to_cert_.find(cert_id); |
115 DCHECK(cert_iter != id_to_cert_.end()); | 120 DCHECK(cert_iter != id_to_cert_.end()); |
116 | 121 |
117 ReverseCertMap::iterator id_iter = cert_to_id_.find(cert_iter->second); | 122 ReverseCertMap::iterator id_iter = cert_to_id_.find(cert_iter->second); |
118 DCHECK(id_iter != cert_to_id_.end()); | 123 DCHECK(id_iter != cert_to_id_.end()); |
119 cert_to_id_.erase(id_iter); | 124 cert_to_id_.erase(id_iter); |
120 | 125 |
121 cert_iter->second->Release(); | 126 cert_iter->second->Release(); |
122 id_to_cert_.erase(cert_iter); | 127 id_to_cert_.erase(cert_iter); |
123 } | 128 } |
124 | 129 |
125 void CertStore::RemoveCertsForRenderProcesHost(int process_id) { | 130 void CertStoreImpl::RemoveCertsForRenderProcesHost(int process_id) { |
126 base::AutoLock auto_lock(cert_lock_); | 131 base::AutoLock auto_lock(cert_lock_); |
127 | 132 |
128 // We iterate through all the cert ids for that process. | 133 // We iterate through all the cert ids for that process. |
129 std::pair<IDMap::iterator, IDMap::iterator> process_ids = | 134 std::pair<IDMap::iterator, IDMap::iterator> process_ids = |
130 process_id_to_cert_id_.equal_range(process_id); | 135 process_id_to_cert_id_.equal_range(process_id); |
131 for (IDMap::iterator ids_iter = process_ids.first; | 136 for (IDMap::iterator ids_iter = process_ids.first; |
132 ids_iter != process_ids.second; ++ids_iter) { | 137 ids_iter != process_ids.second; ++ids_iter) { |
133 int cert_id = ids_iter->second; | 138 int cert_id = ids_iter->second; |
134 // Find all the processes referring to this cert id in | 139 // Find all the processes referring to this cert id in |
135 // cert_id_to_process_id_, then locate the process being removed within | 140 // cert_id_to_process_id_, then locate the process being removed within |
(...skipping 19 matching lines...) Expand all Loading... |
155 if (last_process_for_cert_id) { | 160 if (last_process_for_cert_id) { |
156 // The current cert id is not referenced by any other processes, so | 161 // The current cert id is not referenced by any other processes, so |
157 // remove it from id_to_cert_ and cert_to_id_. | 162 // remove it from id_to_cert_ and cert_to_id_. |
158 RemoveCertInternal(cert_id); | 163 RemoveCertInternal(cert_id); |
159 } | 164 } |
160 } | 165 } |
161 if (process_ids.first != process_ids.second) | 166 if (process_ids.first != process_ids.second) |
162 process_id_to_cert_id_.erase(process_ids.first, process_ids.second); | 167 process_id_to_cert_id_.erase(process_ids.first, process_ids.second); |
163 } | 168 } |
164 | 169 |
165 void CertStore::Observe(int type, | 170 void CertStoreImpl::Observe(int type, |
166 const content::NotificationSource& source, | 171 const content::NotificationSource& source, |
167 const content::NotificationDetails& details) { | 172 const content::NotificationDetails& details) { |
168 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED || | 173 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED || |
169 type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED); | 174 type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED); |
170 content::RenderProcessHost* rph = | 175 content::RenderProcessHost* rph = |
171 content::Source<content::RenderProcessHost>(source).ptr(); | 176 content::Source<content::RenderProcessHost>(source).ptr(); |
172 DCHECK(rph); | 177 DCHECK(rph); |
173 RemoveCertsForRenderProcesHost(rph->GetID()); | 178 RemoveCertsForRenderProcesHost(rph->GetID()); |
174 } | 179 } |
OLD | NEW |