1 // Copyright 2013 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 "content/browser/media/webrtc_identity_store_backend.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/strings/string_util.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "net/base/net_errors.h"
13 #include "sql/statement.h"
14 #include "sql/transaction.h"
15 #include "storage/browser/quota/special_storage_policy.h"
20 static const char kWebRTCIdentityStoreDBName
[] = "webrtc_identity_store";
22 static const base::FilePath::CharType kWebRTCIdentityStoreDirectory
[] =
23 FILE_PATH_LITERAL("WebRTCIdentityStore");
25 // Initializes the identity table, returning true on success.
26 static bool InitDB(sql::Connection
* db
) {
27 if (db
->DoesTableExist(kWebRTCIdentityStoreDBName
)) {
28 if (db
->DoesColumnExist(kWebRTCIdentityStoreDBName
, "origin") &&
29 db
->DoesColumnExist(kWebRTCIdentityStoreDBName
, "identity_name") &&
30 db
->DoesColumnExist(kWebRTCIdentityStoreDBName
, "common_name") &&
31 db
->DoesColumnExist(kWebRTCIdentityStoreDBName
, "certificate") &&
32 db
->DoesColumnExist(kWebRTCIdentityStoreDBName
, "private_key") &&
33 db
->DoesColumnExist(kWebRTCIdentityStoreDBName
, "creation_time"))
36 if (!db
->Execute("DROP TABLE webrtc_identity_store"))
41 "CREATE TABLE webrtc_identity_store"
43 "origin TEXT NOT NULL,"
44 "identity_name TEXT NOT NULL,"
45 "common_name TEXT NOT NULL,"
46 "certificate BLOB NOT NULL,"
47 "private_key BLOB NOT NULL,"
48 "creation_time INTEGER)");
51 struct WebRTCIdentityStoreBackend::IdentityKey
{
52 IdentityKey(const GURL
& origin
, const std::string
& identity_name
)
53 : origin(origin
), identity_name(identity_name
) {}
55 bool operator<(const IdentityKey
& other
) const {
56 return origin
< other
.origin
||
57 (origin
== other
.origin
&& identity_name
< other
.identity_name
);
61 std::string identity_name
;
64 struct WebRTCIdentityStoreBackend::Identity
{
65 Identity(const std::string
& common_name
,
66 const std::string
& certificate
,
67 const std::string
& private_key
)
68 : common_name(common_name
),
69 certificate(certificate
),
70 private_key(private_key
),
71 creation_time(base::Time::Now().ToInternalValue()) {}
73 Identity(const std::string
& common_name
,
74 const std::string
& certificate
,
75 const std::string
& private_key
,
77 : common_name(common_name
),
78 certificate(certificate
),
79 private_key(private_key
),
80 creation_time(creation_time
) {}
82 std::string common_name
;
83 std::string certificate
;
84 std::string private_key
;
88 struct WebRTCIdentityStoreBackend::PendingFindRequest
{
89 PendingFindRequest(const GURL
& origin
,
90 const std::string
& identity_name
,
91 const std::string
& common_name
,
92 const FindIdentityCallback
& callback
)
94 identity_name(identity_name
),
95 common_name(common_name
),
98 ~PendingFindRequest() {}
101 std::string identity_name
;
102 std::string common_name
;
103 FindIdentityCallback callback
;
106 // The class encapsulates the database operations. All members except ctor and
107 // dtor should be accessed on the DB thread.
108 // It can be created/destroyed on any thread.
109 class WebRTCIdentityStoreBackend::SqlLiteStorage
110 : public base::RefCountedThreadSafe
<SqlLiteStorage
> {
112 SqlLiteStorage(base::TimeDelta validity_period
,
113 const base::FilePath
& path
,
114 storage::SpecialStoragePolicy
* policy
)
115 : validity_period_(validity_period
), special_storage_policy_(policy
) {
117 path_
= path
.Append(kWebRTCIdentityStoreDirectory
);
120 void Load(IdentityMap
* out_map
);
122 void AddIdentity(const GURL
& origin
,
123 const std::string
& identity_name
,
124 const Identity
& identity
);
125 void DeleteIdentity(const GURL
& origin
,
126 const std::string
& identity_name
,
127 const Identity
& identity
);
128 void DeleteBetween(base::Time delete_begin
, base::Time delete_end
);
130 void SetValidityPeriodForTesting(base::TimeDelta validity_period
) {
131 DCHECK_CURRENTLY_ON(BrowserThread::DB
);
133 validity_period_
= validity_period
;
137 friend class base::RefCountedThreadSafe
<SqlLiteStorage
>;
143 struct PendingOperation
{
144 PendingOperation(OperationType type
,
146 const std::string
& identity_name
,
147 const Identity
& identity
)
150 identity_name(identity_name
),
151 identity(identity
) {}
155 std::string identity_name
;
158 typedef ScopedVector
<PendingOperation
> PendingOperationList
;
160 virtual ~SqlLiteStorage() {}
161 void OnDatabaseError(int error
, sql::Statement
* stmt
);
162 void BatchOperation(OperationType type
,
164 const std::string
& identity_name
,
165 const Identity
& identity
);
168 base::TimeDelta validity_period_
;
169 // The file path of the DB. Empty if temporary.
170 base::FilePath path_
;
171 scoped_refptr
<storage::SpecialStoragePolicy
> special_storage_policy_
;
172 scoped_ptr
<sql::Connection
> db_
;
173 // Batched DB operations pending to commit.
174 PendingOperationList pending_operations_
;
176 DISALLOW_COPY_AND_ASSIGN(SqlLiteStorage
);
179 WebRTCIdentityStoreBackend::WebRTCIdentityStoreBackend(
180 const base::FilePath
& path
,
181 storage::SpecialStoragePolicy
* policy
,
182 base::TimeDelta validity_period
)
183 : validity_period_(validity_period
),
185 sql_lite_storage_(new SqlLiteStorage(validity_period
, path
, policy
)) {
188 bool WebRTCIdentityStoreBackend::FindIdentity(
190 const std::string
& identity_name
,
191 const std::string
& common_name
,
192 const FindIdentityCallback
& callback
) {
193 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
194 if (state_
== CLOSED
)
197 if (state_
!= LOADED
) {
198 // Queues the request to wait for the DB to load.
199 pending_find_requests_
.push_back(
200 new PendingFindRequest(origin
, identity_name
, common_name
, callback
));
201 if (state_
== LOADING
)
204 DCHECK_EQ(state_
, NOT_STARTED
);
206 // Kick off loading the DB.
207 scoped_ptr
<IdentityMap
> out_map(new IdentityMap());
209 base::Bind(&SqlLiteStorage::Load
, sql_lite_storage_
, out_map
.get()));
210 // |out_map| will be NULL after this call.
211 if (BrowserThread::PostTaskAndReply(
215 base::Bind(&WebRTCIdentityStoreBackend::OnLoaded
,
217 base::Passed(&out_map
)))) {
221 // If it fails to post task, falls back to ERR_FILE_NOT_FOUND.
224 IdentityKey
key(origin
, identity_name
);
225 IdentityMap::iterator iter
= identities_
.find(key
);
226 if (iter
!= identities_
.end() && iter
->second
.common_name
== common_name
) {
227 base::TimeDelta age
= base::Time::Now() - base::Time::FromInternalValue(
228 iter
->second
.creation_time
);
229 if (age
< validity_period_
) {
231 return BrowserThread::PostTask(BrowserThread::IO
,
235 iter
->second
.certificate
,
236 iter
->second
.private_key
));
238 // Removes the expired identity from the in-memory cache. The copy in the
239 // database will be removed on the next load.
240 identities_
.erase(iter
);
243 return BrowserThread::PostTask(
246 base::Bind(callback
, net::ERR_FILE_NOT_FOUND
, "", ""));
249 void WebRTCIdentityStoreBackend::AddIdentity(const GURL
& origin
,
250 const std::string
& identity_name
,
251 const std::string
& common_name
,
252 const std::string
& certificate
,
253 const std::string
& private_key
) {
254 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
255 if (state_
== CLOSED
)
258 // If there is an existing identity for the same origin and identity_name,
260 IdentityKey
key(origin
, identity_name
);
261 Identity
identity(common_name
, certificate
, private_key
);
263 if (identities_
.find(key
) != identities_
.end()) {
264 if (!BrowserThread::PostTask(BrowserThread::DB
,
266 base::Bind(&SqlLiteStorage::DeleteIdentity
,
270 identities_
.find(key
)->second
)))
273 identities_
.insert(std::pair
<IdentityKey
, Identity
>(key
, identity
));
275 BrowserThread::PostTask(BrowserThread::DB
,
277 base::Bind(&SqlLiteStorage::AddIdentity
,
284 void WebRTCIdentityStoreBackend::Close() {
285 if (!BrowserThread::CurrentlyOn(BrowserThread::IO
)) {
286 BrowserThread::PostTask(
289 base::Bind(&WebRTCIdentityStoreBackend::Close
, this));
293 if (state_
== CLOSED
)
297 BrowserThread::PostTask(
300 base::Bind(&SqlLiteStorage::Close
, sql_lite_storage_
));
303 void WebRTCIdentityStoreBackend::DeleteBetween(base::Time delete_begin
,
304 base::Time delete_end
,
305 const base::Closure
& callback
) {
306 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
307 if (state_
== CLOSED
)
310 // Delete the in-memory cache.
311 IdentityMap::iterator it
= identities_
.begin();
312 while (it
!= identities_
.end()) {
313 if (it
->second
.creation_time
>= delete_begin
.ToInternalValue() &&
314 it
->second
.creation_time
<= delete_end
.ToInternalValue()) {
315 identities_
.erase(it
++);
320 BrowserThread::PostTaskAndReply(BrowserThread::DB
,
322 base::Bind(&SqlLiteStorage::DeleteBetween
,
329 void WebRTCIdentityStoreBackend::SetValidityPeriodForTesting(
330 base::TimeDelta validity_period
) {
331 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
332 validity_period_
= validity_period
;
333 BrowserThread::PostTask(
336 base::Bind(&SqlLiteStorage::SetValidityPeriodForTesting
,
341 WebRTCIdentityStoreBackend::~WebRTCIdentityStoreBackend() {}
343 void WebRTCIdentityStoreBackend::OnLoaded(scoped_ptr
<IdentityMap
> out_map
) {
344 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
346 if (state_
!= LOADING
)
349 DVLOG(3) << "WebRTC identity store has loaded.";
352 identities_
.swap(*out_map
);
354 for (size_t i
= 0; i
< pending_find_requests_
.size(); ++i
) {
355 FindIdentity(pending_find_requests_
[i
]->origin
,
356 pending_find_requests_
[i
]->identity_name
,
357 pending_find_requests_
[i
]->common_name
,
358 pending_find_requests_
[i
]->callback
);
359 delete pending_find_requests_
[i
];
361 pending_find_requests_
.clear();
365 // Implementation of SqlLiteStorage.
368 void WebRTCIdentityStoreBackend::SqlLiteStorage::Load(IdentityMap
* out_map
) {
369 DCHECK_CURRENTLY_ON(BrowserThread::DB
);
372 // Ensure the parent directory for storing certs is created before reading
374 const base::FilePath dir
= path_
.DirName();
375 if (!base::PathExists(dir
) && !base::CreateDirectory(dir
)) {
376 DVLOG(2) << "Unable to open DB file path.";
380 db_
.reset(new sql::Connection());
382 db_
->set_error_callback(base::Bind(&SqlLiteStorage::OnDatabaseError
, this));
384 if (!db_
->Open(path_
)) {
385 DVLOG(2) << "Unable to open DB.";
390 if (!InitDB(db_
.get())) {
391 DVLOG(2) << "Unable to init DB.";
398 // Delete expired identities.
399 DeleteBetween(base::Time(), base::Time::Now() - validity_period_
);
401 // Slurp all the identities into the out_map.
402 sql::Statement
stmt(db_
->GetUniqueStatement(
403 "SELECT origin, identity_name, common_name, "
404 "certificate, private_key, creation_time "
405 "FROM webrtc_identity_store"));
406 CHECK(stmt
.is_valid());
408 while (stmt
.Step()) {
409 IdentityKey
key(GURL(stmt
.ColumnString(0)), stmt
.ColumnString(1));
410 std::string
common_name(stmt
.ColumnString(2));
411 std::string cert
, private_key
;
412 stmt
.ColumnBlobAsString(3, &cert
);
413 stmt
.ColumnBlobAsString(4, &private_key
);
414 int64 creation_time
= stmt
.ColumnInt64(5);
415 std::pair
<IdentityMap::iterator
, bool> result
=
416 out_map
->insert(std::pair
<IdentityKey
, Identity
>(
417 key
, Identity(common_name
, cert
, private_key
, creation_time
)));
418 DCHECK(result
.second
);
422 void WebRTCIdentityStoreBackend::SqlLiteStorage::Close() {
423 DCHECK_CURRENTLY_ON(BrowserThread::DB
);
428 void WebRTCIdentityStoreBackend::SqlLiteStorage::AddIdentity(
430 const std::string
& identity_name
,
431 const Identity
& identity
) {
432 DCHECK_CURRENTLY_ON(BrowserThread::DB
);
436 // Do not add for session only origins.
437 if (special_storage_policy_
.get() &&
438 !special_storage_policy_
->IsStorageProtected(origin
) &&
439 special_storage_policy_
->IsStorageSessionOnly(origin
)) {
442 BatchOperation(ADD_IDENTITY
, origin
, identity_name
, identity
);
445 void WebRTCIdentityStoreBackend::SqlLiteStorage::DeleteIdentity(
447 const std::string
& identity_name
,
448 const Identity
& identity
) {
449 DCHECK_CURRENTLY_ON(BrowserThread::DB
);
452 BatchOperation(DELETE_IDENTITY
, origin
, identity_name
, identity
);
455 void WebRTCIdentityStoreBackend::SqlLiteStorage::DeleteBetween(
456 base::Time delete_begin
,
457 base::Time delete_end
) {
458 DCHECK_CURRENTLY_ON(BrowserThread::DB
);
462 // Commit pending operations first.
465 sql::Statement
del_stmt(db_
->GetCachedStatement(
467 "DELETE FROM webrtc_identity_store"
468 " WHERE creation_time >= ? AND creation_time <= ?"));
469 CHECK(del_stmt
.is_valid());
471 del_stmt
.BindInt64(0, delete_begin
.ToInternalValue());
472 del_stmt
.BindInt64(1, delete_end
.ToInternalValue());
474 sql::Transaction
transaction(db_
.get());
475 if (!transaction
.Begin()) {
476 DVLOG(2) << "Failed to begin the transaction.";
480 if (!del_stmt
.Run()) {
481 DVLOG(2) << "Failed to run the delete statement.";
485 if (!transaction
.Commit())
486 DVLOG(2) << "Failed to commit the transaction.";
489 void WebRTCIdentityStoreBackend::SqlLiteStorage::OnDatabaseError(
491 sql::Statement
* stmt
) {
492 DCHECK_CURRENTLY_ON(BrowserThread::DB
);
495 // It's not safe to reset |db_| here.
498 void WebRTCIdentityStoreBackend::SqlLiteStorage::BatchOperation(
501 const std::string
& identity_name
,
502 const Identity
& identity
) {
503 DCHECK_CURRENTLY_ON(BrowserThread::DB
);
504 // Commit every 30 seconds.
505 static const base::TimeDelta
kCommitInterval(
506 base::TimeDelta::FromSeconds(30));
507 // Commit right away if we have more than 512 outstanding operations.
508 static const size_t kCommitAfterBatchSize
= 512;
510 // We do a full copy of the cert here, and hopefully just here.
511 scoped_ptr
<PendingOperation
> operation(
512 new PendingOperation(type
, origin
, identity_name
, identity
));
514 pending_operations_
.push_back(operation
.release());
516 if (pending_operations_
.size() == 1) {
517 // We've gotten our first entry for this batch, fire off the timer.
518 BrowserThread::PostDelayedTask(BrowserThread::DB
,
520 base::Bind(&SqlLiteStorage::Commit
, this),
522 } else if (pending_operations_
.size() >= kCommitAfterBatchSize
) {
523 // We've reached a big enough batch, fire off a commit now.
524 BrowserThread::PostTask(BrowserThread::DB
,
526 base::Bind(&SqlLiteStorage::Commit
, this));
530 void WebRTCIdentityStoreBackend::SqlLiteStorage::Commit() {
531 DCHECK_CURRENTLY_ON(BrowserThread::DB
);
532 // Maybe an old timer fired or we are already Close()'ed.
533 if (!db_
.get() || pending_operations_
.empty())
536 sql::Statement
add_stmt(db_
->GetCachedStatement(
538 "INSERT INTO webrtc_identity_store "
539 "(origin, identity_name, common_name, certificate,"
540 " private_key, creation_time) VALUES"
543 CHECK(add_stmt
.is_valid());
545 sql::Statement
del_stmt(db_
->GetCachedStatement(
547 "DELETE FROM webrtc_identity_store WHERE origin=? AND identity_name=?"));
549 CHECK(del_stmt
.is_valid());
551 sql::Transaction
transaction(db_
.get());
552 if (!transaction
.Begin()) {
553 DVLOG(2) << "Failed to begin the transaction.";
557 // Swaps |pending_operations_| into a temporary list to make sure
558 // |pending_operations_| is always cleared in case of DB errors.
559 PendingOperationList pending_operations_copy
;
560 pending_operations_
.swap(pending_operations_copy
);
562 for (PendingOperationList::const_iterator it
=
563 pending_operations_copy
.begin();
564 it
!= pending_operations_copy
.end();
566 switch ((*it
)->type
) {
568 add_stmt
.Reset(true);
569 add_stmt
.BindString(0, (*it
)->origin
.spec());
570 add_stmt
.BindString(1, (*it
)->identity_name
);
571 add_stmt
.BindString(2, (*it
)->identity
.common_name
);
572 const std::string
& cert
= (*it
)->identity
.certificate
;
573 add_stmt
.BindBlob(3, cert
.data(), cert
.size());
574 const std::string
& private_key
= (*it
)->identity
.private_key
;
575 add_stmt
.BindBlob(4, private_key
.data(), private_key
.size());
576 add_stmt
.BindInt64(5, (*it
)->identity
.creation_time
);
577 if (!add_stmt
.Run()) {
578 DVLOG(2) << "Failed to add the identity to DB.";
583 case DELETE_IDENTITY
:
584 del_stmt
.Reset(true);
585 del_stmt
.BindString(0, (*it
)->origin
.spec());
586 del_stmt
.BindString(1, (*it
)->identity_name
);
587 if (!del_stmt
.Run()) {
588 DVLOG(2) << "Failed to delete the identity from DB.";
599 if (!transaction
.Commit())
600 DVLOG(2) << "Failed to commit the transaction.";
603 } // namespace content