• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_OPERATION_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_OPERATION_H_
7 
8 #include "base/memory/ref_counted.h"
9 #include "net/base/completion_callback.h"
10 #include "net/base/net_log.h"
11 
12 namespace net {
13 class IOBuffer;
14 }
15 
16 namespace disk_cache {
17 
18 class Entry;
19 class SimpleEntryImpl;
20 
21 // SimpleEntryOperation stores the information regarding operations in
22 // SimpleEntryImpl, between the moment they are issued by users of the backend,
23 // and the moment when they are executed.
24 class SimpleEntryOperation {
25  public:
26   typedef net::CompletionCallback CompletionCallback;
27 
28   enum EntryOperationType {
29     TYPE_OPEN = 0,
30     TYPE_CREATE = 1,
31     TYPE_CLOSE = 2,
32     TYPE_READ = 3,
33     TYPE_WRITE = 4,
34     TYPE_READ_SPARSE = 5,
35     TYPE_WRITE_SPARSE = 6,
36     TYPE_GET_AVAILABLE_RANGE = 7,
37     TYPE_DOOM = 8,
38   };
39 
40   SimpleEntryOperation(const SimpleEntryOperation& other);
41   ~SimpleEntryOperation();
42 
43   static SimpleEntryOperation OpenOperation(SimpleEntryImpl* entry,
44                                             bool have_index,
45                                             const CompletionCallback& callback,
46                                             Entry** out_entry);
47   static SimpleEntryOperation CreateOperation(
48       SimpleEntryImpl* entry,
49       bool have_index,
50       const CompletionCallback& callback,
51       Entry** out_entry);
52   static SimpleEntryOperation CloseOperation(SimpleEntryImpl* entry);
53   static SimpleEntryOperation ReadOperation(SimpleEntryImpl* entry,
54                                             int index,
55                                             int offset,
56                                             int length,
57                                             net::IOBuffer* buf,
58                                             const CompletionCallback& callback,
59                                             bool alone_in_queue);
60   static SimpleEntryOperation WriteOperation(
61       SimpleEntryImpl* entry,
62       int index,
63       int offset,
64       int length,
65       net::IOBuffer* buf,
66       bool truncate,
67       bool optimistic,
68       const CompletionCallback& callback);
69   static SimpleEntryOperation ReadSparseOperation(
70       SimpleEntryImpl* entry,
71       int64 sparse_offset,
72       int length,
73       net::IOBuffer* buf,
74       const CompletionCallback& callback);
75   static SimpleEntryOperation WriteSparseOperation(
76       SimpleEntryImpl* entry,
77       int64 sparse_offset,
78       int length,
79       net::IOBuffer* buf,
80       const CompletionCallback& callback);
81   static SimpleEntryOperation GetAvailableRangeOperation(
82       SimpleEntryImpl* entry,
83       int64 sparse_offset,
84       int length,
85       int64* out_start,
86       const CompletionCallback& callback);
87   static SimpleEntryOperation DoomOperation(
88       SimpleEntryImpl* entry,
89       const CompletionCallback& callback);
90 
91   bool ConflictsWith(const SimpleEntryOperation& other_op) const;
92   // Releases all references. After calling this operation, SimpleEntryOperation
93   // will only hold POD members.
94   void ReleaseReferences();
95 
type()96   EntryOperationType type() const {
97     return static_cast<EntryOperationType>(type_);
98   }
callback()99   const CompletionCallback& callback() const { return callback_; }
out_entry()100   Entry** out_entry() { return out_entry_; }
have_index()101   bool have_index() const { return have_index_; }
index()102   int index() const { return index_; }
offset()103   int offset() const { return offset_; }
sparse_offset()104   int64 sparse_offset() const { return sparse_offset_; }
length()105   int length() const { return length_; }
out_start()106   int64* out_start() { return out_start_; }
buf()107   net::IOBuffer* buf() { return buf_.get(); }
truncate()108   bool truncate() const { return truncate_; }
optimistic()109   bool optimistic() const { return optimistic_; }
alone_in_queue()110   bool alone_in_queue() const { return alone_in_queue_; }
111 
112  private:
113   SimpleEntryOperation(SimpleEntryImpl* entry,
114                        net::IOBuffer* buf,
115                        const CompletionCallback& callback,
116                        Entry** out_entry,
117                        int offset,
118                        int64 sparse_offset,
119                        int length,
120                        int64* out_start,
121                        EntryOperationType type,
122                        bool have_index,
123                        int index,
124                        bool truncate,
125                        bool optimistic,
126                        bool alone_in_queue);
127 
128   // This ensures entry will not be deleted until the operation has ran.
129   scoped_refptr<SimpleEntryImpl> entry_;
130   scoped_refptr<net::IOBuffer> buf_;
131   CompletionCallback callback_;
132 
133   // Used in open and create operations.
134   Entry** out_entry_;
135 
136   // Used in write and read operations.
137   const int offset_;
138   const int64 sparse_offset_;
139   const int length_;
140 
141   // Used in get available range operations.
142   int64* const out_start_;
143 
144   const EntryOperationType type_;
145   // Used in open and create operations.
146   const bool have_index_;
147   // Used in write and read operations.
148   const unsigned int index_;
149   // Used only in write operations.
150   const bool truncate_;
151   const bool optimistic_;
152   // Used only in SimpleCache.ReadIsParallelizable histogram.
153   const bool alone_in_queue_;
154 };
155 
156 }  // namespace disk_cache
157 
158 #endif  // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_OPERATION_H_
159