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.
5 #include "components/invalidation/impl/invalidator_storage.h"
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/impl/invalidation_prefs.h"
18 #include "components/invalidation/impl/unacked_invalidation_set.h"
19 #include "components/pref_registry/pref_registry_syncable.h"
20 #include "google/cacheinvalidation/types.pb.h"
24 const char kInvalidatorMaxInvalidationVersions
[] =
25 "invalidator.max_invalidation_versions";
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
;
38 map
->insert(std::make_pair(storage
.object_id(), storage
));
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());
55 namespace invalidation
{
58 void InvalidatorStorage::RegisterProfilePrefs(
59 user_prefs::PrefRegistrySyncable
* registry
) {
60 registry
->RegisterListPref(prefs::kInvalidatorSavedInvalidations
);
61 registry
->RegisterStringPref(prefs::kInvalidatorInvalidationState
,
63 registry
->RegisterStringPref(prefs::kInvalidatorClientId
, std::string());
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
);
71 void InvalidatorStorage::RegisterPrefs(PrefRegistrySimple
* registry
) {
72 registry
->RegisterListPref(prefs::kInvalidatorSavedInvalidations
);
73 registry
->RegisterStringPref(prefs::kInvalidatorInvalidationState
,
75 registry
->RegisterStringPref(prefs::kInvalidatorClientId
, std::string());
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
);
85 InvalidatorStorage::~InvalidatorStorage() {
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
);
94 std::string
InvalidatorStorage::GetInvalidatorClientId() const {
95 return pref_service_
->GetString(prefs::kInvalidatorClientId
);
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
,
106 std::string
InvalidatorStorage::GetBootstrapData() const {
107 std::string
base64_data(
108 pref_service_
->GetString(prefs::kInvalidatorInvalidationState
));
110 base::Base64Decode(base64_data
, &data
);
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());
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();
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
);
139 } // namespace invalidation