An accessibility tree update with two roots should be rejected.
[chromium-blink-merge.git] / components / invalidation / invalidator_storage.cc
blobcd7ae9ce1a722015e201d4f8e2585f466c6868ba
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/invalidator_storage.h"
7 #include <string>
8 #include <utility>
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"
22 namespace {
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;
36 return false;
38 map->insert(std::make_pair(storage.object_id(), storage));
40 return true;
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());
50 return value.Pass();
53 } // namespace
55 namespace invalidation {
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());
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);
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());
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,
103 base64_data);
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;
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();
127 } else {
128 return map;
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