summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-12 00:03:19 +0000
committerrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-12 00:03:19 +0000
commit74a85ce422d6b52a79b3cf159203ec7fa35c15ee (patch)
treee35947baf74dd4325b926bf44a3364ed3cfe4038 /net
parent39008c0d7c9f627b6eeef41bd7ddb78b7ebe086d (diff)
downloadchromium_src-74a85ce422d6b52a79b3cf159203ec7fa35c15ee.zip
chromium_src-74a85ce422d6b52a79b3cf159203ec7fa35c15ee.tar.gz
chromium_src-74a85ce422d6b52a79b3cf159203ec7fa35c15ee.tar.bz2
Extend the IOBuffer to the disk cache.
This is cleanup from bug 5325. Original code review: http://codereview.chromium.org/20134/show Review URL: http://codereview.chromium.org/20251 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9626 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/base/completion_callback.h17
-rw-r--r--net/base/io_buffer.h16
-rw-r--r--net/disk_cache/backend_unittest.cc64
-rw-r--r--net/disk_cache/disk_cache.h14
-rw-r--r--net/disk_cache/disk_cache_perftest.cc30
-rw-r--r--net/disk_cache/entry_impl.cc27
-rw-r--r--net/disk_cache/entry_impl.h4
-rw-r--r--net/disk_cache/entry_unittest.cc361
-rw-r--r--net/disk_cache/mem_entry_impl.cc14
-rw-r--r--net/disk_cache/mem_entry_impl.h4
-rw-r--r--net/disk_cache/stress_cache.cc14
-rw-r--r--net/http/http_cache.cc33
-rw-r--r--net/http/http_cache_unittest.cc8
-rw-r--r--net/http/http_network_transaction.cc1
-rw-r--r--net/http/http_transaction.h1
-rw-r--r--net/http/http_transaction_unittest.h1
-rw-r--r--net/tools/dump_cache/upgrade.cc12
-rw-r--r--net/url_request/url_request_view_cache_job.cc6
18 files changed, 345 insertions, 282 deletions
diff --git a/net/base/completion_callback.h b/net/base/completion_callback.h
index f9bb233..4013f71 100644
--- a/net/base/completion_callback.h
+++ b/net/base/completion_callback.h
@@ -6,7 +6,6 @@
#define NET_BASE_COMPLETION_CALLBACK_H__
#include "base/task.h"
-#include "net/base/io_buffer.h"
namespace net {
@@ -42,23 +41,8 @@ class CancelableCompletionCallback :
is_canceled_ = true;
}
- // Attaches the given buffer to this callback so it is valid until the
- // operation completes. TODO(rvargas): This is a temporal fix for bug 5325
- // while I send IOBuffer to the lower layers of code.
- void UseBuffer(net::IOBuffer* buffer) {
- DCHECK(!buffer_.get());
- buffer_ = buffer;
- }
-
- // The callback is not expected anymore so release the buffer.
- void ReleaseBuffer() {
- DCHECK(buffer_.get());
- buffer_ = NULL;
- }
-
virtual void RunWithParams(const Tuple1<int>& params) {
if (is_canceled_) {
- CancelableCompletionCallback<T>::ReleaseBuffer();
base::RefCounted<CancelableCompletionCallback<T> >::Release();
} else {
CompletionCallbackImpl<T>::RunWithParams(params);
@@ -66,7 +50,6 @@ class CancelableCompletionCallback :
}
private:
- scoped_refptr<net::IOBuffer> buffer_;
bool is_canceled_;
};
diff --git a/net/base/io_buffer.h b/net/base/io_buffer.h
index 39399ad..88066c2 100644
--- a/net/base/io_buffer.h
+++ b/net/base/io_buffer.h
@@ -19,7 +19,7 @@ class IOBuffer : public base::RefCountedThreadSafe<IOBuffer> {
DCHECK(buffer_size);
data_ = new char[buffer_size];
}
- explicit IOBuffer(char* buffer) : data_(buffer) {}
+ explicit IOBuffer(char* data) : data_(data) {}
virtual ~IOBuffer() {
delete[] data_;
}
@@ -30,6 +30,20 @@ class IOBuffer : public base::RefCountedThreadSafe<IOBuffer> {
char* data_;
};
+// This class allows the creation of a temporary IOBuffer that doesn't really
+// own the underlying buffer. Please use this class only as a last resort.
+// A good example is the buffer for a synchronous operation, where we can be
+// sure that nobody is keeping an extra reference to this object so the lifetime
+// of the buffer can be completely managed by its intended owner.
+class WrappedIOBuffer : public net::IOBuffer {
+ public:
+ explicit WrappedIOBuffer(const char* data)
+ : net::IOBuffer(const_cast<char*>(data)) {}
+ ~WrappedIOBuffer() {
+ data_ = NULL;
+ }
+};
+
} // namespace net
#endif // NET_BASE_IO_BUFFER_H_
diff --git a/net/disk_cache/backend_unittest.cc b/net/disk_cache/backend_unittest.cc
index 6d3dfb3..d23ea7f 100644
--- a/net/disk_cache/backend_unittest.cc
+++ b/net/disk_cache/backend_unittest.cc
@@ -7,6 +7,7 @@
#include "base/path_service.h"
#include "base/platform_thread.h"
#include "base/string_util.h"
+#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/backend_impl.h"
#include "net/disk_cache/disk_cache_test_base.h"
@@ -209,21 +210,21 @@ TEST_F(DiskCacheBackendTest, ExternalFiles) {
std::wstring filename = GetCachePath();
file_util::AppendToPath(&filename, L"f_000001");
- const int kDataSize = 50;
- char data[kDataSize];
- CacheTestFillBuffer(data, kDataSize, false);
- ASSERT_EQ(kDataSize, file_util::WriteFile(filename, data, kDataSize));
+ const int kSize = 50;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize);
+ CacheTestFillBuffer(buffer1->data(), kSize, false);
+ ASSERT_EQ(kSize, file_util::WriteFile(filename, buffer1->data(), kSize));
// Now let's create a file with the cache.
disk_cache::Entry* entry;
ASSERT_TRUE(cache_->CreateEntry("key", &entry));
- ASSERT_EQ(0, entry->WriteData(0, 20000, data, 0, NULL, false));
+ ASSERT_EQ(0, entry->WriteData(0, 20000, buffer1, 0, NULL, false));
entry->Close();
// And verify that the first file is still there.
- char buffer[kDataSize];
- ASSERT_EQ(kDataSize, file_util::ReadFile(filename, buffer, kDataSize));
- EXPECT_EQ(0, memcmp(data, buffer, kDataSize));
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize);
+ ASSERT_EQ(kSize, file_util::ReadFile(filename, buffer2->data(), kSize));
+ EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize));
}
void DiskCacheBackendTest::BackendSetSize() {
@@ -237,7 +238,8 @@ void DiskCacheBackendTest::BackendSetSize() {
disk_cache::Entry* entry;
ASSERT_TRUE(cache_->CreateEntry(first, &entry));
- char buffer[cache_size] = {0};
+ scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(cache_size);
+ memset(buffer->data(), 0, cache_size);
EXPECT_EQ(cache_size / 10, entry->WriteData(0, 0, buffer, cache_size / 10,
NULL, false)) << "normal file";
@@ -338,20 +340,20 @@ TEST_F(DiskCacheBackendTest, ValidEntry) {
disk_cache::Entry* entry1;
ASSERT_TRUE(cache_->CreateEntry(key, &entry1));
- char data[] = "And the data to save";
- EXPECT_TRUE(sizeof(data) == entry1->WriteData(0, 0, data, sizeof(data), NULL,
- false));
+ const int kSize = 50;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize);
+ base::strlcpy(buffer1->data(), "And the data to save", kSize);
+ EXPECT_EQ(kSize, entry1->WriteData(0, 0, buffer1, kSize, NULL, false));
entry1->Close();
SimulateCrash();
ASSERT_TRUE(cache_->OpenEntry(key, &entry1));
- char buffer[40];
- memset(buffer, 0, sizeof(buffer));
- EXPECT_TRUE(sizeof(data) == entry1->ReadData(0, 0, buffer, sizeof(data),
- NULL));
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize);
+ memset(buffer2->data(), 0, kSize);
+ EXPECT_EQ(kSize, entry1->ReadData(0, 0, buffer2, kSize, NULL));
entry1->Close();
- EXPECT_STREQ(data, buffer);
+ EXPECT_STREQ(buffer1->data(), buffer2->data());
}
// The same logic of the previous test (ValidEntry), but this time force the
@@ -366,9 +368,10 @@ TEST_F(DiskCacheBackendTest, InvalidEntry) {
disk_cache::Entry* entry1;
ASSERT_TRUE(cache_->CreateEntry(key, &entry1));
- char data[] = "And the data to save";
- EXPECT_TRUE(sizeof(data) == entry1->WriteData(0, 0, data, sizeof(data), NULL,
- false));
+ const int kSize = 50;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize);
+ base::strlcpy(buffer1->data(), "And the data to save", kSize);
+ EXPECT_EQ(kSize, entry1->WriteData(0, 0, buffer1, kSize, NULL, false));
SimulateCrash();
EXPECT_FALSE(cache_->OpenEntry(key, &entry1));
@@ -386,12 +389,13 @@ TEST_F(DiskCacheBackendTest, InvalidEntryRead) {
disk_cache::Entry* entry1;
ASSERT_TRUE(cache_->CreateEntry(key, &entry1));
- char data[] = "And the data to save";
- EXPECT_TRUE(sizeof(data) == entry1->WriteData(0, 0, data, sizeof(data), NULL,
- false));
+ const int kSize = 50;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize);
+ base::strlcpy(buffer1->data(), "And the data to save", kSize);
+ EXPECT_EQ(kSize, entry1->WriteData(0, 0, buffer1, kSize, NULL, false));
entry1->Close();
ASSERT_TRUE(cache_->OpenEntry(key, &entry1));
- EXPECT_TRUE(sizeof(data) == entry1->ReadData(0, 0, data, sizeof(data), NULL));
+ EXPECT_EQ(kSize, entry1->ReadData(0, 0, buffer1, kSize, NULL));
SimulateCrash();
@@ -462,7 +466,8 @@ TEST_F(DiskCacheBackendTest, TrimInvalidEntry) {
disk_cache::Entry* entry;
ASSERT_TRUE(cache_->CreateEntry(first, &entry));
- char buffer[cache_size] = {0};
+ scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(cache_size);
+ memset(buffer->data(), 0, cache_size);
EXPECT_EQ(cache_size * 19 / 20, entry->WriteData(0, 0, buffer,
cache_size * 19 / 20, NULL, false));
@@ -549,12 +554,13 @@ TEST_F(DiskCacheBackendTest, InvalidEntryEnumeration) {
disk_cache::Entry *entry, *entry1, *entry2;
ASSERT_TRUE(cache_->CreateEntry(key, &entry1));
- char data[] = "And the data to save";
- EXPECT_TRUE(sizeof(data) == entry1->WriteData(0, 0, data, sizeof(data), NULL,
- false));
+ const int kSize = 50;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize);
+ base::strlcpy(buffer1->data(), "And the data to save", kSize);
+ EXPECT_EQ(kSize, entry1->WriteData(0, 0, buffer1, kSize, NULL, false));
entry1->Close();
ASSERT_TRUE(cache_->OpenEntry(key, &entry1));
- EXPECT_TRUE(sizeof(data) == entry1->ReadData(0, 0, data, sizeof(data), NULL));
+ EXPECT_EQ(kSize, entry1->ReadData(0, 0, buffer1, kSize, NULL));
std::string key2("Another key");
ASSERT_TRUE(cache_->CreateEntry(key2, &entry2));
diff --git a/net/disk_cache/disk_cache.h b/net/disk_cache/disk_cache.h
index ab01086..59d2ad2 100644
--- a/net/disk_cache/disk_cache.h
+++ b/net/disk_cache/disk_cache.h
@@ -15,6 +15,10 @@
#include "base/time.h"
#include "net/base/completion_callback.h"
+namespace net {
+class IOBuffer;
+}
+
namespace disk_cache {
class Entry;
@@ -123,12 +127,13 @@ class Entry {
// operation is complete. Otherwise, completion_callback will be
// called on the current thread once the read completes. Returns the
// number of bytes read or a network error code. If a completion callback is
- // provided then it will be called if this function returns ERR_IO_PENDING.
+ // provided then it will be called if this function returns ERR_IO_PENDING,
+ // and a reference to |buf| will be retained until the callback is called.
// Note that the callback will be invoked in any case, even after Close has
// been called; in other words, the caller may close this entry without
// having to wait for all the callbacks, and still rely on the cleanup
// performed from the callback code.
- virtual int ReadData(int index, int offset, char* buf, int buf_len,
+ virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len,
net::CompletionCallback* completion_callback) = 0;
// Copies cache data from the given buffer of length |buf_len|. If
@@ -136,14 +141,15 @@ class Entry {
// operation is complete. Otherwise, completion_callback will be
// called on the current thread once the write completes. Returns the
// number of bytes written or a network error code. If a completion callback
- // is provided then it will be called if this function returns ERR_IO_PENDING.
+ // is provided then it will be called if this function returns ERR_IO_PENDING,
+ // and a reference to |buf| will be retained until the callback is called.
// Note that the callback will be invoked in any case, even after Close has
// been called; in other words, the caller may close this entry without
// having to wait for all the callbacks, and still rely on the cleanup
// performed from the callback code.
// If truncate is true, this call will truncate the stored data at the end of
// what we are writing here.
- virtual int WriteData(int index, int offset, const char* buf, int buf_len,
+ virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len,
net::CompletionCallback* completion_callback,
bool truncate) = 0;
diff --git a/net/disk_cache/disk_cache_perftest.cc b/net/disk_cache/disk_cache_perftest.cc
index 0a869b2..74cac65 100644
--- a/net/disk_cache/disk_cache_perftest.cc
+++ b/net/disk_cache/disk_cache_perftest.cc
@@ -11,6 +11,7 @@
#include "base/string_util.h"
#include "base/test_file_util.h"
#include "base/timer.h"
+#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/block_files.h"
#include "net/disk_cache/disk_cache.h"
@@ -41,11 +42,12 @@ const int kMaxSize = 16 * 1024 - 1;
// to kMaxSize of data to each entry.
int TimeWrite(int num_entries, disk_cache::Backend* cache,
TestEntries* entries) {
- char buffer1[200];
- char buffer2[kMaxSize];
+ const int kSize1 = 200;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kMaxSize);
- CacheTestFillBuffer(buffer1, sizeof(buffer1), false);
- CacheTestFillBuffer(buffer2, sizeof(buffer2), false);
+ CacheTestFillBuffer(buffer1->data(), kSize1, false);
+ CacheTestFillBuffer(buffer2->data(), kMaxSize, false);
CallbackTest callback(1);
g_cache_tests_error = false;
@@ -60,17 +62,16 @@ int TimeWrite(int num_entries, disk_cache::Backend* cache,
for (int i = 0; i < num_entries; i++) {
TestEntry entry;
entry.key = GenerateKey(true);
- entry.data_len = rand() % sizeof(buffer2);
+ entry.data_len = rand() % kMaxSize;
entries->push_back(entry);
disk_cache::Entry* cache_entry;
if (!cache->CreateEntry(entry.key, &cache_entry))
break;
- int ret = cache_entry->WriteData(0, 0, buffer1, sizeof(buffer1), &callback,
- false);
+ int ret = cache_entry->WriteData(0, 0, buffer1, kSize1, &callback, false);
if (net::ERR_IO_PENDING == ret)
expected++;
- else if (sizeof(buffer1) != ret)
+ else if (kSize1 != ret)
break;
ret = cache_entry->WriteData(1, 0, buffer2, entry.data_len, &callback,
@@ -91,11 +92,12 @@ int TimeWrite(int num_entries, disk_cache::Backend* cache,
// Reads the data and metadata from each entry listed on |entries|.
int TimeRead(int num_entries, disk_cache::Backend* cache,
const TestEntries& entries, bool cold) {
- char buffer1[200];
- char buffer2[kMaxSize];
+ const int kSize1 = 200;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kMaxSize);
- CacheTestFillBuffer(buffer1, sizeof(buffer1), false);
- CacheTestFillBuffer(buffer2, sizeof(buffer2), false);
+ CacheTestFillBuffer(buffer1->data(), kSize1, false);
+ CacheTestFillBuffer(buffer2->data(), kMaxSize, false);
CallbackTest callback(1);
g_cache_tests_error = false;
@@ -113,10 +115,10 @@ int TimeRead(int num_entries, disk_cache::Backend* cache,
disk_cache::Entry* cache_entry;
if (!cache->OpenEntry(entries[i].key, &cache_entry))
break;
- int ret = cache_entry->ReadData(0, 0, buffer1, sizeof(buffer1), &callback);
+ int ret = cache_entry->ReadData(0, 0, buffer1, kSize1, &callback);
if (net::ERR_IO_PENDING == ret)
expected++;
- else if (sizeof(buffer1) != ret)
+ else if (kSize1 != ret)
break;
ret = cache_entry->ReadData(1, 0, buffer2, entries[i].data_len, &callback);
diff --git a/net/disk_cache/entry_impl.cc b/net/disk_cache/entry_impl.cc
index 0f6d426..a8f9ad1 100644
--- a/net/disk_cache/entry_impl.cc
+++ b/net/disk_cache/entry_impl.cc
@@ -7,6 +7,7 @@
#include "base/histogram.h"
#include "base/message_loop.h"
#include "base/string_util.h"
+#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/backend_impl.h"
#include "net/disk_cache/cache_util.h"
@@ -23,9 +24,9 @@ const int kKeyFileIndex = 3;
// operation from the actual net class.
class SyncCallback: public disk_cache::FileIOCallback {
public:
- SyncCallback(disk_cache::EntryImpl* entry,
+ SyncCallback(disk_cache::EntryImpl* entry, net::IOBuffer* buffer,
net::CompletionCallback* callback )
- : entry_(entry), callback_(callback) {
+ : entry_(entry), callback_(callback), buf_(buffer) {
entry->AddRef();
entry->IncrementIoCount();
}
@@ -36,6 +37,7 @@ class SyncCallback: public disk_cache::FileIOCallback {
private:
disk_cache::EntryImpl* entry_;
net::CompletionCallback* callback_;
+ scoped_refptr<net::IOBuffer> buf_;
DISALLOW_EVIL_CONSTRUCTORS(SyncCallback);
};
@@ -50,6 +52,7 @@ void SyncCallback::OnFileIOComplete(int bytes_copied) {
void SyncCallback::Discard() {
callback_ = NULL;
+ buf_ = NULL;
OnFileIOComplete(0);
}
@@ -194,7 +197,7 @@ int32 EntryImpl::GetDataSize(int index) const {
return entry->Data()->data_size[index];
}
-int EntryImpl::ReadData(int index, int offset, char* buf, int buf_len,
+int EntryImpl::ReadData(int index, int offset, net::IOBuffer* buf, int buf_len,
net::CompletionCallback* completion_callback) {
DCHECK(node_.Data()->dirty);
if (index < 0 || index >= NUM_STREAMS)
@@ -222,7 +225,7 @@ int EntryImpl::ReadData(int index, int offset, char* buf, int buf_len,
if (user_buffers_[index].get()) {
// Complete the operation locally.
DCHECK(kMaxBlockSize >= offset + buf_len);
- memcpy(buf , user_buffers_[index].get() + offset, buf_len);
+ memcpy(buf->data() , user_buffers_[index].get() + offset, buf_len);
stats.AddTime(Time::Now() - start);
return buf_len;
}
@@ -243,10 +246,10 @@ int EntryImpl::ReadData(int index, int offset, char* buf, int buf_len,
SyncCallback* io_callback = NULL;
if (completion_callback)
- io_callback = new SyncCallback(this, completion_callback);
+ io_callback = new SyncCallback(this, buf, completion_callback);
bool completed;
- if (!file->Read(buf, buf_len, file_offset, io_callback, &completed)) {
+ if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) {
if (io_callback)
io_callback->Discard();
return net::ERR_FAILED;
@@ -259,7 +262,7 @@ int EntryImpl::ReadData(int index, int offset, char* buf, int buf_len,
return (completed || !completion_callback) ? buf_len : net::ERR_IO_PENDING;
}
-int EntryImpl::WriteData(int index, int offset, const char* buf, int buf_len,
+int EntryImpl::WriteData(int index, int offset, net::IOBuffer* buf, int buf_len,
net::CompletionCallback* completion_callback,
bool truncate) {
DCHECK(node_.Data()->dirty);
@@ -317,8 +320,11 @@ int EntryImpl::WriteData(int index, int offset, const char* buf, int buf_len,
if (user_buffers_[index].get()) {
// Complete the operation locally.
+ if (!buf_len)
+ return 0;
+
DCHECK(kMaxBlockSize >= offset + buf_len);
- memcpy(user_buffers_[index].get() + offset, buf, buf_len);
+ memcpy(user_buffers_[index].get() + offset, buf->data(), buf_len);
stats.AddTime(Time::Now() - start);
return buf_len;
}
@@ -342,10 +348,11 @@ int EntryImpl::WriteData(int index, int offset, const char* buf, int buf_len,
SyncCallback* io_callback = NULL;
if (completion_callback)
- io_callback = new SyncCallback(this, completion_callback);
+ io_callback = new SyncCallback(this, buf, completion_callback);
bool completed;
- if (!file->Write(buf, buf_len, file_offset, io_callback, &completed)) {
+ if (!file->Write(buf->data(), buf_len, file_offset, io_callback,
+ &completed)) {
if (io_callback)
io_callback->Discard();
return net::ERR_FAILED;
diff --git a/net/disk_cache/entry_impl.h b/net/disk_cache/entry_impl.h
index 9b26c80..0b08a58 100644
--- a/net/disk_cache/entry_impl.h
+++ b/net/disk_cache/entry_impl.h
@@ -27,9 +27,9 @@ class EntryImpl : public Entry, public base::RefCounted<EntryImpl> {
virtual base::Time GetLastUsed() const;
virtual base::Time GetLastModified() const;
virtual int32 GetDataSize(int index) const;
- virtual int ReadData(int index, int offset, char* buf, int buf_len,
+ virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len,
net::CompletionCallback* completion_callback);
- virtual int WriteData(int index, int offset, const char* buf, int buf_len,
+ virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len,
net::CompletionCallback* completion_callback,
bool truncate);
diff --git a/net/disk_cache/entry_unittest.cc b/net/disk_cache/entry_unittest.cc
index 4a93151..47b70d0 100644
--- a/net/disk_cache/entry_unittest.cc
+++ b/net/disk_cache/entry_unittest.cc
@@ -6,6 +6,7 @@
#include "base/platform_thread.h"
#include "base/timer.h"
#include "base/string_util.h"
+#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/disk_cache_test_base.h"
#include "net/disk_cache/disk_cache_test_util.h"
@@ -29,6 +30,7 @@ class DiskCacheEntryTest : public DiskCacheTestWithCache {
void GetKey();
void GrowData();
void TruncateData();
+ void ZeroLengthIO();
void ReuseEntry(int size);
void InvalidData();
void DoomEntry();
@@ -40,32 +42,35 @@ void DiskCacheEntryTest::InternalSyncIO() {
ASSERT_TRUE(cache_->CreateEntry("the first key", &entry1));
ASSERT_TRUE(NULL != entry1);
- char buffer1[10];
- CacheTestFillBuffer(buffer1, sizeof(buffer1), false);
- EXPECT_EQ(0, entry1->ReadData(0, 0, buffer1, sizeof(buffer1), NULL));
- base::strlcpy(buffer1, "the data", arraysize(buffer1));
- EXPECT_EQ(10, entry1->WriteData(0, 0, buffer1, sizeof(buffer1), NULL, false));
- memset(buffer1, 0, sizeof(buffer1));
- EXPECT_EQ(10, entry1->ReadData(0, 0, buffer1, sizeof(buffer1), NULL));
- EXPECT_STREQ("the data", buffer1);
-
- char buffer2[5000];
- char buffer3[10000] = {0};
- CacheTestFillBuffer(buffer2, sizeof(buffer2), false);
- base::strlcpy(buffer2, "The really big data goes here", arraysize(buffer2));
- EXPECT_EQ(5000, entry1->WriteData(1, 1500, buffer2, sizeof(buffer2), NULL,
- false));
- memset(buffer2, 0, sizeof(buffer2));
- EXPECT_EQ(4989, entry1->ReadData(1, 1511, buffer2, sizeof(buffer2), NULL));
- EXPECT_STREQ("big data goes here", buffer2);
- EXPECT_EQ(5000, entry1->ReadData(1, 0, buffer2, sizeof(buffer2), NULL));
- EXPECT_EQ(0, memcmp(buffer2, buffer3, 1500));
- EXPECT_EQ(1500, entry1->ReadData(1, 5000, buffer2, sizeof(buffer2), NULL));
-
- EXPECT_EQ(0, entry1->ReadData(1, 6500, buffer2, sizeof(buffer2), NULL));
- EXPECT_EQ(6500, entry1->ReadData(1, 0, buffer3, sizeof(buffer3), NULL));
+ const int kSize1 = 10;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
+ CacheTestFillBuffer(buffer1->data(), kSize1, false);
+ EXPECT_EQ(0, entry1->ReadData(0, 0, buffer1, kSize1, NULL));
+ base::strlcpy(buffer1->data(), "the data", kSize1);
+ EXPECT_EQ(10, entry1->WriteData(0, 0, buffer1, kSize1, NULL, false));
+ memset(buffer1->data(), 0, kSize1);
+ EXPECT_EQ(10, entry1->ReadData(0, 0, buffer1, kSize1, NULL));
+ EXPECT_STREQ("the data", buffer1->data());
+
+ const int kSize2 = 5000;
+ const int kSize3 = 10000;
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize2);
+ scoped_refptr<net::IOBuffer> buffer3 = new net::IOBuffer(kSize3);
+ memset(buffer3->data(), 0, kSize3);
+ CacheTestFillBuffer(buffer2->data(), kSize2, false);
+ base::strlcpy(buffer2->data(), "The really big data goes here", kSize2);
+ EXPECT_EQ(5000, entry1->WriteData(1, 1500, buffer2, kSize2, NULL, false));
+ memset(buffer2->data(), 0, kSize2);
+ EXPECT_EQ(4989, entry1->ReadData(1, 1511, buffer2, kSize2, NULL));
+ EXPECT_STREQ("big data goes here", buffer2->data());
+ EXPECT_EQ(5000, entry1->ReadData(1, 0, buffer2, kSize2, NULL));
+ EXPECT_EQ(0, memcmp(buffer2->data(), buffer3->data(), 1500));
+ EXPECT_EQ(1500, entry1->ReadData(1, 5000, buffer2, kSize2, NULL));
+
+ EXPECT_EQ(0, entry1->ReadData(1, 6500, buffer2, kSize2, NULL));
+ EXPECT_EQ(6500, entry1->ReadData(1, 0, buffer3, kSize3, NULL));
EXPECT_EQ(8192, entry1->WriteData(1, 0, buffer3, 8192, NULL, false));
- EXPECT_EQ(8192, entry1->ReadData(1, 0, buffer3, sizeof(buffer3), NULL));
+ EXPECT_EQ(8192, entry1->ReadData(1, 0, buffer3, kSize3, NULL));
EXPECT_EQ(8192, entry1->GetDataSize(1));
entry1->Doom();
@@ -110,64 +115,66 @@ void DiskCacheEntryTest::InternalAsyncIO() {
MessageLoopHelper helper;
- char buffer1[10];
- char buffer2[5000];
- char buffer3[10000];
- CacheTestFillBuffer(buffer1, sizeof(buffer1), false);
- CacheTestFillBuffer(buffer2, sizeof(buffer2), false);
- CacheTestFillBuffer(buffer3, sizeof(buffer3), false);
-
- EXPECT_EQ(0, entry1->ReadData(0, 0, buffer1, sizeof(buffer1), &callback1));
- base::strlcpy(buffer1, "the data", arraysize(buffer1));
+ const int kSize1 = 10;
+ const int kSize2 = 5000;
+ const int kSize3 = 10000;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize2);
+ scoped_refptr<net::IOBuffer> buffer3 = new net::IOBuffer(kSize3);
+ CacheTestFillBuffer(buffer1->data(), kSize1, false);
+ CacheTestFillBuffer(buffer2->data(), kSize2, false);
+ CacheTestFillBuffer(buffer3->data(), kSize3, false);
+
+ EXPECT_EQ(0, entry1->ReadData(0, 0, buffer1, kSize1, &callback1));
+ base::strlcpy(buffer1->data(), "the data", kSize1);
int expected = 0;
- int ret = entry1->WriteData(0, 0, buffer1, sizeof(buffer1), &callback2,
- false);
+ int ret = entry1->WriteData(0, 0, buffer1, kSize1, &callback2, false);
EXPECT_TRUE(10 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
- memset(buffer2, 0, sizeof(buffer1));
- ret = entry1->ReadData(0, 0, buffer2, sizeof(buffer1), &callback3);
+ memset(buffer2->data(), 0, kSize1);
+ ret = entry1->ReadData(0, 0, buffer2, kSize1, &callback3);
EXPECT_TRUE(10 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
g_cache_tests_max_id = 3;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
- EXPECT_STREQ("the data", buffer2);
+ EXPECT_STREQ("the data", buffer2->data());
- base::strlcpy(buffer2, "The really big data goes here", arraysize(buffer2));
- ret = entry1->WriteData(1, 1500, buffer2, sizeof(buffer2), &callback4, false);
+ base::strlcpy(buffer2->data(), "The really big data goes here", kSize2);
+ ret = entry1->WriteData(1, 1500, buffer2, kSize2, &callback4, false);
EXPECT_TRUE(5000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
- memset(buffer3, 0, sizeof(buffer2));
- ret = entry1->ReadData(1, 1511, buffer3, sizeof(buffer2), &callback5);
+ memset(buffer3->data(), 0, kSize2);
+ ret = entry1->ReadData(1, 1511, buffer3, kSize2, &callback5);
EXPECT_TRUE(4989 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
g_cache_tests_max_id = 5;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
- EXPECT_STREQ("big data goes here", buffer3);
- ret = entry1->ReadData(1, 0, buffer2, sizeof(buffer2), &callback6);
+ EXPECT_STREQ("big data goes here", buffer3->data());
+ ret = entry1->ReadData(1, 0, buffer2, kSize2, &callback6);
EXPECT_TRUE(5000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
- memset(buffer3, 0, sizeof(buffer3));
+ memset(buffer3->data(), 0, kSize3);
g_cache_tests_max_id = 6;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
- EXPECT_EQ(0, memcmp(buffer2, buffer3, 1500));
- ret = entry1->ReadData(1, 5000, buffer2, sizeof(buffer2), &callback7);
+ EXPECT_EQ(0, memcmp(buffer2->data(), buffer3->data(), 1500));
+ ret = entry1->ReadData(1, 5000, buffer2, kSize2, &callback7);
EXPECT_TRUE(1500 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
- EXPECT_EQ(0, entry1->ReadData(1, 6500, buffer2, sizeof(buffer2), &callback8));
- ret = entry1->ReadData(1, 0, buffer3, sizeof(buffer3), &callback9);
+ EXPECT_EQ(0, entry1->ReadData(1, 6500, buffer2, kSize2, &callback8));
+ ret = entry1->ReadData(1, 0, buffer3, kSize3, &callback9);
EXPECT_TRUE(6500 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
@@ -177,19 +184,19 @@ void DiskCacheEntryTest::InternalAsyncIO() {
if (net::ERR_IO_PENDING == ret)
expected++;
- ret = entry1->ReadData(1, 0, buffer3, sizeof(buffer3), &callback11);
+ ret = entry1->ReadData(1, 0, buffer3, kSize3, &callback11);
EXPECT_TRUE(8192 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_EQ(8192, entry1->GetDataSize(1));
- ret = entry1->ReadData(0, 0, buffer1, sizeof(buffer1), &callback12);
+ ret = entry1->ReadData(0, 0, buffer1, kSize1, &callback12);
EXPECT_TRUE(10 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
- ret = entry1->ReadData(1, 0, buffer2, sizeof(buffer2), &callback13);
+ ret = entry1->ReadData(1, 0, buffer2, kSize2, &callback13);
EXPECT_TRUE(5000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
@@ -220,30 +227,30 @@ void DiskCacheEntryTest::ExternalSyncIO() {
disk_cache::Entry *entry1;
ASSERT_TRUE(cache_->CreateEntry("the first key", &entry1));
- char buffer1[17000], buffer2[25000];
- CacheTestFillBuffer(buffer1, sizeof(buffer1), false);
- CacheTestFillBuffer(buffer2, sizeof(buffer2), false);
- base::strlcpy(buffer1, "the data", arraysize(buffer1));
- EXPECT_EQ(17000, entry1->WriteData(0, 0, buffer1, sizeof(buffer1), NULL,
- false));
- memset(buffer1, 0, sizeof(buffer1));
- EXPECT_EQ(17000, entry1->ReadData(0, 0, buffer1, sizeof(buffer1), NULL));
- EXPECT_STREQ("the data", buffer1);
-
- base::strlcpy(buffer2, "The really big data goes here", arraysize(buffer2));
- EXPECT_EQ(25000, entry1->WriteData(1, 10000, buffer2, sizeof(buffer2), NULL,
- false));
- memset(buffer2, 0, sizeof(buffer2));
- EXPECT_EQ(24989, entry1->ReadData(1, 10011, buffer2, sizeof(buffer2), NULL));
- EXPECT_STREQ("big data goes here", buffer2);
- EXPECT_EQ(25000, entry1->ReadData(1, 0, buffer2, sizeof(buffer2), NULL));
- EXPECT_EQ(0, memcmp(buffer2, buffer2, 10000));
- EXPECT_EQ(5000, entry1->ReadData(1, 30000, buffer2, sizeof(buffer2), NULL));
-
- EXPECT_EQ(0, entry1->ReadData(1, 35000, buffer2, sizeof(buffer2), NULL));
- EXPECT_EQ(17000, entry1->ReadData(1, 0, buffer1, sizeof(buffer1), NULL));
- EXPECT_EQ(17000, entry1->WriteData(1, 20000, buffer1, sizeof(buffer1), NULL,
- false));
+ const int kSize1 = 17000;
+ const int kSize2 = 25000;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize2);
+ CacheTestFillBuffer(buffer1->data(), kSize1, false);
+ CacheTestFillBuffer(buffer2->data(), kSize2, false);
+ base::strlcpy(buffer1->data(), "the data", kSize1);
+ EXPECT_EQ(17000, entry1->WriteData(0, 0, buffer1, kSize1, NULL, false));
+ memset(buffer1->data(), 0, kSize1);
+ EXPECT_EQ(17000, entry1->ReadData(0, 0, buffer1, kSize1, NULL));
+ EXPECT_STREQ("the data", buffer1->data());
+
+ base::strlcpy(buffer2->data(), "The really big data goes here", kSize2);
+ EXPECT_EQ(25000, entry1->WriteData(1, 10000, buffer2, kSize2, NULL, false));
+ memset(buffer2->data(), 0, kSize2);
+ EXPECT_EQ(24989, entry1->ReadData(1, 10011, buffer2, kSize2, NULL));
+ EXPECT_STREQ("big data goes here", buffer2->data());
+ EXPECT_EQ(25000, entry1->ReadData(1, 0, buffer2, kSize2, NULL));
+ EXPECT_EQ(0, memcmp(buffer2->data(), buffer2->data(), 10000));
+ EXPECT_EQ(5000, entry1->ReadData(1, 30000, buffer2, kSize2, NULL));
+
+ EXPECT_EQ(0, entry1->ReadData(1, 35000, buffer2, kSize2, NULL));
+ EXPECT_EQ(17000, entry1->ReadData(1, 0, buffer1, kSize1, NULL));
+ EXPECT_EQ(17000, entry1->WriteData(1, 20000, buffer1, kSize1, NULL, false));
EXPECT_EQ(37000, entry1->GetDataSize(1));
entry1->Doom();
@@ -284,13 +291,17 @@ void DiskCacheEntryTest::ExternalAsyncIO() {
MessageLoopHelper helper;
- char buffer1[17000], buffer2[25000], buffer3[25000];
- CacheTestFillBuffer(buffer1, sizeof(buffer1), false);
- CacheTestFillBuffer(buffer2, sizeof(buffer2), false);
- CacheTestFillBuffer(buffer3, sizeof(buffer3), false);
- base::strlcpy(buffer1, "the data", arraysize(buffer1));
- int ret = entry1->WriteData(0, 0, buffer1, sizeof(buffer1), &callback1,
- false);
+ const int kSize1 = 17000;
+ const int kSize2 = 25000;
+ const int kSize3 = 25000;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize2);
+ scoped_refptr<net::IOBuffer> buffer3 = new net::IOBuffer(kSize3);
+ CacheTestFillBuffer(buffer1->data(), kSize1, false);
+ CacheTestFillBuffer(buffer2->data(), kSize2, false);
+ CacheTestFillBuffer(buffer3->data(), kSize3, false);
+ base::strlcpy(buffer1->data(), "the data", kSize1);
+ int ret = entry1->WriteData(0, 0, buffer1, kSize1, &callback1, false);
EXPECT_TRUE(17000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
@@ -298,19 +309,18 @@ void DiskCacheEntryTest::ExternalAsyncIO() {
g_cache_tests_max_id = 1;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
- memset(buffer2, 0, sizeof(buffer1));
- ret = entry1->ReadData(0, 0, buffer2, sizeof(buffer1), &callback2);
+ memset(buffer2->data(), 0, kSize1);
+ ret = entry1->ReadData(0, 0, buffer2, kSize1, &callback2);
EXPECT_TRUE(17000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
g_cache_tests_max_id = 2;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
- EXPECT_STREQ("the data", buffer1);
+ EXPECT_STREQ("the data", buffer1->data());
- base::strlcpy(buffer2, "The really big data goes here", arraysize(buffer2));
- ret = entry1->WriteData(1, 10000, buffer2, sizeof(buffer2), &callback3,
- false);
+ base::strlcpy(buffer2->data(), "The really big data goes here", kSize2);
+ ret = entry1->WriteData(1, 10000, buffer2, kSize2, &callback3, false);
EXPECT_TRUE(25000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
@@ -318,36 +328,34 @@ void DiskCacheEntryTest::ExternalAsyncIO() {
g_cache_tests_max_id = 3;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
- memset(buffer3, 0, sizeof(buffer3));
- ret = entry1->ReadData(1, 10011, buffer3, sizeof(buffer3), &callback4);
+ memset(buffer3->data(), 0, kSize3);
+ ret = entry1->ReadData(1, 10011, buffer3, kSize3, &callback4);
EXPECT_TRUE(24989 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
g_cache_tests_max_id = 4;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
- EXPECT_STREQ("big data goes here", buffer3);
- ret = entry1->ReadData(1, 0, buffer2, sizeof(buffer2), &callback5);
+ EXPECT_STREQ("big data goes here", buffer3->data());
+ ret = entry1->ReadData(1, 0, buffer2, kSize2, &callback5);
EXPECT_TRUE(25000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
g_cache_tests_max_id = 5;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
- EXPECT_EQ(0, memcmp(buffer2, buffer2, 10000));
- ret = entry1->ReadData(1, 30000, buffer2, sizeof(buffer2), &callback6);
+ EXPECT_EQ(0, memcmp(buffer2->data(), buffer2->data(), 10000));
+ ret = entry1->ReadData(1, 30000, buffer2, kSize2, &callback6);
EXPECT_TRUE(5000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
- EXPECT_EQ(0, entry1->ReadData(1, 35000, buffer2, sizeof(buffer2),
- &callback7));
- ret = entry1->ReadData(1, 0, buffer1, sizeof(buffer1), &callback8);
+ EXPECT_EQ(0, entry1->ReadData(1, 35000, buffer2, kSize2, &callback7));
+ ret = entry1->ReadData(1, 0, buffer1, kSize1, &callback8);
EXPECT_TRUE(17000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
- ret = entry1->WriteData(1, 20000, buffer1, sizeof(buffer1), &callback9,
- false);
+ ret = entry1->WriteData(1, 20000, buffer1, kSize1, &callback9, false);
EXPECT_TRUE(17000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
@@ -381,17 +389,17 @@ void DiskCacheEntryTest::StreamAccess() {
ASSERT_TRUE(NULL != entry);
const int kBufferSize = 1024;
- char buffer1[kBufferSize];
- char buffer2[kBufferSize];
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kBufferSize);
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kBufferSize);
const int kNumStreams = 3;
for (int i = 0; i < kNumStreams; i++) {
- CacheTestFillBuffer(buffer1, kBufferSize, false);
+ CacheTestFillBuffer(buffer1->data(), kBufferSize, false);
EXPECT_EQ(kBufferSize, entry->WriteData(i, 0, buffer1, kBufferSize, NULL,
false));
- memset(buffer2, 0, kBufferSize);
+ memset(buffer2->data(), 0, kBufferSize);
EXPECT_EQ(kBufferSize, entry->ReadData(i, 0, buffer2, kBufferSize, NULL));
- EXPECT_EQ(0, memcmp(buffer1, buffer2, kBufferSize));
+ EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kBufferSize));
}
EXPECT_EQ(net::ERR_INVALID_ARGUMENT,
@@ -461,30 +469,30 @@ void DiskCacheEntryTest::GrowData() {
disk_cache::Entry *entry1, *entry2;
ASSERT_TRUE(cache_->CreateEntry(key1, &entry1));
- char buffer1[20000];
- char buffer2[20000];
- CacheTestFillBuffer(buffer1, sizeof(buffer1), false);
- memset(buffer2, 0, sizeof(buffer2));
+ const int kSize = 20000;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize);
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize);
+ CacheTestFillBuffer(buffer1->data(), kSize, false);
+ memset(buffer2->data(), 0, kSize);
- base::strlcpy(buffer1, "the data", arraysize(buffer1));
+ base::strlcpy(buffer1->data(), "the data", kSize);
EXPECT_EQ(10, entry1->WriteData(0, 0, buffer1, 10, NULL, false));
EXPECT_EQ(10, entry1->ReadData(0, 0, buffer2, 10, NULL));
- EXPECT_STREQ("the data", buffer2);
+ EXPECT_STREQ("the data", buffer2->data());
EXPECT_EQ(10, entry1->GetDataSize(0));
EXPECT_EQ(2000, entry1->WriteData(0, 0, buffer1, 2000, NULL, false));
EXPECT_EQ(2000, entry1->GetDataSize(0));
EXPECT_EQ(2000, entry1->ReadData(0, 0, buffer2, 2000, NULL));
- EXPECT_TRUE(!memcmp(buffer1, buffer2, 2000));
+ EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 2000));
- EXPECT_EQ(20000, entry1->WriteData(0, 0, buffer1, sizeof(buffer1), NULL,
- false));
+ EXPECT_EQ(20000, entry1->WriteData(0, 0, buffer1, kSize, NULL, false));
EXPECT_EQ(20000, entry1->GetDataSize(0));
- EXPECT_EQ(20000, entry1->ReadData(0, 0, buffer2, sizeof(buffer2), NULL));
- EXPECT_TRUE(!memcmp(buffer1, buffer2, sizeof(buffer1)));
+ EXPECT_EQ(20000, entry1->ReadData(0, 0, buffer2, kSize, NULL));
+ EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), kSize));
entry1->Close();
- memset(buffer2, 0, sizeof(buffer2));
+ memset(buffer2->data(), 0, kSize);
ASSERT_TRUE(cache_->CreateEntry("Second key", &entry2));
EXPECT_EQ(10, entry2->WriteData(0, 0, buffer1, 10, NULL, false));
EXPECT_EQ(10, entry2->GetDataSize(0));
@@ -495,17 +503,16 @@ void DiskCacheEntryTest::GrowData() {
EXPECT_EQ(2000, entry2->WriteData(0, 0, buffer1, 2000, NULL, false));
EXPECT_EQ(2000, entry2->GetDataSize(0));
EXPECT_EQ(2000, entry2->ReadData(0, 0, buffer2, 2000, NULL));
- EXPECT_TRUE(!memcmp(buffer1, buffer2, 2000));
+ EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 2000));
entry2->Close();
- memset(buffer2, 0, sizeof(buffer2));
+ memset(buffer2->data(), 0, kSize);
// Go from an internal address to an external one.
ASSERT_TRUE(cache_->OpenEntry("Second key", &entry2));
- EXPECT_EQ(20000, entry2->WriteData(0, 0, buffer1, sizeof(buffer1), NULL,
- false));
+ EXPECT_EQ(20000, entry2->WriteData(0, 0, buffer1, kSize, NULL, false));
EXPECT_EQ(20000, entry2->GetDataSize(0));
- EXPECT_EQ(20000, entry2->ReadData(0, 0, buffer2, sizeof(buffer2), NULL));
- EXPECT_TRUE(!memcmp(buffer1, buffer2, sizeof(buffer1)));
+ EXPECT_EQ(20000, entry2->ReadData(0, 0, buffer2, kSize, NULL));
+ EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), kSize));
entry2->Close();
}
@@ -525,11 +532,13 @@ void DiskCacheEntryTest::TruncateData() {
disk_cache::Entry *entry1;
ASSERT_TRUE(cache_->CreateEntry(key1, &entry1));
- char buffer1[20000];
- char buffer2[20000];
+ const int kSize1 = 20000;
+ const int kSize2 = 20000;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize2);
- CacheTestFillBuffer(buffer1, sizeof(buffer1), false);
- memset(buffer2, 0, sizeof(buffer2));
+ CacheTestFillBuffer(buffer1->data(), kSize1, false);
+ memset(buffer2->data(), 0, kSize2);
// Simple truncation:
EXPECT_EQ(200, entry1->WriteData(0, 0, buffer1, 200, NULL, false));
@@ -549,8 +558,8 @@ void DiskCacheEntryTest::TruncateData() {
EXPECT_EQ(20000, entry1->WriteData(0, 0, buffer1, 20000, NULL, true));
EXPECT_EQ(20000, entry1->GetDataSize(0));
EXPECT_EQ(20000, entry1->ReadData(0, 0, buffer2, 20000, NULL));
- EXPECT_TRUE(!memcmp(buffer1, buffer2, 20000));
- memset(buffer2, 0, sizeof(buffer2));
+ EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 20000));
+ memset(buffer2->data(), 0, kSize2);
// External file truncation
EXPECT_EQ(18000, entry1->WriteData(0, 0, buffer1, 18000, NULL, false));
@@ -564,9 +573,10 @@ void DiskCacheEntryTest::TruncateData() {
EXPECT_EQ(600, entry1->WriteData(0, 1000, buffer1, 600, NULL, true));
EXPECT_EQ(1600, entry1->GetDataSize(0));
EXPECT_EQ(600, entry1->ReadData(0, 1000, buffer2, 600, NULL));
- EXPECT_TRUE(!memcmp(buffer1, buffer2, 600));
+ EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 600));
EXPECT_EQ(1000, entry1->ReadData(0, 0, buffer2, 1000, NULL));
- EXPECT_TRUE(!memcmp(buffer1, buffer2, 1000)) << "Preserves previous data";
+ EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 1000)) <<
+ "Preserves previous data";
// Go from external file to zero length.
EXPECT_EQ(20000, entry1->WriteData(0, 0, buffer1, 20000, NULL, true));
@@ -593,6 +603,33 @@ TEST_F(DiskCacheEntryTest, MemoryOnlyTruncateData) {
TruncateData();
}
+void DiskCacheEntryTest::ZeroLengthIO() {
+ std::string key1("the first key");
+ disk_cache::Entry *entry1;
+ ASSERT_TRUE(cache_->CreateEntry(key1, &entry1));
+
+ EXPECT_EQ(0, entry1->ReadData(0, 0, NULL, 0, NULL));
+ EXPECT_EQ(0, entry1->WriteData(0, 0, NULL, 0, NULL, false));
+
+ // This write should extend the entry.
+ EXPECT_EQ(0, entry1->WriteData(0, 1000, NULL, 0, NULL, false));
+ EXPECT_EQ(0, entry1->ReadData(0, 500, NULL, 0, NULL));
+ EXPECT_EQ(0, entry1->ReadData(0, 2000, NULL, 0, NULL));
+ EXPECT_EQ(1000, entry1->GetDataSize(0));
+ entry1->Close();
+}
+
+TEST_F(DiskCacheEntryTest, ZeroLengthIO) {
+ InitCache();
+ ZeroLengthIO();
+}
+
+TEST_F(DiskCacheEntryTest, MemoryOnlyZeroLengthIO) {
+ SetMemoryOnlyMode();
+ InitCache();
+ ZeroLengthIO();
+}
+
// Write more than the total cache capacity but to a single entry. |size| is the
// amount of bytes to write each time.
void DiskCacheEntryTest::ReuseEntry(int size) {
@@ -604,12 +641,12 @@ void DiskCacheEntryTest::ReuseEntry(int size) {
std::string key2("the second key");
ASSERT_TRUE(cache_->CreateEntry(key2, &entry));
- scoped_array<char> buffer(new char[size]);
- CacheTestFillBuffer(buffer.get(), size, false);
+ scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(size);
+ CacheTestFillBuffer(buffer->data(), size, false);
for (int i = 0; i < 15; i++) {
- EXPECT_EQ(0, entry->WriteData(0, 0, buffer.get(), 0, NULL, true));
- EXPECT_EQ(size, entry->WriteData(0, 0, buffer.get(), size, NULL, false));
+ EXPECT_EQ(0, entry->WriteData(0, 0, buffer, 0, NULL, true));
+ EXPECT_EQ(size, entry->WriteData(0, 0, buffer, size, NULL, false));
entry->Close();
ASSERT_TRUE(cache_->OpenEntry(key2, &entry));
}
@@ -655,18 +692,21 @@ void DiskCacheEntryTest::InvalidData() {
disk_cache::Entry *entry1;
ASSERT_TRUE(cache_->CreateEntry(key1, &entry1));
- char buffer1[20000];
- char buffer2[20000];
- char buffer3[20000];
+ const int kSize1 = 20000;
+ const int kSize2 = 20000;
+ const int kSize3 = 20000;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize2);
+ scoped_refptr<net::IOBuffer> buffer3 = new net::IOBuffer(kSize3);
- CacheTestFillBuffer(buffer1, sizeof(buffer1), false);
- memset(buffer2, 0, sizeof(buffer2));
+ CacheTestFillBuffer(buffer1->data(), kSize1, false);
+ memset(buffer2->data(), 0, kSize2);
// Simple data grow:
EXPECT_EQ(200, entry1->WriteData(0, 400, buffer1, 200, NULL, false));
EXPECT_EQ(600, entry1->GetDataSize(0));
EXPECT_EQ(100, entry1->ReadData(0, 300, buffer3, 100, NULL));
- EXPECT_TRUE(!memcmp(buffer3, buffer2, 100));
+ EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 100));
entry1->Close();
ASSERT_TRUE(cache_->OpenEntry(key1, &entry1));
@@ -674,7 +714,7 @@ void DiskCacheEntryTest::InvalidData() {
EXPECT_EQ(200, entry1->WriteData(0, 800, buffer1, 200, NULL, false));
EXPECT_EQ(1000, entry1->GetDataSize(0));
EXPECT_EQ(100, entry1->ReadData(0, 700, buffer3, 100, NULL));
- EXPECT_TRUE(!memcmp(buffer3, buffer2, 100));
+ EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 100));
entry1->Close();
ASSERT_TRUE(cache_->OpenEntry(key1, &entry1));
@@ -682,31 +722,31 @@ void DiskCacheEntryTest::InvalidData() {
EXPECT_EQ(200, entry1->WriteData(0, 1800, buffer1, 200, NULL, true));
EXPECT_EQ(2000, entry1->GetDataSize(0));
EXPECT_EQ(100, entry1->ReadData(0, 1500, buffer3, 100, NULL));
- EXPECT_TRUE(!memcmp(buffer3, buffer2, 100));
+ EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 100));
// Go to an external file.
EXPECT_EQ(200, entry1->WriteData(0, 19800, buffer1, 200, NULL, false));
EXPECT_EQ(20000, entry1->GetDataSize(0));
EXPECT_EQ(4000, entry1->ReadData(0, 14000, buffer3, 4000, NULL));
- EXPECT_TRUE(!memcmp(buffer3, buffer2, 4000));
+ EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 4000));
// And back to an internal block.
EXPECT_EQ(600, entry1->WriteData(0, 1000, buffer1, 600, NULL, true));
EXPECT_EQ(1600, entry1->GetDataSize(0));
EXPECT_EQ(600, entry1->ReadData(0, 1000, buffer3, 600, NULL));
- EXPECT_TRUE(!memcmp(buffer3, buffer1, 600));
+ EXPECT_TRUE(!memcmp(buffer3->data(), buffer1->data(), 600));
// Extend it again.
EXPECT_EQ(600, entry1->WriteData(0, 2000, buffer1, 600, NULL, false));
EXPECT_EQ(2600, entry1->GetDataSize(0));
EXPECT_EQ(200, entry1->ReadData(0, 1800, buffer3, 200, NULL));
- EXPECT_TRUE(!memcmp(buffer3, buffer2, 200));
+ EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 200));
// And again (with truncation flag).
EXPECT_EQ(600, entry1->WriteData(0, 3000, buffer1, 600, NULL, true));
EXPECT_EQ(3600, entry1->GetDataSize(0));
EXPECT_EQ(200, entry1->ReadData(0, 2800, buffer3, 200, NULL));
- EXPECT_TRUE(!memcmp(buffer3, buffer2, 200));
+ EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 200));
entry1->Close();
}
@@ -729,14 +769,15 @@ void DiskCacheEntryTest::DoomEntry() {
entry1->Doom();
entry1->Close();
- char key_buffer[20000];
- CacheTestFillBuffer(key_buffer, sizeof(key_buffer), true);
- key_buffer[19999] = '\0';
+ const int kSize = 20000;
+ scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize);
+ CacheTestFillBuffer(buffer->data(), kSize, true);
+ buffer->data()[19999] = '\0';
- key1 = key_buffer;
+ key1 = buffer->data();
ASSERT_TRUE(cache_->CreateEntry(key1, &entry1));
- EXPECT_EQ(20000, entry1->WriteData(0, 0, key_buffer, 20000, NULL, false));
- EXPECT_EQ(20000, entry1->WriteData(1, 0, key_buffer, 20000, NULL, false));
+ EXPECT_EQ(20000, entry1->WriteData(0, 0, buffer, kSize, NULL, false));
+ EXPECT_EQ(20000, entry1->WriteData(1, 0, buffer, kSize, NULL, false));
entry1->Doom();
entry1->Close();
@@ -765,14 +806,16 @@ void DiskCacheEntryTest::DoomedEntry() {
Time initial = Time::Now();
PlatformThread::Sleep(20);
- char buffer1[2000];
- char buffer2[2000];
- CacheTestFillBuffer(buffer1, sizeof(buffer1), false);
- memset(buffer2, 0, sizeof(buffer2));
+ const int kSize1 = 2000;
+ const int kSize2 = 2000;
+ scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
+ scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize2);
+ CacheTestFillBuffer(buffer1->data(), kSize1, false);
+ memset(buffer2->data(), 0, kSize2);
EXPECT_EQ(2000, entry->WriteData(0, 0, buffer1, 2000, NULL, false));
EXPECT_EQ(2000, entry->ReadData(0, 0, buffer2, 2000, NULL));
- EXPECT_EQ(0, memcmp(buffer1, buffer2, sizeof(buffer1)));
+ EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize1));
EXPECT_TRUE(initial < entry->GetLastModified());
EXPECT_TRUE(initial < entry->GetLastUsed());
diff --git a/net/disk_cache/mem_entry_impl.cc b/net/disk_cache/mem_entry_impl.cc
index 6bc7b6b..99c53cd 100644
--- a/net/disk_cache/mem_entry_impl.cc
+++ b/net/disk_cache/mem_entry_impl.cc
@@ -4,6 +4,7 @@
#include "net/disk_cache/mem_entry_impl.h"
+#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/mem_backend_impl.h"
@@ -82,8 +83,8 @@ int32 MemEntryImpl::GetDataSize(int index) const {
return data_size_[index];
}
-int MemEntryImpl::ReadData(int index, int offset, char* buf, int buf_len,
- net::CompletionCallback* completion_callback) {
+int MemEntryImpl::ReadData(int index, int offset, net::IOBuffer* buf,
+ int buf_len, net::CompletionCallback* completion_callback) {
if (index < 0 || index >= NUM_STREAMS)
return net::ERR_INVALID_ARGUMENT;
@@ -99,13 +100,12 @@ int MemEntryImpl::ReadData(int index, int offset, char* buf, int buf_len,
UpdateRank(false);
- memcpy(buf , &(data_[index])[offset], buf_len);
+ memcpy(buf->data() , &(data_[index])[offset], buf_len);
return buf_len;
}
-int MemEntryImpl::WriteData(int index, int offset, const char* buf, int buf_len,
- net::CompletionCallback* completion_callback,
- bool truncate) {
+int MemEntryImpl::WriteData(int index, int offset, net::IOBuffer* buf,
+ int buf_len, net::CompletionCallback* completion_callback, bool truncate) {
if (index < 0 || index >= NUM_STREAMS)
return net::ERR_INVALID_ARGUMENT;
@@ -143,7 +143,7 @@ int MemEntryImpl::WriteData(int index, int offset, const char* buf, int buf_len,
if (!buf_len)
return 0;
- memcpy(&(data_[index])[offset], buf, buf_len);
+ memcpy(&(data_[index])[offset], buf->data(), buf_len);
return buf_len;
}
diff --git a/net/disk_cache/mem_entry_impl.h b/net/disk_cache/mem_entry_impl.h
index d6e44dd..cd8c6e7 100644
--- a/net/disk_cache/mem_entry_impl.h
+++ b/net/disk_cache/mem_entry_impl.h
@@ -24,9 +24,9 @@ class MemEntryImpl : public Entry {
virtual base::Time GetLastUsed() const;
virtual base::Time GetLastModified() const;
virtual int32 GetDataSize(int index) const;
- virtual int ReadData(int index, int offset, char* buf, int buf_len,
+ virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len,
net::CompletionCallback* completion_callback);
- virtual int WriteData(int index, int offset, const char* buf, int buf_len,
+ virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len,
net::CompletionCallback* completion_callback,
bool truncate);
diff --git a/net/disk_cache/stress_cache.cc b/net/disk_cache/stress_cache.cc
index a7fb144..e35a195 100644
--- a/net/disk_cache/stress_cache.cc
+++ b/net/disk_cache/stress_cache.cc
@@ -23,6 +23,7 @@
#include "base/process_util.h"
#include "base/string_util.h"
#include "base/thread.h"
+#include "net/base/io_buffer.h"
#include "net/disk_cache/disk_cache.h"
#include "net/disk_cache/disk_cache_test_util.h"
@@ -96,9 +97,9 @@ void StressTheCache(int iteration) {
keys[i] = GenerateKey(true);
}
- const int kDataLen = 4000;
- char data[kDataLen];
- memset(data, 'k', kDataLen);
+ const int kSize = 4000;
+ scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize);
+ memset(buffer->data(), 'k', kSize);
for (int i = 0;; i++) {
int slot = rand() % kNumEntries;
@@ -110,9 +111,8 @@ void StressTheCache(int iteration) {
if (!cache->OpenEntry(keys[key], &entries[slot]))
CHECK(cache->CreateEntry(keys[key], &entries[slot]));
- base::snprintf(data, kDataLen, "%d %d", iteration, i);
- CHECK(kDataLen == entries[slot]->WriteData(0, 0, data, kDataLen, NULL,
- false));
+ base::snprintf(buffer->data(), kSize, "%d %d", iteration, i);
+ CHECK(kSize == entries[slot]->WriteData(0, 0, buffer, kSize, NULL, false));
if (rand() % 100 > 80) {
key = rand() % kNumKeys;
@@ -188,7 +188,7 @@ int main(int argc, const char* argv[]) {
// Some time for the memory manager to flush stuff.
PlatformThread::Sleep(3000);
- MessageLoop message_loop;
+ MessageLoop message_loop(MessageLoop::TYPE_IO);
char* end;
long int iteration = strtol(argv[1], &end, 0);
diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc
index accd7da..f1132fc 100644
--- a/net/http/http_cache.cc
+++ b/net/http/http_cache.cc
@@ -12,6 +12,7 @@
#include "base/ref_counted.h"
#include "base/string_util.h"
#include "base/time.h"
+#include "net/base/io_buffer.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/disk_cache.h"
@@ -168,7 +169,6 @@ class HttpCache::Transaction : public HttpTransaction {
network_trans_(NULL),
callback_(NULL),
mode_(NONE),
- read_buf_(NULL),
read_offset_(0),
effective_load_flags_(0),
final_upload_progress_(0),
@@ -278,13 +278,13 @@ class HttpCache::Transaction : public HttpTransaction {
// Called to write data to the cache entry. If the write fails, then the
// cache entry is destroyed. Future calls to this function will just do
// nothing without side-effect.
- void WriteToEntry(int index, int offset, const char* data, int data_len);
+ void WriteToEntry(int index, int offset, IOBuffer* data, int data_len);
// Called to write response_ to the cache entry.
void WriteResponseInfoToEntry();
// Called to append response data to the cache entry.
- void AppendResponseDataToEntry(const char* data, int data_len);
+ void AppendResponseDataToEntry(IOBuffer* data, int data_len);
// Called when we are done writing to the cache entry.
void DoneWritingToEntry(bool success);
@@ -308,7 +308,7 @@ class HttpCache::Transaction : public HttpTransaction {
HttpResponseInfo auth_response_;
std::string cache_key_;
Mode mode_;
- char* read_buf_;
+ scoped_refptr<IOBuffer> read_buf_;
int read_offset_;
int effective_load_flags_;
uint64 final_upload_progress_;
@@ -435,23 +435,20 @@ int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
case WRITE:
DCHECK(network_trans_.get());
rv = network_trans_->Read(buf, buf_len, &network_read_callback_);
- read_buf_ = buf->data();
+ read_buf_ = buf;
if (rv >= 0)
OnNetworkReadCompleted(rv);
break;
case READ:
DCHECK(entry_);
cache_read_callback_->AddRef(); // Balanced in OnCacheReadCompleted.
- cache_read_callback_->UseBuffer(buf);
rv = entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
- buf->data(), buf_len,
- cache_read_callback_);
- read_buf_ = buf->data();
+ buf, buf_len, cache_read_callback_);
+ read_buf_ = buf;
if (rv >= 0) {
OnCacheReadCompleted(rv);
} else if (rv != ERR_IO_PENDING) {
cache_read_callback_->Release();
- cache_read_callback_->ReleaseBuffer();
}
break;
default:
@@ -777,7 +774,7 @@ int HttpCache::Transaction::ReadResponseInfoFromEntry() {
}
void HttpCache::Transaction::WriteToEntry(int index, int offset,
- const char* data, int data_len) {
+ IOBuffer* data, int data_len) {
if (!entry_)
return;
@@ -820,7 +817,7 @@ void HttpCache::Transaction::WriteResponseInfoToEntry() {
}
}
-void HttpCache::Transaction::AppendResponseDataToEntry(const char* data,
+void HttpCache::Transaction::AppendResponseDataToEntry(IOBuffer* data,
int data_len) {
if (!entry_)
return;
@@ -909,7 +906,6 @@ void HttpCache::Transaction::OnNetworkReadCompleted(int result) {
void HttpCache::Transaction::OnCacheReadCompleted(int result) {
DCHECK(cache_);
cache_read_callback_->Release(); // Balance the AddRef() from Start().
- cache_read_callback_->ReleaseBuffer();
if (result > 0) {
read_offset_ += result;
@@ -1001,16 +997,14 @@ bool HttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry,
HttpResponseInfo* response_info) {
int size = disk_entry->GetDataSize(kResponseInfoIndex);
- std::string data;
- int rv = disk_entry->ReadData(kResponseInfoIndex, 0,
- WriteInto(&data, size + 1),
- size, NULL);
+ scoped_refptr<IOBuffer> buffer = new IOBuffer(size);
+ int rv = disk_entry->ReadData(kResponseInfoIndex, 0, buffer, size, NULL);
if (rv != size) {
DLOG(ERROR) << "ReadData failed: " << rv;
return false;
}
- Pickle pickle(data.data(), static_cast<int>(data.size()));
+ Pickle pickle(buffer->data(), size);
void* iter = NULL;
// read flags and verify version
@@ -1108,7 +1102,8 @@ bool HttpCache::WriteResponseInfo(disk_cache::Entry* disk_entry,
if (response_info->vary_data.is_valid())
response_info->vary_data.Persist(&pickle);
- const char* data = static_cast<const char*>(pickle.data());
+ scoped_refptr<WrappedIOBuffer> data = new WrappedIOBuffer(
+ reinterpret_cast<const char*>(pickle.data()));
int len = static_cast<int>(pickle.size());
return disk_entry->WriteData(kResponseInfoIndex, 0, data, len, NULL,
diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc
index 0a71854..91bec6e 100644
--- a/net/http/http_cache_unittest.cc
+++ b/net/http/http_cache_unittest.cc
@@ -65,7 +65,7 @@ class MockDiskEntry : public disk_cache::Entry,
return static_cast<int32>(data_[index].size());
}
- virtual int ReadData(int index, int offset, char* buf, int buf_len,
+ virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len,
net::CompletionCallback* callback) {
DCHECK(index >= 0 && index < 2);
@@ -75,7 +75,7 @@ class MockDiskEntry : public disk_cache::Entry,
return 0;
int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset);
- memcpy(buf, &data_[index][offset], num);
+ memcpy(buf->data(), &data_[index][offset], num);
if (!callback || (test_mode_ & TEST_MODE_SYNC_CACHE_READ))
return num;
@@ -84,7 +84,7 @@ class MockDiskEntry : public disk_cache::Entry,
return net::ERR_IO_PENDING;
}
- virtual int WriteData(int index, int offset, const char* buf, int buf_len,
+ virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len,
net::CompletionCallback* callback, bool truncate) {
DCHECK(index >= 0 && index < 2);
DCHECK(truncate);
@@ -94,7 +94,7 @@ class MockDiskEntry : public disk_cache::Entry,
data_[index].resize(offset + buf_len);
if (buf_len)
- memcpy(&data_[index][offset], buf, buf_len);
+ memcpy(&data_[index][offset], buf->data(), buf_len);
return buf_len;
}
diff --git a/net/http/http_network_transaction.cc b/net/http/http_network_transaction.cc
index 9667cb0..fb9489c 100644
--- a/net/http/http_network_transaction.cc
+++ b/net/http/http_network_transaction.cc
@@ -13,6 +13,7 @@
#include "net/base/connection_type_histograms.h"
#include "net/base/dns_resolution_observer.h"
#include "net/base/host_resolver.h"
+#include "net/base/io_buffer.h"
#include "net/base/load_flags.h"
#include "net/base/net_util.h"
#include "net/base/ssl_client_socket.h"
diff --git a/net/http/http_transaction.h b/net/http/http_transaction.h
index 46c7671..ffb25c6 100644
--- a/net/http/http_transaction.h
+++ b/net/http/http_transaction.h
@@ -12,6 +12,7 @@ namespace net {
class HttpRequestInfo;
class HttpResponseInfo;
+class IOBuffer;
// Represents a single HTTP transaction (i.e., a single request/response pair).
// HTTP redirects are not followed and authentication challenges are not
diff --git a/net/http/http_transaction_unittest.h b/net/http/http_transaction_unittest.h
index 0c26178..9ef6bf8 100644
--- a/net/http/http_transaction_unittest.h
+++ b/net/http/http_transaction_unittest.h
@@ -13,6 +13,7 @@
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/string_util.h"
+#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/load_flags.h"
#include "net/base/test_completion_callback.h"
diff --git a/net/tools/dump_cache/upgrade.cc b/net/tools/dump_cache/upgrade.cc
index d048c6f..6b4a2da 100644
--- a/net/tools/dump_cache/upgrade.cc
+++ b/net/tools/dump_cache/upgrade.cc
@@ -5,6 +5,7 @@
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/string_util.h"
+#include "net/base/io_buffer.h"
#include "net/disk_cache/backend_impl.h"
#include "net/disk_cache/entry_impl.h"
@@ -444,8 +445,10 @@ void MasterSM::DoReadData(int bytes_read) {
return SendReadData();
}
- if (read_size != entry_->WriteData(stream_, offset_, input_->buffer,
- read_size, NULL, false))
+ scoped_refptr<net::WrappedIOBuffer> buf =
+ new net::WrappedIOBuffer(input_->buffer);
+ if (read_size != entry_->WriteData(stream_, offset_, buf, read_size, NULL,
+ false))
return Fail();
offset_ += read_size;
@@ -713,8 +716,9 @@ void SlaveSM::DoReadData() {
stream < 0 || stream > 1 || size > kBufferSize) {
msg.result = RESULT_INVALID_PARAMETER;
} else {
- int ret = entry_->ReadData(stream, input_->msg.arg3, output_->buffer, size,
- NULL);
+ scoped_refptr<net::WrappedIOBuffer> buf =
+ new net::WrappedIOBuffer(output_->buffer);
+ int ret = entry_->ReadData(stream, input_->msg.arg3, buf, size, NULL);
msg.buffer_bytes = (ret < 0) ? 0 : ret;
msg.result = RESULT_OK;
diff --git a/net/url_request/url_request_view_cache_job.cc b/net/url_request/url_request_view_cache_job.cc
index 5a4a23c..0cd4efd 100644
--- a/net/url_request/url_request_view_cache_job.cc
+++ b/net/url_request/url_request_view_cache_job.cc
@@ -87,9 +87,9 @@ static std::string FormatEntryDetails(disk_cache::Entry* entry) {
int data_size = entry->GetDataSize(i);
- char* data = new char[data_size];
- if (entry->ReadData(i, 0, data, data_size, NULL) == data_size)
- HexDump(data, data_size, &result);
+ scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(data_size);
+ if (entry->ReadData(i, 0, buffer, data_size, NULL) == data_size)
+ HexDump(buffer->data(), data_size, &result);
result.append("</pre>");
}