Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / disk_cache / blockfile / entry_impl_v3.h
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_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_V3_H_
6 #define NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_V3_H_
7
8 #include <string>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "net/base/net_log.h"
12 #include "net/disk_cache/blockfile/disk_format_v3.h"
13 #include "net/disk_cache/blockfile/storage_block.h"
14 #include "net/disk_cache/disk_cache.h"
15
16 namespace disk_cache {
17
18 class BackendImplV3;
19 class SparseControlV3;
20
21 // This class implements the Entry interface. An object of this
22 // class represents a single entry on the cache.
23 class NET_EXPORT_PRIVATE EntryImplV3
24     : public Entry,
25       public base::RefCounted<EntryImplV3> {
26   friend class base::RefCounted<EntryImplV3>;
27   // friend class SparseControlV3;
28  public:
29   enum Operation {
30     kRead,
31     kWrite,
32     kSparseRead,
33     kSparseWrite,
34     kAsyncIO,
35     kReadAsync1,
36     kWriteAsync1
37   };
38
39   EntryImplV3(BackendImplV3* backend, Addr address, bool read_only);
40
41   // Performs the initialization of a EntryImplV3 that will be added to the
42   // cache.
43   bool CreateEntry(Addr node_address, const std::string& key, uint32 hash);
44
45   uint32 GetHash();
46
47   uint32 GetHash() const;
48   Addr GetAddress() const;
49   int GetReuseCounter() const;
50   void SetReuseCounter(int count);
51   int GetRefetchCounter() const;
52   void SetRefetchCounter(int count);
53
54   // Returns true if this entry matches the lookup arguments.
55   bool IsSameEntry(const std::string& key, uint32 hash);
56
57   // Permamently destroys this entry.
58   void InternalDoom();
59
60   // Returns false if the entry is clearly invalid.
61   bool SanityCheck();
62   bool DataSanityCheck();
63
64   // Attempts to make this entry reachable though the key.
65   void FixForDelete();
66
67   // Set the access times for this entry. This method provides support for
68   // the upgrade tool.
69   void SetTimes(base::Time last_used, base::Time last_modified);
70
71   // Logs a begin event and enables logging for the EntryImplV3. Will also cause
72   // an end event to be logged on destruction. The EntryImplV3 must have its key
73   // initialized before this is called. |created| is true if the Entry was
74   // created rather than opened.
75   void BeginLogging(net::NetLog* net_log, bool created);
76
77   const net::BoundNetLog& net_log() const;
78
79   // Entry interface.
80   void Doom() override;
81   void Close() override;
82   std::string GetKey() const override;
83   base::Time GetLastUsed() const override;
84   base::Time GetLastModified() const override;
85   int32 GetDataSize(int index) const override;
86   int ReadData(int index,
87                int offset,
88                IOBuffer* buf,
89                int buf_len,
90                const CompletionCallback& callback) override;
91   int WriteData(int index,
92                 int offset,
93                 IOBuffer* buf,
94                 int buf_len,
95                 const CompletionCallback& callback,
96                 bool truncate) override;
97   int ReadSparseData(int64 offset,
98                      IOBuffer* buf,
99                      int buf_len,
100                      const CompletionCallback& callback) override;
101   int WriteSparseData(int64 offset,
102                       IOBuffer* buf,
103                       int buf_len,
104                       const CompletionCallback& callback) override;
105   int GetAvailableRange(int64 offset,
106                         int len,
107                         int64* start,
108                         const CompletionCallback& callback) override;
109   bool CouldBeSparse() const override;
110   void CancelSparseIO() override;
111   int ReadyForSparseIO(const CompletionCallback& callback) override;
112
113  private:
114   enum {
115      kNumStreams = 3
116   };
117   class UserBuffer;
118
119   ~EntryImplV3() override;
120
121   // Do all the work for ReadDataImpl and WriteDataImpl.  Implemented as
122   // separate functions to make logging of results simpler.
123   int InternalReadData(int index, int offset, IOBuffer* buf,
124                        int buf_len, const CompletionCallback& callback);
125   int InternalWriteData(int index, int offset, IOBuffer* buf, int buf_len,
126                         const CompletionCallback& callback, bool truncate);
127
128   // Initializes the storage for an internal or external data block.
129   bool CreateDataBlock(int index, int size);
130
131   // Initializes the storage for an internal or external generic block.
132   bool CreateBlock(int size, Addr* address);
133
134   // Deletes the data pointed by address, maybe backed by files_[index].
135   // Note that most likely the caller should delete (and store) the reference to
136   // |address| *before* calling this method because we don't want to have an
137   // entry using an address that is already free.
138   void DeleteData(Addr address, int index);
139
140   // Updates ranking information.
141   void UpdateRank(bool modified);
142
143   // Deletes this entry from disk. If |everything| is false, only the user data
144   // will be removed, leaving the key and control data intact.
145   void DeleteEntryData(bool everything);
146
147   // Prepares the target file or buffer for a write of buf_len bytes at the
148   // given offset.
149   bool PrepareTarget(int index, int offset, int buf_len, bool truncate);
150
151   // Adjusts the internal buffer and file handle for a write that truncates this
152   // stream.
153   bool HandleTruncation(int index, int offset, int buf_len);
154
155   // Copies data from disk to the internal buffer.
156   bool CopyToLocalBuffer(int index);
157
158   // Reads from a block data file to this object's memory buffer.
159   bool MoveToLocalBuffer(int index);
160
161   // Loads the external file to this object's memory buffer.
162   bool ImportSeparateFile(int index, int new_size);
163
164   // Makes sure that the internal buffer can handle the a write of |buf_len|
165   // bytes to |offset|.
166   bool PrepareBuffer(int index, int offset, int buf_len);
167
168   // Flushes the in-memory data to the backing storage. The data destination
169   // is determined based on the current data length and |min_len|.
170   bool Flush(int index, int min_len);
171
172   // Updates the size of a given data stream.
173   void UpdateSize(int index, int old_size, int new_size);
174
175   // Initializes the sparse control object. Returns a net error code.
176   int InitSparseData();
177
178   // Adds the provided |flags| to the current EntryFlags for this entry.
179   void SetEntryFlags(uint32 flags);
180
181   // Returns the current EntryFlags for this entry.
182   uint32 GetEntryFlags();
183
184   // Gets the data stored at the given index. If the information is in memory,
185   // a buffer will be allocated and the data will be copied to it (the caller
186   // can find out the size of the buffer before making this call). Otherwise,
187   // the cache address of the data will be returned, and that address will be
188   // removed from the regular book keeping of this entry so the caller is
189   // responsible for deleting the block (or file) from the backing store at some
190   // point; there is no need to report any storage-size change, only to do the
191   // actual cleanup.
192   void GetData(int index, char** buffer, Addr* address);
193
194   // Generates a histogram for the time spent working on this operation.
195   void ReportIOTime(Operation op, const base::TimeTicks& start);
196
197   // Logs this entry to the internal trace buffer.
198   void Log(const char* msg);
199
200   scoped_ptr<EntryRecord> entry_;  // Basic record for this entry.
201   scoped_ptr<ShortEntryRecord> short_entry_;  // Valid for evicted entries.
202   base::WeakPtr<BackendImplV3> backend_;  // Back pointer to the cache.
203   scoped_ptr<UserBuffer> user_buffers_[kNumStreams];  // Stores user data.
204   mutable std::string key_;           // Copy of the key.
205   Addr address_;
206   int unreported_size_[kNumStreams];  // Bytes not reported yet to the backend.
207   bool doomed_;               // True if this entry was removed from the cache.
208   bool read_only_;
209   bool dirty_;                // True if there is something to write.
210   bool modified_;
211   // scoped_ptr<SparseControlV3> sparse_;  // Support for sparse entries.
212
213   net::BoundNetLog net_log_;
214
215   DISALLOW_COPY_AND_ASSIGN(EntryImplV3);
216 };
217
218 }  // namespace disk_cache
219
220 #endif  // NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_V3_H_