Updating trunk VERSION from 1014.0 to 1015.0
[chromium-blink-merge.git] / net / base / default_origin_bound_cert_store.cc
blob3c311ca243510724b8673bada314222596a077cd
1 // Copyright (c) 2011 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 "net/base/default_origin_bound_cert_store.h"
7 #include "base/bind.h"
8 #include "base/message_loop.h"
10 namespace net {
12 // static
13 const size_t DefaultOriginBoundCertStore::kMaxCerts = 3300;
15 DefaultOriginBoundCertStore::DefaultOriginBoundCertStore(
16 PersistentStore* store)
17 : initialized_(false),
18 store_(store) {}
20 void DefaultOriginBoundCertStore::FlushStore(
21 const base::Closure& completion_task) {
22 base::AutoLock autolock(lock_);
24 if (initialized_ && store_)
25 store_->Flush(completion_task);
26 else if (!completion_task.is_null())
27 MessageLoop::current()->PostTask(FROM_HERE, completion_task);
30 bool DefaultOriginBoundCertStore::GetOriginBoundCert(
31 const std::string& origin,
32 SSLClientCertType* type,
33 base::Time* expiration_time,
34 std::string* private_key_result,
35 std::string* cert_result) {
36 base::AutoLock autolock(lock_);
37 InitIfNecessary();
39 OriginBoundCertMap::iterator it = origin_bound_certs_.find(origin);
41 if (it == origin_bound_certs_.end())
42 return false;
44 OriginBoundCert* cert = it->second;
45 *type = cert->type();
46 *expiration_time = cert->expiration_time();
47 *private_key_result = cert->private_key();
48 *cert_result = cert->cert();
50 return true;
53 void DefaultOriginBoundCertStore::SetOriginBoundCert(
54 const std::string& origin,
55 SSLClientCertType type,
56 base::Time expiration_time,
57 const std::string& private_key,
58 const std::string& cert) {
59 base::AutoLock autolock(lock_);
60 InitIfNecessary();
62 InternalDeleteOriginBoundCert(origin);
63 InternalInsertOriginBoundCert(
64 origin,
65 new OriginBoundCert(origin, type, expiration_time, private_key, cert));
68 void DefaultOriginBoundCertStore::DeleteOriginBoundCert(
69 const std::string& origin) {
70 base::AutoLock autolock(lock_);
71 InitIfNecessary();
72 InternalDeleteOriginBoundCert(origin);
75 void DefaultOriginBoundCertStore::DeleteAll() {
76 base::AutoLock autolock(lock_);
77 InitIfNecessary();
78 for (OriginBoundCertMap::iterator it = origin_bound_certs_.begin();
79 it != origin_bound_certs_.end(); ++it) {
80 OriginBoundCert* cert = it->second;
81 if (store_)
82 store_->DeleteOriginBoundCert(*cert);
83 delete cert;
85 origin_bound_certs_.clear();
88 void DefaultOriginBoundCertStore::GetAllOriginBoundCerts(
89 std::vector<OriginBoundCert>* origin_bound_certs) {
90 base::AutoLock autolock(lock_);
91 InitIfNecessary();
92 for (OriginBoundCertMap::iterator it = origin_bound_certs_.begin();
93 it != origin_bound_certs_.end(); ++it) {
94 origin_bound_certs->push_back(*it->second);
98 int DefaultOriginBoundCertStore::GetCertCount() {
99 base::AutoLock autolock(lock_);
100 InitIfNecessary();
102 return origin_bound_certs_.size();
105 DefaultOriginBoundCertStore::~DefaultOriginBoundCertStore() {
106 DeleteAllInMemory();
109 void DefaultOriginBoundCertStore::DeleteAllInMemory() {
110 base::AutoLock autolock(lock_);
112 for (OriginBoundCertMap::iterator it = origin_bound_certs_.begin();
113 it != origin_bound_certs_.end(); ++it) {
114 delete it->second;
116 origin_bound_certs_.clear();
119 void DefaultOriginBoundCertStore::InitStore() {
120 lock_.AssertAcquired();
122 DCHECK(store_) << "Store must exist to initialize";
124 // Initialize the store and sync in any saved persistent certs.
125 std::vector<OriginBoundCert*> certs;
126 // Reserve space for the maximum amount of certs a database should have.
127 // This prevents multiple vector growth / copies as we append certs.
128 certs.reserve(kMaxCerts);
129 store_->Load(&certs);
131 for (std::vector<OriginBoundCert*>::const_iterator it = certs.begin();
132 it != certs.end(); ++it) {
133 origin_bound_certs_[(*it)->origin()] = *it;
137 void DefaultOriginBoundCertStore::InternalDeleteOriginBoundCert(
138 const std::string& origin) {
139 lock_.AssertAcquired();
141 OriginBoundCertMap::iterator it = origin_bound_certs_.find(origin);
142 if (it == origin_bound_certs_.end())
143 return; // There is nothing to delete.
145 OriginBoundCert* cert = it->second;
146 if (store_)
147 store_->DeleteOriginBoundCert(*cert);
148 origin_bound_certs_.erase(it);
149 delete cert;
152 void DefaultOriginBoundCertStore::InternalInsertOriginBoundCert(
153 const std::string& origin,
154 OriginBoundCert* cert) {
155 lock_.AssertAcquired();
157 if (store_)
158 store_->AddOriginBoundCert(*cert);
159 origin_bound_certs_[origin] = cert;
162 DefaultOriginBoundCertStore::PersistentStore::PersistentStore() {}
164 } // namespace net