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