OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/extras/sqlite/sqlite_channel_id_store.h" | 5 #include "net/extras/sqlite/sqlite_channel_id_store.h" |
6 | 6 |
| 7 #include <memory> |
7 #include <set> | 8 #include <set> |
8 #include <utility> | 9 #include <utility> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
13 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
14 #include "base/location.h" | 15 #include "base/location.h" |
15 #include "base/logging.h" | 16 #include "base/logging.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
19 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
21 #include "crypto/ec_private_key.h" | 21 #include "crypto/ec_private_key.h" |
22 #include "net/cert/asn1_util.h" | 22 #include "net/cert/asn1_util.h" |
23 #include "net/cert/x509_certificate.h" | 23 #include "net/cert/x509_certificate.h" |
24 #include "net/cookies/cookie_util.h" | 24 #include "net/cookies/cookie_util.h" |
25 #include "net/ssl/channel_id_service.h" | 25 #include "net/ssl/channel_id_service.h" |
26 #include "net/ssl/ssl_client_cert_type.h" | 26 #include "net/ssl/ssl_client_cert_type.h" |
27 #include "sql/error_delegate_util.h" | 27 #include "sql/error_delegate_util.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 friend class base::RefCountedThreadSafe<SQLiteChannelIDStore::Backend>; | 76 friend class base::RefCountedThreadSafe<SQLiteChannelIDStore::Backend>; |
77 | 77 |
78 // You should call Close() before destructing this object. | 78 // You should call Close() before destructing this object. |
79 virtual ~Backend() { | 79 virtual ~Backend() { |
80 DCHECK(!db_.get()) << "Close should have already been called."; | 80 DCHECK(!db_.get()) << "Close should have already been called."; |
81 DCHECK_EQ(0u, num_pending_); | 81 DCHECK_EQ(0u, num_pending_); |
82 DCHECK(pending_.empty()); | 82 DCHECK(pending_.empty()); |
83 } | 83 } |
84 | 84 |
85 void LoadInBackground( | 85 void LoadInBackground( |
86 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>>* channel_ids); | 86 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>>* |
| 87 channel_ids); |
87 | 88 |
88 // Database upgrade statements. | 89 // Database upgrade statements. |
89 bool EnsureDatabaseVersion(); | 90 bool EnsureDatabaseVersion(); |
90 | 91 |
91 class PendingOperation { | 92 class PendingOperation { |
92 public: | 93 public: |
93 enum OperationType { CHANNEL_ID_ADD, CHANNEL_ID_DELETE }; | 94 enum OperationType { CHANNEL_ID_ADD, CHANNEL_ID_DELETE }; |
94 | 95 |
95 PendingOperation(OperationType op, | 96 PendingOperation(OperationType op, |
96 const DefaultChannelIDStore::ChannelID& channel_id) | 97 const DefaultChannelIDStore::ChannelID& channel_id) |
(...skipping 22 matching lines...) Expand all Loading... |
119 // Close() executed on the background task runner. | 120 // Close() executed on the background task runner. |
120 void InternalBackgroundClose(); | 121 void InternalBackgroundClose(); |
121 | 122 |
122 void BackgroundDeleteAllInList( | 123 void BackgroundDeleteAllInList( |
123 const std::list<std::string>& server_identifiers); | 124 const std::list<std::string>& server_identifiers); |
124 | 125 |
125 void DatabaseErrorCallback(int error, sql::Statement* stmt); | 126 void DatabaseErrorCallback(int error, sql::Statement* stmt); |
126 void KillDatabase(); | 127 void KillDatabase(); |
127 | 128 |
128 const base::FilePath path_; | 129 const base::FilePath path_; |
129 scoped_ptr<sql::Connection> db_; | 130 std::unique_ptr<sql::Connection> db_; |
130 sql::MetaTable meta_table_; | 131 sql::MetaTable meta_table_; |
131 | 132 |
132 typedef std::list<PendingOperation*> PendingOperationsList; | 133 typedef std::list<PendingOperation*> PendingOperationsList; |
133 PendingOperationsList pending_; | 134 PendingOperationsList pending_; |
134 PendingOperationsList::size_type num_pending_; | 135 PendingOperationsList::size_type num_pending_; |
135 // True if the persistent store should skip clear on exit rules. | 136 // True if the persistent store should skip clear on exit rules. |
136 bool force_keep_session_state_; | 137 bool force_keep_session_state_; |
137 // Guard |pending_|, |num_pending_| and |force_keep_session_state_|. | 138 // Guard |pending_|, |num_pending_| and |force_keep_session_state_|. |
138 base::Lock lock_; | 139 base::Lock lock_; |
139 | 140 |
140 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | 141 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
141 | 142 |
142 // Indicates if the kill-database callback has been scheduled. | 143 // Indicates if the kill-database callback has been scheduled. |
143 bool corruption_detected_; | 144 bool corruption_detected_; |
144 | 145 |
145 DISALLOW_COPY_AND_ASSIGN(Backend); | 146 DISALLOW_COPY_AND_ASSIGN(Backend); |
146 }; | 147 }; |
147 | 148 |
148 void SQLiteChannelIDStore::Backend::Load( | 149 void SQLiteChannelIDStore::Backend::Load( |
149 const LoadedCallback& loaded_callback) { | 150 const LoadedCallback& loaded_callback) { |
150 // This function should be called only once per instance. | 151 // This function should be called only once per instance. |
151 DCHECK(!db_.get()); | 152 DCHECK(!db_.get()); |
152 scoped_ptr<std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>>> | 153 std::unique_ptr< |
| 154 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>>> |
153 channel_ids( | 155 channel_ids( |
154 new std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>>()); | 156 new std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>>()); |
155 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>>* channel_ids_ptr = | 157 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>>* |
156 channel_ids.get(); | 158 channel_ids_ptr = channel_ids.get(); |
157 | 159 |
158 background_task_runner_->PostTaskAndReply( | 160 background_task_runner_->PostTaskAndReply( |
159 FROM_HERE, | 161 FROM_HERE, |
160 base::Bind(&Backend::LoadInBackground, this, channel_ids_ptr), | 162 base::Bind(&Backend::LoadInBackground, this, channel_ids_ptr), |
161 base::Bind(loaded_callback, base::Passed(&channel_ids))); | 163 base::Bind(loaded_callback, base::Passed(&channel_ids))); |
162 } | 164 } |
163 | 165 |
164 void SQLiteChannelIDStore::Backend::LoadInBackground( | 166 void SQLiteChannelIDStore::Backend::LoadInBackground( |
165 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>>* channel_ids) { | 167 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>>* |
| 168 channel_ids) { |
166 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 169 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
167 | 170 |
168 // This method should be called only once per instance. | 171 // This method should be called only once per instance. |
169 DCHECK(!db_.get()); | 172 DCHECK(!db_.get()); |
170 | 173 |
171 base::TimeTicks start = base::TimeTicks::Now(); | 174 base::TimeTicks start = base::TimeTicks::Now(); |
172 | 175 |
173 // Ensure the parent directory for storing certs is created before reading | 176 // Ensure the parent directory for storing certs is created before reading |
174 // from it. | 177 // from it. |
175 const base::FilePath dir = path_.DirName(); | 178 const base::FilePath dir = path_.DirName(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 KillDatabase(); | 218 KillDatabase(); |
216 meta_table_.Reset(); | 219 meta_table_.Reset(); |
217 db_.reset(); | 220 db_.reset(); |
218 return; | 221 return; |
219 } | 222 } |
220 | 223 |
221 while (smt.Step()) { | 224 while (smt.Step()) { |
222 std::vector<uint8_t> private_key_from_db, public_key_from_db; | 225 std::vector<uint8_t> private_key_from_db, public_key_from_db; |
223 smt.ColumnBlobAsVector(1, &private_key_from_db); | 226 smt.ColumnBlobAsVector(1, &private_key_from_db); |
224 smt.ColumnBlobAsVector(2, &public_key_from_db); | 227 smt.ColumnBlobAsVector(2, &public_key_from_db); |
225 scoped_ptr<crypto::ECPrivateKey> key( | 228 std::unique_ptr<crypto::ECPrivateKey> key( |
226 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 229 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
227 ChannelIDService::kEPKIPassword, private_key_from_db, | 230 ChannelIDService::kEPKIPassword, private_key_from_db, |
228 public_key_from_db)); | 231 public_key_from_db)); |
229 if (!key) | 232 if (!key) |
230 continue; | 233 continue; |
231 scoped_ptr<DefaultChannelIDStore::ChannelID> channel_id( | 234 std::unique_ptr<DefaultChannelIDStore::ChannelID> channel_id( |
232 new DefaultChannelIDStore::ChannelID( | 235 new DefaultChannelIDStore::ChannelID( |
233 smt.ColumnString(0), // host | 236 smt.ColumnString(0), // host |
234 base::Time::FromInternalValue(smt.ColumnInt64(3)), std::move(key))); | 237 base::Time::FromInternalValue(smt.ColumnInt64(3)), std::move(key))); |
235 channel_ids->push_back(std::move(channel_id)); | 238 channel_ids->push_back(std::move(channel_id)); |
236 } | 239 } |
237 | 240 |
238 UMA_HISTOGRAM_COUNTS_10000( | 241 UMA_HISTOGRAM_COUNTS_10000( |
239 "DomainBoundCerts.DBLoadedCount", | 242 "DomainBoundCerts.DBLoadedCount", |
240 static_cast<base::HistogramBase::Sample>(channel_ids->size())); | 243 static_cast<base::HistogramBase::Sample>(channel_ids->size())); |
241 base::TimeDelta load_time = base::TimeTicks::Now() - start; | 244 base::TimeDelta load_time = base::TimeTicks::Now() - start; |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 | 408 |
406 void SQLiteChannelIDStore::Backend::BatchOperation( | 409 void SQLiteChannelIDStore::Backend::BatchOperation( |
407 PendingOperation::OperationType op, | 410 PendingOperation::OperationType op, |
408 const DefaultChannelIDStore::ChannelID& channel_id) { | 411 const DefaultChannelIDStore::ChannelID& channel_id) { |
409 // Commit every 30 seconds. | 412 // Commit every 30 seconds. |
410 static const int kCommitIntervalMs = 30 * 1000; | 413 static const int kCommitIntervalMs = 30 * 1000; |
411 // Commit right away if we have more than 512 outstanding operations. | 414 // Commit right away if we have more than 512 outstanding operations. |
412 static const size_t kCommitAfterBatchSize = 512; | 415 static const size_t kCommitAfterBatchSize = 512; |
413 | 416 |
414 // We do a full copy of the cert here, and hopefully just here. | 417 // We do a full copy of the cert here, and hopefully just here. |
415 scoped_ptr<PendingOperation> po(new PendingOperation(op, channel_id)); | 418 std::unique_ptr<PendingOperation> po(new PendingOperation(op, channel_id)); |
416 | 419 |
417 PendingOperationsList::size_type num_pending; | 420 PendingOperationsList::size_type num_pending; |
418 { | 421 { |
419 base::AutoLock locked(lock_); | 422 base::AutoLock locked(lock_); |
420 pending_.push_back(po.release()); | 423 pending_.push_back(po.release()); |
421 num_pending = ++num_pending_; | 424 num_pending = ++num_pending_; |
422 } | 425 } |
423 | 426 |
424 if (num_pending == 1) { | 427 if (num_pending == 1) { |
425 // We've gotten our first entry for this batch, fire off the timer. | 428 // We've gotten our first entry for this batch, fire off the timer. |
(...skipping 14 matching lines...) Expand all Loading... |
440 base::AutoLock locked(lock_); | 443 base::AutoLock locked(lock_); |
441 | 444 |
442 for (PendingOperationsList::iterator it = pending_.begin(); | 445 for (PendingOperationsList::iterator it = pending_.begin(); |
443 it != pending_.end();) { | 446 it != pending_.end();) { |
444 bool remove = | 447 bool remove = |
445 std::find(server_identifiers.begin(), server_identifiers.end(), | 448 std::find(server_identifiers.begin(), server_identifiers.end(), |
446 (*it)->channel_id().server_identifier()) != | 449 (*it)->channel_id().server_identifier()) != |
447 server_identifiers.end(); | 450 server_identifiers.end(); |
448 | 451 |
449 if (remove) { | 452 if (remove) { |
450 scoped_ptr<PendingOperation> po(*it); | 453 std::unique_ptr<PendingOperation> po(*it); |
451 it = pending_.erase(it); | 454 it = pending_.erase(it); |
452 --num_pending_; | 455 --num_pending_; |
453 } else { | 456 } else { |
454 ++it; | 457 ++it; |
455 } | 458 } |
456 } | 459 } |
457 } | 460 } |
458 | 461 |
459 void SQLiteChannelIDStore::Backend::Commit() { | 462 void SQLiteChannelIDStore::Backend::Commit() { |
460 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 463 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
(...skipping 21 matching lines...) Expand all Loading... |
482 if (!del_statement.is_valid()) | 485 if (!del_statement.is_valid()) |
483 return; | 486 return; |
484 | 487 |
485 sql::Transaction transaction(db_.get()); | 488 sql::Transaction transaction(db_.get()); |
486 if (!transaction.Begin()) | 489 if (!transaction.Begin()) |
487 return; | 490 return; |
488 | 491 |
489 for (PendingOperationsList::iterator it = ops.begin(); it != ops.end(); | 492 for (PendingOperationsList::iterator it = ops.begin(); it != ops.end(); |
490 ++it) { | 493 ++it) { |
491 // Free the certs as we commit them to the database. | 494 // Free the certs as we commit them to the database. |
492 scoped_ptr<PendingOperation> po(*it); | 495 std::unique_ptr<PendingOperation> po(*it); |
493 switch (po->op()) { | 496 switch (po->op()) { |
494 case PendingOperation::CHANNEL_ID_ADD: { | 497 case PendingOperation::CHANNEL_ID_ADD: { |
495 add_statement.Reset(true); | 498 add_statement.Reset(true); |
496 add_statement.BindString(0, po->channel_id().server_identifier()); | 499 add_statement.BindString(0, po->channel_id().server_identifier()); |
497 std::vector<uint8_t> private_key, public_key; | 500 std::vector<uint8_t> private_key, public_key; |
498 if (!po->channel_id().key()->ExportEncryptedPrivateKey( | 501 if (!po->channel_id().key()->ExportEncryptedPrivateKey( |
499 ChannelIDService::kEPKIPassword, 1, &private_key)) | 502 ChannelIDService::kEPKIPassword, 1, &private_key)) |
500 continue; | 503 continue; |
501 if (!po->channel_id().key()->ExportPublicKey(&public_key)) | 504 if (!po->channel_id().key()->ExportPublicKey(&public_key)) |
502 continue; | 505 continue; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 backend_->SetForceKeepSessionState(); | 613 backend_->SetForceKeepSessionState(); |
611 } | 614 } |
612 | 615 |
613 SQLiteChannelIDStore::~SQLiteChannelIDStore() { | 616 SQLiteChannelIDStore::~SQLiteChannelIDStore() { |
614 backend_->Close(); | 617 backend_->Close(); |
615 // We release our reference to the Backend, though it will probably still have | 618 // We release our reference to the Backend, though it will probably still have |
616 // a reference if the background task runner has not run Close() yet. | 619 // a reference if the background task runner has not run Close() yet. |
617 } | 620 } |
618 | 621 |
619 } // namespace net | 622 } // namespace net |
OLD | NEW |