OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 #include "net/disk_cache/pending_create_entry.h" |
| 6 |
| 7 #include "base/time.h" |
| 8 #include "net/base/net_errors.h" |
| 9 #include "net/disk_cache/disk_cache.h" |
| 10 |
| 11 namespace disk_cache { |
| 12 |
| 13 namespace { |
| 14 |
| 15 // A PendingEntry takes the place of our yet to be created Entry when we perform |
| 16 // a PendingCreateEntry. After a successful create, the new Entry will switch |
| 17 // and takes it place. The PendingEntry will set the output parameter |failed| |
| 18 // to true if it receives any operation we cannot proxy on the new entry. |
| 19 class PendingEntry : public Entry { |
| 20 public: |
| 21 PendingEntry(const std::string& key, bool* failed) : key_(key), |
| 22 failed_(failed) { |
| 23 *failed_ = false; |
| 24 } |
| 25 virtual ~PendingEntry() { |
| 26 } |
| 27 |
| 28 // From disk_cache::Entry: |
| 29 virtual void Close() OVERRIDE { |
| 30 // We do not self delete here, because as a member of ProxyEntry we will be |
| 31 // delete with it. |
| 32 } |
| 33 virtual void Doom() OVERRIDE { |
| 34 } |
| 35 virtual int WriteData(int index, |
| 36 int offset, |
| 37 IOBuffer* buf, |
| 38 int buf_len, |
| 39 const CompletionCallback& callback, |
| 40 bool truncate) OVERRIDE { |
| 41 VLOG(0) << "NETWORK won for " << key_; |
| 42 // If we receive WriteData, that's because the create on the disk cache did |
| 43 // not complete in time to receive data. We fail, which will force |
| 44 // transactions into pass through mode. |
| 45 // TODO(gavinp): Investigate the benefit of buffering here. |
| 46 *failed_ = true; |
| 47 return net::ERR_CACHE_CREATE_RACE_FAILED; |
| 48 } |
| 49 virtual std::string GetKey() const OVERRIDE { |
| 50 return key_; |
| 51 } |
| 52 |
| 53 // The rest of these operations aren't expected to ever be called, but are |
| 54 // required for a complete implementation of the abstract base class. |
| 55 virtual base::Time GetLastUsed() const OVERRIDE { |
| 56 NOTREACHED(); |
| 57 return base::Time(); |
| 58 } |
| 59 virtual base::Time GetLastModified() const OVERRIDE { |
| 60 NOTREACHED(); |
| 61 return base::Time(); |
| 62 } |
| 63 virtual int32 GetDataSize(int index) const OVERRIDE { |
| 64 NOTREACHED(); |
| 65 return 0; |
| 66 } |
| 67 virtual int ReadData(int index, |
| 68 int offset, |
| 69 IOBuffer* buf, |
| 70 int buf_len, |
| 71 const CompletionCallback& callback) OVERRIDE { |
| 72 NOTREACHED(); |
| 73 return net::ERR_CACHE_CREATE_RACE_FAILED; |
| 74 } |
| 75 virtual int ReadSparseData(int64 offset, |
| 76 IOBuffer* buf, |
| 77 int buf_len, |
| 78 const CompletionCallback& callback) OVERRIDE { |
| 79 NOTREACHED(); |
| 80 return net::ERR_CACHE_CREATE_RACE_FAILED; |
| 81 } |
| 82 virtual int WriteSparseData(int64 offset, |
| 83 IOBuffer* buf, |
| 84 int buf_len, |
| 85 const CompletionCallback& callback) OVERRIDE { |
| 86 NOTREACHED(); |
| 87 return net::ERR_CACHE_CREATE_RACE_FAILED; |
| 88 } |
| 89 virtual int GetAvailableRange(int64 offset, |
| 90 int len, |
| 91 int64* start, |
| 92 const CompletionCallback& callback) OVERRIDE { |
| 93 NOTREACHED(); |
| 94 return net::ERR_CACHE_CREATE_RACE_FAILED; |
| 95 } |
| 96 virtual bool CouldBeSparse() const OVERRIDE { |
| 97 NOTREACHED(); |
| 98 return false; |
| 99 } |
| 100 virtual void CancelSparseIO() OVERRIDE { |
| 101 NOTREACHED(); |
| 102 } |
| 103 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE { |
| 104 return net::ERR_CACHE_CREATE_RACE_FAILED; |
| 105 } |
| 106 |
| 107 private: |
| 108 std::string key_; |
| 109 |
| 110 // |failed_| is set to true iff we receive an operation we cannot proxy. |
| 111 bool* failed_; |
| 112 }; |
| 113 |
| 114 // The ProxyEntry initially proxies for a PendingEntry, and once our CreateEntry |
| 115 // is complete, it swaps it in place if possible. |
| 116 class ProxyEntry : public Entry { |
| 117 public: |
| 118 explicit ProxyEntry(const std::string& key); |
| 119 virtual ~ProxyEntry(); |
| 120 |
| 121 Entry* ReleaseCreatedEntry(); |
| 122 |
| 123 Entry** to_create_entry_ptr() { return &to_create_entry_; } |
| 124 const net::CompletionCallback& io_callback() const { return io_callback_; } |
| 125 |
| 126 // Entry interface. |
| 127 virtual void Doom() OVERRIDE { |
| 128 proxy_entry_->Doom(); |
| 129 } |
| 130 virtual void Close() OVERRIDE { |
| 131 proxy_entry_->Close(); |
| 132 proxy_entry_ = NULL; |
| 133 // Normally, for entries that are created uniquely per user we would self |
| 134 // delete here. However, |to_create_entry_| is still referenced if we have |
| 135 // not received our io_callback_ yet, so we must wait for the callback to |
| 136 // self delete. Note that this isn't solved by using a WeakPtr in the |
| 137 // callback, since |to_create_entry_| is set on the Cache thread outside |
| 138 // of the callback. |
| 139 if (io_callback_was_received_) |
| 140 delete this; |
| 141 } |
| 142 virtual std::string GetKey() const OVERRIDE { |
| 143 return proxy_entry_->GetKey(); |
| 144 } |
| 145 virtual base::Time GetLastUsed() const OVERRIDE { |
| 146 return proxy_entry_->GetLastUsed(); |
| 147 } |
| 148 virtual base::Time GetLastModified() const OVERRIDE { |
| 149 return proxy_entry_->GetLastModified(); |
| 150 } |
| 151 virtual int32 GetDataSize(int index) const OVERRIDE { |
| 152 return proxy_entry_->GetDataSize(index); |
| 153 } |
| 154 virtual int ReadData(int index, int offset, IOBuffer* buf, int buf_len, |
| 155 const CompletionCallback& callback) OVERRIDE { |
| 156 return proxy_entry_->ReadData(index, offset, buf, buf_len, callback); |
| 157 } |
| 158 virtual int WriteData(int index, int offset, IOBuffer* buf, int buf_len, |
| 159 const CompletionCallback& callback, |
| 160 bool truncate) OVERRIDE { |
| 161 return proxy_entry_->WriteData(index, offset, buf, buf_len, callback, |
| 162 truncate); |
| 163 } |
| 164 virtual int ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, |
| 165 const CompletionCallback& callback) OVERRIDE { |
| 166 return proxy_entry_->ReadSparseData(offset, buf, buf_len, callback); |
| 167 } |
| 168 virtual int WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, |
| 169 const CompletionCallback& callback) OVERRIDE { |
| 170 return proxy_entry_->WriteSparseData(offset, buf, buf_len, callback); |
| 171 } |
| 172 virtual int GetAvailableRange(int64 offset, int len, int64* start, |
| 173 const CompletionCallback& callback) OVERRIDE { |
| 174 return proxy_entry_->GetAvailableRange(offset, len, start, callback); |
| 175 } |
| 176 virtual bool CouldBeSparse() const OVERRIDE { |
| 177 return proxy_entry_->CouldBeSparse(); |
| 178 } |
| 179 virtual void CancelSparseIO() OVERRIDE { |
| 180 proxy_entry_->CancelSparseIO(); |
| 181 } |
| 182 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE { |
| 183 return proxy_entry_->ReadyForSparseIO(callback); |
| 184 } |
| 185 |
| 186 private: |
| 187 void OnIOComplete(int result); |
| 188 |
| 189 std::string key_; |
| 190 |
| 191 // Initially false, |pending_entry_invalid_| is set to true if the |
| 192 // |pending_entry_| received an operation it could not proxy. |
| 193 bool pending_entry_invalid_; |
| 194 |
| 195 // Before we have created our entry, we use proxy to the pending issue. |
| 196 PendingEntry pending_entry_; |
| 197 |
| 198 bool io_callback_was_received_; |
| 199 |
| 200 // When our IO callback is called, |to_create_entry_| will contain our newly |
| 201 // created entry. |
| 202 Entry* to_create_entry_; |
| 203 |
| 204 // The entry that we are currently a proxy for. |
| 205 Entry* proxy_entry_; |
| 206 |
| 207 net::CompletionCallback io_callback_; |
| 208 }; |
| 209 |
| 210 ProxyEntry::ProxyEntry(const std::string& key) |
| 211 : key_(key), |
| 212 pending_entry_invalid_(false), |
| 213 pending_entry_(key, &pending_entry_invalid_), |
| 214 io_callback_was_received_(false), |
| 215 to_create_entry_(NULL), |
| 216 proxy_entry_(&pending_entry_), |
| 217 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( |
| 218 base::Bind(&ProxyEntry::OnIOComplete, base::Unretained(this)))) { |
| 219 } |
| 220 |
| 221 ProxyEntry::~ProxyEntry() { |
| 222 } |
| 223 |
| 224 Entry* ProxyEntry::ReleaseCreatedEntry() { |
| 225 Entry* to_return = to_create_entry_; |
| 226 to_create_entry_ = NULL; |
| 227 return to_return; |
| 228 } |
| 229 |
| 230 void ProxyEntry::OnIOComplete(int result) { |
| 231 DCHECK(!io_callback_was_received_); |
| 232 io_callback_was_received_ = true; |
| 233 if (!proxy_entry_) { |
| 234 // We are receiving an IO callback after we have already been closed. |
| 235 // Since |to_create_entry_| is no longer referenced by our pending Create |
| 236 // operation, we can now perform the self delete delayed from Close(). |
| 237 VLOG(0) << "Cleaning up after our entry..."; |
| 238 to_create_entry_->Doom(); |
| 239 to_create_entry_->Close(); |
| 240 to_create_entry_ = NULL; |
| 241 delete this; |
| 242 return; |
| 243 } |
| 244 VLOG(0) << "CACHE WON for " << key_; |
| 245 if (result == net::OK) { |
| 246 if (pending_entry_invalid_) { |
| 247 to_create_entry_->Doom(); |
| 248 to_create_entry_->Close(); |
| 249 to_create_entry_ = NULL; |
| 250 return; |
| 251 } |
| 252 // We have successfully created our entry before any invalid operations |
| 253 // occured. We can swap it in and become a pure proxy. |
| 254 proxy_entry_ = to_create_entry_; |
| 255 to_create_entry_ = NULL; |
| 256 } |
| 257 } |
| 258 |
| 259 } // namespace |
| 260 |
| 261 int PendingCreateEntry(Backend* backend, |
| 262 const std::string& key, |
| 263 Entry** entry) { |
| 264 VLOG(0) << "PendingCreateEntry"; |
| 265 ProxyEntry* proxy_entry = new ProxyEntry(key); |
| 266 int rv = backend->CreateEntry(key, proxy_entry->to_create_entry_ptr(), |
| 267 proxy_entry->io_callback()); |
| 268 VLOG(0) << "rv = " << rv; |
| 269 if (rv == net::ERR_IO_PENDING) { |
| 270 *entry = proxy_entry; |
| 271 return net::OK; |
| 272 } |
| 273 if (rv == net::OK) |
| 274 *entry = proxy_entry->ReleaseCreatedEntry(); |
| 275 return rv; |
| 276 } |
| 277 |
| 278 } // namespace disk_cache |
OLD | NEW |