Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1461)

Side by Side Diff: net/tools/dump_cache/simple_cache_dumper.cc

Issue 11299239: Adding a simple class for dumping the contents of a cache. Use this new class in dump_cache. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/tools/dump_cache/simple_cache_dumper.h ('k') | net/tools/dump_cache/upgrade.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/tools/dump_cache/simple_cache_dumper.h"
6
7 #include "base/at_exit.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h"
12 #include "base/message_loop_proxy.h"
13 #include "base/threading/thread.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/net_errors.h"
16 #include "net/disk_cache/disk_cache.h"
17 #include "net/tools/dump_cache/cache_dumper.h"
18
19 namespace net {
20
21 SimpleCacheDumper::SimpleCacheDumper(FilePath input_path, FilePath output_path)
22 : state_(STATE_NONE),
23 input_path_(input_path),
24 output_path_(output_path),
25 cache_(NULL),
26 writer_(new DiskDumper(output_path)),
27 cache_thread_(new base::Thread("CacheThead")),
28 iter_(NULL),
29 src_entry_(NULL),
30 dst_entry_(NULL),
31 io_callback_(base::Bind(&SimpleCacheDumper::OnIOComplete,
32 base::Unretained(this))),
33 rv_(0) {
34 }
35
36 SimpleCacheDumper::~SimpleCacheDumper() {
37 delete(cache_);
38 }
39
40 int SimpleCacheDumper::Run() {
41 MessageLoopForIO main_message_loop;
42
43 LOG(INFO) << "Reading cache from: " << input_path_.value();
44 LOG(INFO) << "Writing cache to: " << output_path_.value();
45
46 if (!cache_thread_->StartWithOptions(
47 base::Thread::Options(MessageLoop::TYPE_IO, 0))) {
48 LOG(ERROR) << "Unable to start thread";
49 return ERR_UNEXPECTED;
50 }
51 state_ = STATE_CREATE_CACHE;
52 int rv = DoLoop(OK);
53 if (rv == ERR_IO_PENDING) {
54 main_message_loop.Run();
55 return rv_;
56 }
57 return rv;
58 }
59
60 int SimpleCacheDumper::DoLoop(int rv) {
61 do {
62 State state = state_;
63 state_ = STATE_NONE;
64 switch (state) {
65 case STATE_CREATE_CACHE:
66 CHECK_EQ(OK, rv);
67 rv = DoCreateCache();
68 break;
69 case STATE_CREATE_CACHE_COMPLETE:
70 rv = DoCreateCacheComplete(rv);
71 break;
72 case STATE_OPEN_ENTRY:
73 CHECK_EQ(OK, rv);
74 rv = DoOpenEntry();
75 break;
76 case STATE_OPEN_ENTRY_COMPLETE:
77 rv = DoOpenEntryComplete(rv);
78 break;
79 case STATE_CREATE_ENTRY:
80 CHECK_EQ(OK, rv);
81 rv = DoCreateEntry();
82 break;
83 case STATE_CREATE_ENTRY_COMPLETE:
84 rv = DoCreateEntryComplete(rv);
85 break;
86 case STATE_READ_HEADERS:
87 CHECK_EQ(OK, rv);
88 rv = DoReadHeaders();
89 break;
90 case STATE_READ_HEADERS_COMPLETE:
91 rv = DoReadHeadersComplete(rv);
92 break;
93 case STATE_WRITE_HEADERS:
94 CHECK_EQ(OK, rv);
95 rv = DoWriteHeaders();
96 break;
97 case STATE_WRITE_HEADERS_COMPLETE:
98 rv = DoWriteHeadersComplete(rv);
99 break;
100 case STATE_READ_BODY:
101 CHECK_EQ(OK, rv);
102 rv = DoReadBody();
103 break;
104 case STATE_READ_BODY_COMPLETE:
105 rv = DoReadBodyComplete(rv);
106 break;
107 case STATE_WRITE_BODY:
108 CHECK_EQ(OK, rv);
109 rv = DoWriteBody();
110 break;
111 case STATE_WRITE_BODY_COMPLETE:
112 rv = DoWriteBodyComplete(rv);
113 break;
114 default:
115 NOTREACHED() << "state_: " << state_;
116 break;
117 }
118 } while (state_ != STATE_NONE && rv != ERR_IO_PENDING);
119 return rv;
120 }
121
122 int SimpleCacheDumper::DoCreateCache() {
123 DCHECK(!cache_);
124 state_ = STATE_CREATE_CACHE_COMPLETE;
125 return disk_cache::CreateCacheBackend(
126 DISK_CACHE, input_path_, 0, false,
127 cache_thread_->message_loop_proxy(),
128 NULL, &cache_, io_callback_);
129 }
130
131 int SimpleCacheDumper::DoCreateCacheComplete(int rv) {
132 if (rv < 0)
133 return rv;
134
135 reinterpret_cast<disk_cache::BackendImpl*>(cache_)->SetUpgradeMode();
136 reinterpret_cast<disk_cache::BackendImpl*>(cache_)->SetFlags(
137 disk_cache::kNoRandom);
138
139 state_ = STATE_OPEN_ENTRY;
140 return OK;
141 }
142
143 int SimpleCacheDumper::DoOpenEntry() {
144 DCHECK(!dst_entry_);
145 DCHECK(!src_entry_);
146 state_ = STATE_OPEN_ENTRY_COMPLETE;
147 return cache_->OpenNextEntry(&iter_, &src_entry_, io_callback_);
148 }
149
150 int SimpleCacheDumper::DoOpenEntryComplete(int rv) {
151 // ERR_FAILED indicates iteration finished.
152 if (rv == ERR_FAILED) {
153 cache_->EndEnumeration(&iter_);
154 return OK;
155 }
156
157 if (rv < 0)
158 return rv;
159
160 state_ = STATE_CREATE_ENTRY;
161 return OK;
162 }
163
164 int SimpleCacheDumper::DoCreateEntry() {
165 DCHECK(!dst_entry_);
166 state_ = STATE_CREATE_ENTRY_COMPLETE;
167
168 return writer_->CreateEntry(src_entry_->GetKey(), &dst_entry_,
169 io_callback_);
170 }
171
172 int SimpleCacheDumper::DoCreateEntryComplete(int rv) {
173 if (rv < 0)
174 return rv;
175
176 state_ = STATE_READ_HEADERS;
177 return OK;
178 }
179
180 int SimpleCacheDumper::DoReadHeaders() {
181 state_ = STATE_READ_HEADERS_COMPLETE;
182 int32 size = src_entry_->GetDataSize(0);
183 buf_ = new IOBufferWithSize(size);
184 return src_entry_->ReadData(0, 0, buf_, size, io_callback_);
185 }
186
187 int SimpleCacheDumper::DoReadHeadersComplete(int rv) {
188 if (rv < 0)
189 return rv;
190
191 state_ = STATE_WRITE_HEADERS;
192 return OK;
193 }
194
195 int SimpleCacheDumper::DoWriteHeaders() {
196 int rv = writer_->WriteEntry(dst_entry_, 0, 0, buf_, buf_->size(),
197 io_callback_);
198 if (rv == 0)
199 return ERR_FAILED;
200
201 state_ = STATE_WRITE_HEADERS_COMPLETE;
202 return OK;
203 }
204
205 int SimpleCacheDumper::DoWriteHeadersComplete(int rv) {
206 if (rv < 0)
207 return rv;
208
209 state_ = STATE_READ_BODY;
210 return OK;
211 }
212
213 int SimpleCacheDumper::DoReadBody() {
214 state_ = STATE_READ_BODY_COMPLETE;
215 int32 size = src_entry_->GetDataSize(1);
216 // If the body is empty, we can neither read nor write it, so
217 // just move to the next.
218 if (size <= 0) {
219 state_ = STATE_WRITE_BODY_COMPLETE;
220 return OK;
221 }
222 buf_ = new IOBufferWithSize(size);
223 return src_entry_->ReadData(1, 0, buf_, size, io_callback_);
224 }
225
226 int SimpleCacheDumper::DoReadBodyComplete(int rv) {
227 if (rv < 0)
228 return rv;
229
230 state_ = STATE_WRITE_BODY;
231 return OK;
232 }
233
234 int SimpleCacheDumper::DoWriteBody() {
235 int rv = writer_->WriteEntry(dst_entry_, 1, 0, buf_, buf_->size(),
236 io_callback_);
237 if (rv == 0)
238 return ERR_FAILED;
239
240 state_ = STATE_WRITE_BODY_COMPLETE;
241 return OK;
242 }
243
244 int SimpleCacheDumper::DoWriteBodyComplete(int rv) {
245 if (rv < 0)
246 return rv;
247
248 src_entry_->Close();
249 writer_->CloseEntry(dst_entry_, base::Time::Now(), base::Time::Now());
250 src_entry_ = NULL;
251 dst_entry_ = NULL;
252
253 state_ = STATE_OPEN_ENTRY;
254 return OK;
255 }
256
257 void SimpleCacheDumper::OnIOComplete(int rv) {
258 rv = DoLoop(rv);
259
260 if (rv != ERR_IO_PENDING) {
261 rv_ = rv;
262 delete cache_;
263 cache_ = NULL;
264 MessageLoop::current()->Quit();
265 }
266 }
267
268 } // namespace net
OLDNEW
« no previous file with comments | « net/tools/dump_cache/simple_cache_dumper.h ('k') | net/tools/dump_cache/upgrade.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698