OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include "net/cert/internal/trust_store_test_helpers.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 |
| 13 namespace net { |
| 14 |
| 15 namespace { |
| 16 |
| 17 class TrustStoreInMemoryAsyncRequest : public TrustStore::Request { |
| 18 public: |
| 19 explicit TrustStoreInMemoryAsyncRequest( |
| 20 const TrustStore::TrustAnchorCallback& callback) |
| 21 : callback_(callback), weak_ptr_factory_(this) {} |
| 22 |
| 23 void PostTrustCallback(std::unique_ptr<TrustAnchors> anchors) { |
| 24 DCHECK(anchors); |
| 25 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 26 FROM_HERE, |
| 27 base::Bind(&TrustStoreInMemoryAsyncRequest::DoTrustCallback, |
| 28 weak_ptr_factory_.GetWeakPtr(), base::Passed(&anchors))); |
| 29 } |
| 30 |
| 31 private: |
| 32 void DoTrustCallback(std::unique_ptr<TrustAnchors> anchors) { |
| 33 DCHECK(anchors); |
| 34 base::ResetAndReturn(&callback_).Run(std::move(anchors)); |
| 35 // |this| may be deleted here. |
| 36 } |
| 37 |
| 38 TrustStore::TrustAnchorCallback callback_; |
| 39 base::WeakPtrFactory<TrustStoreInMemoryAsyncRequest> weak_ptr_factory_; |
| 40 }; |
| 41 |
| 42 } // namespace |
| 43 |
| 44 void TrustStoreRequestDeleter(std::unique_ptr<TrustStore::Request>* req_owner, |
| 45 base::Closure done_callback, |
| 46 std::unique_ptr<TrustAnchors> anchors) { |
| 47 req_owner->reset(); |
| 48 done_callback.Run(); |
| 49 } |
| 50 |
| 51 TrustAnchorResultRecorder::TrustAnchorResultRecorder() = default; |
| 52 TrustAnchorResultRecorder::~TrustAnchorResultRecorder() = default; |
| 53 |
| 54 TrustStore::TrustAnchorCallback TrustAnchorResultRecorder::Callback() { |
| 55 return base::Bind(&TrustAnchorResultRecorder::OnGotAnchors, |
| 56 base::Unretained(this)); |
| 57 } |
| 58 |
| 59 void TrustAnchorResultRecorder::OnGotAnchors( |
| 60 std::unique_ptr<TrustAnchors> anchors) { |
| 61 anchors_ = std::move(anchors); |
| 62 run_loop_.Quit(); |
| 63 } |
| 64 |
| 65 TrustStoreInMemoryAsync::TrustStoreInMemoryAsync() = default; |
| 66 TrustStoreInMemoryAsync::~TrustStoreInMemoryAsync() = default; |
| 67 |
| 68 void TrustStoreInMemoryAsync::AddSyncTrustAnchor( |
| 69 scoped_refptr<TrustAnchor> anchor) { |
| 70 sync_store_.AddTrustAnchor(std::move(anchor)); |
| 71 } |
| 72 |
| 73 void TrustStoreInMemoryAsync::AddAsyncTrustAnchor( |
| 74 scoped_refptr<TrustAnchor> anchor) { |
| 75 async_store_.AddTrustAnchor(std::move(anchor)); |
| 76 } |
| 77 |
| 78 void TrustStoreInMemoryAsync::FindTrustAnchorsForCert( |
| 79 const ParsedCertificate* cert, |
| 80 const TrustAnchorCallback& callback, |
| 81 TrustAnchors* out_matches, |
| 82 std::unique_ptr<Request>* out_req) const { |
| 83 sync_store_.FindTrustAnchorsForCert(cert, TrustAnchorCallback(), out_matches, |
| 84 out_req); |
| 85 DCHECK(!*out_req); |
| 86 |
| 87 if (!callback.is_null()) { |
| 88 std::unique_ptr<TrustAnchors> async_matches( |
| 89 base::MakeUnique<TrustAnchors>()); |
| 90 async_store_.FindTrustAnchorsForCert(cert, TrustAnchorCallback(), |
| 91 async_matches.get(), out_req); |
| 92 DCHECK(!*out_req); |
| 93 |
| 94 std::unique_ptr<TrustStoreInMemoryAsyncRequest> req( |
| 95 base::MakeUnique<TrustStoreInMemoryAsyncRequest>(callback)); |
| 96 req->PostTrustCallback(std::move(async_matches)); |
| 97 |
| 98 *out_req = std::move(req); |
| 99 } |
| 100 } |
| 101 |
| 102 } // namespace net |
OLD | NEW |