Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Unified Diff: blimp/net/blob_channel/blob_channel_receiver.cc

Issue 1891083002: Blimp: Add BlobChannelReceiver and bindings interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@blobchannel-sender
Patch Set: git rm *bindings* Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: blimp/net/blob_channel/blob_channel_receiver.cc
diff --git a/blimp/net/blob_channel/blob_channel_receiver.cc b/blimp/net/blob_channel/blob_channel_receiver.cc
new file mode 100644
index 0000000000000000000000000000000000000000..68bc45d58e0a5fa657c1399380bd968146c8ecad
--- /dev/null
+++ b/blimp/net/blob_channel/blob_channel_receiver.cc
@@ -0,0 +1,89 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "blimp/net/blob_channel/blob_channel_receiver.h"
+
+#include "base/logging.h"
+#include "base/macros.h"
+#include "base/strings/string_number_conversions.h"
+#include "blimp/common/blob_cache/blob_cache.h"
+
+namespace blimp {
+
+BlobChannelReceiver::Delegate::Delegate() {}
+
+BlobChannelReceiver::Delegate::~Delegate() {}
Wez 2016/04/18 22:40:40 Your API contract is that Delegate MUST out-live t
+
+void BlobChannelReceiver::Delegate::SetReceiver(
+ base::WeakPtr<BlobChannelReceiver> receiver) {
+ DCHECK(receiver);
+ DCHECK(!receiver_);
+ receiver_ = receiver;
+}
+
+void BlobChannelReceiver::Delegate::OnBlobReceived(const BlobId& id,
+ BlobData data) {
+ if (receiver_) {
+ receiver_->OnBlobReceived(id, data);
+ }
+}
+
+BlobChannelReceiver::BlobChannelReceiver(BlobCache* cache, Delegate* delegate)
+ : cache_(cache), delegate_(delegate), weak_factory_(this) {
+ DCHECK(cache_);
+ DCHECK(delegate_);
+ delegate_->SetReceiver(weak_factory_.GetWeakPtr());
+}
+
+BlobChannelReceiver::~BlobChannelReceiver() {}
+
+scoped_refptr<RefCountedVector> BlobChannelReceiver::Get(
+ const BlobId& id,
+ const BlobReceivedCallback& callback) {
+ base::AutoLock lock(lock_);
+
+ // Return the value synchronously if the data is already available.
+ if (cache_->Contains(id)) {
+ 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
+ }
+
+ // Only Request() from the delegate if there isn't already a request inflight.
+ if (active_read_callbacks_.find(id) == active_read_callbacks_.end()) {
+ 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
+ }
+
+ // Store the read callback for asynchronous completion.
+ active_read_callbacks_.insert(std::make_pair(id, callback));
+ return nullptr;
+}
+
+void BlobChannelReceiver::OnBlobReceived(const BlobId& id, BlobData data) {
+ std::vector<BlobReceivedCallback> pending_callbacks;
+
+ {
+ base::AutoLock lock(lock_);
+
+ DLOG_IF(WARNING, cache_->Contains(id))
+ << "Redundant blob transfer detected: "
+ << base::HexEncode(id.data(), id.size());
+
+ cache_->Put(id, data);
Wez 2016/04/18 22:40:40 In general adding a blob to the cache will be time
+
+ // Gather the list of pending read callbacks for the blob |id|.
+ auto callbacks_for_id = active_read_callbacks_.equal_range(id);
+ for (auto callback_it = callbacks_for_id.first;
Wez 2016/04/18 22:40:40 nit: callback_it is actually a more confusing name
+ callback_it != callbacks_for_id.second; ++callback_it) {
+ pending_callbacks.push_back(callback_it->second);
+ }
+ active_read_callbacks_.erase(id);
+ }
+
+ // |lock_| is released before the callbacks are run, to prevent potential
+ // reentrant deadlocking issues.
+ for (const BlobReceivedCallback& next_callback : pending_callbacks) {
+ next_callback.Run(id, data);
+ }
+}
+
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698