Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ | 5 #ifndef COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ |
| 6 #define COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ | 6 #define COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <list> | 11 #include <list> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <utility> | 14 #include <utility> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/callback.h" | 17 #include "base/callback.h" |
| 18 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/memory/ref_counted.h" | |
| 20 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
| 21 #include "components/history/core/browser/history_types.h" | 22 #include "components/history/core/browser/history_types.h" |
| 22 #include "components/keyed_service/core/keyed_service.h" | 23 #include "components/keyed_service/core/keyed_service.h" |
| 23 #include "components/precache/core/precache_fetcher.h" | 24 #include "components/precache/core/precache_fetcher.h" |
| 25 #include "net/disk_cache/disk_cache.h" | |
| 26 #include "net/http/http_cache.h" | |
| 24 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 25 | 28 |
| 26 namespace base { | 29 namespace base { |
| 27 class FilePath; | 30 class FilePath; |
| 28 class Time; | 31 class Time; |
| 29 class TimeDelta; | 32 class TimeDelta; |
| 30 } | 33 } |
| 31 | 34 |
| 32 namespace content { | 35 namespace content { |
| 33 class BrowserContext; | 36 class BrowserContext; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 46 } | 49 } |
| 47 | 50 |
| 48 namespace precache { | 51 namespace precache { |
| 49 | 52 |
| 50 class PrecacheDatabase; | 53 class PrecacheDatabase; |
| 51 class PrecacheUnfinishedWork; | 54 class PrecacheUnfinishedWork; |
| 52 | 55 |
| 53 // Visible for test. | 56 // Visible for test. |
| 54 size_t NumTopHosts(); | 57 size_t NumTopHosts(); |
| 55 | 58 |
| 59 // Minimum required cache size (inclusive). | |
| 60 constexpr int kMinCacheSizeBytes = 40e6; | |
|
gavinp
2016/12/08 17:33:47
minor nit: I find 40 * 1000 * 1000 an itsy bitsy b
jamartin
2016/12/08 22:43:58
Done.
| |
| 61 | |
| 56 // Class that manages all precaching-related activities. Owned by the | 62 // Class that manages all precaching-related activities. Owned by the |
| 57 // BrowserContext that it is constructed for. Use | 63 // BrowserContext that it is constructed for. Use |
| 58 // PrecacheManagerFactory::GetForBrowserContext to get an instance of this | 64 // PrecacheManagerFactory::GetForBrowserContext to get an instance of this |
| 59 // class. All methods must be called on the UI thread unless indicated | 65 // class. All methods must be called on the UI thread unless indicated |
| 60 // otherwise. | 66 // otherwise. |
| 61 // TODO(sclittle): Delete precache history when browsing history is deleted. | 67 // TODO(sclittle): Delete precache history when browsing history is deleted. |
| 62 // http://crbug.com/326549 | 68 // http://crbug.com/326549 |
| 63 class PrecacheManager : public KeyedService, | 69 class PrecacheManager : public KeyedService, |
| 64 public PrecacheFetcher::PrecacheDelegate, | 70 public PrecacheFetcher::PrecacheDelegate, |
| 65 public base::SupportsWeakPtr<PrecacheManager> { | 71 public base::SupportsWeakPtr<PrecacheManager> { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, | 128 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, |
| 123 RecordStatsForFetchDuringPrecaching); | 129 RecordStatsForFetchDuringPrecaching); |
| 124 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, RecordStatsForFetchHTTP); | 130 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, RecordStatsForFetchHTTP); |
| 125 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, RecordStatsForFetchHTTPS); | 131 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, RecordStatsForFetchHTTPS); |
| 126 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, RecordStatsForFetchInTopHosts); | 132 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, RecordStatsForFetchInTopHosts); |
| 127 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, | 133 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, |
| 128 RecordStatsForFetchWithEmptyURL); | 134 RecordStatsForFetchWithEmptyURL); |
| 129 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, RecordStatsForFetchWithNonHTTP); | 135 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, RecordStatsForFetchWithNonHTTP); |
| 130 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, | 136 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, |
| 131 RecordStatsForFetchWithSizeZero); | 137 RecordStatsForFetchWithSizeZero); |
| 138 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, | |
| 139 StartPrecachingStopsOnSmallCaches); | |
| 140 FRIEND_TEST_ALL_PREFIXES(PrecacheManagerTest, | |
| 141 StartPrecachingWithGoodSizedCache); | |
| 132 | 142 |
| 133 enum class AllowedType { | 143 enum class AllowedType { |
| 134 ALLOWED, | 144 ALLOWED, |
| 135 DISALLOWED, | 145 DISALLOWED, |
| 136 PENDING | 146 PENDING |
| 137 }; | 147 }; |
| 138 | 148 |
| 139 // From KeyedService. | 149 // From KeyedService. |
| 140 void Shutdown() override; | 150 void Shutdown() override; |
| 141 | 151 |
| 142 // From PrecacheFetcher::PrecacheDelegate. | 152 // From PrecacheFetcher::PrecacheDelegate. |
| 143 void OnDone() override; | 153 void OnDone() override; |
| 144 | 154 |
| 145 // Callback when fetching unfinished work from storage is done. | 155 // Callback when fetching unfinished work from storage is done. |
| 146 void OnGetUnfinishedWorkDone( | 156 void OnGetUnfinishedWorkDone( |
| 147 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work); | 157 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work); |
| 148 | 158 |
| 149 // From history::HistoryService::TopHosts. | 159 // From history::HistoryService::TopHosts. |
| 150 void OnHostsReceived(const history::TopHostsList& host_counts); | 160 void OnHostsReceived(const history::TopHostsList& host_counts); |
| 151 | 161 |
| 152 // Initializes and Starts a PrecacheFetcher with unfinished work. | 162 // Initializes and Starts a PrecacheFetcher with unfinished work. |
| 153 void InitializeAndStartFetcher(); | 163 void InitializeAndStartFetcher(); |
| 154 | 164 |
| 155 // From history::HistoryService::TopHosts. Used for the control group, which | 165 // From history::HistoryService::TopHosts. Used for the control group, which |
| 156 // gets the list of TopHosts for metrics purposes, but otherwise does nothing. | 166 // gets the list of TopHosts for metrics purposes, but otherwise does nothing. |
| 157 void OnHostsReceivedThenDone(const history::TopHostsList& host_counts); | 167 void OnHostsReceivedThenDone(const history::TopHostsList& host_counts); |
| 158 | 168 |
| 169 // Chain of callbacks for StartPrecaching that make sure that we only precache | |
| 170 // if there is a cache big enough. | |
| 171 void PrecacheIfCacheIsBigEnough( | |
| 172 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); | |
| 173 void OnCacheBackendReceived(int net_error_code); | |
| 174 void OnCacheSizeReceived(int cache_size_bytes); | |
| 175 void OnCacheSizeReceivedInUIThread(int cache_size_bytes); | |
| 176 | |
| 159 // Returns true if precaching is allowed for the browser context. | 177 // Returns true if precaching is allowed for the browser context. |
| 160 AllowedType PrecachingAllowed() const; | 178 AllowedType PrecachingAllowed() const; |
| 161 | 179 |
| 162 // Update precache-related metrics in response to a URL being fetched. | 180 // Update precache-related metrics in response to a URL being fetched. |
| 163 void RecordStatsForFetch(const GURL& url, | 181 void RecordStatsForFetch(const GURL& url, |
| 164 const GURL& referrer, | 182 const GURL& referrer, |
| 165 const base::TimeDelta& latency, | 183 const base::TimeDelta& latency, |
| 166 const base::Time& fetch_time, | 184 const base::Time& fetch_time, |
| 167 const net::HttpResponseInfo& info, | 185 const net::HttpResponseInfo& info, |
| 168 int64_t size); | 186 int64_t size); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 196 // canceled. | 214 // canceled. |
| 197 PrecacheCompletionCallback precache_completion_callback_; | 215 PrecacheCompletionCallback precache_completion_callback_; |
| 198 | 216 |
| 199 // The PrecacheDatabase for tracking precache metrics. Should only be used on | 217 // The PrecacheDatabase for tracking precache metrics. Should only be used on |
| 200 // the DB thread. | 218 // the DB thread. |
| 201 std::unique_ptr<PrecacheDatabase> precache_database_; | 219 std::unique_ptr<PrecacheDatabase> precache_database_; |
| 202 | 220 |
| 203 // Flag indicating whether or not precaching is currently in progress. | 221 // Flag indicating whether or not precaching is currently in progress. |
| 204 bool is_precaching_; | 222 bool is_precaching_; |
| 205 | 223 |
| 224 // Pointer to the backend of the cache. Required to get the size of the cache. | |
| 225 // It is not owned and it is reset on demand via callbacks. | |
| 226 // It should only be accessed from the IO thread. | |
| 227 disk_cache::Backend* cache_backend_; | |
| 228 | |
| 229 // The minimum cache size allowed for precaching. We allow this to be modified | |
| 230 // for testing purposes. | |
| 231 int min_cache_size_bytes_ = kMinCacheSizeBytes; | |
| 232 | |
| 206 // Work that hasn't yet finished. | 233 // Work that hasn't yet finished. |
| 207 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; | 234 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; |
| 208 | 235 |
| 209 DISALLOW_COPY_AND_ASSIGN(PrecacheManager); | 236 DISALLOW_COPY_AND_ASSIGN(PrecacheManager); |
| 210 }; | 237 }; |
| 211 | 238 |
| 212 } // namespace precache | 239 } // namespace precache |
| 213 | 240 |
| 214 #endif // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ | 241 #endif // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ |
| OLD | NEW |