| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_THUMBNAIL_STORE_H_ | 5 #ifndef CHROME_BROWSER_THUMBNAIL_STORE_H_ |
| 6 #define CHROME_BROWSER_THUMBNAIL_STORE_H_ | 6 #define CHROME_BROWSER_THUMBNAIL_STORE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/ref_counted.h" | 14 #include "base/ref_counted.h" |
| 15 #include "base/timer.h" | 15 #include "base/timer.h" |
| 16 #include "chrome/browser/cancelable_request.h" | 16 #include "chrome/browser/cancelable_request.h" |
| 17 #include "chrome/browser/history/history.h" | 17 #include "chrome/browser/history/history.h" |
| 18 #include "chrome/browser/history/url_database.h" // For DBCloseScoper | |
| 19 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/common/ref_counted_util.h" | 19 #include "chrome/common/ref_counted_util.h" |
| 21 #include "chrome/common/sqlite_compiled_statement.h" | |
| 22 #include "chrome/common/thumbnail_score.h" | |
| 23 #include "testing/gtest/include/gtest/gtest_prod.h" | 20 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 24 | 21 |
| 25 class DictionaryValue; | 22 class DictionaryValue; |
| 26 class GURL; | 23 class GURL; |
| 27 class HistoryService; | 24 class HistoryService; |
| 25 class Pickle; |
| 28 class Profile; | 26 class Profile; |
| 29 class SkBitmap; | 27 class SkBitmap; |
| 30 struct sqlite3; | 28 struct ThumbnailScore; |
| 31 namespace base { | 29 namespace base { |
| 32 class Time; | 30 class Time; |
| 33 } | 31 } |
| 34 | 32 |
| 35 // This storage interface provides storage for the thumbnails used | 33 // This storage interface provides storage for the thumbnails used |
| 36 // by the new_tab_ui. | 34 // by the new_tab_ui. |
| 37 class ThumbnailStore : public base::RefCountedThreadSafe<ThumbnailStore> { | 35 class ThumbnailStore : public base::RefCountedThreadSafe<ThumbnailStore> { |
| 38 public: | 36 public: |
| 39 ThumbnailStore(); | 37 ThumbnailStore(); |
| 40 ~ThumbnailStore(); | 38 ~ThumbnailStore(); |
| 41 | 39 |
| 42 // Must be called after creation but before other methods are called. | 40 // Must be called after creation but before other methods are called. |
| 43 void Init(const FilePath& db_name, // The location of the database. | 41 // file_path is a directory where a new database should be created |
| 44 Profile* profile); // To get to the HistoryService. | 42 // or the location of an existing databse. |
| 43 void Init(const FilePath& file_path, Profile* profile); |
| 45 | 44 |
| 46 // Stores the given thumbnail and score with the associated url in the cache. | 45 // Stores the given thumbnail and score with the associated url in the cache. |
| 46 // If write_to_disk is true, the thumbnail data is written to disk on the |
| 47 // file_thread. |
| 47 bool SetPageThumbnail(const GURL& url, | 48 bool SetPageThumbnail(const GURL& url, |
| 48 const SkBitmap& thumbnail, | 49 const SkBitmap& thumbnail, |
| 49 const ThumbnailScore& score); | 50 const ThumbnailScore& score, |
| 51 bool write_to_disk); |
| 50 | 52 |
| 51 // Sets *data to point to the thumbnail for the given url. | 53 // Sets *data to point to the thumbnail for the given url. |
| 52 // Returns false if no thumbnail available. | 54 // Returns false if no thumbnail available. |
| 53 bool GetPageThumbnail(const GURL& url, RefCountedBytes** data); | 55 bool GetPageThumbnail(const GURL& url, RefCountedBytes** data); |
| 54 | 56 |
| 55 private: | 57 private: |
| 56 FRIEND_TEST(ThumbnailStoreTest, RetrieveFromCache); | 58 FRIEND_TEST(ThumbnailStoreTest, RetrieveFromCache); |
| 57 FRIEND_TEST(ThumbnailStoreTest, RetrieveFromDisk); | 59 FRIEND_TEST(ThumbnailStoreTest, RetrieveFromDisk); |
| 58 FRIEND_TEST(ThumbnailStoreTest, UpdateThumbnail); | 60 FRIEND_TEST(ThumbnailStoreTest, UpdateThumbnail); |
| 59 FRIEND_TEST(ThumbnailStoreTest, FollowRedirects); | 61 FRIEND_TEST(ThumbnailStoreTest, FollowRedirects); |
| 60 friend class ThumbnailStoreTest; | 62 friend class ThumbnailStoreTest; |
| 61 | 63 |
| 62 struct CacheEntry { | |
| 63 scoped_refptr<RefCountedBytes> data_; | |
| 64 ThumbnailScore score_; | |
| 65 bool dirty_; | |
| 66 | |
| 67 CacheEntry() : data_(NULL), score_(ThumbnailScore()), dirty_(false) {} | |
| 68 CacheEntry(RefCountedBytes* data, | |
| 69 const ThumbnailScore& score, | |
| 70 bool dirty) | |
| 71 : data_(data), | |
| 72 score_(score), | |
| 73 dirty_(dirty) {} | |
| 74 }; | |
| 75 | |
| 76 // Data structure used to store thumbnail data in memory. | 64 // Data structure used to store thumbnail data in memory. |
| 77 typedef std::map<GURL, CacheEntry> Cache; | 65 typedef std::map<GURL, std::pair<scoped_refptr<RefCountedBytes>, |
| 66 ThumbnailScore> > Cache; |
| 78 | 67 |
| 79 // Most visited URLs and their redirect lists ------------------------------- | 68 // Most visited URLs and their redirect lists ------------------------------- |
| 80 | 69 |
| 81 // Query the HistoryService for the most visited URLs and the most recent | 70 // Query the HistoryService for the most visited URLs and the most recent |
| 82 // redirect lists for those URLs. This happens in the background and the | 71 // redirect lists for those URLs. This happens in the background and the |
| 83 // callback is OnURLDataAvailable. | 72 // callback is OnURLDataAvailable. |
| 84 void UpdateURLData(); | 73 void UpdateURLData(); |
| 85 | 74 |
| 86 // The callback for UpdateURLData. | 75 // The callback for UpdateURLData. The ThumbnailStore takes ownership of |
| 76 // the most visited urls list and redirect lists passed in. |
| 87 void OnURLDataAvailable(std::vector<GURL>* urls, | 77 void OnURLDataAvailable(std::vector<GURL>* urls, |
| 88 history::RedirectMap* redirects); | 78 history::RedirectMap* redirects); |
| 89 | 79 |
| 90 // Remove stale data -------------------------------------------------------- | 80 // Remove stale data -------------------------------------------------------- |
| 91 | 81 |
| 92 // Remove entries from the in memory thumbnail cache and redirect lists | 82 // Remove entries from the in memory thumbnail cache and redirect lists |
| 93 // cache that have been blacklisted or are not in the top kMaxCacheSize | 83 // cache that have been blacklisted or are not in the top kMaxCacheSize |
| 94 // visited sites. | 84 // visited sites. |
| 95 void CleanCacheData(); | 85 void CleanCacheData(); |
| 96 | 86 |
| 87 // Deletes thumbnail data from disk for the given list of urls. |
| 88 void DeleteThumbnails( |
| 89 scoped_refptr<RefCountedVector<GURL> > thumbnail_urls) const; |
| 90 |
| 97 // Disk operations ---------------------------------------------------------- | 91 // Disk operations ---------------------------------------------------------- |
| 98 | 92 |
| 99 // Initialize |db_| to the database specified in |db_name|. If |cb_loop| | |
| 100 // is non-null, calls GetAllThumbnailsFromDisk. Done on the file_thread. | |
| 101 void InitializeFromDB(const FilePath& db_name, MessageLoop* cb_loop); | |
| 102 | |
| 103 // Read all thumbnail data from the specified FilePath into a Cache object. | 93 // Read all thumbnail data from the specified FilePath into a Cache object. |
| 104 // Done on the file_thread and returns to OnDiskDataAvailable on the thread | 94 // Done on the file_thread and returns to OnDiskDataAvailable on the thread |
| 105 // owning the specified MessageLoop. | 95 // owning the specified MessageLoop. |
| 106 void GetAllThumbnailsFromDisk(MessageLoop* cb_loop); | 96 void GetAllThumbnailsFromDisk(FilePath filepath, MessageLoop* cb_loop); |
| 97 |
| 98 // Read the thumbnail data from the given file and stores it in the |
| 99 // out parameters GURL, SkBitmap, and ThumbnailScore. |
| 100 bool GetPageThumbnailFromDisk(const FilePath& file, |
| 101 GURL* url, |
| 102 RefCountedBytes* data, |
| 103 ThumbnailScore* score) const; |
| 107 | 104 |
| 108 // Once thumbnail data from the disk is available from the file_thread, | 105 // Once thumbnail data from the disk is available from the file_thread, |
| 109 // this function is invoked on the main thread. It takes ownership of the | 106 // this function is invoked on the main thread. It takes ownership of the |
| 110 // Cache* passed in and retains this Cache* for the lifetime of the object. | 107 // Cache* passed in and retains this Cache* for the lifetime of the object. |
| 111 void OnDiskDataAvailable(Cache* cache); | 108 void OnDiskDataAvailable(Cache* cache); |
| 112 | 109 |
| 113 // Delete each URL in the given vector from the DB and write all dirty | 110 // Write thumbnail data to disk for a given url. |
| 114 // cache entries to the DB. | 111 bool WriteThumbnailToDisk(const GURL& url, |
| 115 void CommitCacheToDB( | 112 scoped_refptr<RefCountedBytes> data, |
| 116 scoped_refptr<RefCountedVector<GURL> > stale_urls) const; | 113 const ThumbnailScore& score) const; |
| 114 |
| 115 |
| 116 // Pack the given ThumbnailScore into the given Pickle. |
| 117 void PackScore(const ThumbnailScore& score, Pickle* packed) const; |
| 118 |
| 119 // Unpack a ThumbnailScore from a given Pickle and associated iterator. |
| 120 // Returns false is a ThumbnailScore could not be unpacked. |
| 121 bool UnpackScore(ThumbnailScore* score, |
| 122 const Pickle& packed, |
| 123 void*& iter) const; |
| 117 | 124 |
| 118 // Decide whether to store data --------------------------------------------- | 125 // Decide whether to store data --------------------------------------------- |
| 119 | 126 |
| 120 bool ShouldStoreThumbnailForURL(const GURL& url) const; | 127 bool ShouldStoreThumbnailForURL(const GURL& url) const; |
| 121 | 128 |
| 122 bool IsURLBlacklisted(const GURL& url) const; | 129 bool IsURLBlacklisted(const GURL& url) const; |
| 123 | 130 |
| 124 std::wstring GetDictionaryKeyForURL(const std::string& url) const; | 131 std::wstring GetDictionaryKeyForURL(const std::string& url) const; |
| 125 | 132 |
| 126 // Returns true if url is in most_visited_urls_. | 133 // Returns true if url is in most_visited_urls_. |
| 127 bool IsPopular(const GURL& url) const; | 134 bool IsPopular(const GURL& url) const; |
| 128 | 135 |
| 129 | 136 |
| 130 | 137 |
| 131 // Member variables --------------------------------------------------------- | 138 // Member variables --------------------------------------------------------- |
| 132 | 139 |
| 133 // The Cache maintained by the object. | 140 // The Cache maintained by the object. |
| 134 scoped_ptr<Cache> cache_; | 141 scoped_ptr<Cache> cache_; |
| 142 bool cache_initialized_; |
| 135 | 143 |
| 136 // The database holding the thumbnails on disk. | 144 // The location of the thumbnail store. |
| 137 sqlite3* db_; | 145 FilePath file_path_; |
| 138 SqliteStatementCache* statement_cache_; | |
| 139 history::DBCloseScoper close_scoper_; | |
| 140 | 146 |
| 141 // We hold a reference to the history service to query for most visited URLs | 147 // We hold a reference to the history service to query for most visited URLs |
| 142 // and redirect information. | 148 // and redirect information. |
| 143 scoped_refptr<HistoryService> hs_; | 149 scoped_refptr<HistoryService> hs_; |
| 144 | 150 |
| 145 // A list of the most_visited_urls_ refreshed every 30mins from the | 151 // A list of the most_visited_urls_ refreshed every 30mins from the |
| 146 // HistoryService. | 152 // HistoryService. |
| 147 scoped_ptr<std::vector<GURL> > most_visited_urls_; | 153 scoped_ptr<std::vector<GURL> > most_visited_urls_; |
| 148 | 154 |
| 149 // A pointer to the persistent URL blacklist for this profile. | 155 // A pointer to the persistent URL blacklist for this profile. |
| 150 const DictionaryValue* url_blacklist_; | 156 const DictionaryValue* url_blacklist_; |
| 151 | 157 |
| 152 // A map pairing the URL that a user typed to a list of URLs it was | 158 // A map pairing the URL that a user typed to a list of URLs it was |
| 153 // redirected to. Ex: | 159 // redirected to. Ex: |
| 154 // google.com => { http://www.google.com/ } | 160 // google.com => { http://www.google.com/ } |
| 155 scoped_ptr<history::RedirectMap> redirect_urls_; | 161 scoped_ptr<history::RedirectMap> redirect_urls_; |
| 156 | 162 |
| 157 // Timer on which UpdateURLData runs. | 163 // Timer on which UpdateURLData runs. |
| 158 base::RepeatingTimer<ThumbnailStore> timer_; | 164 base::RepeatingTimer<ThumbnailStore> timer_; |
| 159 | 165 |
| 160 // Consumer for queries to the HistoryService. | 166 // Consumer for queries to the HistoryService. |
| 161 CancelableRequestConsumer consumer_; | 167 CancelableRequestConsumer consumer_; |
| 162 | 168 |
| 163 static const unsigned int kMaxCacheSize = 45; | 169 static const unsigned int kMaxCacheSize = 45; |
| 164 | 170 |
| 165 DISALLOW_COPY_AND_ASSIGN(ThumbnailStore); | 171 DISALLOW_COPY_AND_ASSIGN(ThumbnailStore); |
| 166 }; | 172 }; |
| 167 | 173 |
| 168 #endif // CHROME_BROWSER_THUMBNAIL_STORE_H_ | 174 #endif // CHROME_BROWSER_THUMBNAIL_STORE_H_ |
| OLD | NEW |