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/browser_context_keyed_service/browser_context_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 BrowserContextKeyedService,
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 // Starts precaching resources that the user is predicted to fetch in the
53 // future. If precaching is already currently in progress, then this method
54 // does nothing. The |precache_completion_callback| will be run when
55 // precaching finishes, but will not be run if precaching is canceled.
57 const PrecacheCompletionCallback& precache_completion_callback,
58 URLListProvider* url_list_provider);
60 // Cancels precaching if it is in progress.
61 void CancelPrecaching();
63 // Returns true if precaching is currently in progress, or false otherwise.
64 bool IsPrecaching() const;
66 // Update precache-related metrics in response to a URL being fetched.
67 void RecordStatsForFetch(const GURL& url,
68 const base::Time& fetch_time,
73 // From BrowserContextKeyedService.
74 virtual void Shutdown() OVERRIDE;
76 // From PrecacheFetcher::PrecacheDelegate.
77 virtual void OnDone() OVERRIDE;
79 void OnURLsReceived(const std::list<GURL>& urls);
81 // The browser context that owns this PrecacheManager.
82 content::BrowserContext* browser_context_;
84 // The PrecacheFetcher used to precache resources. Should only be used on the
86 scoped_ptr<PrecacheFetcher> precache_fetcher_;
88 // The callback that will be run if precaching finishes without being
90 PrecacheCompletionCallback precache_completion_callback_;
92 // The PrecacheDatabase for tracking precache metrics. Should only be used on
94 scoped_refptr<PrecacheDatabase> precache_database_;
96 // Flag indicating whether or not precaching is currently in progress.
99 DISALLOW_COPY_AND_ASSIGN(PrecacheManager);
102 } // namespace precache
104 #endif // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_