Chromium Code Reviews| 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 "blimp/net/blob_channel/blob_channel_receiver.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/macros.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "blimp/common/blob_cache/blob_cache.h" | |
| 11 | |
| 12 namespace blimp { | |
| 13 | |
| 14 BlobChannelReceiver::Delegate::Delegate() {} | |
| 15 | |
| 16 BlobChannelReceiver::Delegate::~Delegate() {} | |
|
Wez
2016/04/18 22:40:40
Your API contract is that Delegate MUST out-live t
| |
| 17 | |
| 18 void BlobChannelReceiver::Delegate::SetReceiver( | |
| 19 base::WeakPtr<BlobChannelReceiver> receiver) { | |
| 20 DCHECK(receiver); | |
| 21 DCHECK(!receiver_); | |
| 22 receiver_ = receiver; | |
| 23 } | |
| 24 | |
| 25 void BlobChannelReceiver::Delegate::OnBlobReceived(const BlobId& id, | |
| 26 BlobData data) { | |
| 27 if (receiver_) { | |
| 28 receiver_->OnBlobReceived(id, data); | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 BlobChannelReceiver::BlobChannelReceiver(BlobCache* cache, Delegate* delegate) | |
| 33 : cache_(cache), delegate_(delegate), weak_factory_(this) { | |
| 34 DCHECK(cache_); | |
| 35 DCHECK(delegate_); | |
| 36 delegate_->SetReceiver(weak_factory_.GetWeakPtr()); | |
| 37 } | |
| 38 | |
| 39 BlobChannelReceiver::~BlobChannelReceiver() {} | |
| 40 | |
| 41 scoped_refptr<RefCountedVector> BlobChannelReceiver::Get( | |
| 42 const BlobId& id, | |
| 43 const BlobReceivedCallback& callback) { | |
| 44 base::AutoLock lock(lock_); | |
| 45 | |
| 46 // Return the value synchronously if the data is already available. | |
| 47 if (cache_->Contains(id)) { | |
| 48 return cache_->Get(id); | |
|
Wez
2016/04/18 22:40:40
See below; like Put(), cache_->Get() seems likely
Wez
2016/04/18 22:44:47
Having this in here also means that the cache impl
| |
| 49 } | |
| 50 | |
| 51 // Only Request() from the delegate if there isn't already a request inflight. | |
| 52 if (active_read_callbacks_.find(id) == active_read_callbacks_.end()) { | |
| 53 delegate_->Request(id); | |
|
Wez
2016/04/18 22:40:40
Minimize the amount of code inside the Lock - set
Wez
2016/04/18 22:44:47
Note that calling delegate_->Request() here means
| |
| 54 } | |
| 55 | |
| 56 // Store the read callback for asynchronous completion. | |
| 57 active_read_callbacks_.insert(std::make_pair(id, callback)); | |
| 58 return nullptr; | |
| 59 } | |
| 60 | |
| 61 void BlobChannelReceiver::OnBlobReceived(const BlobId& id, BlobData data) { | |
| 62 std::vector<BlobReceivedCallback> pending_callbacks; | |
| 63 | |
| 64 { | |
| 65 base::AutoLock lock(lock_); | |
| 66 | |
| 67 DLOG_IF(WARNING, cache_->Contains(id)) | |
| 68 << "Redundant blob transfer detected: " | |
| 69 << base::HexEncode(id.data(), id.size()); | |
| 70 | |
| 71 cache_->Put(id, data); | |
|
Wez
2016/04/18 22:40:40
In general adding a blob to the cache will be time
| |
| 72 | |
| 73 // Gather the list of pending read callbacks for the blob |id|. | |
| 74 auto callbacks_for_id = active_read_callbacks_.equal_range(id); | |
| 75 for (auto callback_it = callbacks_for_id.first; | |
|
Wez
2016/04/18 22:40:40
nit: callback_it is actually a more confusing name
| |
| 76 callback_it != callbacks_for_id.second; ++callback_it) { | |
| 77 pending_callbacks.push_back(callback_it->second); | |
| 78 } | |
| 79 active_read_callbacks_.erase(id); | |
| 80 } | |
| 81 | |
| 82 // |lock_| is released before the callbacks are run, to prevent potential | |
| 83 // reentrant deadlocking issues. | |
| 84 for (const BlobReceivedCallback& next_callback : pending_callbacks) { | |
| 85 next_callback.Run(id, data); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 } // namespace blimp | |
| OLD | NEW |