OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "blimp/net/blob_channel/blob_channel_receiver.h" | 5 #include "blimp/net/blob_channel/blob_channel_receiver.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" |
9 #include "blimp/common/blob_cache/blob_cache.h" | 10 #include "blimp/common/blob_cache/blob_cache.h" |
10 | 11 |
11 namespace blimp { | 12 namespace blimp { |
| 13 namespace { |
| 14 |
| 15 // Takes incoming blobs from |delegate_| stores them in |cache_|, and provides |
| 16 // callers a getter interface for accessing blobs from |cache_|. |
| 17 class BLIMP_NET_EXPORT BlobChannelReceiverImpl : public BlobChannelReceiver { |
| 18 public: |
| 19 BlobChannelReceiverImpl(std::unique_ptr<BlobCache> cache, |
| 20 std::unique_ptr<Delegate> delegate); |
| 21 ~BlobChannelReceiverImpl() override; |
| 22 |
| 23 // BlobChannelReceiver implementation. |
| 24 BlobDataPtr Get(const BlobId& id) override; |
| 25 void OnBlobReceived(const BlobId& id, BlobDataPtr data) override; |
| 26 |
| 27 private: |
| 28 std::unique_ptr<BlobCache> cache_; |
| 29 std::unique_ptr<Delegate> delegate_; |
| 30 |
| 31 // Guards against concurrent access to |cache_|. |
| 32 base::Lock cache_lock_; |
| 33 |
| 34 DISALLOW_COPY_AND_ASSIGN(BlobChannelReceiverImpl); |
| 35 }; |
| 36 |
| 37 } // namespace |
12 | 38 |
13 BlobChannelReceiver::Delegate::Delegate() {} | 39 BlobChannelReceiver::Delegate::Delegate() {} |
14 | 40 |
15 BlobChannelReceiver::Delegate::~Delegate() { | 41 BlobChannelReceiver::Delegate::~Delegate() { |
16 DCHECK(!receiver_); | 42 DCHECK(receiver_); |
17 } | 43 } |
18 | 44 |
19 void BlobChannelReceiver::Delegate::SetReceiver(BlobChannelReceiver* receiver) { | 45 void BlobChannelReceiver::Delegate::SetReceiver(BlobChannelReceiver* receiver) { |
| 46 DCHECK(receiver); |
| 47 DCHECK(!receiver_); |
| 48 |
20 receiver_ = receiver; | 49 receiver_ = receiver; |
21 } | 50 } |
22 | 51 |
23 void BlobChannelReceiver::Delegate::OnBlobReceived(const BlobId& id, | 52 void BlobChannelReceiver::Delegate::OnBlobReceived(const BlobId& id, |
24 BlobDataPtr data) { | 53 BlobDataPtr data) { |
25 if (receiver_) { | 54 receiver_->OnBlobReceived(id, data); |
26 receiver_->OnBlobReceived(id, data); | |
27 } | |
28 } | 55 } |
29 | 56 |
30 BlobChannelReceiver::BlobChannelReceiver(std::unique_ptr<BlobCache> cache, | 57 // static |
31 std::unique_ptr<Delegate> delegate) | 58 std::unique_ptr<BlobChannelReceiver> BlobChannelReceiver::Create( |
| 59 std::unique_ptr<BlobCache> cache, |
| 60 std::unique_ptr<Delegate> delegate) { |
| 61 return base::WrapUnique( |
| 62 new BlobChannelReceiverImpl(std::move(cache), std::move(delegate))); |
| 63 } |
| 64 |
| 65 BlobChannelReceiverImpl::BlobChannelReceiverImpl( |
| 66 std::unique_ptr<BlobCache> cache, |
| 67 std::unique_ptr<Delegate> delegate) |
32 : cache_(std::move(cache)), delegate_(std::move(delegate)) { | 68 : cache_(std::move(cache)), delegate_(std::move(delegate)) { |
33 DCHECK(cache_); | 69 DCHECK(cache_); |
| 70 |
34 delegate_->SetReceiver(this); | 71 delegate_->SetReceiver(this); |
35 } | 72 } |
36 | 73 |
37 BlobChannelReceiver::~BlobChannelReceiver() { | 74 BlobChannelReceiverImpl::~BlobChannelReceiverImpl() {} |
38 delegate_->SetReceiver(nullptr); | |
39 } | |
40 | 75 |
41 BlobDataPtr BlobChannelReceiver::Get(const BlobId& id) { | 76 BlobDataPtr BlobChannelReceiverImpl::Get(const BlobId& id) { |
42 DVLOG(2) << "Get blob: " << id; | 77 DVLOG(2) << "Get blob: " << id; |
43 | 78 |
44 base::AutoLock lock(cache_lock_); | 79 base::AutoLock lock(cache_lock_); |
45 return cache_->Get(id); | 80 return cache_->Get(id); |
46 } | 81 } |
47 | 82 |
48 void BlobChannelReceiver::OnBlobReceived(const BlobId& id, BlobDataPtr data) { | 83 void BlobChannelReceiverImpl::OnBlobReceived(const BlobId& id, |
| 84 BlobDataPtr data) { |
49 DVLOG(2) << "Blob received: " << id; | 85 DVLOG(2) << "Blob received: " << id; |
50 | 86 |
51 base::AutoLock lock(cache_lock_); | 87 base::AutoLock lock(cache_lock_); |
52 cache_->Put(id, data); | 88 cache_->Put(id, data); |
53 } | 89 } |
54 | 90 |
55 } // namespace blimp | 91 } // namespace blimp |
OLD | NEW |