Index: blimp/engine/renderer/blob_channel_sender_proxy.cc |
diff --git a/blimp/engine/renderer/blob_channel_sender_proxy.cc b/blimp/engine/renderer/blob_channel_sender_proxy.cc |
index 8b689d36c73dd840878105bb72a926845ebe97be..79c8c2f5465c1a02dc8553bff062625fac6f8e92 100644 |
--- a/blimp/engine/renderer/blob_channel_sender_proxy.cc |
+++ b/blimp/engine/renderer/blob_channel_sender_proxy.cc |
@@ -4,6 +4,7 @@ |
#include "blimp/engine/renderer/blob_channel_sender_proxy.h" |
+#include "blimp/common/blob_cache/id_util.h" |
#include "content/public/common/service_registry.h" |
#include "content/public/renderer/render_thread.h" |
@@ -19,30 +20,96 @@ mojom::BlobChannelPtr GetConnectedBlobChannel() { |
return blob_channel_ptr; |
} |
+// Manages the creation and lifetime of Mojo shared memory buffers for blobs. |
+// Cleans up the shared memory state when deleted. |
+// The SharedMemoryBlob must not be deleted until the data consumer has |
+// acknowledged that it is finished using the buffer. |
+class SharedMemoryBlob { |
+ public: |
+ explicit SharedMemoryBlob(BlobDataPtr data); |
+ ~SharedMemoryBlob(); |
+ |
+ mojo::ScopedSharedBufferHandle take_remote_handle(); |
+ |
+ private: |
+ mojo::ScopedSharedBufferHandle local_handle_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SharedMemoryBlob); |
+}; |
+ |
+SharedMemoryBlob::SharedMemoryBlob(BlobDataPtr data) { |
+ local_handle_ = mojo::SharedBufferHandle::Create(data->data.size()); |
dcheng
2016/06/25 01:06:31
Any idea how likely it is to run into shared memor
Kevin M
2016/06/27 17:31:12
Added size check. Done.
|
+ CHECK(local_handle_.is_valid()) << "Mojo error when creating shared buffer."; |
dcheng
2016/06/25 01:06:31
IMO, DCHECK() if this is something that should "ne
Kevin M
2016/06/27 17:31:12
Done.
|
+ |
+ auto mapped = local_handle_->Map(data->data.size()); |
+ CHECK(mapped) << "Mojo error when memory mapping shared buffer."; |
+ memcpy(mapped.get(), data->data.data(), data->data.size()); |
+} |
+ |
+SharedMemoryBlob::~SharedMemoryBlob() {} |
+ |
+mojo::ScopedSharedBufferHandle SharedMemoryBlob::take_remote_handle() { |
dcheng
2016/06/25 01:06:31
Take implies that this is destructive, but I don't
Kevin M
2016/06/27 17:31:12
Done.
|
+ auto remote_handle = |
+ local_handle_->Clone(mojo::SharedBufferHandle::AccessMode::READ_ONLY); |
+ CHECK(remote_handle.is_valid()) |
+ << "Mojo error when creating read-only buffer handle."; |
+ return remote_handle; |
+} |
+ |
} // namespace |
BlobChannelSenderProxy::BlobChannelSenderProxy() |
- : blob_channel_(GetConnectedBlobChannel()) {} |
+ : BlobChannelSenderProxy(GetConnectedBlobChannel()) {} |
BlobChannelSenderProxy::~BlobChannelSenderProxy() {} |
+BlobChannelSenderProxy::BlobChannelSenderProxy( |
+ mojom::BlobChannelPtr blob_channel) |
+ : blob_channel_(std::move(blob_channel)) {} |
+ |
+// static |
+std::unique_ptr<BlobChannelSenderProxy> BlobChannelSenderProxy::CreateForTest( |
+ mojom::BlobChannelPtr blob_channel) { |
+ return base::WrapUnique(new BlobChannelSenderProxy(std::move(blob_channel))); |
dcheng
2016/06/25 01:06:31
Nit: base::MakeUnique
Kevin M
2016/06/27 17:31:12
This is a cool method, but N/A in this case. The c
|
+} |
+ |
bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { |
return replication_state_.find(id) != replication_state_.end(); |
} |
bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { |
- return replication_state_.find(id)->second; |
+ auto found = replication_state_.find(id); |
+ return found != replication_state_.end() && found->second; |
} |
void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { |
- DCHECK(!IsInEngineCache(id)); |
dcheng
2016/06/25 01:06:31
How come these (and other checks) changed from DCH
Kevin M
2016/06/27 17:31:12
So that these conditions are correctly handled by
dcheng
2016/06/28 06:26:18
Right, but violating these conditions means that t
Kevin M
2016/06/28 18:18:53
Good point... done.
|
+ if (IsInEngineCache(id)) { |
+ DLOG(FATAL) << "Redundant blob put attempted: " << BlobIdToString(id); |
+ return; |
+ } |
+ |
+ size_t size = data->data.size(); |
+ if (size == 0) { |
+ DLOG(FATAL) << "Zero length blob sent: " << BlobIdToString(id); |
+ return; |
+ } |
replication_state_[id] = false; |
- blob_channel_->PutBlob(id, data->data); |
+ std::unique_ptr<SharedMemoryBlob> shared_mem_blob( |
+ new SharedMemoryBlob(std::move(data))); |
+ blob_channel_->PutBlob(id, shared_mem_blob->take_remote_handle(), size); |
} |
void BlobChannelSenderProxy::DeliverBlob(const std::string& id) { |
- DCHECK(!IsInClientCache(id)); |
+ if (!IsInEngineCache(id)) { |
+ DLOG(FATAL) << "Attempted to deliver an invalid blob: " |
+ << BlobIdToString(id); |
+ return; |
+ } |
+ if (IsInClientCache(id)) { |
+ DVLOG(1) << "Blob is already in the remote cache:" << BlobIdToString(id); |
+ return; |
+ } |
// We assume that the client will have the blob if we push it. |
// TODO(kmarshall): Revisit this assumption when asynchronous blob transport |