summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/invalidations/invalidator_storage.cc
blob: 4ca9f2dc68109345609afc774cc44ce968cce015 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) 2012 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 "chrome/browser/sync/invalidations/invalidator_storage.h"

#include "base/base64.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/string_number_conversions.h"
#include "base/values.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/common/pref_names.h"
#include "sync/internal_api/public/syncable/model_type.h"

using csync::InvalidationVersionMap;

namespace browser_sync {

namespace {

const char kSourceKey[] = "source";
const char kNameKey[] = "name";
const char kMaxVersionKey[] = "max-version";

bool ValueToObjectIdAndVersion(const DictionaryValue& value,
                               invalidation::ObjectId* id,
                               int64* max_version) {
  std::string source_str;
  int source = 0;
  std::string name;
  std::string max_version_str;
  if (!value.GetString(kSourceKey, &source_str)) {
    DLOG(WARNING) << "Unable to deserialize source";
    return false;
  }
  if (!value.GetString(kNameKey, &name)) {
    DLOG(WARNING) << "Unable to deserialize name";
    return false;
  }
  if (!value.GetString(kMaxVersionKey, &max_version_str)) {
    DLOG(WARNING) << "Unable to deserialize max version";
    return false;
  }
  if (!base::StringToInt(source_str, &source)) {
    DLOG(WARNING) << "Invalid source: " << source_str;
    return false;
  }
  if (!base::StringToInt64(max_version_str, max_version)) {
    DLOG(WARNING) << "Invalid max invalidation version: " << max_version_str;
    return false;
  }
  *id = invalidation::ObjectId(source, name);
  return true;
}

// The caller owns the returned value.
DictionaryValue* ObjectIdAndVersionToValue(const invalidation::ObjectId& id,
                                           int64 max_version) {
  DictionaryValue* value = new DictionaryValue;
  value->SetString(kSourceKey, base::IntToString(id.source()));
  value->SetString(kNameKey, id.name());
  value->SetString(kMaxVersionKey, base::Int64ToString(max_version));
  return value;
}

}  // namespace

InvalidatorStorage::InvalidatorStorage(PrefService* pref_service)
    : pref_service_(pref_service) {
  // TODO(tim): Create a Mock instead of maintaining the if(!pref_service_) case
  // throughout this file.  This is a problem now due to lack of injection at
  // ProfileSyncService. Bug 130176.
  if (pref_service_) {
    pref_service_->RegisterListPref(prefs::kInvalidatorMaxInvalidationVersions,
                                    PrefService::UNSYNCABLE_PREF);
    pref_service_->RegisterStringPref(prefs::kInvalidatorInvalidationState,
                                      std::string(),
                                      PrefService::UNSYNCABLE_PREF);

    MigrateMaxInvalidationVersionsPref();
  }
}

InvalidatorStorage::~InvalidatorStorage() {
}

InvalidationVersionMap InvalidatorStorage::GetAllMaxVersions() const {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  InvalidationVersionMap max_versions;
  if (!pref_service_) {
    return max_versions;
  }
  const base::ListValue* max_versions_list =
      pref_service_->GetList(prefs::kInvalidatorMaxInvalidationVersions);
  CHECK(max_versions_list);
  DeserializeFromList(*max_versions_list, &max_versions);
  return max_versions;
}

void InvalidatorStorage::SetMaxVersion(const invalidation::ObjectId& id,
                                       int64 max_version) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  CHECK(pref_service_);
  InvalidationVersionMap max_versions = GetAllMaxVersions();
  InvalidationVersionMap::iterator it = max_versions.find(id);
  if ((it != max_versions.end()) && (max_version <= it->second)) {
    NOTREACHED();
    return;
  }
  max_versions[id] = max_version;

  base::ListValue max_versions_list;
  SerializeToList(max_versions, &max_versions_list);
  pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions,
                     max_versions_list);
}

// static
void InvalidatorStorage::DeserializeFromList(
    const base::ListValue& max_versions_list,
    InvalidationVersionMap* max_versions_map) {
  max_versions_map->clear();
  for (size_t i = 0; i < max_versions_list.GetSize(); ++i) {
    DictionaryValue* value = NULL;
    if (!max_versions_list.GetDictionary(i, &value)) {
      DLOG(WARNING) << "Unable to deserialize entry " << i;
      continue;
    }
    invalidation::ObjectId id;
    int64 max_version = 0;
    if (!ValueToObjectIdAndVersion(*value, &id, &max_version)) {
      DLOG(WARNING) << "Error while deserializing entry " << i;
      continue;
    }
    (*max_versions_map)[id] = max_version;
  }
}

// static
void InvalidatorStorage::SerializeToList(
    const InvalidationVersionMap& max_versions_map,
    base::ListValue* max_versions_list) {
  for (InvalidationVersionMap::const_iterator it = max_versions_map.begin();
       it != max_versions_map.end(); ++it) {
    max_versions_list->Append(ObjectIdAndVersionToValue(it->first, it->second));
  }
}

// Legacy migration code.
void InvalidatorStorage::MigrateMaxInvalidationVersionsPref() {
  pref_service_->RegisterDictionaryPref(prefs::kSyncMaxInvalidationVersions,
                                        PrefService::UNSYNCABLE_PREF);
  const base::DictionaryValue* max_versions_dict =
      pref_service_->GetDictionary(prefs::kSyncMaxInvalidationVersions);
  CHECK(max_versions_dict);
  if (!max_versions_dict->empty()) {
    InvalidationVersionMap max_versions;
    DeserializeMap(max_versions_dict, &max_versions);
    base::ListValue max_versions_list;
    SerializeToList(max_versions, &max_versions_list);
    pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions,
                       max_versions_list);
    UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref",
                          true);
  } else {
    UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref",
                          false);
  }
  pref_service_->ClearPref(prefs::kSyncMaxInvalidationVersions);
}

// Legacy migration code.
// static
void InvalidatorStorage::DeserializeMap(
    const base::DictionaryValue* max_versions_dict,
    InvalidationVersionMap* map) {
  map->clear();
  // Convert from a string -> string DictionaryValue to a
  // ModelType -> int64 map.
  for (base::DictionaryValue::key_iterator it =
           max_versions_dict->begin_keys();
       it != max_versions_dict->end_keys(); ++it) {
    int model_type_int = 0;
    if (!base::StringToInt(*it, &model_type_int)) {
      LOG(WARNING) << "Invalid model type key: " << *it;
      continue;
    }
    if ((model_type_int < syncable::FIRST_REAL_MODEL_TYPE) ||
        (model_type_int >= syncable::MODEL_TYPE_COUNT)) {
      LOG(WARNING) << "Out-of-range model type key: " << model_type_int;
      continue;
    }
    const syncable::ModelType model_type =
        syncable::ModelTypeFromInt(model_type_int);
    std::string max_version_str;
    CHECK(max_versions_dict->GetString(*it, &max_version_str));
    int64 max_version = 0;
    if (!base::StringToInt64(max_version_str, &max_version)) {
      LOG(WARNING) << "Invalid max invalidation version for "
                   << syncable::ModelTypeToString(model_type) << ": "
                   << max_version_str;
      continue;
    }
    invalidation::ObjectId id;
    if (!csync::RealModelTypeToObjectId(model_type, &id)) {
      DLOG(WARNING) << "Invalid model type: " << model_type;
      continue;
    }
    (*map)[id] = max_version;
  }
}

std::string InvalidatorStorage::GetInvalidationState() const {
  std::string utf8_state(pref_service_ ?
      pref_service_->GetString(prefs::kInvalidatorInvalidationState) : "");
  std::string state_data;
  base::Base64Decode(utf8_state, &state_data);
  return state_data;
}

void InvalidatorStorage::SetInvalidationState(const std::string& state) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  std::string utf8_state;
  base::Base64Encode(state, &utf8_state);
  pref_service_->SetString(prefs::kInvalidatorInvalidationState,
                           utf8_state);
}

void InvalidatorStorage::Clear() {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  pref_service_->ClearPref(prefs::kInvalidatorMaxInvalidationVersions);
  pref_service_->ClearPref(prefs::kInvalidatorInvalidationState);
}

}  // namespace browser_sync