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