| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/history/expire_history_backend.h" | 5 #include "chrome/browser/history/expire_history_backend.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/files/file_enumerator.h" | 14 #include "base/files/file_enumerator.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
| 18 #include "chrome/browser/history/archived_database.h" | 18 #include "chrome/browser/history/archived_database.h" |
| 19 #include "chrome/browser/history/history_database.h" | 19 #include "chrome/browser/history/history_database.h" |
| 20 #include "chrome/browser/history/history_notifications.h" | 20 #include "chrome/browser/history/history_notifications.h" |
| 21 #include "chrome/browser/history/thumbnail_database.h" | 21 #include "chrome/browser/history/thumbnail_database.h" |
| 22 #include "components/bookmarks/core/browser/bookmark_service.h" | 22 #include "components/history/core/browser/history_client.h" |
| 23 | 23 |
| 24 namespace history { | 24 namespace history { |
| 25 | 25 |
| 26 // Helpers -------------------------------------------------------------------- | 26 // Helpers -------------------------------------------------------------------- |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 // The number of days by which the expiration threshold is advanced for items | 30 // The number of days by which the expiration threshold is advanced for items |
| 31 // that we want to expire early, such as those of AUTO_SUBFRAME transition type. | 31 // that we want to expire early, such as those of AUTO_SUBFRAME transition type. |
| 32 // | 32 // |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 } | 148 } |
| 149 | 149 |
| 150 ExpireHistoryBackend::DeleteEffects::~DeleteEffects() { | 150 ExpireHistoryBackend::DeleteEffects::~DeleteEffects() { |
| 151 } | 151 } |
| 152 | 152 |
| 153 | 153 |
| 154 // ExpireHistoryBackend ------------------------------------------------------- | 154 // ExpireHistoryBackend ------------------------------------------------------- |
| 155 | 155 |
| 156 ExpireHistoryBackend::ExpireHistoryBackend( | 156 ExpireHistoryBackend::ExpireHistoryBackend( |
| 157 BroadcastNotificationDelegate* delegate, | 157 BroadcastNotificationDelegate* delegate, |
| 158 BookmarkService* bookmark_service) | 158 HistoryClient* history_client) |
| 159 : delegate_(delegate), | 159 : delegate_(delegate), |
| 160 main_db_(NULL), | 160 main_db_(NULL), |
| 161 archived_db_(NULL), | 161 archived_db_(NULL), |
| 162 thumb_db_(NULL), | 162 thumb_db_(NULL), |
| 163 weak_factory_(this), | 163 weak_factory_(this), |
| 164 bookmark_service_(bookmark_service) { | 164 history_client_(history_client) { |
| 165 } | 165 } |
| 166 | 166 |
| 167 ExpireHistoryBackend::~ExpireHistoryBackend() { | 167 ExpireHistoryBackend::~ExpireHistoryBackend() { |
| 168 } | 168 } |
| 169 | 169 |
| 170 void ExpireHistoryBackend::SetDatabases(HistoryDatabase* main_db, | 170 void ExpireHistoryBackend::SetDatabases(HistoryDatabase* main_db, |
| 171 ArchivedDatabase* archived_db, | 171 ArchivedDatabase* archived_db, |
| 172 ThumbnailDatabase* thumb_db) { | 172 ThumbnailDatabase* thumb_db) { |
| 173 main_db_ = main_db; | 173 main_db_ = main_db; |
| 174 archived_db_ = archived_db; | 174 archived_db_ = archived_db; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 196 // should also delete from the archived DB. | 196 // should also delete from the archived DB. |
| 197 VisitVector visits; | 197 VisitVector visits; |
| 198 main_db_->GetVisitsForURL(url_row.id(), &visits); | 198 main_db_->GetVisitsForURL(url_row.id(), &visits); |
| 199 | 199 |
| 200 DeleteVisitRelatedInfo(visits, &effects); | 200 DeleteVisitRelatedInfo(visits, &effects); |
| 201 | 201 |
| 202 // We skip ExpireURLsForVisits (since we are deleting from the | 202 // We skip ExpireURLsForVisits (since we are deleting from the |
| 203 // URL, and not starting with visits in a given time range). We | 203 // URL, and not starting with visits in a given time range). We |
| 204 // therefore need to call the deletion and favicon update | 204 // therefore need to call the deletion and favicon update |
| 205 // functions manually. | 205 // functions manually. |
| 206 BookmarkService* bookmark_service = GetBookmarkService(); | |
| 207 DeleteOneURL(url_row, | 206 DeleteOneURL(url_row, |
| 208 bookmark_service && bookmark_service->IsBookmarked(*url), | 207 history_client_ && history_client_->IsBookmarked(*url), |
| 209 &effects); | 208 &effects); |
| 210 } | 209 } |
| 211 | 210 |
| 212 DeleteFaviconsIfPossible(&effects); | 211 DeleteFaviconsIfPossible(&effects); |
| 213 | 212 |
| 214 BroadcastNotifications(&effects, DELETION_USER_INITIATED); | 213 BroadcastNotifications(&effects, DELETION_USER_INITIATED); |
| 215 } | 214 } |
| 216 | 215 |
| 217 void ExpireHistoryBackend::ExpireHistoryBetween( | 216 void ExpireHistoryBackend::ExpireHistoryBetween( |
| 218 const std::set<GURL>& restrict_urls, | 217 const std::set<GURL>& restrict_urls, |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 content::PageTransitionStripQualifier(visits[i].transition); | 456 content::PageTransitionStripQualifier(visits[i].transition); |
| 458 if (transition != content::PAGE_TRANSITION_RELOAD) | 457 if (transition != content::PAGE_TRANSITION_RELOAD) |
| 459 cur.visit_count++; | 458 cur.visit_count++; |
| 460 if ((transition == content::PAGE_TRANSITION_TYPED && | 459 if ((transition == content::PAGE_TRANSITION_TYPED && |
| 461 !content::PageTransitionIsRedirect(visits[i].transition)) || | 460 !content::PageTransitionIsRedirect(visits[i].transition)) || |
| 462 transition == content::PAGE_TRANSITION_KEYWORD_GENERATED) | 461 transition == content::PAGE_TRANSITION_KEYWORD_GENERATED) |
| 463 cur.typed_count++; | 462 cur.typed_count++; |
| 464 } | 463 } |
| 465 | 464 |
| 466 // Check each unique URL with deleted visits. | 465 // Check each unique URL with deleted visits. |
| 467 BookmarkService* bookmark_service = GetBookmarkService(); | |
| 468 for (std::map<URLID, ChangedURL>::const_iterator i = changed_urls.begin(); | 466 for (std::map<URLID, ChangedURL>::const_iterator i = changed_urls.begin(); |
| 469 i != changed_urls.end(); ++i) { | 467 i != changed_urls.end(); ++i) { |
| 470 // The unique URL rows should already be filled in. | 468 // The unique URL rows should already be filled in. |
| 471 URLRow& url_row = effects->affected_urls[i->first]; | 469 URLRow& url_row = effects->affected_urls[i->first]; |
| 472 if (!url_row.id()) | 470 if (!url_row.id()) |
| 473 continue; // URL row doesn't exist in the database. | 471 continue; // URL row doesn't exist in the database. |
| 474 | 472 |
| 475 // Check if there are any other visits for this URL and update the time | 473 // Check if there are any other visits for this URL and update the time |
| 476 // (the time change may not actually be synced to disk below when we're | 474 // (the time change may not actually be synced to disk below when we're |
| 477 // archiving). | 475 // archiving). |
| 478 VisitRow last_visit; | 476 VisitRow last_visit; |
| 479 if (main_db_->GetMostRecentVisitForURL(url_row.id(), &last_visit)) | 477 if (main_db_->GetMostRecentVisitForURL(url_row.id(), &last_visit)) |
| 480 url_row.set_last_visit(last_visit.visit_time); | 478 url_row.set_last_visit(last_visit.visit_time); |
| 481 else | 479 else |
| 482 url_row.set_last_visit(base::Time()); | 480 url_row.set_last_visit(base::Time()); |
| 483 | 481 |
| 484 // Don't delete URLs with visits still in the DB, or bookmarked. | 482 // Don't delete URLs with visits still in the DB, or bookmarked. |
| 485 bool is_bookmarked = | 483 bool is_bookmarked = |
| 486 (bookmark_service && bookmark_service->IsBookmarked(url_row.url())); | 484 (history_client_ && history_client_->IsBookmarked(url_row.url())); |
| 487 if (!is_bookmarked && url_row.last_visit().is_null()) { | 485 if (!is_bookmarked && url_row.last_visit().is_null()) { |
| 488 // Not bookmarked and no more visits. Nuke the url. | 486 // Not bookmarked and no more visits. Nuke the url. |
| 489 DeleteOneURL(url_row, is_bookmarked, effects); | 487 DeleteOneURL(url_row, is_bookmarked, effects); |
| 490 } else { | 488 } else { |
| 491 // NOTE: The calls to std::max() below are a backstop, but they should | 489 // NOTE: The calls to std::max() below are a backstop, but they should |
| 492 // never actually be needed unless the database is corrupt (I think). | 490 // never actually be needed unless the database is corrupt (I think). |
| 493 url_row.set_visit_count( | 491 url_row.set_visit_count( |
| 494 std::max(0, url_row.visit_count() - i->second.visit_count)); | 492 std::max(0, url_row.visit_count() - i->second.visit_count)); |
| 495 url_row.set_typed_count( | 493 url_row.set_typed_count( |
| 496 std::max(0, url_row.typed_count() - i->second.typed_count)); | 494 std::max(0, url_row.typed_count() - i->second.typed_count)); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 DeleteFaviconsIfPossible(&deleted_effects); | 619 DeleteFaviconsIfPossible(&deleted_effects); |
| 622 BroadcastNotifications(&deleted_effects, DELETION_ARCHIVED); | 620 BroadcastNotifications(&deleted_effects, DELETION_ARCHIVED); |
| 623 | 621 |
| 624 return more_to_expire; | 622 return more_to_expire; |
| 625 } | 623 } |
| 626 | 624 |
| 627 void ExpireHistoryBackend::ParanoidExpireHistory() { | 625 void ExpireHistoryBackend::ParanoidExpireHistory() { |
| 628 // TODO(brettw): Bug 1067331: write this to clean up any errors. | 626 // TODO(brettw): Bug 1067331: write this to clean up any errors. |
| 629 } | 627 } |
| 630 | 628 |
| 631 BookmarkService* ExpireHistoryBackend::GetBookmarkService() { | |
| 632 // We use the bookmark service to determine if a URL is bookmarked. The | |
| 633 // bookmark service is loaded on a separate thread and may not be done by the | |
| 634 // time we get here. We therefor block until the bookmarks have finished | |
| 635 // loading. | |
| 636 if (bookmark_service_) | |
| 637 bookmark_service_->BlockTillLoaded(); | |
| 638 return bookmark_service_; | |
| 639 } | |
| 640 | |
| 641 } // namespace history | 629 } // namespace history |
| OLD | NEW |