1 // Copyright 2013 The Chromium Authors
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 <limits.h>
8
9 #include "net/base/io_buffer.h"
10 #include "net/disk_cache/disk_cache.h"
11 #include "net/disk_cache/simple/simple_entry_impl.h"
12
13 namespace disk_cache {
14
15 SimpleEntryOperation::SimpleEntryOperation(SimpleEntryOperation&& other) =
16 default;
17
18 SimpleEntryOperation::~SimpleEntryOperation() = default;
19
20 // static
OpenOperation(SimpleEntryImpl * entry,EntryResultState result_state,EntryResultCallback callback)21 SimpleEntryOperation SimpleEntryOperation::OpenOperation(
22 SimpleEntryImpl* entry,
23 EntryResultState result_state,
24 EntryResultCallback callback) {
25 SimpleEntryOperation op(entry, nullptr, CompletionOnceCallback(), 0, 0, 0,
26 TYPE_OPEN, INDEX_NOEXIST, 0, false, false);
27 op.entry_callback_ = std::move(callback);
28 op.entry_result_state_ = result_state;
29 return op;
30 }
31
32 // static
CreateOperation(SimpleEntryImpl * entry,EntryResultState result_state,EntryResultCallback callback)33 SimpleEntryOperation SimpleEntryOperation::CreateOperation(
34 SimpleEntryImpl* entry,
35 EntryResultState result_state,
36 EntryResultCallback callback) {
37 SimpleEntryOperation op(entry, nullptr, CompletionOnceCallback(), 0, 0, 0,
38 TYPE_CREATE, INDEX_NOEXIST, 0, false, false);
39 op.entry_callback_ = std::move(callback);
40 op.entry_result_state_ = result_state;
41 return op;
42 }
43
44 // static
OpenOrCreateOperation(SimpleEntryImpl * entry,OpenEntryIndexEnum index_state,EntryResultState result_state,EntryResultCallback callback)45 SimpleEntryOperation SimpleEntryOperation::OpenOrCreateOperation(
46 SimpleEntryImpl* entry,
47 OpenEntryIndexEnum index_state,
48 EntryResultState result_state,
49 EntryResultCallback callback) {
50 SimpleEntryOperation op(entry, nullptr, CompletionOnceCallback(), 0, 0, 0,
51 TYPE_OPEN_OR_CREATE, index_state, 0, false, false);
52 op.entry_callback_ = std::move(callback);
53 op.entry_result_state_ = result_state;
54 return op;
55 }
56
57 // static
CloseOperation(SimpleEntryImpl * entry)58 SimpleEntryOperation SimpleEntryOperation::CloseOperation(
59 SimpleEntryImpl* entry) {
60 return SimpleEntryOperation(entry, nullptr, CompletionOnceCallback(), 0, 0, 0,
61 TYPE_CLOSE, INDEX_NOEXIST, 0, false, false);
62 }
63
64 // static
ReadOperation(SimpleEntryImpl * entry,int index,int offset,int length,net::IOBuffer * buf,CompletionOnceCallback callback)65 SimpleEntryOperation SimpleEntryOperation::ReadOperation(
66 SimpleEntryImpl* entry,
67 int index,
68 int offset,
69 int length,
70 net::IOBuffer* buf,
71 CompletionOnceCallback callback) {
72 return SimpleEntryOperation(entry, buf, std::move(callback), offset, 0,
73 length, TYPE_READ, INDEX_NOEXIST, index, false,
74 false);
75 }
76
77 // static
WriteOperation(SimpleEntryImpl * entry,int index,int offset,int length,net::IOBuffer * buf,bool truncate,bool optimistic,CompletionOnceCallback callback)78 SimpleEntryOperation SimpleEntryOperation::WriteOperation(
79 SimpleEntryImpl* entry,
80 int index,
81 int offset,
82 int length,
83 net::IOBuffer* buf,
84 bool truncate,
85 bool optimistic,
86 CompletionOnceCallback callback) {
87 return SimpleEntryOperation(entry, buf, std::move(callback), offset, 0,
88 length, TYPE_WRITE, INDEX_NOEXIST, index,
89 truncate, optimistic);
90 }
91
92 // static
ReadSparseOperation(SimpleEntryImpl * entry,int64_t sparse_offset,int length,net::IOBuffer * buf,CompletionOnceCallback callback)93 SimpleEntryOperation SimpleEntryOperation::ReadSparseOperation(
94 SimpleEntryImpl* entry,
95 int64_t sparse_offset,
96 int length,
97 net::IOBuffer* buf,
98 CompletionOnceCallback callback) {
99 return SimpleEntryOperation(entry, buf, std::move(callback), 0, sparse_offset,
100 length, TYPE_READ_SPARSE, INDEX_NOEXIST, 0, false,
101 false);
102 }
103
104 // static
WriteSparseOperation(SimpleEntryImpl * entry,int64_t sparse_offset,int length,net::IOBuffer * buf,CompletionOnceCallback callback)105 SimpleEntryOperation SimpleEntryOperation::WriteSparseOperation(
106 SimpleEntryImpl* entry,
107 int64_t sparse_offset,
108 int length,
109 net::IOBuffer* buf,
110 CompletionOnceCallback callback) {
111 return SimpleEntryOperation(entry, buf, std::move(callback), 0, sparse_offset,
112 length, TYPE_WRITE_SPARSE, INDEX_NOEXIST, 0,
113 false, false);
114 }
115
116 // static
GetAvailableRangeOperation(SimpleEntryImpl * entry,int64_t sparse_offset,int length,RangeResultCallback callback)117 SimpleEntryOperation SimpleEntryOperation::GetAvailableRangeOperation(
118 SimpleEntryImpl* entry,
119 int64_t sparse_offset,
120 int length,
121 RangeResultCallback callback) {
122 SimpleEntryOperation op(entry, nullptr, CompletionOnceCallback(), 0,
123 sparse_offset, length, TYPE_GET_AVAILABLE_RANGE,
124 INDEX_NOEXIST, 0, false, false);
125 op.range_callback_ = std::move(callback);
126 return op;
127 }
128
129 // static
DoomOperation(SimpleEntryImpl * entry,net::CompletionOnceCallback callback)130 SimpleEntryOperation SimpleEntryOperation::DoomOperation(
131 SimpleEntryImpl* entry,
132 net::CompletionOnceCallback callback) {
133 net::IOBuffer* const buf = nullptr;
134 const int offset = 0;
135 const int64_t sparse_offset = 0;
136 const int length = 0;
137 const OpenEntryIndexEnum index_state = INDEX_NOEXIST;
138 const int index = 0;
139 const bool truncate = false;
140 const bool optimistic = false;
141 return SimpleEntryOperation(entry, buf, std::move(callback), offset,
142 sparse_offset, length, TYPE_DOOM, index_state,
143 index, truncate, optimistic);
144 }
145
SimpleEntryOperation(SimpleEntryImpl * entry,net::IOBuffer * buf,net::CompletionOnceCallback callback,int offset,int64_t sparse_offset,int length,EntryOperationType type,OpenEntryIndexEnum index_state,int index,bool truncate,bool optimistic)146 SimpleEntryOperation::SimpleEntryOperation(SimpleEntryImpl* entry,
147 net::IOBuffer* buf,
148 net::CompletionOnceCallback callback,
149 int offset,
150 int64_t sparse_offset,
151 int length,
152 EntryOperationType type,
153 OpenEntryIndexEnum index_state,
154 int index,
155 bool truncate,
156 bool optimistic)
157 : entry_(entry),
158 buf_(buf),
159 callback_(std::move(callback)),
160 offset_(offset),
161 sparse_offset_(sparse_offset),
162 length_(length),
163 type_(type),
164 index_state_(index_state),
165 index_(index),
166 truncate_(truncate),
167 optimistic_(optimistic) {}
168
169 } // namespace disk_cache
170