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_
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"
35 namespace sync_driver
{
41 class PrecacheDatabase
;
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
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
> {
57 typedef base::Closure PrecacheCompletionCallback
;
59 PrecacheManager(content::BrowserContext
* browser_context
,
60 const sync_driver::SyncService
* const sync_service
);
61 ~PrecacheManager() override
;
63 // Returns true if precaching is enabled as part of a field trial or by the
64 // command line flag. This method can be called on any thread.
65 static bool IsPrecachingEnabled();
67 // Returns true if precaching is allowed for the browser context.
68 bool IsPrecachingAllowed();
70 // Starts precaching resources that the user is predicted to fetch in the
71 // future. If precaching is already currently in progress, then this method
72 // does nothing. The |precache_completion_callback| will be run when
73 // precaching finishes, but will not be run if precaching is canceled.
75 const PrecacheCompletionCallback
& precache_completion_callback
,
76 const history::HistoryService
& history_service
);
78 // Cancels precaching if it is in progress.
79 void CancelPrecaching();
81 // Returns true if precaching is currently in progress, or false otherwise.
82 bool IsPrecaching() const;
84 // Update precache-related metrics in response to a URL being fetched.
85 void RecordStatsForFetch(const GURL
& url
,
86 const base::Time
& fetch_time
,
92 void Shutdown() override
;
94 // From PrecacheFetcher::PrecacheDelegate.
95 void OnDone() override
;
97 // From history::HistoryService::TopHosts.
98 void OnHostsReceived(const history::TopHostsList
& host_counts
);
100 // The browser context that owns this PrecacheManager.
101 content::BrowserContext
* const browser_context_
;
103 // The sync service corresponding to the browser context. Used to determine
104 // whether precache can run. May be null.
105 const sync_driver::SyncService
* const sync_service_
;
107 // The PrecacheFetcher used to precache resources. Should only be used on the
109 scoped_ptr
<PrecacheFetcher
> precache_fetcher_
;
111 // The callback that will be run if precaching finishes without being
113 PrecacheCompletionCallback precache_completion_callback_
;
115 // The PrecacheDatabase for tracking precache metrics. Should only be used on
117 const scoped_refptr
<PrecacheDatabase
> precache_database_
;
119 // Flag indicating whether or not precaching is currently in progress.
122 DISALLOW_COPY_AND_ASSIGN(PrecacheManager
);
125 } // namespace precache
127 #endif // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_