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