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#ifndef NET_TOOLS_DUMP_CACHE_SIMPLE_CACHE_DUMPER_H_
6#define NET_TOOLS_DUMP_CACHE_SIMPLE_CACHE_DUMPER_H_
7
8#include "base/files/file_path.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/threading/thread.h"
11#include "net/base/completion_callback.h"
12#include "net/disk_cache/disk_cache.h"
13
14class DiskDumper;
15
16namespace net {
17
18class IOBufferWithSize;
19
20// A class for dumping the contents of a disk cache to a series of text
21// files.  The files will contain the response headers, followed by the
22// response body, as if the HTTP response were written directly to disk.
23class SimpleCacheDumper {
24 public:
25  SimpleCacheDumper(base::FilePath input_path, base::FilePath output_path);
26  ~SimpleCacheDumper();
27
28  // Dumps the cache to disk. Returns OK if the operation was successful,
29  // and a net error code otherwise.
30  int Run();
31
32 private:
33  enum State {
34    STATE_NONE,
35    STATE_CREATE_CACHE,
36    STATE_CREATE_CACHE_COMPLETE,
37    STATE_OPEN_ENTRY,
38    STATE_OPEN_ENTRY_COMPLETE,
39    STATE_CREATE_ENTRY,
40    STATE_CREATE_ENTRY_COMPLETE,
41    STATE_READ_HEADERS,
42    STATE_READ_HEADERS_COMPLETE,
43    STATE_WRITE_HEADERS,
44    STATE_WRITE_HEADERS_COMPLETE,
45    STATE_READ_BODY,
46    STATE_READ_BODY_COMPLETE,
47    STATE_WRITE_BODY,
48    STATE_WRITE_BODY_COMPLETE,
49    STATE_DONE,
50  };
51
52  int DoLoop(int rv);
53
54  int DoCreateCache();
55  int DoCreateCacheComplete(int rv);
56  int DoOpenEntry();
57  int DoOpenEntryComplete(int rv);
58  int DoCreateEntry();
59  int DoCreateEntryComplete(int rv);
60  int DoReadHeaders();
61  int DoReadHeadersComplete(int rv);
62  int DoWriteHeaders();
63  int DoWriteHeadersComplete(int rv);
64  int DoReadBody();
65  int DoReadBodyComplete(int rv);
66  int DoWriteBody();
67  int DoWriteBodyComplete(int rv);
68  int DoDone();
69
70  void OnIOComplete(int rv);
71
72  State state_;
73  base::FilePath input_path_;
74  base::FilePath output_path_;
75  scoped_ptr<disk_cache::Backend> cache_;
76  scoped_ptr<DiskDumper> writer_;
77  base::Thread* cache_thread_;
78  scoped_ptr<disk_cache::Backend::Iterator> iter_;
79  disk_cache::Entry* src_entry_;
80  disk_cache::Entry* dst_entry_;
81  CompletionCallback io_callback_;
82  scoped_refptr<IOBufferWithSize> buf_;
83  int rv_;
84
85  DISALLOW_COPY_AND_ASSIGN(SimpleCacheDumper);
86};
87
88}  // namespace net
89
90#endif  // NET_TOOLS_DUMP_CACHE_SIMPLE_CACHE_DUMPER_H_
91