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_CACHE_DUMPER_H_
6#define NET_TOOLS_DUMP_CACHE_CACHE_DUMPER_H_
7
8#include <string>
9
10#include "base/files/file_path.h"
11#include "net/disk_cache/backend_impl.h"
12
13#ifdef WIN32
14// Dumping the cache often creates very large filenames, which are tricky
15// on windows.  Most API calls don't support large filenames, including
16// most of the base library functions.  Unfortunately, adding "\\?\" into
17// the filename support is tricky.  Instead, if WIN32_LARGE_FILENAME_SUPPORT
18// is set, we use direct WIN32 APIs for manipulating the files.
19#define WIN32_LARGE_FILENAME_SUPPORT
20#endif
21
22// An abstract class for writing cache dump data.
23class CacheDumpWriter {
24 public:
25  virtual ~CacheDumpWriter() {}
26
27  // Creates an entry to be written.
28  // On success, populates the |entry|.
29  // Returns a net error code.
30  virtual int CreateEntry(const std::string& key,
31                          disk_cache::Entry** entry,
32                          const net::CompletionCallback& callback) = 0;
33
34  // Write to the current entry.
35  // Returns a net error code.
36  virtual int WriteEntry(disk_cache::Entry* entry, int stream, int offset,
37                         net::IOBuffer* buf, int buf_len,
38                         const net::CompletionCallback& callback) = 0;
39
40  // Close the current entry.
41  virtual void CloseEntry(disk_cache::Entry* entry, base::Time last_used,
42                          base::Time last_modified) = 0;
43};
44
45// Writes data to a cache.
46class CacheDumper : public CacheDumpWriter {
47 public:
48  explicit CacheDumper(disk_cache::Backend* cache);
49
50  virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry,
51                          const net::CompletionCallback& callback) OVERRIDE;
52  virtual int WriteEntry(disk_cache::Entry* entry, int stream, int offset,
53                         net::IOBuffer* buf, int buf_len,
54                         const net::CompletionCallback& callback) OVERRIDE;
55  virtual void CloseEntry(disk_cache::Entry* entry, base::Time last_used,
56                          base::Time last_modified) OVERRIDE;
57
58 private:
59  disk_cache::Backend* cache_;
60};
61
62// Writes data to a disk.
63class DiskDumper : public CacheDumpWriter {
64 public:
65  explicit DiskDumper(const base::FilePath& path);
66
67  virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry,
68                          const net::CompletionCallback& callback) OVERRIDE;
69  virtual int WriteEntry(disk_cache::Entry* entry, int stream, int offset,
70                         net::IOBuffer* buf, int buf_len,
71                         const net::CompletionCallback& callback) OVERRIDE;
72  virtual void CloseEntry(disk_cache::Entry* entry, base::Time last_used,
73                          base::Time last_modified) OVERRIDE;
74
75 private:
76  base::FilePath path_;
77  // This is a bit of a hack.  As we get a CreateEntry, we coin the current
78  // entry_path_ where we write that entry to disk.  Subsequent calls to
79  // WriteEntry() utilize this path for writing to disk.
80  base::FilePath entry_path_;
81  std::string entry_url_;
82#ifdef WIN32_LARGE_FILENAME_SUPPORT
83  HANDLE entry_;
84#else
85  FILE* entry_;
86#endif
87};
88
89#endif  // NET_TOOLS_DUMP_CACHE_CACHE_DUMPER_H_
90