Fix waterfall bots to not use fastbuild and add presubmit validation.
[chromium-blink-merge.git] / components / precache / content / precache_manager.h
blob9be6cd09007ae57178dfacd5fa05884a66df1a22
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 precaching is allowed for the browser context based on user
65 // settings, and enabled as part of a field trial or by commandline flag.
66 // Virtual for testing.
67 virtual bool ShouldRun() const;
69 // Returns true if precaching is allowed for the browser context based on user
70 // settings. Virtual for testing.
71 virtual bool WouldRun() const;
73 // Starts precaching resources that the user is predicted to fetch in the
74 // future. If precaching is already currently in progress, then this method
75 // does nothing. The |precache_completion_callback| will be passed true when
76 // precaching finishes, and passed false when precaching abort due to failed
77 // preconditions, but will not be run if precaching is canceled.
78 void StartPrecaching(
79 const PrecacheCompletionCallback& precache_completion_callback);
81 // Cancels precaching if it is in progress.
82 void CancelPrecaching();
84 // Returns true if precaching is currently in progress, or false otherwise.
85 bool IsPrecaching() const;
87 // Update precache-related metrics in response to a URL being fetched.
88 void RecordStatsForFetch(const GURL& url,
89 const base::TimeDelta& latency,
90 const base::Time& fetch_time,
91 int64 size,
92 bool was_cached);
94 // Posts a task to the DB thread to delete all history entries from the
95 // database. Does not wait for completion of this task.
96 void ClearHistory();
98 private:
99 enum class AllowedType {
100 ALLOWED,
101 DISALLOWED,
102 PENDING
105 // From KeyedService.
106 void Shutdown() override;
108 // From PrecacheFetcher::PrecacheDelegate.
109 void OnDone() override;
111 // From history::HistoryService::TopHosts.
112 void OnHostsReceived(const history::TopHostsList& host_counts);
114 // Returns true if precaching is enabled as part of a field trial or by the
115 // command line flag. This has a different meaning from the
116 // "is_precaching_enabled" pref set in PrecacheServiceLauncher. This method
117 // can be called on any thread.
118 static bool IsPrecachingEnabled();
120 // Returns true if precaching is allowed for the browser context.
121 AllowedType PrecachingAllowed() const;
123 // The browser context that owns this PrecacheManager.
124 content::BrowserContext* const browser_context_;
126 // The sync service corresponding to the browser context. Used to determine
127 // whether precache can run. May be null.
128 const sync_driver::SyncService* const sync_service_;
130 // The history service corresponding to the browser context. Used to determine
131 // the list of top hosts. May be null.
132 const history::HistoryService* const history_service_;
134 // The PrecacheFetcher used to precache resources. Should only be used on the
135 // UI thread.
136 scoped_ptr<PrecacheFetcher> precache_fetcher_;
138 // The callback that will be run if precaching finishes without being
139 // canceled.
140 PrecacheCompletionCallback precache_completion_callback_;
142 // The PrecacheDatabase for tracking precache metrics. Should only be used on
143 // the DB thread.
144 const scoped_refptr<PrecacheDatabase> precache_database_;
146 // Flag indicating whether or not precaching is currently in progress.
147 bool is_precaching_;
149 DISALLOW_COPY_AND_ASSIGN(PrecacheManager);
152 } // namespace precache
154 #endif // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_