Chromium Code Reviews| 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. | |
|
rvargas (doing something else)
2013/01/23 22:04:09
This object should handle the case and keep going
| |
| 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 delete this; | |
| 133 } | |
| 134 virtual std::string GetKey() const OVERRIDE { | |
| 135 return proxy_entry_->GetKey(); | |
| 136 } | |
| 137 virtual base::Time GetLastUsed() const OVERRIDE { | |
| 138 return proxy_entry_->GetLastUsed(); | |
| 139 } | |
| 140 virtual base::Time GetLastModified() const OVERRIDE { | |
| 141 return proxy_entry_->GetLastModified(); | |
| 142 } | |
| 143 virtual int32 GetDataSize(int index) const OVERRIDE { | |
| 144 return proxy_entry_->GetDataSize(index); | |
| 145 } | |
| 146 virtual int ReadData(int index, int offset, IOBuffer* buf, int buf_len, | |
| 147 const CompletionCallback& callback) OVERRIDE { | |
| 148 return proxy_entry_->ReadData(index, offset, buf, buf_len, callback); | |
| 149 } | |
| 150 virtual int WriteData(int index, int offset, IOBuffer* buf, int buf_len, | |
| 151 const CompletionCallback& callback, | |
| 152 bool truncate) OVERRIDE { | |
| 153 return proxy_entry_->WriteData(index, offset, buf, buf_len, callback, | |
| 154 truncate); | |
| 155 } | |
| 156 virtual int ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, | |
| 157 const CompletionCallback& callback) OVERRIDE { | |
| 158 return proxy_entry_->ReadSparseData(offset, buf, buf_len, callback); | |
| 159 } | |
| 160 virtual int WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, | |
| 161 const CompletionCallback& callback) OVERRIDE { | |
| 162 return proxy_entry_->WriteSparseData(offset, buf, buf_len, callback); | |
| 163 } | |
| 164 virtual int GetAvailableRange(int64 offset, int len, int64* start, | |
| 165 const CompletionCallback& callback) OVERRIDE { | |
| 166 return proxy_entry_->GetAvailableRange(offset, len, start, callback); | |
| 167 } | |
| 168 virtual bool CouldBeSparse() const OVERRIDE { | |
| 169 return proxy_entry_->CouldBeSparse(); | |
| 170 } | |
| 171 virtual void CancelSparseIO() OVERRIDE { | |
| 172 proxy_entry_->CancelSparseIO(); | |
| 173 } | |
| 174 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE { | |
| 175 return proxy_entry_->ReadyForSparseIO(callback); | |
| 176 } | |
| 177 | |
| 178 private: | |
| 179 void OnIOComplete(int result); | |
| 180 | |
| 181 std::string key_; | |
| 182 | |
| 183 // Initially false, |pending_entry_invalid_| is set to true if the | |
| 184 // |pending_entry_| received an operation it could not proxy. | |
| 185 bool pending_entry_invalid_; | |
| 186 | |
| 187 // Before we have created our entry, we use proxy to the pending issue. | |
| 188 PendingEntry pending_entry_; | |
| 189 | |
| 190 // When our IO callback is called, |to_create_entry_| will contain our newly | |
| 191 // created entry. | |
| 192 Entry* to_create_entry_; | |
| 193 | |
| 194 // The entry that we are currently a proxy for. | |
| 195 Entry* proxy_entry_; | |
| 196 | |
| 197 base::WeakPtrFactory<ProxyEntry> weak_factory_; | |
| 198 net::CompletionCallback io_callback_; | |
| 199 }; | |
| 200 | |
| 201 ProxyEntry::ProxyEntry(const std::string& key) | |
| 202 : key_(key), | |
| 203 pending_entry_invalid_(false), | |
| 204 pending_entry_(key, &pending_entry_invalid_), | |
| 205 to_create_entry_(NULL), | |
| 206 proxy_entry_(&pending_entry_), | |
| 207 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | |
| 208 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( | |
| 209 base::Bind(&ProxyEntry::OnIOComplete, | |
| 210 weak_factory_.GetWeakPtr()))) { | |
| 211 } | |
| 212 | |
| 213 ProxyEntry::~ProxyEntry() { | |
| 214 if (to_create_entry_) { | |
| 215 to_create_entry_->Doom(); | |
| 216 to_create_entry_->Close(); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 Entry* ProxyEntry::ReleaseCreatedEntry() { | |
| 221 Entry* to_return = to_create_entry_; | |
| 222 to_create_entry_ = NULL; | |
| 223 return to_return; | |
| 224 } | |
| 225 | |
| 226 void ProxyEntry::OnIOComplete(int result) { | |
| 227 VLOG(0) << "CACHE WON for " << key_; | |
| 228 if (result == net::OK) { | |
| 229 if (pending_entry_invalid_) { | |
| 230 to_create_entry_->Doom(); | |
| 231 to_create_entry_->Close(); | |
| 232 to_create_entry_ = NULL; | |
| 233 return; | |
| 234 } | |
| 235 // We have successfully created our entry before any invalid operations | |
| 236 // occured. We can swap it in and become a pure proxy. | |
| 237 proxy_entry_ = to_create_entry_; | |
| 238 to_create_entry_ = NULL; | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 } // namespace | |
| 243 | |
| 244 int PendingCreateEntry(Backend* backend, | |
| 245 const std::string& key, | |
| 246 Entry** entry) { | |
| 247 VLOG(0) << "PendingCreateEntry"; | |
| 248 ProxyEntry* proxy_entry = new ProxyEntry(key); | |
| 249 int rv = backend->CreateEntry(key, proxy_entry->to_create_entry_ptr(), | |
| 250 proxy_entry->io_callback()); | |
| 251 VLOG(0) << "rv = " << rv; | |
| 252 if (rv == net::ERR_IO_PENDING) { | |
| 253 *entry = proxy_entry; | |
| 254 return net::OK; | |
| 255 } | |
| 256 if (rv == net::OK) | |
| 257 *entry = proxy_entry->ReleaseCreatedEntry(); | |
| 258 return rv; | |
| 259 } | |
| 260 | |
| 261 } // namespace disk_cache | |
| OLD | NEW |