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_BACKEND_H_ 6 #define CHROME_BROWSER_HISTORY_TOP_SITES_BACKEND_H_ 7 8 #include "base/callback.h" 9 #include "base/files/file_path.h" 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "chrome/browser/history/history_types.h" 13 14 class CancelableTaskTracker; 15 16 namespace base { 17 class FilePath; 18 } 19 20 namespace history { 21 22 class TopSitesDatabase; 23 24 // Service used by TopSites to have db interaction happen on the DB thread. All 25 // public methods are invoked on the ui thread and get funneled to the DB 26 // thread. 27 class TopSitesBackend : public base::RefCountedThreadSafe<TopSitesBackend> { 28 public: 29 // The boolean parameter indicates if the DB existed on disk or needs to be 30 // migrated. 31 typedef base::Callback<void(const scoped_refptr<MostVisitedThumbnails>&)> 32 GetMostVisitedThumbnailsCallback; 33 34 TopSitesBackend(); 35 36 void Init(const base::FilePath& path); 37 38 // Schedules the db to be shutdown. 39 void Shutdown(); 40 41 // Fetches MostVisitedThumbnails. 42 void GetMostVisitedThumbnails( 43 const GetMostVisitedThumbnailsCallback& callback, 44 CancelableTaskTracker* tracker); 45 46 // Updates top sites database from the specified delta. 47 void UpdateTopSites(const TopSitesDelta& delta); 48 49 // Sets the thumbnail. 50 void SetPageThumbnail(const MostVisitedURL& url, 51 int url_rank, 52 const Images& thumbnail); 53 54 // Deletes the database and recreates it. 55 void ResetDatabase(); 56 57 // Schedules a request that does nothing on the DB thread, but then notifies 58 // the the calling thread with a reply. This is used to make sure the db has 59 // finished processing a request. 60 void DoEmptyRequest(const base::Closure& reply, 61 CancelableTaskTracker* tracker); 62 63 private: 64 friend class base::RefCountedThreadSafe<TopSitesBackend>; 65 66 virtual ~TopSitesBackend(); 67 68 // Invokes Init on the db_. 69 void InitDBOnDBThread(const base::FilePath& path); 70 71 // Shuts down the db. 72 void ShutdownDBOnDBThread(); 73 74 // Does the work of getting the most visted thumbnails. 75 void GetMostVisitedThumbnailsOnDBThread( 76 scoped_refptr<MostVisitedThumbnails> thumbnails); 77 78 // Updates top sites. 79 void UpdateTopSitesOnDBThread(const TopSitesDelta& delta); 80 81 // Sets the thumbnail. 82 void SetPageThumbnailOnDBThread(const MostVisitedURL& url, 83 int url_rank, 84 const Images& thumbnail); 85 86 // Resets the database. 87 void ResetDatabaseOnDBThread(const base::FilePath& file_path); 88 89 base::FilePath db_path_; 90 91 scoped_ptr<TopSitesDatabase> db_; 92 93 DISALLOW_COPY_AND_ASSIGN(TopSitesBackend); 94 }; 95 96 } // namespace history 97 98 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_BACKEND_H_ 99