1 // Copyright (c) 2012 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 #ifndef NET_DISK_CACHE_IN_FLIGHT_BACKEND_IO_H_
6 #define NET_DISK_CACHE_IN_FLIGHT_BACKEND_IO_H_
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/time/time.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/io_buffer.h"
15 #include "net/disk_cache/in_flight_io.h"
17 namespace disk_cache {
23 // This class represents a single asynchronous disk cache IO operation while it
24 // is being bounced between threads.
25 class BackendIO : public BackgroundIO {
27 BackendIO(InFlightIO* controller, BackendImpl* backend,
28 const net::CompletionCallback& callback);
30 // Runs the actual operation on the background thread.
31 void ExecuteOperation();
33 // Callback implementation.
34 void OnIOComplete(int result);
36 // Called when we are finishing this operation. If |cancel| is true, the user
37 // callback will not be invoked.
38 void OnDone(bool cancel);
40 // Returns true if this operation is directed to an entry (vs. the backend).
41 bool IsEntryOperation();
43 net::CompletionCallback callback() const { return callback_; }
45 // Grabs an extra reference of entry_.
46 void ReferenceEntry();
48 // The operations we proxy:
50 void OpenEntry(const std::string& key, Entry** entry);
51 void CreateEntry(const std::string& key, Entry** entry);
52 void DoomEntry(const std::string& key);
53 void DoomAllEntries();
54 void DoomEntriesBetween(const base::Time initial_time,
55 const base::Time end_time);
56 void DoomEntriesSince(const base::Time initial_time);
57 void OpenNextEntry(void** iter, Entry** next_entry);
58 void OpenPrevEntry(void** iter, Entry** prev_entry);
59 void EndEnumeration(void* iterator);
60 void OnExternalCacheHit(const std::string& key);
61 void CloseEntryImpl(EntryImpl* entry);
62 void DoomEntryImpl(EntryImpl* entry);
63 void FlushQueue(); // Dummy operation.
64 void RunTask(const base::Closure& task);
65 void ReadData(EntryImpl* entry, int index, int offset, net::IOBuffer* buf,
67 void WriteData(EntryImpl* entry, int index, int offset, net::IOBuffer* buf,
68 int buf_len, bool truncate);
69 void ReadSparseData(EntryImpl* entry, int64 offset, net::IOBuffer* buf,
71 void WriteSparseData(EntryImpl* entry, int64 offset, net::IOBuffer* buf,
73 void GetAvailableRange(EntryImpl* entry, int64 offset, int len, int64* start);
74 void CancelSparseIO(EntryImpl* entry);
75 void ReadyForSparseIO(EntryImpl* entry);
78 // There are two types of operations to proxy: regular backend operations are
79 // executed sequentially (queued by the message loop). On the other hand,
80 // operations targeted to a given entry can be long lived and support multiple
81 // simultaneous users (multiple reads or writes to the same entry), and they
82 // are subject to throttling, so we keep an explicit queue.
95 OP_ON_EXTERNAL_CACHE_HIT,
110 virtual ~BackendIO();
112 // Returns true if this operation returns an entry.
115 // Returns the time that has passed since the operation was created.
116 base::TimeDelta ElapsedTime() const;
118 void ExecuteBackendOperation();
119 void ExecuteEntryOperation();
121 BackendImpl* backend_;
122 net::CompletionCallback callback_;
123 Operation operation_;
125 // The arguments of all the operations we proxy:
128 base::Time initial_time_;
129 base::Time end_time_;
135 scoped_refptr<net::IOBuffer> buf_;
140 base::TimeTicks start_time_;
143 DISALLOW_COPY_AND_ASSIGN(BackendIO);
146 // The specialized controller that keeps track of current operations.
147 class InFlightBackendIO : public InFlightIO {
149 InFlightBackendIO(BackendImpl* backend,
150 base::MessageLoopProxy* background_thread);
151 virtual ~InFlightBackendIO();
153 // Proxied operations.
154 void Init(const net::CompletionCallback& callback);
155 void OpenEntry(const std::string& key, Entry** entry,
156 const net::CompletionCallback& callback);
157 void CreateEntry(const std::string& key, Entry** entry,
158 const net::CompletionCallback& callback);
159 void DoomEntry(const std::string& key,
160 const net::CompletionCallback& callback);
161 void DoomAllEntries(const net::CompletionCallback& callback);
162 void DoomEntriesBetween(const base::Time initial_time,
163 const base::Time end_time,
164 const net::CompletionCallback& callback);
165 void DoomEntriesSince(const base::Time initial_time,
166 const net::CompletionCallback& callback);
167 void OpenNextEntry(void** iter, Entry** next_entry,
168 const net::CompletionCallback& callback);
169 void OpenPrevEntry(void** iter, Entry** prev_entry,
170 const net::CompletionCallback& callback);
171 void EndEnumeration(void* iterator);
172 void OnExternalCacheHit(const std::string& key);
173 void CloseEntryImpl(EntryImpl* entry);
174 void DoomEntryImpl(EntryImpl* entry);
175 void FlushQueue(const net::CompletionCallback& callback);
176 void RunTask(const base::Closure& task,
177 const net::CompletionCallback& callback);
178 void ReadData(EntryImpl* entry, int index, int offset, net::IOBuffer* buf,
179 int buf_len, const net::CompletionCallback& callback);
181 EntryImpl* entry, int index, int offset, net::IOBuffer* buf,
182 int buf_len, bool truncate, const net::CompletionCallback& callback);
183 void ReadSparseData(EntryImpl* entry, int64 offset, net::IOBuffer* buf,
184 int buf_len, const net::CompletionCallback& callback);
185 void WriteSparseData(EntryImpl* entry, int64 offset, net::IOBuffer* buf,
186 int buf_len, const net::CompletionCallback& callback);
187 void GetAvailableRange(EntryImpl* entry, int64 offset, int len, int64* start,
188 const net::CompletionCallback& callback);
189 void CancelSparseIO(EntryImpl* entry);
190 void ReadyForSparseIO(EntryImpl* entry,
191 const net::CompletionCallback& callback);
193 // Blocks until all operations are cancelled or completed.
194 void WaitForPendingIO();
196 scoped_refptr<base::MessageLoopProxy> background_thread() {
197 return background_thread_;
200 // Returns true if the current thread is the background thread.
201 bool BackgroundIsCurrentThread() {
202 return background_thread_->BelongsToCurrentThread();
205 base::WeakPtr<InFlightBackendIO> GetWeakPtr();
208 virtual void OnOperationComplete(BackgroundIO* operation,
209 bool cancel) OVERRIDE;
212 void PostOperation(BackendIO* operation);
214 BackendImpl* backend_;
215 scoped_refptr<base::MessageLoopProxy> background_thread_;
216 base::WeakPtrFactory<InFlightBackendIO> ptr_factory_;
218 DISALLOW_COPY_AND_ASSIGN(InFlightBackendIO);
221 } // namespace disk_cache
223 #endif // NET_DISK_CACHE_IN_FLIGHT_BACKEND_IO_H_