15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_HISTORY_TOP_SITES_DATABASE_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_HISTORY_TOP_SITES_DATABASE_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h" 121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/history/core/browser/history_types.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sql/meta_table.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base { 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FilePath; 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace sql { 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Connection; 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace history { 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TopSitesDatabase { 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TopSitesDatabase(); 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~TopSitesDatabase(); 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Must be called after creation but before any other methods are called. 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true on success. If false, no other functions should be called. 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool Init(const base::FilePath& db_name); 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Thumbnails ---------------------------------------------------------------- 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a list of all URLs currently in the table. 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // WARNING: clears both input arguments. 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void GetPageThumbnails(MostVisitedURLList* urls, 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::map<GURL, Images>* thumbnails); 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Set a thumbnail for a URL. |url_rank| is the position of the URL 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // in the list of TopURLs, zero-based. 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If the URL is not in the table, add it. If it is, replace its 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // thumbnail and rank. Shift the ranks of other URLs if necessary. 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void SetPageThumbnail(const MostVisitedURL& url, 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int new_rank, 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const Images& thumbnail); 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sets the rank for a given URL. The URL must be in the database. 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Use SetPageThumbnail if it's not. 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void UpdatePageRank(const MostVisitedURL& url, int new_rank); 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Get a thumbnail for a given page. Returns true iff we have the thumbnail. 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool GetPageThumbnail(const GURL& url, Images* thumbnail); 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Remove the record for this URL. Returns true iff removed successfully. 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool RemoveURL(const MostVisitedURL& url); 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 6058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, Version1); 6158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, Version2); 621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, Version3); 63a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, Recovery1); 64a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, Recovery2); 65a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, Recovery3); 661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(TopSitesDatabaseTest, AddRemoveEditThumbnails); 671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Rank of all URLs that are forced and therefore cannot be automatically 691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // evicted. 70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) static const int kRankOfForcedURL; 711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Rank used to indicate that a URL is not stored in the database. 73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) static const int kRankOfNonExistingURL; 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Upgrades the thumbnail table to version 3, returning true if the 761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // upgrade was successful. 771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) bool UpgradeToVersion3(); 781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Adds a new URL to the database. 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void AddPageThumbnail(const MostVisitedURL& url, 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int new_rank, 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const Images& thumbnail); 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sets the page rank. Should be called within an open transaction. 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void UpdatePageRankNoTransaction(const MostVisitedURL& url, int new_rank); 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Updates thumbnail of a URL that's already in the database. 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if the database query succeeds. 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool UpdatePageThumbnail(const MostVisitedURL& url, 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const Images& thumbnail); 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Returns |url|'s current rank or kRankOfNonExistingURL if not present. 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int GetURLRank(const MostVisitedURL& url); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 95a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Helper function to implement internals of Init(). This allows 96a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Init() to retry in case of failure, since some failures will 97a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // invoke recovery code. 98a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) bool InitImpl(const base::FilePath& db_name); 99a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) sql::Connection* CreateDB(const base::FilePath& db_name); 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<sql::Connection> db_; 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql::MetaTable meta_table_; 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(TopSitesDatabase); 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace history 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // CHROME_BROWSER_HISTORY_TOP_SITES_DATABASE_H_ 111