| Index: net/cert/internal/trust_store_test_helpers.cc
|
| diff --git a/net/cert/internal/trust_store_test_helpers.cc b/net/cert/internal/trust_store_test_helpers.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..ab3244c41d0898cbb07347b4d55d208e9e754d1b
|
| --- /dev/null
|
| +++ b/net/cert/internal/trust_store_test_helpers.cc
|
| @@ -0,0 +1,102 @@
|
| +// Copyright 2016 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 "net/cert/internal/trust_store_test_helpers.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/callback_helpers.h"
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/memory/weak_ptr.h"
|
| +#include "base/threading/thread_task_runner_handle.h"
|
| +
|
| +namespace net {
|
| +
|
| +namespace {
|
| +
|
| +class TrustStoreInMemoryAsyncRequest : public TrustStore::Request {
|
| + public:
|
| + explicit TrustStoreInMemoryAsyncRequest(
|
| + const TrustStore::TrustAnchorCallback& callback)
|
| + : callback_(callback), weak_ptr_factory_(this) {}
|
| +
|
| + void PostTrustCallback(std::unique_ptr<TrustAnchors> anchors) {
|
| + DCHECK(anchors);
|
| + base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&TrustStoreInMemoryAsyncRequest::DoTrustCallback,
|
| + weak_ptr_factory_.GetWeakPtr(), base::Passed(&anchors)));
|
| + }
|
| +
|
| + private:
|
| + void DoTrustCallback(std::unique_ptr<TrustAnchors> anchors) {
|
| + DCHECK(anchors);
|
| + base::ResetAndReturn(&callback_).Run(std::move(anchors));
|
| + // |this| may be deleted here.
|
| + }
|
| +
|
| + TrustStore::TrustAnchorCallback callback_;
|
| + base::WeakPtrFactory<TrustStoreInMemoryAsyncRequest> weak_ptr_factory_;
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +void TrustStoreRequestDeleter(std::unique_ptr<TrustStore::Request>* req_owner,
|
| + base::Closure done_callback,
|
| + std::unique_ptr<TrustAnchors> anchors) {
|
| + req_owner->reset();
|
| + done_callback.Run();
|
| +}
|
| +
|
| +TrustAnchorResultRecorder::TrustAnchorResultRecorder() = default;
|
| +TrustAnchorResultRecorder::~TrustAnchorResultRecorder() = default;
|
| +
|
| +TrustStore::TrustAnchorCallback TrustAnchorResultRecorder::Callback() {
|
| + return base::Bind(&TrustAnchorResultRecorder::OnGotAnchors,
|
| + base::Unretained(this));
|
| +}
|
| +
|
| +void TrustAnchorResultRecorder::OnGotAnchors(
|
| + std::unique_ptr<TrustAnchors> anchors) {
|
| + anchors_ = std::move(anchors);
|
| + run_loop_.Quit();
|
| +}
|
| +
|
| +TrustStoreInMemoryAsync::TrustStoreInMemoryAsync() = default;
|
| +TrustStoreInMemoryAsync::~TrustStoreInMemoryAsync() = default;
|
| +
|
| +void TrustStoreInMemoryAsync::AddSyncTrustAnchor(
|
| + scoped_refptr<TrustAnchor> anchor) {
|
| + sync_store_.AddTrustAnchor(std::move(anchor));
|
| +}
|
| +
|
| +void TrustStoreInMemoryAsync::AddAsyncTrustAnchor(
|
| + scoped_refptr<TrustAnchor> anchor) {
|
| + async_store_.AddTrustAnchor(std::move(anchor));
|
| +}
|
| +
|
| +void TrustStoreInMemoryAsync::FindTrustAnchorsForCert(
|
| + const ParsedCertificate* cert,
|
| + const TrustAnchorCallback& callback,
|
| + TrustAnchors* out_matches,
|
| + std::unique_ptr<Request>* out_req) const {
|
| + sync_store_.FindTrustAnchorsForCert(cert, TrustAnchorCallback(), out_matches,
|
| + out_req);
|
| + DCHECK(!*out_req);
|
| +
|
| + if (!callback.is_null()) {
|
| + std::unique_ptr<TrustAnchors> async_matches(
|
| + base::MakeUnique<TrustAnchors>());
|
| + async_store_.FindTrustAnchorsForCert(cert, TrustAnchorCallback(),
|
| + async_matches.get(), out_req);
|
| + DCHECK(!*out_req);
|
| +
|
| + std::unique_ptr<TrustStoreInMemoryAsyncRequest> req(
|
| + base::MakeUnique<TrustStoreInMemoryAsyncRequest>(callback));
|
| + req->PostTrustCallback(std::move(async_matches));
|
| +
|
| + *out_req = std::move(req);
|
| + }
|
| +}
|
| +
|
| +} // namespace net
|
|
|