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

Side by Side Diff: components/precache/content/precache_manager.h

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

Powered by Google App Engine
This is Rietveld 408576698