summaryrefslogtreecommitdiffstats
path: root/net/disk_cache/simple/simple_synchronous_entry.h
blob: bd05ac71bfc38650e6a7e303d3f4b9af93638e62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_
#define NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_

#include <algorithm>
#include <string>
#include <utility>
#include <vector>

#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/platform_file.h"
#include "base/time/time.h"
#include "net/disk_cache/simple/simple_entry_format.h"

namespace net {
class IOBuffer;
}

namespace disk_cache {

// Worker thread interface to the very simple cache. This interface is not
// thread safe, and callers must ensure that it is only ever accessed from
// a single thread between synchronization points.
class SimpleSynchronousEntry {
 public:
  struct CRCRecord {
    CRCRecord();
    CRCRecord(int index_p, bool has_crc32_p, uint32 data_crc32_p);

    int index;
    bool has_crc32;
    uint32 data_crc32;
  };

  static void OpenEntry(
      const base::FilePath& path,
      uint64 entry_hash,
      SimpleSynchronousEntry** out_entry,
      int* out_result);

  static void CreateEntry(
      const base::FilePath& path,
      const std::string& key,
      uint64 entry_hash,
      SimpleSynchronousEntry** out_entry,
      int* out_result);

  // Deletes an entry without first Opening it. Does not check if there is
  // already an Entry object in memory holding the open files. Be careful! This
  // is meant to be used by the Backend::DoomEntry() call. |callback| will be
  // run by |callback_runner|.
  static void DoomEntry(const base::FilePath& path,
                        const std::string& key,
                        uint64 entry_hash,
                        int* out_result);

  // Like |DoomEntry()| above. Deletes all entries corresponding to the
  // |key_hashes|. Succeeds only when all entries are deleted.
  static void DoomEntrySet(scoped_ptr<std::vector<uint64> > key_hashes,
                           const base::FilePath& path,
                           int* out_result);

  // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO.
  void ReadData(int index,
                int offset,
                net::IOBuffer* buf,
                int buf_len,
                uint32* out_crc32,
                int* out_result);
  void WriteData(int index,
                 int offset,
                 net::IOBuffer* buf,
                 int buf_len,
                 bool truncate,
                 int* out_result);
  void CheckEOFRecord(int index,
                      uint32 expected_crc32,
                      int* out_result);

  // Close all streams, and add write EOF records to streams indicated by the
  // CRCRecord entries in |crc32s_to_write|.
  void Close(scoped_ptr<std::vector<CRCRecord> > crc32s_to_write);

  const base::FilePath& path() const { return path_; }
  std::string key() const { return key_; }
  base::Time last_used() const { return last_used_; }
  base::Time last_modified() const { return last_modified_; }
  int32 data_size(int index) const { return data_size_[index]; }

  int64 GetFileSize() const;

 private:
  SimpleSynchronousEntry(
      const base::FilePath& path,
      const std::string& key,
      uint64 entry_hash);

  // Like Entry, the SimpleSynchronousEntry self releases when Close() is
  // called.
  ~SimpleSynchronousEntry();

  bool OpenOrCreateFiles(bool create);
  void CloseFiles();

  // Returns a net::Error, i.e. net::OK on success.
  int InitializeForOpen();

  // Returns a net::Error, including net::OK on success and net::FILE_EXISTS
  // when the entry already exists.
  int InitializeForCreate();

  void Doom();

  static bool DeleteFilesForEntryHash(const base::FilePath& path,
                                      uint64 entry_hash);

  const base::FilePath path_;
  const uint64 entry_hash_;
  std::string key_;

  bool have_open_files_;
  bool initialized_;

  base::Time last_used_;
  base::Time last_modified_;
  int32 data_size_[kSimpleEntryFileCount];

  base::PlatformFile files_[kSimpleEntryFileCount];
};

}  // namespace disk_cache

#endif  // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_