summaryrefslogtreecommitdiffstats
path: root/chrome/browser/history/top_sites_backend.h
blob: 50d08678f8150dc580b1e4004670de7ea1aa6c2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_HISTORY_TOP_SITES_BACKEND_H_
#define CHROME_BROWSER_HISTORY_TOP_SITES_BACKEND_H_
#pragma once

#include "base/file_path.h"
#include "base/ref_counted.h"
#include "base/scoped_ptr.h"
#include "chrome/browser/cancelable_request.h"
#include "chrome/browser/history/history_types.h"

class FilePath;

namespace history {

class TopSitesDatabase;

// Service used by TopSites to have db interaction happen on the DB thread.  All
// public methods are invoked on the ui thread and get funneled to the DB
// thread.
class TopSitesBackend
    : public base::RefCountedThreadSafe<TopSitesBackend>,
      public CancelableRequestProvider {
 public:
  TopSitesBackend();

  void Init(const FilePath& path);

  // Schedules the db to be shutdown.
  void Shutdown();

  // The boolean parameter indicates if the DB existed on disk or needs to be
  // migrated.
  typedef Callback3<Handle, scoped_refptr<MostVisitedThumbnails>, bool >::Type
      GetMostVisitedThumbnailsCallback;
  typedef CancelableRequest1<TopSitesBackend::GetMostVisitedThumbnailsCallback,
                             scoped_refptr<MostVisitedThumbnails> >
      GetMostVisitedThumbnailsRequest;

  // Fetches MostVisitedThumbnails.
  Handle GetMostVisitedThumbnails(CancelableRequestConsumerBase* consumer,
                                  GetMostVisitedThumbnailsCallback* callback);

  // Updates top sites database from the specified delta.
  void UpdateTopSites(const TopSitesDelta& delta);

  // Sets the thumbnail.
  void SetPageThumbnail(const MostVisitedURL& url,
                        int url_rank,
                        const Images& thumbnail);

  // Deletes the database and recreates it.
  void ResetDatabase();

  typedef Callback1<Handle>::Type EmptyRequestCallback;
  typedef CancelableRequest<TopSitesBackend::EmptyRequestCallback>
      EmptyRequestRequest;

  // Schedules a request that does nothing on the DB thread, but then notifies
  // the callback on the calling thread. This is used to make sure the db has
  // finished processing a request.
  Handle DoEmptyRequest(CancelableRequestConsumerBase* consumer,
                        EmptyRequestCallback* callback);

 private:
  friend class base::RefCountedThreadSafe<TopSitesBackend>;

  ~TopSitesBackend();

  // Invokes Init on the db_.
  void InitDBOnDBThread(const FilePath& path);

  // Shuts down the db.
  void ShutdownDBOnDBThread();

  // Does the work of getting the most visted thumbnails.
  void GetMostVisitedThumbnailsOnDBThread(
      scoped_refptr<GetMostVisitedThumbnailsRequest> request);

  // Updates top sites.
  void UpdateTopSitesOnDBThread(const TopSitesDelta& delta);

  // Sets the thumbnail.
  void SetPageThumbnailOnDBThread(const MostVisitedURL& url,
                                  int url_rank,
                                  const Images& thumbnail);

  // Resets the database.
  void ResetDatabaseOnDBThread(const FilePath& file_path);

  // Notifies the request.
  void DoEmptyRequestOnDBThread(scoped_refptr<EmptyRequestRequest> request);

  FilePath db_path_;

  scoped_ptr<TopSitesDatabase> db_;

  DISALLOW_COPY_AND_ASSIGN(TopSitesBackend);
};

}  // namespace history

#endif  // CHROME_BROWSER_HISTORY_TOP_SITES_BACKEND_H_