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 { |
12 | 14 |
13 BlobChannelReceiver::Delegate::Delegate() {} | 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; |
14 | 22 |
15 BlobChannelReceiver::Delegate::~Delegate() { | 23 // BlobChannelReceiver implementation. |
16 DCHECK(!receiver_); | 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 |
| 38 |
| 39 // static |
| 40 std::unique_ptr<BlobChannelReceiver> BlobChannelReceiver::Create( |
| 41 std::unique_ptr<BlobCache> cache, |
| 42 std::unique_ptr<Delegate> delegate) { |
| 43 return base::WrapUnique( |
| 44 new BlobChannelReceiverImpl(std::move(cache), std::move(delegate))); |
17 } | 45 } |
18 | 46 |
19 void BlobChannelReceiver::Delegate::SetReceiver(BlobChannelReceiver* receiver) { | 47 BlobChannelReceiverImpl::BlobChannelReceiverImpl( |
20 receiver_ = receiver; | 48 std::unique_ptr<BlobCache> cache, |
21 } | 49 std::unique_ptr<Delegate> delegate) |
22 | |
23 void BlobChannelReceiver::Delegate::OnBlobReceived(const BlobId& id, | |
24 BlobDataPtr data) { | |
25 if (receiver_) { | |
26 receiver_->OnBlobReceived(id, data); | |
27 } | |
28 } | |
29 | |
30 BlobChannelReceiver::BlobChannelReceiver(std::unique_ptr<BlobCache> cache, | |
31 std::unique_ptr<Delegate> delegate) | |
32 : cache_(std::move(cache)), delegate_(std::move(delegate)) { | 50 : cache_(std::move(cache)), delegate_(std::move(delegate)) { |
33 DCHECK(cache_); | 51 DCHECK(cache_); |
| 52 |
34 delegate_->SetReceiver(this); | 53 delegate_->SetReceiver(this); |
35 } | 54 } |
36 | 55 |
37 BlobChannelReceiver::~BlobChannelReceiver() { | 56 BlobChannelReceiverImpl::~BlobChannelReceiverImpl() {} |
38 delegate_->SetReceiver(nullptr); | |
39 } | |
40 | 57 |
41 BlobDataPtr BlobChannelReceiver::Get(const BlobId& id) { | 58 BlobDataPtr BlobChannelReceiverImpl::Get(const BlobId& id) { |
42 DVLOG(2) << "Get blob: " << id; | 59 DVLOG(2) << "Get blob: " << id; |
43 | 60 |
44 base::AutoLock lock(cache_lock_); | 61 base::AutoLock lock(cache_lock_); |
45 return cache_->Get(id); | 62 return cache_->Get(id); |
46 } | 63 } |
47 | 64 |
48 void BlobChannelReceiver::OnBlobReceived(const BlobId& id, BlobDataPtr data) { | 65 void BlobChannelReceiverImpl::OnBlobReceived(const BlobId& id, |
| 66 BlobDataPtr data) { |
49 DVLOG(2) << "Blob received: " << id; | 67 DVLOG(2) << "Blob received: " << id; |
50 | 68 |
51 base::AutoLock lock(cache_lock_); | 69 base::AutoLock lock(cache_lock_); |
52 cache_->Put(id, data); | 70 cache_->Put(id, data); |
53 } | 71 } |
54 | 72 |
55 } // namespace blimp | 73 } // namespace blimp |
OLD | NEW |