Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / precache / content / precache_manager.h
blob56d5d221ff8d0ef153e7cf79095eb4e329910b0f
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>
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"
17 #include "url/gurl.h"
19 namespace base {
20 class Time;
23 namespace content {
24 class BrowserContext;
27 namespace precache {
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
36 // otherwise.
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> {
42 public:
43 typedef base::Closure PrecacheCompletionCallback;
45 explicit PrecacheManager(content::BrowserContext* browser_context);
46 ~PrecacheManager() override;
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.
59 void StartPrecaching(
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,
72 int64 size,
73 bool was_cached);
75 private:
76 // From KeyedService.
77 void Shutdown() override;
79 // From PrecacheFetcher::PrecacheDelegate.
80 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
88 // UI thread.
89 scoped_ptr<PrecacheFetcher> precache_fetcher_;
91 // The callback that will be run if precaching finishes without being
92 // canceled.
93 PrecacheCompletionCallback precache_completion_callback_;
95 // The PrecacheDatabase for tracking precache metrics. Should only be used on
96 // the DB thread.
97 scoped_refptr<PrecacheDatabase> precache_database_;
99 // Flag indicating whether or not precaching is currently in progress.
100 bool is_precaching_;
102 DISALLOW_COPY_AND_ASSIGN(PrecacheManager);
105 } // namespace precache
107 #endif // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_