OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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_SPARSE_CONTROL_H_ |
| 6 #define NET_DISK_CACHE_SPARSE_CONTROL_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/compiler_specific.h" |
| 12 #include "net/base/completion_callback.h" |
| 13 #include "net/disk_cache/bitmap.h" |
| 14 #include "net/disk_cache/disk_format.h" |
| 15 |
| 16 namespace net { |
| 17 class IOBuffer; |
| 18 class ReusedIOBuffer; |
| 19 } |
| 20 |
| 21 namespace disk_cache { |
| 22 |
| 23 class Entry; |
| 24 class EntryImpl; |
| 25 |
| 26 // This class provides support for the sparse capabilities of the disk cache. |
| 27 // Basically, sparse IO is directed from EntryImpl to this class, and we split |
| 28 // the operation into multiple small pieces, sending each one to the |
| 29 // appropriate entry. An instance of this class is asociated with each entry |
| 30 // used directly for sparse operations (the entry passed in to the constructor). |
| 31 class SparseControl { |
| 32 public: |
| 33 // The operation to perform. |
| 34 enum SparseOperation { |
| 35 kNoOperation, |
| 36 kReadOperation, |
| 37 kWriteOperation |
| 38 }; |
| 39 |
| 40 explicit SparseControl(EntryImpl* entry) |
| 41 : entry_(entry), child_(NULL), operation_(kNoOperation), init_(false), |
| 42 child_map_(child_data_.bitmap, kNumSparseBits, kNumSparseBits / 32), |
| 43 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 44 child_callback_(this, &SparseControl::OnChildIOCompleted)), |
| 45 user_callback_(NULL) {} |
| 46 ~SparseControl(); |
| 47 |
| 48 // Initializes the object for the current entry. If this entry already stores |
| 49 // sparse data, or can be used to do it, it updates the relevant information |
| 50 // on disk and returns net::OK. Otherwise it returns a net error code. |
| 51 int Init(); |
| 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, int64 offset, net::IOBuffer* buf, |
| 60 int buf_len, net::CompletionCallback* callback); |
| 61 |
| 62 // Implements Entry::GetAvailableRange(). |
| 63 int GetAvailableRange(int64 offset, int len, int64* start); |
| 64 |
| 65 private: |
| 66 // Creates a new sparse entry or opens an aready created entry from disk. |
| 67 // These methods just read / write the required info from disk for the current |
| 68 // entry, and verify that everything is correct. The return value is a net |
| 69 // error code. |
| 70 int CreateSparseEntry(); |
| 71 int OpenSparseEntry(int data_len); |
| 72 |
| 73 // Opens and closes a child entry. A child entry is a regular EntryImpl object |
| 74 // with a key derived from the key of the resource to store and the range |
| 75 // stored by that child. |
| 76 bool OpenChild(); |
| 77 void CloseChild(); |
| 78 std::string GenerateChildKey(); |
| 79 |
| 80 // Returns true if the required child is tracked by the parent entry, i.e. it |
| 81 // was already created. |
| 82 bool ChildPresent(); |
| 83 |
| 84 // Starts tracking this child. A new child entry was created so we must set |
| 85 // the corresponding bit on the bitmap of children. |
| 86 void SetChildBit(); |
| 87 |
| 88 // Writes to disk the tracking information for this entry. |
| 89 void WriteSparseData(); |
| 90 |
| 91 // Verify that the range to be accessed for the current child is appropriate. |
| 92 // Returns false if an error is detected or there is no need to perform the |
| 93 // current IO operation (for instance if the required range is not stored by |
| 94 // the child). |
| 95 bool VerifyRange(); |
| 96 |
| 97 // Updates the contents bitmap for the current range, based on the result of |
| 98 // the current operation. |
| 99 void UpdateRange(int result); |
| 100 |
| 101 // Initializes the sparse info for the current child. |
| 102 void InitChildData(); |
| 103 |
| 104 // Iterates through all the children needed to complete the current operation. |
| 105 void DoChildrenIO(); |
| 106 |
| 107 // Performs a single operation with the current child. Returns true when we |
| 108 // should move on to the next child and false when we should interrupt our |
| 109 // work. |
| 110 bool DoChildIO(); |
| 111 |
| 112 // Performs the required work after a single IO operations finishes. |
| 113 void DoChildIOCompleted(int result); |
| 114 |
| 115 // Invoked by the callback of asynchronous operations. |
| 116 void OnChildIOCompleted(int result); |
| 117 |
| 118 // Reports to the user that we are done. |
| 119 void DoUserCallback(); |
| 120 |
| 121 EntryImpl* entry_; // The sparse entry. |
| 122 Entry* child_; // The current child entry. |
| 123 SparseOperation operation_; |
| 124 bool pending_; // True if any child IO operation returned pending. |
| 125 bool finished_; |
| 126 bool init_; |
| 127 |
| 128 SparseHeader sparse_header_; // Data about the children of entry_. |
| 129 Bitmap children_map_; // The actual bitmap of children. |
| 130 SparseData child_data_; // Parent and allocation map of child_. |
| 131 Bitmap child_map_; // The allocation map as a bitmap. |
| 132 |
| 133 net::CompletionCallbackImpl<SparseControl> child_callback_; |
| 134 net::CompletionCallback* user_callback_; |
| 135 int64 offset_; // Current sparse offset. |
| 136 scoped_refptr<net::ReusedIOBuffer> user_buf_; |
| 137 int buf_len_; // Bytes to read or write. |
| 138 int child_offset_; // Offset to use for the current child. |
| 139 int child_len_; // Bytes to read or write for this child. |
| 140 int result_; |
| 141 |
| 142 DISALLOW_COPY_AND_ASSIGN(SparseControl); |
| 143 }; |
| 144 |
| 145 } // namespace disk_cache |
| 146 |
| 147 #endif // NET_DISK_CACHE_SPARSE_CONTROL_H_ |
OLD | NEW |