1// Copyright (c) 2012 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_DATABASE_H_
6#define CHROME_BROWSER_HISTORY_TOP_SITES_DATABASE_H_
7
8#include <map>
9#include <string>
10
11#include "base/gtest_prod_util.h"
12#include "components/history/core/browser/history_types.h"
13#include "sql/meta_table.h"
14
15namespace base {
16class FilePath;
17}
18
19namespace sql {
20class Connection;
21}
22
23namespace history {
24
25class TopSitesDatabase {
26 public:
27  TopSitesDatabase();
28  ~TopSitesDatabase();
29
30  // Must be called after creation but before any other methods are called.
31  // Returns true on success. If false, no other functions should be called.
32  bool Init(const base::FilePath& db_name);
33
34  // Thumbnails ----------------------------------------------------------------
35
36  // Returns a list of all URLs currently in the table.
37  // WARNING: clears both input arguments.
38  void GetPageThumbnails(MostVisitedURLList* urls,
39                         std::map<GURL, Images>* thumbnails);
40
41  // Set a thumbnail for a URL. |url_rank| is the position of the URL
42  // in the list of TopURLs, zero-based.
43  // If the URL is not in the table, add it. If it is, replace its
44  // thumbnail and rank. Shift the ranks of other URLs if necessary.
45  void SetPageThumbnail(const MostVisitedURL& url,
46                        int new_rank,
47                        const Images& thumbnail);
48
49  // Sets the rank for a given URL. The URL must be in the database.
50  // Use SetPageThumbnail if it's not.
51  void UpdatePageRank(const MostVisitedURL& url, int new_rank);
52
53  // Get a thumbnail for a given page. Returns true iff we have the thumbnail.
54  bool GetPageThumbnail(const GURL& url, Images* thumbnail);
55
56  // Remove the record for this URL. Returns true iff removed successfully.
57  bool RemoveURL(const MostVisitedURL& url);
58
59 private:
60  FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, Version1);
61  FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, Version2);
62  FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, Version3);
63  FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, Recovery1);
64  FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, Recovery2);
65  FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, Recovery3);
66  FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, AddRemoveEditThumbnails);
67
68  // Rank of all URLs that are forced and therefore cannot be automatically
69  // evicted.
70  static const int kRankOfForcedURL;
71
72  // Rank used to indicate that a URL is not stored in the database.
73  static const int kRankOfNonExistingURL;
74
75  // Upgrades the thumbnail table to version 3, returning true if the
76  // upgrade was successful.
77  bool UpgradeToVersion3();
78
79  // Adds a new URL to the database.
80  void AddPageThumbnail(const MostVisitedURL& url,
81                        int new_rank,
82                        const Images& thumbnail);
83
84  // Sets the page rank. Should be called within an open transaction.
85  void UpdatePageRankNoTransaction(const MostVisitedURL& url, int new_rank);
86
87  // Updates thumbnail of a URL that's already in the database.
88  // Returns true if the database query succeeds.
89  bool UpdatePageThumbnail(const MostVisitedURL& url,
90                           const Images& thumbnail);
91
92  // Returns |url|'s current rank or kRankOfNonExistingURL if not present.
93  int GetURLRank(const MostVisitedURL& url);
94
95  // Helper function to implement internals of Init().  This allows
96  // Init() to retry in case of failure, since some failures will
97  // invoke recovery code.
98  bool InitImpl(const base::FilePath& db_name);
99
100  sql::Connection* CreateDB(const base::FilePath& db_name);
101
102  scoped_ptr<sql::Connection> db_;
103  sql::MetaTable meta_table_;
104
105  DISALLOW_COPY_AND_ASSIGN(TopSitesDatabase);
106};
107
108}  // namespace history
109
110#endif  // CHROME_BROWSER_HISTORY_TOP_SITES_DATABASE_H_
111