Chromium Code Reviews| Index: components/cert_database/core/cert_database_service.cc |
| diff --git a/components/cert_database/core/cert_database_service.cc b/components/cert_database/core/cert_database_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7b89c78563ff76071843517401c176db1ad81458 |
| --- /dev/null |
| +++ b/components/cert_database/core/cert_database_service.cc |
| @@ -0,0 +1,84 @@ |
| +// 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. |
| + |
| +#include "components/cert_database/public/cert_database_service.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/callback.h" |
| +#include "base/location.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/thread_task_runner_handle.h" |
| +#include "components/cert_database/public/cert_database_service_io_part.h" |
| +#include "net/cert/nss_cert_database.h" |
| + |
| +namespace cert_database { |
| + |
| +namespace { |
| + |
| +void CallBackOnTaskRunner( |
| + const scoped_refptr<base::TaskRunner>& task_runner, |
| + const CertDatabaseService::GetCertDBCallback& callback, |
| + net::NSSCertDatabase* cert_db) { |
| + task_runner->PostTask(FROM_HERE, base::Bind(callback, cert_db)); |
| +} |
| + |
| +void GetNSSCertDBOnIOThread( |
| + base::WeakPtr<CertDatabaseServiceIOPart> io_part, |
| + const CertDatabaseService::GetCertDBCallback& callback) { |
| + if (!io_part) |
| + return; |
| + net::NSSCertDatabase* db = io_part->GetNSSCertDatabase(callback); |
| + if (db) |
| + callback.Run(db); |
| +} |
| + |
| +void DestroyIOPart(const base::WeakPtr<CertDatabaseServiceIOPart>& io_part) { |
| + if (!io_part) { |
| + LOG(WARNING) << "CertDatabaseServiceIOPart already destructed."; |
| + return; |
| + } |
| + delete io_part.get(); |
| +} |
| + |
| +} // namespace |
| + |
| +CertDatabaseService::CertDatabaseService( |
| + const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) |
| + : io_task_runner_(io_task_runner), is_io_part_set_(false) { |
| +} |
| + |
| +CertDatabaseService::~CertDatabaseService() { |
| + io_task_runner_->PostTask(FROM_HERE, base::Bind(&DestroyIOPart, io_part_)); |
| +} |
| + |
| +void CertDatabaseService::GetNSSCertDatabase( |
| + const GetCertDBCallback& callback) { |
| + CHECK(is_io_part_set_); |
| + GetCertDBCallback callback_on_origin_thread = base::Bind( |
| + &CallBackOnTaskRunner, base::ThreadTaskRunnerHandle::Get(), callback); |
| + io_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&GetNSSCertDBOnIOThread, io_part_, callback_on_origin_thread)); |
| +} |
| + |
| +void CertDatabaseService::SetIOPart( |
| + scoped_ptr<CertDatabaseServiceIOPart> io_part) { |
| + DCHECK(!is_io_part_set_); |
| + is_io_part_set_ = true; |
| + io_part_ = io_part->GetWeakPtr(); |
| + ignore_result(io_part.release()); |
|
mmenke
2014/11/04 20:02:18
Who owns the io_part_ after this? It's not at all
pneubeck (no reviews)
2014/11/05 14:53:38
Added a comment to clarify (there was one in the h
|
| + |
| + // Must immediatly post to IO for initialization, so that subsequent accesses |
| + // hit the IO thread after Init() was called. |
| + io_task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&CertDatabaseServiceIOPart::Init, io_part_)); |
| +} |
| + |
| +base::WeakPtr<CertDatabaseServiceIOPart> CertDatabaseService::GetIOPart() { |
| + CHECK(is_io_part_set_); |
| + return io_part_; |
| +} |
| + |
| +} // namespace cert_database |