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_BLOCKFILE_IN_FLIGHT_BACKEND_IO_H_
6 #define NET_DISK_CACHE_BLOCKFILE_IN_FLIGHT_BACKEND_IO_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/time/time.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/io_buffer.h"
16 #include "net/disk_cache/blockfile/in_flight_io.h"
17 #include "net/disk_cache/blockfile/rankings.h"
19 namespace disk_cache
{
25 // This class represents a single asynchronous disk cache IO operation while it
26 // is being bounced between threads.
27 class BackendIO
: public BackgroundIO
{
29 BackendIO(InFlightIO
* controller
, BackendImpl
* backend
,
30 const net::CompletionCallback
& callback
);
32 // Runs the actual operation on the background thread.
33 void ExecuteOperation();
35 // Callback implementation.
36 void OnIOComplete(int result
);
38 // Called when we are finishing this operation. If |cancel| is true, the user
39 // callback will not be invoked.
40 void OnDone(bool cancel
);
42 // Returns true if this operation is directed to an entry (vs. the backend).
43 bool IsEntryOperation();
45 net::CompletionCallback
callback() const { return callback_
; }
47 // Grabs an extra reference of entry_.
48 void ReferenceEntry();
50 // The operations we proxy:
52 void OpenEntry(const std::string
& key
, Entry
** entry
);
53 void CreateEntry(const std::string
& key
, Entry
** entry
);
54 void DoomEntry(const std::string
& key
);
55 void DoomAllEntries();
56 void DoomEntriesBetween(const base::Time initial_time
,
57 const base::Time end_time
);
58 void DoomEntriesSince(const base::Time initial_time
);
59 void OpenNextEntry(Rankings::Iterator
* iterator
, Entry
** next_entry
);
60 void EndEnumeration(scoped_ptr
<Rankings::Iterator
> iterator
);
61 void OnExternalCacheHit(const std::string
& key
);
62 void CloseEntryImpl(EntryImpl
* entry
);
63 void DoomEntryImpl(EntryImpl
* entry
);
64 void FlushQueue(); // Dummy operation.
65 void RunTask(const base::Closure
& task
);
66 void ReadData(EntryImpl
* entry
, int index
, int offset
, net::IOBuffer
* buf
,
68 void WriteData(EntryImpl
* entry
, int index
, int offset
, net::IOBuffer
* buf
,
69 int buf_len
, bool truncate
);
70 void ReadSparseData(EntryImpl
* entry
, int64 offset
, net::IOBuffer
* buf
,
72 void WriteSparseData(EntryImpl
* entry
, int64 offset
, net::IOBuffer
* buf
,
74 void GetAvailableRange(EntryImpl
* entry
, int64 offset
, int len
, int64
* start
);
75 void CancelSparseIO(EntryImpl
* entry
);
76 void ReadyForSparseIO(EntryImpl
* entry
);
79 // There are two types of operations to proxy: regular backend operations are
80 // executed sequentially (queued by the message loop). On the other hand,
81 // operations targeted to a given entry can be long lived and support multiple
82 // simultaneous users (multiple reads or writes to the same entry), and they
83 // are subject to throttling, so we keep an explicit queue.
95 OP_ON_EXTERNAL_CACHE_HIT
,
110 ~BackendIO() override
;
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_
;
130 Rankings::Iterator
* iterator_
;
131 scoped_ptr
<Rankings::Iterator
> scoped_iterator_
;
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
{
150 BackendImpl
* backend
,
151 const scoped_refptr
<base::SingleThreadTaskRunner
>& background_thread
);
152 ~InFlightBackendIO() override
;
154 // Proxied operations.
155 void Init(const net::CompletionCallback
& callback
);
156 void OpenEntry(const std::string
& key
, Entry
** entry
,
157 const net::CompletionCallback
& callback
);
158 void CreateEntry(const std::string
& key
, Entry
** entry
,
159 const net::CompletionCallback
& callback
);
160 void DoomEntry(const std::string
& key
,
161 const net::CompletionCallback
& callback
);
162 void DoomAllEntries(const net::CompletionCallback
& callback
);
163 void DoomEntriesBetween(const base::Time initial_time
,
164 const base::Time end_time
,
165 const net::CompletionCallback
& callback
);
166 void DoomEntriesSince(const base::Time initial_time
,
167 const net::CompletionCallback
& callback
);
168 void OpenNextEntry(Rankings::Iterator
* iterator
, Entry
** next_entry
,
169 const net::CompletionCallback
& callback
);
170 void EndEnumeration(scoped_ptr
<Rankings::Iterator
> iterator
);
171 void OnExternalCacheHit(const std::string
& key
);
172 void CloseEntryImpl(EntryImpl
* entry
);
173 void DoomEntryImpl(EntryImpl
* entry
);
174 void FlushQueue(const net::CompletionCallback
& callback
);
175 void RunTask(const base::Closure
& task
,
176 const net::CompletionCallback
& callback
);
177 void ReadData(EntryImpl
* entry
, int index
, int offset
, net::IOBuffer
* buf
,
178 int buf_len
, const net::CompletionCallback
& callback
);
180 EntryImpl
* entry
, int index
, int offset
, net::IOBuffer
* buf
,
181 int buf_len
, bool truncate
, const net::CompletionCallback
& callback
);
182 void ReadSparseData(EntryImpl
* entry
, int64 offset
, net::IOBuffer
* buf
,
183 int buf_len
, const net::CompletionCallback
& callback
);
184 void WriteSparseData(EntryImpl
* entry
, int64 offset
, net::IOBuffer
* buf
,
185 int buf_len
, const net::CompletionCallback
& callback
);
186 void GetAvailableRange(EntryImpl
* entry
, int64 offset
, int len
, int64
* start
,
187 const net::CompletionCallback
& callback
);
188 void CancelSparseIO(EntryImpl
* entry
);
189 void ReadyForSparseIO(EntryImpl
* entry
,
190 const net::CompletionCallback
& callback
);
192 // Blocks until all operations are cancelled or completed.
193 void WaitForPendingIO();
195 scoped_refptr
<base::SingleThreadTaskRunner
> background_thread() {
196 return background_thread_
;
199 // Returns true if the current thread is the background thread.
200 bool BackgroundIsCurrentThread() {
201 return background_thread_
->RunsTasksOnCurrentThread();
204 base::WeakPtr
<InFlightBackendIO
> GetWeakPtr();
207 void OnOperationComplete(BackgroundIO
* operation
, bool cancel
) override
;
210 void PostOperation(BackendIO
* operation
);
212 BackendImpl
* backend_
;
213 scoped_refptr
<base::SingleThreadTaskRunner
> background_thread_
;
214 base::WeakPtrFactory
<InFlightBackendIO
> ptr_factory_
;
216 DISALLOW_COPY_AND_ASSIGN(InFlightBackendIO
);
219 } // namespace disk_cache
221 #endif // NET_DISK_CACHE_BLOCKFILE_IN_FLIGHT_BACKEND_IO_H_