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::TrustAnchorsCallback& callback) |
| 21 : callback_(callback), weak_ptr_factory_(this) {} |
| 22 |
| 23 void PostTrustCallback(TrustAnchors anchors) { |
| 24 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 25 FROM_HERE, |
| 26 base::Bind(&TrustStoreInMemoryAsyncRequest::DoTrustCallback, |
| 27 weak_ptr_factory_.GetWeakPtr(), std::move(anchors))); |
| 28 } |
| 29 |
| 30 private: |
| 31 void DoTrustCallback(TrustAnchors anchors) { |
| 32 base::ResetAndReturn(&callback_).Run(std::move(anchors)); |
| 33 // |this| may be deleted here. |
| 34 } |
| 35 |
| 36 TrustStore::TrustAnchorsCallback callback_; |
| 37 base::WeakPtrFactory<TrustStoreInMemoryAsyncRequest> weak_ptr_factory_; |
| 38 }; |
| 39 |
| 40 } // namespace |
| 41 |
| 42 void TrustStoreRequestDeleter(std::unique_ptr<TrustStore::Request>* req_owner, |
| 43 const base::Closure& done_callback, |
| 44 std::unique_ptr<TrustAnchors> anchors) { |
| 45 req_owner->reset(); |
| 46 done_callback.Run(); |
| 47 } |
| 48 |
| 49 TrustAnchorResultRecorder::TrustAnchorResultRecorder() = default; |
| 50 TrustAnchorResultRecorder::~TrustAnchorResultRecorder() = default; |
| 51 |
| 52 TrustStore::TrustAnchorsCallback TrustAnchorResultRecorder::Callback() { |
| 53 return base::Bind(&TrustAnchorResultRecorder::OnGotAnchors, |
| 54 base::Unretained(this)); |
| 55 } |
| 56 |
| 57 void TrustAnchorResultRecorder::OnGotAnchors(TrustAnchors anchors) { |
| 58 anchors_ = std::move(anchors); |
| 59 run_loop_.Quit(); |
| 60 } |
| 61 |
| 62 TrustStoreInMemoryAsync::TrustStoreInMemoryAsync() = default; |
| 63 TrustStoreInMemoryAsync::~TrustStoreInMemoryAsync() = default; |
| 64 |
| 65 void TrustStoreInMemoryAsync::AddSyncTrustAnchor( |
| 66 scoped_refptr<TrustAnchor> anchor) { |
| 67 sync_store_.AddTrustAnchor(std::move(anchor)); |
| 68 } |
| 69 |
| 70 void TrustStoreInMemoryAsync::AddAsyncTrustAnchor( |
| 71 scoped_refptr<TrustAnchor> anchor) { |
| 72 async_store_.AddTrustAnchor(std::move(anchor)); |
| 73 } |
| 74 |
| 75 void TrustStoreInMemoryAsync::FindTrustAnchorsForCert( |
| 76 const ParsedCertificate* cert, |
| 77 const TrustAnchorsCallback& callback, |
| 78 TrustAnchors* synchronous_matches, |
| 79 std::unique_ptr<Request>* out_req) const { |
| 80 sync_store_.FindTrustAnchorsForCert(cert, TrustAnchorsCallback(), |
| 81 synchronous_matches, nullptr); |
| 82 if (!callback.is_null()) { |
| 83 TrustAnchors async_matches; |
| 84 async_store_.FindTrustAnchorsForCert(cert, TrustAnchorsCallback(), |
| 85 &async_matches, nullptr); |
| 86 |
| 87 std::unique_ptr<TrustStoreInMemoryAsyncRequest> req( |
| 88 base::MakeUnique<TrustStoreInMemoryAsyncRequest>(callback)); |
| 89 req->PostTrustCallback(std::move(async_matches)); |
| 90 |
| 91 *out_req = std::move(req); |
| 92 } |
| 93 } |
| 94 |
| 95 } // namespace net |
OLD | NEW |