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

Unified Diff: components/cronet/host_cache_persistence_manager.cc

Issue 2953483003: Add HostCachePersistenceManager for Cronet (Closed)
Patch Set: address comments 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..2d771b1f57a3ce70ad0ce606dc5680cfd76eab24
--- /dev/null
+++ b/components/cronet/host_cache_persistence_manager.cc
@@ -0,0 +1,77 @@
+// 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/values.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),
+ pref_service_(pref_service),
+ pref_name_(pref_name),
+ delay_(delay),
+ 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_))
+ ReadFromDisk();
+
+ registrar_.Init(pref_service_);
+ registrar_.Add(pref_name_,
+ base::Bind(&HostCachePersistenceManager::ReadFromDisk,
+ weak_factory_.GetWeakPtr()));
+ cache_->set_persistence_delegate(this);
+}
+
+HostCachePersistenceManager::~HostCachePersistenceManager() {
+ DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+
+ timer_.Stop();
+ registrar_.RemoveAll();
+ cache_->set_persistence_delegate(nullptr);
+}
+
+void HostCachePersistenceManager::ReadFromDisk() {
+ 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::WriteToDisk,
+ weak_factory_.GetWeakPtr()));
+}
+
+void HostCachePersistenceManager::WriteToDisk() {
+ DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+
+ std::unique_ptr<base::ListValue> value = cache_->GetAsListValue(false);
pauljensen 2017/06/23 14:10:31 I'd prefer if GetAsListValue() took a ListValue* r
mgersh 2017/06/23 16:02:06 Done.
+ writing_pref_ = true;
+ pref_service_->Set(pref_name_, *value);
+ writing_pref_ = false;
+}
+
+} // namespace cronet

Powered by Google App Engine
This is Rietveld 408576698