diff options
author | ahendrickson@chromium.org <ahendrickson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-21 00:09:52 +0000 |
---|---|---|
committer | ahendrickson@chromium.org <ahendrickson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-21 00:09:52 +0000 |
commit | 2606e5a98b9a92fad68ed66bb02ec71409d42ef6 (patch) | |
tree | 087ec7b2bb1c1e338e41396a01723c89682253c6 /content/browser/download/download_buffer.cc | |
parent | 4a14bca90a556d9f920e8f2bef5116430176d56c (diff) | |
download | chromium_src-2606e5a98b9a92fad68ed66bb02ec71409d42ef6.zip chromium_src-2606e5a98b9a92fad68ed66bb02ec71409d42ef6.tar.gz chromium_src-2606e5a98b9a92fad68ed66bb02ec71409d42ef6.tar.bz2 |
DownloadBuffer is used in two different ways, only one of which is used across threads.
This CL splits a ContentVector typedef from the DownloadBuffer class.
DownloadBuffer is also made RefCountedThreadSafe.
BUG=None
TEST=None
Review URL: http://codereview.chromium.org/8036038
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@106640 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/download/download_buffer.cc')
-rw-r--r-- | content/browser/download/download_buffer.cc | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/content/browser/download/download_buffer.cc b/content/browser/download/download_buffer.cc new file mode 100644 index 0000000..eadb60b --- /dev/null +++ b/content/browser/download/download_buffer.cc @@ -0,0 +1,71 @@ +// Copyright (c) 2011 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 "content/browser/download/download_buffer.h" + +#include "net/base/io_buffer.h" + +namespace content { + +net::IOBuffer* AssembleData(const ContentVector& contents, size_t* num_bytes) { + if (*num_bytes) + *num_bytes = 0; + + size_t data_len; + // Determine how large a buffer we need. + size_t assembly_buffer_length = 0; + for (size_t i = 0; i < contents.size(); ++i) { + data_len = contents[i].second; + assembly_buffer_length += data_len; + } + + // 0-length IOBuffers are not allowed. + if (assembly_buffer_length == 0) + return NULL; + + net::IOBuffer* assembly_buffer = new net::IOBuffer(assembly_buffer_length); + + // Copy the data into |assembly_buffer|. + size_t bytes_copied = 0; + for (size_t i = 0; i < contents.size(); ++i) { + net::IOBuffer* data = contents[i].first; + data_len = contents[i].second; + DCHECK(data != NULL); + DCHECK_LE(bytes_copied + data_len, assembly_buffer_length); + memcpy(assembly_buffer->data() + bytes_copied, data->data(), data_len); + bytes_copied += data_len; + } + DCHECK_EQ(assembly_buffer_length, bytes_copied); + + if (num_bytes) + *num_bytes = assembly_buffer_length; + + return assembly_buffer; +} + +DownloadBuffer::DownloadBuffer() { +} + +DownloadBuffer::~DownloadBuffer() { +} + +size_t DownloadBuffer::AddData(net::IOBuffer* io_buffer, size_t byte_count) { + base::AutoLock auto_lock(lock_); + contents_.push_back(std::make_pair(io_buffer, byte_count)); + return contents_.size(); +} + +ContentVector* DownloadBuffer::ReleaseContents() { + base::AutoLock auto_lock(lock_); + ContentVector* other_contents = new ContentVector; + other_contents->swap(contents_); + return other_contents; +} + +size_t DownloadBuffer::size() const { + base::AutoLock auto_lock(lock_); + return contents_.size(); +} + +} // namespace content |