OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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_IN_FLIGHT_BACKEND_IO_H_ |
| 6 #define NET_DISK_CACHE_IN_FLIGHT_BACKEND_IO_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/message_loop_proxy.h" |
| 11 #include "net/base/completion_callback.h" |
| 12 #include "net/base/io_buffer.h" |
| 13 #include "net/disk_cache/entry_impl.h" |
| 14 #include "net/disk_cache/in_flight_io.h" |
| 15 |
| 16 namespace disk_cache { |
| 17 |
| 18 class BackendImpl; |
| 19 |
| 20 // This class represents a single asynchronous disk cache IO operation while it |
| 21 // is being bounced between threads. |
| 22 class BackendIO : public BackgroundIO { |
| 23 public: |
| 24 BackendIO(InFlightIO* controller, BackendImpl* backend, |
| 25 net::CompletionCallback* callback); |
| 26 |
| 27 // Runs the actual operation on the background thread. |
| 28 void ExecuteOperation(); |
| 29 |
| 30 // Callback implementation. |
| 31 void OnIOComplete(int result); |
| 32 |
| 33 // Returns true if this operation is directed to an entry (vs. the backend). |
| 34 bool IsEntryOperation(); |
| 35 |
| 36 net::CompletionCallback* callback() { return callback_; } |
| 37 |
| 38 void ReleaseEntry(); |
| 39 |
| 40 // The operations we proxy: |
| 41 void Init(); |
| 42 void OpenEntry(const std::string& key, Entry** entry); |
| 43 void CreateEntry(const std::string& key, Entry** entry); |
| 44 void DoomEntry(const std::string& key); |
| 45 void DoomAllEntries(); |
| 46 void DoomEntriesBetween(const base::Time initial_time, |
| 47 const base::Time end_time); |
| 48 void DoomEntriesSince(const base::Time initial_time); |
| 49 void OpenNextEntry(void** iter, Entry** next_entry); |
| 50 void OpenPrevEntry(void** iter, Entry** prev_entry); |
| 51 void EndEnumeration(void* iterator); |
| 52 void CloseEntryImpl(EntryImpl* entry); |
| 53 void DoomEntryImpl(EntryImpl* entry); |
| 54 void FlushQueue(); // Dummy operation. |
| 55 void ReadData(EntryImpl* entry, int index, int offset, net::IOBuffer* buf, |
| 56 int buf_len); |
| 57 void WriteData(EntryImpl* entry, int index, int offset, net::IOBuffer* buf, |
| 58 int buf_len, bool truncate); |
| 59 void ReadSparseData(EntryImpl* entry, int64 offset, net::IOBuffer* buf, |
| 60 int buf_len); |
| 61 void WriteSparseData(EntryImpl* entry, int64 offset, net::IOBuffer* buf, |
| 62 int buf_len); |
| 63 void GetAvailableRange(EntryImpl* entry, int64 offset, int len, int64* start); |
| 64 void CancelSparseIO(EntryImpl* entry); |
| 65 void ReadyForSparseIO(EntryImpl* entry); |
| 66 |
| 67 private: |
| 68 // There are two types of operations to proxy: regular backend operations are |
| 69 // queued so that we don't have more than one operation going on at the same |
| 70 // time (for instance opening an entry and creating the same entry). On the |
| 71 // other hand, operations targeted to a given entry can be long lived and |
| 72 // support multiple simultaneous users (multiple reads or writes to the same |
| 73 // entry), so they are not queued, just posted to the worker thread as they |
| 74 // come. |
| 75 enum Operation { |
| 76 OP_NONE = 0, |
| 77 OP_INIT, |
| 78 OP_OPEN, |
| 79 OP_CREATE, |
| 80 OP_DOOM, |
| 81 OP_DOOM_ALL, |
| 82 OP_DOOM_BETWEEN, |
| 83 OP_DOOM_SINCE, |
| 84 OP_OPEN_NEXT, |
| 85 OP_OPEN_PREV, |
| 86 OP_END_ENUMERATION, |
| 87 OP_CLOSE_ENTRY, |
| 88 OP_DOOM_ENTRY, |
| 89 OP_FLUSH_QUEUE, |
| 90 OP_MAX_BACKEND, |
| 91 OP_READ, |
| 92 OP_WRITE, |
| 93 OP_READ_SPARSE, |
| 94 OP_WRITE_SPARSE, |
| 95 OP_GET_RANGE, |
| 96 OP_CANCEL_IO, |
| 97 OP_IS_READY |
| 98 }; |
| 99 |
| 100 ~BackendIO() {} |
| 101 |
| 102 void ExecuteBackendOperation(); |
| 103 void ExecuteEntryOperation(); |
| 104 |
| 105 BackendImpl* backend_; |
| 106 net::CompletionCallback* callback_; |
| 107 Operation operation_; |
| 108 net::CompletionCallbackImpl<BackendIO> my_callback_; |
| 109 |
| 110 // The arguments of all the operations we proxy: |
| 111 std::string key_; |
| 112 Entry** entry_ptr_; |
| 113 base::Time initial_time_; |
| 114 base::Time end_time_; |
| 115 void** iter_ptr_; |
| 116 void* iter_; |
| 117 EntryImpl* entry_; |
| 118 int index_; |
| 119 int offset_; |
| 120 scoped_refptr<net::IOBuffer> buf_; |
| 121 int buf_len_; |
| 122 bool truncate_; |
| 123 int64 offset64_; |
| 124 int64* start_; |
| 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(BackendIO); |
| 127 }; |
| 128 |
| 129 // The specialized controller that keeps track of current operations. |
| 130 class InFlightBackendIO : public InFlightIO { |
| 131 public: |
| 132 InFlightBackendIO(BackendImpl* backend, |
| 133 base::MessageLoopProxy* background_thread) |
| 134 : backend_(backend), background_thread_(background_thread) {} |
| 135 ~InFlightBackendIO() {} |
| 136 |
| 137 // The operations we proxy: |
| 138 void Init(net::CompletionCallback* callback); |
| 139 void OpenEntry(const std::string& key, Entry** entry, |
| 140 net::CompletionCallback* callback); |
| 141 void CreateEntry(const std::string& key, Entry** entry, |
| 142 net::CompletionCallback* callback); |
| 143 void DoomEntry(const std::string& key, net::CompletionCallback* callback); |
| 144 void DoomAllEntries(net::CompletionCallback* callback); |
| 145 void DoomEntriesBetween(const base::Time initial_time, |
| 146 const base::Time end_time, |
| 147 net::CompletionCallback* callback); |
| 148 void DoomEntriesSince(const base::Time initial_time, |
| 149 net::CompletionCallback* callback); |
| 150 void OpenNextEntry(void** iter, Entry** next_entry, |
| 151 net::CompletionCallback* callback); |
| 152 void OpenPrevEntry(void** iter, Entry** prev_entry, |
| 153 net::CompletionCallback* callback); |
| 154 void EndEnumeration(void* iterator); |
| 155 void CloseEntryImpl(EntryImpl* entry); |
| 156 void DoomEntryImpl(EntryImpl* entry); |
| 157 void FlushQueue(net::CompletionCallback* callback); |
| 158 void ReadData(EntryImpl* entry, int index, int offset, net::IOBuffer* buf, |
| 159 int buf_len, net::CompletionCallback* callback); |
| 160 void WriteData(EntryImpl* entry, int index, int offset, net::IOBuffer* buf, |
| 161 int buf_len, bool truncate, net::CompletionCallback* callback); |
| 162 void ReadSparseData(EntryImpl* entry, int64 offset, net::IOBuffer* buf, |
| 163 int buf_len, net::CompletionCallback* callback); |
| 164 void WriteSparseData(EntryImpl* entry, int64 offset, net::IOBuffer* buf, |
| 165 int buf_len, net::CompletionCallback* callback); |
| 166 void GetAvailableRange(EntryImpl* entry, int64 offset, int len, int64* start, |
| 167 net::CompletionCallback* callback); |
| 168 void CancelSparseIO(EntryImpl* entry); |
| 169 void ReadyForSparseIO(EntryImpl* entry, net::CompletionCallback* callback); |
| 170 |
| 171 // Blocks until all operations are cancelled or completed. |
| 172 void WaitForPendingIO(); |
| 173 |
| 174 scoped_refptr<base::MessageLoopProxy> background_thread() { |
| 175 return background_thread_; |
| 176 } |
| 177 |
| 178 // Returns true if the current thread is the background thread. |
| 179 bool BackgroundIsCurrentThread() { |
| 180 return background_thread_->BelongsToCurrentThread(); |
| 181 } |
| 182 |
| 183 protected: |
| 184 virtual void OnOperationComplete(BackgroundIO* operation, bool cancel); |
| 185 |
| 186 private: |
| 187 typedef std::list<scoped_refptr<BackendIO> > OperationList; |
| 188 void QueueOperation(BackendIO* operation); |
| 189 void PostOperation(BackendIO* operation); |
| 190 |
| 191 BackendImpl* backend_; |
| 192 scoped_refptr<base::MessageLoopProxy> background_thread_; |
| 193 OperationList pending_ops_; // The list of operations to be posted. |
| 194 |
| 195 DISALLOW_COPY_AND_ASSIGN(InFlightBackendIO); |
| 196 }; |
| 197 |
| 198 } // namespace disk_cache |
| 199 |
| 200 #endif // NET_DISK_CACHE_IN_FLIGHT_BACKEND_IO_H_ |
OLD | NEW |