OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/cronet/host_cache_persistence_manager.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "base/time/time.h" | |
11 #include "base/timer/timer.h" | |
12 #include "base/values.h" | |
13 #include "components/prefs/pref_change_registrar.h" | |
14 #include "components/prefs/pref_service.h" | |
15 | |
16 namespace cronet { | |
17 | |
18 HostCachePersistenceManager::HostCachePersistenceManager( | |
19 net::HostCache* cache, | |
20 PrefService* pref_service, | |
21 std::string pref_name, | |
22 base::TimeDelta delay) | |
23 : cache_(cache), | |
24 registrar_(base::MakeUnique<PrefChangeRegistrar>()), | |
25 pref_service_(pref_service), | |
26 pref_name_(pref_name), | |
27 delay_(delay), | |
28 timer_(base::MakeUnique<base::OneShotTimer>()), | |
29 weak_factory_(this) { | |
30 DCHECK(cache_); | |
31 DCHECK(pref_service_); | |
32 | |
33 // Get the initial value of the pref if it's already initialized. | |
34 if (pref_service_->HasPrefPath(pref_name_)) | |
35 OnRead(); | |
36 | |
37 registrar_->Init(pref_service_); | |
38 registrar_->Add(pref_name_, base::Bind(&HostCachePersistenceManager::OnRead, | |
39 weak_factory_.GetWeakPtr())); | |
40 cache_->set_persistence_delegate(this); | |
41 } | |
42 | |
43 HostCachePersistenceManager::~HostCachePersistenceManager() { | |
44 DCHECK(cache_); | |
45 DCHECK(pref_service_); | |
46 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
47 | |
48 timer_->Stop(); | |
49 registrar_->RemoveAll(); | |
50 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
| |
51 } | |
52 | |
53 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.
| |
54 DCHECK(cache_); | |
55 DCHECK(pref_service_); | |
56 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
57 | |
58 if (writing_pref_) | |
59 return; | |
60 | |
61 const base::ListValue* pref_value = pref_service_->GetList(pref_name_); | |
62 cache_->RestoreFromListValue(*pref_value); | |
63 } | |
64 | |
65 void HostCachePersistenceManager::ScheduleWrite() { | |
66 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
67 | |
68 if (timer_->IsRunning()) | |
69 return; | |
70 | |
71 timer_->Start(FROM_HERE, delay_, | |
72 base::Bind(&HostCachePersistenceManager::DoWrite, | |
73 weak_factory_.GetWeakPtr())); | |
74 } | |
75 | |
76 void HostCachePersistenceManager::DoWrite() { | |
77 DCHECK(cache_); | |
78 DCHECK(pref_service_); | |
79 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
80 | |
81 std::unique_ptr<base::ListValue> value = cache_->GetAsListValue(false); | |
82 writing_pref_ = true; | |
83 pref_service_->Set(pref_name_, *value); | |
84 writing_pref_ = false; | |
85 } | |
86 | |
87 } // namespace cronet | |
OLD | NEW |