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

Side by Side Diff: components/history/core/browser/expire_history_backend.cc

Issue 2903573002: [Thumbnails DB] Add functionality to clear unused on-demand favicons. (Closed)
Patch Set: Brett's comments Created 3 years, 5 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 "components/history/core/browser/expire_history_backend.h" 5 #include "components/history/core/browser/expire_history_backend.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <functional> 10 #include <functional>
11 #include <limits> 11 #include <limits>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/compiler_specific.h" 14 #include "base/compiler_specific.h"
15 #include "base/feature_list.h"
15 #include "base/files/file_enumerator.h" 16 #include "base/files/file_enumerator.h"
16 #include "base/files/file_util.h" 17 #include "base/files/file_util.h"
17 #include "base/location.h" 18 #include "base/location.h"
18 #include "base/logging.h" 19 #include "base/logging.h"
19 #include "base/sequenced_task_runner.h" 20 #include "base/sequenced_task_runner.h"
20 #include "components/history/core/browser/history_backend_client.h" 21 #include "components/history/core/browser/history_backend_client.h"
21 #include "components/history/core/browser/history_backend_notifier.h" 22 #include "components/history/core/browser/history_backend_notifier.h"
22 #include "components/history/core/browser/history_database.h" 23 #include "components/history/core/browser/history_database.h"
23 #include "components/history/core/browser/thumbnail_database.h" 24 #include "components/history/core/browser/thumbnail_database.h"
24 25
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 // last expiration found at least kNumExpirePerIteration and we want to check 108 // last expiration found at least kNumExpirePerIteration and we want to check
108 // again "soon." 109 // again "soon."
109 const int kExpirationDelaySec = 30; 110 const int kExpirationDelaySec = 30;
110 111
111 // The number of minutes between checking, as with kExpirationDelaySec, but 112 // The number of minutes between checking, as with kExpirationDelaySec, but
112 // when we didn't find enough things to expire last time. If there was no 113 // when we didn't find enough things to expire last time. If there was no
113 // history to expire last iteration, it's likely there is nothing next 114 // history to expire last iteration, it's likely there is nothing next
114 // iteration, so we want to wait longer before checking to avoid wasting CPU. 115 // iteration, so we want to wait longer before checking to avoid wasting CPU.
115 const int kExpirationEmptyDelayMin = 5; 116 const int kExpirationEmptyDelayMin = 5;
116 117
118 // The minimum number of hours between checking for old on-demand favicons that
119 // should be cleared.
120 const int kClearOnDemandFaviconsIntervalHours = 24;
121
122 bool IsAnyURLBookmarked(HistoryBackendClient* backend_client,
123 const std::vector<GURL>& urls) {
124 for (const GURL& url : urls) {
125 if (backend_client->IsBookmarked(url))
126 return true;
127 }
128 return false;
129 }
130
117 } // namespace 131 } // namespace
118 132
133 namespace internal {
134
135 const base::Feature kClearOldOnDemandFavicons{
136 "ClearOldOnDemandFavicons", base::FEATURE_DISABLED_BY_DEFAULT};
137
138 } // namespace internal
119 139
120 // ExpireHistoryBackend::DeleteEffects ---------------------------------------- 140 // ExpireHistoryBackend::DeleteEffects ----------------------------------------
121 141
122 ExpireHistoryBackend::DeleteEffects::DeleteEffects() { 142 ExpireHistoryBackend::DeleteEffects::DeleteEffects() {
123 } 143 }
124 144
125 ExpireHistoryBackend::DeleteEffects::~DeleteEffects() { 145 ExpireHistoryBackend::DeleteEffects::~DeleteEffects() {
126 } 146 }
127 147
128 148
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 } 486 }
467 487
468 void ExpireHistoryBackend::DoExpireIteration() { 488 void ExpireHistoryBackend::DoExpireIteration() {
469 DCHECK(!work_queue_.empty()) << "queue has to be non-empty"; 489 DCHECK(!work_queue_.empty()) << "queue has to be non-empty";
470 490
471 const ExpiringVisitsReader* reader = work_queue_.front(); 491 const ExpiringVisitsReader* reader = work_queue_.front();
472 bool more_to_expire = ExpireSomeOldHistory( 492 bool more_to_expire = ExpireSomeOldHistory(
473 GetCurrentExpirationTime(), reader, kNumExpirePerIteration); 493 GetCurrentExpirationTime(), reader, kNumExpirePerIteration);
474 494
475 work_queue_.pop(); 495 work_queue_.pop();
476 // If there are more items to expire, add the reader back to the queue, thus 496 if (more_to_expire) {
477 // creating a new task for future iterations. 497 // If there are more items to expire, add the reader back to the queue, thus
478 if (more_to_expire) 498 // creating a new task for future iterations.
479 work_queue_.push(reader); 499 work_queue_.push(reader);
500 } else {
501 // Otherwise do a final clean-up - remove old favicons not bound to visits.
502 ClearOldOnDemandFavicons(GetCurrentExpirationTime());
503 }
480 504
481 ScheduleExpire(); 505 ScheduleExpire();
482 } 506 }
483 507
508 void ExpireHistoryBackend::ClearOldOnDemandFavicons(
509 base::Time expiration_threshold) {
510 if (!base::FeatureList::IsEnabled(internal::kClearOldOnDemandFavicons))
511 return;
512
513 if (expiration_threshold <
brettw 2017/07/13 23:50:16 Can you put a comment on this block about how this
mastiz 2017/07/18 07:48:19 Done, but please double check because I didn't ful
514 last_on_demand_expiration_threshold_ +
515 base::TimeDelta::FromHours(kClearOnDemandFaviconsIntervalHours)) {
516 return;
517 }
518
519 last_on_demand_expiration_threshold_ = expiration_threshold;
520
521 std::map<favicon_base::FaviconID, IconMappingsForExpiry> icon_mappings =
522 thumb_db_->GetOldOnDemandFavicons(expiration_threshold);
523 DeleteEffects effects;
524
525 for (auto id_and_mappings_pair : icon_mappings) {
526 favicon_base::FaviconID icon_id = id_and_mappings_pair.first;
527 const IconMappingsForExpiry& mappings = id_and_mappings_pair.second;
528
529 if (backend_client_ &&
530 IsAnyURLBookmarked(backend_client_, mappings.page_urls)) {
531 continue;
532 }
533
534 thumb_db_->DeleteFavicon(icon_id);
535 thumb_db_->DeleteIconMappingsForFaviconId(icon_id);
536 effects.deleted_favicons.insert(mappings.icon_url);
537 }
538
539 BroadcastNotifications(&effects, DELETION_EXPIRED);
540 }
541
484 bool ExpireHistoryBackend::ExpireSomeOldHistory( 542 bool ExpireHistoryBackend::ExpireSomeOldHistory(
485 base::Time end_time, 543 base::Time end_time,
486 const ExpiringVisitsReader* reader, 544 const ExpiringVisitsReader* reader,
487 int max_visits) { 545 int max_visits) {
488 if (!main_db_) 546 if (!main_db_)
489 return false; 547 return false;
490 548
491 // Add an extra time unit to given end time, because 549 // Add an extra time unit to given end time, because
492 // GetAllVisitsInRange, et al. queries' end value is non-inclusive. 550 // GetAllVisitsInRange, et al. queries' end value is non-inclusive.
493 base::Time effective_end_time = 551 base::Time effective_end_time =
(...skipping 11 matching lines...) Expand all
505 BroadcastNotifications(&deleted_effects, DELETION_EXPIRED); 563 BroadcastNotifications(&deleted_effects, DELETION_EXPIRED);
506 564
507 return more_to_expire; 565 return more_to_expire;
508 } 566 }
509 567
510 void ExpireHistoryBackend::ParanoidExpireHistory() { 568 void ExpireHistoryBackend::ParanoidExpireHistory() {
511 // TODO(brettw): Bug 1067331: write this to clean up any errors. 569 // TODO(brettw): Bug 1067331: write this to clean up any errors.
512 } 570 }
513 571
514 } // namespace history 572 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698