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_
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 #include "components/precache/core/precache_fetcher.h"
29 class PrecacheDatabase
;
30 class URLListProvider
;
32 // Class that manages all precaching-related activities. Owned by the
33 // BrowserContext that it is constructed for. Use
34 // PrecacheManagerFactory::GetForBrowserContext to get an instance of this
35 // class. All methods must be called on the UI thread unless indicated
37 // TODO(sclittle): Delete precache history when browsing history is deleted.
38 // http://crbug.com/326549
39 class PrecacheManager
: public KeyedService
,
40 public PrecacheFetcher::PrecacheDelegate
,
41 public base::SupportsWeakPtr
<PrecacheManager
> {
43 typedef base::Closure PrecacheCompletionCallback
;
45 explicit PrecacheManager(content::BrowserContext
* browser_context
);
46 virtual ~PrecacheManager();
48 // Returns true if precaching is enabled as part of a field trial or by the
49 // command line flag. This method can be called on any thread.
50 static bool IsPrecachingEnabled();
52 // Returns true if precaching is allowed for the browser context.
53 bool IsPrecachingAllowed();
55 // Starts precaching resources that the user is predicted to fetch in the
56 // future. If precaching is already currently in progress, then this method
57 // does nothing. The |precache_completion_callback| will be run when
58 // precaching finishes, but will not be run if precaching is canceled.
60 const PrecacheCompletionCallback
& precache_completion_callback
,
61 URLListProvider
* url_list_provider
);
63 // Cancels precaching if it is in progress.
64 void CancelPrecaching();
66 // Returns true if precaching is currently in progress, or false otherwise.
67 bool IsPrecaching() const;
69 // Update precache-related metrics in response to a URL being fetched.
70 void RecordStatsForFetch(const GURL
& url
,
71 const base::Time
& fetch_time
,
77 virtual void Shutdown() override
;
79 // From PrecacheFetcher::PrecacheDelegate.
80 virtual void OnDone() override
;
82 void OnURLsReceived(const std::list
<GURL
>& urls
);
84 // The browser context that owns this PrecacheManager.
85 content::BrowserContext
* browser_context_
;
87 // The PrecacheFetcher used to precache resources. Should only be used on the
89 scoped_ptr
<PrecacheFetcher
> precache_fetcher_
;
91 // The callback that will be run if precaching finishes without being
93 PrecacheCompletionCallback precache_completion_callback_
;
95 // The PrecacheDatabase for tracking precache metrics. Should only be used on
97 scoped_refptr
<PrecacheDatabase
> precache_database_
;
99 // Flag indicating whether or not precaching is currently in progress.
102 DISALLOW_COPY_AND_ASSIGN(PrecacheManager
);
105 } // namespace precache
107 #endif // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_