| OLD | NEW |
| (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 #ifndef NET_DISK_CACHE_BLOCKFILE_SPARSE_CONTROL_V3_H_ | |
| 6 #define NET_DISK_CACHE_BLOCKFILE_SPARSE_CONTROL_V3_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/compiler_specific.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "net/base/completion_callback.h" | |
| 16 #include "net/disk_cache/blockfile/bitmap.h" | |
| 17 #include "net/disk_cache/disk_format.h" | |
| 18 | |
| 19 namespace net { | |
| 20 class IOBuffer; | |
| 21 class DrainableIOBuffer; | |
| 22 } | |
| 23 | |
| 24 namespace disk_cache { | |
| 25 | |
| 26 class Entry; | |
| 27 class EntryImpl; | |
| 28 | |
| 29 // This class provides support for the sparse capabilities of the disk cache. | |
| 30 // Basically, sparse IO is directed from EntryImpl to this class, and we split | |
| 31 // the operation into multiple small pieces, sending each one to the | |
| 32 // appropriate entry. An instance of this class is asociated with each entry | |
| 33 // used directly for sparse operations (the entry passed in to the constructor). | |
| 34 class SparseControl { | |
| 35 public: | |
| 36 typedef net::CompletionCallback CompletionCallback; | |
| 37 | |
| 38 // The operation to perform. | |
| 39 enum SparseOperation { | |
| 40 kNoOperation, | |
| 41 kReadOperation, | |
| 42 kWriteOperation, | |
| 43 kGetRangeOperation | |
| 44 }; | |
| 45 | |
| 46 explicit SparseControl(EntryImpl* entry); | |
| 47 ~SparseControl(); | |
| 48 | |
| 49 // Performs a quick test to see if the entry is sparse or not, without | |
| 50 // generating disk IO (so the answer provided is only a best effort). | |
| 51 bool CouldBeSparse() const; | |
| 52 | |
| 53 // Performs an actual sparse read or write operation for this entry. |op| is | |
| 54 // the operation to perform, |offset| is the desired sparse offset, |buf| and | |
| 55 // |buf_len| specify the actual data to use and |callback| is the callback | |
| 56 // to use for asynchronous operations. See the description of the Read / | |
| 57 // WriteSparseData for details about the arguments. The return value is the | |
| 58 // number of bytes read or written, or a net error code. | |
| 59 int StartIO(SparseOperation op, | |
| 60 int64_t offset, | |
| 61 net::IOBuffer* buf, | |
| 62 int buf_len, | |
| 63 const CompletionCallback& callback); | |
| 64 | |
| 65 // Implements Entry::GetAvailableRange(). | |
| 66 int GetAvailableRange(int64_t offset, int len, int64_t* start); | |
| 67 | |
| 68 // Cancels the current sparse operation (if any). | |
| 69 void CancelIO(); | |
| 70 | |
| 71 // Returns OK if the entry can be used for new IO or ERR_IO_PENDING if we are | |
| 72 // busy. If the entry is busy, we'll invoke the callback when we are ready | |
| 73 // again. See disk_cache::Entry::ReadyToUse() for more info. | |
| 74 int ReadyToUse(const CompletionCallback& completion_callback); | |
| 75 | |
| 76 // Deletes the children entries of |entry|. | |
| 77 static void DeleteChildren(EntryImpl* entry); | |
| 78 | |
| 79 private: | |
| 80 // Initializes the object for the current entry. If this entry already stores | |
| 81 // sparse data, or can be used to do it, it updates the relevant information | |
| 82 // on disk and returns net::OK. Otherwise it returns a net error code. | |
| 83 int Init(); | |
| 84 | |
| 85 // Creates a new sparse entry or opens an aready created entry from disk. | |
| 86 // These methods just read / write the required info from disk for the current | |
| 87 // entry, and verify that everything is correct. The return value is a net | |
| 88 // error code. | |
| 89 int CreateSparseEntry(); | |
| 90 int OpenSparseEntry(int data_len); | |
| 91 | |
| 92 // Opens and closes a child entry. A child entry is a regular EntryImpl object | |
| 93 // with a key derived from the key of the resource to store and the range | |
| 94 // stored by that child. | |
| 95 bool OpenChild(); | |
| 96 void CloseChild(); | |
| 97 | |
| 98 // Continues the current operation (open) without a current child. | |
| 99 bool ContinueWithoutChild(const std::string& key); | |
| 100 | |
| 101 // Writes to disk the tracking information for this entry. | |
| 102 void WriteSparseData(); | |
| 103 | |
| 104 // Performs a single operation with the current child. Returns true when we | |
| 105 // should move on to the next child and false when we should interrupt our | |
| 106 // work. | |
| 107 bool DoChildIO(); | |
| 108 | |
| 109 // Performs the required work after a single IO operations finishes. | |
| 110 void DoChildIOCompleted(int result); | |
| 111 | |
| 112 std::string GenerateChildKey(); | |
| 113 | |
| 114 // Deletes the current child and continues the current operation (open). | |
| 115 bool KillChildAndContinue(const std::string& key, bool fatal); | |
| 116 | |
| 117 // Returns true if the required child is tracked by the parent entry, i.e. it | |
| 118 // was already created. | |
| 119 bool ChildPresent(); | |
| 120 | |
| 121 // Sets the bit for the current child to the provided |value|. In other words, | |
| 122 // starts or stops tracking this child. | |
| 123 void SetChildBit(bool value); | |
| 124 | |
| 125 // Verify that the range to be accessed for the current child is appropriate. | |
| 126 // Returns false if an error is detected or there is no need to perform the | |
| 127 // current IO operation (for instance if the required range is not stored by | |
| 128 // the child). | |
| 129 bool VerifyRange(); | |
| 130 | |
| 131 // Updates the contents bitmap for the current range, based on the result of | |
| 132 // the current operation. | |
| 133 void UpdateRange(int result); | |
| 134 | |
| 135 // Returns the number of bytes stored at |block_index|, if its allocation-bit | |
| 136 // is off (because it is not completely filled). | |
| 137 int PartialBlockLength(int block_index) const; | |
| 138 | |
| 139 // Initializes the sparse info for the current child. | |
| 140 void InitChildData(); | |
| 141 | |
| 142 // Performs the required work for GetAvailableRange for one child. | |
| 143 int DoGetAvailableRange(); | |
| 144 | |
| 145 // Reports to the user that we are done. | |
| 146 void DoUserCallback(); | |
| 147 void DoAbortCallbacks(); | |
| 148 | |
| 149 // Invoked by the callback of asynchronous operations. | |
| 150 void OnChildIOCompleted(int result); | |
| 151 | |
| 152 EntryImpl* entry_; // The sparse entry. | |
| 153 EntryImpl* child_; // The current child entry. | |
| 154 SparseOperation operation_; | |
| 155 bool pending_; // True if any child IO operation returned pending. | |
| 156 bool finished_; | |
| 157 bool init_; | |
| 158 bool range_found_; // True if GetAvailableRange found something. | |
| 159 bool abort_; // True if we should abort the current operation ASAP. | |
| 160 | |
| 161 SparseHeader sparse_header_; // Data about the children of entry_. | |
| 162 Bitmap children_map_; // The actual bitmap of children. | |
| 163 SparseData child_data_; // Parent and allocation map of child_. | |
| 164 Bitmap child_map_; // The allocation map as a bitmap. | |
| 165 | |
| 166 CompletionCallback user_callback_; | |
| 167 std::vector<CompletionCallback> abort_callbacks_; | |
| 168 int64_t offset_; // Current sparse offset. | |
| 169 scoped_refptr<net::DrainableIOBuffer> user_buf_; | |
| 170 int buf_len_; // Bytes to read or write. | |
| 171 int child_offset_; // Offset to use for the current child. | |
| 172 int child_len_; // Bytes to read or write for this child. | |
| 173 int result_; | |
| 174 | |
| 175 DISALLOW_COPY_AND_ASSIGN(SparseControl); | |
| 176 }; | |
| 177 | |
| 178 } // namespace disk_cache | |
| 179 | |
| 180 #endif // NET_DISK_CACHE_BLOCKFILE_SPARSE_CONTROL_V3_H_ | |
| OLD | NEW |