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

Unified Diff: components/history/core/browser/history_backend.cc

Issue 2672753002: Refactor History's CommitLaterTask with CancelableCallback (Closed)
Patch Set: Remove leftover friendship. Created 3 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: components/history/core/browser/history_backend.cc
diff --git a/components/history/core/browser/history_backend.cc b/components/history/core/browser/history_backend.cc
index 08cacf54212abaa3b3639e655f75878563e0b986..6cea2d4e62fed3963902720bbc19458feaa19411 100644
--- a/components/history/core/browser/history_backend.cc
+++ b/components/history/core/browser/history_backend.cc
@@ -114,44 +114,6 @@ MostVisitedURL MakeMostVisitedURL(const PageUsageData& page_data,
return mv;
}
-// This task is run on a timer so that commits happen at regular intervals
-// so they are batched together. The important thing about this class is that
-// it supports canceling of the task so the reference to the backend will be
-// freed. The problem is that when history is shutting down, there is likely
-// to be one of these commits still pending and holding a reference.
-//
-// The backend can call Cancel to have this task release the reference. The
-// task will still run (if we ever get to processing the event before
-// shutdown), but it will not do anything.
-//
-// Note that this is a refcounted object and is not a task in itself. It should
-// be assigned to a RunnableMethod.
-//
-// TODO(brettw): bug 1165182: This should be replaced with a
Marc Treib 2017/02/02 10:52:21 Wow, this might be the oldest Chrome bug I've seen
-// base::WeakPtrFactory which will handle everything automatically (like we do
-// in ExpireHistoryBackend).
-class CommitLaterTask : public base::RefCounted<CommitLaterTask> {
- public:
- explicit CommitLaterTask(HistoryBackend* history_backend)
- : history_backend_(history_backend) {}
-
- // The backend will call this function if it is being destroyed so that we
- // release our reference.
- void Cancel() { history_backend_ = nullptr; }
-
- void RunCommit() {
- if (history_backend_)
- history_backend_->Commit();
- }
-
- private:
- friend class base::RefCounted<CommitLaterTask>;
-
- ~CommitLaterTask() {}
-
- scoped_refptr<HistoryBackend> history_backend_;
-};
-
QueuedHistoryDBTask::QueuedHistoryDBTask(
std::unique_ptr<HistoryDBTask> task,
scoped_refptr<base::SingleThreadTaskRunner> origin_loop,
@@ -214,10 +176,12 @@ HistoryBackend::HistoryBackend(
recent_redirects_(kMaxRedirectCount),
segment_queried_(false),
backend_client_(std::move(backend_client)),
- task_runner_(task_runner) {}
+ task_runner_(task_runner),
+ weak_ptr_factory_(this) {}
HistoryBackend::~HistoryBackend() {
- DCHECK(!scheduled_commit_) << "Deleting without cleanup";
+ DCHECK(!scheduled_commit_tracker_.HasTrackedTasks())
+ << "Deleting without cleanup";
queued_history_db_tasks_.clear();
// Release stashed embedder object before cleaning up the databases.
@@ -264,8 +228,7 @@ void HistoryBackend::SetOnBackendDestroyTask(
}
void HistoryBackend::Closing() {
- // Any scheduled commit will have a reference to us, we must make it
- // release that reference before we can be destroyed.
+ weak_ptr_factory_.InvalidateWeakPtrs();
CancelScheduledCommit();
// Release our reference to the delegate, this reference will be keeping the
@@ -2242,20 +2205,16 @@ void HistoryBackend::Commit() {
}
void HistoryBackend::ScheduleCommit() {
- if (scheduled_commit_)
+ if (scheduled_commit_tracker_.HasTrackedTasks())
return;
- scheduled_commit_ = new CommitLaterTask(this);
- task_runner_->PostDelayedTask(
- FROM_HERE,
- base::Bind(&CommitLaterTask::RunCommit, scheduled_commit_),
+ scheduled_commit_tracker_.PostDelayedTask(
+ task_runner_.get(), FROM_HERE,
+ base::Bind(&HistoryBackend::Commit, weak_ptr_factory_.GetWeakPtr()),
base::TimeDelta::FromSeconds(kCommitIntervalSeconds));
}
void HistoryBackend::CancelScheduledCommit() {
- if (scheduled_commit_) {
- scheduled_commit_->Cancel();
- scheduled_commit_ = nullptr;
- }
+ scheduled_commit_tracker_.TryCancelAll();
}
void HistoryBackend::ProcessDBTaskImpl() {

Powered by Google App Engine
This is Rietveld 408576698