// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ #define COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ #include #include #include "base/bind.h" #include "base/files/file_path.h" #include "base/memory/scoped_ptr.h" #include "base/message_loop/message_loop.h" #include "base/sequenced_task_runner.h" #include "base/strings/string_util.h" #include "base/threading/sequenced_worker_pool.h" #include "base/threading/thread_checker.h" #include "base/strings/string_split.h" #include "components/leveldb_proto/leveldb_database.h" #include "components/leveldb_proto/proto_database.h" namespace leveldb_proto { typedef base::StringPairs KeyValueVector; typedef std::vector KeyVector; // When the ProtoDatabaseImpl instance is deleted, in-progress asynchronous // operations will be completed and the corresponding callbacks will be called. // Construction/calls/destruction should all happen on the same thread. template class ProtoDatabaseImpl : public ProtoDatabase { public: // All blocking calls/disk access will happen on the provided |task_runner|. explicit ProtoDatabaseImpl( scoped_refptr task_runner); virtual ~ProtoDatabaseImpl(); // ProtoDatabase implementation. // TODO(cjhopman): Perhaps Init() shouldn't be exposed to users and not just // part of the constructor virtual void Init(const base::FilePath& database_dir, typename ProtoDatabase::InitCallback callback) override; virtual void UpdateEntries( scoped_ptr::KeyEntryVector> entries_to_save, scoped_ptr keys_to_remove, typename ProtoDatabase::UpdateCallback callback) override; virtual void LoadEntries( typename ProtoDatabase::LoadCallback callback) override; // Allow callers to provide their own Database implementation. void InitWithDatabase(scoped_ptr database, const base::FilePath& database_dir, typename ProtoDatabase::InitCallback callback); private: base::ThreadChecker thread_checker_; // Used to run blocking tasks in-order. scoped_refptr task_runner_; scoped_ptr db_; DISALLOW_COPY_AND_ASSIGN(ProtoDatabaseImpl); }; namespace { template void RunInitCallback(typename ProtoDatabase::InitCallback callback, const bool* success) { callback.Run(*success); } template void RunUpdateCallback(typename ProtoDatabase::UpdateCallback callback, const bool* success) { callback.Run(*success); } template void RunLoadCallback(typename ProtoDatabase::LoadCallback callback, const bool* success, scoped_ptr > entries) { callback.Run(*success, entries.Pass()); } void InitFromTaskRunner(LevelDB* database, const base::FilePath& database_dir, bool* success) { DCHECK(success); // TODO(cjhopman): Histogram for database size. *success = database->Init(database_dir); } template void UpdateEntriesFromTaskRunner( LevelDB* database, scoped_ptr::KeyEntryVector> entries_to_save, scoped_ptr keys_to_remove, bool* success) { DCHECK(success); // Serialize the values from Proto to string before passing on to database. KeyValueVector pairs_to_save; for (typename ProtoDatabase::KeyEntryVector::iterator it = entries_to_save->begin(); it != entries_to_save->end(); ++it) { pairs_to_save.push_back( std::make_pair(it->first, it->second.SerializeAsString())); } *success = database->Save(pairs_to_save, *keys_to_remove); } template void LoadEntriesFromTaskRunner(LevelDB* database, std::vector* entries, bool* success) { DCHECK(success); DCHECK(entries); entries->clear(); std::vector loaded_entries; *success = database->Load(&loaded_entries); for (std::vector::iterator it = loaded_entries.begin(); it != loaded_entries.end(); ++it) { T entry; if (!entry.ParseFromString(*it)) { DLOG(WARNING) << "Unable to parse leveldb_proto entry " << *it; // TODO(cjhopman): Decide what to do about un-parseable entries. } entries->push_back(entry); } } } // namespace template ProtoDatabaseImpl::ProtoDatabaseImpl( scoped_refptr task_runner) : task_runner_(task_runner) {} template ProtoDatabaseImpl::~ProtoDatabaseImpl() { DCHECK(thread_checker_.CalledOnValidThread()); if (!task_runner_->DeleteSoon(FROM_HERE, db_.release())) { DLOG(WARNING) << "DOM distiller database will not be deleted."; } } template void ProtoDatabaseImpl::Init( const base::FilePath& database_dir, typename ProtoDatabase::InitCallback callback) { DCHECK(thread_checker_.CalledOnValidThread()); InitWithDatabase(scoped_ptr(new LevelDB()), database_dir, callback); } template void ProtoDatabaseImpl::InitWithDatabase( scoped_ptr database, const base::FilePath& database_dir, typename ProtoDatabase::InitCallback callback) { DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(!db_); DCHECK(database); db_.reset(database.release()); bool* success = new bool(false); task_runner_->PostTaskAndReply( FROM_HERE, base::Bind(InitFromTaskRunner, base::Unretained(db_.get()), database_dir, success), base::Bind(RunInitCallback, callback, base::Owned(success))); } template void ProtoDatabaseImpl::UpdateEntries( scoped_ptr::KeyEntryVector> entries_to_save, scoped_ptr keys_to_remove, typename ProtoDatabase::UpdateCallback callback) { DCHECK(thread_checker_.CalledOnValidThread()); bool* success = new bool(false); task_runner_->PostTaskAndReply( FROM_HERE, base::Bind(UpdateEntriesFromTaskRunner, base::Unretained(db_.get()), base::Passed(&entries_to_save), base::Passed(&keys_to_remove), success), base::Bind(RunUpdateCallback, callback, base::Owned(success))); } template void ProtoDatabaseImpl::LoadEntries( typename ProtoDatabase::LoadCallback callback) { DCHECK(thread_checker_.CalledOnValidThread()); bool* success = new bool(false); scoped_ptr > entries(new std::vector()); // Get this pointer before entries is base::Passed() so we can use it below. std::vector* entries_ptr = entries.get(); task_runner_->PostTaskAndReply( FROM_HERE, base::Bind(LoadEntriesFromTaskRunner, base::Unretained(db_.get()), entries_ptr, success), base::Bind(RunLoadCallback, callback, base::Owned(success), base::Passed(&entries))); } } // namespace leveldb_proto #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_