xref: /aosp_15_r20/external/cronet/net/disk_cache/simple/simple_entry_operation.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_OPERATION_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_OPERATION_H_
7 
8 #include <stdint.h>
9 
10 #include "base/memory/scoped_refptr.h"
11 #include "net/base/completion_once_callback.h"
12 #include "net/disk_cache/disk_cache.h"
13 #include "net/disk_cache/simple/simple_histogram_enums.h"
14 
15 namespace net {
16 class IOBuffer;
17 }
18 
19 namespace disk_cache {
20 
21 class SimpleEntryImpl;
22 
23 // SimpleEntryOperation stores the information regarding operations in
24 // SimpleEntryImpl, between the moment they are issued by users of the backend,
25 // and the moment when they are executed.
26 class SimpleEntryOperation {
27  public:
28   typedef net::CompletionOnceCallback CompletionOnceCallback;
29 
30   enum EntryOperationType {
31     TYPE_OPEN = 0,
32     TYPE_CREATE = 1,
33     TYPE_OPEN_OR_CREATE = 2,
34     TYPE_CLOSE = 3,
35     TYPE_READ = 4,
36     TYPE_WRITE = 5,
37     TYPE_READ_SPARSE = 6,
38     TYPE_WRITE_SPARSE = 7,
39     TYPE_GET_AVAILABLE_RANGE = 8,
40     TYPE_DOOM = 9,
41   };
42 
43   // Whether an open/create method has returned an entry (optimistically)
44   // already, or if it still needs to be delivered via a callback.
45   enum EntryResultState {
46     ENTRY_ALREADY_RETURNED = 0,
47     ENTRY_NEEDS_CALLBACK = 1,
48   };
49 
50   SimpleEntryOperation(SimpleEntryOperation&& other);
51   ~SimpleEntryOperation();
52 
53   static SimpleEntryOperation OpenOperation(SimpleEntryImpl* entry,
54                                             EntryResultState result_state,
55                                             EntryResultCallback);
56   static SimpleEntryOperation CreateOperation(SimpleEntryImpl* entry,
57                                               EntryResultState result_state,
58                                               EntryResultCallback);
59   static SimpleEntryOperation OpenOrCreateOperation(
60       SimpleEntryImpl* entry,
61       OpenEntryIndexEnum index_state,
62       EntryResultState result_state,
63       EntryResultCallback);
64   static SimpleEntryOperation CloseOperation(SimpleEntryImpl* entry);
65   static SimpleEntryOperation ReadOperation(SimpleEntryImpl* entry,
66                                             int index,
67                                             int offset,
68                                             int length,
69                                             net::IOBuffer* buf,
70                                             CompletionOnceCallback callback);
71   static SimpleEntryOperation WriteOperation(SimpleEntryImpl* entry,
72                                              int index,
73                                              int offset,
74                                              int length,
75                                              net::IOBuffer* buf,
76                                              bool truncate,
77                                              bool optimistic,
78                                              CompletionOnceCallback callback);
79   static SimpleEntryOperation ReadSparseOperation(
80       SimpleEntryImpl* entry,
81       int64_t sparse_offset,
82       int length,
83       net::IOBuffer* buf,
84       CompletionOnceCallback callback);
85   static SimpleEntryOperation WriteSparseOperation(
86       SimpleEntryImpl* entry,
87       int64_t sparse_offset,
88       int length,
89       net::IOBuffer* buf,
90       CompletionOnceCallback callback);
91   static SimpleEntryOperation GetAvailableRangeOperation(
92       SimpleEntryImpl* entry,
93       int64_t sparse_offset,
94       int length,
95       RangeResultCallback callback);
96   static SimpleEntryOperation DoomOperation(SimpleEntryImpl* entry,
97                                             CompletionOnceCallback callback);
98 
type()99   EntryOperationType type() const {
100     return static_cast<EntryOperationType>(type_);
101   }
ReleaseCallback()102   CompletionOnceCallback ReleaseCallback() { return std::move(callback_); }
ReleaseEntryResultCallback()103   EntryResultCallback ReleaseEntryResultCallback() {
104     return std::move(entry_callback_);
105   }
ReleaseRangeResultCalback()106   RangeResultCallback ReleaseRangeResultCalback() {
107     return std::move(range_callback_);
108   }
109 
entry_result_state()110   EntryResultState entry_result_state() { return entry_result_state_; }
111 
index_state()112   OpenEntryIndexEnum index_state() const { return index_state_; }
index()113   int index() const { return index_; }
offset()114   int offset() const { return offset_; }
sparse_offset()115   int64_t sparse_offset() const { return sparse_offset_; }
length()116   int length() const { return length_; }
buf()117   net::IOBuffer* buf() { return buf_.get(); }
truncate()118   bool truncate() const { return truncate_; }
optimistic()119   bool optimistic() const { return optimistic_; }
120 
121  private:
122   SimpleEntryOperation(SimpleEntryImpl* entry,
123                        net::IOBuffer* buf,
124                        CompletionOnceCallback callback,
125                        int offset,
126                        int64_t sparse_offset,
127                        int length,
128                        EntryOperationType type,
129                        OpenEntryIndexEnum index_state,
130                        int index,
131                        bool truncate,
132                        bool optimistic);
133 
134   // This ensures entry will not be deleted until the operation has ran.
135   scoped_refptr<SimpleEntryImpl> entry_;
136   scoped_refptr<net::IOBuffer> buf_;
137   CompletionOnceCallback callback_;
138 
139   // Used in open and create operations.
140   EntryResultCallback entry_callback_;
141   EntryResultState entry_result_state_;
142 
143   // Used in write and read operations.
144   const int offset_;
145   const int64_t sparse_offset_;
146   const int length_;
147 
148   // Used in get available range operations.
149   RangeResultCallback range_callback_;
150 
151   const EntryOperationType type_;
152   // Used in the "open or create" operation.
153   const OpenEntryIndexEnum index_state_;
154   // Used in write and read operations.
155   const unsigned int index_;
156   // Used only in write operations.
157   const bool truncate_;
158   const bool optimistic_;
159 };
160 
161 }  // namespace disk_cache
162 
163 #endif  // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_OPERATION_H_
164