blob: 81d5f7c888b1d020bfa23d601e89dae0e4aa8f56 [file] [log] [blame]
Ben Murdochbb1529c2013-08-08 10:24:53 +01001// 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.
4
5#include "net/disk_cache/simple/simple_entry_operation.h"
6
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"
11
12namespace disk_cache {
13
14SimpleEntryOperation::SimpleEntryOperation(const SimpleEntryOperation& other)
15 : entry_(other.entry_.get()),
16 buf_(other.buf_),
17 callback_(other.callback_),
18 out_entry_(other.out_entry_),
19 offset_(other.offset_),
20 length_(other.length_),
21 type_(other.type_),
22 have_index_(other.have_index_),
23 index_(other.index_),
24 truncate_(other.truncate_),
25 optimistic_(other.optimistic_),
26 alone_in_queue_(other.alone_in_queue_) {
27}
28
29SimpleEntryOperation::~SimpleEntryOperation() {}
30
31// Static.
32SimpleEntryOperation SimpleEntryOperation::OpenOperation(
33 SimpleEntryImpl* entry,
34 bool have_index,
35 const CompletionCallback& callback,
36 Entry** out_entry) {
37 return SimpleEntryOperation(entry,
38 NULL,
39 callback,
40 out_entry,
41 0,
42 0,
43 TYPE_OPEN,
44 have_index,
45 0,
46 false,
47 false,
48 false);
49}
50
51// Static.
52SimpleEntryOperation SimpleEntryOperation::CreateOperation(
53 SimpleEntryImpl* entry,
54 bool have_index,
55 const CompletionCallback& callback,
56 Entry** out_entry) {
57 return SimpleEntryOperation(entry,
58 NULL,
59 callback,
60 out_entry,
61 0,
62 0,
63 TYPE_CREATE,
64 have_index,
65 0,
66 false,
67 false,
68 false);
69}
70
71// Static.
72SimpleEntryOperation SimpleEntryOperation::CloseOperation(
73 SimpleEntryImpl* entry) {
74 return SimpleEntryOperation(entry,
75 NULL,
76 CompletionCallback(),
77 NULL,
78 0,
79 0,
80 TYPE_CLOSE,
81 false,
82 0,
83 false,
84 false,
85 false);
86}
87
88// Static.
89SimpleEntryOperation SimpleEntryOperation::ReadOperation(
90 SimpleEntryImpl* entry,
91 int index,
92 int offset,
93 int length,
94 net::IOBuffer* buf,
95 const CompletionCallback& callback,
96 bool alone_in_queue) {
97 return SimpleEntryOperation(entry,
98 buf,
99 callback,
100 NULL,
101 offset,
102 length,
103 TYPE_READ,
104 false,
105 index,
106 false,
107 false,
108 alone_in_queue);
109}
110
111// Static.
112SimpleEntryOperation SimpleEntryOperation::WriteOperation(
113 SimpleEntryImpl* entry,
114 int index,
115 int offset,
116 int length,
117 net::IOBuffer* buf,
118 bool truncate,
119 bool optimistic,
120 const CompletionCallback& callback) {
121 return SimpleEntryOperation(entry,
122 buf,
123 callback,
124 NULL,
125 offset,
126 length,
127 TYPE_WRITE,
128 false,
129 index,
130 truncate,
131 optimistic,
132 false);
133}
134
135bool SimpleEntryOperation::ConflictsWith(
136 const SimpleEntryOperation& other_op) const {
137 if (type_ != TYPE_READ && type_ != TYPE_WRITE)
138 return true;
139 if (other_op.type() != TYPE_READ && other_op.type() != TYPE_WRITE)
140 return true;
141 if (type() == TYPE_READ && other_op.type() == TYPE_READ)
142 return false;
143 if (index_ != other_op.index_)
144 return false;
145 int end = (type_ == TYPE_WRITE && truncate_) ? INT_MAX : offset_ + length_;
146 int other_op_end = (other_op.type() == TYPE_WRITE && other_op.truncate())
147 ? INT_MAX
148 : other_op.offset() + other_op.length();
149 return (offset_ < other_op_end && other_op.offset() < end);
150}
151
152void SimpleEntryOperation::ReleaseReferences() {
153 callback_ = CompletionCallback();
154 buf_ = NULL;
155 entry_ = NULL;
156}
157
158SimpleEntryOperation::SimpleEntryOperation(SimpleEntryImpl* entry,
159 net::IOBuffer* buf,
160 const CompletionCallback& callback,
161 Entry** out_entry,
162 int offset,
163 int length,
164 EntryOperationType type,
165 bool have_index,
166 int index,
167 bool truncate,
168 bool optimistic,
169 bool alone_in_queue)
170 : entry_(entry),
171 buf_(buf),
172 callback_(callback),
173 out_entry_(out_entry),
174 offset_(offset),
175 length_(length),
176 type_(type),
177 have_index_(have_index),
178 index_(index),
179 truncate_(truncate),
180 optimistic_(optimistic),
181 alone_in_queue_(alone_in_queue) {
182}
183
184} // namespace disk_cache