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 dd7dd9ee39461c1ab30458238460c56914000f61..ebd6dbc74f447a0b4a9d8fa3387a543b182aa248 100644 |
--- a/chrome/browser/sync/invalidations/invalidator_storage.cc |
+++ b/chrome/browser/sync/invalidations/invalidator_storage.cc |
@@ -5,9 +5,13 @@ |
#include "chrome/browser/sync/invalidations/invalidator_storage.h" |
#include "base/base64.h" |
+#include "base/bind.h" |
+#include "base/callback.h" |
+#include "base/location.h" |
#include "base/logging.h" |
#include "base/metrics/histogram.h" |
#include "base/string_number_conversions.h" |
+#include "base/task_runner.h" |
#include "base/values.h" |
#include "chrome/browser/prefs/pref_service.h" |
#include "chrome/common/pref_names.h" |
@@ -22,35 +26,53 @@ namespace { |
const char kSourceKey[] = "source"; |
const char kNameKey[] = "name"; |
const char kMaxVersionKey[] = "max-version"; |
+const char kPayloadKey[] = "payload"; |
+const char kCurrentAckHandleKey[] = "current-ack"; |
+const char kExpectedAckHandleKey[] = "expected-ack"; |
bool ValueToObjectIdAndState(const DictionaryValue& value, |
invalidation::ObjectId* id, |
syncer::InvalidationState* state) { |
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; |
} |
+ int source = 0; |
+ if (!base::StringToInt(source_str, &source)) { |
+ DLOG(WARNING) << "Invalid source: " << source_str; |
+ return false; |
+ } |
+ std::string name; |
if (!value.GetString(kNameKey, &name)) { |
DLOG(WARNING) << "Unable to deserialize name"; |
return false; |
} |
+ *id = invalidation::ObjectId(source, name); |
+ std::string max_version_str; |
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, &state->version)) { |
DLOG(WARNING) << "Invalid max invalidation version: " << max_version_str; |
return false; |
} |
- *id = invalidation::ObjectId(source, name); |
+ value.GetString(kPayloadKey, &state->payload); |
+ // The ack handle fields won't be set if upgrading from previous versions of |
+ // Chrome. |
+ const base::DictionaryValue* current_ack_handle_value = NULL; |
+ if (value.GetDictionary(kCurrentAckHandleKey, ¤t_ack_handle_value)) { |
+ state->current.ResetFromValue(*current_ack_handle_value); |
+ } |
+ const base::DictionaryValue* expected_ack_handle_value = NULL; |
+ if (value.GetDictionary(kExpectedAckHandleKey, &expected_ack_handle_value)) { |
+ state->expected.ResetFromValue(*expected_ack_handle_value); |
+ } else { |
+ // In this case, we should never have a valid current value set. |
+ DCHECK(!state->current.IsValid()); |
+ state->current = syncer::AckHandle::InvalidAckHandle(); |
+ } |
return true; |
} |
@@ -61,6 +83,11 @@ DictionaryValue* ObjectIdAndStateToValue( |
value->SetString(kSourceKey, base::IntToString(id.source())); |
value->SetString(kNameKey, id.name()); |
value->SetString(kMaxVersionKey, base::Int64ToString(state.version)); |
+ value->SetString(kPayloadKey, state.payload); |
+ if (state.current.IsValid()) |
+ value->Set(kCurrentAckHandleKey, state.current.ToValue().release()); |
+ if (state.expected.IsValid()) |
+ value->Set(kExpectedAckHandleKey, state.expected.ToValue().release()); |
return value; |
} |
@@ -98,8 +125,10 @@ InvalidationStateMap InvalidatorStorage::GetAllInvalidationStates() const { |
return state_map; |
} |
-void InvalidatorStorage::SetMaxVersion(const invalidation::ObjectId& id, |
- int64 max_version) { |
+void InvalidatorStorage::SetMaxVersionAndPayload( |
+ const invalidation::ObjectId& id, |
+ int64 max_version, |
+ const std::string& payload) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
CHECK(pref_service_); |
InvalidationStateMap state_map = GetAllInvalidationStates(); |
@@ -109,6 +138,7 @@ void InvalidatorStorage::SetMaxVersion(const invalidation::ObjectId& id, |
return; |
} |
state_map[id].version = max_version; |
+ state_map[id].payload = payload; |
base::ListValue state_map_list; |
SerializeToList(state_map, &state_map_list); |
@@ -248,4 +278,47 @@ 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 = GetAllInvalidationStates(); |
+ |
+ 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); |
+ |
+ ignore_result(task_runner->PostTask(FROM_HERE, |
+ base::Bind(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 = GetAllInvalidationStates(); |
+ |
+ 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 |