Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2059)

Side by Side Diff: net/http/http_cache.h

Issue 2519473002: Fixes the cache lock issue. (Closed)
Patch Set: Feedback addressed Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/load_states_list.h ('k') | net/http/http_cache.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file declares a HttpTransactionFactory implementation that can be 5 // This file declares a HttpTransactionFactory implementation that can be
6 // layered on top of another HttpTransactionFactory to add HTTP caching. The 6 // layered on top of another HttpTransactionFactory to add HTTP caching. The
7 // caching logic follows RFC 7234 (any exceptions are called out in the code). 7 // caching logic follows RFC 7234 (any exceptions are called out in the code).
8 // 8 //
9 // The HttpCache takes a disk_cache::Backend as a parameter, and uses that for 9 // The HttpCache takes a disk_cache::Backend as a parameter, and uses that for
10 // the cache storage. 10 // the cache storage.
11 // 11 //
12 // See HttpTransactionFactory and HttpTransaction for more details. 12 // See HttpTransactionFactory and HttpTransaction for more details.
13 13
14 #ifndef NET_HTTP_HTTP_CACHE_H_ 14 #ifndef NET_HTTP_HTTP_CACHE_H_
15 #define NET_HTTP_HTTP_CACHE_H_ 15 #define NET_HTTP_HTTP_CACHE_H_
16 16
17 #include <list> 17 #include <list>
18 #include <map> 18 #include <map>
19 #include <memory> 19 #include <memory>
20 #include <string> 20 #include <string>
21 #include <unordered_map> 21 #include <unordered_map>
22 #include <unordered_set>
22 23
23 #include "base/files/file_path.h" 24 #include "base/files/file_path.h"
24 #include "base/macros.h" 25 #include "base/macros.h"
25 #include "base/memory/weak_ptr.h" 26 #include "base/memory/weak_ptr.h"
26 #include "base/threading/non_thread_safe.h" 27 #include "base/threading/non_thread_safe.h"
27 #include "base/time/clock.h" 28 #include "base/time/clock.h"
28 #include "base/time/time.h" 29 #include "base/time/time.h"
29 #include "net/base/cache_type.h" 30 #include "net/base/cache_type.h"
30 #include "net/base/completion_callback.h" 31 #include "net/base/completion_callback.h"
31 #include "net/base/load_states.h" 32 #include "net/base/load_states.h"
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 } 196 }
196 197
197 // Causes all transactions created after this point to generate a failure 198 // Causes all transactions created after this point to generate a failure
198 // when attempting to conditionalize a network request. 199 // when attempting to conditionalize a network request.
199 void FailConditionalizationForTest() { 200 void FailConditionalizationForTest() {
200 fail_conditionalization_for_test_ = true; 201 fail_conditionalization_for_test_ = true;
201 } 202 }
202 203
203 // HttpTransactionFactory implementation: 204 // HttpTransactionFactory implementation:
204 int CreateTransaction(RequestPriority priority, 205 int CreateTransaction(RequestPriority priority,
205 std::unique_ptr<HttpTransaction>* trans) override; 206 std::unique_ptr<HttpTransaction>* transaction) override;
206 HttpCache* GetCache() override; 207 HttpCache* GetCache() override;
207 HttpNetworkSession* GetSession() override; 208 HttpNetworkSession* GetSession() override;
208 209
209 base::WeakPtr<HttpCache> GetWeakPtr() { return weak_factory_.GetWeakPtr(); } 210 base::WeakPtr<HttpCache> GetWeakPtr() { return weak_factory_.GetWeakPtr(); }
210 211
211 // Resets the network layer to allow for tests that probe 212 // Resets the network layer to allow for tests that probe
212 // network changes (e.g. host unreachable). The old network layer is 213 // network changes (e.g. host unreachable). The old network layer is
213 // returned to allow for filter patterns that only intercept 214 // returned to allow for filter patterns that only intercept
214 // some creation requests. Note ownership exchange. 215 // some creation requests. Note ownership exchange.
215 std::unique_ptr<HttpTransactionFactory> 216 std::unique_ptr<HttpTransactionFactory>
216 SetHttpNetworkTransactionFactoryForTesting( 217 SetHttpNetworkTransactionFactoryForTesting(
217 std::unique_ptr<HttpTransactionFactory> new_network_layer); 218 std::unique_ptr<HttpTransactionFactory> new_network_layer);
218 219
219 private: 220 private:
220 // Types -------------------------------------------------------------------- 221 // Types --------------------------------------------------------------------
221 222
222 // Disk cache entry data indices. 223 // Disk cache entry data indices.
223 enum { 224 enum {
224 kResponseInfoIndex = 0, 225 kResponseInfoIndex = 0,
225 kResponseContentIndex, 226 kResponseContentIndex,
226 kMetadataIndex, 227 kMetadataIndex,
227 228
228 // Must remain at the end of the enum. 229 // Must remain at the end of the enum.
229 kNumCacheEntryDataIndices 230 kNumCacheEntryDataIndices
230 }; 231 };
231 232
233 class DataAccess;
234 class SharedWriters;
235 class Transaction;
236 using TransactionList = std::list<Transaction*>;
237 using TransactionSet = std::unordered_set<Transaction*>;
232 class MetadataWriter; 238 class MetadataWriter;
233 class QuicServerInfoFactoryAdaptor; 239 class QuicServerInfoFactoryAdaptor;
234 class Transaction;
235 class WorkItem; 240 class WorkItem;
236 friend class Transaction;
237 friend class ViewCacheHelper; 241 friend class ViewCacheHelper;
238 struct PendingOp; // Info for an entry under construction. 242 struct PendingOp; // Info for an entry under construction.
239 243
240 typedef std::list<Transaction*> TransactionList;
241 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; 244 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList;
242 245
243 struct ActiveEntry { 246 struct ActiveEntry {
244 explicit ActiveEntry(disk_cache::Entry* entry); 247 explicit ActiveEntry(disk_cache::Entry* entry);
245 ~ActiveEntry(); 248 ~ActiveEntry();
246 249
247 disk_cache::Entry* disk_entry; 250 disk_cache::Entry* disk_entry;
251 // Either writer or shared_writers will be non-null at any point in time.
248 Transaction* writer; 252 Transaction* writer;
249 TransactionList readers; 253 std::unique_ptr<SharedWriters> shared_writers;
254 TransactionSet readers;
250 TransactionList pending_queue; 255 TransactionList pending_queue;
251 bool will_process_pending_queue; 256 bool will_process_pending_queue;
252 bool doomed; 257 bool doomed;
253 }; 258 };
254 259
255 using ActiveEntriesMap = 260 using ActiveEntriesMap =
256 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; 261 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>;
257 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; 262 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>;
258 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; 263 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>;
259 using PlaybackCacheMap = std::unordered_map<std::string, int>; 264 using PlaybackCacheMap = std::unordered_map<std::string, int>;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 // Closes a previously doomed entry. 300 // Closes a previously doomed entry.
296 void FinalizeDoomedEntry(ActiveEntry* entry); 301 void FinalizeDoomedEntry(ActiveEntry* entry);
297 302
298 // Returns an entry that is currently in use and not doomed, or NULL. 303 // Returns an entry that is currently in use and not doomed, or NULL.
299 ActiveEntry* FindActiveEntry(const std::string& key); 304 ActiveEntry* FindActiveEntry(const std::string& key);
300 305
301 // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk 306 // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk
302 // cache entry. 307 // cache entry.
303 ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry); 308 ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry);
304 309
305 // Deletes an ActiveEntry. 310 // Deletes an ActiveEntry. Expects writer to be nullptr and containers to be
311 // empty: shared_writers, pending_queue and readers.
306 void DeactivateEntry(ActiveEntry* entry); 312 void DeactivateEntry(ActiveEntry* entry);
307 313
308 // Deletes an ActiveEntry using an exhaustive search. 314 // Deletes an ActiveEntry using an exhaustive search.
309 void SlowDeactivateEntry(ActiveEntry* entry); 315 void SlowDeactivateEntry(ActiveEntry* entry);
310 316
311 // Returns the PendingOp for the desired |key|. If an entry is not under 317 // Returns the PendingOp for the desired |key|. If an entry is not under
312 // construction already, a new PendingOp structure is created. 318 // construction already, a new PendingOp structure is created.
313 PendingOp* GetPendingOp(const std::string& key); 319 PendingOp* GetPendingOp(const std::string& key);
314 320
315 // Deletes a PendingOp. 321 // Deletes a PendingOp.
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might 391 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might
386 // be possible to simplify it using either base::Owned() or base::Passed() 392 // be possible to simplify it using either base::Owned() or base::Passed()
387 // with the callback. 393 // with the callback.
388 static void OnPendingOpComplete(const base::WeakPtr<HttpCache>& cache, 394 static void OnPendingOpComplete(const base::WeakPtr<HttpCache>& cache,
389 PendingOp* pending_op, 395 PendingOp* pending_op,
390 int result); 396 int result);
391 397
392 // Processes the backend creation notification. 398 // Processes the backend creation notification.
393 void OnBackendCreated(int result, PendingOp* pending_op); 399 void OnBackendCreated(int result, PendingOp* pending_op);
394 400
401 // Destroys the entry and any pending queue transactions' callbacks are
402 // invoked with ERR_CACHE_RACE that restarts their state machine.
403 void DestroyEntryRestartPendingQueue(ActiveEntry* entry);
404
405 // Dooms the entry and any pending queue transactions' callbacks are
406 // invoked with ERR_CACHE_RACE that restarts their state machine.
407 void DoomEntryRestartPendingQueue(const std::string& key, ActiveEntry* entry);
408
409 // Writes response info to entry.
410 int WriteResponseInfo(ActiveEntry* entry,
411 const HttpResponseInfo* response,
412 CompletionCallback& callback,
413 bool truncated,
414 int* io_buf_len);
415
416 // Checks if the response is completed based on content length.
417 bool IsResponseCompleted(const ActiveEntry* entry,
418 const HttpResponseInfo* response);
419
420 // Returns true if we should bother attempting to resume this request if it is
421 // aborted while in progress. If |has_data| is true, the size of the stored
422 // data is considered for the result.
423 bool CanResumeEntry(bool has_data,
424 const std::string& method,
425 const HttpResponseInfo* response,
426 ActiveEntry* entry);
427
428 // Response data completely written to the cache successfully, or
429 // Response data could not be written to the cache successfully, or
430 // Response data could not be read from the network successfully.
431 // Success is true if the entry was marked as truncated successfully.
432 // destroyed is set to true when the calling SharedWiters object is reset.
433 void ResponseDoneSharedWriters(ActiveEntry* entry,
434 bool success,
435 bool* destroyed);
436
437 // Resets SharedWriters if entry contains an empty SharedWriters object and
438 // calls ProcessPendingQueue.
439 void ResetSharedWritersProcessPendingQueue(ActiveEntry* entry);
440 void ResetSharedWriters(ActiveEntry* entry);
441
442 void RemovedSharedWriterTransaction(Transaction* transaction,
443 ActiveEntry* entry);
444
395 // Variables ---------------------------------------------------------------- 445 // Variables ----------------------------------------------------------------
396 446
397 NetLog* net_log_; 447 NetLog* net_log_;
398 448
399 // Used when lazily constructing the disk_cache_. 449 // Used when lazily constructing the disk_cache_.
400 std::unique_ptr<BackendFactory> backend_factory_; 450 std::unique_ptr<BackendFactory> backend_factory_;
401 bool building_backend_; 451 bool building_backend_;
402 bool bypass_lock_for_test_; 452 bool bypass_lock_for_test_;
403 bool fail_conditionalization_for_test_; 453 bool fail_conditionalization_for_test_;
404 454
(...skipping 18 matching lines...) Expand all
423 std::unique_ptr<base::Clock> clock_; 473 std::unique_ptr<base::Clock> clock_;
424 474
425 base::WeakPtrFactory<HttpCache> weak_factory_; 475 base::WeakPtrFactory<HttpCache> weak_factory_;
426 476
427 DISALLOW_COPY_AND_ASSIGN(HttpCache); 477 DISALLOW_COPY_AND_ASSIGN(HttpCache);
428 }; 478 };
429 479
430 } // namespace net 480 } // namespace net
431 481
432 #endif // NET_HTTP_HTTP_CACHE_H_ 482 #endif // NET_HTTP_HTTP_CACHE_H_
OLDNEW
« no previous file with comments | « net/base/load_states_list.h ('k') | net/http/http_cache.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698