Index: net/tools/dump_cache/upgrade.cc |
=================================================================== |
--- net/tools/dump_cache/upgrade.cc (revision 49603) |
+++ net/tools/dump_cache/upgrade.cc (working copy) |
@@ -7,8 +7,10 @@ |
#include "base/message_loop.h" |
#include "base/scoped_ptr.h" |
#include "base/string_util.h" |
+#include "base/thread.h" |
#include "googleurl/src/gurl.h" |
#include "net/base/io_buffer.h" |
+#include "net/base/test_completion_callback.h" |
#include "net/disk_cache/backend_impl.h" |
#include "net/disk_cache/entry_impl.h" |
#include "net/http/http_cache.h" |
@@ -109,7 +111,7 @@ |
class BaseSM : public MessageLoopForIO::IOHandler { |
public: |
- BaseSM(HANDLE channel); |
+ explicit BaseSM(HANDLE channel); |
virtual ~BaseSM(); |
protected: |
@@ -128,11 +130,14 @@ |
scoped_array<char> out_buffer_; |
IoBuffer* input_; |
IoBuffer* output_; |
+ base::Thread cache_thread_; |
+ |
DISALLOW_COPY_AND_ASSIGN(BaseSM); |
}; |
BaseSM::BaseSM(HANDLE channel) |
- : entry_(NULL), channel_(channel), state_(0), pending_count_(0) { |
+ : entry_(NULL), channel_(channel), state_(0), pending_count_(0), |
+ cache_thread_("cache") { |
in_buffer_.reset(new char[kChannelSize]); |
out_buffer_.reset(new char[kChannelSize]); |
input_ = reinterpret_cast<IoBuffer*>(in_buffer_.get()); |
@@ -143,6 +148,8 @@ |
in_context_.handler = this; |
out_context_.handler = this; |
MessageLoopForIO::current()->RegisterIOHandler(channel_, this); |
+ CHECK(cache_thread_.StartWithOptions( |
+ base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
} |
BaseSM::~BaseSM() { |
@@ -200,8 +207,10 @@ |
class MasterSM : public BaseSM { |
public: |
- MasterSM(const std::wstring& path, HANDLE channel, bool dump_to_disk) |
- : BaseSM(channel), path_(path), dump_to_disk_(dump_to_disk) { |
+ MasterSM(const std::wstring& path, HANDLE channel, bool dump_to_disk) |
+ : BaseSM(channel), path_(path), dump_to_disk_(dump_to_disk), |
+ ALLOW_THIS_IN_INITIALIZER_LIST( |
+ write_callback_(this, &MasterSM::DoReadDataComplete)) { |
} |
virtual ~MasterSM() { |
delete writer_; |
@@ -233,6 +242,7 @@ |
void CloseEntry(); |
void SendReadData(); |
void DoReadData(int bytes_read); |
+ void DoReadDataComplete(int ret); |
void SendQuit(); |
void DoEnd(); |
void Fail(); |
@@ -244,10 +254,12 @@ |
int bytes_remaining_; |
int offset_; |
int copied_entries_; |
- scoped_ptr<disk_cache::BackendImpl> cache_; |
+ int read_size_; |
+ scoped_ptr<disk_cache::Backend> cache_; |
CacheDumpWriter* writer_; |
const std::wstring& path_; |
bool dump_to_disk_; |
+ net::CompletionCallbackImpl<MasterSM> write_callback_; |
}; |
void MasterSM::OnIOCompleted(MessageLoopForIO::IOContext* context, |
@@ -302,11 +314,18 @@ |
if (dump_to_disk_) { |
writer_ = new DiskDumper(path_); |
} else { |
- cache_.reset(new disk_cache::BackendImpl(FilePath::FromWStringHack(path_))); |
- if (!cache_->Init()) { |
+ disk_cache::Backend* cache; |
+ TestCompletionCallback cb; |
+ int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, |
+ FilePath::FromWStringHack(path_), 0, |
+ false, |
+ cache_thread_.message_loop_proxy(), |
+ &cache, &cb); |
+ if (cb.GetResult(rv) != net::OK) { |
printf("Unable to initialize new files\n"); |
return false; |
} |
+ cache_.reset(cache); |
writer_ = new CacheDumper(cache_.get()); |
} |
if (!writer_) |
@@ -474,7 +493,15 @@ |
scoped_refptr<net::WrappedIOBuffer> buf = |
new net::WrappedIOBuffer(input_->buffer); |
- if (!writer_->WriteEntry(entry_, stream_, offset_, buf, read_size)) |
+ int rv = writer_->WriteEntry(entry_, stream_, offset_, buf, read_size, |
+ &write_callback_); |
+ if (rv == net::ERR_IO_PENDING) { |
+ // We'll continue in DoReadDataComplete. |
+ read_size_ = read_size; |
+ return; |
+ } |
+ |
+ if (rv <= 0) |
return Fail(); |
offset_ += read_size; |
@@ -483,6 +510,16 @@ |
SendReadData(); |
} |
+void MasterSM::DoReadDataComplete(int ret) { |
+ if (ret != read_size_) |
+ return Fail(); |
+ |
+ offset_ += ret; |
+ bytes_remaining_ -= ret; |
+ // Read some more. |
+ SendReadData(); |
+} |
+ |
void MasterSM::SendQuit() { |
DEBUGMSG("Master SendQuit\n"); |
state_ = MASTER_END; |
@@ -508,15 +545,7 @@ |
class SlaveSM : public BaseSM { |
public: |
- SlaveSM(const std::wstring& path, HANDLE channel) |
- : BaseSM(channel), iterator_(NULL) { |
- cache_.reset(new disk_cache::BackendImpl(FilePath::FromWStringHack(path))); |
- if (!cache_->Init()) { |
- printf("Unable to open cache files\n"); |
- return; |
- } |
- cache_->SetUpgradeMode(); |
- } |
+ SlaveSM(const std::wstring& path, HANDLE channel); |
virtual ~SlaveSM(); |
bool DoInit(); |
@@ -538,14 +567,36 @@ |
void DoGetUseTimes(); |
void DoGetDataSize(); |
void DoReadData(); |
+ void DoReadDataComplete(int ret); |
void DoEnd(); |
void Fail(); |
void* iterator_; |
+ Message msg_; // Only used for DoReadDataComplete. |
+ net::CompletionCallbackImpl<SlaveSM> read_callback_; |
scoped_ptr<disk_cache::BackendImpl> cache_; |
}; |
+SlaveSM::SlaveSM(const std::wstring& path, HANDLE channel) |
+ : BaseSM(channel), iterator_(NULL), |
+ ALLOW_THIS_IN_INITIALIZER_LIST( |
+ read_callback_(this, &SlaveSM::DoReadDataComplete)) { |
+ disk_cache::Backend* cache; |
+ TestCompletionCallback cb; |
+ int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, |
+ FilePath::FromWStringHack(path), 0, |
+ false, |
+ cache_thread_.message_loop_proxy(), |
+ &cache, &cb); |
+ if (cb.GetResult(rv) != net::OK) { |
+ printf("Unable to open cache files\n"); |
+ return; |
+ } |
+ cache_.reset(reinterpret_cast<disk_cache::BackendImpl*>(cache)); |
+ cache_->SetUpgradeMode(); |
+} |
+ |
SlaveSM::~SlaveSM() { |
if (iterator_) |
cache_->EndEnumeration(&iterator_); |
@@ -753,7 +804,13 @@ |
} else { |
scoped_refptr<net::WrappedIOBuffer> buf = |
new net::WrappedIOBuffer(output_->buffer); |
- int ret = entry_->ReadData(stream, input_->msg.arg3, buf, size, NULL); |
+ int ret = entry_->ReadData(stream, input_->msg.arg3, buf, size, |
+ &read_callback_); |
+ if (ret == net::ERR_IO_PENDING) { |
+ // Save the message so we can continue were we left. |
+ msg_ = msg; |
+ return; |
+ } |
msg.buffer_bytes = (ret < 0) ? 0 : ret; |
msg.result = RESULT_OK; |
@@ -761,6 +818,14 @@ |
SendMsg(msg); |
} |
+void SlaveSM::DoReadDataComplete(int ret) { |
+ DEBUGMSG("\t\t\tSlave DoReadDataComplete\n"); |
+ DCHECK_EQ(READ_DATA, msg_.command); |
+ msg_.buffer_bytes = (ret < 0) ? 0 : ret; |
+ msg_.result = RESULT_OK; |
+ SendMsg(msg_); |
+} |
+ |
void SlaveSM::DoEnd() { |
DEBUGMSG("\t\t\tSlave DoEnd\n"); |
MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |