| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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_EXPIRE_HISTORY_BACKEND_H_ | |
| 6 #define CHROME_BROWSER_HISTORY_EXPIRE_HISTORY_BACKEND_H_ | |
| 7 | |
| 8 #include <queue> | |
| 9 #include <set> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/gtest_prod_util.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "components/history/core/browser/history_types.h" | |
| 18 | |
| 19 class GURL; | |
| 20 class TestingProfile; | |
| 21 | |
| 22 namespace history { | |
| 23 | |
| 24 class HistoryBackendNotifier; | |
| 25 class HistoryClient; | |
| 26 class HistoryDatabase; | |
| 27 class ThumbnailDatabase; | |
| 28 | |
| 29 // Encapsulates visit expiration criteria and type of visits to expire. | |
| 30 class ExpiringVisitsReader { | |
| 31 public: | |
| 32 virtual ~ExpiringVisitsReader() {} | |
| 33 // Populates |visits| from |db|, using provided |end_time| and |max_visits| | |
| 34 // cap. | |
| 35 virtual bool Read(base::Time end_time, HistoryDatabase* db, | |
| 36 VisitVector* visits, int max_visits) const = 0; | |
| 37 }; | |
| 38 | |
| 39 typedef std::vector<const ExpiringVisitsReader*> ExpiringVisitsReaders; | |
| 40 | |
| 41 // Helper component to HistoryBackend that manages expiration and deleting of | |
| 42 // history. | |
| 43 // | |
| 44 // It will automatically start periodically expiring old history once you call | |
| 45 // StartExpiringOldStuff(). | |
| 46 class ExpireHistoryBackend { | |
| 47 public: | |
| 48 // The delegate pointer must be non-NULL. We will NOT take ownership of it. | |
| 49 // HistoryClient may be NULL. The HistoryClient is used when expiring URLS so | |
| 50 // that we don't remove any URLs or favicons that are bookmarked (visits are | |
| 51 // removed though). | |
| 52 ExpireHistoryBackend(HistoryBackendNotifier* notifier, | |
| 53 HistoryClient* history_client); | |
| 54 ~ExpireHistoryBackend(); | |
| 55 | |
| 56 // Completes initialization by setting the databases that this class will use. | |
| 57 void SetDatabases(HistoryDatabase* main_db, | |
| 58 ThumbnailDatabase* thumb_db); | |
| 59 | |
| 60 // Begins periodic expiration of history older than the given threshold. This | |
| 61 // will continue until the object is deleted. | |
| 62 void StartExpiringOldStuff(base::TimeDelta expiration_threshold); | |
| 63 | |
| 64 // Deletes everything associated with a URL. | |
| 65 void DeleteURL(const GURL& url); | |
| 66 | |
| 67 // Deletes everything associated with each URL in the list. | |
| 68 void DeleteURLs(const std::vector<GURL>& url); | |
| 69 | |
| 70 // Removes all visits to restrict_urls (or all URLs if empty) in the given | |
| 71 // time range, updating the URLs accordingly. | |
| 72 void ExpireHistoryBetween(const std::set<GURL>& restrict_urls, | |
| 73 base::Time begin_time, base::Time end_time); | |
| 74 | |
| 75 // Removes all visits to all URLs with the given times, updating the | |
| 76 // URLs accordingly. |times| must be in reverse chronological order | |
| 77 // and not contain any duplicates. | |
| 78 void ExpireHistoryForTimes(const std::vector<base::Time>& times); | |
| 79 | |
| 80 // Removes the given list of visits, updating the URLs accordingly (similar to | |
| 81 // ExpireHistoryBetween(), but affecting a specific set of visits). | |
| 82 void ExpireVisits(const VisitVector& visits); | |
| 83 | |
| 84 // Expires all visits before and including the given time, updating the URLs | |
| 85 // accordingly. Currently only used for testing. | |
| 86 void ExpireHistoryBefore(base::Time end_time); | |
| 87 | |
| 88 // Returns the current cut-off time before which we will start expiring stuff. | |
| 89 // Note that this as an absolute time rather than a delta, so the caller | |
| 90 // should not save it. | |
| 91 base::Time GetCurrentExpirationTime() const { | |
| 92 return base::Time::Now() - expiration_threshold_; | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 FRIEND_TEST_ALL_PREFIXES(ExpireHistoryTest, DeleteFaviconsIfPossible); | |
| 97 FRIEND_TEST_ALL_PREFIXES(ExpireHistoryTest, ExpireSomeOldHistory); | |
| 98 FRIEND_TEST_ALL_PREFIXES(ExpireHistoryTest, ExpiringVisitsReader); | |
| 99 FRIEND_TEST_ALL_PREFIXES(ExpireHistoryTest, ExpireSomeOldHistoryWithSource); | |
| 100 friend class ::TestingProfile; | |
| 101 | |
| 102 struct DeleteEffects { | |
| 103 DeleteEffects(); | |
| 104 ~DeleteEffects(); | |
| 105 | |
| 106 // The time range affected. These can be is_null() to be unbounded in one | |
| 107 // or both directions. | |
| 108 base::Time begin_time, end_time; | |
| 109 | |
| 110 // The unique URL rows affected by this delete. | |
| 111 std::map<URLID, URLRow> affected_urls; | |
| 112 | |
| 113 // The URLs modified, but not deleted, during this operation. | |
| 114 URLRows modified_urls; | |
| 115 | |
| 116 // The URLs deleted during this operation. | |
| 117 URLRows deleted_urls; | |
| 118 | |
| 119 // All favicon IDs that the deleted URLs had. Favicons will be shared | |
| 120 // between all URLs with the same favicon, so this is the set of IDs that we | |
| 121 // will need to check when the delete operations are complete. | |
| 122 std::set<favicon_base::FaviconID> affected_favicons; | |
| 123 | |
| 124 // All favicon urls that were actually deleted from the thumbnail db. | |
| 125 std::set<GURL> deleted_favicons; | |
| 126 }; | |
| 127 | |
| 128 // Deletes the visit-related stuff for all the visits in the given list, and | |
| 129 // adds the rows for unique URLs affected to the affected_urls list in | |
| 130 // the dependencies structure. | |
| 131 void DeleteVisitRelatedInfo(const VisitVector& visits, | |
| 132 DeleteEffects* effects); | |
| 133 | |
| 134 // Finds or deletes dependency information for the given URL. Information that | |
| 135 // is specific to this URL (URL row, thumbnails, etc.) is deleted. | |
| 136 // | |
| 137 // This does not affect the visits! This is used for expiration as well as | |
| 138 // deleting from the UI, and they handle visits differently. | |
| 139 // | |
| 140 // Other information will be collected and returned in the output containers. | |
| 141 // This includes some of the things deleted that are needed elsewhere, plus | |
| 142 // some things like favicons that could be shared by many URLs, and need to | |
| 143 // be checked for deletion (this allows us to delete many URLs with only one | |
| 144 // check for shared information at the end). | |
| 145 // | |
| 146 // Assumes the main_db_ is non-NULL. | |
| 147 // | |
| 148 // NOTE: If the url is bookmarked only the segments and text db are updated, | |
| 149 // everything else is unchanged. This is done so that bookmarks retain their | |
| 150 // favicons and thumbnails. | |
| 151 void DeleteOneURL(const URLRow& url_row, | |
| 152 bool is_bookmarked, | |
| 153 DeleteEffects* effects); | |
| 154 | |
| 155 // Deletes all the URLs in the given vector and handles their dependencies. | |
| 156 // This will delete starred URLs | |
| 157 void DeleteURLs(const URLRows& urls, DeleteEffects* effects); | |
| 158 | |
| 159 // Expiration involves removing visits, then propagating the visits out from | |
| 160 // there and delete any orphaned URLs. These will be added to the deleted URLs | |
| 161 // field of the dependencies and DeleteOneURL will handle deleting out from | |
| 162 // there. This function does not handle favicons. | |
| 163 // | |
| 164 // When a URL is not deleted, the last visit time and the visit and typed | |
| 165 // counts will be updated. | |
| 166 // | |
| 167 // The visits in the given vector should have already been deleted from the | |
| 168 // database, and the list of affected URLs already be filled into | |
| 169 // |depenencies->affected_urls|. | |
| 170 // | |
| 171 // Starred URLs will not be deleted. The information in the dependencies that | |
| 172 // DeleteOneURL fills in will be updated, and this function will also delete | |
| 173 // any now-unused favicons. | |
| 174 void ExpireURLsForVisits(const VisitVector& visits, DeleteEffects* effects); | |
| 175 | |
| 176 // Deletes the favicons listed in |effects->affected_favicons| if they are | |
| 177 // unsued. Fails silently (we don't care about favicons so much, so don't want | |
| 178 // to stop everything if it fails). Fills |expired_favicons| with the set of | |
| 179 // favicon urls that no longer have associated visits and were therefore | |
| 180 // expired. | |
| 181 void DeleteFaviconsIfPossible(DeleteEffects* effects); | |
| 182 | |
| 183 // Enum representing what type of action resulted in the history DB deletion. | |
| 184 enum DeletionType { | |
| 185 // User initiated the deletion from the History UI. | |
| 186 DELETION_USER_INITIATED, | |
| 187 // History data was automatically expired due to being more than 90 days | |
| 188 // old. | |
| 189 DELETION_EXPIRED | |
| 190 }; | |
| 191 | |
| 192 // Broadcasts URL modified and deleted notifications. | |
| 193 void BroadcastNotifications(DeleteEffects* effects, DeletionType type); | |
| 194 | |
| 195 // Schedules a call to DoExpireIteration. | |
| 196 void ScheduleExpire(); | |
| 197 | |
| 198 // Calls ExpireSomeOldHistory to expire some amount of old history, according | |
| 199 // to the items in work queue, and schedules another call to happen in the | |
| 200 // future. | |
| 201 void DoExpireIteration(); | |
| 202 | |
| 203 // Tries to expire the oldest |max_visits| visits from history that are older | |
| 204 // than |time_threshold|. The return value indicates if we think there might | |
| 205 // be more history to expire with the current time threshold (it does not | |
| 206 // indicate success or failure). | |
| 207 bool ExpireSomeOldHistory(base::Time end_time, | |
| 208 const ExpiringVisitsReader* reader, | |
| 209 int max_visits); | |
| 210 | |
| 211 // Tries to detect possible bad history or inconsistencies in the database | |
| 212 // and deletes items. For example, URLs with no visits. | |
| 213 void ParanoidExpireHistory(); | |
| 214 | |
| 215 // Returns the HistoryClient, blocking until the bookmarks are loaded. This | |
| 216 // may return NULL during testing. | |
| 217 HistoryClient* GetHistoryClient(); | |
| 218 | |
| 219 // Initializes periodic expiration work queue by populating it with with tasks | |
| 220 // for all known readers. | |
| 221 void InitWorkQueue(); | |
| 222 | |
| 223 // Returns the reader for all visits. This method is only used by the unit | |
| 224 // tests. | |
| 225 const ExpiringVisitsReader* GetAllVisitsReader(); | |
| 226 | |
| 227 // Returns the reader for AUTO_SUBFRAME visits. This method is only used by | |
| 228 // the unit tests. | |
| 229 const ExpiringVisitsReader* GetAutoSubframeVisitsReader(); | |
| 230 | |
| 231 // Non-owning pointer to the notification delegate (guaranteed non-NULL). | |
| 232 HistoryBackendNotifier* notifier_; | |
| 233 | |
| 234 // Non-owning pointers to the databases we deal with (MAY BE NULL). | |
| 235 HistoryDatabase* main_db_; // Main history database. | |
| 236 ThumbnailDatabase* thumb_db_; // Thumbnails and favicons. | |
| 237 | |
| 238 // The threshold for "old" history where we will automatically delete it. | |
| 239 base::TimeDelta expiration_threshold_; | |
| 240 | |
| 241 // List of all distinct types of readers. This list is used to populate the | |
| 242 // work queue. | |
| 243 ExpiringVisitsReaders readers_; | |
| 244 | |
| 245 // Work queue for periodic expiration tasks, used by DoExpireIteration() to | |
| 246 // determine what to do at an iteration, as well as populate it for future | |
| 247 // iterations. | |
| 248 std::queue<const ExpiringVisitsReader*> work_queue_; | |
| 249 | |
| 250 // Readers for various types of visits. | |
| 251 // TODO(dglazkov): If you are adding another one, please consider reorganizing | |
| 252 // into a map. | |
| 253 scoped_ptr<ExpiringVisitsReader> all_visits_reader_; | |
| 254 scoped_ptr<ExpiringVisitsReader> auto_subframe_visits_reader_; | |
| 255 | |
| 256 // The HistoryClient; may be NULL. | |
| 257 // | |
| 258 // Use GetHistoryClient to access this, which makes sure the bookmarks are | |
| 259 // loaded before returning. | |
| 260 HistoryClient* history_client_; | |
| 261 | |
| 262 // Used to generate runnable methods to do timers on this class. They will be | |
| 263 // automatically canceled when this class is deleted. | |
| 264 base::WeakPtrFactory<ExpireHistoryBackend> weak_factory_; | |
| 265 | |
| 266 DISALLOW_COPY_AND_ASSIGN(ExpireHistoryBackend); | |
| 267 }; | |
| 268 | |
| 269 } // namespace history | |
| 270 | |
| 271 #endif // CHROME_BROWSER_HISTORY_EXPIRE_HISTORY_BACKEND_H_ | |
| OLD | NEW |