• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DATABASE_H_
6 #define CHROME_BROWSER_HISTORY_TOP_SITES_DATABASE_H_
7 #pragma once
8 
9 #include <map>
10 #include <string>
11 
12 #include "app/sql/meta_table.h"
13 #include "base/memory/ref_counted.h"
14 #include "chrome/browser/history/history_types.h"
15 #include "chrome/browser/history/url_database.h"  // For DBCloseScoper.
16 
17 class FilePath;
18 class RefCountedMemory;
19 class SkBitmap;
20 
21 namespace app {
22 class Connection;
23 }
24 
25 namespace history {
26 
27 class TopSitesDatabase {
28  public:
29   TopSitesDatabase();
30   ~TopSitesDatabase();
31 
32   // Must be called after creation but before any other methods are called.
33   // Returns true on success. If false, no other functions should be called.
34   bool Init(const FilePath& db_name);
35 
36   // Returns true if migration of top sites from history may be needed. A value
37   // of true means either migration is definitely needed (the top sites file is
38   // old) or doesn't exist (as would happen for a new user).
may_need_history_migration()39   bool may_need_history_migration() const {
40     return may_need_history_migration_;
41   }
42 
43   // Thumbnails ----------------------------------------------------------------
44 
45   // Returns a list of all URLs currently in the table.
46   // WARNING: clears both input arguments.
47   void GetPageThumbnails(MostVisitedURLList* urls,
48                          std::map<GURL, Images>* thumbnails);
49 
50   // Set a thumbnail for a URL. |url_rank| is the position of the URL
51   // in the list of TopURLs, zero-based.
52   // If the URL is not in the table, add it. If it is, replace its
53   // thumbnail and rank. Shift the ranks of other URLs if necessary.
54   void SetPageThumbnail(const MostVisitedURL& url,
55                         int new_rank,
56                         const Images& thumbnail);
57 
58   // Sets the rank for a given URL. The URL must be in the database.
59   // Use SetPageThumbnail if it's not.
60   void UpdatePageRank(const MostVisitedURL& url, int new_rank);
61 
62   // Get a thumbnail for a given page. Returns true iff we have the thumbnail.
63   bool GetPageThumbnail(const GURL& url, Images* thumbnail);
64 
65   // Remove the record for this URL. Returns true iff removed successfully.
66   bool RemoveURL(const MostVisitedURL& url);
67 
68  private:
69   // Creates the thumbnail table, returning true if the table already exists
70   // or was successfully created.
71   bool InitThumbnailTable();
72 
73   // Adds a new URL to the database.
74   void AddPageThumbnail(const MostVisitedURL& url,
75                         int new_rank,
76                         const Images& thumbnail);
77 
78   // Sets the page rank. Should be called within an open transaction.
79   void UpdatePageRankNoTransaction(const MostVisitedURL& url, int new_rank);
80 
81   // Updates thumbnail of a URL that's already in the database.
82   void UpdatePageThumbnail(const MostVisitedURL& url,
83                            const Images& thumbnail);
84 
85   // Returns the URL's current rank or -1 if it is not present.
86   int GetURLRank(const MostVisitedURL& url);
87 
88   // Returns the number of URLs (rows) in the database.
89   int GetRowCount();
90 
91   sql::Connection* CreateDB(const FilePath& db_name);
92 
93   // Encodes redirects into a string.
94   static std::string GetRedirects(const MostVisitedURL& url);
95 
96   // Decodes redirects from a string and sets them for the url.
97   static void SetRedirects(const std::string& redirects, MostVisitedURL* url);
98 
99   scoped_ptr<sql::Connection> db_;
100   sql::MetaTable meta_table_;
101 
102   // See description above class.
103   bool may_need_history_migration_;
104 
105   DISALLOW_COPY_AND_ASSIGN(TopSitesDatabase);
106 };
107 
108 }  // namespace history
109 
110 #endif  // CHROME_BROWSER_HISTORY_TOP_SITES_DATABASE_H_
111