1 // Copyright 2013 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.
5 #include "net/disk_cache/simple/simple_entry_operation.h"
7 #include "base/logging.h"
8 #include "net/base/io_buffer.h"
9 #include "net/disk_cache/disk_cache.h"
10 #include "net/disk_cache/simple/simple_entry_impl.h"
12 namespace disk_cache
{
16 bool IsReadWriteType(unsigned int type
) {
17 return type
== SimpleEntryOperation::TYPE_READ
||
18 type
== SimpleEntryOperation::TYPE_WRITE
||
19 type
== SimpleEntryOperation::TYPE_READ_SPARSE
||
20 type
== SimpleEntryOperation::TYPE_WRITE_SPARSE
;
23 bool IsReadType(unsigned type
) {
24 return type
== SimpleEntryOperation::TYPE_READ
||
25 type
== SimpleEntryOperation::TYPE_READ_SPARSE
;
28 bool IsSparseType(unsigned type
) {
29 return type
== SimpleEntryOperation::TYPE_READ_SPARSE
||
30 type
== SimpleEntryOperation::TYPE_WRITE_SPARSE
;
35 SimpleEntryOperation::SimpleEntryOperation(const SimpleEntryOperation
& other
)
36 : entry_(other
.entry_
.get()),
38 callback_(other
.callback_
),
39 out_entry_(other
.out_entry_
),
40 offset_(other
.offset_
),
41 sparse_offset_(other
.sparse_offset_
),
42 length_(other
.length_
),
43 out_start_(other
.out_start_
),
45 have_index_(other
.have_index_
),
47 truncate_(other
.truncate_
),
48 optimistic_(other
.optimistic_
),
49 alone_in_queue_(other
.alone_in_queue_
) {
52 SimpleEntryOperation::~SimpleEntryOperation() {}
55 SimpleEntryOperation
SimpleEntryOperation::OpenOperation(
56 SimpleEntryImpl
* entry
,
58 const CompletionCallback
& callback
,
60 return SimpleEntryOperation(entry
,
77 SimpleEntryOperation
SimpleEntryOperation::CreateOperation(
78 SimpleEntryImpl
* entry
,
80 const CompletionCallback
& callback
,
82 return SimpleEntryOperation(entry
,
99 SimpleEntryOperation
SimpleEntryOperation::CloseOperation(
100 SimpleEntryImpl
* entry
) {
101 return SimpleEntryOperation(entry
,
103 CompletionCallback(),
118 SimpleEntryOperation
SimpleEntryOperation::ReadOperation(
119 SimpleEntryImpl
* entry
,
124 const CompletionCallback
& callback
,
125 bool alone_in_queue
) {
126 return SimpleEntryOperation(entry
,
143 SimpleEntryOperation
SimpleEntryOperation::WriteOperation(
144 SimpleEntryImpl
* entry
,
151 const CompletionCallback
& callback
) {
152 return SimpleEntryOperation(entry
,
169 SimpleEntryOperation
SimpleEntryOperation::ReadSparseOperation(
170 SimpleEntryImpl
* entry
,
174 const CompletionCallback
& callback
) {
175 return SimpleEntryOperation(entry
,
192 SimpleEntryOperation
SimpleEntryOperation::WriteSparseOperation(
193 SimpleEntryImpl
* entry
,
197 const CompletionCallback
& callback
) {
198 return SimpleEntryOperation(entry
,
215 SimpleEntryOperation
SimpleEntryOperation::GetAvailableRangeOperation(
216 SimpleEntryImpl
* entry
,
220 const CompletionCallback
& callback
) {
221 return SimpleEntryOperation(entry
,
229 TYPE_GET_AVAILABLE_RANGE
,
238 SimpleEntryOperation
SimpleEntryOperation::DoomOperation(
239 SimpleEntryImpl
* entry
,
240 const CompletionCallback
& callback
) {
241 net::IOBuffer
* const buf
= NULL
;
242 Entry
** const out_entry
= NULL
;
243 const int offset
= 0;
244 const int64 sparse_offset
= 0;
245 const int length
= 0;
246 int64
* const out_start
= NULL
;
247 const bool have_index
= false;
249 const bool truncate
= false;
250 const bool optimistic
= false;
251 const bool alone_in_queue
= false;
252 return SimpleEntryOperation(entry
,
268 bool SimpleEntryOperation::ConflictsWith(
269 const SimpleEntryOperation
& other_op
) const {
270 EntryOperationType other_type
= other_op
.type();
272 // Non-read/write operations conflict with everything.
273 if (!IsReadWriteType(type_
) || !IsReadWriteType(other_type
))
276 // Reads (sparse or otherwise) conflict with nothing.
277 if (IsReadType(type_
) && IsReadType(other_type
))
280 // Sparse and non-sparse operations do not conflict with each other.
281 if (IsSparseType(type_
) != IsSparseType(other_type
)) {
285 // There must be two read/write operations, at least one must be a write, and
286 // they must be either both non-sparse or both sparse. Compare the streams
287 // and offsets to see whether they overlap.
289 if (IsSparseType(type_
)) {
290 int64 end
= sparse_offset_
+ length_
;
291 int64 other_op_end
= other_op
.sparse_offset() + other_op
.length();
292 return sparse_offset_
< other_op_end
&& other_op
.sparse_offset() < end
;
295 if (index_
!= other_op
.index_
)
297 int end
= (type_
== TYPE_WRITE
&& truncate_
) ? INT_MAX
: offset_
+ length_
;
298 int other_op_end
= (other_op
.type() == TYPE_WRITE
&& other_op
.truncate())
300 : other_op
.offset() + other_op
.length();
301 return offset_
< other_op_end
&& other_op
.offset() < end
;
304 void SimpleEntryOperation::ReleaseReferences() {
305 callback_
= CompletionCallback();
310 SimpleEntryOperation::SimpleEntryOperation(SimpleEntryImpl
* entry
,
312 const CompletionCallback
& callback
,
318 EntryOperationType type
,
327 out_entry_(out_entry
),
329 sparse_offset_(sparse_offset
),
331 out_start_(out_start
),
333 have_index_(have_index
),
336 optimistic_(optimistic
),
337 alone_in_queue_(alone_in_queue
) {
340 } // namespace disk_cache