Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 CONTENT_BROWSER_LOADER_ASYNC_REVALIDATION_MANAGER_H_ | |
| 6 #define CONTENT_BROWSER_LOADER_ASYNC_REVALIDATION_MANAGER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 | |
| 14 class GURL; | |
| 15 struct ResourceHostMsg_Request; | |
| 16 | |
| 17 namespace net { | |
| 18 class URLRequest; | |
| 19 class HttpCache; | |
| 20 } | |
| 21 | |
| 22 namespace content { | |
| 23 | |
| 24 class AsyncRevalidationDriver; | |
| 25 class ResourceContext; | |
| 26 class ResourceScheduler; | |
| 27 | |
| 28 // One instance of this class manages all active AsyncRevalidationDriver objects | |
| 29 // for all profiles. It is created by and owned by | |
| 30 // ResourceDispatcherHostImpl. It also implements the creation of a new | |
| 31 // net::URLRequest and AsyncRevalidationDriver from an existing net::URLRequest | |
| 32 // that has had the stale-while-revalidate algorithm applied to it. | |
| 33 class AsyncRevalidationManager { | |
| 34 public: | |
| 35 AsyncRevalidationManager(); | |
| 36 ~AsyncRevalidationManager(); | |
| 37 | |
| 38 // Starts an async revalidation by copying |for_request|. |scheduler| must | |
| 39 // remain valid until this object is destroyed. | |
| 40 void BeginAsyncRevalidation(const net::URLRequest& for_request, | |
| 41 ResourceScheduler* scheduler); | |
| 42 | |
| 43 // Cancel all pending async revalidations that use ResourceContext. | |
| 44 void CancelAsyncRevalidationsForResourceContext( | |
| 45 ResourceContext* resource_context); | |
| 46 | |
| 47 static bool QualifiesForAsyncRevalidation( | |
| 48 const ResourceHostMsg_Request& request); | |
| 49 | |
| 50 private: | |
| 51 // The key of the map of pending async revalidations. This key has a distinct | |
| 52 // value for every in-progress async revalidation. It is used to avoid | |
| 53 // duplicate async revalidations, and also to cancel affected async | |
| 54 // revalidations when a ResourceContext is removed. | |
| 55 // | |
| 56 // Request headers are intentionally not included in the key as they usually | |
| 57 // don't affect caching, and could cause effectively identical revalidations | |
| 58 // to be considered different because they have a different "Referer" | |
| 59 // header. In rare cases where a "Vary" header leads to us to access different | |
| 60 // resources at the same URL, they fight over a single cache slot and so | |
| 61 // performing additional async revalidations will not improve matters. | |
|
davidben
2015/12/07 23:56:04
Nit: This comment assumes net::HttpCache won't eve
Adam Rice
2015/12/08 18:05:35
I filed a bug for it and added a link.
| |
| 62 struct AsyncRevalidationKey { | |
| 63 AsyncRevalidationKey(const ResourceContext* resource_context, | |
| 64 const net::HttpCache* http_cache, | |
| 65 const GURL& url); | |
| 66 | |
| 67 // Create a prefix key that is used to match all of the | |
| 68 // AsyncRevalidationDrivers using |resource_context| in the map. | |
| 69 explicit AsyncRevalidationKey(const ResourceContext* resource_context); | |
| 70 | |
| 71 // The key for a map needs to be copyable. | |
| 72 AsyncRevalidationKey(const AsyncRevalidationKey& rhs) = default; | |
| 73 ~AsyncRevalidationKey(); | |
| 74 | |
| 75 // No operator= is generated because the struct members are immutable. | |
| 76 | |
| 77 // |resource_context| and |http_cache| are never dereferenced; they are only | |
| 78 // compared to other values. | |
| 79 const ResourceContext* const resource_context; | |
| 80 | |
| 81 // There are multiple independent HttpCache objects per ResourceContext. | |
| 82 const net::HttpCache* const http_cache; | |
| 83 | |
| 84 // Derived from the url via net::HttpUtil::SpecForRequest(). | |
| 85 const std::string url_key; | |
| 86 | |
| 87 struct LessThan { | |
| 88 bool operator()(const AsyncRevalidationKey& lhs, | |
| 89 const AsyncRevalidationKey& rhs) const; | |
| 90 }; | |
| 91 }; | |
| 92 | |
| 93 using AsyncRevalidationMap = std::map<AsyncRevalidationKey, | |
| 94 scoped_ptr<AsyncRevalidationDriver>, | |
| 95 AsyncRevalidationKey::LessThan>; | |
| 96 | |
| 97 void OnAsyncRevalidationComplete(AsyncRevalidationMap::iterator it); | |
| 98 | |
| 99 // Map of AsyncRevalidationDriver instances that are currently in-flight: | |
| 100 // either waiting to be scheduled or active on the network. | |
| 101 AsyncRevalidationMap in_progress_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(AsyncRevalidationManager); | |
| 104 }; | |
| 105 | |
| 106 } // namespace content | |
| 107 | |
| 108 #endif // CONTENT_BROWSER_LOADER_ASYNC_REVALIDATION_MANAGER_H_ | |
| OLD | NEW |