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 <set> | |
10 #include <string> | 9 #include <string> |
11 #include <vector> | 10 #include <vector> |
12 | 11 |
13 #include "base/file_path.h" | 12 #include "base/file_path.h" |
14 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
15 #include "base/ref_counted.h" | 14 #include "base/ref_counted.h" |
16 #include "base/timer.h" | 15 #include "base/timer.h" |
17 #include "chrome/browser/cancelable_request.h" | 16 #include "chrome/browser/cancelable_request.h" |
18 #include "chrome/browser/history/history.h" | 17 #include "chrome/browser/history/history.h" |
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 "testing/gtest/include/gtest/gtest_prod.h" | 20 #include "testing/gtest/include/gtest/gtest_prod.h" |
22 | 21 |
23 class DictionaryValue; | 22 class DictionaryValue; |
24 class GURL; | 23 class GURL; |
25 class HistoryService; | 24 class HistoryService; |
26 class PageUsageData; | |
27 class Pickle; | 25 class Pickle; |
28 class Profile; | 26 class Profile; |
29 class SkBitmap; | 27 class SkBitmap; |
30 struct ThumbnailScore; | 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 typedef Callback2<int, scoped_refptr<RefCountedBytes> >::Type | |
40 ThumbnailDataCallback; | |
41 | |
42 ThumbnailStore(); | 37 ThumbnailStore(); |
43 ~ThumbnailStore(); | 38 ~ThumbnailStore(); |
44 | 39 |
45 // Must be called after creation but before other methods are called. | 40 // Must be called after creation but before other methods are called. |
46 // file_path is a directory where a new database should be created | 41 // file_path is a directory where a new database should be created |
47 // or the location of an existing databse. | 42 // or the location of an existing databse. |
48 void Init(const FilePath& file_path, Profile* profile); | 43 void Init(const FilePath& file_path, Profile* profile); |
49 | 44 |
50 // 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. |
51 // If write_to_disk is true, the thumbnail data is written to disk on the | 46 // If write_to_disk is true, the thumbnail data is written to disk on the |
52 // file_thread. | 47 // file_thread. |
53 bool SetPageThumbnail(const GURL& url, | 48 bool SetPageThumbnail(const GURL& url, |
54 const SkBitmap& thumbnail, | 49 const SkBitmap& thumbnail, |
55 const ThumbnailScore& score, | 50 const ThumbnailScore& score, |
56 bool write_to_disk); | 51 bool write_to_disk); |
57 | 52 |
58 // Sets *data to point to the thumbnail for the given url. | 53 // Sets *data to point to the thumbnail for the given url. |
59 // A return value of ASYNC means you should call GetPageThumbnailAsync. | 54 // Returns false if no thumbnail available. |
60 // On a return value of SUCCESS, the refcount of the out parameter data | 55 bool GetPageThumbnail(const GURL& url, RefCountedBytes** data); |
61 // is incremented for the caller who takes ownership of that reference. | |
62 enum GetStatus { SUCCESS, FAIL, ASYNC }; | |
63 ThumbnailStore::GetStatus GetPageThumbnail(const GURL& url, | |
64 RefCountedBytes** data); | |
65 | |
66 // Retrieves the redirects list for the given url asynchronously. | |
67 // Calls the callback with the request_id and thumbnail data if found. | |
68 void GetPageThumbnailAsync(const GURL& url, | |
69 int request_id, | |
70 ThumbnailStore::ThumbnailDataCallback* cb); | |
71 | |
72 // Cancels the given set of request_id's which were issued from | |
73 // GetPageThumbnailAsync. | |
74 // This method is called from ~DOMUIThumbnailSource. If a | |
75 // DOMUIThumbnailSource requests a thumbnail but is destroyed before the | |
76 // data is sent back, this method will cancel the request and delete the | |
77 // callback. | |
78 void CancelPendingRequests(const std::set<int>& pending_requests); | |
79 | 56 |
80 private: | 57 private: |
81 FRIEND_TEST(ThumbnailStoreTest, RetrieveFromCache); | 58 FRIEND_TEST(ThumbnailStoreTest, RetrieveFromCache); |
82 FRIEND_TEST(ThumbnailStoreTest, RetrieveFromDisk); | 59 FRIEND_TEST(ThumbnailStoreTest, RetrieveFromDisk); |
83 FRIEND_TEST(ThumbnailStoreTest, UpdateThumbnail); | 60 FRIEND_TEST(ThumbnailStoreTest, UpdateThumbnail); |
84 FRIEND_TEST(ThumbnailStoreTest, FollowRedirects); | 61 FRIEND_TEST(ThumbnailStoreTest, FollowRedirects); |
| 62 friend class ThumbnailStoreTest; |
85 | 63 |
86 // Data structure used to store thumbnail data in memory. | 64 // Data structure used to store thumbnail data in memory. |
87 typedef std::map<GURL, std::pair<scoped_refptr<RefCountedBytes>, | 65 typedef std::map<GURL, std::pair<scoped_refptr<RefCountedBytes>, |
88 ThumbnailScore> > Cache; | 66 ThumbnailScore> > Cache; |
89 | 67 |
90 // Data structure used to cache the redirect lists for urls. | 68 // Most visited URLs and their redirect lists ------------------------------- |
91 typedef std::map<GURL, scoped_refptr<RefCountedVector<GURL> > > RedirectMap; | |
92 | 69 |
93 // Data structure used to store request_id's and callbacks for | 70 // Query the HistoryService for the most visited URLs and the most recent |
94 // GetPageThumbnailAsync. | 71 // redirect lists for those URLs. This happens in the background and the |
95 typedef std::map<int, std::pair<ThumbnailStore::ThumbnailDataCallback*, | 72 // callback is OnURLDataAvailable. |
96 HistoryService::Handle> > RequestMap; | 73 void UpdateURLData(); |
| 74 |
| 75 // The callback for UpdateURLData. The ThumbnailStore takes ownership of |
| 76 // the most visited urls list and redirect lists passed in. |
| 77 void OnURLDataAvailable(std::vector<GURL>* urls, |
| 78 history::RedirectMap* redirects); |
| 79 |
| 80 // Remove stale data -------------------------------------------------------- |
| 81 |
| 82 // Remove entries from the in memory thumbnail cache and redirect lists |
| 83 // cache that have been blacklisted or are not in the top kMaxCacheSize |
| 84 // visited sites. |
| 85 void CleanCacheData(); |
97 | 86 |
98 // Deletes thumbnail data from disk for the given list of urls. | 87 // Deletes thumbnail data from disk for the given list of urls. |
99 void DeleteThumbnails( | 88 void DeleteThumbnails( |
100 scoped_refptr<RefCountedVector<GURL> > thumbnail_urls) const; | 89 scoped_refptr<RefCountedVector<GURL> > thumbnail_urls) const; |
101 | 90 |
| 91 // Disk operations ---------------------------------------------------------- |
| 92 |
102 // 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. |
103 // 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 |
104 // owning the specified MessageLoop. | 95 // owning the specified MessageLoop. |
105 void GetAllThumbnailsFromDisk(FilePath filepath, MessageLoop* cb_loop); | 96 void GetAllThumbnailsFromDisk(FilePath filepath, MessageLoop* cb_loop); |
106 | 97 |
107 // Read the thumbnail data from the given file and stores it in the | 98 // Read the thumbnail data from the given file and stores it in the |
108 // out parameters GURL, SkBitmap, and ThumbnailScore. | 99 // out parameters GURL, SkBitmap, and ThumbnailScore. |
109 bool GetPageThumbnailFromDisk(const FilePath& file, | 100 bool GetPageThumbnailFromDisk(const FilePath& file, |
110 GURL* url, | 101 GURL* url, |
111 RefCountedBytes* data, | 102 RefCountedBytes* data, |
112 ThumbnailScore* score) const; | 103 ThumbnailScore* score) const; |
113 | 104 |
114 // 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, |
115 // 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 |
116 // 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. |
117 void OnDiskDataAvailable(ThumbnailStore::Cache* cache); | 108 void OnDiskDataAvailable(Cache* cache); |
118 | 109 |
119 // Write thumbnail data to disk for a given url. | 110 // Write thumbnail data to disk for a given url. |
120 bool WriteThumbnailToDisk(const GURL& url, | 111 bool WriteThumbnailToDisk(const GURL& url, |
121 scoped_refptr<RefCountedBytes> data, | 112 scoped_refptr<RefCountedBytes> data, |
122 const ThumbnailScore& score) const; | 113 const ThumbnailScore& score) const; |
123 | 114 |
| 115 |
124 // Pack the given ThumbnailScore into the given Pickle. | 116 // Pack the given ThumbnailScore into the given Pickle. |
125 void PackScore(const ThumbnailScore& score, Pickle* packed) const; | 117 void PackScore(const ThumbnailScore& score, Pickle* packed) const; |
126 | 118 |
127 // Unpack a ThumbnailScore from a given Pickle and associated iterator. | 119 // Unpack a ThumbnailScore from a given Pickle and associated iterator. |
128 // Returns false is a ThumbnailScore could not be unpacked. | 120 // Returns false is a ThumbnailScore could not be unpacked. |
129 bool UnpackScore(ThumbnailScore* score, | 121 bool UnpackScore(ThumbnailScore* score, |
130 const Pickle& packed, | 122 const Pickle& packed, |
131 void*& iter) const; | 123 void*& iter) const; |
132 | 124 |
| 125 // Decide whether to store data --------------------------------------------- |
| 126 |
133 bool ShouldStoreThumbnailForURL(const GURL& url) const; | 127 bool ShouldStoreThumbnailForURL(const GURL& url) const; |
134 | 128 |
135 bool IsURLBlacklisted(const GURL& url) const; | 129 bool IsURLBlacklisted(const GURL& url) const; |
136 | 130 |
137 std::wstring GetDictionaryKeyForURL(const std::string& url) const; | 131 std::wstring GetDictionaryKeyForURL(const std::string& url) const; |
138 | 132 |
139 // Returns true if url is in most_visited_urls_. | 133 // Returns true if url is in most_visited_urls_. |
140 bool IsPopular(const GURL& url) const; | 134 bool IsPopular(const GURL& url) const; |
141 | 135 |
142 // The callback for GetPageThumbnailAsync. Caches the redirect list | |
143 // and calls the callback for the request asssociated with the url. | |
144 void OnRedirectQueryComplete(HistoryService::Handle request_handle, | |
145 GURL url, | |
146 bool success, | |
147 history::RedirectList* redirects); | |
148 | 136 |
149 // Called on a timer initiated in Init(). Calls the HistoryService to | |
150 // update the list of most visited URLs. The callback is | |
151 // OnMostVisitedURLsAvailable. | |
152 void UpdateMostVisited(); | |
153 | 137 |
154 // Updates the list of most visited URLs. Then calls CleanCacheData. | 138 // Member variables --------------------------------------------------------- |
155 void OnMostVisitedURLsAvailable(CancelableRequestProvider::Handle handle, | |
156 std::vector<PageUsageData*>* data); | |
157 | |
158 // Remove entries from the in memory thumbnail cache and redirect lists | |
159 // cache that have been blacklisted or are not in the top kMaxCacheSize | |
160 // visited sites. | |
161 void CleanCacheData(); | |
162 | 139 |
163 // The Cache maintained by the object. | 140 // The Cache maintained by the object. |
164 scoped_ptr<ThumbnailStore::Cache> cache_; | 141 scoped_ptr<Cache> cache_; |
165 bool cache_initialized_; | 142 bool cache_initialized_; |
166 | 143 |
167 // The location of the thumbnail store. | 144 // The location of the thumbnail store. |
168 FilePath file_path_; | 145 FilePath file_path_; |
169 | 146 |
| 147 // We hold a reference to the history service to query for most visited URLs |
| 148 // and redirect information. |
170 scoped_refptr<HistoryService> hs_; | 149 scoped_refptr<HistoryService> hs_; |
171 | 150 |
172 // 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 |
173 // HistoryService. | 152 // HistoryService. |
174 std::vector<GURL> most_visited_urls_; | 153 scoped_ptr<std::vector<GURL> > most_visited_urls_; |
175 | 154 |
176 // A pointer to the persistent URL blacklist for this profile. | 155 // A pointer to the persistent URL blacklist for this profile. |
177 const DictionaryValue* url_blacklist_; | 156 const DictionaryValue* url_blacklist_; |
178 | 157 |
179 // 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 |
180 // redirected to. Ex: | 159 // redirected to. Ex: |
181 // google.com => { http://www.google.com/ } | 160 // google.com => { http://www.google.com/ } |
182 ThumbnailStore::RedirectMap redirect_urls_; | 161 scoped_ptr<history::RedirectMap> redirect_urls_; |
183 | 162 |
184 // When GetPageThumbnailAsync is called, this map records the request_id | 163 // Timer on which UpdateURLData runs. |
185 // and callback associated with the request. When the thumbnail becomes | |
186 // available, the callback is taken from this map and the thumbnail data | |
187 // is returned to it. | |
188 ThumbnailStore::RequestMap redirect_requests_; | |
189 | |
190 // Timer on which UpdateMostVisited runs. | |
191 base::RepeatingTimer<ThumbnailStore> timer_; | 164 base::RepeatingTimer<ThumbnailStore> timer_; |
192 | 165 |
193 // Consumer for getting redirect lists from the history service. | 166 // Consumer for queries to the HistoryService. |
194 CancelableRequestConsumerT<int, -1> hs_consumer_; | 167 CancelableRequestConsumer consumer_; |
195 | 168 |
196 // Consumer for getting the list of most visited urls. | |
197 CancelableRequestConsumerTSimple<PageUsageData*> cancelable_consumer_; | |
198 | |
199 static const int kMostVisitedScope = 90; | |
200 static const unsigned int kMaxCacheSize = 45; | 169 static const unsigned int kMaxCacheSize = 45; |
201 | 170 |
202 DISALLOW_COPY_AND_ASSIGN(ThumbnailStore); | 171 DISALLOW_COPY_AND_ASSIGN(ThumbnailStore); |
203 }; | 172 }; |
204 | 173 |
205 #endif // CHROME_BROWSER_THUMBNAIL_STORE_H_ | 174 #endif // CHROME_BROWSER_THUMBNAIL_STORE_H_ |
OLD | NEW |