Chromium Code Reviews| Index: content/child/webblobregistry_impl.cc |
| diff --git a/content/child/webblobregistry_impl.cc b/content/child/webblobregistry_impl.cc |
| index d673c2a657fddc9eb044eda48b4762719e8fb607..b613baff5b4dda03a6c1a530b211cef47d48250e 100644 |
| --- a/content/child/webblobregistry_impl.cc |
| +++ b/content/child/webblobregistry_impl.cc |
| @@ -43,15 +43,27 @@ void WebBlobRegistryImpl::registerBlobData( |
| const blink::WebString& uuid, const blink::WebBlobData& data) { |
| const std::string uuid_str(uuid.utf8()); |
| + std::vector<char> consolidating_buffer; |
| + consolidating_buffer.reserve(kLargeThresholdBytes); |
|
michaeln
2015/01/22 00:46:42
should we care about preallocating this much mem?
dmurph
2015/01/22 19:54:17
I can just use the internal vector in DataElement.
|
| + |
| sender_->Send(new BlobHostMsg_StartBuilding(uuid_str)); |
| size_t i = 0; |
| WebBlobData::Item data_item; |
| while (data.itemAt(i++, data_item)) { |
| + if (data_item.length == 0) { |
| + continue; |
| + } |
| + if (data_item.type != WebBlobData::Item::TypeData && |
| + consolidating_buffer.size() != 0) { |
| + FlushBlobItemBuffer(uuid_str, &consolidating_buffer); |
| + } |
| switch (data_item.type) { |
| case WebBlobData::Item::TypeData: { |
| // WebBlobData does not allow partial data items. |
| DCHECK(!data_item.offset && data_item.length == -1); |
| - SendDataForBlob(uuid_str, data_item.data); |
| + DCHECK_EQ(data_item.length, |
| + static_cast<long long>(data_item.data.size())); |
| + BufferBlobData(uuid_str, data_item.data, &consolidating_buffer); |
| break; |
| } |
| case WebBlobData::Item::TypeFile: |
| @@ -95,6 +107,9 @@ void WebBlobRegistryImpl::registerBlobData( |
| NOTREACHED(); |
| } |
| } |
| + if (consolidating_buffer.size() != 0) { |
| + FlushBlobItemBuffer(uuid_str, &consolidating_buffer); |
| + } |
| sender_->Send(new BlobHostMsg_FinishBuilding( |
| uuid_str, data.contentType().utf8().data())); |
| } |
| @@ -116,37 +131,59 @@ void WebBlobRegistryImpl::revokePublicBlobURL(const WebURL& url) { |
| sender_->Send(new BlobHostMsg_RevokePublicURL(url)); |
| } |
| -void WebBlobRegistryImpl::SendDataForBlob(const std::string& uuid_str, |
| - const WebThreadSafeData& data) { |
| +void WebBlobRegistryImpl::FlushBlobItemBuffer( |
| + const std::string& uuid_str, |
| + std::vector<char>* consolidating_buffer) const { |
| + DCHECK_NE(consolidating_buffer->size(), 0ul); |
| + DCHECK_LT(consolidating_buffer->size(), kLargeThresholdBytes); |
| + storage::BlobData::Item item; |
| + item.SetToBytes(&(*consolidating_buffer)[0], consolidating_buffer->size()); |
| + sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); |
| + consolidating_buffer->clear(); |
| +} |
| - if (data.size() == 0) |
| - return; |
| - if (data.size() < kLargeThresholdBytes) { |
| - storage::BlobData::Item item; |
| - item.SetToBytes(data.data(), data.size()); |
| - sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); |
| +void WebBlobRegistryImpl::BufferBlobData( |
| + const std::string& uuid_str, |
| + const blink::WebThreadSafeData& data, |
| + std::vector<char>* consolidating_buffer) { |
| + size_t buffer_size = consolidating_buffer->size(); |
| + size_t data_size = data.size(); |
| + DCHECK_NE(data_size, 0ul); |
| + if (buffer_size != 0 && buffer_size + data_size >= kLargeThresholdBytes) { |
| + FlushBlobItemBuffer(uuid_str, consolidating_buffer); |
| + buffer_size = 0; |
| + } |
| + if (data_size >= kLargeThresholdBytes) { |
| + SendOversizedDataForBlob(uuid_str, data); |
| } else { |
| - // We handle larger amounts of data via SharedMemory instead of |
| - // writing it directly to the IPC channel. |
| - size_t shared_memory_size = std::min( |
| - data.size(), kMaxSharedMemoryBytes); |
| - scoped_ptr<base::SharedMemory> shared_memory( |
| - ChildThread::AllocateSharedMemory(shared_memory_size, |
| - sender_.get())); |
| - CHECK(shared_memory.get()); |
| - if (!shared_memory->Map(shared_memory_size)) |
| - CHECK(false); |
| + DCHECK_LT(buffer_size + data_size, kLargeThresholdBytes); |
| + consolidating_buffer->insert(consolidating_buffer->end(), data.data(), |
| + data.data() + data_size); |
| + } |
| +} |
| - size_t data_size = data.size(); |
| - const char* data_ptr = data.data(); |
| - while (data_size) { |
| - size_t chunk_size = std::min(data_size, shared_memory_size); |
| - memcpy(shared_memory->memory(), data_ptr, chunk_size); |
| - sender_->Send(new BlobHostMsg_SyncAppendSharedMemory( |
| - uuid_str, shared_memory->handle(), chunk_size)); |
| - data_size -= chunk_size; |
| - data_ptr += chunk_size; |
| - } |
| +void WebBlobRegistryImpl::SendOversizedDataForBlob( |
| + const std::string& uuid_str, |
| + const blink::WebThreadSafeData& data) { |
| + DCHECK_GE(data.size(), kLargeThresholdBytes); |
| + // We handle larger amounts of data via SharedMemory instead of |
| + // writing it directly to the IPC channel. |
| + size_t shared_memory_size = std::min(data.size(), kMaxSharedMemoryBytes); |
| + scoped_ptr<base::SharedMemory> shared_memory( |
| + ChildThread::AllocateSharedMemory(shared_memory_size, sender_.get())); |
| + CHECK(shared_memory.get()); |
| + if (!shared_memory->Map(shared_memory_size)) |
| + CHECK(false); |
| + |
| + size_t data_size = data.size(); |
| + const char* data_ptr = data.data(); |
| + while (data_size) { |
| + size_t chunk_size = std::min(data_size, shared_memory_size); |
| + memcpy(shared_memory->memory(), data_ptr, chunk_size); |
| + sender_->Send(new BlobHostMsg_SyncAppendSharedMemory( |
| + uuid_str, shared_memory->handle(), chunk_size)); |
| + data_size -= chunk_size; |
| + data_ptr += chunk_size; |
| } |
| } |