Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / precache / content / precache_manager.h
blobbc84c81077fb7891452921c18fdbcdcf45a0defa
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_
6 #define COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_
8 #include <list>
9 #include <string>
10 #include <utility>
11 #include <vector>
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "components/history/core/browser/history_types.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "components/precache/core/precache_fetcher.h"
21 #include "url/gurl.h"
23 namespace base {
24 class Time;
27 namespace content {
28 class BrowserContext;
31 namespace history {
32 class HistoryService;
35 namespace sync_driver {
36 class SyncService;
39 namespace precache {
41 class PrecacheDatabase;
43 // Visible for test.
44 int NumTopHosts();
46 // Class that manages all precaching-related activities. Owned by the
47 // BrowserContext that it is constructed for. Use
48 // PrecacheManagerFactory::GetForBrowserContext to get an instance of this
49 // class. All methods must be called on the UI thread unless indicated
50 // otherwise.
51 // TODO(sclittle): Delete precache history when browsing history is deleted.
52 // http://crbug.com/326549
53 class PrecacheManager : public KeyedService,
54 public PrecacheFetcher::PrecacheDelegate,
55 public base::SupportsWeakPtr<PrecacheManager> {
56 public:
57 typedef base::Callback<void(bool)> PrecacheCompletionCallback;
59 PrecacheManager(content::BrowserContext* browser_context,
60 const sync_driver::SyncService* const sync_service,
61 const history::HistoryService* const history_service);
62 ~PrecacheManager() override;
64 // Returns true if the client is in the experiment group -- that is,
65 // precaching is allowed based on user settings, and enabled as part of a
66 // field trial or by commandline flag. Virtual for testing.
67 virtual bool IsInExperimentGroup() const;
69 // Returns true if the client is in the control group -- that is, precaching
70 // is allowed based on user settings, and the browser is in the control group
71 // of the field trial. Virtual for testing.
72 virtual bool IsInControlGroup() const;
74 // Returns true if precaching is allowed based on user settings. Virtual for
75 // testing.
76 virtual bool IsPrecachingAllowed() const;
78 // Starts precaching resources that the user is predicted to fetch in the
79 // future. If precaching is already currently in progress, then this method
80 // does nothing. The |precache_completion_callback| will be passed true when
81 // precaching finishes, and passed false when precaching abort due to failed
82 // preconditions, but will not be run if precaching is canceled.
83 void StartPrecaching(
84 const PrecacheCompletionCallback& precache_completion_callback);
86 // Cancels precaching if it is in progress.
87 void CancelPrecaching();
89 // Returns true if precaching is currently in progress, or false otherwise.
90 bool IsPrecaching() const;
92 // Update precache-related metrics in response to a URL being fetched.
93 void RecordStatsForFetch(const GURL& url,
94 const GURL& referrer,
95 const base::TimeDelta& latency,
96 const base::Time& fetch_time,
97 int64 size,
98 bool was_cached);
100 // Posts a task to the DB thread to delete all history entries from the
101 // database. Does not wait for completion of this task.
102 void ClearHistory();
104 private:
105 enum class AllowedType {
106 ALLOWED,
107 DISALLOWED,
108 PENDING
111 // From KeyedService.
112 void Shutdown() override;
114 // From PrecacheFetcher::PrecacheDelegate.
115 void OnDone() override;
117 // From history::HistoryService::TopHosts.
118 void OnHostsReceived(const history::TopHostsList& host_counts);
120 // From history::HistoryService::TopHosts. Used for the control group, which
121 // gets the list of TopHosts for metrics purposes, but otherwise does nothing.
122 void OnHostsReceivedThenDone(const history::TopHostsList& host_counts);
124 // Returns true if precaching is allowed for the browser context.
125 AllowedType PrecachingAllowed() const;
127 // Update precache-related metrics in response to a URL being fetched. Called
128 // by RecordStatsForFetch() by way of an asynchronous HistoryService callback.
129 void RecordStatsForFetchInternal(const GURL& url,
130 const base::TimeDelta& latency,
131 const base::Time& fetch_time,
132 int64 size,
133 bool was_cached,
134 int host_rank);
136 // The browser context that owns this PrecacheManager.
137 content::BrowserContext* const browser_context_;
139 // The sync service corresponding to the browser context. Used to determine
140 // whether precache can run. May be null.
141 const sync_driver::SyncService* const sync_service_;
143 // The history service corresponding to the browser context. Used to determine
144 // the list of top hosts. May be null.
145 const history::HistoryService* const history_service_;
147 // The PrecacheFetcher used to precache resources. Should only be used on the
148 // UI thread.
149 scoped_ptr<PrecacheFetcher> precache_fetcher_;
151 // The callback that will be run if precaching finishes without being
152 // canceled.
153 PrecacheCompletionCallback precache_completion_callback_;
155 // The PrecacheDatabase for tracking precache metrics. Should only be used on
156 // the DB thread.
157 const scoped_refptr<PrecacheDatabase> precache_database_;
159 // Flag indicating whether or not precaching is currently in progress.
160 bool is_precaching_;
162 DISALLOW_COPY_AND_ASSIGN(PrecacheManager);
165 } // namespace precache
167 #endif // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_