Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(426)

Side by Side Diff: chrome/browser/history/expire_history_backend.cc

Issue 285233012: Abstract history dependencies on bookmarks through HistoryClient (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unit tests Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/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
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;
175 thumb_db_ = thumb_db; 175 thumb_db_ = thumb_db;
176 } 176 }
177 177
178 void ExpireHistoryBackend::DeleteURL(const GURL& url) { 178 void ExpireHistoryBackend::DeleteURL(const GURL& url) {
179 DeleteURLs(std::vector<GURL>(1, url)); 179 DeleteURLs(std::vector<GURL>(1, url));
180 } 180 }
181 181
182 void ExpireHistoryBackend::DeleteURLs(const std::vector<GURL>& urls) { 182 void ExpireHistoryBackend::DeleteURLs(const std::vector<GURL>& urls) {
183 if (!main_db_) 183 if (!main_db_)
184 return; 184 return;
185 185
186 DeleteEffects effects; 186 DeleteEffects effects;
187 HistoryClient* history_client = GetHistoryClient();
187 for (std::vector<GURL>::const_iterator url = urls.begin(); url != urls.end(); 188 for (std::vector<GURL>::const_iterator url = urls.begin(); url != urls.end();
188 ++url) { 189 ++url) {
189 URLRow url_row; 190 URLRow url_row;
190 if (!main_db_->GetRowForURL(*url, &url_row)) 191 if (!main_db_->GetRowForURL(*url, &url_row))
191 continue; // Nothing to delete. 192 continue; // Nothing to delete.
192 193
193 // Collect all the visits and delete them. Note that we don't give 194 // Collect all the visits and delete them. Note that we don't give
194 // up if there are no visits, since the URL could still have an 195 // up if there are no visits, since the URL could still have an
195 // entry that we should delete. TODO(brettw): bug 1171148: We 196 // entry that we should delete. TODO(brettw): bug 1171148: We
196 // should also delete from the archived DB. 197 // should also delete from the archived DB.
197 VisitVector visits; 198 VisitVector visits;
198 main_db_->GetVisitsForURL(url_row.id(), &visits); 199 main_db_->GetVisitsForURL(url_row.id(), &visits);
199 200
200 DeleteVisitRelatedInfo(visits, &effects); 201 DeleteVisitRelatedInfo(visits, &effects);
201 202
202 // We skip ExpireURLsForVisits (since we are deleting from the 203 // We skip ExpireURLsForVisits (since we are deleting from the
203 // URL, and not starting with visits in a given time range). We 204 // URL, and not starting with visits in a given time range). We
204 // therefore need to call the deletion and favicon update 205 // therefore need to call the deletion and favicon update
205 // functions manually. 206 // functions manually.
206 BookmarkService* bookmark_service = GetBookmarkService();
207 DeleteOneURL(url_row, 207 DeleteOneURL(url_row,
208 bookmark_service && bookmark_service->IsBookmarked(*url), 208 history_client && history_client->IsBookmarked(*url),
209 &effects); 209 &effects);
210 } 210 }
211 211
212 DeleteFaviconsIfPossible(&effects); 212 DeleteFaviconsIfPossible(&effects);
213 213
214 BroadcastNotifications(&effects, DELETION_USER_INITIATED); 214 BroadcastNotifications(&effects, DELETION_USER_INITIATED);
215 } 215 }
216 216
217 void ExpireHistoryBackend::ExpireHistoryBetween( 217 void ExpireHistoryBackend::ExpireHistoryBetween(
218 const std::set<GURL>& restrict_urls, 218 const std::set<GURL>& restrict_urls,
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 content::PageTransitionStripQualifier(visits[i].transition); 457 content::PageTransitionStripQualifier(visits[i].transition);
458 if (transition != content::PAGE_TRANSITION_RELOAD) 458 if (transition != content::PAGE_TRANSITION_RELOAD)
459 cur.visit_count++; 459 cur.visit_count++;
460 if ((transition == content::PAGE_TRANSITION_TYPED && 460 if ((transition == content::PAGE_TRANSITION_TYPED &&
461 !content::PageTransitionIsRedirect(visits[i].transition)) || 461 !content::PageTransitionIsRedirect(visits[i].transition)) ||
462 transition == content::PAGE_TRANSITION_KEYWORD_GENERATED) 462 transition == content::PAGE_TRANSITION_KEYWORD_GENERATED)
463 cur.typed_count++; 463 cur.typed_count++;
464 } 464 }
465 465
466 // Check each unique URL with deleted visits. 466 // Check each unique URL with deleted visits.
467 BookmarkService* bookmark_service = GetBookmarkService(); 467 HistoryClient* history_client = GetHistoryClient();
468 for (std::map<URLID, ChangedURL>::const_iterator i = changed_urls.begin(); 468 for (std::map<URLID, ChangedURL>::const_iterator i = changed_urls.begin();
469 i != changed_urls.end(); ++i) { 469 i != changed_urls.end(); ++i) {
470 // The unique URL rows should already be filled in. 470 // The unique URL rows should already be filled in.
471 URLRow& url_row = effects->affected_urls[i->first]; 471 URLRow& url_row = effects->affected_urls[i->first];
472 if (!url_row.id()) 472 if (!url_row.id())
473 continue; // URL row doesn't exist in the database. 473 continue; // URL row doesn't exist in the database.
474 474
475 // Check if there are any other visits for this URL and update the time 475 // 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 476 // (the time change may not actually be synced to disk below when we're
477 // archiving). 477 // archiving).
478 VisitRow last_visit; 478 VisitRow last_visit;
479 if (main_db_->GetMostRecentVisitForURL(url_row.id(), &last_visit)) 479 if (main_db_->GetMostRecentVisitForURL(url_row.id(), &last_visit))
480 url_row.set_last_visit(last_visit.visit_time); 480 url_row.set_last_visit(last_visit.visit_time);
481 else 481 else
482 url_row.set_last_visit(base::Time()); 482 url_row.set_last_visit(base::Time());
483 483
484 // Don't delete URLs with visits still in the DB, or bookmarked. 484 // Don't delete URLs with visits still in the DB, or bookmarked.
485 bool is_bookmarked = 485 bool is_bookmarked =
486 (bookmark_service && bookmark_service->IsBookmarked(url_row.url())); 486 (history_client && history_client->IsBookmarked(url_row.url()));
487 if (!is_bookmarked && url_row.last_visit().is_null()) { 487 if (!is_bookmarked && url_row.last_visit().is_null()) {
488 // Not bookmarked and no more visits. Nuke the url. 488 // Not bookmarked and no more visits. Nuke the url.
489 DeleteOneURL(url_row, is_bookmarked, effects); 489 DeleteOneURL(url_row, is_bookmarked, effects);
490 } else { 490 } else {
491 // NOTE: The calls to std::max() below are a backstop, but they should 491 // 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). 492 // never actually be needed unless the database is corrupt (I think).
493 url_row.set_visit_count( 493 url_row.set_visit_count(
494 std::max(0, url_row.visit_count() - i->second.visit_count)); 494 std::max(0, url_row.visit_count() - i->second.visit_count));
495 url_row.set_typed_count( 495 url_row.set_typed_count(
496 std::max(0, url_row.typed_count() - i->second.typed_count)); 496 std::max(0, url_row.typed_count() - i->second.typed_count));
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 DeleteFaviconsIfPossible(&deleted_effects); 621 DeleteFaviconsIfPossible(&deleted_effects);
622 BroadcastNotifications(&deleted_effects, DELETION_ARCHIVED); 622 BroadcastNotifications(&deleted_effects, DELETION_ARCHIVED);
623 623
624 return more_to_expire; 624 return more_to_expire;
625 } 625 }
626 626
627 void ExpireHistoryBackend::ParanoidExpireHistory() { 627 void ExpireHistoryBackend::ParanoidExpireHistory() {
628 // TODO(brettw): Bug 1067331: write this to clean up any errors. 628 // TODO(brettw): Bug 1067331: write this to clean up any errors.
629 } 629 }
630 630
631 BookmarkService* ExpireHistoryBackend::GetBookmarkService() { 631 HistoryClient* ExpireHistoryBackend::GetHistoryClient() {
632 // We use the bookmark service to determine if a URL is bookmarked. The 632 // We use the history client to determine if a URL is bookmarked. The data is
633 // bookmark service is loaded on a separate thread and may not be done by the 633 // loaded on a separate thread and may not be done by the time we get here.
634 // time we get here. We therefor block until the bookmarks have finished 634 // We therefore block until the bookmarks have finished loading.
635 // loading. 635 if (history_client_)
636 if (bookmark_service_) 636 history_client_->BlockTillBookmarksLoaded();
637 bookmark_service_->BlockTillLoaded(); 637 return history_client_;
638 return bookmark_service_;
639 } 638 }
640 639
641 } // namespace history 640 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698