Index: chrome/browser/sync/invalidations/invalidator_storage.cc |
diff --git a/chrome/browser/sync/invalidations/invalidator_storage.cc b/chrome/browser/sync/invalidations/invalidator_storage.cc |
index 186a7cd7c2658f00b445f6c6327ef15d9ab6c331..89c303e80715cc6bbd87ee1a9739e7716a4f40ec 100644 |
--- a/chrome/browser/sync/invalidations/invalidator_storage.cc |
+++ b/chrome/browser/sync/invalidations/invalidator_storage.cc |
@@ -4,16 +4,21 @@ |
#include "chrome/browser/sync/invalidations/invalidator_storage.h" |
+#include "base/bind.h" |
#include "base/base64.h" |
+#include "base/callback.h" |
+#include "base/location.h" |
#include "base/logging.h" |
+#include "base/task_runner.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/base/model_type.h" |
+#include "sync/notifier/invalidation_util.h" |
-using syncer::InvalidationVersionMap; |
+using syncer::InvalidationStateMap; |
namespace browser_sync { |
@@ -22,10 +27,13 @@ namespace { |
const char kSourceKey[] = "source"; |
const char kNameKey[] = "name"; |
const char kMaxVersionKey[] = "max-version"; |
+const char kCurrentAckHandleKey[] = "current-ack"; |
+const char kExpectedAckHandleKey[] = "expected-ack"; |
+// TODO(dcheng): Rename. |
akalin
2012/10/19 13:27:16
!
dcheng
2012/10/19 19:38:11
Done. I thought I updated this when I updated the
|
bool ValueToObjectIdAndVersion(const DictionaryValue& value, |
invalidation::ObjectId* id, |
- int64* max_version) { |
+ syncer::LocalState* state) { |
std::string source_str; |
int source = 0; |
std::string name; |
@@ -46,24 +54,46 @@ bool ValueToObjectIdAndVersion(const DictionaryValue& value, |
DLOG(WARNING) << "Invalid source: " << source_str; |
return false; |
} |
- if (!base::StringToInt64(max_version_str, max_version)) { |
+ if (!base::StringToInt64(max_version_str, &state->version)) { |
DLOG(WARNING) << "Invalid max invalidation version: " << max_version_str; |
return false; |
} |
*id = invalidation::ObjectId(source, name); |
+ // The following field is optional, as it may not always be set (for |
+ // example, when upgrading from previous versions of Chrome). |
+ const base::Value* current_ack_handle_value = NULL; |
+ if (value.Get(kCurrentAckHandleKey, ¤t_ack_handle_value)) { |
+ state->current.ResetFromValue(*current_ack_handle_value); |
+ } |
+ // TODO(dcheng): Consider regenerating the latest ack handle value on Chrome |
+ // startup to allow for format changes? |
+ const base::Value* expected_ack_handle_value = NULL; |
+ if (value.Get(kExpectedAckHandleKey, &expected_ack_handle_value)) { |
+ state->expected.ResetFromValue(*expected_ack_handle_value); |
+ } |
akalin
2012/10/19 13:27:16
does it make sense to have only one ack handle fie
dcheng
2012/10/19 19:38:11
It's possible to have an expected value but no cur
|
return true; |
} |
// The caller owns the returned value. |
-DictionaryValue* ObjectIdAndVersionToValue(const invalidation::ObjectId& id, |
- int64 max_version) { |
+DictionaryValue* ObjectIdAndStateToValue(const invalidation::ObjectId& id, |
+ const syncer::LocalState& state) { |
DictionaryValue* value = new DictionaryValue; |
value->SetString(kSourceKey, base::IntToString(id.source())); |
value->SetString(kNameKey, id.name()); |
- value->SetString(kMaxVersionKey, base::Int64ToString(max_version)); |
+ value->SetString(kMaxVersionKey, base::Int64ToString(state.version)); |
+ value->Set(kCurrentAckHandleKey, state.current.ToValue().release()); |
+ value->Set(kExpectedAckHandleKey, state.expected.ToValue().release()); |
return value; |
} |
+// Simple helper to allow us to bind the last parameter to a partial callback |
+// and call it on the right thread. |
+void GenerateAckHandleReplyHelper( |
akalin
2012/10/19 13:27:16
almost certain you can just rebind callback with a
dcheng
2012/10/19 19:38:11
Apparently base::Bind(callback, ack_handles) works
|
+ base::Callback<void(const syncer::AckHandleMap&)> callback, |
+ const syncer::AckHandleMap& ack_handles) { |
+ callback.Run(ack_handles); |
+} |
+ |
} // namespace |
InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) |
@@ -85,80 +115,80 @@ InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) |
InvalidatorStorage::~InvalidatorStorage() { |
} |
-InvalidationVersionMap InvalidatorStorage::GetAllMaxVersions() const { |
+InvalidationStateMap InvalidatorStorage::GetStateMap() const { |
DCHECK(thread_checker_.CalledOnValidThread()); |
- InvalidationVersionMap max_versions; |
+ InvalidationStateMap state; |
if (!pref_service_) { |
- return max_versions; |
+ return state; |
} |
- const base::ListValue* max_versions_list = |
+ const base::ListValue* state_map_list = |
pref_service_->GetList(prefs::kInvalidatorMaxInvalidationVersions); |
- CHECK(max_versions_list); |
- DeserializeFromList(*max_versions_list, &max_versions); |
- return max_versions; |
+ CHECK(state_map_list); |
+ DeserializeFromList(*state_map_list, &state); |
+ return state; |
} |
void InvalidatorStorage::SetMaxVersion(const invalidation::ObjectId& id, |
int64 max_version) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
CHECK(pref_service_); |
- InvalidationVersionMap max_versions = GetAllMaxVersions(); |
- InvalidationVersionMap::iterator it = max_versions.find(id); |
- if ((it != max_versions.end()) && (max_version <= it->second)) { |
+ InvalidationStateMap state_map = GetStateMap(); |
+ InvalidationStateMap::iterator it = state_map.find(id); |
+ if ((it != state_map.end()) && (max_version <= it->second.version)) { |
NOTREACHED(); |
return; |
} |
- max_versions[id] = max_version; |
+ state_map[id].version = max_version; |
- base::ListValue max_versions_list; |
- SerializeToList(max_versions, &max_versions_list); |
+ base::ListValue state_map_list; |
+ SerializeToList(state_map, &state_map_list); |
pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
- max_versions_list); |
+ state_map_list); |
} |
void InvalidatorStorage::Forget(const syncer::ObjectIdSet& ids) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
CHECK(pref_service_); |
- InvalidationVersionMap max_versions = GetAllMaxVersions(); |
+ InvalidationStateMap state_map = GetStateMap(); |
for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); |
++it) { |
- max_versions.erase(*it); |
+ state_map.erase(*it); |
} |
- base::ListValue max_versions_list; |
- SerializeToList(max_versions, &max_versions_list); |
+ base::ListValue state_map_list; |
+ SerializeToList(state_map, &state_map_list); |
pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
- max_versions_list); |
+ state_map_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) { |
+ const base::ListValue& state_map_list, |
+ InvalidationStateMap* state_map) { |
+ state_map->clear(); |
+ for (size_t i = 0; i < state_map_list.GetSize(); ++i) { |
const DictionaryValue* value = NULL; |
- if (!max_versions_list.GetDictionary(i, &value)) { |
+ if (!state_map_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)) { |
+ syncer::LocalState state; |
+ if (!ValueToObjectIdAndVersion(*value, &id, &state)) { |
DLOG(WARNING) << "Error while deserializing entry " << i; |
continue; |
} |
- (*max_versions_map)[id] = max_version; |
+ (*state_map)[id] = state; |
} |
} |
// 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)); |
+ const InvalidationStateMap& state_map, |
+ base::ListValue* state_map_list) { |
+ for (InvalidationStateMap::const_iterator it = state_map.begin(); |
+ it != state_map.end(); ++it) { |
+ state_map_list->Append(ObjectIdAndStateToValue(it->first, it->second)); |
} |
} |
@@ -170,12 +200,12 @@ void InvalidatorStorage::MigrateMaxInvalidationVersionsPref() { |
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); |
+ InvalidationStateMap state_map; |
+ DeserializeMap(max_versions_dict, &state_map); |
+ base::ListValue state_map_list; |
+ SerializeToList(state_map, &state_map_list); |
pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
- max_versions_list); |
+ state_map_list); |
UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref", |
true); |
} else { |
@@ -189,7 +219,7 @@ void InvalidatorStorage::MigrateMaxInvalidationVersionsPref() { |
// static |
void InvalidatorStorage::DeserializeMap( |
const base::DictionaryValue* max_versions_dict, |
- InvalidationVersionMap* map) { |
+ InvalidationStateMap* map) { |
map->clear(); |
// Convert from a string -> string DictionaryValue to a |
// ModelType -> int64 map. |
@@ -222,7 +252,7 @@ void InvalidatorStorage::DeserializeMap( |
DLOG(WARNING) << "Invalid model type: " << model_type; |
continue; |
} |
- (*map)[id] = max_version; |
+ (*map)[id].version = max_version; |
} |
} |
@@ -248,4 +278,48 @@ void InvalidatorStorage::Clear() { |
pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); |
} |
+void InvalidatorStorage::GenerateAckHandles( |
+ const syncer::ObjectIdSet& ids, |
+ const scoped_refptr<base::TaskRunner>& task_runner, |
+ const base::Callback<void(const syncer::AckHandleMap&)> callback) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ CHECK(pref_service_); |
+ InvalidationStateMap state_map = GetStateMap(); |
+ |
+ syncer::AckHandleMap ack_handles; |
+ for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); |
+ ++it) { |
+ state_map[*it].expected = syncer::AckHandle::CreateUnique(); |
+ ack_handles.insert(std::make_pair(*it, state_map[*it].expected)); |
+ } |
+ |
+ base::ListValue state_map_list; |
+ SerializeToList(state_map, &state_map_list); |
+ pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
+ state_map_list); |
+ |
+ task_runner->PostTask(FROM_HERE, |
+ base::Bind(&GenerateAckHandleReplyHelper, |
+ callback, ack_handles)); |
+} |
+ |
+void InvalidatorStorage::Acknowledge(const invalidation::ObjectId& id, |
+ const syncer::AckHandle& ack_handle) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ CHECK(pref_service_); |
+ InvalidationStateMap state_map = GetStateMap(); |
+ |
+ InvalidationStateMap::iterator it = state_map.find(id); |
+ // This could happen if the acknowledgement is delayed and Forget() has |
+ // already been called. |
+ if (it == state_map.end()) |
+ return; |
+ it->second.current = ack_handle; |
+ |
+ base::ListValue state_map_list; |
+ SerializeToList(state_map, &state_map_list); |
+ pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
+ state_map_list); |
+} |
+ |
} // namespace browser_sync |