Stack sampling profiler: add fire-and-forget interface
[chromium-blink-merge.git] / components / precache / content / precache_manager.h
blob2f8313df342ed14beef2a81d226a53d904b53d17
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::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.
74 void StartPrecaching(
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,
87 int64 size,
88 bool was_cached);
90 // Posts a task to the DB thread to delete all history entries from the
91 // database. Does not wait for completion of this task.
92 void ClearHistory();
94 private:
95 // From KeyedService.
96 void Shutdown() override;
98 // From PrecacheFetcher::PrecacheDelegate.
99 void OnDone() override;
101 // From history::HistoryService::TopHosts.
102 void OnHostsReceived(const history::TopHostsList& host_counts);
104 // The browser context that owns this PrecacheManager.
105 content::BrowserContext* const browser_context_;
107 // The sync service corresponding to the browser context. Used to determine
108 // whether precache can run. May be null.
109 const sync_driver::SyncService* const sync_service_;
111 // The PrecacheFetcher used to precache resources. Should only be used on the
112 // UI thread.
113 scoped_ptr<PrecacheFetcher> precache_fetcher_;
115 // The callback that will be run if precaching finishes without being
116 // canceled.
117 PrecacheCompletionCallback precache_completion_callback_;
119 // The PrecacheDatabase for tracking precache metrics. Should only be used on
120 // the DB thread.
121 const scoped_refptr<PrecacheDatabase> precache_database_;
123 // Flag indicating whether or not precaching is currently in progress.
124 bool is_precaching_;
126 DISALLOW_COPY_AND_ASSIGN(PrecacheManager);
129 } // namespace precache
131 #endif // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_