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#pragma once
8
9#include "base/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#include "content/browser/cancelable_request.h"
14
15class FilePath;
16
17namespace history {
18
19class TopSitesDatabase;
20
21// Service used by TopSites to have db interaction happen on the DB thread.  All
22// public methods are invoked on the ui thread and get funneled to the DB
23// thread.
24class TopSitesBackend
25    : public base::RefCountedThreadSafe<TopSitesBackend>,
26      public CancelableRequestProvider {
27 public:
28  TopSitesBackend();
29
30  void Init(const FilePath& path);
31
32  // Schedules the db to be shutdown.
33  void Shutdown();
34
35  // The boolean parameter indicates if the DB existed on disk or needs to be
36  // migrated.
37  typedef Callback3<Handle, scoped_refptr<MostVisitedThumbnails>, bool >::Type
38      GetMostVisitedThumbnailsCallback;
39  typedef CancelableRequest1<TopSitesBackend::GetMostVisitedThumbnailsCallback,
40                             scoped_refptr<MostVisitedThumbnails> >
41      GetMostVisitedThumbnailsRequest;
42
43  // Fetches MostVisitedThumbnails.
44  Handle GetMostVisitedThumbnails(CancelableRequestConsumerBase* consumer,
45                                  GetMostVisitedThumbnailsCallback* callback);
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  typedef Callback1<Handle>::Type EmptyRequestCallback;
59  typedef CancelableRequest<TopSitesBackend::EmptyRequestCallback>
60      EmptyRequestRequest;
61
62  // Schedules a request that does nothing on the DB thread, but then notifies
63  // the callback on the calling thread. This is used to make sure the db has
64  // finished processing a request.
65  Handle DoEmptyRequest(CancelableRequestConsumerBase* consumer,
66                        EmptyRequestCallback* callback);
67
68 private:
69  friend class base::RefCountedThreadSafe<TopSitesBackend>;
70
71  ~TopSitesBackend();
72
73  // Invokes Init on the db_.
74  void InitDBOnDBThread(const FilePath& path);
75
76  // Shuts down the db.
77  void ShutdownDBOnDBThread();
78
79  // Does the work of getting the most visted thumbnails.
80  void GetMostVisitedThumbnailsOnDBThread(
81      scoped_refptr<GetMostVisitedThumbnailsRequest> request);
82
83  // Updates top sites.
84  void UpdateTopSitesOnDBThread(const TopSitesDelta& delta);
85
86  // Sets the thumbnail.
87  void SetPageThumbnailOnDBThread(const MostVisitedURL& url,
88                                  int url_rank,
89                                  const Images& thumbnail);
90
91  // Resets the database.
92  void ResetDatabaseOnDBThread(const FilePath& file_path);
93
94  // Notifies the request.
95  void DoEmptyRequestOnDBThread(scoped_refptr<EmptyRequestRequest> request);
96
97  FilePath db_path_;
98
99  scoped_ptr<TopSitesDatabase> db_;
100
101  DISALLOW_COPY_AND_ASSIGN(TopSitesBackend);
102};
103
104}  // namespace history
105
106#endif  // CHROME_BROWSER_HISTORY_TOP_SITES_BACKEND_H_
107