1// Copyright (c) 2011 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 CHROME_BROWSER_HISTORY_TOP_SITES_BACKEND_H_
6#define CHROME_BROWSER_HISTORY_TOP_SITES_BACKEND_H_
7
8#include "base/callback.h"
9#include "base/files/file_path.h"
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
12#include "chrome/browser/history/history_types.h"
13
14class CancelableTaskTracker;
15
16namespace base {
17class FilePath;
18}
19
20namespace history {
21
22class TopSitesDatabase;
23
24// Service used by TopSites to have db interaction happen on the DB thread.  All
25// public methods are invoked on the ui thread and get funneled to the DB
26// thread.
27class TopSitesBackend : public base::RefCountedThreadSafe<TopSitesBackend> {
28 public:
29  // The boolean parameter indicates if the DB existed on disk or needs to be
30  // migrated.
31  typedef base::Callback<void(const scoped_refptr<MostVisitedThumbnails>&,
32                              const bool*)>
33      GetMostVisitedThumbnailsCallback;
34
35  TopSitesBackend();
36
37  void Init(const base::FilePath& path);
38
39  // Schedules the db to be shutdown.
40  void Shutdown();
41
42  // Fetches MostVisitedThumbnails.
43  void GetMostVisitedThumbnails(
44      const GetMostVisitedThumbnailsCallback& callback,
45      CancelableTaskTracker* tracker);
46
47  // Updates top sites database from the specified delta.
48  void UpdateTopSites(const TopSitesDelta& delta);
49
50  // Sets the thumbnail.
51  void SetPageThumbnail(const MostVisitedURL& url,
52                        int url_rank,
53                        const Images& thumbnail);
54
55  // Deletes the database and recreates it.
56  void ResetDatabase();
57
58  // Schedules a request that does nothing on the DB thread, but then notifies
59  // the the calling thread with a reply. This is used to make sure the db has
60  // finished processing a request.
61  void DoEmptyRequest(const base::Closure& reply,
62                      CancelableTaskTracker* tracker);
63
64 private:
65  friend class base::RefCountedThreadSafe<TopSitesBackend>;
66
67  virtual ~TopSitesBackend();
68
69  // Invokes Init on the db_.
70  void InitDBOnDBThread(const base::FilePath& path);
71
72  // Shuts down the db.
73  void ShutdownDBOnDBThread();
74
75  // Does the work of getting the most visted thumbnails.
76  void GetMostVisitedThumbnailsOnDBThread(
77      scoped_refptr<MostVisitedThumbnails> thumbnails,
78      bool* need_history_migration);
79
80  // Updates top sites.
81  void UpdateTopSitesOnDBThread(const TopSitesDelta& delta);
82
83  // Sets the thumbnail.
84  void SetPageThumbnailOnDBThread(const MostVisitedURL& url,
85                                  int url_rank,
86                                  const Images& thumbnail);
87
88  // Resets the database.
89  void ResetDatabaseOnDBThread(const base::FilePath& file_path);
90
91  base::FilePath db_path_;
92
93  scoped_ptr<TopSitesDatabase> db_;
94
95  DISALLOW_COPY_AND_ASSIGN(TopSitesBackend);
96};
97
98}  // namespace history
99
100#endif  // CHROME_BROWSER_HISTORY_TOP_SITES_BACKEND_H_
101