| 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::BlobChannelReceiver(BlobCache* cache, |
| 15 BlobReceiverBindings* bindings) |
| 16 : cache_(cache), bindings_(bindings) { |
| 17 DCHECK(cache_); |
| 18 DCHECK(bindings_); |
| 19 bindings_->SetDelegate(this); |
| 20 } |
| 21 |
| 22 BlobChannelReceiver::~BlobChannelReceiver() {} |
| 23 |
| 24 scoped_refptr<RefCountedVector> BlobChannelReceiver::Get( |
| 25 const std::string& id, |
| 26 const BlobReceivedCallback& callback) { |
| 27 base::AutoLock lock(lock_); |
| 28 |
| 29 // Return the value synchronously if the data is already available. |
| 30 if (cache_->Contains(id)) { |
| 31 return cache_->Get(id); |
| 32 } |
| 33 |
| 34 if (active_read_callbacks_.find(id) == active_read_callbacks_.end()) { |
| 35 // Only Get() from the bindings if there isn't already a request inflight. |
| 36 bindings_->Get(id); |
| 37 } |
| 38 |
| 39 // Store the read callback for asynchronous completion. |
| 40 active_read_callbacks_.insert(std::make_pair(id, callback)); |
| 41 return nullptr; |
| 42 } |
| 43 |
| 44 void BlobChannelReceiver::OnBlobReceived( |
| 45 const std::string& id, |
| 46 std::unique_ptr<std::vector<uint8_t>> data) { |
| 47 scoped_refptr<RefCountedVector> cached_item(new RefCountedVector); |
| 48 std::vector<BlobReceivedCallback> pending_callbacks; |
| 49 |
| 50 { |
| 51 base::AutoLock lock(lock_); |
| 52 |
| 53 DLOG_IF(WARNING, cache_->Contains(id)) |
| 54 << "Redundant blob transfer detected: " |
| 55 << base::HexEncode(id.data(), id.size()); |
| 56 |
| 57 cached_item->data.swap(*data); |
| 58 cache_->Put(id, cached_item); |
| 59 |
| 60 // Gather the list of pending read callbacks for the blob |id|. |
| 61 auto callbacks_for_id = active_read_callbacks_.equal_range(id); |
| 62 for (auto callback_it = callbacks_for_id.first; |
| 63 callback_it != callbacks_for_id.second; ++callback_it) { |
| 64 pending_callbacks.push_back(callback_it->second); |
| 65 } |
| 66 active_read_callbacks_.erase(id); |
| 67 } |
| 68 |
| 69 // |lock_| is released before the callbacks are run, to prevent potential |
| 70 // reentrant deadlocking issues. |
| 71 for (const BlobReceivedCallback& next_callback : pending_callbacks) { |
| 72 next_callback.Run(id, cached_item); |
| 73 } |
| 74 } |
| 75 |
| 76 } // namespace blimp |
| OLD | NEW |