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

Unified Diff: components/cronet/host_cache_persistence_manager.cc

Issue 2953483003: Add HostCachePersistenceManager for Cronet (Closed)
Patch Set: Created 3 years, 6 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/cronet/host_cache_persistence_manager.cc
diff --git a/components/cronet/host_cache_persistence_manager.cc b/components/cronet/host_cache_persistence_manager.cc
new file mode 100644
index 0000000000000000000000000000000000000000..043f8d67d9cc910c0c31810dfb5f5e351f8a92dc
--- /dev/null
+++ b/components/cronet/host_cache_persistence_manager.cc
@@ -0,0 +1,87 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/cronet/host_cache_persistence_manager.h"
+
+#include <memory>
+
+#include "base/memory/ptr_util.h"
+#include "base/time/time.h"
+#include "base/timer/timer.h"
+#include "base/values.h"
+#include "components/prefs/pref_change_registrar.h"
+#include "components/prefs/pref_service.h"
+
+namespace cronet {
+
+HostCachePersistenceManager::HostCachePersistenceManager(
+ net::HostCache* cache,
+ PrefService* pref_service,
+ std::string pref_name,
+ base::TimeDelta delay)
+ : cache_(cache),
+ registrar_(base::MakeUnique<PrefChangeRegistrar>()),
+ pref_service_(pref_service),
+ pref_name_(pref_name),
+ delay_(delay),
+ timer_(base::MakeUnique<base::OneShotTimer>()),
+ weak_factory_(this) {
+ DCHECK(cache_);
+ DCHECK(pref_service_);
+
+ // Get the initial value of the pref if it's already initialized.
+ if (pref_service_->HasPrefPath(pref_name_))
+ OnRead();
+
+ registrar_->Init(pref_service_);
+ registrar_->Add(pref_name_, base::Bind(&HostCachePersistenceManager::OnRead,
+ weak_factory_.GetWeakPtr()));
+ cache_->set_persistence_delegate(this);
+}
+
+HostCachePersistenceManager::~HostCachePersistenceManager() {
+ DCHECK(cache_);
+ DCHECK(pref_service_);
+ DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+
+ timer_->Stop();
+ registrar_->RemoveAll();
+ cache_->set_persistence_delegate(nullptr);
pauljensen 2017/06/22 19:03:50 I feel like either there should be a DCHECK_EQ(cac
mgersh 2017/06/22 21:27:32 Done. Picked the second because it'll give a more
+}
+
+void HostCachePersistenceManager::OnRead() {
pauljensen 2017/06/22 19:03:50 It looks like this is the counterpart to DoWrite()
mgersh 2017/06/22 21:27:32 Done.
+ DCHECK(cache_);
+ DCHECK(pref_service_);
+ DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+
+ if (writing_pref_)
+ return;
+
+ const base::ListValue* pref_value = pref_service_->GetList(pref_name_);
+ cache_->RestoreFromListValue(*pref_value);
+}
+
+void HostCachePersistenceManager::ScheduleWrite() {
+ DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+
+ if (timer_->IsRunning())
+ return;
+
+ timer_->Start(FROM_HERE, delay_,
+ base::Bind(&HostCachePersistenceManager::DoWrite,
+ weak_factory_.GetWeakPtr()));
+}
+
+void HostCachePersistenceManager::DoWrite() {
+ DCHECK(cache_);
+ DCHECK(pref_service_);
+ DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+
+ std::unique_ptr<base::ListValue> value = cache_->GetAsListValue(false);
+ writing_pref_ = true;
+ pref_service_->Set(pref_name_, *value);
+ writing_pref_ = false;
+}
+
+} // namespace cronet

Powered by Google App Engine
This is Rietveld 408576698