OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_HISTORY_EXPIRE_HISTORY_BACKEND_H__ | 5 #ifndef CHROME_BROWSER_HISTORY_EXPIRE_HISTORY_BACKEND_H__ |
6 #define CHROME_BROWSER_HISTORY_EXPIRE_HISTORY_BACKEND_H__ | 6 #define CHROME_BROWSER_HISTORY_EXPIRE_HISTORY_BACKEND_H__ |
7 | 7 |
| 8 #include <queue> |
8 #include <set> | 9 #include <set> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
12 #include "base/task.h" | 13 #include "base/task.h" |
13 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "base/scoped_ptr.h" |
14 #include "chrome/browser/history/history_types.h" | 16 #include "chrome/browser/history/history_types.h" |
15 #include "chrome/browser/history/text_database_manager.h" | 17 #include "chrome/browser/history/text_database_manager.h" |
16 #include "testing/gtest/include/gtest/gtest_prod.h" | 18 #include "testing/gtest/include/gtest/gtest_prod.h" |
17 | 19 |
18 class BookmarkService; | 20 class BookmarkService; |
19 class GURL; | 21 class GURL; |
20 class NotificationType; | 22 class NotificationType; |
21 class TestingProfile; | 23 class TestingProfile; |
22 | 24 |
23 namespace history { | 25 namespace history { |
24 | 26 |
25 class ArchivedDatabase; | 27 class ArchivedDatabase; |
26 class HistoryDatabase; | 28 class HistoryDatabase; |
27 struct HistoryDetails; | 29 struct HistoryDetails; |
28 class ThumbnailDatabase; | 30 class ThumbnailDatabase; |
29 | 31 |
30 // Delegate used to broadcast notifications to the main thread. | 32 // Delegate used to broadcast notifications to the main thread. |
31 class BroadcastNotificationDelegate { | 33 class BroadcastNotificationDelegate { |
32 public: | 34 public: |
33 // Schedules a broadcast of the given notification on the application main | 35 // Schedules a broadcast of the given notification on the application main |
34 // thread. The details argument will have ownership taken by this function. | 36 // thread. The details argument will have ownership taken by this function. |
35 virtual void BroadcastNotifications(NotificationType type, | 37 virtual void BroadcastNotifications(NotificationType type, |
36 HistoryDetails* details_deleted) = 0; | 38 HistoryDetails* details_deleted) = 0; |
37 }; | 39 }; |
38 | 40 |
| 41 // Encapsulates visit expiration criteria and type of visits to expire. |
| 42 class ExpiringVisitsReader { |
| 43 public: |
| 44 virtual ~ExpiringVisitsReader() {} |
| 45 // Populates |visits| from |db|, using provided |end_time| and |max_visits| |
| 46 // cap. |
| 47 virtual bool Read(base::Time end_time, HistoryDatabase* db, |
| 48 VisitVector* visits, int max_visits) const = 0; |
| 49 }; |
| 50 |
| 51 typedef std::vector<const ExpiringVisitsReader*> ExpiringVisitsReaders; |
| 52 |
39 // Helper component to HistoryBackend that manages expiration and deleting of | 53 // Helper component to HistoryBackend that manages expiration and deleting of |
40 // history, as well as moving data from the main database to the archived | 54 // history, as well as moving data from the main database to the archived |
41 // database as it gets old. | 55 // database as it gets old. |
42 // | 56 // |
43 // It will automatically start periodically archiving old history once you call | 57 // It will automatically start periodically archiving old history once you call |
44 // StartArchivingOldStuff(). | 58 // StartArchivingOldStuff(). |
45 class ExpireHistoryBackend { | 59 class ExpireHistoryBackend { |
46 public: | 60 public: |
47 // The delegate pointer must be non-NULL. We will NOT take ownership of it. | 61 // The delegate pointer must be non-NULL. We will NOT take ownership of it. |
48 // BookmarkService may be NULL. The BookmarkService is used when expiring | 62 // BookmarkService may be NULL. The BookmarkService is used when expiring |
(...skipping 30 matching lines...) Expand all Loading... |
79 // not save it. | 93 // not save it. |
80 base::Time GetCurrentArchiveTime() const { | 94 base::Time GetCurrentArchiveTime() const { |
81 return base::Time::Now() - expiration_threshold_; | 95 return base::Time::Now() - expiration_threshold_; |
82 } | 96 } |
83 | 97 |
84 private: | 98 private: |
85 //friend class ExpireHistoryTest_DeleteFaviconsIfPossible_Test; | 99 //friend class ExpireHistoryTest_DeleteFaviconsIfPossible_Test; |
86 FRIEND_TEST(ExpireHistoryTest, DeleteTextIndexForURL); | 100 FRIEND_TEST(ExpireHistoryTest, DeleteTextIndexForURL); |
87 FRIEND_TEST(ExpireHistoryTest, DeleteFaviconsIfPossible); | 101 FRIEND_TEST(ExpireHistoryTest, DeleteFaviconsIfPossible); |
88 FRIEND_TEST(ExpireHistoryTest, ArchiveSomeOldHistory); | 102 FRIEND_TEST(ExpireHistoryTest, ArchiveSomeOldHistory); |
| 103 FRIEND_TEST(ExpireHistoryTest, ExpiringVisitsReader); |
89 friend class ::TestingProfile; | 104 friend class ::TestingProfile; |
90 | 105 |
91 struct DeleteDependencies { | 106 struct DeleteDependencies { |
92 // The time range affected. These can be is_null() to be unbounded in one | 107 // The time range affected. These can be is_null() to be unbounded in one |
93 // or both directions. | 108 // or both directions. |
94 base::Time begin_time, end_time; | 109 base::Time begin_time, end_time; |
95 | 110 |
96 // ----- Filled by DeleteVisitRelatedInfo or manually if a function doesn't | 111 // ----- Filled by DeleteVisitRelatedInfo or manually if a function doesn't |
97 // call that function. ----- | 112 // call that function. ----- |
98 | 113 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 void ArchiveURLsAndVisits(const VisitVector& visits, | 215 void ArchiveURLsAndVisits(const VisitVector& visits, |
201 DeleteDependencies* dependencies); | 216 DeleteDependencies* dependencies); |
202 | 217 |
203 // Deletes the favicons listed in the set if unused. Fails silently (we don't | 218 // Deletes the favicons listed in the set if unused. Fails silently (we don't |
204 // care about favicons so much, so don't want to stop everything if it fails). | 219 // care about favicons so much, so don't want to stop everything if it fails). |
205 void DeleteFaviconsIfPossible(const std::set<FavIconID>& favicon_id); | 220 void DeleteFaviconsIfPossible(const std::set<FavIconID>& favicon_id); |
206 | 221 |
207 // Broadcast the URL deleted notification. | 222 // Broadcast the URL deleted notification. |
208 void BroadcastDeleteNotifications(DeleteDependencies* dependencies); | 223 void BroadcastDeleteNotifications(DeleteDependencies* dependencies); |
209 | 224 |
210 // Schedules a call to DoArchiveIteration at the given time in the | 225 // Schedules a call to DoArchiveIteration. |
| 226 void ScheduleArchive(); |
| 227 |
| 228 // Calls ArchiveSomeOldHistory to expire some amount of old history, according |
| 229 // to the items in work queue, and schedules another call to happen in the |
211 // future. | 230 // future. |
212 void ScheduleArchive(base::TimeDelta delay); | |
213 | |
214 // Calls ArchiveSomeOldHistory to expire some amount of old history, and | |
215 // schedules another call to happen in the future. | |
216 void DoArchiveIteration(); | 231 void DoArchiveIteration(); |
217 | 232 |
218 // Tries to expire the oldest |max_visits| visits from history that are older | 233 // Tries to expire the oldest |max_visits| visits from history that are older |
219 // than |time_threshold|. The return value indicates if we think there might | 234 // than |time_threshold|. The return value indicates if we think there might |
220 // be more history to expire with the current time threshold (it does not | 235 // be more history to expire with the current time threshold (it does not |
221 // indicate success or failure). | 236 // indicate success or failure). |
222 bool ArchiveSomeOldHistory(base::Time time_threshold, int max_visits); | 237 bool ArchiveSomeOldHistory(base::Time end_time, |
| 238 const ExpiringVisitsReader* reader, |
| 239 int max_visits); |
223 | 240 |
224 // Tries to detect possible bad history or inconsistencies in the database | 241 // Tries to detect possible bad history or inconsistencies in the database |
225 // and deletes items. For example, URLs with no visits. | 242 // and deletes items. For example, URLs with no visits. |
226 void ParanoidExpireHistory(); | 243 void ParanoidExpireHistory(); |
227 | 244 |
228 // Returns the BookmarkService, blocking until it is loaded. This may return | 245 // Returns the BookmarkService, blocking until it is loaded. This may return |
229 // NULL. | 246 // NULL. |
230 BookmarkService* GetBookmarkService(); | 247 BookmarkService* GetBookmarkService(); |
231 | 248 |
| 249 // Initializes periodic expiration work queue by populating it with with tasks |
| 250 // for all known readers. |
| 251 void InitWorkQueue(); |
| 252 |
| 253 // Returns the reader for all visits. This method is only used by the unit |
| 254 // tests. |
| 255 const ExpiringVisitsReader* GetAllVisitsReader(); |
| 256 |
| 257 // Returns the reader for AUTO_SUBFRAME visits. This method is only used by |
| 258 // the unit tests. |
| 259 const ExpiringVisitsReader* GetAutoSubframeVisitsReader(); |
| 260 |
232 // Non-owning pointer to the notification delegate (guaranteed non-NULL). | 261 // Non-owning pointer to the notification delegate (guaranteed non-NULL). |
233 BroadcastNotificationDelegate* delegate_; | 262 BroadcastNotificationDelegate* delegate_; |
234 | 263 |
235 // Non-owning pointers to the databases we deal with (MAY BE NULL). | 264 // Non-owning pointers to the databases we deal with (MAY BE NULL). |
236 HistoryDatabase* main_db_; // Main history database. | 265 HistoryDatabase* main_db_; // Main history database. |
237 ArchivedDatabase* archived_db_; // Old history. | 266 ArchivedDatabase* archived_db_; // Old history. |
238 ThumbnailDatabase* thumb_db_; // Thumbnails and favicons. | 267 ThumbnailDatabase* thumb_db_; // Thumbnails and favicons. |
239 TextDatabaseManager* text_db_; // Full text index. | 268 TextDatabaseManager* text_db_; // Full text index. |
240 | 269 |
241 // Used to generate runnable methods to do timers on this class. They will be | 270 // Used to generate runnable methods to do timers on this class. They will be |
242 // automatically canceled when this class is deleted. | 271 // automatically canceled when this class is deleted. |
243 ScopedRunnableMethodFactory<ExpireHistoryBackend> factory_; | 272 ScopedRunnableMethodFactory<ExpireHistoryBackend> factory_; |
244 | 273 |
245 // The threshold for "old" history where we will automatically expire it to | 274 // The threshold for "old" history where we will automatically expire it to |
246 // the archived database. | 275 // the archived database. |
247 base::TimeDelta expiration_threshold_; | 276 base::TimeDelta expiration_threshold_; |
248 | 277 |
| 278 // List of all distinct types of readers. This list is used to populate the |
| 279 // work queue. |
| 280 ExpiringVisitsReaders readers_; |
| 281 |
| 282 // Work queue for periodic expiration tasks, used by DoArchiveIteration() to |
| 283 // determine what to do at an iteration, as well as populate it for future |
| 284 // iterations. |
| 285 std::queue<const ExpiringVisitsReader*> work_queue_; |
| 286 |
| 287 // Readers for various types of visits. |
| 288 // TODO(dglazkov): If you are adding another one, please consider reorganizing |
| 289 // into a map. |
| 290 scoped_ptr<ExpiringVisitsReader> all_visits_reader_; |
| 291 scoped_ptr<ExpiringVisitsReader> auto_subframe_visits_reader_; |
| 292 |
249 // The BookmarkService; may be null. This is owned by the Profile. | 293 // The BookmarkService; may be null. This is owned by the Profile. |
250 // | 294 // |
251 // Use GetBookmarkService to access this, which makes sure the service is | 295 // Use GetBookmarkService to access this, which makes sure the service is |
252 // loaded. | 296 // loaded. |
253 BookmarkService* bookmark_service_; | 297 BookmarkService* bookmark_service_; |
254 | 298 |
255 DISALLOW_EVIL_CONSTRUCTORS(ExpireHistoryBackend); | 299 DISALLOW_EVIL_CONSTRUCTORS(ExpireHistoryBackend); |
256 }; | 300 }; |
257 | 301 |
258 } // namespace history | 302 } // namespace history |
259 | 303 |
260 #endif // CHROME_BROWSER_HISTORY_EXPIRE_HISTORY_BACKEND_H__ | 304 #endif // CHROME_BROWSER_HISTORY_EXPIRE_HISTORY_BACKEND_H__ |
OLD | NEW |