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.
5 #ifndef CHROME_BROWSER_HISTORY_TOP_SITES_DATABASE_H_
6 #define CHROME_BROWSER_HISTORY_TOP_SITES_DATABASE_H_
11 #include "base/gtest_prod_util.h"
12 #include "components/history/core/browser/history_types.h"
13 #include "sql/meta_table.h"
25 class TopSitesDatabase {
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);
34 // Thumbnails ----------------------------------------------------------------
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);
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,
47 const Images& thumbnail);
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);
53 // Get a thumbnail for a given page. Returns true iff we have the thumbnail.
54 bool GetPageThumbnail(const GURL& url, Images* thumbnail);
56 // Remove the record for this URL. Returns true iff removed successfully.
57 bool RemoveURL(const MostVisitedURL& url);
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);
68 // Rank of all URLs that are forced and therefore cannot be automatically
70 static const int kRankOfForcedURL;
72 // Rank used to indicate that a URL is not stored in the database.
73 static const int kRankOfNonExistingURL;
75 // Upgrades the thumbnail table to version 3, returning true if the
76 // upgrade was successful.
77 bool UpgradeToVersion3();
79 // Adds a new URL to the database.
80 void AddPageThumbnail(const MostVisitedURL& url,
82 const Images& thumbnail);
84 // Sets the page rank. Should be called within an open transaction.
85 void UpdatePageRankNoTransaction(const MostVisitedURL& url, int new_rank);
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);
92 // Returns |url|'s current rank or kRankOfNonExistingURL if not present.
93 int GetURLRank(const MostVisitedURL& url);
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);
100 sql::Connection* CreateDB(const base::FilePath& db_name);
102 scoped_ptr<sql::Connection> db_;
103 sql::MetaTable meta_table_;
105 DISALLOW_COPY_AND_ASSIGN(TopSitesDatabase);
108 } // namespace history
110 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_DATABASE_H_