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.
4
5#ifndef COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_
6#define COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_
7
8#include <list>
9
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"
18
19namespace base {
20class Time;
21}
22
23namespace content {
24class BrowserContext;
25}
26
27namespace precache {
28
29class PrecacheDatabase;
30class URLListProvider;
31
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
39class PrecacheManager : public KeyedService,
40                        public PrecacheFetcher::PrecacheDelegate,
41                        public base::SupportsWeakPtr<PrecacheManager> {
42 public:
43  typedef base::Closure PrecacheCompletionCallback;
44
45  explicit PrecacheManager(content::BrowserContext* browser_context);
46  virtual ~PrecacheManager();
47
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();
51
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.
56  void StartPrecaching(
57      const PrecacheCompletionCallback& precache_completion_callback,
58      URLListProvider* url_list_provider);
59
60  // Cancels precaching if it is in progress.
61  void CancelPrecaching();
62
63  // Returns true if precaching is currently in progress, or false otherwise.
64  bool IsPrecaching() const;
65
66  // Update precache-related metrics in response to a URL being fetched.
67  void RecordStatsForFetch(const GURL& url,
68                           const base::Time& fetch_time,
69                           int64 size,
70                           bool was_cached);
71
72 private:
73  // From KeyedService.
74  virtual void Shutdown() OVERRIDE;
75
76  // From PrecacheFetcher::PrecacheDelegate.
77  virtual void OnDone() OVERRIDE;
78
79  void OnURLsReceived(const std::list<GURL>& urls);
80
81  // The browser context that owns this PrecacheManager.
82  content::BrowserContext* browser_context_;
83
84  // The PrecacheFetcher used to precache resources. Should only be used on the
85  // UI thread.
86  scoped_ptr<PrecacheFetcher> precache_fetcher_;
87
88  // The callback that will be run if precaching finishes without being
89  // canceled.
90  PrecacheCompletionCallback precache_completion_callback_;
91
92  // The PrecacheDatabase for tracking precache metrics. Should only be used on
93  // the DB thread.
94  scoped_refptr<PrecacheDatabase> precache_database_;
95
96  // Flag indicating whether or not precaching is currently in progress.
97  bool is_precaching_;
98
99  DISALLOW_COPY_AND_ASSIGN(PrecacheManager);
100};
101
102}  // namespace precache
103
104#endif  // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_
105