OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/invalidation/invalidator_storage.h" | |
6 | |
7 #include <string> | |
8 #include <utility> | |
9 | |
10 #include "base/base64.h" | |
11 #include "base/basictypes.h" | |
12 #include "base/logging.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/prefs/pref_registry_simple.h" | |
15 #include "base/prefs/pref_service.h" | |
16 #include "base/values.h" | |
17 #include "components/invalidation/invalidation_prefs.h" | |
18 #include "components/invalidation/unacked_invalidation_set.h" | |
19 #include "components/pref_registry/pref_registry_syncable.h" | |
20 #include "google/cacheinvalidation/types.pb.h" | |
21 | |
22 namespace { | |
23 | |
24 const char kInvalidatorMaxInvalidationVersions[] = | |
25 "invalidator.max_invalidation_versions"; | |
26 | |
27 bool ValueToUnackedInvalidationStorageMap( | |
28 const base::ListValue& value, | |
29 syncer::UnackedInvalidationsMap* map) { | |
30 for (size_t i = 0; i != value.GetSize(); ++i) { | |
31 invalidation::ObjectId invalid_id; | |
32 syncer::UnackedInvalidationSet storage(invalid_id); | |
33 const base::DictionaryValue* dict; | |
34 if (!value.GetDictionary(i, &dict) || !storage.ResetFromValue(*dict)) { | |
35 DLOG(WARNING) << "Failed to parse ObjectState at position " << i; | |
36 return false; | |
37 } | |
38 map->insert(std::make_pair(storage.object_id(), storage)); | |
39 } | |
40 return true; | |
41 } | |
42 | |
43 scoped_ptr<base::ListValue> UnackedInvalidationStorageMapToValue( | |
44 const syncer::UnackedInvalidationsMap& map) { | |
45 scoped_ptr<base::ListValue> value(new base::ListValue); | |
46 for (syncer::UnackedInvalidationsMap::const_iterator it = map.begin(); | |
47 it != map.end(); ++it) { | |
48 value->Append(it->second.ToValue().release()); | |
49 } | |
50 return value.Pass(); | |
51 } | |
52 | |
53 } // namespace | |
54 | |
55 namespace invalidation { | |
56 | |
57 // static | |
58 void InvalidatorStorage::RegisterProfilePrefs( | |
59 user_prefs::PrefRegistrySyncable* registry) { | |
60 registry->RegisterListPref(prefs::kInvalidatorSavedInvalidations); | |
61 registry->RegisterStringPref(prefs::kInvalidatorInvalidationState, | |
62 std::string()); | |
63 registry->RegisterStringPref(prefs::kInvalidatorClientId, std::string()); | |
64 | |
65 // This pref is obsolete. We register it so we can clear it. | |
66 // At some point in the future, it will be safe to remove this. | |
67 registry->RegisterListPref(kInvalidatorMaxInvalidationVersions); | |
68 } | |
69 | |
70 // static | |
71 void InvalidatorStorage::RegisterPrefs(PrefRegistrySimple* registry) { | |
72 registry->RegisterListPref(prefs::kInvalidatorSavedInvalidations); | |
73 registry->RegisterStringPref(prefs::kInvalidatorInvalidationState, | |
74 std::string()); | |
75 registry->RegisterStringPref(prefs::kInvalidatorClientId, std::string()); | |
76 } | |
77 | |
78 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) | |
79 : pref_service_(pref_service) { | |
80 DCHECK(pref_service_); | |
81 if (pref_service_->FindPreference(kInvalidatorMaxInvalidationVersions)) | |
82 pref_service_->ClearPref(kInvalidatorMaxInvalidationVersions); | |
83 } | |
84 | |
85 InvalidatorStorage::~InvalidatorStorage() { | |
86 } | |
87 | |
88 void InvalidatorStorage::ClearAndSetNewClientId(const std::string& client_id) { | |
89 DCHECK(thread_checker_.CalledOnValidThread()); | |
90 Clear(); // We can't reuse our old invalidation state if the ID changes. | |
91 pref_service_->SetString(prefs::kInvalidatorClientId, client_id); | |
92 } | |
93 | |
94 std::string InvalidatorStorage::GetInvalidatorClientId() const { | |
95 return pref_service_->GetString(prefs::kInvalidatorClientId); | |
96 } | |
97 | |
98 void InvalidatorStorage::SetBootstrapData(const std::string& data) { | |
99 DCHECK(thread_checker_.CalledOnValidThread()); | |
100 std::string base64_data; | |
101 base::Base64Encode(data, &base64_data); | |
102 pref_service_->SetString(prefs::kInvalidatorInvalidationState, | |
103 base64_data); | |
104 } | |
105 | |
106 std::string InvalidatorStorage::GetBootstrapData() const { | |
107 std::string base64_data( | |
108 pref_service_->GetString(prefs::kInvalidatorInvalidationState)); | |
109 std::string data; | |
110 base::Base64Decode(base64_data, &data); | |
111 return data; | |
112 } | |
113 | |
114 void InvalidatorStorage::SetSavedInvalidations( | |
115 const syncer::UnackedInvalidationsMap& map) { | |
116 scoped_ptr<base::ListValue> value(UnackedInvalidationStorageMapToValue(map)); | |
117 pref_service_->Set(prefs::kInvalidatorSavedInvalidations, *value.get()); | |
118 } | |
119 | |
120 syncer::UnackedInvalidationsMap | |
121 InvalidatorStorage::GetSavedInvalidations() const { | |
122 syncer::UnackedInvalidationsMap map; | |
123 const base::ListValue* value = | |
124 pref_service_->GetList(prefs::kInvalidatorSavedInvalidations); | |
125 if (!ValueToUnackedInvalidationStorageMap(*value, &map)) { | |
126 return syncer::UnackedInvalidationsMap(); | |
127 } else { | |
128 return map; | |
129 } | |
130 } | |
131 | |
132 void InvalidatorStorage::Clear() { | |
133 DCHECK(thread_checker_.CalledOnValidThread()); | |
134 pref_service_->ClearPref(prefs::kInvalidatorSavedInvalidations); | |
135 pref_service_->ClearPref(prefs::kInvalidatorClientId); | |
136 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); | |
137 } | |
138 | |
139 } // namespace invalidation | |
OLD | NEW |